rpc_hout.c revision 1.6 1 /* $NetBSD: rpc_hout.c,v 1.6 1997/10/11 21:01:34 christos Exp $ */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user or with the express written consent of
9 * Sun Microsystems, Inc.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
36 #else
37 __RCSID("$NetBSD: rpc_hout.c,v 1.6 1997/10/11 21:01:34 christos Exp $");
38 #endif
39 #endif
40
41 /*
42 * rpc_hout.c, Header file outputter for the RPC protocol compiler
43 */
44 #include <stdio.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include "rpc_scan.h"
48 #include "rpc_parse.h"
49 #include "rpc_util.h"
50
51 static void pconstdef __P((definition *));
52 static void pargdef __P((definition *));
53 static void pstructdef __P((definition *));
54 static void puniondef __P((definition *));
55 static void pdefine __P((char *, char *));
56 static void puldefine __P((char *, char *));
57 static int define_printed __P((proc_list *, version_list *));
58 static void pprogramdef __P((definition *));
59 static void parglist __P((proc_list *, char *));
60 static void penumdef __P((definition *));
61 static void ptypedef __P((definition *));
62 static int undefined2 __P((char *, char *));
63
64 /*
65 * Print the C-version of an xdr definition
66 */
67 void
68 print_datadef(def)
69 definition *def;
70 {
71
72 if (def->def_kind == DEF_PROGRAM ) /* handle data only */
73 return;
74
75 if (def->def_kind != DEF_CONST) {
76 f_print(fout, "\n");
77 }
78 switch (def->def_kind) {
79 case DEF_STRUCT:
80 pstructdef(def);
81 break;
82 case DEF_UNION:
83 puniondef(def);
84 break;
85 case DEF_ENUM:
86 penumdef(def);
87 break;
88 case DEF_TYPEDEF:
89 ptypedef(def);
90 break;
91 case DEF_PROGRAM:
92 pprogramdef(def);
93 break;
94 case DEF_CONST:
95 pconstdef(def);
96 break;
97 }
98 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
99 pxdrfuncdecl( def->def_name,
100 def->def_kind != DEF_TYPEDEF ||
101 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
102
103 }
104 }
105
106
107 void
108 print_funcdef(def)
109 definition *def;
110 {
111 switch (def->def_kind) {
112 case DEF_PROGRAM:
113 f_print(fout, "\n");
114 pprogramdef(def);
115 break;
116 case DEF_CONST:
117 case DEF_TYPEDEF:
118 case DEF_ENUM:
119 case DEF_UNION:
120 case DEF_STRUCT:
121 break;
122 }
123 }
124
125 void
126 pxdrfuncdecl( name, pointerp )
127 char* name;
128 int pointerp;
129 {
130
131 f_print(fout,"#ifdef __cplusplus\n");
132 f_print(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n",
133 name,
134 name, pointerp ? ("*") : "");
135 f_print(fout,"#elif __STDC__\n");
136 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n",
137 name,
138 name, pointerp ? ("*") : "");
139 f_print(fout,"#else /* Old Style C */\n");
140 f_print(fout, "bool_t xdr_%s();\n", name);
141 f_print(fout,"#endif /* Old Style C */\n\n");
142 }
143
144
145 static void
146 pconstdef(def)
147 definition *def;
148 {
149 pdefine(def->def_name, def->def.co);
150 }
151
152 /* print out the definitions for the arguments of functions in the
153 header file
154 */
155 static void
156 pargdef(def)
157 definition *def;
158 {
159 decl_list *l;
160 version_list *vers;
161 char *name;
162 proc_list *plist;
163
164
165 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
166 for(plist = vers->procs; plist != NULL;
167 plist = plist->next) {
168
169 if (!newstyle || plist->arg_num < 2) {
170 continue; /* old style or single args */
171 }
172 name = plist->args.argname;
173 f_print(fout, "struct %s {\n", name);
174 for (l = plist->args.decls;
175 l != NULL; l = l->next) {
176 pdeclaration(name, &l->decl, 1, ";\n" );
177 }
178 f_print(fout, "};\n");
179 f_print(fout, "typedef struct %s %s;\n", name, name);
180 pxdrfuncdecl( name,NULL );
181 f_print( fout, "\n" );
182 }
183 }
184
185 }
186
187
188 static void
189 pstructdef(def)
190 definition *def;
191 {
192 decl_list *l;
193 char *name = def->def_name;
194
195 f_print(fout, "struct %s {\n", name);
196 for (l = def->def.st.decls; l != NULL; l = l->next) {
197 pdeclaration(name, &l->decl, 1, ";\n");
198 }
199 f_print(fout, "};\n");
200 f_print(fout, "typedef struct %s %s;\n", name, name);
201 }
202
203 static void
204 puniondef(def)
205 definition *def;
206 {
207 case_list *l;
208 char *name = def->def_name;
209 declaration *decl;
210
211 f_print(fout, "struct %s {\n", name);
212 decl = &def->def.un.enum_decl;
213 if (streq(decl->type, "bool")) {
214 f_print(fout, "\tbool_t %s;\n", decl->name);
215 } else {
216 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
217 }
218 f_print(fout, "\tunion {\n");
219 for (l = def->def.un.cases; l != NULL; l = l->next) {
220 if(l->contflag == 0)
221 pdeclaration(name, &l->case_decl, 2, ";\n" );
222 }
223 decl = def->def.un.default_decl;
224 if (decl && !streq(decl->type, "void")) {
225 pdeclaration(name, decl, 2, ";\n" );
226 }
227 f_print(fout, "\t} %s_u;\n", name);
228 f_print(fout, "};\n");
229 f_print(fout, "typedef struct %s %s;\n", name, name);
230 }
231
232 static void
233 pdefine(name, num)
234 char *name;
235 char *num;
236 {
237 f_print(fout, "#define %s %s\n", name, num);
238 }
239
240 static void
241 puldefine(name, num)
242 char *name;
243 char *num;
244 {
245 f_print(fout, "#define %s ((u_long)%s)\n", name, num);
246 }
247
248 static int
249 define_printed(stop, start)
250 proc_list *stop;
251 version_list *start;
252 {
253 version_list *vers;
254 proc_list *proc;
255
256 for (vers = start; vers != NULL; vers = vers->next) {
257 for (proc = vers->procs; proc != NULL; proc = proc->next) {
258 if (proc == stop) {
259 return (0);
260 } else if (streq(proc->proc_name, stop->proc_name)) {
261 return (1);
262 }
263 }
264 }
265 errx(1, "Internal error %s, %d: procedure not found\n",
266 __FILE__, __LINE__);
267 /* NOTREACHED */
268 }
269
270 static void
271 pprogramdef(def)
272 definition *def;
273 {
274 version_list *vers;
275 proc_list *proc;
276 int i;
277 char *ext;
278
279 pargdef(def);
280
281 puldefine(def->def_name, def->def.pr.prog_num);
282 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
283 if (tblflag) {
284 f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
285 locase(def->def_name), vers->vers_num);
286 f_print(fout, "extern %s_%s_nproc;\n",
287 locase(def->def_name), vers->vers_num);
288 }
289 puldefine(vers->vers_name, vers->vers_num);
290
291 /*
292 * Print out 3 definitions, one for ANSI-C, another for C++,
293 * a third for old style C
294 */
295
296 for(i=0;i<3;i++){
297 if(i==0){
298 f_print(fout,"\n#ifdef __cplusplus\n");
299 ext="extern \"C\" ";
300 }else if ( i== 1){
301 f_print(fout,"\n#elif __STDC__\n");
302 ext="extern " ;
303 }else{
304 f_print(fout,"\n#else /* Old Style C */\n");
305 ext="extern ";
306 }
307
308
309 for (proc = vers->procs; proc != NULL; proc = proc->next) {
310 if (!define_printed(proc, def->def.pr.versions)) {
311 puldefine(proc->proc_name, proc->proc_num);
312 }
313 f_print(fout,"%s",ext);
314 pprocdef(proc, vers, "CLIENT *", 0,i);
315 f_print(fout,"%s",ext);
316 pprocdef(proc, vers, "struct svc_req *", 1,i);
317
318 }
319
320 }
321 f_print(fout,"#endif /* Old Style C */\n");
322 }
323 }
324
325 void
326 pprocdef(proc, vp, addargtype, server_p,mode)
327 proc_list *proc;
328 version_list *vp;
329 char* addargtype;
330 int server_p;
331 int mode;
332 {
333
334 ptype( proc->res_prefix, proc->res_type, 1 );
335 f_print( fout, "* " );
336 if( server_p )
337 pvname_svc(proc->proc_name, vp->vers_num);
338 else
339 pvname(proc->proc_name, vp->vers_num);
340
341 /*
342 * mode 0 == cplusplus, mode 1 = ANSI-C, mode 2 = old style C
343 */
344 if(mode == 0 || mode ==1)
345 parglist( proc, addargtype );
346 else
347 f_print(fout, "();\n");
348 }
349
350
351 /* print out argument list of procedure */
352 static void
353 parglist(proc, addargtype)
354 proc_list *proc;
355 char* addargtype;
356 {
357 decl_list *dl;
358
359 f_print(fout,"(");
360
361 if( proc->arg_num < 2 && newstyle &&
362 streq( proc->args.decls->decl.type, "void")) {
363 /* 0 argument in new style: do nothing */
364 } else {
365 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
366 ptype( dl->decl.prefix, dl->decl.type, 1 );
367 if( !newstyle )
368 f_print( fout, "*" ); /* old style passes by reference */
369
370 f_print( fout, ", " );
371 }
372 }
373
374 f_print(fout, "%s);\n", addargtype);
375 }
376
377 static void
378 penumdef(def)
379 definition *def;
380 {
381 char *name = def->def_name;
382 enumval_list *l;
383 char *last = NULL;
384 int count = 0;
385
386 f_print(fout, "enum %s {\n", name);
387 for (l = def->def.en.vals; l != NULL; l = l->next) {
388 f_print(fout, "\t%s", l->name);
389 if (l->assignment) {
390 f_print(fout, " = %s", l->assignment);
391 last = l->assignment;
392 count = 1;
393 } else {
394 if (last == NULL) {
395 f_print(fout, " = %d", count++);
396 } else {
397 f_print(fout, " = %s + %d", last, count++);
398 }
399 }
400 f_print(fout, ",\n");
401 }
402 f_print(fout, "};\n");
403 f_print(fout, "typedef enum %s %s;\n", name, name);
404 }
405
406 static void
407 ptypedef(def)
408 definition *def;
409 {
410 char *name = def->def_name;
411 char *old = def->def.ty.old_type;
412 char prefix[8]; /* enough to contain "struct ", including NUL */
413 relation rel = def->def.ty.rel;
414
415
416 if (!streq(name, old)) {
417 if (streq(old, "string")) {
418 old = "char";
419 rel = REL_POINTER;
420 } else if (streq(old, "opaque")) {
421 old = "char";
422 } else if (streq(old, "bool")) {
423 old = "bool_t";
424 }
425 if (undefined2(old, name) && def->def.ty.old_prefix) {
426 s_print(prefix, "%s ", def->def.ty.old_prefix);
427 } else {
428 prefix[0] = 0;
429 }
430 f_print(fout, "typedef ");
431 switch (rel) {
432 case REL_ARRAY:
433 f_print(fout, "struct {\n");
434 f_print(fout, "\tu_int %s_len;\n", name);
435 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
436 f_print(fout, "} %s", name);
437 break;
438 case REL_POINTER:
439 f_print(fout, "%s%s *%s", prefix, old, name);
440 break;
441 case REL_VECTOR:
442 f_print(fout, "%s%s %s[%s]", prefix, old, name,
443 def->def.ty.array_max);
444 break;
445 case REL_ALIAS:
446 f_print(fout, "%s%s %s", prefix, old, name);
447 break;
448 }
449 f_print(fout, ";\n");
450 }
451 }
452
453 void
454 pdeclaration(name, dec, tab, separator)
455 char *name;
456 declaration *dec;
457 int tab;
458 char *separator;
459 {
460 char buf[8]; /* enough to hold "struct ", include NUL */
461 char *prefix;
462 char *type;
463
464 if (streq(dec->type, "void")) {
465 return;
466 }
467 tabify(fout, tab);
468 if (streq(dec->type, name) && !dec->prefix) {
469 f_print(fout, "struct ");
470 }
471 if (streq(dec->type, "string")) {
472 f_print(fout, "char *%s", dec->name);
473 } else {
474 prefix = "";
475 if (streq(dec->type, "bool")) {
476 type = "bool_t";
477 } else if (streq(dec->type, "opaque")) {
478 type = "char";
479 } else {
480 if (dec->prefix) {
481 s_print(buf, "%s ", dec->prefix);
482 prefix = buf;
483 }
484 type = dec->type;
485 }
486 switch (dec->rel) {
487 case REL_ALIAS:
488 f_print(fout, "%s%s %s", prefix, type, dec->name);
489 break;
490 case REL_VECTOR:
491 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
492 dec->array_max);
493 break;
494 case REL_POINTER:
495 f_print(fout, "%s%s *%s", prefix, type, dec->name);
496 break;
497 case REL_ARRAY:
498 f_print(fout, "struct {\n");
499 tabify(fout, tab);
500 f_print(fout, "\tu_int %s_len;\n", dec->name);
501 tabify(fout, tab);
502 f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
503 tabify(fout, tab);
504 f_print(fout, "} %s", dec->name);
505 break;
506 }
507 }
508 f_print(fout, separator );
509 }
510
511 static int
512 undefined2(type, stop)
513 char *type;
514 char *stop;
515 {
516 list *l;
517 definition *def;
518
519 for (l = defined; l != NULL; l = l->next) {
520 def = (definition *) l->val;
521 if (def->def_kind != DEF_PROGRAM) {
522 if (streq(def->def_name, stop)) {
523 return (1);
524 } else if (streq(def->def_name, type)) {
525 return (0);
526 }
527 }
528 }
529 return (1);
530 }
531