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