[BACK]Return to rpc_hout.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rpcgen

Annotation of src/usr.bin/rpcgen/rpc_hout.c, Revision 1.18

1.18    ! deraadt     1: /*     $OpenBSD: rpc_hout.c,v 1.17 2007/10/03 14:35:48 weingart Exp $  */
1.1       deraadt     2: /*     $NetBSD: rpc_hout.c,v 1.4 1995/06/11 21:49:55 pk Exp $  */
                      3: /*
                      4:  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
                      5:  * unrestricted use provided that this legend is included on all tape
                      6:  * media and as a part of the software program in whole or part.  Users
                      7:  * may copy or modify Sun RPC without charge, but are not authorized
                      8:  * to license or distribute it to anyone else except as part of a product or
                      9:  * program developed by the user or with the express written consent of
                     10:  * Sun Microsystems, Inc.
                     11:  *
                     12:  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
                     13:  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
                     14:  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
                     15:  *
                     16:  * Sun RPC is provided with no support and without any obligation on the
                     17:  * part of Sun Microsystems, Inc. to assist in its use, correction,
                     18:  * modification or enhancement.
                     19:  *
                     20:  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
                     21:  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
                     22:  * OR ANY PART THEREOF.
                     23:  *
                     24:  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
                     25:  * or profits or other special, indirect and consequential damages, even if
                     26:  * Sun has been advised of the possibility of such damages.
                     27:  *
                     28:  * Sun Microsystems, Inc.
                     29:  * 2550 Garcia Avenue
                     30:  * Mountain View, California  94043
                     31:  */
                     32:
                     33: /*
1.9       deraadt    34:  * rpc_hout.c, Header file outputter for the RPC protocol compiler
1.1       deraadt    35:  */
                     36: #include <sys/cdefs.h>
                     37: #include <stdio.h>
1.15      david      38: #include <stdlib.h>
1.1       deraadt    39: #include <ctype.h>
                     40: #include "rpc_parse.h"
                     41: #include "rpc_util.h"
                     42:
1.10      millert    43: static void pconstdef(definition *);
                     44: static void pargdef(definition *);
                     45: static void pstructdef(definition *);
                     46: static void puniondef(definition *);
                     47: static void pprogramdef(definition *);
                     48: static void penumdef(definition *);
                     49: static void ptypedef(definition *);
                     50: static void pdefine(char *, char *);
                     51: static void puldefine(char *, char *);
                     52: static int define_printed(proc_list *, version_list *);
                     53: static int undefined2(char *, char *);
                     54: static void parglist(proc_list *, char *);
1.9       deraadt    55: void pxdrfuncdecl(char *, int);
                     56: void pprocdef(proc_list *, version_list *, char *, int, int);
                     57: void pdeclaration(char *, declaration *, int, char *);
1.1       deraadt    58:
                     59: /*
1.9       deraadt    60:  * Print the C-version of an xdr definition
1.1       deraadt    61:  */
                     62: void
                     63: print_datadef(def)
                     64:        definition *def;
                     65: {
                     66:
1.9       deraadt    67:        if (def->def_kind == DEF_PROGRAM)  /* handle data only */
1.12      deraadt    68:                return;
1.1       deraadt    69:
1.9       deraadt    70:        if (def->def_kind != DEF_CONST)
1.11      deraadt    71:                fprintf(fout, "\n");
1.1       deraadt    72:        switch (def->def_kind) {
                     73:        case DEF_STRUCT:
                     74:                pstructdef(def);
                     75:                break;
                     76:        case DEF_UNION:
                     77:                puniondef(def);
                     78:                break;
                     79:        case DEF_ENUM:
                     80:                penumdef(def);
                     81:                break;
                     82:        case DEF_TYPEDEF:
                     83:                ptypedef(def);
                     84:                break;
                     85:        case DEF_PROGRAM:
                     86:                pprogramdef(def);
                     87:                break;
                     88:        case DEF_CONST:
                     89:                pconstdef(def);
                     90:                break;
                     91:        }
                     92:        if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
1.12      deraadt    93:                pxdrfuncdecl(def->def_name,
1.9       deraadt    94:                    def->def_kind != DEF_TYPEDEF ||
                     95:                    !isvectordef(def->def.ty.old_type, def->def.ty.rel));
1.1       deraadt    96:        }
                     97: }
                     98:
                     99:
                    100: void
                    101: print_funcdef(def)
                    102:        definition *def;
                    103: {
                    104:        switch (def->def_kind) {
                    105:        case DEF_PROGRAM:
1.11      deraadt   106:                fprintf(fout, "\n");
1.1       deraadt   107:                pprogramdef(def);
                    108:                break;
1.9       deraadt   109:        }
1.1       deraadt   110: }
                    111:
