gdb - Local mods (compile)
[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.
ce0e08e2 8 *
984263bc
MD
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.
ce0e08e2 12 *
984263bc
MD
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.
ce0e08e2 16 *
984263bc
MD
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.
ce0e08e2 20 *
984263bc
MD
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.
ce0e08e2 24 *
984263bc
MD
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
1de703da 28 *
ce0e08e2
PA
29 * @(#)rpc_hout.c 1.16 94/04/25 SMI; 1.12 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_hout.c,v 1.15 2005/11/13 21:17:24 dwmalone Exp $
984263bc
MD
31 */
32
984263bc
MD
33/*
34 * rpc_hout.c, Header file outputter for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
36 */
37#include <stdio.h>
38#include <ctype.h>
39#include "rpc_parse.h"
ce0e08e2 40#include "rpc_scan.h"
984263bc
MD
41#include "rpc_util.h"
42
ce0e08e2 43void storexdrfuncdecl(const char *, int);
cb0ca794
JS
44static void pconstdef(definition *);
45static void pstructdef(definition *);
46static void puniondef(definition *);
ce0e08e2 47static void pprogramdef(definition *, int);
cb0ca794
JS
48static void penumdef(definition *);
49static void ptypedef(definition *);
ce0e08e2
PA
50static void pdefine(const char *, const char *);
51static int undefined2(const char *, const char *);
52static void parglist(proc_list *, const char *);
53static void pprocdef(proc_list *, version_list *, const char *, int);
984263bc 54
984263bc
MD
55/*
56 * Print the C-version of an xdr definition
57 */
58void
ce0e08e2 59print_datadef(definition *def, int headeronly)
984263bc 60{
ce0e08e2 61
984263bc
MD
62 if (def->def_kind == DEF_PROGRAM) /* handle data only */
63 return;
64
cb0ca794 65 if (def->def_kind != DEF_CONST)
984263bc 66 f_print(fout, "\n");
cb0ca794 67
984263bc
MD
68 switch (def->def_kind) {
69 case DEF_STRUCT:
70 pstructdef(def);
71 break;
72 case DEF_UNION:
73 puniondef(def);
74 break;
75 case DEF_ENUM:
76 penumdef(def);
77 break;
78 case DEF_TYPEDEF:
79 ptypedef(def);
80 break;
81 case DEF_PROGRAM:
ce0e08e2 82 pprogramdef(def, headeronly);
984263bc
MD
83 break;
84 case DEF_CONST:
85 pconstdef(def);
86 break;
87 }
88 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
cb0ca794
JS
89 if (def->def_kind != DEF_TYPEDEF ||
90 !isvectordef(def->def.ty.old_type, def->def.ty.rel))
91 storexdrfuncdecl(def->def_name, 1);
92 else
93 storexdrfuncdecl(def->def_name, 0);
984263bc
MD
94 }
95}
96
97
98void
ce0e08e2 99print_funcdef(definition *def, int headeronly)
984263bc
MD
100{
101 switch (def->def_kind) {
102 case DEF_PROGRAM:
103 f_print(fout, "\n");
ce0e08e2 104 pprogramdef(def, headeronly);
984263bc
MD
105 break;
106 default:
cb0ca794 107 break;
984263bc
MD
108 }
109}
110
111/* store away enough information to allow the XDR functions to be spat
112 out at the end of the file */
113
114void
ce0e08e2 115storexdrfuncdecl(const char *name, int pointerp)
984263bc
MD
116{
117 xdrfunc * xdrptr;
118
ce0e08e2 119 xdrptr = XALLOC(struct xdrfunc);
984263bc
MD
120
121 xdrptr->name = name;
122 xdrptr->pointerp = pointerp;
123 xdrptr->next = NULL;
124
cb0ca794 125 if (xdrfunc_tail == NULL)
984263bc 126 xdrfunc_head = xdrptr;
cb0ca794 127 else
984263bc 128 xdrfunc_tail->next = xdrptr;
cb0ca794 129 xdrfunc_tail = xdrptr;
984263bc
MD
130
131}
132
133void
ce0e08e2 134print_xdr_func_def(const char *name, int pointerp)
984263bc 135{
ce0e08e2
PA
136 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
137 name, pointerp ? "*" : "");
984263bc
MD
138}
139
140
141static void
cb0ca794 142pconstdef(definition *def)
984263bc
MD
143{
144 pdefine(def->def_name, def->def.co);
145}
146
147/* print out the definitions for the arguments of functions in the
148 header file
149*/
150static void
cb0ca794 151pargdef(definition *def)
984263bc
MD
152{
153 decl_list *l;
154 version_list *vers;
155 char *name;
156 proc_list *plist;
157
984263bc 158 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
cb0ca794
JS
159 for (plist = vers->procs; plist != NULL; plist = plist->next) {
160 if (!newstyle || plist->arg_num < 2)
161 continue; /* old style or single args */
162
163 name = plist->args.argname;
164 f_print(fout, "struct %s {\n", name);
165 for (l = plist->args.decls; l != NULL; l = l->next)
166 pdeclaration(name, &l->decl, 1, ";\n");
167 f_print(fout, "};\n");
168 f_print(fout, "typedef struct %s %s;\n", name, name);
169 storexdrfuncdecl(name, 1);
170 f_print(fout, "\n");
984263bc 171 }
cb0ca794 172 }
984263bc
MD
173}
174
175
176static void
cb0ca794 177pstructdef(definition *def)
984263bc
MD
178{
179 decl_list *l;
ce0e08e2 180 const char *name = def->def_name;
984263bc
MD
181
182 f_print(fout, "struct %s {\n", name);
cb0ca794 183 for (l = def->def.st.decls; l != NULL; l = l->next)
984263bc 184 pdeclaration(name, &l->decl, 1, ";\n");
cb0ca794 185
984263bc
MD
186 f_print(fout, "};\n");
187 f_print(fout, "typedef struct %s %s;\n", name, name);
188}
189
190static void
cb0ca794 191puniondef(definition *def)
984263bc
MD
192{
193 case_list *l;
ce0e08e2 194 const char *name = def->def_name;
984263bc
MD
195 declaration *decl;
196
197 f_print(fout, "struct %s {\n", name);
198 decl = &def->def.un.enum_decl;
cb0ca794 199 if (streq(decl->type, "bool"))
984263bc 200 f_print(fout, "\tbool_t %s;\n", decl->name);
cb0ca794 201 else
984263bc 202 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
cb0ca794 203
984263bc
MD
204 f_print(fout, "\tunion {\n");
205 for (l = def->def.un.cases; l != NULL; l = l->next) {
cb0ca794
JS
206 if (l->contflag == 0)
207 pdeclaration(name, &l->case_decl, 2, ";\n");
984263bc
MD
208 }
209 decl = def->def.un.default_decl;
210 if (decl && !streq(decl->type, "void")) {
211 pdeclaration(name, decl, 2, ";\n");
212 }
213 f_print(fout, "\t} %s_u;\n", name);
214 f_print(fout, "};\n");
215 f_print(fout, "typedef struct %s %s;\n", name, name);
216}
217
218static void
ce0e08e2 219pdefine(const char *name, const char *num)
984263bc
MD
220{
221 f_print(fout, "#define\t%s %s\n", name, num);
222}
223
224static void
ce0e08e2 225puldefine(const char *name, const char *num)
984263bc
MD
226{
227 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
228}
229
230static int
cb0ca794 231define_printed(proc_list *stop, version_list *start)
984263bc
MD
232{
233 version_list *vers;
234 proc_list *proc;
235
236 for (vers = start; vers != NULL; vers = vers->next) {
237 for (proc = vers->procs; proc != NULL; proc = proc->next) {
cb0ca794
JS
238 if (proc == stop)
239 return(0);
240 else if (streq(proc->proc_name, stop->proc_name))
241 return(1);
984263bc
MD
242 }
243 }
244 abort();
245 /* NOTREACHED */
246}
247
248static void
ce0e08e2 249pfreeprocdef(const char * name, const char *vers)
984263bc
MD
250{
251 f_print(fout, "extern int ");
252 pvname(name, vers);
ce0e08e2
PA
253 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
254}
255
256static void
257pdispatch(const char * name, const char *vers)
258{
259
260 f_print(fout, "void ");
261 pvname(name, vers);
262 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
984263bc
MD
263}
264
265static void
ce0e08e2 266pprogramdef(definition *def, int headeronly)
984263bc
MD
267{
268 version_list *vers;
269 proc_list *proc;
ce0e08e2 270 const char *ext;
984263bc
MD
271
272 pargdef(def);
273
274 puldefine(def->def_name, def->def.pr.prog_num);
275 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
276 if (tblflag) {
277 f_print(fout,
278 "extern struct rpcgen_table %s_%s_table[];\n",
279 locase(def->def_name), vers->vers_num);
cb0ca794 280 f_print(fout, "extern %s_%s_nproc;\n",
984263bc
MD
281 locase(def->def_name), vers->vers_num);
282 }
283 puldefine(vers->vers_name, vers->vers_num);
284
ce0e08e2
PA
285 f_print(fout, "\n");
286 ext = "extern ";
287 if (headeronly) {
288 f_print(fout, "%s", ext);
289 pdispatch(def->def_name, vers->vers_num);
290 }
291 for (proc = vers->procs; proc != NULL; proc = proc->next) {
292 if (!define_printed(proc, def->def.pr.versions)) {
293 puldefine(proc->proc_name, proc->proc_num);
984263bc 294 }
ce0e08e2
PA
295 f_print(fout, "%s", ext);
296 pprocdef(proc, vers, "CLIENT *", 0);
297 f_print(fout, "%s", ext);
298 pprocdef(proc, vers, "struct svc_req *", 1);
984263bc 299 }
ce0e08e2 300 pfreeprocdef(def->def_name, vers->vers_num);
984263bc
MD
301 }
302}
303
304static void
ce0e08e2 305pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
984263bc 306{
cb0ca794
JS
307 if (mtflag) {
308 /* Print MT style stubs */
984263bc
MD
309 if (server_p)
310 f_print(fout, "bool_t ");
311 else
312 f_print(fout, "enum clnt_stat ");
313 } else {
314 ptype(proc->res_prefix, proc->res_type, 1);
315 f_print(fout, "* ");
316 }
317 if (server_p)
318 pvname_svc(proc->proc_name, vp->vers_num);
319 else
320 pvname(proc->proc_name, vp->vers_num);
321
ce0e08e2 322 parglist(proc, addargtype);
984263bc
MD
323}
324
325
326
327/* print out argument list of procedure */
328static void
ce0e08e2 329parglist(proc_list *proc, const char *addargtype)
984263bc
MD
330{
331 decl_list *dl;
332
333 f_print(fout, "(");
334 if (proc->arg_num < 2 && newstyle &&
335 streq(proc->args.decls->decl.type, "void")) {
336 /* 0 argument in new style: do nothing*/
337 }
338 else {
339 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
340 ptype(dl->decl.prefix, dl->decl.type, 1);
341 if (!newstyle)
342 f_print(fout, "*");
343 /* old style passes by reference */
344 f_print(fout, ", ");
345 }
346 }
347
348 if (mtflag) {
349 ptype(proc->res_prefix, proc->res_type, 1);
350 f_print(fout, "*, ");
351 }
352
353 f_print(fout, "%s);\n", addargtype);
984263bc
MD
354}
355
356static void
cb0ca794 357penumdef(definition *def)
984263bc 358{
ce0e08e2 359 const char *name = def->def_name;
984263bc 360 enumval_list *l;
ce0e08e2 361 const char *last = NULL;
984263bc
MD
362 int count = 0;
363
364 f_print(fout, "enum %s {\n", name);
365 for (l = def->def.en.vals; l != NULL; l = l->next) {
366 f_print(fout, "\t%s", l->name);
367 if (l->assignment) {
368 f_print(fout, " = %s", l->assignment);
369 last = l->assignment;
370 count = 1;
371 } else {
cb0ca794 372 if (last == NULL)
984263bc 373 f_print(fout, " = %d", count++);
cb0ca794 374 else
984263bc 375 f_print(fout, " = %s + %d", last, count++);
984263bc
MD
376 }
377 if (l->next)
378 f_print(fout, ",\n");
379 else
380 f_print(fout, "\n");
381 }
382 f_print(fout, "};\n");
383 f_print(fout, "typedef enum %s %s;\n", name, name);
384}
385
386static void
cb0ca794 387ptypedef(definition *def)
984263bc 388{
ce0e08e2
PA
389 const char *name = def->def_name;
390 const char *old = def->def.ty.old_type;
984263bc
MD
391 char prefix[8]; /* enough to contain "struct ", including NUL */
392 relation rel = def->def.ty.rel;
393
984263bc
MD
394 if (!streq(name, old)) {
395 if (streq(old, "string")) {
396 old = "char";
397 rel = REL_POINTER;
398 } else if (streq(old, "opaque")) {
399 old = "char";
400 } else if (streq(old, "bool")) {
401 old = "bool_t";
402 }
cb0ca794 403 if (undefined2(old, name) && def->def.ty.old_prefix)
984263bc 404 s_print(prefix, "%s ", def->def.ty.old_prefix);
cb0ca794 405 else
984263bc 406 prefix[0] = 0;
984263bc
MD
407 f_print(fout, "typedef ");
408 switch (rel) {
409 case REL_ARRAY:
410 f_print(fout, "struct {\n");
411 f_print(fout, "\tu_int %s_len;\n", name);
412 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
413 f_print(fout, "} %s", name);
414 break;
415 case REL_POINTER:
416 f_print(fout, "%s%s *%s", prefix, old, name);
417 break;
418 case REL_VECTOR:
419 f_print(fout, "%s%s %s[%s]", prefix, old, name,
420 def->def.ty.array_max);
421 break;
422 case REL_ALIAS:
423 f_print(fout, "%s%s %s", prefix, old, name);
424 break;
425 }
426 f_print(fout, ";\n");
427 }
428}
429
430void
ce0e08e2 431pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
984263bc
MD
432{
433 char buf[8]; /* enough to hold "struct ", include NUL */
ce0e08e2
PA
434 const char *prefix;
435 const char *type;
984263bc 436
cb0ca794 437 if (streq(dec->type, "void"))
984263bc 438 return;
cb0ca794 439
984263bc 440 tabify(fout, tab);
cb0ca794 441 if (streq(dec->type, name) && !dec->prefix)
984263bc 442 f_print(fout, "struct ");
cb0ca794 443
984263bc
MD
444 if (streq(dec->type, "string")) {
445 f_print(fout, "char *%s", dec->name);
446 } else {
447 prefix = "";
448 if (streq(dec->type, "bool")) {
449 type = "bool_t";
450 } else if (streq(dec->type, "opaque")) {
451 type = "char";
452 } else {
453 if (dec->prefix) {
454 s_print(buf, "%s ", dec->prefix);
455 prefix = buf;
456 }
457 type = dec->type;
458 }
459 switch (dec->rel) {
460 case REL_ALIAS:
461 f_print(fout, "%s%s %s", prefix, type, dec->name);
462 break;
463 case REL_VECTOR:
464 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
465 dec->array_max);
466 break;
467 case REL_POINTER:
468 f_print(fout, "%s%s *%s", prefix, type, dec->name);
469 break;
470 case REL_ARRAY:
471 f_print(fout, "struct {\n");
472 tabify(fout, tab);
473 f_print(fout, "\tu_int %s_len;\n", dec->name);
474 tabify(fout, tab);
475 f_print(fout,
476 "\t%s%s *%s_val;\n", prefix, type, dec->name);
477 tabify(fout, tab);
478 f_print(fout, "} %s", dec->name);
479 break;
480 }
481 }
9462167a 482 f_print(fout, "%s", separator);
984263bc
MD
483}
484
485static int
ce0e08e2 486undefined2(const char *type, const char *stop)
984263bc
MD
487{
488 list *l;
489 definition *def;
490
491 for (l = defined; l != NULL; l = l->next) {
492 def = (definition *) l->val;
493 if (def->def_kind != DEF_PROGRAM) {
cb0ca794
JS
494 if (streq(def->def_name, stop))
495 return(1);
496 else if (streq(def->def_name, type))
497 return(0);
984263bc
MD
498 }
499 }
cb0ca794 500 return(1);
984263bc 501}