renames vmcb->control.v_irq to virq_pending
[freebsd-src/fkvm-freebsd.git] / usr.bin / rpcgen / rpc_hout.c
blob70683638360e648461a3bd86764eac48653f98fd
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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
30 #if 0
31 #ifndef lint
32 #ident "@(#)rpc_hout.c 1.16 94/04/25 SMI"
33 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
34 #endif
35 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * rpc_hout.c, Header file outputter for the RPC protocol compiler
42 * Copyright (C) 1987, Sun Microsystems, Inc.
44 #include <stdio.h>
45 #include <ctype.h>
46 #include "rpc_parse.h"
47 #include "rpc_scan.h"
48 #include "rpc_util.h"
50 void storexdrfuncdecl(const char *, int );
51 static void pconstdef( definition * );
52 static void pstructdef( definition * );
53 static void puniondef( definition * );
54 static void pprogramdef( definition *, int );
55 static void penumdef( definition * );
56 static void ptypedef( definition * );
57 static void pdefine(const char *, const char *);
58 static int undefined2(const char *, const char *);
59 static void parglist(proc_list *, const char *);
60 static void pprocdef(proc_list *, version_list *, const char *, int);
63 * Print the C-version of an xdr definition
65 void
66 print_datadef(definition *def, int headeronly)
69 if (def->def_kind == DEF_PROGRAM) /* handle data only */
70 return;
72 if (def->def_kind != DEF_CONST) {
73 f_print(fout, "\n");
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, headeronly);
90 break;
91 case DEF_CONST:
92 pconstdef(def);
93 break;
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));
104 void
105 print_funcdef(definition *def, int headeronly)
107 switch (def->def_kind) {
108 case DEF_PROGRAM:
109 f_print(fout, "\n");
110 pprogramdef(def, headeronly);
111 break;
112 default:
113 break;
117 /* store away enough information to allow the XDR functions to be spat
118 out at the end of the file */
120 void
121 storexdrfuncdecl(const char *name, int pointerp)
123 xdrfunc * xdrptr;
125 xdrptr = XALLOC(struct xdrfunc);
127 xdrptr->name = name;
128 xdrptr->pointerp = pointerp;
129 xdrptr->next = NULL;
131 if (xdrfunc_tail == NULL){
132 xdrfunc_head = xdrptr;
133 xdrfunc_tail = xdrptr;
134 } else {
135 xdrfunc_tail->next = xdrptr;
136 xdrfunc_tail = xdrptr;
142 void
143 print_xdr_func_def(const char *name, int pointerp)
145 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
146 name, pointerp ? "*" : "");
150 static void
151 pconstdef(def)
152 definition *def;
154 pdefine(def->def_name, def->def.co);
157 /* print out the definitions for the arguments of functions in the
158 header file
160 static void
161 pargdef(definition *def)
163 decl_list *l;
164 version_list *vers;
165 char *name;
166 proc_list *plist;
169 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
170 for (plist = vers->procs; plist != NULL;
171 plist = plist->next) {
173 if (!newstyle || plist->arg_num < 2) {
174 continue; /* old style or single args */
176 name = plist->args.argname;
177 f_print(fout, "struct %s {\n", name);
178 for (l = plist->args.decls;
179 l != NULL; l = l->next) {
180 pdeclaration(name, &l->decl, 1,
181 ";\n");
183 f_print(fout, "};\n");
184 f_print(fout, "typedef struct %s %s;\n",
185 name, name);
186 storexdrfuncdecl(name, 1);
187 f_print(fout, "\n");
193 static void
194 pstructdef(definition *def)
196 decl_list *l;
197 const char *name = def->def_name;
199 f_print(fout, "struct %s {\n", name);
200 for (l = def->def.st.decls; l != NULL; l = l->next) {
201 pdeclaration(name, &l->decl, 1, ";\n");
203 f_print(fout, "};\n");
204 f_print(fout, "typedef struct %s %s;\n", name, name);
207 static void
208 puniondef(def)
209 definition *def;
211 case_list *l;
212 const char *name = def->def_name;
213 declaration *decl;
215 f_print(fout, "struct %s {\n", name);
216 decl = &def->def.un.enum_decl;
217 if (streq(decl->type, "bool")) {
218 f_print(fout, "\tbool_t %s;\n", decl->name);
219 } else {
220 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
222 f_print(fout, "\tunion {\n");
223 for (l = def->def.un.cases; l != NULL; l = l->next) {
224 if (l->contflag == 0)
225 pdeclaration(name, &l->case_decl, 2, ";\n");
227 decl = def->def.un.default_decl;
228 if (decl && !streq(decl->type, "void")) {
229 pdeclaration(name, decl, 2, ";\n");
231 f_print(fout, "\t} %s_u;\n", name);
232 f_print(fout, "};\n");
233 f_print(fout, "typedef struct %s %s;\n", name, name);
236 static void
237 pdefine(const char *name, const char *num)
239 f_print(fout, "#define\t%s %s\n", name, num);
242 static void
243 puldefine(const char *name, const char *num)
245 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
248 static int
249 define_printed(proc_list *stop, version_list *start)
251 version_list *vers;
252 proc_list *proc;
254 for (vers = start; vers != NULL; vers = vers->next) {
255 for (proc = vers->procs; proc != NULL; proc = proc->next) {
256 if (proc == stop) {
257 return (0);
258 } else if (streq(proc->proc_name, stop->proc_name)) {
259 return (1);
263 abort();
264 /* NOTREACHED */
267 static void
268 pfreeprocdef(const char * name, const char *vers)
270 f_print(fout, "extern int ");
271 pvname(name, vers);
272 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
275 static void
276 pdispatch(const char * name, const char *vers)
279 f_print(fout, "void ");
280 pvname(name, vers);
281 f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
284 static void
285 pprogramdef(definition *def, int headeronly)
287 version_list *vers;
288 proc_list *proc;
289 const char *ext;
291 pargdef(def);
293 puldefine(def->def_name, def->def.pr.prog_num);
294 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
295 if (tblflag) {
296 f_print(fout,
297 "extern struct rpcgen_table %s_%s_table[];\n",
298 locase(def->def_name), vers->vers_num);
299 f_print(fout,
300 "extern %s_%s_nproc;\n",
301 locase(def->def_name), vers->vers_num);
303 puldefine(vers->vers_name, vers->vers_num);
305 f_print(fout, "\n");
306 ext = "extern ";
307 if (headeronly) {
308 f_print(fout, "%s", ext);
309 pdispatch(def->def_name, vers->vers_num);
311 for (proc = vers->procs; proc != NULL; proc = proc->next) {
312 if (!define_printed(proc, def->def.pr.versions)) {
313 puldefine(proc->proc_name, proc->proc_num);
315 f_print(fout, "%s", ext);
316 pprocdef(proc, vers, "CLIENT *", 0);
317 f_print(fout, "%s", ext);
318 pprocdef(proc, vers, "struct svc_req *", 1);
320 pfreeprocdef(def->def_name, vers->vers_num);
324 static void
325 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
327 if (mtflag) {/* Print MT style stubs */
328 if (server_p)
329 f_print(fout, "bool_t ");
330 else
331 f_print(fout, "enum clnt_stat ");
332 } else {
333 ptype(proc->res_prefix, proc->res_type, 1);
334 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);
341 parglist(proc, addargtype);
346 /* print out argument list of procedure */
347 static void
348 parglist(proc_list *proc, const char *addargtype)
350 decl_list *dl;
352 f_print(fout, "(");
353 if (proc->arg_num < 2 && newstyle &&
354 streq(proc->args.decls->decl.type, "void")) {
355 /* 0 argument in new style: do nothing*/
357 else {
358 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
359 ptype(dl->decl.prefix, dl->decl.type, 1);
360 if (!newstyle)
361 f_print(fout, "*");
362 /* old style passes by reference */
363 f_print(fout, ", ");
367 if (mtflag) {
368 ptype(proc->res_prefix, proc->res_type, 1);
369 f_print(fout, "*, ");
372 f_print(fout, "%s);\n", addargtype);
376 static void
377 penumdef(def)
378 definition *def;
380 const char *name = def->def_name;
381 enumval_list *l;
382 const char *last = NULL;
383 int count = 0;
385 f_print(fout, "enum %s {\n", name);
386 for (l = def->def.en.vals; l != NULL; l = l->next) {
387 f_print(fout, "\t%s", l->name);
388 if (l->assignment) {
389 f_print(fout, " = %s", l->assignment);
390 last = l->assignment;
391 count = 1;
392 } else {
393 if (last == NULL) {
394 f_print(fout, " = %d", count++);
395 } else {
396 f_print(fout, " = %s + %d", last, count++);
399 if (l->next)
400 f_print(fout, ",\n");
401 else
402 f_print(fout, "\n");
404 f_print(fout, "};\n");
405 f_print(fout, "typedef enum %s %s;\n", name, name);
408 static void
409 ptypedef(def)
410 definition *def;
412 const char *name = def->def_name;
413 const char *old = def->def.ty.old_type;
414 char prefix[8]; /* enough to contain "struct ", including NUL */
415 relation rel = def->def.ty.rel;
418 if (!streq(name, old)) {
419 if (streq(old, "string")) {
420 old = "char";
421 rel = REL_POINTER;
422 } else if (streq(old, "opaque")) {
423 old = "char";
424 } else if (streq(old, "bool")) {
425 old = "bool_t";
427 if (undefined2(old, name) && def->def.ty.old_prefix) {
428 s_print(prefix, "%s ", def->def.ty.old_prefix);
429 } else {
430 prefix[0] = 0;
432 f_print(fout, "typedef ");
433 switch (rel) {
434 case REL_ARRAY:
435 f_print(fout, "struct {\n");
436 f_print(fout, "\tu_int %s_len;\n", name);
437 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
438 f_print(fout, "} %s", name);
439 break;
440 case REL_POINTER:
441 f_print(fout, "%s%s *%s", prefix, old, name);
442 break;
443 case REL_VECTOR:
444 f_print(fout, "%s%s %s[%s]", prefix, old, name,
445 def->def.ty.array_max);
446 break;
447 case REL_ALIAS:
448 f_print(fout, "%s%s %s", prefix, old, name);
449 break;
451 f_print(fout, ";\n");
455 void
456 pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
458 char buf[8]; /* enough to hold "struct ", include NUL */
459 const char *prefix;
460 const char *type;
462 if (streq(dec->type, "void")) {
463 return;
465 tabify(fout, tab);
466 if (streq(dec->type, name) && !dec->prefix) {
467 f_print(fout, "struct ");
469 if (streq(dec->type, "string")) {
470 f_print(fout, "char *%s", dec->name);
471 } else {
472 prefix = "";
473 if (streq(dec->type, "bool")) {
474 type = "bool_t";
475 } else if (streq(dec->type, "opaque")) {
476 type = "char";
477 } else {
478 if (dec->prefix) {
479 s_print(buf, "%s ", dec->prefix);
480 prefix = buf;
482 type = dec->type;
484 switch (dec->rel) {
485 case REL_ALIAS:
486 f_print(fout, "%s%s %s", prefix, type, dec->name);
487 break;
488 case REL_VECTOR:
489 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
490 dec->array_max);
491 break;
492 case REL_POINTER:
493 f_print(fout, "%s%s *%s", prefix, type, dec->name);
494 break;
495 case REL_ARRAY:
496 f_print(fout, "struct {\n");
497 tabify(fout, tab);
498 f_print(fout, "\tu_int %s_len;\n", dec->name);
499 tabify(fout, tab);
500 f_print(fout,
501 "\t%s%s *%s_val;\n", prefix, type, dec->name);
502 tabify(fout, tab);
503 f_print(fout, "} %s", dec->name);
504 break;
507 f_print(fout, separator);
510 static int
511 undefined2(const char *type, const char *stop)
513 list *l;
514 definition *def;
516 for (l = defined; l != NULL; l = l->next) {
517 def = (definition *) l->val;
518 if (def->def_kind != DEF_PROGRAM) {
519 if (streq(def->def_name, stop)) {
520 return (1);
521 } else if (streq(def->def_name, type)) {
522 return (0);
526 return (1);