[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.21

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