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