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>
39 * rpc_hout.c, Header file outputter for the RPC protocol compiler
40 * Copyright (C) 1987, Sun Microsystems, Inc.
44 #include "rpc_parse.h"
48 void storexdrfuncdecl(const char *, int );
49 static void pconstdef( definition * );
50 static void pstructdef( definition * );
51 static void puniondef( definition * );
52 static void pprogramdef( definition *, int );
53 static void penumdef( definition * );
54 static void ptypedef( definition * );
55 static void pdefine(const char *, const char *);
56 static int undefined2(const char *, const char *);
57 static void parglist(proc_list *, const char *);
58 static void pprocdef(proc_list *, version_list *, const char *, int);
61 * Print the C-version of an xdr definition
64 print_datadef(definition *def, int headeronly)
67 if (def->def_kind == DEF_PROGRAM) /* handle data only */
70 if (def->def_kind != DEF_CONST) {
73 switch (def->def_kind) {
87 pprogramdef(def, headeronly);
93 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
94 storexdrfuncdecl(def->def_name,
95 def->def_kind != DEF_TYPEDEF ||
96 !isvectordef(def->def.ty.old_type,
103 print_funcdef(definition *def, int headeronly)
105 switch (def->def_kind) {
108 pprogramdef(def, headeronly);
115 /* store away enough information to allow the XDR functions to be spat
116 out at the end of the file */
119 storexdrfuncdecl(const char *name, int pointerp)
123 xdrptr = XALLOC(struct xdrfunc);
126 xdrptr->pointerp = pointerp;
129 if (xdrfunc_tail == NULL){
130 xdrfunc_head = xdrptr;
131 xdrfunc_tail = xdrptr;
133 xdrfunc_tail->next = xdrptr;
134 xdrfunc_tail = xdrptr;
141 print_xdr_func_def(const char *name, int pointerp)
143 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
144 name, pointerp ? "*" : "");
149 pconstdef(definition *def)
151 pdefine(def->def_name, def->def.co);
154 /* print out the definitions for the arguments of functions in the
158 pargdef(definition *def)
166 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
167 for (plist = vers->procs; plist != NULL;
168 plist = plist->next) {
170 if (!newstyle || plist->arg_num < 2) {
171 continue; /* old style or single args */
173 name = plist->args.argname;
174 f_print(fout, "struct %s {\n", name);
175 for (l = plist->args.decls;
176 l != NULL; l = l->next) {
177 pdeclaration(name, &l->decl, 1,
180 f_print(fout, "};\n");
181 f_print(fout, "typedef struct %s %s;\n",
183 storexdrfuncdecl(name, 1);
191 pstructdef(definition *def)
194 const char *name = def->def_name;
196 f_print(fout, "struct %s {\n", name);
197 for (l = def->def.st.decls; l != NULL; l = l->next) {
198 pdeclaration(name, &l->decl, 1, ";\n");
200 f_print(fout, "};\n");
201 f_print(fout, "typedef struct %s %s;\n", name, name);
205 puniondef(definition *def)
208 const char *name = def->def_name;
211 f_print(fout, "struct %s {\n", name);
212 decl = &def->def.un.enum_decl;
213 if (streq(decl->type, "bool")) {
214 f_print(fout, "\tbool_t %s;\n", decl->name);
216 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
218 f_print(fout, "\tunion {\n");
219 for (l = def->def.un.cases; l != NULL; l = l->next) {
220 if (l->contflag == 0)
221 pdeclaration(name, &l->case_decl, 2, ";\n");
223 decl = def->def.un.default_decl;
224 if (decl && !streq(decl->type, "void")) {
225 pdeclaration(name, decl, 2, ";\n");
227 f_print(fout, "\t} %s_u;\n", name);
228 f_print(fout, "};\n");
229 f_print(fout, "typedef struct %s %s;\n", name, name);
233 pdefine(const char *name, const char *num)
235 f_print(fout, "#define\t%s %s\n", name, num);
239 puldefine(const char *name, const char *num)
241 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
245 define_printed(proc_list *stop, version_list *start)
250 for (vers = start; vers != NULL; vers = vers->next) {
251 for (proc = vers->procs; proc != NULL; proc = proc->next) {
254 } else if (streq(proc->proc_name, stop->proc_name)) {
264 pfreeprocdef(const char * name, const char *vers)
266 f_print(fout, "extern int ");
268 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
272 pdispatch(const char * name, const char *vers)
275 f_print(fout, "void ");
277 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
281 pprogramdef(definition *def, int headeronly)
289 puldefine(def->def_name, def->def.pr.prog_num);
290 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
293 "extern struct rpcgen_table %s_%s_table[];\n",
294 locase(def->def_name), vers->vers_num);
296 "extern %s_%s_nproc;\n",
297 locase(def->def_name), vers->vers_num);
299 puldefine(vers->vers_name, vers->vers_num);
304 f_print(fout, "%s", ext);
305 pdispatch(def->def_name, vers->vers_num);
307 for (proc = vers->procs; proc != NULL; proc = proc->next) {
308 if (!define_printed(proc, def->def.pr.versions)) {
309 puldefine(proc->proc_name, proc->proc_num);
311 f_print(fout, "%s", ext);
312 pprocdef(proc, vers, "CLIENT *", 0);
313 f_print(fout, "%s", ext);
314 pprocdef(proc, vers, "struct svc_req *", 1);
316 pfreeprocdef(def->def_name, vers->vers_num);
321 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
323 if (mtflag) {/* Print MT style stubs */
325 f_print(fout, "bool_t ");
327 f_print(fout, "enum clnt_stat ");
329 ptype(proc->res_prefix, proc->res_type, 1);
333 pvname_svc(proc->proc_name, vp->vers_num);
335 pvname(proc->proc_name, vp->vers_num);
337 parglist(proc, addargtype);
342 /* print out argument list of procedure */
344 parglist(proc_list *proc, const char *addargtype)
349 if (proc->arg_num < 2 && newstyle &&
350 streq(proc->args.decls->decl.type, "void")) {
351 /* 0 argument in new style: do nothing*/
354 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
355 ptype(dl->decl.prefix, dl->decl.type, 1);
358 /* old style passes by reference */
364 ptype(proc->res_prefix, proc->res_type, 1);
365 f_print(fout, "*, ");
368 f_print(fout, "%s);\n", addargtype);
373 penumdef(definition *def)
375 const char *name = def->def_name;
377 const char *last = NULL;
380 f_print(fout, "enum %s {\n", name);
381 for (l = def->def.en.vals; l != NULL; l = l->next) {
382 f_print(fout, "\t%s", l->name);
384 f_print(fout, " = %s", l->assignment);
385 last = l->assignment;
389 f_print(fout, " = %d", count++);
391 f_print(fout, " = %s + %d", last, count++);
395 f_print(fout, ",\n");
399 f_print(fout, "};\n");
400 f_print(fout, "typedef enum %s %s;\n", name, name);
404 ptypedef(definition *def)
406 const char *name = def->def_name;
407 const char *old = def->def.ty.old_type;
408 char prefix[8]; /* enough to contain "struct ", including NUL */
409 relation rel = def->def.ty.rel;
412 if (!streq(name, old)) {
413 if (streq(old, "string")) {
416 } else if (streq(old, "opaque")) {
418 } else if (streq(old, "bool")) {
421 if (undefined2(old, name) && def->def.ty.old_prefix) {
422 s_print(prefix, "%s ", def->def.ty.old_prefix);
426 f_print(fout, "typedef ");
429 f_print(fout, "struct {\n");
430 f_print(fout, "\tu_int %s_len;\n", name);
431 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
432 f_print(fout, "} %s", name);
435 f_print(fout, "%s%s *%s", prefix, old, name);
438 f_print(fout, "%s%s %s[%s]", prefix, old, name,
439 def->def.ty.array_max);
442 f_print(fout, "%s%s %s", prefix, old, name);
445 f_print(fout, ";\n");
450 pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
452 char buf[8]; /* enough to hold "struct ", include NUL */
456 if (streq(dec->type, "void")) {
460 if (streq(dec->type, name) && !dec->prefix) {
461 f_print(fout, "struct ");
463 if (streq(dec->type, "string")) {
464 f_print(fout, "char *%s", dec->name);
467 if (streq(dec->type, "bool")) {
469 } else if (streq(dec->type, "opaque")) {
473 s_print(buf, "%s ", dec->prefix);
480 f_print(fout, "%s%s %s", prefix, type, dec->name);
483 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
487 f_print(fout, "%s%s *%s", prefix, type, dec->name);
490 f_print(fout, "struct {\n");
492 f_print(fout, "\tu_int %s_len;\n", dec->name);
495 "\t%s%s *%s_val;\n", prefix, type, dec->name);
497 f_print(fout, "} %s", dec->name);
501 fputs(separator, fout);
505 undefined2(const char *type, const char *stop)
510 for (l = defined; l != NULL; l = l->next) {
511 def = (definition *) l->val;
512 if (def->def_kind != DEF_PROGRAM) {
513 if (streq(def->def_name, stop)) {
515 } else if (streq(def->def_name, type)) {