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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #ident "@(#)rpc_hout.c 1.16 94/04/25 SMI"
34 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * rpc_hout.c, Header file outputter for the RPC protocol compiler
43 * Copyright (C) 1987, Sun Microsystems, Inc.
47 #include "rpc_parse.h"
50 void storexdrfuncdecl( char *, int );
51 static void pconstdef( definition * );
52 static void pstructdef( definition * );
53 static void puniondef( definition * );
54 static void pprogramdef( definition * );
55 static void pstructdef( definition * );
56 static void penumdef( definition * );
57 static void ptypedef( definition * );
58 static void pdefine( char *, char * );
59 static int undefined2( char *, char * );
60 static void parglist( proc_list *, char * );
61 static void pprocdef( proc_list *, version_list *, char *, int, int );
62 void pdeclaration( char *, declaration *, int, char * );
65 * Print the C-version of an xdr definition
72 if (def->def_kind == DEF_PROGRAM) /* handle data only */
75 if (def->def_kind != DEF_CONST) {
78 switch (def->def_kind) {
98 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
99 storexdrfuncdecl(def->def_name,
100 def->def_kind != DEF_TYPEDEF ||
101 !isvectordef(def->def.ty.old_type,
111 switch (def->def_kind) {
121 /* store away enough information to allow the XDR functions to be spat
122 out at the end of the file */
125 storexdrfuncdecl(name, pointerp)
131 xdrptr = XALLOC(struct xdrfunc);
134 xdrptr->pointerp = pointerp;
137 if (xdrfunc_tail == NULL){
138 xdrfunc_head = xdrptr;
139 xdrfunc_tail = xdrptr;
141 xdrfunc_tail->next = xdrptr;
142 xdrfunc_tail = xdrptr;
149 print_xdr_func_def(name, pointerp, i)
155 f_print(fout, "extern bool_t xdr_%s();\n", name);
159 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
160 name, pointerp ? "*" : "");
170 pdefine(def->def_name, def->def.co);
173 /* print out the definitions for the arguments of functions in the
186 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
187 for (plist = vers->procs; plist != NULL;
188 plist = plist->next) {
190 if (!newstyle || plist->arg_num < 2) {
191 continue; /* old style or single args */
193 name = plist->args.argname;
194 f_print(fout, "struct %s {\n", name);
195 for (l = plist->args.decls;
196 l != NULL; l = l->next) {
197 pdeclaration(name, &l->decl, 1,
200 f_print(fout, "};\n");
201 f_print(fout, "typedef struct %s %s;\n",
203 storexdrfuncdecl(name, 1);
215 char *name = def->def_name;
217 f_print(fout, "struct %s {\n", name);
218 for (l = def->def.st.decls; l != NULL; l = l->next) {
219 pdeclaration(name, &l->decl, 1, ";\n");
221 f_print(fout, "};\n");
222 f_print(fout, "typedef struct %s %s;\n", name, name);
230 char *name = def->def_name;
233 f_print(fout, "struct %s {\n", name);
234 decl = &def->def.un.enum_decl;
235 if (streq(decl->type, "bool")) {
236 f_print(fout, "\tbool_t %s;\n", decl->name);
238 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
240 f_print(fout, "\tunion {\n");
241 for (l = def->def.un.cases; l != NULL; l = l->next) {
242 if (l->contflag == 0)
243 pdeclaration(name, &l->case_decl, 2, ";\n");
245 decl = def->def.un.default_decl;
246 if (decl && !streq(decl->type, "void")) {
247 pdeclaration(name, decl, 2, ";\n");
249 f_print(fout, "\t} %s_u;\n", name);
250 f_print(fout, "};\n");
251 f_print(fout, "typedef struct %s %s;\n", name, name);
259 f_print(fout, "#define\t%s %s\n", name, num);
267 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
271 define_printed(stop, start)
278 for (vers = start; vers != NULL; vers = vers->next) {
279 for (proc = vers->procs; proc != NULL; proc = proc->next) {
282 } else if (streq(proc->proc_name, stop->proc_name)) {
292 pfreeprocdef(char * name, char *vers, int mode)
294 f_print(fout, "extern int ");
297 f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
299 f_print(fout,"_freeresult();\n");
305 pdispatch(char * name, char *vers, int mode)
308 f_print(fout, "void ");
312 "(struct svc_req *rqstp, register SVCXPRT *transp);\n");
314 f_print(fout,"();\n");
329 puldefine(def->def_name, def->def.pr.prog_num);
330 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
333 "extern struct rpcgen_table %s_%s_table[];\n",
334 locase(def->def_name), vers->vers_num);
336 "extern %s_%s_nproc;\n",
337 locase(def->def_name), vers->vers_num);
339 puldefine(vers->vers_name, vers->vers_num);
342 * Print out 2 definitions, one for ANSI-C, another for
348 f_print(fout, "%s", ext);
349 pdispatch(def->def_name, vers->vers_num, 2);
350 for (proc = vers->procs; proc != NULL;
352 if (!define_printed(proc,
353 def->def.pr.versions)) {
354 puldefine(proc->proc_name,
357 f_print(fout, "%s", ext);
358 pprocdef(proc, vers, NULL, 0, 2);
361 f_print(fout, "%s", ext);
362 pprocdef(proc, vers, NULL, 1, 2);
365 pfreeprocdef(def->def_name, vers->vers_num, 2);
368 for (i = 1; i < 3; i++){
370 f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
373 f_print(fout, "\n#else /* K&R C */\n");
377 f_print(fout, "%s", ext);
378 pdispatch(def->def_name, vers->vers_num, i);
379 for (proc = vers->procs; proc != NULL;
381 if (!define_printed(proc,
382 def->def.pr.versions)) {
383 puldefine(proc->proc_name,
386 f_print(fout, "%s", ext);
387 pprocdef(proc, vers, "CLIENT *", 0, i);
388 f_print(fout, "%s", ext);
389 pprocdef(proc, vers, "struct svc_req *", 1, i);
391 pfreeprocdef(def->def_name, vers->vers_num, i);
393 f_print(fout, "#endif /* K&R C */\n");
399 pprocdef(proc, vp, addargtype, server_p, mode)
406 if (mtflag) {/* Print MT style stubs */
408 f_print(fout, "bool_t ");
410 f_print(fout, "enum clnt_stat ");
412 ptype(proc->res_prefix, proc->res_type, 1);
416 pvname_svc(proc->proc_name, vp->vers_num);
418 pvname(proc->proc_name, vp->vers_num);
421 * mode 1 = ANSI-C, mode 2 = K&R C
424 parglist(proc, addargtype);
426 f_print(fout, "();\n");
431 /* print out argument list of procedure */
433 parglist(proc, addargtype)
440 if (proc->arg_num < 2 && newstyle &&
441 streq(proc->args.decls->decl.type, "void")) {
442 /* 0 argument in new style: do nothing*/
445 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
446 ptype(dl->decl.prefix, dl->decl.type, 1);
449 /* old style passes by reference */
455 ptype(proc->res_prefix, proc->res_type, 1);
456 f_print(fout, "*, ");
459 f_print(fout, "%s);\n", addargtype);
467 char *name = def->def_name;
472 f_print(fout, "enum %s {\n", name);
473 for (l = def->def.en.vals; l != NULL; l = l->next) {
474 f_print(fout, "\t%s", l->name);
476 f_print(fout, " = %s", l->assignment);
477 last = l->assignment;
481 f_print(fout, " = %d", count++);
483 f_print(fout, " = %s + %d", last, count++);
487 f_print(fout, ",\n");
491 f_print(fout, "};\n");
492 f_print(fout, "typedef enum %s %s;\n", name, name);
499 char *name = def->def_name;
500 char *old = def->def.ty.old_type;
501 char prefix[8]; /* enough to contain "struct ", including NUL */
502 relation rel = def->def.ty.rel;
505 if (!streq(name, old)) {
506 if (streq(old, "string")) {
509 } else if (streq(old, "opaque")) {
511 } else if (streq(old, "bool")) {
514 if (undefined2(old, name) && def->def.ty.old_prefix) {
515 s_print(prefix, "%s ", def->def.ty.old_prefix);
519 f_print(fout, "typedef ");
522 f_print(fout, "struct {\n");
523 f_print(fout, "\tu_int %s_len;\n", name);
524 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
525 f_print(fout, "} %s", name);
528 f_print(fout, "%s%s *%s", prefix, old, name);
531 f_print(fout, "%s%s %s[%s]", prefix, old, name,
532 def->def.ty.array_max);
535 f_print(fout, "%s%s %s", prefix, old, name);
538 f_print(fout, ";\n");
543 pdeclaration(name, dec, tab, separator)
549 char buf[8]; /* enough to hold "struct ", include NUL */
553 if (streq(dec->type, "void")) {
557 if (streq(dec->type, name) && !dec->prefix) {
558 f_print(fout, "struct ");
560 if (streq(dec->type, "string")) {
561 f_print(fout, "char *%s", dec->name);
564 if (streq(dec->type, "bool")) {
566 } else if (streq(dec->type, "opaque")) {
570 s_print(buf, "%s ", dec->prefix);
577 f_print(fout, "%s%s %s", prefix, type, dec->name);
580 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
584 f_print(fout, "%s%s *%s", prefix, type, dec->name);
587 f_print(fout, "struct {\n");
589 f_print(fout, "\tu_int %s_len;\n", dec->name);
592 "\t%s%s *%s_val;\n", prefix, type, dec->name);
594 f_print(fout, "} %s", dec->name);
598 f_print(fout, separator);
602 undefined2(type, stop)
609 for (l = defined; l != NULL; l = l->next) {
610 def = (definition *) l->val;
611 if (def->def_kind != DEF_PROGRAM) {
612 if (streq(def->def_name, stop)) {
614 } else if (streq(def->def_name, type)) {