1.9       deraadt   112: void
                    113: pxdrfuncdecl(name, pointerp)
                    114:        char *name;
                    115:        int pointerp;
1.1       deraadt   116: {
                    117:
1.11      deraadt   118:        fprintf(fout,"#ifdef __cplusplus\n");
1.14      deraadt   119:        fprintf(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s %s);\n",
1.9       deraadt   120:            name, name, pointerp ? ("*") : "");
1.11      deraadt   121:        fprintf(fout,"#elif defined(__STDC__)\n");
1.14      deraadt   122:        fprintf(fout, "extern bool_t xdr_%s(XDR *, %s %s);\n",
1.9       deraadt   123:            name, name, pointerp ? ("*") : "");
1.11      deraadt   124:        fprintf(fout,"#else /* Old Style C */\n");
                    125:        fprintf(fout, "bool_t xdr_%s();\n", name);
                    126:        fprintf(fout,"#endif /* Old Style C */\n\n");
1.1       deraadt   127: }
                    128:
                    129:
1.9       deraadt   130: static void
1.1       deraadt   131: pconstdef(def)
                    132:        definition *def;
                    133: {
                    134:        pdefine(def->def_name, def->def.co);
                    135: }
                    136:
1.12      deraadt   137: /*
                    138:  * print out the definitions for the arguments of functions in the
                    139:  * header file
                    140:  */
1.9       deraadt   141: static void
1.1       deraadt   142: pargdef(def)
                    143:        definition *def;
                    144: {
                    145:        decl_list *l;
                    146:        version_list *vers;
                    147:        char *name;
                    148:        proc_list *plist;
                    149:
                    150:        for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
1.12      deraadt   151:                for (plist = vers->procs; plist != NULL;
1.9       deraadt   152:                    plist = plist->next) {
                    153:                        if (!newstyle || plist->arg_num < 2) {
                    154:                                continue; /* old style or single args */
                    155:                        }
                    156:                        name = plist->args.argname;
1.11      deraadt   157:                        fprintf(fout, "struct %s {\n", name);
1.9       deraadt   158:                        for (l = plist->args.decls;
1.12      deraadt   159:                            l != NULL; l = l->next) {
1.9       deraadt   160:                                pdeclaration(name, &l->decl, 1, ";\n");
1.1       deraadt   161:                        }
1.11      deraadt   162:                        fprintf(fout, "};\n");
                    163:                        fprintf(fout, "typedef struct %s %s;\n", name, name);
1.9       deraadt   164:                        pxdrfuncdecl(name, NULL);
1.11      deraadt   165:                        fprintf(fout, "\n");
1.1       deraadt   166:                }
1.9       deraadt   167:        }
1.1       deraadt   168: }
                    169:
