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