xref: /openbsd-src/usr.bin/rpcgen/rpc_hout.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: rpc_hout.c,v 1.20 2011/04/06 11:36:26 miod Exp $	*/
2 /*	$NetBSD: rpc_hout.c,v 1.4 1995/06/11 21:49:55 pk Exp $	*/
3 
4 /*
5  * Copyright (c) 2010, Oracle America, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials
16  *       provided with the distribution.
17  *     * Neither the name of the "Oracle America, Inc." nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * rpc_hout.c, Header file outputter for the RPC protocol compiler
37  */
38 #include <sys/cdefs.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <ctype.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 
45 static void pconstdef(definition *);
46 static void pargdef(definition *);
47 static void pstructdef(definition *);
48 static void puniondef(definition *);
49 static void pprogramdef(definition *);
50 static void penumdef(definition *);
51 static void ptypedef(definition *);
52 static void pdefine(char *, char *);
53 static void puldefine(char *, char *);
54 static int define_printed(proc_list *, version_list *);
55 static int undefined2(char *, char *);
56 static void parglist(proc_list *, char *);
57 void pxdrfuncdecl(char *, int);
58 void pprocdef(proc_list *, version_list *, char *, int, int);
59 void pdeclaration(char *, declaration *, int, char *);
60 
61 /*
62  * Print the C-version of an xdr definition
63  */
64 void
65 print_datadef(def)
66 	definition *def;
67 {
68 
69 	if (def->def_kind == DEF_PROGRAM)  /* handle data only */
70 		return;
71 
72 	if (def->def_kind != DEF_CONST)
73 		fprintf(fout, "\n");
74 	switch (def->def_kind) {
75 	case DEF_STRUCT:
76 		pstructdef(def);
77 		break;
78 	case DEF_UNION:
79 		puniondef(def);
80 		break;
81 	case DEF_ENUM:
82 		penumdef(def);
83 		break;
84 	case DEF_TYPEDEF:
85 		ptypedef(def);
86 		break;
87 	case DEF_PROGRAM:
88 		pprogramdef(def);
89 		break;
90 	case DEF_CONST:
91 		pconstdef(def);
92 		break;
93 	}
94 	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
95 		pxdrfuncdecl(def->def_name,
96 		    def->def_kind != DEF_TYPEDEF ||
97 		    !isvectordef(def->def.ty.old_type, def->def.ty.rel));
98 	}
99 }
100 
101 
102 void
103 print_funcdef(def)
104 	definition *def;
105 {
106 	switch (def->def_kind) {
107 	case DEF_PROGRAM:
108 		fprintf(fout, "\n");
109 		pprogramdef(def);
110 		break;
111 	}
112 }
113 
114 void
115 pxdrfuncdecl(name, pointerp)
116 	char *name;
117 	int pointerp;
118 {
119 
120 	fprintf(fout,"#ifdef __cplusplus\n");
121 	fprintf(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s %s);\n",
122 	    name, name, pointerp ? ("*") : "");
123 	fprintf(fout,"#elif defined(__STDC__)\n");
124 	fprintf(fout, "extern bool_t xdr_%s(XDR *, %s %s);\n",
125 	    name, name, pointerp ? ("*") : "");
126 	fprintf(fout,"#else /* Old Style C */\n");
127 	fprintf(fout, "bool_t xdr_%s();\n", name);
128 	fprintf(fout,"#endif /* Old Style C */\n\n");
129 }
130 
131 
132 static void
133 pconstdef(def)
134 	definition *def;
135 {
136 	pdefine(def->def_name, def->def.co);
137 }
138 
139 /*
140  * print out the definitions for the arguments of functions in the
141  * header file
142  */
143 static void
144 pargdef(def)
145 	definition *def;
146 {
147 	decl_list *l;
148 	version_list *vers;
149 	char *name;
150 	proc_list *plist;
151 
152 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
153 		for (plist = vers->procs; plist != NULL;
154 		    plist = plist->next) {
155 			if (!newstyle || plist->arg_num < 2) {
156 				continue; /* old style or single args */
157 			}
158 			name = plist->args.argname;
159 			fprintf(fout, "struct %s {\n", name);
160 			for (l = plist->args.decls;
161 			    l != NULL; l = l->next) {
162 				pdeclaration(name, &l->decl, 1, ";\n");
163 			}
164 			fprintf(fout, "};\n");
165 			fprintf(fout, "typedef struct %s %s;\n", name, name);
166 			pxdrfuncdecl(name, 0);
167 			fprintf(fout, "\n");
168 		}
169 	}
170 }
171 
172 static void
173 pstructdef(def)
174 	definition *def;
175 {
176 	char *name = def->def_name;
177 	decl_list *l;
178 
179 	fprintf(fout, "struct %s {\n", name);
180 	for (l = def->def.st.decls; l != NULL; l = l->next)
181 		pdeclaration(name, &l->decl, 1, ";\n");
182 	fprintf(fout, "};\n");
183 	fprintf(fout, "typedef struct %s %s;\n", name, name);
184 }
185 
186 static void
187 puniondef(def)
188 	definition *def;
189 {
190 	case_list *l;
191 	char *name = def->def_name;
192 	declaration *decl;
193 
194 	fprintf(fout, "struct %s {\n", name);
195 	decl = &def->def.un.enum_decl;
196 	if (streq(decl->type, "bool")) {
197 		fprintf(fout, "\tbool_t %s;\n", decl->name);
198 	} else {
199 		fprintf(fout, "\t%s %s;\n", decl->type, decl->name);
200 	}
201 	fprintf(fout, "\tunion {\n");
202 	for (l = def->def.un.cases; l != NULL; l = l->next) {
203 	  if (l->contflag == 0)
204 		pdeclaration(name, &l->case_decl, 2, ";\n");
205 	}
206 	decl = def->def.un.default_decl;
207 	if (decl && !streq(decl->type, "void")) {
208 		pdeclaration(name, decl, 2, ";\n");
209 	}
210 	fprintf(fout, "\t} %s_u;\n", name);
211 	fprintf(fout, "};\n");
212 	fprintf(fout, "typedef struct %s %s;\n", name, name);
213 }
214 
215 static void
216 pdefine(name, num)
217 	char *name;
218 	char *num;
219 {
220 	fprintf(fout, "#define %s %s\n", name, num);
221 }
222 
223 static void
224 puldefine(name, num)
225 	char *name;
226 	char *num;
227 {
228 	fprintf(fout, "#define %s ((u_long)%s)\n", name, num);
229 }
230 
231 static int
232 define_printed(stop, start)
233 	proc_list *stop;
234 	version_list *start;
235 {
236 	version_list *vers;
237 	proc_list *proc;
238 
239 	for (vers = start; vers != NULL; vers = vers->next) {
240 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
241 			if (proc == stop) {
242 				return (0);
243 			} else if (streq(proc->proc_name, stop->proc_name)) {
244 				return (1);
245 			}
246 		}
247 	}
248 	abort();
249 	/* NOTREACHED */
250 }
251 
252 static void
253 pprogramdef(def)
254 	definition *def;
255 {
256 	version_list *vers;
257 	proc_list *proc;
258 	int i;
259 	char *ext;
260 
261 	pargdef(def);
262 
263 	puldefine(def->def_name, def->def.pr.prog_num);
264 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
265 		if (tblflag) {
266 			fprintf(fout, "extern struct rpcgen_table %s_%s_table[];\n",
267 			    locase(def->def_name), vers->vers_num);
268 			fprintf(fout, "extern %s_%s_nproc;\n",
269 			    locase(def->def_name), vers->vers_num);
270 		}
271 		puldefine(vers->vers_name, vers->vers_num);
272 
273 		/*
274 		 * Print out 3 definitions, one for ANSI-C, another for C++,
275 		 * a third for old style C
276 		 */
277 		for (i=0; i<3; i++) {
278 			if (i==0) {
279 				fprintf(fout,"\n#ifdef __cplusplus\n");
280 				ext = "extern \"C\" ";
281 			} else if (i==1) {
282 				fprintf(fout,"\n#elif defined(__STDC__)\n");
283 				ext = "extern ";
284 			} else {
285 				fprintf(fout,"\n#else /* Old Style C */\n");
286 				ext = "extern ";
287 			}
288 
289 			for (proc = vers->procs; proc != NULL; proc = proc->next) {
290 				if (!define_printed(proc, def->def.pr.versions))
291 					puldefine(proc->proc_name, proc->proc_num);
292 				fprintf(fout,"%s",ext);
293 				pprocdef(proc, vers, "CLIENT *", 0,i);
294 				fprintf(fout,"%s",ext);
295 				pprocdef(proc, vers, "struct svc_req *", 1,i);
296 			}
297 		}
298 		fprintf(fout,"#endif /* Old Style C */\n");
299 	}
300 }
301 
302 void
303 pprocdef(proc, vp, addargtype, server_p,mode)
304 	proc_list *proc;
305 	version_list *vp;
306 	char *addargtype;
307 	int server_p;
308 	int mode;
309 {
310 
311 	ptype(proc->res_prefix, proc->res_type, 1);
312 	fprintf(fout, "* ");
313 	if (server_p)
314 		pvname_svc(proc->proc_name, vp->vers_num);
315 	else
316 		pvname(proc->proc_name, vp->vers_num);
317 
318 	/*
319 	 * mode  0 == cplusplus, mode  1 = ANSI-C, mode 2 = old style C
320 	 */
321 	if (mode == 0 || mode == 1)
322 		parglist(proc, addargtype);
323 	else
324 		fprintf(fout, "();\n");
325 }
326 
327 /* print out argument list of procedure */
328 static void
329 parglist(proc, addargtype)
330 	proc_list *proc;
331 	char *addargtype;
332 {
333 	decl_list *dl;
334 
335 	fprintf(fout,"(");
336 
337 	if (proc->arg_num < 2 && newstyle &&
338 	   streq(proc->args.decls->decl.type, "void")) {
339 		/* 0 argument in new style:  do nothing */
340 	} else {
341 		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
342 			ptype(dl->decl.prefix, dl->decl.type, 1);
343 			if (!newstyle)
344 				fprintf(fout, "*"); /* old style passes by reference */
345 			fprintf(fout, ", ");
346 		}
347 	}
348 	fprintf(fout, "%s);\n", addargtype);
349 }
350 
351 static void
352 penumdef(def)
353 	definition *def;
354 {
355 	char *name = def->def_name;
356 	enumval_list *l;
357 	char *last = NULL;
358 	int count = 0;
359 
360 	fprintf(fout, "enum %s {\n", name);
361 	for (l = def->def.en.vals; l != NULL; l = l->next) {
362 		fprintf(fout, "\t%s", l->name);
363 		if (l->assignment) {
364 			fprintf(fout, " = %s", l->assignment);
365 			last = l->assignment;
366 			count = 1;
367 		} else {
368 			if (last == NULL) {
369 				fprintf(fout, " = %d", count++);
370 			} else {
371 				fprintf(fout, " = %s + %d", last, count++);
372 			}
373 		}
374 		if (l->next)
375 			fprintf(fout, ",\n");
376 		else
377 			fprintf(fout, "\n");
378 	}
379 	fprintf(fout, "};\n");
380 	fprintf(fout, "typedef enum %s %s;\n", name, name);
381 }
382 
383 static void
384 ptypedef(def)
385 	definition *def;
386 {
387 	char *name = def->def_name;
388 	char *old = def->def.ty.old_type;
389 	char prefix[8];	/* enough to contain "struct ", including NUL */
390 	relation rel = def->def.ty.rel;
391 
392 	if (!streq(name, old)) {
393 		if (streq(old, "string")) {
394 			old = "char";
395 			rel = REL_POINTER;
396 		} else if (streq(old, "opaque")) {
397 			old = "char";
398 		} else if (streq(old, "bool")) {
399 			old = "bool_t";
400 		}
401 		if (undefined2(old, name) && def->def.ty.old_prefix) {
402 			snprintf(prefix, sizeof prefix, "%s ", def->def.ty.old_prefix);
403 		} else {
404 			prefix[0] = 0;
405 		}
406 		fprintf(fout, "typedef ");
407 		switch (rel) {
408 		case REL_ARRAY:
409 			fprintf(fout, "struct {\n");
410 			fprintf(fout, "\tu_int %s_len;\n", name);
411 			fprintf(fout, "\t%s%s *%s_val;\n", prefix, old, name);
412 			fprintf(fout, "} %s", name);
413 			break;
414 		case REL_POINTER:
415 			fprintf(fout, "%s%s *%s", prefix, old, name);
416 			break;
417 		case REL_VECTOR:
418 			fprintf(fout, "%s%s %s[%s]", prefix, old, name,
419 				def->def.ty.array_max);
420 			break;
421 		case REL_ALIAS:
422 			fprintf(fout, "%s%s %s", prefix, old, name);
423 			break;
424 		}
425 		fprintf(fout, ";\n");
426 	}
427 }
428 
429 void
430 pdeclaration(name, dec, tab, separator)
431 	char *name;
432 	declaration *dec;
433 	int tab;
434 	char *separator;
435 {
436 	char buf[8];	/* enough to hold "struct ", include NUL */
437 	char *prefix;
438 	char *type;
439 
440 	if (streq(dec->type, "void"))
441 		return;
442 	tabify(fout, tab);
443 	if (streq(dec->type, name) && !dec->prefix) {
444 		fprintf(fout, "struct ");
445 	}
446 	if (streq(dec->type, "string")) {
447 		fprintf(fout, "char *%s", dec->name);
448 	} else {
449 		prefix = "";
450 		if (streq(dec->type, "bool")) {
451 			type = "bool_t";
452 		} else if (streq(dec->type, "opaque")) {
453 			type = "char";
454 		} else {
455 			if (dec->prefix) {
456 				snprintf(buf, sizeof buf, "%s ", dec->prefix);
457 				prefix = buf;
458 			}
459 			type = dec->type;
460 		}
461 		switch (dec->rel) {
462 		case REL_ALIAS:
463 			fprintf(fout, "%s%s %s", prefix, type, dec->name);
464 			break;
465 		case REL_VECTOR:
466 			fprintf(fout, "%s%s %s[%s]", prefix, type, dec->name,
467 				dec->array_max);
468 			break;
469 		case REL_POINTER:
470 			fprintf(fout, "%s%s *%s", prefix, type, dec->name);
471 			break;
472 		case REL_ARRAY:
473 			fprintf(fout, "struct {\n");
474 			tabify(fout, tab);
475 			fprintf(fout, "\tu_int %s_len;\n", dec->name);
476 			tabify(fout, tab);
477 			fprintf(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
478 			tabify(fout, tab);
479 			fprintf(fout, "} %s", dec->name);
480 			break;
481 		}
482 	}
483 	fprintf(fout, "%s", separator);
484 }
485 
486 static int
487 undefined2(type, stop)
488 	char *type;
489 	char *stop;
490 {
491 	list *l;
492 	definition *def;
493 
494 	for (l = defined; l != NULL; l = l->next) {
495 		def = (definition *) l->val;
496 		if (def->def_kind != DEF_PROGRAM) {
497 			if (streq(def->def_name, stop)) {
498 				return (1);
499 			} else if (streq(def->def_name, type)) {
500 				return (0);
501 			}
502 		}
503 	}
504 	return (1);
505 }
506