1.9       deraadt   170: static void
1.1       deraadt   171: pstructdef(def)
                    172:        definition *def;
                    173: {
1.9       deraadt   174:        char *name = def->def_name;
1.8       deraadt   175:        decl_list *l;
1.1       deraadt   176:
1.11      deraadt   177:        fprintf(fout, "struct %s {\n", name);
1.9       deraadt   178:        for (l = def->def.st.decls; l != NULL; l = l->next)
1.1       deraadt   179:                pdeclaration(name, &l->decl, 1, ";\n");
1.11      deraadt   180:        fprintf(fout, "};\n");
                    181:        fprintf(fout, "typedef struct %s %s;\n", name, name);
1.1       deraadt   182: }
                    183:
1.9       deraadt   184: static void
1.1       deraadt   185: puniondef(def)
                    186:        definition *def;
                    187: {
                    188:        case_list *l;
                    189:        char *name = def->def_name;
                    190:        declaration *decl;
                    191:
1.11      deraadt   192:        fprintf(fout, "struct %s {\n", name);
1.1       deraadt   193:        decl = &def->def.un.enum_decl;
                    194:        if (streq(decl->type, "bool")) {
1.11      deraadt   195:                fprintf(fout, "\tbool_t %s;\n", decl->name);
1.1       deraadt   196:        } else {
1.11      deraadt   197:                fprintf(fout, "\t%s %s;\n", decl->type, decl->name);
1.1       deraadt   198:        }
1.11      deraadt   199:        fprintf(fout, "\tunion {\n");
1.1       deraadt   200:        for (l = def->def.un.cases; l != NULL; l = l->next) {
1.9       deraadt   201:          if (l->contflag == 0)
                    202:                pdeclaration(name, &l->case_decl, 2, ";\n");
1.1       deraadt   203:        }
                    204:        decl = def->def.un.default_decl;
                    205:        if (decl && !streq(decl->type, "void")) {
1.9       deraadt   206:                pdeclaration(name, decl, 2, ";\n");
1.1       deraadt   207:        }
1.11      deraadt   208:        fprintf(fout, "\t} %s_u;\n", name);
                    209:        fprintf(fout, "};\n");
                    210:        fprintf(fout, "typedef struct %s %s;\n", name, name);
1.1       deraadt   211: }
                    212:
1.9       deraadt   213: static void
1.1       deraadt   214: pdefine(name, num)
                    215:        char *name;
                    216:        char *num;
                    217: {
1.11      deraadt   218:        fprintf(fout, "#define %s %s\n", name, num);
1.1       deraadt   219: }
                    220:
1.9       deraadt   221: static void
1.1       deraadt   222: puldefine(name, num)
                    223:        char *name;
                    224:        char *num;
                    225: {
1.11      deraadt   226:        fprintf(fout, "#define %s ((u_long)%s)\n", name, num);
1.1       deraadt   227: }
                    228:
1.9       deraadt   229: static int
1.1       deraadt   230: define_printed(stop, start)
                    231:        proc_list *stop;
                    232:        version_list *start;
                    233: {
                    234:        version_list *vers;
                    235:        proc_list *proc;
                    236:
                    237:        for (vers = start; vers != NULL; vers = vers->next) {
                    238:                for (proc = vers->procs; proc != NULL; proc = proc->next) {
                    239:                        if (proc == stop) {
                    240:                                return (0);
                    241:                        } else if (streq(proc->proc_name, stop->proc_name)) {
                    242:                                return (1);
                    243:                        }
                    244:                }
                    245:        }
                    246:        abort();
                    247:        /* NOTREACHED */
                    248: }
                    249:
