2.9
[glibc/nacl-glibc.git] / sunrpc / rpc_hout.c
blob270d149a6ef2739b6aa4148f4bfcde1a5ff2cbdd
1 /*
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 or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From: @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
36 * rpc_hout.c, Header file outputter for the RPC protocol compiler
38 #include <stdio.h>
39 #include <ctype.h>
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
42 #include "proto.h"
44 static void pconstdef (definition * def);
45 static void pargdef (definition * def);
46 static void pstructdef (definition * def);
47 static void puniondef (definition * def);
48 static void pdefine (const char *name, const char *num);
49 static int define_printed (proc_list * stop, version_list * start);
50 static void pprogramdef (definition * def);
51 static void parglist (proc_list * proc, const char *addargtype);
52 static void penumdef (definition * def);
53 static void ptypedef (definition * def);
54 static int undefined2 (const char *type, const char *stop);
56 /* store away enough information to allow the XDR functions to be spat
57 out at the end of the file */
59 static void
60 storexdrfuncdecl (const char *name, int pointerp)
62 xdrfunc * xdrptr;
64 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
66 xdrptr->name = (char *)name;
67 xdrptr->pointerp = pointerp;
68 xdrptr->next = NULL;
70 if (xdrfunc_tail == NULL)
72 xdrfunc_head = xdrptr;
73 xdrfunc_tail = xdrptr;
75 else
77 xdrfunc_tail->next = xdrptr;
78 xdrfunc_tail = xdrptr;
83 * Print the C-version of an xdr definition
85 void
86 print_datadef (definition *def)
89 if (def->def_kind == DEF_PROGRAM) /* handle data only */
90 return;
92 if (def->def_kind != DEF_CONST)
94 f_print (fout, "\n");
96 switch (def->def_kind)
98 case DEF_STRUCT:
99 pstructdef (def);
100 break;
101 case DEF_UNION:
102 puniondef (def);
103 break;
104 case DEF_ENUM:
105 penumdef (def);
106 break;
107 case DEF_TYPEDEF:
108 ptypedef (def);
109 break;
110 case DEF_PROGRAM:
111 pprogramdef (def);
112 break;
113 case DEF_CONST:
114 pconstdef (def);
115 break;
117 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST)
119 storexdrfuncdecl(def->def_name,
120 def->def_kind != DEF_TYPEDEF ||
121 !isvectordef(def->def.ty.old_type,
122 def->def.ty.rel));
127 void
128 print_funcdef (definition *def)
130 switch (def->def_kind)
132 case DEF_PROGRAM:
133 f_print (fout, "\n");
134 pprogramdef (def);
135 break;
136 default:
137 break;
138 /* ?... shouldn't happen I guess */
142 void
143 print_xdr_func_def (char *name, int pointerp, int i)
145 if (i == 2)
147 f_print (fout, "extern bool_t xdr_%s ();\n", name);
148 return;
150 else
151 f_print(fout, "extern bool_t xdr_%s (XDR *, %s%s);\n", name,
152 name, pointerp ? "*" : "");
155 static void
156 pconstdef (definition *def)
158 pdefine (def->def_name, def->def.co);
161 /* print out the definitions for the arguments of functions in the
162 header file
164 static void
165 pargdef (definition * def)
167 decl_list *l;
168 version_list *vers;
169 const char *name;
170 proc_list *plist;
172 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
174 for (plist = vers->procs; plist != NULL;
175 plist = plist->next)
178 if (!newstyle || plist->arg_num < 2)
180 continue; /* old style or single args */
182 name = plist->args.argname;
183 f_print (fout, "struct %s {\n", name);
184 for (l = plist->args.decls;
185 l != NULL; l = l->next)
187 pdeclaration (name, &l->decl, 1, ";\n");
189 f_print (fout, "};\n");
190 f_print (fout, "typedef struct %s %s;\n", name, name);
191 storexdrfuncdecl (name, 1);
192 f_print (fout, "\n");
198 static void
199 pstructdef (definition *def)
201 decl_list *l;
202 const char *name = def->def_name;
204 f_print (fout, "struct %s {\n", name);
205 for (l = def->def.st.decls; l != NULL; l = l->next)
207 pdeclaration (name, &l->decl, 1, ";\n");
209 f_print (fout, "};\n");
210 f_print (fout, "typedef struct %s %s;\n", name, name);
213 static void
214 puniondef (definition *def)
216 case_list *l;
217 const char *name = def->def_name;
218 declaration *decl;
220 f_print (fout, "struct %s {\n", name);
221 decl = &def->def.un.enum_decl;
222 if (streq (decl->type, "bool"))
224 f_print (fout, "\tbool_t %s;\n", decl->name);
226 else
228 f_print (fout, "\t%s %s;\n", decl->type, decl->name);
230 f_print (fout, "\tunion {\n");
231 for (l = def->def.un.cases; l != NULL; l = l->next)
233 if (l->contflag == 0)
234 pdeclaration (name, &l->case_decl, 2, ";\n");
236 decl = def->def.un.default_decl;
237 if (decl && !streq (decl->type, "void"))
239 pdeclaration (name, decl, 2, ";\n");
241 f_print (fout, "\t} %s_u;\n", name);
242 f_print (fout, "};\n");
243 f_print (fout, "typedef struct %s %s;\n", name, name);
246 static void
247 pdefine (const char *name, const char *num)
249 f_print (fout, "#define %s %s\n", name, num);
252 static int
253 define_printed (proc_list *stop, version_list *start)
255 version_list *vers;
256 proc_list *proc;
258 for (vers = start; vers != NULL; vers = vers->next)
260 for (proc = vers->procs; proc != NULL; proc = proc->next)
262 if (proc == stop)
264 return 0;
266 else if (streq (proc->proc_name, stop->proc_name))
268 return 1;
272 abort ();
273 /* NOTREACHED */
276 static void
277 pfreeprocdef (const char *name, const char *vers, int mode)
279 f_print (fout, "extern int ");
280 pvname (name, vers);
281 if (mode == 1)
282 f_print (fout,"_freeresult (SVCXPRT *, xdrproc_t, caddr_t);\n");
283 else
284 f_print (fout,"_freeresult ();\n");
287 static void
288 pprogramdef (definition *def)
290 version_list *vers;
291 proc_list *proc;
292 int i;
293 const char *ext;
295 pargdef (def);
297 pdefine (def->def_name, def->def.pr.prog_num);
298 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
300 if (tblflag)
302 f_print (fout, "extern struct rpcgen_table %s_%s_table[];\n",
303 locase (def->def_name), vers->vers_num);
304 f_print (fout, "extern %s_%s_nproc;\n",
305 locase (def->def_name), vers->vers_num);
307 pdefine (vers->vers_name, vers->vers_num);
310 * Print out 2 definitions, one for ANSI-C, another for
311 * old K & R C
314 if(!Cflag)
316 ext = "extern ";
317 for (proc = vers->procs; proc != NULL;
318 proc = proc->next)
320 if (!define_printed(proc, def->def.pr.versions))
322 pdefine (proc->proc_name, proc->proc_num);
324 f_print (fout, "%s", ext);
325 pprocdef (proc, vers, NULL, 0, 2);
327 if (mtflag)
329 f_print(fout, "%s", ext);
330 pprocdef (proc, vers, NULL, 1, 2);
333 pfreeprocdef (def->def_name, vers->vers_num, 2);
335 else
337 for (i = 1; i < 3; i++)
339 if (i == 1)
341 f_print (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
342 ext = "extern ";
344 else
346 f_print (fout, "\n#else /* K&R C */\n");
347 ext = "extern ";
350 for (proc = vers->procs; proc != NULL; proc = proc->next)
352 if (!define_printed(proc, def->def.pr.versions))
354 pdefine(proc->proc_name, proc->proc_num);
356 f_print (fout, "%s", ext);
357 pprocdef (proc, vers, "CLIENT *", 0, i);
358 f_print (fout, "%s", ext);
359 pprocdef (proc, vers, "struct svc_req *", 1, i);
361 pfreeprocdef (def->def_name, vers->vers_num, i);
363 f_print (fout, "#endif /* K&R C */\n");
368 void
369 pprocdef (proc_list * proc, version_list * vp,
370 const char *addargtype, int server_p, int mode)
372 if (mtflag)
373 {/* Print MT style stubs */
374 if (server_p)
375 f_print (fout, "bool_t ");
376 else
377 f_print (fout, "enum clnt_stat ");
379 else
381 ptype (proc->res_prefix, proc->res_type, 1);
382 f_print (fout, "* ");
384 if (server_p)
385 pvname_svc (proc->proc_name, vp->vers_num);
386 else
387 pvname (proc->proc_name, vp->vers_num);
390 * mode 1 = ANSI-C, mode 2 = K&R C
392 if (mode == 1)
393 parglist (proc, addargtype);
394 else
395 f_print (fout, "();\n");
398 /* print out argument list of procedure */
399 static void
400 parglist (proc_list *proc, const char *addargtype)
402 decl_list *dl;
404 f_print(fout,"(");
405 if (proc->arg_num < 2 && newstyle &&
406 streq (proc->args.decls->decl.type, "void"))
408 /* 0 argument in new style: do nothing */
410 else
412 for (dl = proc->args.decls; dl != NULL; dl = dl->next)
414 ptype (dl->decl.prefix, dl->decl.type, 1);
415 if (!newstyle)
416 f_print (fout, "*"); /* old style passes by reference */
418 f_print (fout, ", ");
421 if (mtflag)
423 ptype(proc->res_prefix, proc->res_type, 1);
424 f_print(fout, "*, ");
427 f_print (fout, "%s);\n", addargtype);
430 static void
431 penumdef (definition *def)
433 const char *name = def->def_name;
434 enumval_list *l;
435 const char *last = NULL;
436 int count = 0;
438 f_print (fout, "enum %s {\n", name);
439 for (l = def->def.en.vals; l != NULL; l = l->next)
441 f_print (fout, "\t%s", l->name);
442 if (l->assignment)
444 f_print (fout, " = %s", l->assignment);
445 last = l->assignment;
446 count = 1;
448 else
450 if (last == NULL)
452 f_print (fout, " = %d", count++);
454 else
456 f_print (fout, " = %s + %d", last, count++);
459 f_print (fout, ",\n");
461 f_print (fout, "};\n");
462 f_print (fout, "typedef enum %s %s;\n", name, name);
465 static void
466 ptypedef (definition *def)
468 const char *name = def->def_name;
469 const char *old = def->def.ty.old_type;
470 char prefix[8]; /* enough to contain "struct ", including NUL */
471 relation rel = def->def.ty.rel;
473 if (!streq (name, old))
475 if (streq (old, "string"))
477 old = "char";
478 rel = REL_POINTER;
480 else if (streq (old, "opaque"))
482 old = "char";
484 else if (streq (old, "bool"))
486 old = "bool_t";
488 if (undefined2 (old, name) && def->def.ty.old_prefix)
490 s_print (prefix, "%s ", def->def.ty.old_prefix);
492 else
494 prefix[0] = 0;
496 f_print (fout, "typedef ");
497 switch (rel)
499 case REL_ARRAY:
500 f_print (fout, "struct {\n");
501 f_print (fout, "\tu_int %s_len;\n", name);
502 f_print (fout, "\t%s%s *%s_val;\n", prefix, old, name);
503 f_print (fout, "} %s", name);
504 break;
505 case REL_POINTER:
506 f_print (fout, "%s%s *%s", prefix, old, name);
507 break;
508 case REL_VECTOR:
509 f_print (fout, "%s%s %s[%s]", prefix, old, name,
510 def->def.ty.array_max);
511 break;
512 case REL_ALIAS:
513 f_print (fout, "%s%s %s", prefix, old, name);
514 break;
516 f_print (fout, ";\n");
520 void
521 pdeclaration (const char *name, declaration * dec, int tab,
522 const char *separator)
524 char buf[8]; /* enough to hold "struct ", include NUL */
525 const char *prefix;
526 const char *type;
528 if (streq (dec->type, "void"))
530 return;
532 tabify (fout, tab);
533 if (streq (dec->type, name) && !dec->prefix)
535 f_print (fout, "struct ");
537 if (streq (dec->type, "string"))
539 f_print (fout, "char *%s", dec->name);
541 else
543 prefix = "";
544 if (streq (dec->type, "bool"))
546 type = "bool_t";
548 else if (streq (dec->type, "opaque"))
550 type = "char";
552 else
554 if (dec->prefix)
556 s_print (buf, "%s ", dec->prefix);
557 prefix = buf;
559 type = dec->type;
561 switch (dec->rel)
563 case REL_ALIAS:
564 f_print (fout, "%s%s %s", prefix, type, dec->name);
565 break;
566 case REL_VECTOR:
567 f_print (fout, "%s%s %s[%s]", prefix, type, dec->name,
568 dec->array_max);
569 break;
570 case REL_POINTER:
571 f_print (fout, "%s%s *%s", prefix, type, dec->name);
572 break;
573 case REL_ARRAY:
574 f_print (fout, "struct {\n");
575 tabify (fout, tab);
576 f_print (fout, "\tu_int %s_len;\n", dec->name);
577 tabify (fout, tab);
578 f_print (fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
579 tabify (fout, tab);
580 f_print (fout, "} %s", dec->name);
581 break;
584 f_print (fout, separator);
587 static int
588 undefined2 (const char *type, const char *stop)
590 list *l;
591 definition *def;
593 for (l = defined; l != NULL; l = l->next)
595 def = (definition *) l->val;
596 if (def->def_kind != DEF_PROGRAM)
598 if (streq (def->def_name, stop))
600 return 1;
602 else if (streq (def->def_name, type))
604 return 0;
608 return 1;