2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
34 * rpc_hout.c, Header file outputter for the RPC protocol compiler
38 #include "rpc_parse.h"
40 #include "rpc_output.h"
43 static int undefined2(char *type, char *stop);
44 static void pxdrfuncdecl(char *name, int pointerp);
45 static void pconstdef(definition *def);
46 static void pargdef(definition *def);
47 static void pstructdef(definition *def);
48 static void puniondef(definition *def);
49 static void pdefine(char *name, char *num);
50 static void puldefine(char *name, char *num);
51 static int define_printed(proc_list *stop, version_list *start);
52 static void pprogramdef(definition *def);
53 static void pprocdef(proc_list *proc, version_list *vp,
54 char *addargtype, int server_p, int mode);
55 static void parglist(proc_list *proc, char *addargtype);
56 static void penumdef(definition *def);
57 static void ptypedef(definition *def);
60 * Print the C-version of an xdr definition
63 print_datadef(definition *def)
66 if (def->def_kind == DEF_PROGRAM ) /* handle data only */
69 if (def->def_kind != DEF_CONST) {
72 switch (def->def_kind) {
92 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
93 pxdrfuncdecl( def->def_name,
94 def->def_kind != DEF_TYPEDEF ||
95 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
102 print_funcdef(definition *def)
104 switch (def->def_kind) {
115 pxdrfuncdecl(char *name, int pointerp)
118 "#ifdef __cplusplus \n"
119 "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n"
121 "extern bool_t xdr_%s(XDR *, %s%s);\n"
122 "#else /* Old Style C */ \n"
124 "#endif /* Old Style C */ \n\n",
125 name, name, pointerp ? "*" : "",
126 name, name, pointerp ? "*" : "",
132 pconstdef(definition *def)
134 pdefine(def->def_name, def->def.co);
137 /* print out the definitions for the arguments of functions in the
141 pargdef(definition *def)
149 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
150 for(plist = vers->procs; plist != NULL;
151 plist = plist->next) {
153 if (!newstyle || plist->arg_num < 2) {
154 continue; /* old style or single args */
156 name = plist->args.argname;
157 f_print(fout, "struct %s {\n", name);
158 for (l = plist->args.decls;
159 l != NULL; l = l->next) {
160 pdeclaration(name, &l->decl, 1, ";\n" );
162 f_print(fout, "};\n");
163 f_print(fout, "typedef struct %s %s;\n", name, name);
164 pxdrfuncdecl(name, 0);
165 f_print( fout, "\n" );
173 pstructdef(definition *def)
176 char *name = def->def_name;
178 f_print(fout, "struct %s {\n", name);
179 for (l = def->def.st.decls; l != NULL; l = l->next) {
180 pdeclaration(name, &l->decl, 1, ";\n");
182 f_print(fout, "};\n");
183 f_print(fout, "typedef struct %s %s;\n", name, name);
187 puniondef(definition *def)
190 char *name = def->def_name;
193 f_print(fout, "struct %s {\n", name);
194 decl = &def->def.un.enum_decl;
195 if (streq(decl->type, "bool")) {
196 f_print(fout, "\tbool_t %s;\n", decl->name);
198 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
200 f_print(fout, "\tunion {\n");
201 for (l = def->def.un.cases; l != NULL; l = l->next) {
202 if (l->contflag == 0)
203 pdeclaration(name, &l->case_decl, 2, ";\n");
205 decl = def->def.un.default_decl;
206 if (decl && !streq(decl->type, "void")) {
207 pdeclaration(name, decl, 2, ";\n");
209 f_print(fout, "\t} %s_u;\n", name);
210 f_print(fout, "};\n");
211 f_print(fout, "typedef struct %s %s;\n", name, name);
215 pdefine(char *name, char *num)
217 f_print(fout, "#define %s %s\n", name, num);
221 puldefine(char *name, char *num)
223 f_print(fout, "#define %s ((u_int32_t)%s)\n", name, num);
227 define_printed(proc_list *stop, version_list *start)
232 for (vers = start; vers != NULL; vers = vers->next) {
233 for (proc = vers->procs; proc != NULL; proc = proc->next) {
236 } else if (streq(proc->proc_name, stop->proc_name)) {
246 pprogramdef(definition *def)
255 puldefine(def->def_name, def->def.pr.prog_num);
256 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
258 f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
259 locase(def->def_name), vers->vers_num);
260 f_print(fout, "extern %s_%s_nproc;\n",
261 locase(def->def_name), vers->vers_num);
263 puldefine(vers->vers_name, vers->vers_num);
266 * Print out 3 definitions, one for ANSI-C, another for C++,
267 * a third for old style C
270 for (i = 0; i < 3; i++) {
272 f_print(fout, "\n#ifdef __cplusplus\n");
273 ext = "extern \"C\" ";
275 f_print(fout, "\n#elif __STDC__\n");
278 f_print(fout, "\n#else /* Old Style C */ \n");
283 for (proc = vers->procs; proc != NULL; proc = proc->next) {
284 if (!define_printed(proc, def->def.pr.versions)) {
285 puldefine(proc->proc_name, proc->proc_num);
287 f_print(fout, "%s", ext);
288 pprocdef(proc, vers, "CLIENT *", 0, i);
289 f_print(fout, "%s", ext);
290 pprocdef(proc, vers, "struct svc_req *", 1, i);
295 f_print(fout, "#endif /* Old Style C */ \n");
300 pprocdef(proc_list *proc, version_list *vp, char *addargtype,
301 int server_p, int mode)
303 ptype(proc->res_prefix, proc->res_type, 1);
306 pvname_svc(proc->proc_name, vp->vers_num);
308 pvname(proc->proc_name, vp->vers_num);
311 * mode 0 == cplusplus, mode 1 = ANSI-C, mode 2 = old style C
313 if (mode == 0 || mode == 1)
314 parglist(proc, addargtype);
316 f_print(fout, "();\n");
321 /* print out argument list of procedure */
323 parglist(proc_list *proc, char *addargtype)
329 if (proc->arg_num < 2 && newstyle &&
330 streq(proc->args.decls->decl.type, "void")) {
331 /* 0 argument in new style: do nothing */
333 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
334 ptype(dl->decl.prefix, dl->decl.type, 1);
336 f_print(fout, "*"); /* old style passes by reference */
342 f_print(fout, "%s);\n", addargtype);
346 penumdef(definition *def)
348 char *name = def->def_name;
353 f_print(fout, "enum %s {\n", name);
354 for (l = def->def.en.vals; l != NULL; l = l->next) {
355 f_print(fout, "\t%s", l->name);
357 f_print(fout, " = %s", l->assignment);
358 last = l->assignment;
362 f_print(fout, " = %d", count++);
364 f_print(fout, " = %s + %d", last, count++);
367 f_print(fout, ",\n");
369 f_print(fout, "};\n");
370 f_print(fout, "typedef enum %s %s;\n", name, name);
374 ptypedef(definition *def)
376 char *name = def->def_name;
377 char *old = def->def.ty.old_type;
378 char prefix[8]; /* enough to contain "struct ", including NUL */
379 relation rel = def->def.ty.rel;
382 if (!streq(name, old)) {
383 if (streq(old, "string")) {
386 } else if (streq(old, "opaque")) {
388 } else if (streq(old, "bool")) {
391 if (undefined2(old, name) && def->def.ty.old_prefix) {
392 s_print(prefix, "%s ", def->def.ty.old_prefix);
396 f_print(fout, "typedef ");
399 f_print(fout, "struct {\n");
400 f_print(fout, "\tu_int %s_len;\n", name);
401 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
402 f_print(fout, "} %s", name);
405 f_print(fout, "%s%s *%s", prefix, old, name);
408 f_print(fout, "%s%s %s[%s]", prefix, old, name,
409 def->def.ty.array_max);
412 f_print(fout, "%s%s %s", prefix, old, name);
415 f_print(fout, ";\n");
420 pdeclaration(char *name, declaration *dec, int tab, char *separator)
422 char buf[8]; /* enough to hold "struct ", include NUL */
426 if (streq(dec->type, "void")) {
430 if (streq(dec->type, name) && !dec->prefix) {
431 f_print(fout, "struct ");
433 if (streq(dec->type, "string")) {
434 f_print(fout, "char *%s", dec->name);
437 if (streq(dec->type, "bool")) {
439 } else if (streq(dec->type, "opaque")) {
443 s_print(buf, "%s ", dec->prefix);
450 f_print(fout, "%s%s %s", prefix, type, dec->name);
453 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
457 f_print(fout, "%s%s *%s", prefix, type, dec->name);
460 f_print(fout, "struct {\n");
462 f_print(fout, "\tu_int %s_len;\n", dec->name);
464 f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
466 f_print(fout, "} %s", dec->name);
470 f_print(fout, separator );
474 undefined2(char *type, char *stop)
479 for (l = defined; l != NULL; l = l->next) {
480 def = (definition *) l->val;
481 if (def->def_kind != DEF_PROGRAM) {
482 if (streq(def->def_name, stop)) {
484 } else if (streq(def->def_name, type)) {