2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * From: @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
38 * rpc_hout.c, Header file outputter for the RPC protocol compiler
42 #include "rpc_parse.h"
46 static void pconstdef(definition *def);
47 static void pargdef(definition *def);
48 static void pstructdef(definition *def);
49 static void puniondef(definition *def);
50 static void pdefine(const char *name, const char *num);
51 static void puldefine(const char *name, const char *num);
52 static int define_printed(proc_list *stop, version_list *start);
53 static void pprogramdef(definition *def);
54 static void parglist(proc_list *proc, const char *addargtype);
55 static void penumdef(definition *def);
56 static void ptypedef(definition *def);
57 static int undefined2(const char *type, const char *stop);
60 * Print the C-version of an xdr definition
63 print_datadef(definition *def)
66 if (def->def_kind == DEF_PROGRAM ) /* handle data only */
69 if (def->def_kind != DEF_CONST) {
72 switch (def->def_kind) {
92 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
93 pxdrfuncdecl( def->def_name,
94 def->def_kind != DEF_TYPEDEF ||
95 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
102 print_funcdef(definition *def)
104 switch (def->def_kind) {
110 /* ?... shouldn't happen I guess */
115 pxdrfuncdecl(const char *name, int pointerp)
118 f_print(fout,"#ifdef __cplusplus \n");
119 f_print(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
120 f_print(fout,"#elif __STDC__ \n");
121 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
122 f_print(fout,"#else /* Old Style C */ \n");
123 f_print(fout, "bool_t xdr_%s();\n", name);
124 f_print(fout,"#endif /* Old Style C */ \n\n");
129 pconstdef(definition *def)
131 pdefine(def->def_name, def->def.co);
134 /* print out the definitions for the arguments of functions in the
138 pargdef(definition *def)
146 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
147 for(plist = vers->procs; plist != NULL;
148 plist = plist->next) {
150 if (!newstyle || plist->arg_num < 2) {
151 continue; /* old style or single args */
153 name = plist->args.argname;
154 f_print(fout, "struct %s {\n", name);
155 for (l = plist->args.decls;
156 l != NULL; l = l->next) {
157 pdeclaration(name, &l->decl, 1, ";\n" );
159 f_print(fout, "};\n");
160 f_print(fout, "typedef struct %s %s;\n", name, name);
161 pxdrfuncdecl(name, 0);
162 f_print( fout, "\n" );
170 pstructdef(definition *def)
173 const char *name = def->def_name;
175 f_print(fout, "struct %s {\n", name);
176 for (l = def->def.st.decls; l != NULL; l = l->next) {
177 pdeclaration(name, &l->decl, 1, ";\n");
179 f_print(fout, "};\n");
180 f_print(fout, "typedef struct %s %s;\n", name, name);
184 puniondef(definition *def)
187 const char *name = def->def_name;
190 f_print(fout, "struct %s {\n", name);
191 decl = &def->def.un.enum_decl;
192 if (streq(decl->type, "bool")) {
193 f_print(fout, "\tbool_t %s;\n", decl->name);
195 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
197 f_print(fout, "\tunion {\n");
198 for (l = def->def.un.cases; l != NULL; l = l->next) {
200 pdeclaration(name, &l->case_decl, 2, ";\n" );
202 decl = def->def.un.default_decl;
203 if (decl && !streq(decl->type, "void")) {
204 pdeclaration(name, decl, 2, ";\n" );
206 f_print(fout, "\t} %s_u;\n", name);
207 f_print(fout, "};\n");
208 f_print(fout, "typedef struct %s %s;\n", name, name);
212 pdefine(const char *name, const char *num)
214 f_print(fout, "#define %s %s\n", name, num);
218 puldefine(const char *name, const char *num)
220 f_print(fout, "#define %s ((u_long)%s)\n", name, num);
224 define_printed(proc_list *stop, version_list *start)
229 for (vers = start; vers != NULL; vers = vers->next) {
230 for (proc = vers->procs; proc != NULL; proc = proc->next) {
233 } else if (streq(proc->proc_name, stop->proc_name)) {
243 pprogramdef(definition *def)
252 puldefine(def->def_name, def->def.pr.prog_num);
253 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
255 f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
256 locase(def->def_name), vers->vers_num);
257 f_print(fout, "extern %s_%s_nproc;\n",
258 locase(def->def_name), vers->vers_num);
260 puldefine(vers->vers_name, vers->vers_num);
263 * Print out 3 definitions, one for ANSI-C, another for C++,
264 * a third for old style C
269 f_print(fout,"\n#ifdef __cplusplus\n");
273 f_print(fout,"\n#elif __STDC__\n");
277 f_print(fout,"\n#else /* Old Style C */ \n");
282 for (proc = vers->procs; proc != NULL; proc = proc->next) {
283 if (!define_printed(proc, def->def.pr.versions)) {
284 puldefine(proc->proc_name, proc->proc_num);
286 f_print(fout,"%s",ext);
287 pprocdef(proc, vers, "CLIENT *", 0,i);
288 f_print(fout,"%s",ext);
289 pprocdef(proc, vers, "struct svc_req *", 1,i);
294 f_print(fout,"#endif /* Old Style C */ \n");
299 pprocdef(proc_list *proc, version_list *vp,
300 const char *addargtype, int server_p, int mode)
306 ptype( proc->res_prefix, proc->res_type, 1 );
307 f_print( fout, "* " );
309 pvname_svc(proc->proc_name, vp->vers_num);
311 pvname(proc->proc_name, vp->vers_num);
314 * mode 0 == cplusplus, mode 1 = ANSI-C, mode 2 = old style C
316 if(mode == 0 || mode ==1)
317 parglist(proc, addargtype);
319 f_print(fout, "();\n");
327 /* print out argument list of procedure */
329 parglist(proc_list *proc, const char *addargtype)
335 if( proc->arg_num < 2 && newstyle &&
336 streq( proc->args.decls->decl.type, "void")) {
337 /* 0 argument in new style: do nothing */
339 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
340 ptype( dl->decl.prefix, dl->decl.type, 1 );
342 f_print( fout, "*" ); /* old style passes by reference */
344 f_print( fout, ", " );
348 f_print(fout, "%s);\n", addargtype);
353 penumdef(definition *def)
355 const char *name = def->def_name;
357 const char *last = NULL;
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);
364 f_print(fout, " = %s", l->assignment);
365 last = l->assignment;
369 f_print(fout, " = %d", count++);
371 f_print(fout, " = %s + %d", last, count++);
374 f_print(fout, ",\n");
376 f_print(fout, "};\n");
377 f_print(fout, "typedef enum %s %s;\n", name, name);
381 ptypedef(definition *def)
383 const char *name = def->def_name;
384 const char *old = def->def.ty.old_type;
385 char prefix[8]; /* enough to contain "struct ", including NUL */
386 relation rel = def->def.ty.rel;
389 if (!streq(name, old)) {
390 if (streq(old, "string")) {
393 } else if (streq(old, "opaque")) {
395 } else if (streq(old, "bool")) {
398 if (undefined2(old, name) && def->def.ty.old_prefix) {
399 s_print(prefix, "%s ", def->def.ty.old_prefix);
403 f_print(fout, "typedef ");
406 f_print(fout, "struct {\n");
407 f_print(fout, "\tu_int %s_len;\n", name);
408 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
409 f_print(fout, "} %s", name);
412 f_print(fout, "%s%s *%s", prefix, old, name);
415 f_print(fout, "%s%s %s[%s]", prefix, old, name,
416 def->def.ty.array_max);
419 f_print(fout, "%s%s %s", prefix, old, name);
422 f_print(fout, ";\n");
427 pdeclaration(const char *name, declaration *dec, int tab,
428 const char *separator)
430 char buf[8]; /* enough to hold "struct ", include NUL */
434 if (streq(dec->type, "void")) {
438 if (streq(dec->type, name) && !dec->prefix) {
439 f_print(fout, "struct ");
441 if (streq(dec->type, "string")) {
442 f_print(fout, "char *%s", dec->name);
445 if (streq(dec->type, "bool")) {
447 } else if (streq(dec->type, "opaque")) {
451 s_print(buf, "%s ", dec->prefix);
458 f_print(fout, "%s%s %s", prefix, type, dec->name);
461 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
465 f_print(fout, "%s%s *%s", prefix, type, dec->name);
468 f_print(fout, "struct {\n");
470 f_print(fout, "\tu_int %s_len;\n", dec->name);
472 f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
474 f_print(fout, "} %s", dec->name);
478 f_print(fout, separator );
482 undefined2(const char *type, const char *stop)
487 for (l = defined; l != NULL; l = l->next) {
488 def = (definition *) l->val;
489 if (def->def_kind != DEF_PROGRAM) {
490 if (streq(def->def_name, stop)) {
492 } else if (streq(def->def_name, type)) {