1.9       deraadt   250: static void
1.1       deraadt   251: pprogramdef(def)
                    252:        definition *def;
                    253: {
                    254:        version_list *vers;
                    255:        proc_list *proc;
                    256:        int i;
                    257:        char *ext;
1.9       deraadt   258:
1.1       deraadt   259:        pargdef(def);
                    260:
                    261:        puldefine(def->def_name, def->def.pr.prog_num);
                    262:        for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
                    263:                if (tblflag) {
1.11      deraadt   264:                        fprintf(fout, "extern struct rpcgen_table %s_%s_table[];\n",
1.9       deraadt   265:                            locase(def->def_name), vers->vers_num);
1.11      deraadt   266:                        fprintf(fout, "extern %s_%s_nproc;\n",
1.9       deraadt   267:                            locase(def->def_name), vers->vers_num);
1.1       deraadt   268:                }
                    269:                puldefine(vers->vers_name, vers->vers_num);
                    270:
1.9       deraadt   271:                /*
                    272:                 * Print out 3 definitions, one for ANSI-C, another for C++,
                    273:                 * a third for old style C
1.1       deraadt   274:                 */
1.12      deraadt   275:                for (i=0; i<3; i++) {
1.9       deraadt   276:                        if (i==0) {
1.11      deraadt   277:                                fprintf(fout,"\n#ifdef __cplusplus\n");
1.9       deraadt   278:                                ext = "extern \"C\" ";
                    279:                        } else if (i==1) {
1.11      deraadt   280:                                fprintf(fout,"\n#elif defined(__STDC__)\n");
1.13      deraadt   281:                                ext = "extern ";
1.9       deraadt   282:                        } else {
1.11      deraadt   283:                                fprintf(fout,"\n#else /* Old Style C */\n");
1.13      deraadt   284:                                ext = "extern ";
1.1       deraadt   285:                        }
1.9       deraadt   286:
1.1       deraadt   287:                        for (proc = vers->procs; proc != NULL; proc = proc->next) {
1.9       deraadt   288:                                if (!define_printed(proc, def->def.pr.versions))
1.1       deraadt   289:                                        puldefine(proc->proc_name, proc->proc_num);
1.11      deraadt   290:                                fprintf(fout,"%s",ext);
1.1       deraadt   291:                                pprocdef(proc, vers, "CLIENT *", 0,i);
1.11      deraadt   292:                                fprintf(fout,"%s",ext);
1.1       deraadt   293:                                pprocdef(proc, vers, "struct svc_req *", 1,i);
                    294:                        }
                    295:                }
1.11      deraadt   296:                fprintf(fout,"#endif /* Old Style C */\n");
1.1       deraadt   297:        }
                    298: }
                    299:
1.9       deraadt   300: void
1.1       deraadt   301: pprocdef(proc, vp, addargtype, server_p,mode)
                    302:        proc_list *proc;
                    303:        version_list *vp;
1.9       deraadt   304:        char *addargtype;
1.1       deraadt   305:        int server_p;
                    306:        int mode;
                    307: {
                    308:
1.9       deraadt   309:        ptype(proc->res_prefix, proc->res_type, 1);
1.11      deraadt   310:        fprintf(fout, "* ");
1.9       deraadt   311:        if (server_p)
1.1       deraadt   312:                pvname_svc(proc->proc_name, vp->vers_num);
                    313:        else
                    314:                pvname(proc->proc_name, vp->vers_num);
                    315:
                    316:        /*
1.9       deraadt   317:         * mode  0 == cplusplus, mode  1 = ANSI-C, mode 2 = old style C
1.1       deraadt   318:         */
1.9       deraadt   319:        if (mode == 0 || mode == 1)
                    320:                parglist(proc, addargtype);
1.1       deraadt   321:        else
1.11      deraadt   322:                fprintf(fout, "();\n");
1.1       deraadt   323: }
                    324:
                    325: /* print out argument list of procedure */
1.9       deraadt   326: static void
1.1       deraadt   327: parglist(proc, addargtype)
                    328:        proc_list *proc;
