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