rpc_hout.c revision 1.21 1 1.21 dholland /* $NetBSD: rpc_hout.c,v 1.21 2013/08/11 08:03:10 dholland Exp $ */
2 1.1 glass /*
3 1.1 glass * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 1.1 glass * unrestricted use provided that this legend is included on all tape
5 1.1 glass * media and as a part of the software program in whole or part. Users
6 1.1 glass * may copy or modify Sun RPC without charge, but are not authorized
7 1.1 glass * to license or distribute it to anyone else except as part of a product or
8 1.4 pk * program developed by the user or with the express written consent of
9 1.4 pk * Sun Microsystems, Inc.
10 1.4 pk *
11 1.1 glass * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 glass * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 glass * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.4 pk *
15 1.1 glass * Sun RPC is provided with no support and without any obligation on the
16 1.1 glass * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 glass * modification or enhancement.
18 1.4 pk *
19 1.1 glass * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 glass * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 glass * OR ANY PART THEREOF.
22 1.4 pk *
23 1.1 glass * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 glass * or profits or other special, indirect and consequential damages, even if
25 1.1 glass * Sun has been advised of the possibility of such damages.
26 1.4 pk *
27 1.1 glass * Sun Microsystems, Inc.
28 1.1 glass * 2550 Garcia Avenue
29 1.1 glass * Mountain View, California 94043
30 1.1 glass */
31 1.4 pk
32 1.20 jmc #if HAVE_NBTOOL_CONFIG_H
33 1.20 jmc #include "nbtool_config.h"
34 1.20 jmc #endif
35 1.20 jmc
36 1.6 christos #include <sys/cdefs.h>
37 1.17 tv #if defined(__RCSID) && !defined(lint)
38 1.6 christos #if 0
39 1.4 pk static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
40 1.6 christos #else
41 1.21 dholland __RCSID("$NetBSD: rpc_hout.c,v 1.21 2013/08/11 08:03:10 dholland Exp $");
42 1.6 christos #endif
43 1.1 glass #endif
44 1.1 glass
45 1.1 glass /*
46 1.7 lukem * rpc_hout.c, Header file outputter for the RPC protocol compiler
47 1.1 glass */
48 1.17 tv #include <ctype.h>
49 1.17 tv #include <err.h>
50 1.1 glass #include <stdio.h>
51 1.6 christos #include "rpc_scan.h"
52 1.4 pk #include "rpc_parse.h"
53 1.1 glass #include "rpc_util.h"
54 1.1 glass
55 1.6 christos static void pconstdef __P((definition *));
56 1.6 christos static void pargdef __P((definition *));
57 1.6 christos static void pstructdef __P((definition *));
58 1.6 christos static void puniondef __P((definition *));
59 1.21 dholland static void pdefine __P((const char *, const char *));
60 1.21 dholland static void puldefine __P((const char *, const char *));
61 1.6 christos static int define_printed __P((proc_list *, version_list *));
62 1.6 christos static void pprogramdef __P((definition *));
63 1.6 christos static void penumdef __P((definition *));
64 1.6 christos static void ptypedef __P((definition *));
65 1.21 dholland static int undefined2 __P((const char *, const char *));
66 1.1 glass
67 1.1 glass /*
68 1.7 lukem * Print the C-version of an xdr definition
69 1.1 glass */
70 1.1 glass void
71 1.21 dholland print_datadef(definition *def)
72 1.1 glass {
73 1.4 pk
74 1.7 lukem if (def->def_kind == DEF_PROGRAM) /* handle data only */
75 1.7 lukem return;
76 1.4 pk
77 1.1 glass if (def->def_kind != DEF_CONST) {
78 1.1 glass f_print(fout, "\n");
79 1.1 glass }
80 1.1 glass switch (def->def_kind) {
81 1.1 glass case DEF_STRUCT:
82 1.1 glass pstructdef(def);
83 1.1 glass break;
84 1.1 glass case DEF_UNION:
85 1.1 glass puniondef(def);
86 1.1 glass break;
87 1.1 glass case DEF_ENUM:
88 1.1 glass penumdef(def);
89 1.1 glass break;
90 1.1 glass case DEF_TYPEDEF:
91 1.1 glass ptypedef(def);
92 1.1 glass break;
93 1.1 glass case DEF_PROGRAM:
94 1.1 glass pprogramdef(def);
95 1.1 glass break;
96 1.1 glass case DEF_CONST:
97 1.1 glass pconstdef(def);
98 1.1 glass break;
99 1.1 glass }
100 1.1 glass if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
101 1.7 lukem pxdrfuncdecl(def->def_name,
102 1.7 lukem def->def_kind != DEF_TYPEDEF ||
103 1.7 lukem !isvectordef(def->def.ty.old_type, def->def.ty.rel));
104 1.4 pk
105 1.1 glass }
106 1.4 pk }
107 1.4 pk
108 1.4 pk
109 1.4 pk void
110 1.21 dholland print_funcdef(definition *def)
111 1.4 pk {
112 1.4 pk switch (def->def_kind) {
113 1.4 pk case DEF_PROGRAM:
114 1.1 glass f_print(fout, "\n");
115 1.4 pk pprogramdef(def);
116 1.4 pk break;
117 1.6 christos case DEF_CONST:
118 1.6 christos case DEF_TYPEDEF:
119 1.6 christos case DEF_ENUM:
120 1.6 christos case DEF_UNION:
121 1.6 christos case DEF_STRUCT:
122 1.6 christos break;
123 1.7 lukem }
124 1.4 pk }
125 1.4 pk
126 1.6 christos void
127 1.21 dholland pxdrfuncdecl(const char *name, int pointerp)
128 1.7 lukem {
129 1.7 lukem
130 1.7 lukem f_print(fout, "#ifdef __cplusplus\n");
131 1.7 lukem f_print(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n",
132 1.7 lukem name,
133 1.13 lukem name, pointerp ? (" *") : "");
134 1.7 lukem f_print(fout, "#elif __STDC__\n");
135 1.7 lukem f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n",
136 1.7 lukem name,
137 1.13 lukem name, pointerp ? (" *") : "");
138 1.7 lukem f_print(fout, "#else /* Old Style C */\n");
139 1.7 lukem f_print(fout, "bool_t xdr_%s();\n", name);
140 1.7 lukem f_print(fout, "#endif /* Old Style C */\n\n");
141 1.1 glass }
142 1.1 glass
143 1.4 pk
144 1.6 christos static void
145 1.21 dholland pconstdef(definition *def)
146 1.1 glass {
147 1.1 glass pdefine(def->def_name, def->def.co);
148 1.1 glass }
149 1.21 dholland
150 1.7 lukem /* print out the definitions for the arguments of functions in the
151 1.7 lukem header file
152 1.4 pk */
153 1.7 lukem static void
154 1.21 dholland pargdef(definition *def)
155 1.4 pk {
156 1.4 pk decl_list *l;
157 1.4 pk version_list *vers;
158 1.7 lukem char *name;
159 1.4 pk proc_list *plist;
160 1.4 pk
161 1.7 lukem
162 1.4 pk for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
163 1.7 lukem for (plist = vers->procs; plist != NULL;
164 1.7 lukem plist = plist->next) {
165 1.7 lukem
166 1.7 lukem if (!newstyle || plist->arg_num < 2) {
167 1.7 lukem continue; /* old style or single args */
168 1.7 lukem }
169 1.7 lukem name = plist->args.argname;
170 1.7 lukem f_print(fout, "struct %s {\n", name);
171 1.7 lukem for (l = plist->args.decls;
172 1.7 lukem l != NULL; l = l->next) {
173 1.7 lukem pdeclaration(name, &l->decl, 1, ";\n");
174 1.4 pk }
175 1.7 lukem f_print(fout, "};\n");
176 1.7 lukem f_print(fout, "typedef struct %s %s;\n", name, name);
177 1.13 lukem pxdrfuncdecl(name, 1);
178 1.7 lukem f_print(fout, "\n");
179 1.4 pk }
180 1.7 lukem }
181 1.4 pk
182 1.4 pk }
183 1.4 pk
184 1.4 pk
185 1.7 lukem static void
186 1.21 dholland pstructdef(definition *def)
187 1.1 glass {
188 1.1 glass decl_list *l;
189 1.21 dholland const char *name = def->def_name;
190 1.1 glass
191 1.1 glass f_print(fout, "struct %s {\n", name);
192 1.1 glass for (l = def->def.st.decls; l != NULL; l = l->next) {
193 1.4 pk pdeclaration(name, &l->decl, 1, ";\n");
194 1.1 glass }
195 1.1 glass f_print(fout, "};\n");
196 1.1 glass f_print(fout, "typedef struct %s %s;\n", name, name);
197 1.1 glass }
198 1.1 glass
199 1.6 christos static void
200 1.21 dholland puniondef(definition *def)
201 1.1 glass {
202 1.1 glass case_list *l;
203 1.21 dholland const char *name = def->def_name;
204 1.1 glass declaration *decl;
205 1.1 glass
206 1.1 glass f_print(fout, "struct %s {\n", name);
207 1.1 glass decl = &def->def.un.enum_decl;
208 1.1 glass if (streq(decl->type, "bool")) {
209 1.1 glass f_print(fout, "\tbool_t %s;\n", decl->name);
210 1.1 glass } else {
211 1.1 glass f_print(fout, "\t%s %s;\n", decl->type, decl->name);
212 1.1 glass }
213 1.1 glass f_print(fout, "\tunion {\n");
214 1.1 glass for (l = def->def.un.cases; l != NULL; l = l->next) {
215 1.7 lukem if (l->contflag == 0)
216 1.7 lukem pdeclaration(name, &l->case_decl, 2, ";\n");
217 1.1 glass }
218 1.1 glass decl = def->def.un.default_decl;
219 1.1 glass if (decl && !streq(decl->type, "void")) {
220 1.7 lukem pdeclaration(name, decl, 2, ";\n");
221 1.1 glass }
222 1.1 glass f_print(fout, "\t} %s_u;\n", name);
223 1.1 glass f_print(fout, "};\n");
224 1.1 glass f_print(fout, "typedef struct %s %s;\n", name, name);
225 1.1 glass }
226 1.1 glass
227 1.6 christos static void
228 1.21 dholland pdefine(const char *name, const char *num)
229 1.1 glass {
230 1.1 glass f_print(fout, "#define %s %s\n", name, num);
231 1.1 glass }
232 1.1 glass
233 1.6 christos static void
234 1.21 dholland puldefine(const char *name, const char *num)
235 1.1 glass {
236 1.12 fvdl f_print(fout, "#define %s %s\n", name, num);
237 1.1 glass }
238 1.1 glass
239 1.6 christos static int
240 1.21 dholland define_printed(proc_list *stop, version_list *start)
241 1.1 glass {
242 1.1 glass version_list *vers;
243 1.1 glass proc_list *proc;
244 1.1 glass
245 1.1 glass for (vers = start; vers != NULL; vers = vers->next) {
246 1.1 glass for (proc = vers->procs; proc != NULL; proc = proc->next) {
247 1.1 glass if (proc == stop) {
248 1.1 glass return (0);
249 1.7 lukem } else
250 1.7 lukem if (streq(proc->proc_name, stop->proc_name)) {
251 1.7 lukem return (1);
252 1.7 lukem }
253 1.1 glass }
254 1.1 glass }
255 1.19 itojun errx(1, "Internal error %s, %d: procedure not found",
256 1.6 christos __FILE__, __LINE__);
257 1.1 glass /* NOTREACHED */
258 1.1 glass }
259 1.1 glass
260 1.6 christos static void
261 1.21 dholland pprogramdef(definition *def)
262 1.1 glass {
263 1.1 glass version_list *vers;
264 1.1 glass proc_list *proc;
265 1.7 lukem int i;
266 1.21 dholland const char *ext;
267 1.7 lukem
268 1.4 pk pargdef(def);
269 1.1 glass
270 1.1 glass puldefine(def->def_name, def->def.pr.prog_num);
271 1.1 glass for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
272 1.4 pk if (tblflag) {
273 1.4 pk f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
274 1.7 lukem locase(def->def_name), vers->vers_num);
275 1.4 pk f_print(fout, "extern %s_%s_nproc;\n",
276 1.7 lukem locase(def->def_name), vers->vers_num);
277 1.4 pk }
278 1.1 glass puldefine(vers->vers_name, vers->vers_num);
279 1.4 pk
280 1.7 lukem /* Print out 3 definitions, one for ANSI-C, another for C++, a
281 1.7 lukem * third for old style C */
282 1.7 lukem
283 1.7 lukem for (i = 0; i < 3; i++) {
284 1.7 lukem if (i == 0) {
285 1.7 lukem f_print(fout, "\n#ifdef __cplusplus\n");
286 1.7 lukem ext = "extern \"C\" ";
287 1.7 lukem } else
288 1.7 lukem if (i == 1) {
289 1.7 lukem f_print(fout, "\n#elif __STDC__\n");
290 1.7 lukem ext = "extern ";
291 1.7 lukem } else {
292 1.7 lukem f_print(fout, "\n#else /* Old Style C */\n");
293 1.7 lukem ext = "extern ";
294 1.7 lukem }
295 1.7 lukem
296 1.7 lukem
297 1.4 pk for (proc = vers->procs; proc != NULL; proc = proc->next) {
298 1.4 pk if (!define_printed(proc, def->def.pr.versions)) {
299 1.4 pk puldefine(proc->proc_name, proc->proc_num);
300 1.4 pk }
301 1.7 lukem f_print(fout, "%s", ext);
302 1.7 lukem pprocdef(proc, vers, "CLIENT *", 0, i);
303 1.7 lukem f_print(fout, "%s", ext);
304 1.7 lukem pprocdef(proc, vers, "struct svc_req *", 1, i);
305 1.7 lukem
306 1.1 glass }
307 1.4 pk
308 1.1 glass }
309 1.7 lukem f_print(fout, "#endif /* Old Style C */\n");
310 1.1 glass }
311 1.1 glass }
312 1.1 glass
313 1.6 christos void
314 1.21 dholland pprocdef(proc_list *proc, version_list *vp, const char *addargtype,
315 1.21 dholland int server_p, int mode)
316 1.4 pk {
317 1.15 mycroft decl_list *dl;
318 1.4 pk
319 1.15 mycroft if (Mflag) {
320 1.15 mycroft if (server_p)
321 1.15 mycroft f_print(fout, "bool_t ");
322 1.15 mycroft else
323 1.15 mycroft f_print(fout, "enum clnt_stat ");
324 1.15 mycroft } else {
325 1.15 mycroft ptype(proc->res_prefix, proc->res_type, 1);
326 1.15 mycroft f_print(fout, "*");
327 1.15 mycroft }
328 1.7 lukem if (server_p)
329 1.4 pk pvname_svc(proc->proc_name, vp->vers_num);
330 1.4 pk else
331 1.4 pk pvname(proc->proc_name, vp->vers_num);
332 1.4 pk
333 1.4 pk /*
334 1.7 lukem * mode 0 == cplusplus, mode 1 = ANSI-C, mode 2 = old style C
335 1.4 pk */
336 1.15 mycroft if (mode == 0 || mode == 1) {
337 1.15 mycroft f_print(fout, "(");
338 1.15 mycroft if (proc->arg_num < 2 && newstyle &&
339 1.15 mycroft streq(proc->args.decls->decl.type, "void")) {
340 1.15 mycroft /* 0 argument in new style: do nothing */
341 1.15 mycroft } else {
342 1.15 mycroft for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
343 1.15 mycroft ptype(dl->decl.prefix, dl->decl.type, 1);
344 1.15 mycroft if (!newstyle)
345 1.15 mycroft f_print(fout, "*");
346 1.15 mycroft f_print(fout, ", ");
347 1.15 mycroft }
348 1.15 mycroft }
349 1.15 mycroft if (Mflag) {
350 1.15 mycroft if (streq(proc->res_type, "void"))
351 1.15 mycroft f_print(fout, "char");
352 1.15 mycroft else
353 1.15 mycroft ptype(proc->res_prefix, proc->res_type, 0);
354 1.15 mycroft if (!isvectordef(proc->res_type, REL_ALIAS))
355 1.15 mycroft f_print(fout, "*");
356 1.15 mycroft f_print(fout, ", ");
357 1.15 mycroft }
358 1.15 mycroft f_print(fout, "%s);\n", addargtype);
359 1.15 mycroft }
360 1.4 pk else
361 1.4 pk f_print(fout, "();\n");
362 1.4 pk }
363 1.4 pk
364 1.1 glass
365 1.6 christos static void
366 1.21 dholland penumdef(definition *def)
367 1.1 glass {
368 1.21 dholland const char *name = def->def_name;
369 1.1 glass enumval_list *l;
370 1.21 dholland const char *last = NULL;
371 1.21 dholland int count = 0;
372 1.21 dholland const char *first = "";
373 1.1 glass
374 1.1 glass f_print(fout, "enum %s {\n", name);
375 1.1 glass for (l = def->def.en.vals; l != NULL; l = l->next) {
376 1.18 christos f_print(fout, "%s\t%s", first, l->name);
377 1.1 glass if (l->assignment) {
378 1.1 glass f_print(fout, " = %s", l->assignment);
379 1.1 glass last = l->assignment;
380 1.1 glass count = 1;
381 1.1 glass } else {
382 1.1 glass if (last == NULL) {
383 1.1 glass f_print(fout, " = %d", count++);
384 1.1 glass } else {
385 1.1 glass f_print(fout, " = %s + %d", last, count++);
386 1.1 glass }
387 1.1 glass }
388 1.18 christos first = ",\n";
389 1.1 glass }
390 1.18 christos f_print(fout, "\n};\n");
391 1.1 glass f_print(fout, "typedef enum %s %s;\n", name, name);
392 1.1 glass }
393 1.1 glass
394 1.6 christos static void
395 1.21 dholland ptypedef(definition *def)
396 1.1 glass {
397 1.21 dholland const char *name = def->def_name;
398 1.21 dholland const char *old = def->def.ty.old_type;
399 1.7 lukem char prefix[8]; /* enough to contain "struct ", including NUL */
400 1.1 glass relation rel = def->def.ty.rel;
401 1.1 glass
402 1.1 glass
403 1.1 glass if (!streq(name, old)) {
404 1.1 glass if (streq(old, "string")) {
405 1.1 glass old = "char";
406 1.1 glass rel = REL_POINTER;
407 1.7 lukem } else
408 1.7 lukem if (streq(old, "opaque")) {
409 1.7 lukem old = "char";
410 1.7 lukem } else
411 1.7 lukem if (streq(old, "bool")) {
412 1.7 lukem old = "bool_t";
413 1.7 lukem }
414 1.1 glass if (undefined2(old, name) && def->def.ty.old_prefix) {
415 1.1 glass s_print(prefix, "%s ", def->def.ty.old_prefix);
416 1.1 glass } else {
417 1.1 glass prefix[0] = 0;
418 1.1 glass }
419 1.1 glass f_print(fout, "typedef ");
420 1.1 glass switch (rel) {
421 1.1 glass case REL_ARRAY:
422 1.1 glass f_print(fout, "struct {\n");
423 1.1 glass f_print(fout, "\tu_int %s_len;\n", name);
424 1.1 glass f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
425 1.1 glass f_print(fout, "} %s", name);
426 1.1 glass break;
427 1.1 glass case REL_POINTER:
428 1.1 glass f_print(fout, "%s%s *%s", prefix, old, name);
429 1.1 glass break;
430 1.1 glass case REL_VECTOR:
431 1.1 glass f_print(fout, "%s%s %s[%s]", prefix, old, name,
432 1.7 lukem def->def.ty.array_max);
433 1.1 glass break;
434 1.1 glass case REL_ALIAS:
435 1.4 pk f_print(fout, "%s%s %s", prefix, old, name);
436 1.1 glass break;
437 1.1 glass }
438 1.1 glass f_print(fout, ";\n");
439 1.1 glass }
440 1.1 glass }
441 1.1 glass
442 1.6 christos void
443 1.21 dholland pdeclaration(const char *name, declaration *dec, int tab,
444 1.21 dholland const char *separator)
445 1.1 glass {
446 1.7 lukem char buf[8]; /* enough to hold "struct ", include NUL */
447 1.21 dholland const char *prefix;
448 1.21 dholland const char *type;
449 1.1 glass
450 1.1 glass if (streq(dec->type, "void")) {
451 1.1 glass return;
452 1.1 glass }
453 1.1 glass tabify(fout, tab);
454 1.1 glass if (streq(dec->type, name) && !dec->prefix) {
455 1.1 glass f_print(fout, "struct ");
456 1.1 glass }
457 1.1 glass if (streq(dec->type, "string")) {
458 1.1 glass f_print(fout, "char *%s", dec->name);
459 1.1 glass } else {
460 1.1 glass prefix = "";
461 1.1 glass if (streq(dec->type, "bool")) {
462 1.1 glass type = "bool_t";
463 1.7 lukem } else
464 1.7 lukem if (streq(dec->type, "opaque")) {
465 1.7 lukem type = "char";
466 1.7 lukem } else {
467 1.7 lukem if (dec->prefix) {
468 1.7 lukem s_print(buf, "%s ", dec->prefix);
469 1.7 lukem prefix = buf;
470 1.7 lukem }
471 1.7 lukem type = dec->type;
472 1.1 glass }
473 1.1 glass switch (dec->rel) {
474 1.1 glass case REL_ALIAS:
475 1.1 glass f_print(fout, "%s%s %s", prefix, type, dec->name);
476 1.1 glass break;
477 1.1 glass case REL_VECTOR:
478 1.1 glass f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
479 1.7 lukem dec->array_max);
480 1.1 glass break;
481 1.1 glass case REL_POINTER:
482 1.1 glass f_print(fout, "%s%s *%s", prefix, type, dec->name);
483 1.1 glass break;
484 1.1 glass case REL_ARRAY:
485 1.1 glass f_print(fout, "struct {\n");
486 1.1 glass tabify(fout, tab);
487 1.1 glass f_print(fout, "\tu_int %s_len;\n", dec->name);
488 1.1 glass tabify(fout, tab);
489 1.1 glass f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
490 1.1 glass tabify(fout, tab);
491 1.1 glass f_print(fout, "} %s", dec->name);
492 1.1 glass break;
493 1.1 glass }
494 1.1 glass }
495 1.14 is f_print(fout, "%s", separator);
496 1.1 glass }
497 1.1 glass
498 1.6 christos static int
499 1.21 dholland undefined2(const char *type, const char *stop)
500 1.1 glass {
501 1.7 lukem list *l;
502 1.1 glass definition *def;
503 1.1 glass
504 1.1 glass for (l = defined; l != NULL; l = l->next) {
505 1.1 glass def = (definition *) l->val;
506 1.1 glass if (def->def_kind != DEF_PROGRAM) {
507 1.1 glass if (streq(def->def_name, stop)) {
508 1.1 glass return (1);
509 1.7 lukem } else
510 1.7 lukem if (streq(def->def_name, type)) {
511 1.7 lukem return (0);
512 1.7 lukem }
513 1.1 glass }
514 1.1 glass }
515 1.1 glass return (1);
516 1.1 glass }
517