8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / rpcgen / rpc_hout.c
bloba7173b929c9970353f4d8aff5453be3793fa4307
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
29 * University Copyright- Copyright (c) 1982, 1986, 1988
30 * The Regents of the University of California
31 * All Rights Reserved
33 * University Acknowledgment- Portions of this document are derived from
34 * software developed by the University of California, Berkeley, and its
35 * contributors.
39 * rpc_hout.c, Header file outputter for the RPC protocol compiler
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include "rpc_parse.h"
45 #include "rpc_util.h"
47 extern void pprocdef(proc_list *, version_list *, char *, int, int);
48 extern void pdeclaration(char *, declaration *, int, char *);
50 static void storexdrfuncdecl(char *, int);
51 static void pconstdef(definition *);
52 static void pstructdef(definition *);
53 static void puniondef(definition *);
54 static void pdefine(char *, char *);
55 static void pprogramdef(definition *);
56 static void parglist(proc_list *, char *);
57 static void penumdef(definition *);
58 static void ptypedef(definition *);
59 static uint_t undefined2(char *, char *);
61 enum rpc_gvc {
62 PROGRAM,
63 VERSION,
64 PROCEDURE
68 * Print the C-version of an xdr definition
70 void
71 print_datadef(definition *def)
73 if (def->def_kind == DEF_PROGRAM) /* handle data only */
74 return;
76 if (def->def_kind != DEF_CONST)
77 f_print(fout, "\n");
78 switch (def->def_kind) {
79 case DEF_STRUCT:
80 pstructdef(def);
81 break;
82 case DEF_UNION:
83 puniondef(def);
84 break;
85 case DEF_ENUM:
86 penumdef(def);
87 break;
88 case DEF_TYPEDEF:
89 ptypedef(def);
90 break;
91 case DEF_PROGRAM:
92 pprogramdef(def);
93 break;
94 case DEF_CONST:
95 pconstdef(def);
96 break;
98 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST)
99 storexdrfuncdecl(def->def_name, def->def_kind != DEF_TYPEDEF ||
100 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
104 void
105 print_funcdef(definition *def)
107 switch (def->def_kind) {
108 case DEF_PROGRAM:
109 f_print(fout, "\n");
110 pprogramdef(def);
111 break;
116 * store away enough information to allow the XDR functions to be spat
117 * out at the end of the file
119 static void
120 storexdrfuncdecl(char *name, int pointerp)
122 xdrfunc *xdrptr;
124 xdrptr = malloc(sizeof (struct xdrfunc));
126 xdrptr->name = name;
127 xdrptr->pointerp = pointerp;
128 xdrptr->next = NULL;
130 if (xdrfunc_tail == NULL) {
131 xdrfunc_head = xdrptr;
132 xdrfunc_tail = xdrptr;
133 } else {
134 xdrfunc_tail->next = xdrptr;
135 xdrfunc_tail = xdrptr;
141 void
142 print_xdr_func_def(char *name, int pointerp, int i)
144 if (i == 2)
145 f_print(fout, "extern bool_t xdr_%s();\n", name);
146 else
147 f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
148 name, pointerp ? "*" : "");
152 static void
153 pconstdef(definition *def)
155 pdefine(def->def_name, def->def.co);
159 * print out the definitions for the arguments of functions in the
160 * header file
162 static void
163 pargdef(definition *def)
165 decl_list *l;
166 version_list *vers;
167 char *name;
168 proc_list *plist;
170 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
171 for (plist = vers->procs; plist != NULL; plist = plist->next) {
172 if (!newstyle || plist->arg_num < 2)
173 continue; /* old style or single args */
174 name = plist->args.argname;
175 f_print(fout, "struct %s {\n", name);
176 for (l = plist->args.decls; l != NULL; l = l->next)
177 pdeclaration(name, &l->decl, 1, ";\n");
178 f_print(fout, "};\n");
179 f_print(fout, "typedef struct %s %s;\n", name, name);
180 storexdrfuncdecl(name, 1);
181 f_print(fout, "\n");
187 static void
188 pstructdef(definition *def)
190 decl_list *l;
191 char *name = def->def_name;
193 f_print(fout, "struct %s {\n", name);
194 for (l = def->def.st.decls; l != NULL; l = l->next)
195 pdeclaration(name, &l->decl, 1, ";\n");
196 f_print(fout, "};\n");
197 f_print(fout, "typedef struct %s %s;\n", name, name);
200 static void
201 puniondef(definition *def)
203 case_list *l;
204 char *name = def->def_name;
205 declaration *decl;
207 f_print(fout, "struct %s {\n", name);
208 decl = &def->def.un.enum_decl;
209 if (streq(decl->type, "bool"))
210 f_print(fout, "\tbool_t %s;\n", decl->name);
211 else
212 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
213 f_print(fout, "\tunion {\n");
214 for (l = def->def.un.cases; l != NULL; l = l->next) {
215 if (l->contflag == 0)
216 pdeclaration(name, &l->case_decl, 2, ";\n");
218 decl = def->def.un.default_decl;
219 if (decl && !streq(decl->type, "void"))
220 pdeclaration(name, decl, 2, ";\n");
221 f_print(fout, "\t} %s_u;\n", name);
222 f_print(fout, "};\n");
223 f_print(fout, "typedef struct %s %s;\n", name, name);
226 static void
227 pdefine(char *name, char *num)
229 f_print(fout, "#define\t%s %s\n", name, num);
232 static void
233 puldefine(char *name, char *num, enum rpc_gvc which)
235 switch (which) {
236 case PROGRAM:
237 case VERSION:
238 case PROCEDURE:
239 f_print(fout, "#define\t%s\t%s\n", name, num);
240 break;
241 default:
242 break;
246 static uint_t
247 define_printed(proc_list *stop, version_list *start)
249 version_list *vers;
250 proc_list *proc;
252 for (vers = start; vers != NULL; vers = vers->next) {
253 for (proc = vers->procs; proc != NULL; proc = proc->next) {
254 if (proc == stop)
255 return (0);
256 if (streq(proc->proc_name, stop->proc_name))
257 return (1);
260 abort();
261 /* NOTREACHED */
264 static void
265 pfreeprocdef(char *name, char *vers, int mode)
267 f_print(fout, "extern int ");
268 pvname(name, vers);
269 if (mode == 1)
270 f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
271 else
272 f_print(fout, "_freeresult();\n");
275 static void
276 pprogramdef(definition *def)
278 version_list *vers;
279 proc_list *proc;
280 int i;
281 char *ext;
283 pargdef(def);
285 puldefine(def->def_name, def->def.pr.prog_num, PROGRAM);
286 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
287 if (tblflag) {
288 f_print(fout,
289 "extern struct rpcgen_table %s_%s_table[];\n",
290 locase(def->def_name), vers->vers_num);
291 f_print(fout,
292 "extern int %s_%s_nproc;\n",
293 locase(def->def_name), vers->vers_num);
295 puldefine(vers->vers_name, vers->vers_num, VERSION);
298 * Print out 2 definitions, one for ANSI-C, another for
299 * old K & R C
302 if (!Cflag) {
303 ext = "extern ";
304 for (proc = vers->procs; proc != NULL;
305 proc = proc->next) {
306 if (!define_printed(proc, def->def.pr.versions))
307 puldefine(proc->proc_name,
308 proc->proc_num, PROCEDURE);
309 f_print(fout, "%s", ext);
310 pprocdef(proc, vers, NULL, 0, 2);
312 if (mtflag) {
313 f_print(fout, "%s", ext);
314 pprocdef(proc, vers, NULL, 1, 2);
317 pfreeprocdef(def->def_name, vers->vers_num, 2);
318 } else {
319 for (i = 1; i < 3; i++) {
320 if (i == 1) {
321 f_print(fout, "\n#if defined(__STDC__)"
322 " || defined(__cplusplus)\n");
323 ext = "extern ";
324 } else {
325 f_print(fout, "\n#else /* K&R C */\n");
326 ext = "extern ";
329 for (proc = vers->procs; proc != NULL;
330 proc = proc->next) {
331 if (!define_printed(proc,
332 def->def.pr.versions)) {
333 puldefine(proc->proc_name,
334 proc->proc_num, PROCEDURE);
336 f_print(fout, "%s", ext);
337 pprocdef(proc, vers, "CLIENT *", 0, i);
338 f_print(fout, "%s", ext);
339 pprocdef(proc, vers,
340 "struct svc_req *", 1, i);
342 pfreeprocdef(def->def_name, vers->vers_num, i);
344 f_print(fout, "#endif /* K&R C */\n");
349 void
350 pprocdef(proc_list *proc, version_list *vp, char *addargtype, int server_p,
351 int mode)
353 if (mtflag) {
354 /* Print MT style stubs */
355 if (server_p)
356 f_print(fout, "bool_t ");
357 else
358 f_print(fout, "enum clnt_stat ");
359 } else {
360 ptype(proc->res_prefix, proc->res_type, 1);
361 f_print(fout, "* ");
363 if (server_p)
364 pvname_svc(proc->proc_name, vp->vers_num);
365 else
366 pvname(proc->proc_name, vp->vers_num);
369 * mode 1 = ANSI-C, mode 2 = K&R C
371 if (mode == 1)
372 parglist(proc, addargtype);
373 else
374 f_print(fout, "();\n");
377 /* print out argument list of procedure */
378 static void
379 parglist(proc_list *proc, char *addargtype)
381 decl_list *dl;
382 int oneway = streq(proc->res_type, "oneway");
384 f_print(fout, "(");
385 if (proc->arg_num < 2 && newstyle &&
386 streq(proc->args.decls->decl.type, "void")) {
387 /* 0 argument in new style: do nothing */
388 /* EMPTY */
389 } else {
390 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
391 ptype(dl->decl.prefix, dl->decl.type, 1);
392 if (!newstyle || (dl->decl.rel == REL_POINTER))
393 f_print(fout, "*");
394 /* old style passes by reference */
395 f_print(fout, ", ");
399 if (mtflag && !oneway) {
400 ptype(proc->res_prefix, proc->res_type, 1);
401 f_print(fout, "*, ");
404 f_print(fout, "%s);\n", addargtype);
407 static void
408 penumdef(definition *def)
410 char *name = def->def_name;
411 enumval_list *l;
412 char *last = NULL;
413 int count = 0;
415 f_print(fout, "enum %s {\n", name);
416 for (l = def->def.en.vals; l != NULL; l = l->next) {
417 f_print(fout, "\t%s", l->name);
418 if (l->assignment) {
419 f_print(fout, " = %s", l->assignment);
420 last = l->assignment;
421 count = 1;
422 } else {
423 if (last == NULL)
424 f_print(fout, " = %d", count++);
425 else
426 f_print(fout, " = %s + %d", last, count++);
428 if (l->next)
429 f_print(fout, ",\n");
430 else
431 f_print(fout, "\n");
433 f_print(fout, "};\n");
434 f_print(fout, "typedef enum %s %s;\n", name, name);
437 static void
438 ptypedef(definition *def)
440 char *name = def->def_name;
441 char *old = def->def.ty.old_type;
442 char prefix[8]; /* enough to contain "struct ", including NUL */
443 relation rel = def->def.ty.rel;
446 if (!streq(name, old)) {
447 if (streq(old, "string")) {
448 old = "char";
449 rel = REL_POINTER;
450 } else if (streq(old, "opaque")) {
451 old = "char";
452 } else if (streq(old, "bool")) {
453 old = "bool_t";
455 if (undefined2(old, name) && def->def.ty.old_prefix)
456 (void) snprintf(prefix, sizeof (prefix), "%s ",
457 def->def.ty.old_prefix);
458 else
459 prefix[0] = 0;
460 f_print(fout, "typedef ");
461 switch (rel) {
462 case REL_ARRAY:
463 f_print(fout, "struct {\n");
464 f_print(fout, "\tu_int %s_len;\n", name);
465 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
466 f_print(fout, "} %s", name);
467 break;
468 case REL_POINTER:
469 f_print(fout, "%s%s *%s", prefix, old, name);
470 break;
471 case REL_VECTOR:
472 f_print(fout, "%s%s %s[%s]", prefix, old, name,
473 def->def.ty.array_max);
474 break;
475 case REL_ALIAS:
476 f_print(fout, "%s%s %s", prefix, old, name);
477 break;
479 f_print(fout, ";\n");
483 void
484 pdeclaration(char *name, declaration *dec, int tab, char *separator)
486 char buf[8]; /* enough to hold "struct ", include NUL */
487 char *prefix;
488 char *type;
490 if (streq(dec->type, "void"))
491 return;
492 tabify(fout, tab);
493 if (streq(dec->type, name) && !dec->prefix)
494 f_print(fout, "struct ");
495 if (streq(dec->type, "string")) {
496 f_print(fout, "char *%s", dec->name);
497 } else {
498 prefix = "";
499 if (streq(dec->type, "bool")) {
500 type = "bool_t";
501 } else if (streq(dec->type, "opaque")) {
502 type = "char";
503 } else {
504 if (dec->prefix) {
505 (void) snprintf(buf, sizeof (buf),
506 "%s ", dec->prefix);
507 prefix = buf;
509 type = dec->type;
511 switch (dec->rel) {
512 case REL_ALIAS:
513 f_print(fout, "%s%s %s", prefix, type, dec->name);
514 break;
515 case REL_VECTOR:
516 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
517 dec->array_max);
518 break;
519 case REL_POINTER:
520 f_print(fout, "%s%s *%s", prefix, type, dec->name);
521 break;
522 case REL_ARRAY:
523 f_print(fout, "struct {\n");
524 tabify(fout, tab);
525 f_print(fout, "\tu_int %s_len;\n", dec->name);
526 tabify(fout, tab);
527 f_print(fout,
528 "\t%s%s *%s_val;\n", prefix, type, dec->name);
529 tabify(fout, tab);
530 f_print(fout, "} %s", dec->name);
531 break;
534 /* LINTED variable format */
535 f_print(fout, separator);
538 static uint_t
539 undefined2(char *type, char *stop)
541 list *l;
542 definition *def;
544 for (l = defined; l != NULL; l = l->next) {
545 def = (definition *) l->val;
546 if (def->def_kind != DEF_PROGRAM) {
547 if (streq(def->def_name, stop))
548 return (1);
549 if (streq(def->def_name, type))
550 return (0);
553 return (1);