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