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 $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_hout.c,v 1.5 2004/06/19 16:40:36 joerg Exp $
35 * rpc_hout.c, Header file outputter for the RPC protocol compiler
36 * Copyright (C) 1987, Sun Microsystems, Inc.
40 #include "rpc_parse.h"
44 void storexdrfuncdecl(const char *, int);
45 static void pconstdef(definition *);
46 static void pstructdef(definition *);
47 static void puniondef(definition *);
48 static void pprogramdef(definition *, int);
49 static void penumdef(definition *);
50 static void ptypedef(definition *);
51 static void pdefine(const char *, const char *);
52 static int undefined2(const char *, const char *);
53 static void parglist(proc_list *, const char *);
54 static void pprocdef(proc_list *, version_list *, const char *, int);
57 * Print the C-version of an xdr definition
60 print_datadef(definition *def, int headeronly)
63 if (def->def_kind == DEF_PROGRAM) /* handle data only */
66 if (def->def_kind != DEF_CONST)
69 switch (def->def_kind) {
83 pprogramdef(def, headeronly);
89 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
90 if (def->def_kind != DEF_TYPEDEF ||
91 !isvectordef(def->def.ty.old_type, def->def.ty.rel))
92 storexdrfuncdecl(def->def_name, 1);
94 storexdrfuncdecl(def->def_name, 0);
100 print_funcdef(definition *def, int headeronly)
102 switch (def->def_kind) {
105 pprogramdef(def, headeronly);
112 /* store away enough information to allow the XDR functions to be spat
113 out at the end of the file */
116 storexdrfuncdecl(const char *name, int pointerp)
120 xdrptr = XALLOC(struct xdrfunc);
123 xdrptr->pointerp = pointerp;
126 if (xdrfunc_tail == NULL)
127 xdrfunc_head = xdrptr;
129 xdrfunc_tail->next = xdrptr;
130 xdrfunc_tail = xdrptr;
135 print_xdr_func_def(const char *name, int pointerp)
137 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
138 name, pointerp ? "*" : "");
143 pconstdef(definition *def)
145 pdefine(def->def_name, def->def.co);
148 /* print out the definitions for the arguments of functions in the
152 pargdef(definition *def)
159 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
160 for (plist = vers->procs; plist != NULL; plist = plist->next) {
161 if (!newstyle || plist->arg_num < 2)
162 continue; /* old style or single args */
164 name = plist->args.argname;
165 f_print(fout, "struct %s {\n", name);
166 for (l = plist->args.decls; l != NULL; l = l->next)
167 pdeclaration(name, &l->decl, 1, ";\n");
168 f_print(fout, "};\n");
169 f_print(fout, "typedef struct %s %s;\n", name, name);
170 storexdrfuncdecl(name, 1);
178 pstructdef(definition *def)
181 const char *name = def->def_name;
183 f_print(fout, "struct %s {\n", name);
184 for (l = def->def.st.decls; l != NULL; l = l->next)
185 pdeclaration(name, &l->decl, 1, ";\n");
187 f_print(fout, "};\n");
188 f_print(fout, "typedef struct %s %s;\n", name, name);
192 puniondef(definition *def)
195 const char *name = def->def_name;
198 f_print(fout, "struct %s {\n", name);
199 decl = &def->def.un.enum_decl;
200 if (streq(decl->type, "bool"))
201 f_print(fout, "\tbool_t %s;\n", decl->name);
203 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
205 f_print(fout, "\tunion {\n");
206 for (l = def->def.un.cases; l != NULL; l = l->next) {
207 if (l->contflag == 0)
208 pdeclaration(name, &l->case_decl, 2, ";\n");
210 decl = def->def.un.default_decl;
211 if (decl && !streq(decl->type, "void")) {
212 pdeclaration(name, decl, 2, ";\n");
214 f_print(fout, "\t} %s_u;\n", name);
215 f_print(fout, "};\n");
216 f_print(fout, "typedef struct %s %s;\n", name, name);
220 pdefine(const char *name, const char *num)
222 f_print(fout, "#define\t%s %s\n", name, num);
226 puldefine(const char *name, const char *num)
228 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
232 define_printed(proc_list *stop, version_list *start)
237 for (vers = start; vers != NULL; vers = vers->next) {
238 for (proc = vers->procs; proc != NULL; proc = proc->next) {
241 else if (streq(proc->proc_name, stop->proc_name))
250 pfreeprocdef(const char * name, const char *vers)
252 f_print(fout, "extern int ");
254 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
258 pdispatch(const char * name, const char *vers)
261 f_print(fout, "void ");
263 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
267 pprogramdef(definition *def, int headeronly)
275 puldefine(def->def_name, def->def.pr.prog_num);
276 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
279 "extern struct rpcgen_table %s_%s_table[];\n",
280 locase(def->def_name), vers->vers_num);
281 f_print(fout, "extern %s_%s_nproc;\n",
282 locase(def->def_name), vers->vers_num);
284 puldefine(vers->vers_name, vers->vers_num);
289 f_print(fout, "%s", ext);
290 pdispatch(def->def_name, vers->vers_num);
292 for (proc = vers->procs; proc != NULL; proc = proc->next) {
293 if (!define_printed(proc, def->def.pr.versions)) {
294 puldefine(proc->proc_name, proc->proc_num);
296 f_print(fout, "%s", ext);
297 pprocdef(proc, vers, "CLIENT *", 0);
298 f_print(fout, "%s", ext);
299 pprocdef(proc, vers, "struct svc_req *", 1);
301 pfreeprocdef(def->def_name, vers->vers_num);
306 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
309 /* Print MT style stubs */
311 f_print(fout, "bool_t ");
313 f_print(fout, "enum clnt_stat ");
315 ptype(proc->res_prefix, proc->res_type, 1);
319 pvname_svc(proc->proc_name, vp->vers_num);
321 pvname(proc->proc_name, vp->vers_num);
323 parglist(proc, addargtype);
328 /* print out argument list of procedure */
330 parglist(proc_list *proc, const char *addargtype)
335 if (proc->arg_num < 2 && newstyle &&
336 streq(proc->args.decls->decl.type, "void")) {
337 /* 0 argument in new style: do nothing*/
340 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
341 ptype(dl->decl.prefix, dl->decl.type, 1);
344 /* old style passes by reference */
350 ptype(proc->res_prefix, proc->res_type, 1);
351 f_print(fout, "*, ");
354 f_print(fout, "%s);\n", addargtype);
358 penumdef(definition *def)
360 const char *name = def->def_name;
362 const char *last = NULL;
365 f_print(fout, "enum %s {\n", name);
366 for (l = def->def.en.vals; l != NULL; l = l->next) {
367 f_print(fout, "\t%s", l->name);
369 f_print(fout, " = %s", l->assignment);
370 last = l->assignment;
374 f_print(fout, " = %d", count++);
376 f_print(fout, " = %s + %d", last, count++);
379 f_print(fout, ",\n");
383 f_print(fout, "};\n");
384 f_print(fout, "typedef enum %s %s;\n", name, name);
388 ptypedef(definition *def)
390 const char *name = def->def_name;
391 const char *old = def->def.ty.old_type;
392 char prefix[8]; /* enough to contain "struct ", including NUL */
393 relation rel = def->def.ty.rel;
395 if (!streq(name, old)) {
396 if (streq(old, "string")) {
399 } else if (streq(old, "opaque")) {
401 } else if (streq(old, "bool")) {
404 if (undefined2(old, name) && def->def.ty.old_prefix)
405 s_print(prefix, "%s ", def->def.ty.old_prefix);
408 f_print(fout, "typedef ");
411 f_print(fout, "struct {\n");
412 f_print(fout, "\tu_int %s_len;\n", name);
413 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
414 f_print(fout, "} %s", name);
417 f_print(fout, "%s%s *%s", prefix, old, name);
420 f_print(fout, "%s%s %s[%s]", prefix, old, name,
421 def->def.ty.array_max);
424 f_print(fout, "%s%s %s", prefix, old, name);
427 f_print(fout, ";\n");
432 pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
434 char buf[8]; /* enough to hold "struct ", include NUL */
438 if (streq(dec->type, "void"))
442 if (streq(dec->type, name) && !dec->prefix)
443 f_print(fout, "struct ");
445 if (streq(dec->type, "string")) {
446 f_print(fout, "char *%s", dec->name);
449 if (streq(dec->type, "bool")) {
451 } else if (streq(dec->type, "opaque")) {
455 s_print(buf, "%s ", dec->prefix);
462 f_print(fout, "%s%s %s", prefix, type, dec->name);
465 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
469 f_print(fout, "%s%s *%s", prefix, type, dec->name);
472 f_print(fout, "struct {\n");
474 f_print(fout, "\tu_int %s_len;\n", dec->name);
477 "\t%s%s *%s_val;\n", prefix, type, dec->name);
479 f_print(fout, "} %s", dec->name);
483 f_print(fout, separator);
487 undefined2(const char *type, const char *stop)
492 for (l = defined; l != NULL; l = l->next) {
493 def = (definition *) l->val;
494 if (def->def_kind != DEF_PROGRAM) {
495 if (streq(def->def_name, stop))
497 else if (streq(def->def_name, type))