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
29 * @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
30 * $DragonFly: src/usr.bin/rpcgen/rpc_hout.c,v 1.3 2003/11/03 19:14:35 eirikn Exp $
33 #ident "@(#)rpc_hout.c 1.16 94/04/25 SMI"
36 * rpc_hout.c, Header file outputter for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsystems, Inc.
41 #include "rpc_parse.h"
44 void storexdrfuncdecl __P(( char *, int ));
45 static void pconstdef __P(( definition * ));
46 static void pstructdef __P(( definition * ));
47 static void puniondef __P(( definition * ));
48 static void pprogramdef __P(( definition * ));
49 static void pstructdef __P(( definition * ));
50 static void penumdef __P(( definition * ));
51 static void ptypedef __P(( definition * ));
52 static void pdefine __P(( char *, char * ));
53 static int undefined2 __P(( char *, char * ));
54 static void parglist __P(( proc_list *, char * ));
55 static void pprocdef __P(( proc_list *, version_list *, char *, int, int ));
56 void pdeclaration __P(( char *, declaration *, int, char * ));
58 static char RESULT[] = "clnt_res";
62 * Print the C-version of an xdr definition
69 if (def->def_kind == DEF_PROGRAM) /* handle data only */
72 if (def->def_kind != DEF_CONST) {
75 switch (def->def_kind) {
95 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
96 storexdrfuncdecl(def->def_name,
97 def->def_kind != DEF_TYPEDEF ||
98 !isvectordef(def->def.ty.old_type,
108 switch (def->def_kind) {
117 /* store away enough information to allow the XDR functions to be spat
118 out at the end of the file */
121 storexdrfuncdecl(name, pointerp)
127 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
130 xdrptr->pointerp = pointerp;
133 if (xdrfunc_tail == NULL){
134 xdrfunc_head = xdrptr;
135 xdrfunc_tail = xdrptr;
137 xdrfunc_tail->next = xdrptr;
138 xdrfunc_tail = xdrptr;
145 print_xdr_func_def(name, pointerp, i)
151 f_print(fout, "extern bool_t xdr_%s();\n", name);
155 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
156 name, pointerp ? "*" : "");
166 pdefine(def->def_name, def->def.co);
169 /* print out the definitions for the arguments of functions in the
182 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
183 for (plist = vers->procs; plist != NULL;
184 plist = plist->next) {
186 if (!newstyle || plist->arg_num < 2) {
187 continue; /* old style or single args */
189 name = plist->args.argname;
190 f_print(fout, "struct %s {\n", name);
191 for (l = plist->args.decls;
192 l != NULL; l = l->next) {
193 pdeclaration(name, &l->decl, 1,
196 f_print(fout, "};\n");
197 f_print(fout, "typedef struct %s %s;\n",
199 storexdrfuncdecl(name, 1);
211 char *name = def->def_name;
213 f_print(fout, "struct %s {\n", name);
214 for (l = def->def.st.decls; l != NULL; l = l->next) {
215 pdeclaration(name, &l->decl, 1, ";\n");
217 f_print(fout, "};\n");
218 f_print(fout, "typedef struct %s %s;\n", name, name);
226 char *name = def->def_name;
229 f_print(fout, "struct %s {\n", name);
230 decl = &def->def.un.enum_decl;
231 if (streq(decl->type, "bool")) {
232 f_print(fout, "\tbool_t %s;\n", decl->name);
234 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
236 f_print(fout, "\tunion {\n");
237 for (l = def->def.un.cases; l != NULL; l = l->next) {
238 if (l->contflag == 0)
239 pdeclaration(name, &l->case_decl, 2, ";\n");
241 decl = def->def.un.default_decl;
242 if (decl && !streq(decl->type, "void")) {
243 pdeclaration(name, decl, 2, ";\n");
245 f_print(fout, "\t} %s_u;\n", name);
246 f_print(fout, "};\n");
247 f_print(fout, "typedef struct %s %s;\n", name, name);
255 f_print(fout, "#define\t%s %s\n", name, num);
263 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
267 define_printed(stop, start)
274 for (vers = start; vers != NULL; vers = vers->next) {
275 for (proc = vers->procs; proc != NULL; proc = proc->next) {
278 } else if (streq(proc->proc_name, stop->proc_name)) {
288 pfreeprocdef(char * name, char *vers, int mode)
290 f_print(fout, "extern int ");
293 f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
295 f_print(fout,"_freeresult();\n");
311 puldefine(def->def_name, def->def.pr.prog_num);
312 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
315 "extern struct rpcgen_table %s_%s_table[];\n",
316 locase(def->def_name), vers->vers_num);
318 "extern %s_%s_nproc;\n",
319 locase(def->def_name), vers->vers_num);
321 puldefine(vers->vers_name, vers->vers_num);
324 * Print out 2 definitions, one for ANSI-C, another for
330 for (proc = vers->procs; proc != NULL;
332 if (!define_printed(proc,
333 def->def.pr.versions)) {
334 puldefine(proc->proc_name,
337 f_print(fout, "%s", ext);
338 pprocdef(proc, vers, NULL, 0, 2);
341 f_print(fout, "%s", ext);
342 pprocdef(proc, vers, NULL, 1, 2);
345 pfreeprocdef(def->def_name, vers->vers_num, 2);
348 for (i = 1; i < 3; i++){
350 f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
353 f_print(fout, "\n#else /* K&R C */\n");
357 for (proc = vers->procs; proc != NULL;
359 if (!define_printed(proc,
360 def->def.pr.versions)) {
361 puldefine(proc->proc_name,
364 f_print(fout, "%s", ext);
365 pprocdef(proc, vers, "CLIENT *", 0, i);
366 f_print(fout, "%s", ext);
367 pprocdef(proc, vers, "struct svc_req *", 1, i);
369 pfreeprocdef(def->def_name, vers->vers_num, i);
371 f_print(fout, "#endif /* K&R C */\n");
377 pprocdef(proc, vp, addargtype, server_p, mode)
384 if (mtflag) {/* Print MT style stubs */
386 f_print(fout, "bool_t ");
388 f_print(fout, "enum clnt_stat ");
390 ptype(proc->res_prefix, proc->res_type, 1);
394 pvname_svc(proc->proc_name, vp->vers_num);
396 pvname(proc->proc_name, vp->vers_num);
399 * mode 1 = ANSI-C, mode 2 = K&R C
402 parglist(proc, addargtype);
404 f_print(fout, "();\n");
409 /* print out argument list of procedure */
411 parglist(proc, addargtype)
418 if (proc->arg_num < 2 && newstyle &&
419 streq(proc->args.decls->decl.type, "void")) {
420 /* 0 argument in new style: do nothing*/
423 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
424 ptype(dl->decl.prefix, dl->decl.type, 1);
427 /* old style passes by reference */
433 ptype(proc->res_prefix, proc->res_type, 1);
434 f_print(fout, "*, ");
437 f_print(fout, "%s);\n", addargtype);
445 char *name = def->def_name;
450 f_print(fout, "enum %s {\n", name);
451 for (l = def->def.en.vals; l != NULL; l = l->next) {
452 f_print(fout, "\t%s", l->name);
454 f_print(fout, " = %s", l->assignment);
455 last = l->assignment;
459 f_print(fout, " = %d", count++);
461 f_print(fout, " = %s + %d", last, count++);
465 f_print(fout, ",\n");
469 f_print(fout, "};\n");
470 f_print(fout, "typedef enum %s %s;\n", name, name);
477 char *name = def->def_name;
478 char *old = def->def.ty.old_type;
479 char prefix[8]; /* enough to contain "struct ", including NUL */
480 relation rel = def->def.ty.rel;
483 if (!streq(name, old)) {
484 if (streq(old, "string")) {
487 } else if (streq(old, "opaque")) {
489 } else if (streq(old, "bool")) {
492 if (undefined2(old, name) && def->def.ty.old_prefix) {
493 s_print(prefix, "%s ", def->def.ty.old_prefix);
497 f_print(fout, "typedef ");
500 f_print(fout, "struct {\n");
501 f_print(fout, "\tu_int %s_len;\n", name);
502 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
503 f_print(fout, "} %s", name);
506 f_print(fout, "%s%s *%s", prefix, old, name);
509 f_print(fout, "%s%s %s[%s]", prefix, old, name,
510 def->def.ty.array_max);
513 f_print(fout, "%s%s %s", prefix, old, name);
516 f_print(fout, ";\n");
521 pdeclaration(name, dec, tab, separator)
527 char buf[8]; /* enough to hold "struct ", include NUL */
531 if (streq(dec->type, "void")) {
535 if (streq(dec->type, name) && !dec->prefix) {
536 f_print(fout, "struct ");
538 if (streq(dec->type, "string")) {
539 f_print(fout, "char *%s", dec->name);
542 if (streq(dec->type, "bool")) {
544 } else if (streq(dec->type, "opaque")) {
548 s_print(buf, "%s ", dec->prefix);
555 f_print(fout, "%s%s %s", prefix, type, dec->name);
558 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
562 f_print(fout, "%s%s *%s", prefix, type, dec->name);
565 f_print(fout, "struct {\n");
567 f_print(fout, "\tu_int %s_len;\n", dec->name);
570 "\t%s%s *%s_val;\n", prefix, type, dec->name);
572 f_print(fout, "} %s", dec->name);
576 f_print(fout, separator);
580 undefined2(type, stop)
587 for (l = defined; l != NULL; l = l->next) {
588 def = (definition *) l->val;
589 if (def->def_kind != DEF_PROGRAM) {
590 if (streq(def->def_name, stop)) {
592 } else if (streq(def->def_name, type)) {