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
31 * rpc_hout.c, Header file outputter for the RPC protocol compiler
32 * Copyright (C) 1987, Sun Microsystems, Inc.
36 #include "rpc_parse.h"
40 void storexdrfuncdecl(const char *, int );
41 static void pconstdef( definition * );
42 static void pstructdef( definition * );
43 static void puniondef( definition * );
44 static void pprogramdef( definition *, int );
45 static void penumdef( definition * );
46 static void ptypedef( definition * );
47 static void pdefine(const char *, const char *);
48 static int undefined2(const char *, const char *);
49 static void parglist(proc_list *, const char *);
50 static void pprocdef(proc_list *, version_list *, const char *, int);
53 * Print the C-version of an xdr definition
56 print_datadef(definition *def, int headeronly)
59 if (def->def_kind == DEF_PROGRAM) /* handle data only */
62 if (def->def_kind != DEF_CONST) {
65 switch (def->def_kind) {
79 pprogramdef(def, headeronly);
85 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
86 storexdrfuncdecl(def->def_name,
87 def->def_kind != DEF_TYPEDEF ||
88 !isvectordef(def->def.ty.old_type,
95 print_funcdef(definition *def, int headeronly)
97 switch (def->def_kind) {
100 pprogramdef(def, headeronly);
107 /* store away enough information to allow the XDR functions to be spat
108 out at the end of the file */
111 storexdrfuncdecl(const char *name, int pointerp)
115 xdrptr = XALLOC(struct xdrfunc);
118 xdrptr->pointerp = pointerp;
121 if (xdrfunc_tail == NULL){
122 xdrfunc_head = xdrptr;
123 xdrfunc_tail = xdrptr;
125 xdrfunc_tail->next = xdrptr;
126 xdrfunc_tail = xdrptr;
133 print_xdr_func_def(const char *name, int pointerp)
135 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
136 name, pointerp ? "*" : "");
141 pconstdef(definition *def)
143 pdefine(def->def_name, def->def.co);
146 /* print out the definitions for the arguments of functions in the
150 pargdef(definition *def)
158 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
159 for (plist = vers->procs; plist != NULL;
160 plist = plist->next) {
162 if (!newstyle || plist->arg_num < 2) {
163 continue; /* old style or single args */
165 name = plist->args.argname;
166 f_print(fout, "struct %s {\n", name);
167 for (l = plist->args.decls;
168 l != NULL; l = l->next) {
169 pdeclaration(name, &l->decl, 1,
172 f_print(fout, "};\n");
173 f_print(fout, "typedef struct %s %s;\n",
175 storexdrfuncdecl(name, 1);
183 pstructdef(definition *def)
186 const char *name = def->def_name;
188 f_print(fout, "struct %s {\n", name);
189 for (l = def->def.st.decls; l != NULL; l = l->next) {
190 pdeclaration(name, &l->decl, 1, ";\n");
192 f_print(fout, "};\n");
193 f_print(fout, "typedef struct %s %s;\n", name, name);
197 puniondef(definition *def)
200 const char *name = def->def_name;
203 f_print(fout, "struct %s {\n", name);
204 decl = &def->def.un.enum_decl;
205 if (streq(decl->type, "bool")) {
206 f_print(fout, "\tbool_t %s;\n", decl->name);
208 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
210 f_print(fout, "\tunion {\n");
211 for (l = def->def.un.cases; l != NULL; l = l->next) {
212 if (l->contflag == 0)
213 pdeclaration(name, &l->case_decl, 2, ";\n");
215 decl = def->def.un.default_decl;
216 if (decl && !streq(decl->type, "void")) {
217 pdeclaration(name, decl, 2, ";\n");
219 f_print(fout, "\t} %s_u;\n", name);
220 f_print(fout, "};\n");
221 f_print(fout, "typedef struct %s %s;\n", name, name);
225 pdefine(const char *name, const char *num)
227 f_print(fout, "#define\t%s %s\n", name, num);
231 puldefine(const char *name, const char *num)
233 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
237 define_printed(proc_list *stop, version_list *start)
242 for (vers = start; vers != NULL; vers = vers->next) {
243 for (proc = vers->procs; proc != NULL; proc = proc->next) {
246 } else if (streq(proc->proc_name, stop->proc_name)) {
256 pfreeprocdef(const char * name, const char *vers)
258 f_print(fout, "extern int ");
260 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
264 pdispatch(const char * name, const char *vers)
267 f_print(fout, "void ");
269 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
273 pprogramdef(definition *def, int headeronly)
281 puldefine(def->def_name, def->def.pr.prog_num);
282 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
285 "extern struct rpcgen_table %s_%s_table[];\n",
286 locase(def->def_name), vers->vers_num);
288 "extern %s_%s_nproc;\n",
289 locase(def->def_name), vers->vers_num);
291 puldefine(vers->vers_name, vers->vers_num);
296 f_print(fout, "%s", ext);
297 pdispatch(def->def_name, vers->vers_num);
299 for (proc = vers->procs; proc != NULL; proc = proc->next) {
300 if (!define_printed(proc, def->def.pr.versions)) {
301 puldefine(proc->proc_name, proc->proc_num);
303 f_print(fout, "%s", ext);
304 pprocdef(proc, vers, "CLIENT *", 0);
305 f_print(fout, "%s", ext);
306 pprocdef(proc, vers, "struct svc_req *", 1);
308 pfreeprocdef(def->def_name, vers->vers_num);
313 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
315 if (mtflag) {/* Print MT style stubs */
317 f_print(fout, "bool_t ");
319 f_print(fout, "enum clnt_stat ");
321 ptype(proc->res_prefix, proc->res_type, 1);
325 pvname_svc(proc->proc_name, vp->vers_num);
327 pvname(proc->proc_name, vp->vers_num);
329 parglist(proc, addargtype);
334 /* print out argument list of procedure */
336 parglist(proc_list *proc, const char *addargtype)
341 if (proc->arg_num < 2 && newstyle &&
342 streq(proc->args.decls->decl.type, "void")) {
343 /* 0 argument in new style: do nothing*/
346 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
347 ptype(dl->decl.prefix, dl->decl.type, 1);
350 /* old style passes by reference */
356 ptype(proc->res_prefix, proc->res_type, 1);
357 f_print(fout, "*, ");
360 f_print(fout, "%s);\n", addargtype);
365 penumdef(definition *def)
367 const char *name = def->def_name;
369 const char *last = NULL;
372 f_print(fout, "enum %s {\n", name);
373 for (l = def->def.en.vals; l != NULL; l = l->next) {
374 f_print(fout, "\t%s", l->name);
376 f_print(fout, " = %s", l->assignment);
377 last = l->assignment;
381 f_print(fout, " = %d", count++);
383 f_print(fout, " = %s + %d", last, count++);
387 f_print(fout, ",\n");
391 f_print(fout, "};\n");
392 f_print(fout, "typedef enum %s %s;\n", name, name);
396 ptypedef(definition *def)
398 const char *name = def->def_name;
399 const char *old = def->def.ty.old_type;
400 char prefix[8]; /* enough to contain "struct ", including NUL */
401 relation rel = def->def.ty.rel;
404 if (!streq(name, old)) {
405 if (streq(old, "string")) {
408 } else if (streq(old, "opaque")) {
410 } else if (streq(old, "bool")) {
413 if (undefined2(old, name) && def->def.ty.old_prefix) {
414 s_print(prefix, "%s ", def->def.ty.old_prefix);
418 f_print(fout, "typedef ");
421 f_print(fout, "struct {\n");
422 f_print(fout, "\tu_int %s_len;\n", name);
423 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
424 f_print(fout, "} %s", name);
427 f_print(fout, "%s%s *%s", prefix, old, name);
430 f_print(fout, "%s%s %s[%s]", prefix, old, name,
431 def->def.ty.array_max);
434 f_print(fout, "%s%s %s", prefix, old, name);
437 f_print(fout, ";\n");
442 pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
444 char buf[8]; /* enough to hold "struct ", include NUL */
448 if (streq(dec->type, "void")) {
452 if (streq(dec->type, name) && !dec->prefix) {
453 f_print(fout, "struct ");
455 if (streq(dec->type, "string")) {
456 f_print(fout, "char *%s", dec->name);
459 if (streq(dec->type, "bool")) {
461 } else if (streq(dec->type, "opaque")) {
465 s_print(buf, "%s ", dec->prefix);
472 f_print(fout, "%s%s %s", prefix, type, dec->name);
475 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
479 f_print(fout, "%s%s *%s", prefix, type, dec->name);
482 f_print(fout, "struct {\n");
484 f_print(fout, "\tu_int %s_len;\n", dec->name);
487 "\t%s%s *%s_val;\n", prefix, type, dec->name);
489 f_print(fout, "} %s", dec->name);
493 fputs(separator, fout);
497 undefined2(const char *type, const char *stop)
502 for (l = defined; l != NULL; l = l->next) {
503 def = (definition *) l->val;
504 if (def->def_kind != DEF_PROGRAM) {
505 if (streq(def->def_name, stop)) {
507 } else if (streq(def->def_name, type)) {