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.16 94/04/25 SMI; 1.12 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_hout.c,v 1.15 2005/11/13 21:17:24 dwmalone Exp $
34 * rpc_hout.c, Header file outputter for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
39 #include "rpc_parse.h"
43 void storexdrfuncdecl(const char *, int);
44 static void pconstdef(definition *);
45 static void pstructdef(definition *);
46 static void puniondef(definition *);
47 static void pprogramdef(definition *, int);
48 static void penumdef(definition *);
49 static void ptypedef(definition *);
50 static void pdefine(const char *, const char *);
51 static int undefined2(const char *, const char *);
52 static void parglist(proc_list *, const char *);
53 static void pprocdef(proc_list *, version_list *, const char *, int);
56 * Print the C-version of an xdr definition
59 print_datadef(definition *def, int headeronly)
62 if (def->def_kind == DEF_PROGRAM) /* handle data only */
65 if (def->def_kind != DEF_CONST)
68 switch (def->def_kind) {
82 pprogramdef(def, headeronly);
88 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
89 if (def->def_kind != DEF_TYPEDEF ||
90 !isvectordef(def->def.ty.old_type, def->def.ty.rel))
91 storexdrfuncdecl(def->def_name, 1);
93 storexdrfuncdecl(def->def_name, 0);
99 print_funcdef(definition *def, int headeronly)
101 switch (def->def_kind) {
104 pprogramdef(def, headeronly);
111 /* store away enough information to allow the XDR functions to be spat
112 out at the end of the file */
115 storexdrfuncdecl(const char *name, int pointerp)
119 xdrptr = XALLOC(struct xdrfunc);
122 xdrptr->pointerp = pointerp;
125 if (xdrfunc_tail == NULL)
126 xdrfunc_head = xdrptr;
128 xdrfunc_tail->next = xdrptr;
129 xdrfunc_tail = xdrptr;
134 print_xdr_func_def(const char *name, int pointerp)
136 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
137 name, pointerp ? "*" : "");
142 pconstdef(definition *def)
144 pdefine(def->def_name, def->def.co);
147 /* print out the definitions for the arguments of functions in the
151 pargdef(definition *def)
158 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
159 for (plist = vers->procs; plist != NULL; plist = plist->next) {
160 if (!newstyle || plist->arg_num < 2)
161 continue; /* old style or single args */
163 name = plist->args.argname;
164 f_print(fout, "struct %s {\n", name);
165 for (l = plist->args.decls; l != NULL; l = l->next)
166 pdeclaration(name, &l->decl, 1, ";\n");
167 f_print(fout, "};\n");
168 f_print(fout, "typedef struct %s %s;\n", name, name);
169 storexdrfuncdecl(name, 1);
177 pstructdef(definition *def)
180 const char *name = def->def_name;
182 f_print(fout, "struct %s {\n", name);
183 for (l = def->def.st.decls; l != NULL; l = l->next)
184 pdeclaration(name, &l->decl, 1, ";\n");
186 f_print(fout, "};\n");
187 f_print(fout, "typedef struct %s %s;\n", name, name);
191 puniondef(definition *def)
194 const char *name = def->def_name;
197 f_print(fout, "struct %s {\n", name);
198 decl = &def->def.un.enum_decl;
199 if (streq(decl->type, "bool"))
200 f_print(fout, "\tbool_t %s;\n", decl->name);
202 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
204 f_print(fout, "\tunion {\n");
205 for (l = def->def.un.cases; l != NULL; l = l->next) {
206 if (l->contflag == 0)
207 pdeclaration(name, &l->case_decl, 2, ";\n");
209 decl = def->def.un.default_decl;
210 if (decl && !streq(decl->type, "void")) {
211 pdeclaration(name, decl, 2, ";\n");
213 f_print(fout, "\t} %s_u;\n", name);
214 f_print(fout, "};\n");
215 f_print(fout, "typedef struct %s %s;\n", name, name);
219 pdefine(const char *name, const char *num)
221 f_print(fout, "#define\t%s %s\n", name, num);
225 puldefine(const char *name, const char *num)
227 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
231 define_printed(proc_list *stop, version_list *start)
236 for (vers = start; vers != NULL; vers = vers->next) {
237 for (proc = vers->procs; proc != NULL; proc = proc->next) {
240 else if (streq(proc->proc_name, stop->proc_name))
249 pfreeprocdef(const char * name, const char *vers)
251 f_print(fout, "extern int ");
253 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
257 pdispatch(const char * name, const char *vers)
260 f_print(fout, "void ");
262 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
266 pprogramdef(definition *def, int headeronly)
274 puldefine(def->def_name, def->def.pr.prog_num);
275 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
278 "extern struct rpcgen_table %s_%s_table[];\n",
279 locase(def->def_name), vers->vers_num);
280 f_print(fout, "extern %s_%s_nproc;\n",
281 locase(def->def_name), vers->vers_num);
283 puldefine(vers->vers_name, vers->vers_num);
288 f_print(fout, "%s", ext);
289 pdispatch(def->def_name, vers->vers_num);
291 for (proc = vers->procs; proc != NULL; proc = proc->next) {
292 if (!define_printed(proc, def->def.pr.versions)) {
293 puldefine(proc->proc_name, proc->proc_num);
295 f_print(fout, "%s", ext);
296 pprocdef(proc, vers, "CLIENT *", 0);
297 f_print(fout, "%s", ext);
298 pprocdef(proc, vers, "struct svc_req *", 1);
300 pfreeprocdef(def->def_name, vers->vers_num);
305 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
308 /* Print MT style stubs */
310 f_print(fout, "bool_t ");
312 f_print(fout, "enum clnt_stat ");
314 ptype(proc->res_prefix, proc->res_type, 1);
318 pvname_svc(proc->proc_name, vp->vers_num);
320 pvname(proc->proc_name, vp->vers_num);
322 parglist(proc, addargtype);
327 /* print out argument list of procedure */
329 parglist(proc_list *proc, const char *addargtype)
334 if (proc->arg_num < 2 && newstyle &&
335 streq(proc->args.decls->decl.type, "void")) {
336 /* 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);
343 /* old style passes by reference */
349 ptype(proc->res_prefix, proc->res_type, 1);
350 f_print(fout, "*, ");
353 f_print(fout, "%s);\n", addargtype);
357 penumdef(definition *def)
359 const char *name = def->def_name;
361 const char *last = NULL;
364 f_print(fout, "enum %s {\n", name);
365 for (l = def->def.en.vals; l != NULL; l = l->next) {
366 f_print(fout, "\t%s", l->name);
368 f_print(fout, " = %s", l->assignment);
369 last = l->assignment;
373 f_print(fout, " = %d", count++);
375 f_print(fout, " = %s + %d", last, count++);
378 f_print(fout, ",\n");
382 f_print(fout, "};\n");
383 f_print(fout, "typedef enum %s %s;\n", name, name);
387 ptypedef(definition *def)
389 const char *name = def->def_name;
390 const char *old = def->def.ty.old_type;
391 char prefix[8]; /* enough to contain "struct ", including NUL */
392 relation rel = def->def.ty.rel;
394 if (!streq(name, old)) {
395 if (streq(old, "string")) {
398 } else if (streq(old, "opaque")) {
400 } else if (streq(old, "bool")) {
403 if (undefined2(old, name) && def->def.ty.old_prefix)
404 s_print(prefix, "%s ", def->def.ty.old_prefix);
407 f_print(fout, "typedef ");
410 f_print(fout, "struct {\n");
411 f_print(fout, "\tu_int %s_len;\n", name);
412 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
413 f_print(fout, "} %s", name);
416 f_print(fout, "%s%s *%s", prefix, old, name);
419 f_print(fout, "%s%s %s[%s]", prefix, old, name,
420 def->def.ty.array_max);
423 f_print(fout, "%s%s %s", prefix, old, name);
426 f_print(fout, ";\n");
431 pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
433 char buf[8]; /* enough to hold "struct ", include NUL */
437 if (streq(dec->type, "void"))
441 if (streq(dec->type, name) && !dec->prefix)
442 f_print(fout, "struct ");
444 if (streq(dec->type, "string")) {
445 f_print(fout, "char *%s", dec->name);
448 if (streq(dec->type, "bool")) {
450 } else if (streq(dec->type, "opaque")) {
454 s_print(buf, "%s ", dec->prefix);
461 f_print(fout, "%s%s %s", prefix, type, dec->name);
464 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
468 f_print(fout, "%s%s *%s", prefix, type, dec->name);
471 f_print(fout, "struct {\n");
473 f_print(fout, "\tu_int %s_len;\n", dec->name);
476 "\t%s%s *%s_val;\n", prefix, type, dec->name);
478 f_print(fout, "} %s", dec->name);
482 f_print(fout, "%s", separator);
486 undefined2(const char *type, const char *stop)
491 for (l = defined; l != NULL; l = l->next) {
492 def = (definition *) l->val;
493 if (def->def_kind != DEF_PROGRAM) {
494 if (streq(def->def_name, stop))
496 else if (streq(def->def_name, type))