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