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
32 #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 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * rpc_hout.c, Header file outputter for the RPC protocol compiler
42 * Copyright (C) 1987, Sun Microsystems, Inc.
46 #include "rpc_parse.h"
50 void storexdrfuncdecl(const char *, int );
51 static void pconstdef( definition * );
52 static void pstructdef( definition * );
53 static void puniondef( definition * );
54 static void pprogramdef( definition *, int );
55 static void penumdef( definition * );
56 static void ptypedef( definition * );
57 static void pdefine(const char *, const char *);
58 static int undefined2(const char *, const char *);
59 static void parglist(proc_list *, const char *);
60 static void pprocdef(proc_list *, version_list *, const char *, int);
63 * Print the C-version of an xdr definition
66 print_datadef(definition *def, int headeronly)
69 if (def->def_kind == DEF_PROGRAM) /* handle data only */
72 if (def->def_kind != DEF_CONST) {
75 switch (def->def_kind) {
89 pprogramdef(def, headeronly);
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,
105 print_funcdef(definition *def, int headeronly)
107 switch (def->def_kind) {
110 pprogramdef(def, headeronly);
117 /* store away enough information to allow the XDR functions to be spat
118 out at the end of the file */
121 storexdrfuncdecl(const char *name, int pointerp)
125 xdrptr = XALLOC(struct xdrfunc);
128 xdrptr->pointerp = pointerp;
131 if (xdrfunc_tail == NULL){
132 xdrfunc_head = xdrptr;
133 xdrfunc_tail = xdrptr;
135 xdrfunc_tail->next = xdrptr;
136 xdrfunc_tail = xdrptr;
143 print_xdr_func_def(const char *name, int pointerp)
145 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
146 name, pointerp ? "*" : "");
151 pconstdef(definition *def)
153 pdefine(def->def_name, def->def.co);
156 /* print out the definitions for the arguments of functions in the
160 pargdef(definition *def)
168 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
169 for (plist = vers->procs; plist != NULL;
170 plist = plist->next) {
172 if (!newstyle || plist->arg_num < 2) {
173 continue; /* old style or single args */
175 name = plist->args.argname;
176 f_print(fout, "struct %s {\n", name);
177 for (l = plist->args.decls;
178 l != NULL; l = l->next) {
179 pdeclaration(name, &l->decl, 1,
182 f_print(fout, "};\n");
183 f_print(fout, "typedef struct %s %s;\n",
185 storexdrfuncdecl(name, 1);
193 pstructdef(definition *def)
196 const char *name = def->def_name;
198 f_print(fout, "struct %s {\n", name);
199 for (l = def->def.st.decls; l != NULL; l = l->next) {
200 pdeclaration(name, &l->decl, 1, ";\n");
202 f_print(fout, "};\n");
203 f_print(fout, "typedef struct %s %s;\n", name, name);
207 puniondef(definition *def)
210 const char *name = def->def_name;
213 f_print(fout, "struct %s {\n", name);
214 decl = &def->def.un.enum_decl;
215 if (streq(decl->type, "bool")) {
216 f_print(fout, "\tbool_t %s;\n", decl->name);
218 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
220 f_print(fout, "\tunion {\n");
221 for (l = def->def.un.cases; l != NULL; l = l->next) {
222 if (l->contflag == 0)
223 pdeclaration(name, &l->case_decl, 2, ";\n");
225 decl = def->def.un.default_decl;
226 if (decl && !streq(decl->type, "void")) {
227 pdeclaration(name, decl, 2, ";\n");
229 f_print(fout, "\t} %s_u;\n", name);
230 f_print(fout, "};\n");
231 f_print(fout, "typedef struct %s %s;\n", name, name);
235 pdefine(const char *name, const char *num)
237 f_print(fout, "#define\t%s %s\n", name, num);
241 puldefine(const char *name, const char *num)
243 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
247 define_printed(proc_list *stop, version_list *start)
252 for (vers = start; vers != NULL; vers = vers->next) {
253 for (proc = vers->procs; proc != NULL; proc = proc->next) {
256 } else if (streq(proc->proc_name, stop->proc_name)) {
266 pfreeprocdef(const char * name, const char *vers)
268 f_print(fout, "extern int ");
270 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
274 pdispatch(const char * name, const char *vers)
277 f_print(fout, "void ");
279 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
283 pprogramdef(definition *def, int headeronly)
291 puldefine(def->def_name, def->def.pr.prog_num);
292 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
295 "extern struct rpcgen_table %s_%s_table[];\n",
296 locase(def->def_name), vers->vers_num);
298 "extern %s_%s_nproc;\n",
299 locase(def->def_name), vers->vers_num);
301 puldefine(vers->vers_name, vers->vers_num);
306 f_print(fout, "%s", ext);
307 pdispatch(def->def_name, vers->vers_num);
309 for (proc = vers->procs; proc != NULL; proc = proc->next) {
310 if (!define_printed(proc, def->def.pr.versions)) {
311 puldefine(proc->proc_name, proc->proc_num);
313 f_print(fout, "%s", ext);
314 pprocdef(proc, vers, "CLIENT *", 0);
315 f_print(fout, "%s", ext);
316 pprocdef(proc, vers, "struct svc_req *", 1);
318 pfreeprocdef(def->def_name, vers->vers_num);
323 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
325 if (mtflag) {/* Print MT style stubs */
327 f_print(fout, "bool_t ");
329 f_print(fout, "enum clnt_stat ");
331 ptype(proc->res_prefix, proc->res_type, 1);
335 pvname_svc(proc->proc_name, vp->vers_num);
337 pvname(proc->proc_name, vp->vers_num);
339 parglist(proc, addargtype);
344 /* print out argument list of procedure */
346 parglist(proc_list *proc, const char *addargtype)
351 if (proc->arg_num < 2 && newstyle &&
352 streq(proc->args.decls->decl.type, "void")) {
353 /* 0 argument in new style: do nothing*/
356 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
357 ptype(dl->decl.prefix, dl->decl.type, 1);
360 /* old style passes by reference */
366 ptype(proc->res_prefix, proc->res_type, 1);
367 f_print(fout, "*, ");
370 f_print(fout, "%s);\n", addargtype);
375 penumdef(definition *def)
377 const char *name = def->def_name;
379 const char *last = NULL;
382 f_print(fout, "enum %s {\n", name);
383 for (l = def->def.en.vals; l != NULL; l = l->next) {
384 f_print(fout, "\t%s", l->name);
386 f_print(fout, " = %s", l->assignment);
387 last = l->assignment;
391 f_print(fout, " = %d", count++);
393 f_print(fout, " = %s + %d", last, count++);
397 f_print(fout, ",\n");
401 f_print(fout, "};\n");
402 f_print(fout, "typedef enum %s %s;\n", name, name);
406 ptypedef(definition *def)
408 const char *name = def->def_name;
409 const char *old = def->def.ty.old_type;
410 char prefix[8]; /* enough to contain "struct ", including NUL */
411 relation rel = def->def.ty.rel;
414 if (!streq(name, old)) {
415 if (streq(old, "string")) {
418 } else if (streq(old, "opaque")) {
420 } else if (streq(old, "bool")) {
423 if (undefined2(old, name) && def->def.ty.old_prefix) {
424 s_print(prefix, "%s ", def->def.ty.old_prefix);
428 f_print(fout, "typedef ");
431 f_print(fout, "struct {\n");
432 f_print(fout, "\tu_int %s_len;\n", name);
433 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
434 f_print(fout, "} %s", name);
437 f_print(fout, "%s%s *%s", prefix, old, name);
440 f_print(fout, "%s%s %s[%s]", prefix, old, name,
441 def->def.ty.array_max);
444 f_print(fout, "%s%s %s", prefix, old, name);
447 f_print(fout, ";\n");
452 pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
454 char buf[8]; /* enough to hold "struct ", include NUL */
458 if (streq(dec->type, "void")) {
462 if (streq(dec->type, name) && !dec->prefix) {
463 f_print(fout, "struct ");
465 if (streq(dec->type, "string")) {
466 f_print(fout, "char *%s", dec->name);
469 if (streq(dec->type, "bool")) {
471 } else if (streq(dec->type, "opaque")) {
475 s_print(buf, "%s ", dec->prefix);
482 f_print(fout, "%s%s %s", prefix, type, dec->name);
485 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
489 f_print(fout, "%s%s *%s", prefix, type, dec->name);
492 f_print(fout, "struct {\n");
494 f_print(fout, "\tu_int %s_len;\n", dec->name);
497 "\t%s%s *%s_val;\n", prefix, type, dec->name);
499 f_print(fout, "} %s", dec->name);
503 fputs(separator, fout);
507 undefined2(const char *type, const char *stop)
512 for (l = defined; l != NULL; l = l->next) {
513 def = (definition *) l->val;
514 if (def->def_kind != DEF_PROGRAM) {
515 if (streq(def->def_name, stop)) {
517 } else if (streq(def->def_name, type)) {