1.9       deraadt   329:        char *addargtype;
1.1       deraadt   330: {
                    331:        decl_list *dl;
                    332:
1.11      deraadt   333:        fprintf(fout,"(");
1.1       deraadt   334:
1.9       deraadt   335:        if (proc->arg_num < 2 && newstyle &&
                    336:           streq(proc->args.decls->decl.type, "void")) {
1.1       deraadt   337:                /* 0 argument in new style:  do nothing */
                    338:        } else {
                    339:                for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
1.9       deraadt   340:                        ptype(dl->decl.prefix, dl->decl.type, 1);
                    341:                        if (!newstyle)
1.11      deraadt   342:                                fprintf(fout, "*"); /* old style passes by reference */
                    343:                        fprintf(fout, ", ");
1.1       deraadt   344:                }
                    345:        }
1.11      deraadt   346:        fprintf(fout, "%s);\n", addargtype);
1.1       deraadt   347: }
                    348:
1.9       deraadt   349: static void
1.1       deraadt   350: penumdef(def)
                    351:        definition *def;
                    352: {
                    353:        char *name = def->def_name;
                    354:        enumval_list *l;
                    355:        char *last = NULL;
                    356:        int count = 0;
                    357:
1.11      deraadt   358:        fprintf(fout, "enum %s {\n", name);
1.1       deraadt   359:        for (l = def->def.en.vals; l != NULL; l = l->next) {
1.11      deraadt   360:                fprintf(fout, "\t%s", l->name);
1.1       deraadt   361:                if (l->assignment) {
1.11      deraadt   362:                        fprintf(fout, " = %s", l->assignment);
1.1       deraadt   363:                        last = l->assignment;
                    364:                        count = 1;
                    365:                } else {
                    366:                        if (last == NULL) {
1.11      deraadt   367:                                fprintf(fout, " = %d", count++);
1.1       deraadt   368:                        } else {
1.11      deraadt   369:                                fprintf(fout, " = %s + %d", last, count++);
1.1       deraadt   370:                        }
                    371:                }
1.3       tholo     372:                if (l->next)
1.11      deraadt   373:                        fprintf(fout, ",\n");
1.3       tholo     374:                else
1.11      deraadt   375:                        fprintf(fout, "\n");
1.1       deraadt   376:        }
1.11      deraadt   377:        fprintf(fout, "};\n");
                    378:        fprintf(fout, "typedef enum %s %s;\n", name, name);
1.1       deraadt   379: }
                    380:
1.9       deraadt   381: static void
1.1       deraadt   382: ptypedef(def)
                    383:        definition *def;
                    384: {
                    385:        char *name = def->def_name;
                    386:        char *old = def->def.ty.old_type;
                    387:        char prefix[8]; /* enough to contain "struct ", including NUL */
                    388:        relation rel = def->def.ty.rel;
                    389:
                    390:        if (!streq(name, old)) {
                    391:                if (streq(old, "string")) {
                    392:                        old = "char";
                    393:                        rel = REL_POINTER;
                    394:                } else if (streq(old, "opaque")) {
                    395:                        old = "char";
                    396:                } else if (streq(old, "bool")) {
                    397:                        old = "bool_t";
                    398:                }
                    399:                if (undefined2(old, name) && def->def.ty.old_prefix) {
1.11      deraadt   400:                        snprintf(prefix, sizeof prefix, "%s ", def->def.ty.old_prefix);
1.1       deraadt   401:                } else {
                    402:                        prefix[0] = 0;
                    403:                }
1.11      deraadt   404:                fprintf(fout, "typedef ");
1.1       deraadt   405:                switch (rel) {
                    406:                case REL_ARRAY:
1.11      deraadt   407:                        fprintf(fout, "struct {\n");
                    408:                        fprintf(fout, "\tu_int %s_len;\n", name);
                    409:                        fprintf(fout, "\t%s%s *%s_val;\n", prefix, old, name);
                    410:                        fprintf(fout, "} %s", name);
1.1       deraadt   411:                        break;
                    412:                case REL_POINTER:
1.11      deraadt   413:                        fprintf(fout, "%s%s *%s", prefix, old, name);
1.1       deraadt   414:                        break;
                    415:                case REL_VECTOR:
1.11      deraadt   416:                        fprintf(fout, "%s%s %s[%s]", prefix, old, name,
1.1       deraadt   417:                                def->def.ty.array_max);
                    418:                        break;
                    419:                case REL_ALIAS:
1.11      deraadt   420:                        fprintf(fout, "%s%s %s", prefix, old, name);
1.1       deraadt   421:                        break;
                    422:                }
1.11      deraadt   423:                fprintf(fout, ";\n");
1.1       deraadt   424:        }
                    425: }
                    426:
