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