2.10.90-26
[glibc.git] / sunrpc / rpc_hout.c
blob346e154219414909075a01e6e69c7b25ee864659
1 /*
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
6 * met:
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
35 #include <stdio.h>
36 #include <ctype.h>
37 #include "rpc_parse.h"
38 #include "rpc_util.h"
39 #include "proto.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 */
56 static void
57 storexdrfuncdecl (const char *name, int pointerp)
59 xdrfunc * xdrptr;
61 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
63 xdrptr->name = (char *)name;
64 xdrptr->pointerp = pointerp;
65 xdrptr->next = NULL;
67 if (xdrfunc_tail == NULL)
69 xdrfunc_head = xdrptr;
70 xdrfunc_tail = xdrptr;
72 else
74 xdrfunc_tail->next = xdrptr;
75 xdrfunc_tail = xdrptr;
80 * Print the C-version of an xdr definition
82 void
83 print_datadef (definition *def)
86 if (def->def_kind == DEF_PROGRAM) /* handle data only */
87 return;
89 if (def->def_kind != DEF_CONST)
91 f_print (fout, "\n");
93 switch (def->def_kind)
95 case DEF_STRUCT:
96 pstructdef (def);
97 break;
98 case DEF_UNION:
99 puniondef (def);
100 break;
101 case DEF_ENUM:
102 penumdef (def);
103 break;
104 case DEF_TYPEDEF:
105 ptypedef (def);
106 break;
107 case DEF_PROGRAM:
108 pprogramdef (def);
109 break;
110 case DEF_CONST:
111 pconstdef (def);
112 break;
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,
119 def->def.ty.rel));
124 void
125 print_funcdef (definition *def)
127 switch (def->def_kind)
129 case DEF_PROGRAM:
130 f_print (fout, "\n");
131 pprogramdef (def);
132 break;
133 default:
134 break;
135 /* ?... shouldn't happen I guess */
139 void
140 print_xdr_func_def (char *name, int pointerp, int i)
142 if (i == 2)
144 f_print (fout, "extern bool_t xdr_%s ();\n", name);
145 return;
147 else
148 f_print(fout, "extern bool_t xdr_%s (XDR *, %s%s);\n", name,
149 name, pointerp ? "*" : "");
152 static void
153 pconstdef (definition *def)
155 pdefine (def->def_name, def->def.co);
158 /* print out the definitions for the arguments of functions in the
159 header file
161 static void
162 pargdef (definition * def)
164 decl_list *l;
165 version_list *vers;
166 const char *name;
167 proc_list *plist;
169 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
171 for (plist = vers->procs; plist != NULL;
172 plist = plist->next)
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");
195 static void
196 pstructdef (definition *def)
198 decl_list *l;
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);
210 static void
211 puniondef (definition *def)
213 case_list *l;
214 const char *name = def->def_name;
215 declaration *decl;
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);
223 else
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);
243 static void
244 pdefine (const char *name, const char *num)
246 f_print (fout, "#define %s %s\n", name, num);
249 static int
250 define_printed (proc_list *stop, version_list *start)
252 version_list *vers;
253 proc_list *proc;
255 for (vers = start; vers != NULL; vers = vers->next)
257 for (proc = vers->procs; proc != NULL; proc = proc->next)
259 if (proc == stop)
261 return 0;
263 else if (streq (proc->proc_name, stop->proc_name))
265 return 1;
269 abort ();
270 /* NOTREACHED */
273 static void
274 pfreeprocdef (const char *name, const char *vers, int mode)
276 f_print (fout, "extern int ");
277 pvname (name, vers);
278 if (mode == 1)
279 f_print (fout,"_freeresult (SVCXPRT *, xdrproc_t, caddr_t);\n");
280 else
281 f_print (fout,"_freeresult ();\n");
284 static void
285 pprogramdef (definition *def)
287 version_list *vers;
288 proc_list *proc;
289 int i;
290 const char *ext;
292 pargdef (def);
294 pdefine (def->def_name, def->def.pr.prog_num);
295 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
297 if (tblflag)
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
308 * old K & R C
311 if(!Cflag)
313 ext = "extern ";
314 for (proc = vers->procs; proc != NULL;
315 proc = proc->next)
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);
324 if (mtflag)
326 f_print(fout, "%s", ext);
327 pprocdef (proc, vers, NULL, 1, 2);
330 pfreeprocdef (def->def_name, vers->vers_num, 2);
332 else
334 for (i = 1; i < 3; i++)
336 if (i == 1)
338 f_print (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
339 ext = "extern ";
341 else
343 f_print (fout, "\n#else /* K&R C */\n");
344 ext = "extern ";
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");
365 void
366 pprocdef (proc_list * proc, version_list * vp,
367 const char *addargtype, int server_p, int mode)
369 if (mtflag)
370 {/* Print MT style stubs */
371 if (server_p)
372 f_print (fout, "bool_t ");
373 else
374 f_print (fout, "enum clnt_stat ");
376 else
378 ptype (proc->res_prefix, proc->res_type, 1);
379 f_print (fout, "* ");
381 if (server_p)
382 pvname_svc (proc->proc_name, vp->vers_num);
383 else
384 pvname (proc->proc_name, vp->vers_num);
387 * mode 1 = ANSI-C, mode 2 = K&R C
389 if (mode == 1)
390 parglist (proc, addargtype);
391 else
392 f_print (fout, "();\n");
395 /* print out argument list of procedure */
396 static void
397 parglist (proc_list *proc, const char *addargtype)
399 decl_list *dl;
401 f_print(fout,"(");
402 if (proc->arg_num < 2 && newstyle &&
403 streq (proc->args.decls->decl.type, "void"))
405 /* 0 argument in new style: do nothing */
407 else
409 for (dl = proc->args.decls; dl != NULL; dl = dl->next)
411 ptype (dl->decl.prefix, dl->decl.type, 1);
412 if (!newstyle)
413 f_print (fout, "*"); /* old style passes by reference */
415 f_print (fout, ", ");
418 if (mtflag)
420 ptype(proc->res_prefix, proc->res_type, 1);
421 f_print(fout, "*, ");
424 f_print (fout, "%s);\n", addargtype);
427 static void
428 penumdef (definition *def)
430 const char *name = def->def_name;
431 enumval_list *l;
432 const char *last = NULL;
433 int count = 0;
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);
439 if (l->assignment)
441 f_print (fout, " = %s", l->assignment);
442 last = l->assignment;
443 count = 1;
445 else
447 if (last == NULL)
449 f_print (fout, " = %d", count++);
451 else
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);
462 static void
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"))
474 old = "char";
475 rel = REL_POINTER;
477 else if (streq (old, "opaque"))
479 old = "char";
481 else if (streq (old, "bool"))
483 old = "bool_t";
485 if (undefined2 (old, name) && def->def.ty.old_prefix)
487 s_print (prefix, "%s ", def->def.ty.old_prefix);
489 else
491 prefix[0] = 0;
493 f_print (fout, "typedef ");
494 switch (rel)
496 case REL_ARRAY:
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);
501 break;
502 case REL_POINTER:
503 f_print (fout, "%s%s *%s", prefix, old, name);
504 break;
505 case REL_VECTOR:
506 f_print (fout, "%s%s %s[%s]", prefix, old, name,
507 def->def.ty.array_max);
508 break;
509 case REL_ALIAS:
510 f_print (fout, "%s%s %s", prefix, old, name);
511 break;
513 f_print (fout, ";\n");
517 void
518 pdeclaration (const char *name, declaration * dec, int tab,
519 const char *separator)
521 char buf[8]; /* enough to hold "struct ", include NUL */
522 const char *prefix;
523 const char *type;
525 if (streq (dec->type, "void"))
527 return;
529 tabify (fout, tab);
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);
538 else
540 prefix = "";
541 if (streq (dec->type, "bool"))
543 type = "bool_t";
545 else if (streq (dec->type, "opaque"))
547 type = "char";
549 else
551 if (dec->prefix)
553 s_print (buf, "%s ", dec->prefix);
554 prefix = buf;
556 type = dec->type;
558 switch (dec->rel)
560 case REL_ALIAS:
561 f_print (fout, "%s%s %s", prefix, type, dec->name);
562 break;
563 case REL_VECTOR:
564 f_print (fout, "%s%s %s[%s]", prefix, type, dec->name,
565 dec->array_max);
566 break;
567 case REL_POINTER:
568 f_print (fout, "%s%s *%s", prefix, type, dec->name);
569 break;
570 case REL_ARRAY:
571 f_print (fout, "struct {\n");
572 tabify (fout, tab);
573 f_print (fout, "\tu_int %s_len;\n", dec->name);
574 tabify (fout, tab);
575 f_print (fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
576 tabify (fout, tab);
577 f_print (fout, "} %s", dec->name);
578 break;
581 f_print (fout, separator);
584 static int
585 undefined2 (const char *type, const char *stop)
587 list *l;
588 definition *def;
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))
597 return 1;
599 else if (streq (def->def_name, type))
601 return 0;
605 return 1;