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"
33 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
37 * rpc_hout.c, Header file outputter for the RPC protocol compiler
38 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include "rpc_parse.h"
45 void storexdrfuncdecl __P(( char *, int ));
46 static void pconstdef __P(( definition * ));
47 static void pstructdef __P(( definition * ));
48 static void puniondef __P(( definition * ));
49 static void pprogramdef __P(( definition * ));
50 static void pstructdef __P(( definition * ));
51 static void penumdef __P(( definition * ));
52 static void ptypedef __P(( definition * ));
53 static void pdefine __P(( char *, char * ));
54 static int undefined2 __P(( char *, char * ));
55 static void parglist __P(( proc_list *, char * ));
56 static void pprocdef __P(( proc_list *, version_list *, char *, int, int ));
57 void pdeclaration __P(( char *, declaration *, int, char * ));
59 static char RESULT[] = "clnt_res";
63 * Print the C-version of an xdr definition
70 if (def->def_kind == DEF_PROGRAM) /* handle data only */
73 if (def->def_kind != DEF_CONST) {
76 switch (def->def_kind) {
96 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
97 storexdrfuncdecl(def->def_name,
98 def->def_kind != DEF_TYPEDEF ||
99 !isvectordef(def->def.ty.old_type,
109 switch (def->def_kind) {
118 /* store away enough information to allow the XDR functions to be spat
119 out at the end of the file */
122 storexdrfuncdecl(name, pointerp)
128 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
131 xdrptr->pointerp = pointerp;
134 if (xdrfunc_tail == NULL){
135 xdrfunc_head = xdrptr;
136 xdrfunc_tail = xdrptr;
138 xdrfunc_tail->next = xdrptr;
139 xdrfunc_tail = xdrptr;
146 print_xdr_func_def(name, pointerp, i)
152 f_print(fout, "extern bool_t xdr_%s();\n", name);
156 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
157 name, pointerp ? "*" : "");
167 pdefine(def->def_name, def->def.co);
170 /* print out the definitions for the arguments of functions in the
183 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
184 for (plist = vers->procs; plist != NULL;
185 plist = plist->next) {
187 if (!newstyle || plist->arg_num < 2) {
188 continue; /* old style or single args */
190 name = plist->args.argname;
191 f_print(fout, "struct %s {\n", name);
192 for (l = plist->args.decls;
193 l != NULL; l = l->next) {
194 pdeclaration(name, &l->decl, 1,
197 f_print(fout, "};\n");
198 f_print(fout, "typedef struct %s %s;\n",
200 storexdrfuncdecl(name, 1);
212 char *name = def->def_name;
214 f_print(fout, "struct %s {\n", name);
215 for (l = def->def.st.decls; l != NULL; l = l->next) {
216 pdeclaration(name, &l->decl, 1, ";\n");
218 f_print(fout, "};\n");
219 f_print(fout, "typedef struct %s %s;\n", name, name);
227 char *name = def->def_name;
230 f_print(fout, "struct %s {\n", name);
231 decl = &def->def.un.enum_decl;
232 if (streq(decl->type, "bool")) {
233 f_print(fout, "\tbool_t %s;\n", decl->name);
235 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
237 f_print(fout, "\tunion {\n");
238 for (l = def->def.un.cases; l != NULL; l = l->next) {
239 if (l->contflag == 0)
240 pdeclaration(name, &l->case_decl, 2, ";\n");
242 decl = def->def.un.default_decl;
243 if (decl && !streq(decl->type, "void")) {
244 pdeclaration(name, decl, 2, ";\n");
246 f_print(fout, "\t} %s_u;\n", name);
247 f_print(fout, "};\n");
248 f_print(fout, "typedef struct %s %s;\n", name, name);
256 f_print(fout, "#define\t%s %s\n", name, num);
264 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
268 define_printed(stop, start)
275 for (vers = start; vers != NULL; vers = vers->next) {
276 for (proc = vers->procs; proc != NULL; proc = proc->next) {
279 } else if (streq(proc->proc_name, stop->proc_name)) {
289 pfreeprocdef(char * name, char *vers, int mode)
291 f_print(fout, "extern int ");
294 f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
296 f_print(fout,"_freeresult();\n");
312 puldefine(def->def_name, def->def.pr.prog_num);
313 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
316 "extern struct rpcgen_table %s_%s_table[];\n",
317 locase(def->def_name), vers->vers_num);
319 "extern %s_%s_nproc;\n",
320 locase(def->def_name), vers->vers_num);
322 puldefine(vers->vers_name, vers->vers_num);
325 * Print out 2 definitions, one for ANSI-C, another for
331 for (proc = vers->procs; proc != NULL;
333 if (!define_printed(proc,
334 def->def.pr.versions)) {
335 puldefine(proc->proc_name,
338 f_print(fout, "%s", ext);
339 pprocdef(proc, vers, NULL, 0, 2);
342 f_print(fout, "%s", ext);
343 pprocdef(proc, vers, NULL, 1, 2);
346 pfreeprocdef(def->def_name, vers->vers_num, 2);
349 for (i = 1; i < 3; i++){
351 f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
354 f_print(fout, "\n#else /* K&R C */\n");
358 for (proc = vers->procs; proc != NULL;
360 if (!define_printed(proc,
361 def->def.pr.versions)) {
362 puldefine(proc->proc_name,
365 f_print(fout, "%s", ext);
366 pprocdef(proc, vers, "CLIENT *", 0, i);
367 f_print(fout, "%s", ext);
368 pprocdef(proc, vers, "struct svc_req *", 1, i);
370 pfreeprocdef(def->def_name, vers->vers_num, i);
372 f_print(fout, "#endif /* K&R C */\n");
378 pprocdef(proc, vp, addargtype, server_p, mode)
385 if (mtflag) {/* Print MT style stubs */
387 f_print(fout, "bool_t ");
389 f_print(fout, "enum clnt_stat ");
391 ptype(proc->res_prefix, proc->res_type, 1);
395 pvname_svc(proc->proc_name, vp->vers_num);
397 pvname(proc->proc_name, vp->vers_num);
400 * mode 1 = ANSI-C, mode 2 = K&R C
403 parglist(proc, addargtype);
405 f_print(fout, "();\n");
410 /* print out argument list of procedure */
412 parglist(proc, addargtype)
419 if (proc->arg_num < 2 && newstyle &&
420 streq(proc->args.decls->decl.type, "void")) {
421 /* 0 argument in new style: do nothing*/
424 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
425 ptype(dl->decl.prefix, dl->decl.type, 1);
428 /* old style passes by reference */
434 ptype(proc->res_prefix, proc->res_type, 1);
435 f_print(fout, "*, ");
438 f_print(fout, "%s);\n", addargtype);
446 char *name = def->def_name;
451 f_print(fout, "enum %s {\n", name);
452 for (l = def->def.en.vals; l != NULL; l = l->next) {
453 f_print(fout, "\t%s", l->name);
455 f_print(fout, " = %s", l->assignment);
456 last = l->assignment;
460 f_print(fout, " = %d", count++);
462 f_print(fout, " = %s + %d", last, count++);
466 f_print(fout, ",\n");
470 f_print(fout, "};\n");
471 f_print(fout, "typedef enum %s %s;\n", name, name);
478 char *name = def->def_name;
479 char *old = def->def.ty.old_type;
480 char prefix[8]; /* enough to contain "struct ", including NUL */
481 relation rel = def->def.ty.rel;
484 if (!streq(name, old)) {
485 if (streq(old, "string")) {
488 } else if (streq(old, "opaque")) {
490 } else if (streq(old, "bool")) {
493 if (undefined2(old, name) && def->def.ty.old_prefix) {
494 s_print(prefix, "%s ", def->def.ty.old_prefix);
498 f_print(fout, "typedef ");
501 f_print(fout, "struct {\n");
502 f_print(fout, "\tu_int %s_len;\n", name);
503 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
504 f_print(fout, "} %s", name);
507 f_print(fout, "%s%s *%s", prefix, old, name);
510 f_print(fout, "%s%s %s[%s]", prefix, old, name,
511 def->def.ty.array_max);
514 f_print(fout, "%s%s %s", prefix, old, name);
517 f_print(fout, ";\n");
522 pdeclaration(name, dec, tab, separator)
528 char buf[8]; /* enough to hold "struct ", include NUL */
532 if (streq(dec->type, "void")) {
536 if (streq(dec->type, name) && !dec->prefix) {
537 f_print(fout, "struct ");
539 if (streq(dec->type, "string")) {
540 f_print(fout, "char *%s", dec->name);
543 if (streq(dec->type, "bool")) {
545 } else if (streq(dec->type, "opaque")) {
549 s_print(buf, "%s ", dec->prefix);
556 f_print(fout, "%s%s %s", prefix, type, dec->name);
559 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
563 f_print(fout, "%s%s *%s", prefix, type, dec->name);
566 f_print(fout, "struct {\n");
568 f_print(fout, "\tu_int %s_len;\n", dec->name);
571 "\t%s%s *%s_val;\n", prefix, type, dec->name);
573 f_print(fout, "} %s", dec->name);
577 f_print(fout, separator);
581 undefined2(type, stop)
588 for (l = defined; l != NULL; l = l->next) {
589 def = (definition *) l->val;
590 if (def->def_kind != DEF_PROGRAM) {
591 if (streq(def->def_name, stop)) {
593 } else if (streq(def->def_name, type)) {