]> sourceware.org Git - glibc.git/blob - sunrpc/rpc_hout.c
Update.
[glibc.git] / sunrpc / rpc_hout.c
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.
9 *
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.
13 *
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.
17 *
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.
21 *
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.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30
31 /*
32 * From: @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
33 */
34 char hout_rcsid[] =
35 "$Id$";
36
37 /*
38 * rpc_hout.c, Header file outputter for the RPC protocol compiler
39 */
40 #include <stdio.h>
41 #include <ctype.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 #include "proto.h"
45
46 static void pconstdef (definition * def);
47 static void pargdef (definition * def);
48 static void pstructdef (definition * def);
49 static void puniondef (definition * def);
50 static void pdefine (const char *name, const char *num);
51 static int define_printed (proc_list * stop, version_list * start);
52 static void pprogramdef (definition * def);
53 static void parglist (proc_list * proc, const char *addargtype);
54 static void penumdef (definition * def);
55 static void ptypedef (definition * def);
56 static int undefined2 (const char *type, const char *stop);
57
58 /* store away enough information to allow the XDR functions to be spat
59 out at the end of the file */
60
61 void
62 storexdrfuncdecl (const char *name, int pointerp)
63 {
64 xdrfunc * xdrptr;
65
66 xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
67
68 xdrptr->name = (char *)name;
69 xdrptr->pointerp = pointerp;
70 xdrptr->next = NULL;
71
72 if (xdrfunc_tail == NULL)
73 {
74 xdrfunc_head = xdrptr;
75 xdrfunc_tail = xdrptr;
76 }
77 else
78 {
79 xdrfunc_tail->next = xdrptr;
80 xdrfunc_tail = xdrptr;
81 }
82 }
83
84 /*
85 * Print the C-version of an xdr definition
86 */
87 void
88 print_datadef (definition *def)
89 {
90
91 if (def->def_kind == DEF_PROGRAM) /* handle data only */
92 return;
93
94 if (def->def_kind != DEF_CONST)
95 {
96 f_print (fout, "\n");
97 }
98 switch (def->def_kind)
99 {
100 case DEF_STRUCT:
101 pstructdef (def);
102 break;
103 case DEF_UNION:
104 puniondef (def);
105 break;
106 case DEF_ENUM:
107 penumdef (def);
108 break;
109 case DEF_TYPEDEF:
110 ptypedef (def);
111 break;
112 case DEF_PROGRAM:
113 pprogramdef (def);
114 break;
115 case DEF_CONST:
116 pconstdef (def);
117 break;
118 }
119 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST)
120 {
121 storexdrfuncdecl(def->def_name,
122 def->def_kind != DEF_TYPEDEF ||
123 !isvectordef(def->def.ty.old_type,
124 def->def.ty.rel));
125 }
126 }
127
128
129 void
130 print_funcdef (definition *def)
131 {
132 switch (def->def_kind)
133 {
134 case DEF_PROGRAM:
135 f_print (fout, "\n");
136 pprogramdef (def);
137 break;
138 default:
139 /* ?... shouldn't happen I guess */
140 }
141 }
142
143 void
144 print_xdr_func_def (char *name, int pointerp, int i)
145 {
146 if (i == 2)
147 {
148 f_print (fout, "extern bool_t xdr_%s ();\n", name);
149 return;
150 }
151 else
152 f_print(fout, "extern bool_t xdr_%s (XDR *, %s%s);\n", name,
153 name, pointerp ? "*" : "");
154 }
155
156 static void
157 pconstdef (definition *def)
158 {
159 pdefine (def->def_name, def->def.co);
160 }
161
162 /* print out the definitions for the arguments of functions in the
163 header file
164 */
165 static void
166 pargdef (definition * def)
167 {
168 decl_list *l;
169 version_list *vers;
170 const char *name;
171 proc_list *plist;
172
173 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
174 {
175 for (plist = vers->procs; plist != NULL;
176 plist = plist->next)
177 {
178
179 if (!newstyle || plist->arg_num < 2)
180 {
181 continue; /* old style or single args */
182 }
183 name = plist->args.argname;
184 f_print (fout, "struct %s {\n", name);
185 for (l = plist->args.decls;
186 l != NULL; l = l->next)
187 {
188 pdeclaration (name, &l->decl, 1, ";\n");
189 }
190 f_print (fout, "};\n");
191 f_print (fout, "typedef struct %s %s;\n", name, name);
192 storexdrfuncdecl (name, 0);
193 f_print (fout, "\n");
194 }
195 }
196
197 }
198
199 static void
200 pstructdef (definition *def)
201 {
202 decl_list *l;
203 const char *name = def->def_name;
204
205 f_print (fout, "struct %s {\n", name);
206 for (l = def->def.st.decls; l != NULL; l = l->next)
207 {
208 pdeclaration (name, &l->decl, 1, ";\n");
209 }
210 f_print (fout, "};\n");
211 f_print (fout, "typedef struct %s %s;\n", name, name);
212 }
213
214 static void
215 puniondef (definition *def)
216 {
217 case_list *l;
218 const char *name = def->def_name;
219 declaration *decl;
220
221 f_print (fout, "struct %s {\n", name);
222 decl = &def->def.un.enum_decl;
223 if (streq (decl->type, "bool"))
224 {
225 f_print (fout, "\tbool_t %s;\n", decl->name);
226 }
227 else
228 {
229 f_print (fout, "\t%s %s;\n", decl->type, decl->name);
230 }
231 f_print (fout, "\tunion {\n");
232 for (l = def->def.un.cases; l != NULL; l = l->next)
233 {
234 if (l->contflag == 0)
235 pdeclaration (name, &l->case_decl, 2, ";\n");
236 }
237 decl = def->def.un.default_decl;
238 if (decl && !streq (decl->type, "void"))
239 {
240 pdeclaration (name, decl, 2, ";\n");
241 }
242 f_print (fout, "\t} %s_u;\n", name);
243 f_print (fout, "};\n");
244 f_print (fout, "typedef struct %s %s;\n", name, name);
245 }
246
247 static void
248 pdefine (const char *name, const char *num)
249 {
250 f_print (fout, "#define %s %s\n", name, num);
251 }
252
253 static int
254 define_printed (proc_list *stop, version_list *start)
255 {
256 version_list *vers;
257 proc_list *proc;
258
259 for (vers = start; vers != NULL; vers = vers->next)
260 {
261 for (proc = vers->procs; proc != NULL; proc = proc->next)
262 {
263 if (proc == stop)
264 {
265 return 0;
266 }
267 else if (streq (proc->proc_name, stop->proc_name))
268 {
269 return 1;
270 }
271 }
272 }
273 abort ();
274 /* NOTREACHED */
275 }
276
277 static void
278 pfreeprocdef (const char *name, const char *vers, int mode)
279 {
280 f_print (fout, "extern int ");
281 pvname (name, vers);
282 if (mode == 1)
283 f_print (fout,"_freeresult (SVCXPRT *, xdrproc_t, caddr_t);\n");
284 else
285 f_print (fout,"_freeresult ();\n");
286 }
287
288 static void
289 pprogramdef (definition *def)
290 {
291 version_list *vers;
292 proc_list *proc;
293 int i;
294 const char *ext;
295
296 pargdef (def);
297
298 pdefine (def->def_name, def->def.pr.prog_num);
299 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
300 {
301 if (tblflag)
302 {
303 f_print (fout, "extern struct rpcgen_table %s_%s_table[];\n",
304 locase (def->def_name), vers->vers_num);
305 f_print (fout, "extern %s_%s_nproc;\n",
306 locase (def->def_name), vers->vers_num);
307 }
308 pdefine (vers->vers_name, vers->vers_num);
309
310 /*
311 * Print out 2 definitions, one for ANSI-C, another for
312 * old K & R C
313 */
314
315 if(!Cflag)
316 {
317 ext = "extern ";
318 for (proc = vers->procs; proc != NULL;
319 proc = proc->next)
320 {
321 if (!define_printed(proc, def->def.pr.versions))
322 {
323 pdefine (proc->proc_name, proc->proc_num);
324 }
325 f_print (fout, "%s", ext);
326 pprocdef (proc, vers, NULL, 0, 2);
327
328 if (mtflag)
329 {
330 f_print(fout, "%s", ext);
331 pprocdef (proc, vers, NULL, 1, 2);
332 }
333 }
334 pfreeprocdef (def->def_name, vers->vers_num, 2);
335 }
336 else
337 {
338 for (i = 1; i < 3; i++)
339 {
340 if (i == 1)
341 {
342 f_print (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
343 ext = "extern ";
344 }
345 else
346 {
347 f_print (fout, "\n#else /* K&R C */\n");
348 ext = "extern ";
349 }
350
351 for (proc = vers->procs; proc != NULL; proc = proc->next)
352 {
353 if (!define_printed(proc, def->def.pr.versions))
354 {
355 pdefine(proc->proc_name, proc->proc_num);
356 }
357 f_print (fout, "%s", ext);
358 pprocdef (proc, vers, "CLIENT *", 0, i);
359 f_print (fout, "%s", ext);
360 pprocdef (proc, vers, "struct svc_req *", 1, i);
361 }
362 pfreeprocdef (def->def_name, vers->vers_num, i);
363 }
364 f_print (fout, "#endif /* K&R C */\n");
365 }
366 }
367 }
368
369 void
370 pprocdef (proc_list * proc, version_list * vp,
371 const char *addargtype, int server_p, int mode)
372 {
373 if (mtflag)
374 {/* Print MT style stubs */
375 if (server_p)
376 f_print (fout, "bool_t ");
377 else
378 f_print (fout, "enum clnt_stat ");
379 }
380 else
381 {
382 ptype (proc->res_prefix, proc->res_type, 1);
383 f_print (fout, "* ");
384 }
385 if (server_p)
386 pvname_svc (proc->proc_name, vp->vers_num);
387 else
388 pvname (proc->proc_name, vp->vers_num);
389
390 /*
391 * mode 1 = ANSI-C, mode 2 = K&R C
392 */
393 if (mode == 1)
394 parglist (proc, addargtype);
395 else
396 f_print (fout, "();\n");
397 }
398
399 /* print out argument list of procedure */
400 static void
401 parglist (proc_list *proc, const char *addargtype)
402 {
403 decl_list *dl;
404
405 f_print(fout,"(");
406 if (proc->arg_num < 2 && newstyle &&
407 streq (proc->args.decls->decl.type, "void"))
408 {
409 /* 0 argument in new style: do nothing */
410 }
411 else
412 {
413 for (dl = proc->args.decls; dl != NULL; dl = dl->next)
414 {
415 ptype (dl->decl.prefix, dl->decl.type, 1);
416 if (!newstyle)
417 f_print (fout, "*"); /* old style passes by reference */
418
419 f_print (fout, ", ");
420 }
421 }
422 if (mtflag)
423 {
424 ptype(proc->res_prefix, proc->res_type, 1);
425 f_print(fout, "*, ");
426 }
427
428 f_print (fout, "%s);\n", addargtype);
429 }
430
431 static void
432 penumdef (definition *def)
433 {
434 const char *name = def->def_name;
435 enumval_list *l;
436 const char *last = NULL;
437 int count = 0;
438
439 f_print (fout, "enum %s {\n", name);
440 for (l = def->def.en.vals; l != NULL; l = l->next)
441 {
442 f_print (fout, "\t%s", l->name);
443 if (l->assignment)
444 {
445 f_print (fout, " = %s", l->assignment);
446 last = l->assignment;
447 count = 1;
448 }
449 else
450 {
451 if (last == NULL)
452 {
453 f_print (fout, " = %d", count++);
454 }
455 else
456 {
457 f_print (fout, " = %s + %d", last, count++);
458 }
459 }
460 f_print (fout, ",\n");
461 }
462 f_print (fout, "};\n");
463 f_print (fout, "typedef enum %s %s;\n", name, name);
464 }
465
466 static void
467 ptypedef (definition *def)
468 {
469 const char *name = def->def_name;
470 const char *old = def->def.ty.old_type;
471 char prefix[8]; /* enough to contain "struct ", including NUL */
472 relation rel = def->def.ty.rel;
473
474 if (!streq (name, old))
475 {
476 if (streq (old, "string"))
477 {
478 old = "char";
479 rel = REL_POINTER;
480 }
481 else if (streq (old, "opaque"))
482 {
483 old = "char";
484 }
485 else if (streq (old, "bool"))
486 {
487 old = "bool_t";
488 }
489 if (undefined2 (old, name) && def->def.ty.old_prefix)
490 {
491 s_print (prefix, "%s ", def->def.ty.old_prefix);
492 }
493 else
494 {
495 prefix[0] = 0;
496 }
497 f_print (fout, "typedef ");
498 switch (rel)
499 {
500 case REL_ARRAY:
501 f_print (fout, "struct {\n");
502 f_print (fout, "\tu_int %s_len;\n", name);
503 f_print (fout, "\t%s%s *%s_val;\n", prefix, old, name);
504 f_print (fout, "} %s", name);
505 break;
506 case REL_POINTER:
507 f_print (fout, "%s%s *%s", prefix, old, name);
508 break;
509 case REL_VECTOR:
510 f_print (fout, "%s%s %s[%s]", prefix, old, name,
511 def->def.ty.array_max);
512 break;
513 case REL_ALIAS:
514 f_print (fout, "%s%s %s", prefix, old, name);
515 break;
516 }
517 f_print (fout, ";\n");
518 }
519 }
520
521 void
522 pdeclaration (const char *name, declaration * dec, int tab,
523 const char *separator)
524 {
525 char buf[8]; /* enough to hold "struct ", include NUL */
526 const char *prefix;
527 const char *type;
528
529 if (streq (dec->type, "void"))
530 {
531 return;
532 }
533 tabify (fout, tab);
534 if (streq (dec->type, name) && !dec->prefix)
535 {
536 f_print (fout, "struct ");
537 }
538 if (streq (dec->type, "string"))
539 {
540 f_print (fout, "char *%s", dec->name);
541 }
542 else
543 {
544 prefix = "";
545 if (streq (dec->type, "bool"))
546 {
547 type = "bool_t";
548 }
549 else if (streq (dec->type, "opaque"))
550 {
551 type = "char";
552 }
553 else
554 {
555 if (dec->prefix)
556 {
557 s_print (buf, "%s ", dec->prefix);
558 prefix = buf;
559 }
560 type = dec->type;
561 }
562 switch (dec->rel)
563 {
564 case REL_ALIAS:
565 f_print (fout, "%s%s %s", prefix, type, dec->name);
566 break;
567 case REL_VECTOR:
568 f_print (fout, "%s%s %s[%s]", prefix, type, dec->name,
569 dec->array_max);
570 break;
571 case REL_POINTER:
572 f_print (fout, "%s%s *%s", prefix, type, dec->name);
573 break;
574 case REL_ARRAY:
575 f_print (fout, "struct {\n");
576 tabify (fout, tab);
577 f_print (fout, "\tu_int %s_len;\n", dec->name);
578 tabify (fout, tab);
579 f_print (fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
580 tabify (fout, tab);
581 f_print (fout, "} %s", dec->name);
582 break;
583 }
584 }
585 f_print (fout, separator);
586 }
587
588 static int
589 undefined2 (const char *type, const char *stop)
590 {
591 list *l;
592 definition *def;
593
594 for (l = defined; l != NULL; l = l->next)
595 {
596 def = (definition *) l->val;
597 if (def->def_kind != DEF_PROGRAM)
598 {
599 if (streq (def->def_name, stop))
600 {
601 return 1;
602 }
603 else if (streq (def->def_name, type))
604 {
605 return 0;
606 }
607 }
608 }
609 return 1;
610 }
This page took 0.063832 seconds and 5 git commands to generate.