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