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