Once again change RPC copyright notices.
[glibc.git] / sunrpc / rpc_hout.c
blob31b6d3a3fe646988238a339141c802414bb0a89e
1 /*
2 * From: @(#)rpc_hout.c 1.12 89/02/22
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_hout.c, Header file outputter for the RPC protocol compiler
36 #include <stdio.h>
37 #include <ctype.h>
38 #include "rpc_parse.h"
39 #include "rpc_util.h"
40 #include "proto.h"
42 static void pconstdef (definition * def);
43 static void pargdef (definition * def);
44 static void pstructdef (definition * def);
45 static void puniondef (definition * def);
46 static void pdefine (const char *name, const char *num);
47 static int define_printed (proc_list * stop, version_list * start);
48 static void pprogramdef (definition * def);
49 static void parglist (proc_list * proc, const char *addargtype);
50 static void penumdef (definition * def);
51 static void ptypedef (definition * def);
52 static int undefined2 (const char *type, const char *stop);
54 /* store away enough information to allow the XDR functions to be spat
55 out at the end of the file */
57 static void
58 storexdrfuncdecl (const char *name, int pointerp)
60 xdrfunc * xdrptr;
62 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
64 xdrptr->name = (char *)name;
65 xdrptr->pointerp = pointerp;
66 xdrptr->next = NULL;
68 if (xdrfunc_tail == NULL)
70 xdrfunc_head = xdrptr;
71 xdrfunc_tail = xdrptr;
73 else
75 xdrfunc_tail->next = xdrptr;
76 xdrfunc_tail = xdrptr;
81 * Print the C-version of an xdr definition
83 void
84 print_datadef (definition *def)
87 if (def->def_kind == DEF_PROGRAM) /* handle data only */
88 return;
90 if (def->def_kind != DEF_CONST)
92 f_print (fout, "\n");
94 switch (def->def_kind)
96 case DEF_STRUCT:
97 pstructdef (def);
98 break;
99 case DEF_UNION:
100 puniondef (def);
101 break;
102 case DEF_ENUM:
103 penumdef (def);
104 break;
105 case DEF_TYPEDEF:
106 ptypedef (def);
107 break;
108 case DEF_PROGRAM:
109 pprogramdef (def);
110 break;
111 case DEF_CONST:
112 pconstdef (def);
113 break;
115 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST)
117 storexdrfuncdecl(def->def_name,
118 def->def_kind != DEF_TYPEDEF ||
119 !isvectordef(def->def.ty.old_type,
120 def->def.ty.rel));
125 void
126 print_funcdef (definition *def)
128 switch (def->def_kind)
130 case DEF_PROGRAM:
131 f_print (fout, "\n");
132 pprogramdef (def);
133 break;
134 default:
135 break;
136 /* ?... shouldn't happen I guess */
140 void
141 print_xdr_func_def (char *name, int pointerp, int i)
143 if (i == 2)
145 f_print (fout, "extern bool_t xdr_%s ();\n", name);
146 return;
148 else
149 f_print(fout, "extern bool_t xdr_%s (XDR *, %s%s);\n", name,
150 name, pointerp ? "*" : "");
153 static void
154 pconstdef (definition *def)
156 pdefine (def->def_name, def->def.co);
159 /* print out the definitions for the arguments of functions in the
160 header file
162 static void
163 pargdef (definition * def)
165 decl_list *l;
166 version_list *vers;
167 const char *name;
168 proc_list *plist;
170 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
172 for (plist = vers->procs; plist != NULL;
173 plist = plist->next)
176 if (!newstyle || plist->arg_num < 2)
178 continue; /* old style or single args */
180 name = plist->args.argname;
181 f_print (fout, "struct %s {\n", name);
182 for (l = plist->args.decls;
183 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);
189 storexdrfuncdecl (name, 1);
190 f_print (fout, "\n");
196 static void
197 pstructdef (definition *def)
199 decl_list *l;
200 const char *name = def->def_name;
202 f_print (fout, "struct %s {\n", name);
203 for (l = def->def.st.decls; l != NULL; l = l->next)
205 pdeclaration (name, &l->decl, 1, ";\n");
207 f_print (fout, "};\n");
208 f_print (fout, "typedef struct %s %s;\n", name, name);
211 static void
212 puniondef (definition *def)
214 case_list *l;
215 const char *name = def->def_name;
216 declaration *decl;
218 f_print (fout, "struct %s {\n", name);
219 decl = &def->def.un.enum_decl;
220 if (streq (decl->type, "bool"))
222 f_print (fout, "\tbool_t %s;\n", decl->name);
224 else
226 f_print (fout, "\t%s %s;\n", decl->type, decl->name);
228 f_print (fout, "\tunion {\n");
229 for (l = def->def.un.cases; l != NULL; l = l->next)
231 if (l->contflag == 0)
232 pdeclaration (name, &l->case_decl, 2, ";\n");
234 decl = def->def.un.default_decl;
235 if (decl && !streq (decl->type, "void"))
237 pdeclaration (name, decl, 2, ";\n");
239 f_print (fout, "\t} %s_u;\n", name);
240 f_print (fout, "};\n");
241 f_print (fout, "typedef struct %s %s;\n", name, name);
244 static void
245 pdefine (const char *name, const char *num)
247 f_print (fout, "#define %s %s\n", name, num);
250 static int
251 define_printed (proc_list *stop, version_list *start)
253 version_list *vers;
254 proc_list *proc;
256 for (vers = start; vers != NULL; vers = vers->next)
258 for (proc = vers->procs; proc != NULL; proc = proc->next)
260 if (proc == stop)
262 return 0;
264 else if (streq (proc->proc_name, stop->proc_name))
266 return 1;
270 abort ();
271 /* NOTREACHED */
274 static void
275 pfreeprocdef (const char *name, const char *vers, int mode)
277 f_print (fout, "extern int ");
278 pvname (name, vers);
279 if (mode == 1)
280 f_print (fout,"_freeresult (SVCXPRT *, xdrproc_t, caddr_t);\n");
281 else
282 f_print (fout,"_freeresult ();\n");
285 static void
286 pprogramdef (definition *def)
288 version_list *vers;
289 proc_list *proc;
290 int i;
291 const char *ext;
293 pargdef (def);
295 pdefine (def->def_name, def->def.pr.prog_num);
296 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
298 if (tblflag)
300 f_print (fout, "extern struct rpcgen_table %s_%s_table[];\n",
301 locase (def->def_name), vers->vers_num);
302 f_print (fout, "extern %s_%s_nproc;\n",
303 locase (def->def_name), vers->vers_num);
305 pdefine (vers->vers_name, vers->vers_num);
308 * Print out 2 definitions, one for ANSI-C, another for
309 * old K & R C
312 if(!Cflag)
314 ext = "extern ";
315 for (proc = vers->procs; proc != NULL;
316 proc = proc->next)
318 if (!define_printed(proc, def->def.pr.versions))
320 pdefine (proc->proc_name, proc->proc_num);
322 f_print (fout, "%s", ext);
323 pprocdef (proc, vers, NULL, 0, 2);
325 if (mtflag)
327 f_print(fout, "%s", ext);
328 pprocdef (proc, vers, NULL, 1, 2);
331 pfreeprocdef (def->def_name, vers->vers_num, 2);
333 else
335 for (i = 1; i < 3; i++)
337 if (i == 1)
339 f_print (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
340 ext = "extern ";
342 else
344 f_print (fout, "\n#else /* K&R C */\n");
345 ext = "extern ";
348 for (proc = vers->procs; proc != NULL; proc = proc->next)
350 if (!define_printed(proc, def->def.pr.versions))
352 pdefine(proc->proc_name, proc->proc_num);
354 f_print (fout, "%s", ext);
355 pprocdef (proc, vers, "CLIENT *", 0, i);
356 f_print (fout, "%s", ext);
357 pprocdef (proc, vers, "struct svc_req *", 1, i);
359 pfreeprocdef (def->def_name, vers->vers_num, i);
361 f_print (fout, "#endif /* K&R C */\n");
366 void
367 pprocdef (proc_list * proc, version_list * vp,
368 const char *addargtype, int server_p, int mode)
370 if (mtflag)
371 {/* Print MT style stubs */
372 if (server_p)
373 f_print (fout, "bool_t ");
374 else
375 f_print (fout, "enum clnt_stat ");
377 else
379 ptype (proc->res_prefix, proc->res_type, 1);
380 f_print (fout, "* ");
382 if (server_p)
383 pvname_svc (proc->proc_name, vp->vers_num);
384 else
385 pvname (proc->proc_name, vp->vers_num);
388 * mode 1 = ANSI-C, mode 2 = K&R C
390 if (mode == 1)
391 parglist (proc, addargtype);
392 else
393 f_print (fout, "();\n");
396 /* print out argument list of procedure */
397 static void
398 parglist (proc_list *proc, const char *addargtype)
400 decl_list *dl;
402 f_print(fout,"(");
403 if (proc->arg_num < 2 && newstyle &&
404 streq (proc->args.decls->decl.type, "void"))
406 /* 0 argument in new style: do nothing */
408 else
410 for (dl = proc->args.decls; dl != NULL; dl = dl->next)
412 ptype (dl->decl.prefix, dl->decl.type, 1);
413 if (!newstyle)
414 f_print (fout, "*"); /* old style passes by reference */
416 f_print (fout, ", ");
419 if (mtflag)
421 ptype(proc->res_prefix, proc->res_type, 1);
422 f_print(fout, "*, ");
425 f_print (fout, "%s);\n", addargtype);
428 static void
429 penumdef (definition *def)
431 const char *name = def->def_name;
432 enumval_list *l;
433 const char *last = NULL;
434 int count = 0;
436 f_print (fout, "enum %s {\n", name);
437 for (l = def->def.en.vals; l != NULL; l = l->next)
439 f_print (fout, "\t%s", l->name);
440 if (l->assignment)
442 f_print (fout, " = %s", l->assignment);
443 last = l->assignment;
444 count = 1;
446 else
448 if (last == NULL)
450 f_print (fout, " = %d", count++);
452 else
454 f_print (fout, " = %s + %d", last, count++);
457 f_print (fout, ",\n");
459 f_print (fout, "};\n");
460 f_print (fout, "typedef enum %s %s;\n", name, name);
463 static void
464 ptypedef (definition *def)
466 const char *name = def->def_name;
467 const char *old = def->def.ty.old_type;
468 char prefix[8]; /* enough to contain "struct ", including NUL */
469 relation rel = def->def.ty.rel;
471 if (!streq (name, old))
473 if (streq (old, "string"))
475 old = "char";
476 rel = REL_POINTER;
478 else if (streq (old, "opaque"))
480 old = "char";
482 else if (streq (old, "bool"))
484 old = "bool_t";
486 if (undefined2 (old, name) && def->def.ty.old_prefix)
488 s_print (prefix, "%s ", def->def.ty.old_prefix);
490 else
492 prefix[0] = 0;
494 f_print (fout, "typedef ");
495 switch (rel)
497 case REL_ARRAY:
498 f_print (fout, "struct {\n");
499 f_print (fout, "\tu_int %s_len;\n", name);
500 f_print (fout, "\t%s%s *%s_val;\n", prefix, old, name);
501 f_print (fout, "} %s", name);
502 break;
503 case REL_POINTER:
504 f_print (fout, "%s%s *%s", prefix, old, name);
505 break;
506 case REL_VECTOR:
507 f_print (fout, "%s%s %s[%s]", prefix, old, name,
508 def->def.ty.array_max);
509 break;
510 case REL_ALIAS:
511 f_print (fout, "%s%s %s", prefix, old, name);
512 break;
514 f_print (fout, ";\n");
518 void
519 pdeclaration (const char *name, declaration * dec, int tab,
520 const char *separator)
522 char buf[8]; /* enough to hold "struct ", include NUL */
523 const char *prefix;
524 const char *type;
526 if (streq (dec->type, "void"))
528 return;
530 tabify (fout, tab);
531 if (streq (dec->type, name) && !dec->prefix)
533 f_print (fout, "struct ");
535 if (streq (dec->type, "string"))
537 f_print (fout, "char *%s", dec->name);
539 else
541 prefix = "";
542 if (streq (dec->type, "bool"))
544 type = "bool_t";
546 else if (streq (dec->type, "opaque"))
548 type = "char";
550 else
552 if (dec->prefix)
554 s_print (buf, "%s ", dec->prefix);
555 prefix = buf;
557 type = dec->type;
559 switch (dec->rel)
561 case REL_ALIAS:
562 f_print (fout, "%s%s %s", prefix, type, dec->name);
563 break;
564 case REL_VECTOR:
565 f_print (fout, "%s%s %s[%s]", prefix, type, dec->name,
566 dec->array_max);
567 break;
568 case REL_POINTER:
569 f_print (fout, "%s%s *%s", prefix, type, dec->name);
570 break;
571 case REL_ARRAY:
572 f_print (fout, "struct {\n");
573 tabify (fout, tab);
574 f_print (fout, "\tu_int %s_len;\n", dec->name);
575 tabify (fout, tab);
576 f_print (fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
577 tabify (fout, tab);
578 f_print (fout, "} %s", dec->name);
579 break;
582 f_print (fout, separator);
585 static int
586 undefined2 (const char *type, const char *stop)
588 list *l;
589 definition *def;
591 for (l = defined; l != NULL; l = l->next)
593 def = (definition *) l->val;
594 if (def->def_kind != DEF_PROGRAM)
596 if (streq (def->def_name, stop))
598 return 1;
600 else if (streq (def->def_name, type))
602 return 0;
606 return 1;