1.9       deraadt   427: void
1.1       deraadt   428: pdeclaration(name, dec, tab, separator)
                    429:        char *name;
                    430:        declaration *dec;
                    431:        int tab;
1.12      deraadt   432:        char *separator;
1.1       deraadt   433: {
                    434:        char buf[8];    /* enough to hold "struct ", include NUL */
                    435:        char *prefix;
                    436:        char *type;
                    437:
1.9       deraadt   438:        if (streq(dec->type, "void"))
1.1       deraadt   439:                return;
                    440:        tabify(fout, tab);
                    441:        if (streq(dec->type, name) && !dec->prefix) {
1.11      deraadt   442:                fprintf(fout, "struct ");
1.1       deraadt   443:        }
                    444:        if (streq(dec->type, "string")) {
1.11      deraadt   445:                fprintf(fout, "char *%s", dec->name);
1.1       deraadt   446:        } else {
                    447:                prefix = "";
                    448:                if (streq(dec->type, "bool")) {
                    449:                        type = "bool_t";
                    450:                } else if (streq(dec->type, "opaque")) {
                    451:                        type = "char";
                    452:                } else {
                    453:                        if (dec->prefix) {
1.11      deraadt   454:                                snprintf(buf, sizeof buf, "%s ", dec->prefix);
1.1       deraadt   455:                                prefix = buf;
                    456:                        }
                    457:                        type = dec->type;
                    458:                }
                    459:                switch (dec->rel) {
                    460:                case REL_ALIAS:
1.11      deraadt   461:                        fprintf(fout, "%s%s %s", prefix, type, dec->name);
1.1       deraadt   462:                        break;
                    463:                case REL_VECTOR:
1.11      deraadt   464:                        fprintf(fout, "%s%s %s[%s]", prefix, type, dec->name,
1.1       deraadt   465:                                dec->array_max);
                    466:                        break;
                    467:                case REL_POINTER:
1.11      deraadt   468:                        fprintf(fout, "%s%s *%s", prefix, type, dec->name);
1.1       deraadt   469:                        break;
                    470:                case REL_ARRAY:
1.11      deraadt   471:                        fprintf(fout, "struct {\n");
1.1       deraadt   472:                        tabify(fout, tab);
1.11      deraadt   473:                        fprintf(fout, "\tu_int %s_len;\n", dec->name);
1.1       deraadt   474:                        tabify(fout, tab);
1.11      deraadt   475:                        fprintf(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
1.1       deraadt   476:                        tabify(fout, tab);
1.11      deraadt   477:                        fprintf(fout, "} %s", dec->name);
1.1       deraadt   478:                        break;
                    479:                }
                    480:        }
1.16      grange    481:        fprintf(fout, "%s", separator);
1.1       deraadt   482: }
                    483:
1.9       deraadt   484: static int
1.1       deraadt   485: undefined2(type, stop)
                    486:        char *type;
                    487:        char *stop;
                    488: {
                    489:        list *l;
                    490:        definition *def;
                    491:
                    492:        for (l = defined; l != NULL; l = l->next) {
                    493:                def = (definition *) l->val;
                    494:                if (def->def_kind != DEF_PROGRAM) {
                    495:                        if (streq(def->def_name, stop)) {
                    496:                                return (1);
                    497:                        } else if (streq(def->def_name, type)) {
                    498:                                return (0);
                    499:                        }
                    500:                }
                    501:        }
                    502:        return (1);
                    503: }