If PCI_MAP_FIXUP is defined, we can no longer do the reallocation magic;
[dragonfly/port-amd64.git] / usr.bin / rpcgen / rpc_hout.c
blobe48e6de50c6265a086fc5d778b83372903688034
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.
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
29 * @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
30 * $DragonFly: src/usr.bin/rpcgen/rpc_hout.c,v 1.5 2004/06/19 16:40:36 joerg Exp $
33 #ident "@(#)rpc_hout.c 1.16 94/04/25 SMI"
36 * rpc_hout.c, Header file outputter for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsystems, Inc.
39 #include <stdio.h>
40 #include <ctype.h>
41 #include "rpc_parse.h"
42 #include "rpc_util.h"
44 void storexdrfuncdecl(char *, int);
45 static void pconstdef(definition *);
46 static void pstructdef(definition *);
47 static void puniondef(definition *);
48 static void pprogramdef(definition *);
49 static void pstructdef(definition *);
50 static void penumdef(definition *);
51 static void ptypedef(definition *);
52 static void pdefine(char *, char *);
53 static int undefined2(char *, char *);
54 static void parglist(proc_list *, char *);
55 static void pprocdef(proc_list *, version_list *, char *, int, int);
56 void pdeclaration(char *, declaration *, int, char *);
58 static char RESULT[] = "clnt_res";
61 * Print the C-version of an xdr definition
63 void
64 print_datadef(definition *def)
66 if (def->def_kind == DEF_PROGRAM) /* handle data only */
67 return;
69 if (def->def_kind != DEF_CONST)
70 f_print(fout, "\n");
72 switch (def->def_kind) {
73 case DEF_STRUCT:
74 pstructdef(def);
75 break;
76 case DEF_UNION:
77 puniondef(def);
78 break;
79 case DEF_ENUM:
80 penumdef(def);
81 break;
82 case DEF_TYPEDEF:
83 ptypedef(def);
84 break;
85 case DEF_PROGRAM:
86 pprogramdef(def);
87 break;
88 case DEF_CONST:
89 pconstdef(def);
90 break;
92 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
93 if (def->def_kind != DEF_TYPEDEF ||
94 !isvectordef(def->def.ty.old_type, def->def.ty.rel))
95 storexdrfuncdecl(def->def_name, 1);
96 else
97 storexdrfuncdecl(def->def_name, 0);
102 void
103 print_funcdef(definition *def)
105 switch (def->def_kind) {
106 case DEF_PROGRAM:
107 f_print(fout, "\n");
108 pprogramdef(def);
109 break;
110 default:
111 break;
115 /* store away enough information to allow the XDR functions to be spat
116 out at the end of the file */
118 void
119 storexdrfuncdecl(char *name, int pointerp)
121 xdrfunc * xdrptr;
123 xdrptr = malloc(sizeof(struct xdrfunc));
125 xdrptr->name = name;
126 xdrptr->pointerp = pointerp;
127 xdrptr->next = NULL;
129 if (xdrfunc_tail == NULL)
130 xdrfunc_head = xdrptr;
131 else
132 xdrfunc_tail->next = xdrptr;
133 xdrfunc_tail = xdrptr;
137 void
138 print_xdr_func_def(char *name, int pointerp, int i)
140 if (i == 2)
141 f_print(fout, "extern bool_t xdr_%s();\n", name);
142 else
143 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
144 name, pointerp ? "*" : "");
148 static void
149 pconstdef(definition *def)
151 pdefine(def->def_name, def->def.co);
154 /* print out the definitions for the arguments of functions in the
155 header file
157 static void
158 pargdef(definition *def)
160 decl_list *l;
161 version_list *vers;
162 char *name;
163 proc_list *plist;
165 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
166 for (plist = vers->procs; plist != NULL; plist = plist->next) {
167 if (!newstyle || plist->arg_num < 2)
168 continue; /* old style or single args */
170 name = plist->args.argname;
171 f_print(fout, "struct %s {\n", name);
172 for (l = plist->args.decls; l != NULL; l = l->next)
173 pdeclaration(name, &l->decl, 1, ";\n");
174 f_print(fout, "};\n");
175 f_print(fout, "typedef struct %s %s;\n", name, name);
176 storexdrfuncdecl(name, 1);
177 f_print(fout, "\n");
183 static void
184 pstructdef(definition *def)
186 decl_list *l;
187 char *name = def->def_name;
189 f_print(fout, "struct %s {\n", name);
190 for (l = def->def.st.decls; l != NULL; l = l->next)
191 pdeclaration(name, &l->decl, 1, ";\n");
193 f_print(fout, "};\n");
194 f_print(fout, "typedef struct %s %s;\n", name, name);
197 static void
198 puniondef(definition *def)
200 case_list *l;
201 char *name = def->def_name;
202 declaration *decl;
204 f_print(fout, "struct %s {\n", name);
205 decl = &def->def.un.enum_decl;
206 if (streq(decl->type, "bool"))
207 f_print(fout, "\tbool_t %s;\n", decl->name);
208 else
209 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
211 f_print(fout, "\tunion {\n");
212 for (l = def->def.un.cases; l != NULL; l = l->next) {
213 if (l->contflag == 0)
214 pdeclaration(name, &l->case_decl, 2, ";\n");
216 decl = def->def.un.default_decl;
217 if (decl && !streq(decl->type, "void")) {
218 pdeclaration(name, decl, 2, ";\n");
220 f_print(fout, "\t} %s_u;\n", name);
221 f_print(fout, "};\n");
222 f_print(fout, "typedef struct %s %s;\n", name, name);
225 static void
226 pdefine(char *name, char *num)
228 f_print(fout, "#define\t%s %s\n", name, num);
231 static void
232 puldefine(char *name, char *num)
234 f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
237 static int
238 define_printed(proc_list *stop, version_list *start)
240 version_list *vers;
241 proc_list *proc;
243 for (vers = start; vers != NULL; vers = vers->next) {
244 for (proc = vers->procs; proc != NULL; proc = proc->next) {
245 if (proc == stop)
246 return(0);
247 else if (streq(proc->proc_name, stop->proc_name))
248 return(1);
251 abort();
252 /* NOTREACHED */
255 static void
256 pfreeprocdef(char * name, char *vers, int mode)
258 f_print(fout, "extern int ");
259 pvname(name, vers);
260 if (mode == 1)
261 f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
262 else
263 f_print(fout,"_freeresult();\n");
266 static void
267 pprogramdef(definition *def)
269 version_list *vers;
270 proc_list *proc;
271 int i;
272 char *ext;
274 pargdef(def);
276 puldefine(def->def_name, def->def.pr.prog_num);
277 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
278 if (tblflag) {
279 f_print(fout,
280 "extern struct rpcgen_table %s_%s_table[];\n",
281 locase(def->def_name), vers->vers_num);
282 f_print(fout, "extern %s_%s_nproc;\n",
283 locase(def->def_name), vers->vers_num);
285 puldefine(vers->vers_name, vers->vers_num);
288 * Print out 2 definitions, one for ANSI-C, another for
289 * old K & R C
292 if(!Cflag){
293 ext = "extern ";
294 for (proc = vers->procs; proc != NULL;
295 proc = proc->next) {
296 if (!define_printed(proc,
297 def->def.pr.versions)) {
298 puldefine(proc->proc_name,
299 proc->proc_num);
301 f_print(fout, "%s", ext);
302 pprocdef(proc, vers, NULL, 0, 2);
304 if (mtflag) {
305 f_print(fout, "%s", ext);
306 pprocdef(proc, vers, NULL, 1, 2);
309 pfreeprocdef(def->def_name, vers->vers_num, 2);
310 } else {
311 for (i = 1; i < 3; i++) {
312 if (i == 1) {
313 f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
314 ext = "extern ";
315 } else {
316 f_print(fout, "\n#else /* K&R C */\n");
317 ext = "extern ";
320 for (proc = vers->procs; proc != NULL;
321 proc = proc->next) {
322 if (!define_printed(proc,
323 def->def.pr.versions)) {
324 puldefine(proc->proc_name,
325 proc->proc_num);
327 f_print(fout, "%s", ext);
328 pprocdef(proc, vers, "CLIENT *", 0, i);
329 f_print(fout, "%s", ext);
330 pprocdef(proc, vers, "struct svc_req *", 1, i);
332 pfreeprocdef(def->def_name, vers->vers_num, i);
334 f_print(fout, "#endif /* K&R C */\n");
339 static void
340 pprocdef(proc_list *proc, version_list *vp, char* addargtype, int server_p,
341 int mode)
343 if (mtflag) {
344 /* Print MT style stubs */
345 if (server_p)
346 f_print(fout, "bool_t ");
347 else
348 f_print(fout, "enum clnt_stat ");
349 } else {
350 ptype(proc->res_prefix, proc->res_type, 1);
351 f_print(fout, "* ");
353 if (server_p)
354 pvname_svc(proc->proc_name, vp->vers_num);
355 else
356 pvname(proc->proc_name, vp->vers_num);
359 * mode 1 = ANSI-C, mode 2 = K&R C
361 if ( mode == 1)
362 parglist(proc, addargtype);
363 else
364 f_print(fout, "();\n");
369 /* print out argument list of procedure */
370 static void
371 parglist(proc_list *proc, char* addargtype)
373 decl_list *dl;
375 f_print(fout, "(");
376 if (proc->arg_num < 2 && newstyle &&
377 streq(proc->args.decls->decl.type, "void")) {
378 /* 0 argument in new style: do nothing*/
380 else {
381 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
382 ptype(dl->decl.prefix, dl->decl.type, 1);
383 if (!newstyle)
384 f_print(fout, "*");
385 /* old style passes by reference */
386 f_print(fout, ", ");
390 if (mtflag) {
391 ptype(proc->res_prefix, proc->res_type, 1);
392 f_print(fout, "*, ");
395 f_print(fout, "%s);\n", addargtype);
398 static void
399 penumdef(definition *def)
401 char *name = def->def_name;
402 enumval_list *l;
403 char *last = NULL;
404 int count = 0;
406 f_print(fout, "enum %s {\n", name);
407 for (l = def->def.en.vals; l != NULL; l = l->next) {
408 f_print(fout, "\t%s", l->name);
409 if (l->assignment) {
410 f_print(fout, " = %s", l->assignment);
411 last = l->assignment;
412 count = 1;
413 } else {
414 if (last == NULL)
415 f_print(fout, " = %d", count++);
416 else
417 f_print(fout, " = %s + %d", last, count++);
419 if (l->next)
420 f_print(fout, ",\n");
421 else
422 f_print(fout, "\n");
424 f_print(fout, "};\n");
425 f_print(fout, "typedef enum %s %s;\n", name, name);
428 static void
429 ptypedef(definition *def)
431 char *name = def->def_name;
432 char *old = def->def.ty.old_type;
433 char prefix[8]; /* enough to contain "struct ", including NUL */
434 relation rel = def->def.ty.rel;
436 if (!streq(name, old)) {
437 if (streq(old, "string")) {
438 old = "char";
439 rel = REL_POINTER;
440 } else if (streq(old, "opaque")) {
441 old = "char";
442 } else if (streq(old, "bool")) {
443 old = "bool_t";
445 if (undefined2(old, name) && def->def.ty.old_prefix)
446 s_print(prefix, "%s ", def->def.ty.old_prefix);
447 else
448 prefix[0] = 0;
449 f_print(fout, "typedef ");
450 switch (rel) {
451 case REL_ARRAY:
452 f_print(fout, "struct {\n");
453 f_print(fout, "\tu_int %s_len;\n", name);
454 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
455 f_print(fout, "} %s", name);
456 break;
457 case REL_POINTER:
458 f_print(fout, "%s%s *%s", prefix, old, name);
459 break;
460 case REL_VECTOR:
461 f_print(fout, "%s%s %s[%s]", prefix, old, name,
462 def->def.ty.array_max);
463 break;
464 case REL_ALIAS:
465 f_print(fout, "%s%s %s", prefix, old, name);
466 break;
468 f_print(fout, ";\n");
472 void
473 pdeclaration(char *name, declaration *dec, int tab, char *separator)
475 char buf[8]; /* enough to hold "struct ", include NUL */
476 char *prefix;
477 char *type;
479 if (streq(dec->type, "void"))
480 return;
482 tabify(fout, tab);
483 if (streq(dec->type, name) && !dec->prefix)
484 f_print(fout, "struct ");
486 if (streq(dec->type, "string")) {
487 f_print(fout, "char *%s", dec->name);
488 } else {
489 prefix = "";
490 if (streq(dec->type, "bool")) {
491 type = "bool_t";
492 } else if (streq(dec->type, "opaque")) {
493 type = "char";
494 } else {
495 if (dec->prefix) {
496 s_print(buf, "%s ", dec->prefix);
497 prefix = buf;
499 type = dec->type;
501 switch (dec->rel) {
502 case REL_ALIAS:
503 f_print(fout, "%s%s %s", prefix, type, dec->name);
504 break;
505 case REL_VECTOR:
506 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
507 dec->array_max);
508 break;
509 case REL_POINTER:
510 f_print(fout, "%s%s *%s", prefix, type, dec->name);
511 break;
512 case REL_ARRAY:
513 f_print(fout, "struct {\n");
514 tabify(fout, tab);
515 f_print(fout, "\tu_int %s_len;\n", dec->name);
516 tabify(fout, tab);
517 f_print(fout,
518 "\t%s%s *%s_val;\n", prefix, type, dec->name);
519 tabify(fout, tab);
520 f_print(fout, "} %s", dec->name);
521 break;
524 f_print(fout, separator);
527 static int
528 undefined2(char *type, char *stop)
530 list *l;
531 definition *def;
533 for (l = defined; l != NULL; l = l->next) {
534 def = (definition *) l->val;
535 if (def->def_kind != DEF_PROGRAM) {
536 if (streq(def->def_name, stop))
537 return(1);
538 else if (streq(def->def_name, type))
539 return(0);
542 return(1);