2 * From: @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * rpc_hout.c, Header file outputter for the RPC protocol compiler
37 #include "rpc_parse.h"
41 static void pconstdef (definition * def);
42 static void pargdef (definition * def);
43 static void pstructdef (definition * def);
44 static void puniondef (definition * def);
45 static void pdefine (const char *name, const char *num);
46 static int define_printed (proc_list * stop, version_list * start);
47 static void pprogramdef (definition * def);
48 static void parglist (proc_list * proc, const char *addargtype);
49 static void penumdef (definition * def);
50 static void ptypedef (definition * def);
51 static int undefined2 (const char *type, const char *stop);
53 /* store away enough information to allow the XDR functions to be spat
54 out at the end of the file */
57 storexdrfuncdecl (const char *name, int pointerp)
61 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
63 xdrptr->name = (char *)name;
64 xdrptr->pointerp = pointerp;
67 if (xdrfunc_tail == NULL)
69 xdrfunc_head = xdrptr;
70 xdrfunc_tail = xdrptr;
74 xdrfunc_tail->next = xdrptr;
75 xdrfunc_tail = xdrptr;
80 * Print the C-version of an xdr definition
83 print_datadef (definition *def)
86 if (def->def_kind == DEF_PROGRAM) /* handle data only */
89 if (def->def_kind != DEF_CONST)
93 switch (def->def_kind)
114 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST)
116 storexdrfuncdecl(def->def_name,
117 def->def_kind != DEF_TYPEDEF ||
118 !isvectordef(def->def.ty.old_type,
125 print_funcdef (definition *def)
127 switch (def->def_kind)
130 f_print (fout, "\n");
135 /* ?... shouldn't happen I guess */
140 print_xdr_func_def (char *name, int pointerp, int i)
144 f_print (fout, "extern bool_t xdr_%s ();\n", name);
148 f_print(fout, "extern bool_t xdr_%s (XDR *, %s%s);\n", name,
149 name, pointerp ? "*" : "");
153 pconstdef (definition *def)
155 pdefine (def->def_name, def->def.co);
158 /* print out the definitions for the arguments of functions in the
162 pargdef (definition * def)
169 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
171 for (plist = vers->procs; plist != NULL;
175 if (!newstyle || plist->arg_num < 2)
177 continue; /* old style or single args */
179 name = plist->args.argname;
180 f_print (fout, "struct %s {\n", name);
181 for (l = plist->args.decls;
182 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);
188 storexdrfuncdecl (name, 1);
189 f_print (fout, "\n");
196 pstructdef (definition *def)
199 const char *name = def->def_name;
201 f_print (fout, "struct %s {\n", name);
202 for (l = def->def.st.decls; l != NULL; l = l->next)
204 pdeclaration (name, &l->decl, 1, ";\n");
206 f_print (fout, "};\n");
207 f_print (fout, "typedef struct %s %s;\n", name, name);
211 puniondef (definition *def)
214 const char *name = def->def_name;
217 f_print (fout, "struct %s {\n", name);
218 decl = &def->def.un.enum_decl;
219 if (streq (decl->type, "bool"))
221 f_print (fout, "\tbool_t %s;\n", decl->name);
225 f_print (fout, "\t%s %s;\n", decl->type, decl->name);
227 f_print (fout, "\tunion {\n");
228 for (l = def->def.un.cases; l != NULL; l = l->next)
230 if (l->contflag == 0)
231 pdeclaration (name, &l->case_decl, 2, ";\n");
233 decl = def->def.un.default_decl;
234 if (decl && !streq (decl->type, "void"))
236 pdeclaration (name, decl, 2, ";\n");
238 f_print (fout, "\t} %s_u;\n", name);
239 f_print (fout, "};\n");
240 f_print (fout, "typedef struct %s %s;\n", name, name);
244 pdefine (const char *name, const char *num)
246 f_print (fout, "#define %s %s\n", name, num);
250 define_printed (proc_list *stop, version_list *start)
255 for (vers = start; vers != NULL; vers = vers->next)
257 for (proc = vers->procs; proc != NULL; proc = proc->next)
263 else if (streq (proc->proc_name, stop->proc_name))
274 pfreeprocdef (const char *name, const char *vers, int mode)
276 f_print (fout, "extern int ");
279 f_print (fout,"_freeresult (SVCXPRT *, xdrproc_t, caddr_t);\n");
281 f_print (fout,"_freeresult ();\n");
285 pprogramdef (definition *def)
294 pdefine (def->def_name, def->def.pr.prog_num);
295 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
299 f_print (fout, "extern struct rpcgen_table %s_%s_table[];\n",
300 locase (def->def_name), vers->vers_num);
301 f_print (fout, "extern %s_%s_nproc;\n",
302 locase (def->def_name), vers->vers_num);
304 pdefine (vers->vers_name, vers->vers_num);
307 * Print out 2 definitions, one for ANSI-C, another for
314 for (proc = vers->procs; proc != NULL;
317 if (!define_printed(proc, def->def.pr.versions))
319 pdefine (proc->proc_name, proc->proc_num);
321 f_print (fout, "%s", ext);
322 pprocdef (proc, vers, NULL, 0, 2);
326 f_print(fout, "%s", ext);
327 pprocdef (proc, vers, NULL, 1, 2);
330 pfreeprocdef (def->def_name, vers->vers_num, 2);
334 for (i = 1; i < 3; i++)
338 f_print (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
343 f_print (fout, "\n#else /* K&R C */\n");
347 for (proc = vers->procs; proc != NULL; proc = proc->next)
349 if (!define_printed(proc, def->def.pr.versions))
351 pdefine(proc->proc_name, proc->proc_num);
353 f_print (fout, "%s", ext);
354 pprocdef (proc, vers, "CLIENT *", 0, i);
355 f_print (fout, "%s", ext);
356 pprocdef (proc, vers, "struct svc_req *", 1, i);
358 pfreeprocdef (def->def_name, vers->vers_num, i);
360 f_print (fout, "#endif /* K&R C */\n");
366 pprocdef (proc_list * proc, version_list * vp,
367 const char *addargtype, int server_p, int mode)
370 {/* Print MT style stubs */
372 f_print (fout, "bool_t ");
374 f_print (fout, "enum clnt_stat ");
378 ptype (proc->res_prefix, proc->res_type, 1);
379 f_print (fout, "* ");
382 pvname_svc (proc->proc_name, vp->vers_num);
384 pvname (proc->proc_name, vp->vers_num);
387 * mode 1 = ANSI-C, mode 2 = K&R C
390 parglist (proc, addargtype);
392 f_print (fout, "();\n");
395 /* print out argument list of procedure */
397 parglist (proc_list *proc, const char *addargtype)
402 if (proc->arg_num < 2 && newstyle &&
403 streq (proc->args.decls->decl.type, "void"))
405 /* 0 argument in new style: do nothing */
409 for (dl = proc->args.decls; dl != NULL; dl = dl->next)
411 ptype (dl->decl.prefix, dl->decl.type, 1);
413 f_print (fout, "*"); /* old style passes by reference */
415 f_print (fout, ", ");
420 ptype(proc->res_prefix, proc->res_type, 1);
421 f_print(fout, "*, ");
424 f_print (fout, "%s);\n", addargtype);
428 penumdef (definition *def)
430 const char *name = def->def_name;
432 const char *last = NULL;
435 f_print (fout, "enum %s {\n", name);
436 for (l = def->def.en.vals; l != NULL; l = l->next)
438 f_print (fout, "\t%s", l->name);
441 f_print (fout, " = %s", l->assignment);
442 last = l->assignment;
449 f_print (fout, " = %d", count++);
453 f_print (fout, " = %s + %d", last, count++);
456 f_print (fout, ",\n");
458 f_print (fout, "};\n");
459 f_print (fout, "typedef enum %s %s;\n", name, name);
463 ptypedef (definition *def)
465 const char *name = def->def_name;
466 const char *old = def->def.ty.old_type;
467 char prefix[8]; /* enough to contain "struct ", including NUL */
468 relation rel = def->def.ty.rel;
470 if (!streq (name, old))
472 if (streq (old, "string"))
477 else if (streq (old, "opaque"))
481 else if (streq (old, "bool"))
485 if (undefined2 (old, name) && def->def.ty.old_prefix)
487 s_print (prefix, "%s ", def->def.ty.old_prefix);
493 f_print (fout, "typedef ");
497 f_print (fout, "struct {\n");
498 f_print (fout, "\tu_int %s_len;\n", name);
499 f_print (fout, "\t%s%s *%s_val;\n", prefix, old, name);
500 f_print (fout, "} %s", name);
503 f_print (fout, "%s%s *%s", prefix, old, name);
506 f_print (fout, "%s%s %s[%s]", prefix, old, name,
507 def->def.ty.array_max);
510 f_print (fout, "%s%s %s", prefix, old, name);
513 f_print (fout, ";\n");
518 pdeclaration (const char *name, declaration * dec, int tab,
519 const char *separator)
521 char buf[8]; /* enough to hold "struct ", include NUL */
525 if (streq (dec->type, "void"))
530 if (streq (dec->type, name) && !dec->prefix)
532 f_print (fout, "struct ");
534 if (streq (dec->type, "string"))
536 f_print (fout, "char *%s", dec->name);
541 if (streq (dec->type, "bool"))
545 else if (streq (dec->type, "opaque"))
553 s_print (buf, "%s ", dec->prefix);
561 f_print (fout, "%s%s %s", prefix, type, dec->name);
564 f_print (fout, "%s%s %s[%s]", prefix, type, dec->name,
568 f_print (fout, "%s%s *%s", prefix, type, dec->name);
571 f_print (fout, "struct {\n");
573 f_print (fout, "\tu_int %s_len;\n", dec->name);
575 f_print (fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
577 f_print (fout, "} %s", dec->name);
581 f_print (fout, separator);
585 undefined2 (const char *type, const char *stop)
590 for (l = defined; l != NULL; l = l->next)
592 def = (definition *) l->val;
593 if (def->def_kind != DEF_PROGRAM)
595 if (streq (def->def_name, stop))
599 else if (streq (def->def_name, type))