xref: /freebsd/usr.bin/rpcgen/rpc_hout.c (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
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(definition *def)
152 {
153 	pdefine(def->def_name, def->def.co);
154 }
155 
156 /* print out the definitions for the arguments of functions in the
157     header file
158 */
159 static void
160 pargdef(definition *def)
161 {
162 	decl_list *l;
163 	version_list *vers;
164 	char *name;
165 	proc_list *plist;
166 
167 
168 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
169 			for (plist = vers->procs; plist != NULL;
170 			    plist = plist->next) {
171 
172 				if (!newstyle || plist->arg_num < 2) {
173 					continue; /* old style or single args */
174 				}
175 				name = plist->args.argname;
176 				f_print(fout, "struct %s {\n", name);
177 				for (l = plist->args.decls;
178 				    l != NULL; l = l->next) {
179 					pdeclaration(name, &l->decl, 1,
180 						     ";\n");
181 				}
182 				f_print(fout, "};\n");
183 				f_print(fout, "typedef struct %s %s;\n",
184 					name, name);
185 				storexdrfuncdecl(name, 1);
186 				f_print(fout, "\n");
187 			}
188 		}
189 }
190 
191 
192 static void
193 pstructdef(definition *def)
194 {
195 	decl_list *l;
196 	const char *name = def->def_name;
197 
198 	f_print(fout, "struct %s {\n", name);
199 	for (l = def->def.st.decls; l != NULL; l = l->next) {
200 		pdeclaration(name, &l->decl, 1, ";\n");
201 	}
202 	f_print(fout, "};\n");
203 	f_print(fout, "typedef struct %s %s;\n", name, name);
204 }
205 
206 static void
207 puniondef(definition *def)
208 {
209 	case_list *l;
210 	const char *name = def->def_name;
211 	declaration *decl;
212 
213 	f_print(fout, "struct %s {\n", name);
214 	decl = &def->def.un.enum_decl;
215 	if (streq(decl->type, "bool")) {
216 		f_print(fout, "\tbool_t %s;\n", decl->name);
217 	} else {
218 		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
219 	}
220 	f_print(fout, "\tunion {\n");
221 	for (l = def->def.un.cases; l != NULL; l = l->next) {
222 	    if (l->contflag == 0)
223 		pdeclaration(name, &l->case_decl, 2, ";\n");
224 	}
225 	decl = def->def.un.default_decl;
226 	if (decl && !streq(decl->type, "void")) {
227 		pdeclaration(name, decl, 2, ";\n");
228 	}
229 	f_print(fout, "\t} %s_u;\n", name);
230 	f_print(fout, "};\n");
231 	f_print(fout, "typedef struct %s %s;\n", name, name);
232 }
233 
234 static void
235 pdefine(const char *name, const char *num)
236 {
237 	f_print(fout, "#define\t%s %s\n", name, num);
238 }
239 
240 static void
241 puldefine(const char *name, const char *num)
242 {
243 	f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
244 }
245 
246 static int
247 define_printed(proc_list *stop, version_list *start)
248 {
249 	version_list *vers;
250 	proc_list *proc;
251 
252 	for (vers = start; vers != NULL; vers = vers->next) {
253 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
254 			if (proc == stop) {
255 				return (0);
256 			} else if (streq(proc->proc_name, stop->proc_name)) {
257 				return (1);
258 			}
259 		}
260 	}
261 	abort();
262 	/* NOTREACHED */
263 }
264 
265 static void
266 pfreeprocdef(const char * name, const char *vers)
267 {
268 	f_print(fout, "extern int ");
269 	pvname(name, vers);
270 	f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
271 }
272 
273 static void
274 pdispatch(const char * name, const char *vers)
275 {
276 
277 	f_print(fout, "void ");
278 	pvname(name, vers);
279 	f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
280 }
281 
282 static void
283 pprogramdef(definition *def, int headeronly)
284 {
285 	version_list *vers;
286 	proc_list *proc;
287 	const char *ext;
288 
289 	pargdef(def);
290 
291 	puldefine(def->def_name, def->def.pr.prog_num);
292 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
293 		if (tblflag) {
294 			f_print(fout,
295 				"extern struct rpcgen_table %s_%s_table[];\n",
296 				locase(def->def_name), vers->vers_num);
297 			f_print(fout,
298 				"extern %s_%s_nproc;\n",
299 				locase(def->def_name), vers->vers_num);
300 		}
301 		puldefine(vers->vers_name, vers->vers_num);
302 
303 		f_print(fout, "\n");
304 		ext = "extern  ";
305 		if (headeronly) {
306 			f_print(fout, "%s", ext);
307 			pdispatch(def->def_name, vers->vers_num);
308 		}
309 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
310 			if (!define_printed(proc, def->def.pr.versions)) {
311 				puldefine(proc->proc_name, proc->proc_num);
312 			}
313 			f_print(fout, "%s", ext);
314 			pprocdef(proc, vers, "CLIENT *", 0);
315 			f_print(fout, "%s", ext);
316 			pprocdef(proc, vers, "struct svc_req *", 1);
317 		}
318 		pfreeprocdef(def->def_name, vers->vers_num);
319 	}
320 }
321 
322 static void
323 pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
324 {
325 	if (mtflag) {/* Print MT style stubs */
326 		if (server_p)
327 			f_print(fout, "bool_t ");
328 		else
329 			f_print(fout, "enum clnt_stat ");
330 	} else {
331 		ptype(proc->res_prefix, proc->res_type, 1);
332 		f_print(fout, "* ");
333 	}
334 	if (server_p)
335 		pvname_svc(proc->proc_name, vp->vers_num);
336 	else
337 		pvname(proc->proc_name, vp->vers_num);
338 
339 	parglist(proc, addargtype);
340 }
341 
342 
343 
344 /* print out argument list of procedure */
345 static void
346 parglist(proc_list *proc, const char *addargtype)
347 {
348 	decl_list *dl;
349 
350 	f_print(fout, "(");
351 	if (proc->arg_num < 2 && newstyle &&
352 	    streq(proc->args.decls->decl.type, "void")) {
353 		/* 0 argument in new style:  do nothing*/
354 	}
355 	else {
356 		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
357 			ptype(dl->decl.prefix, dl->decl.type, 1);
358 			if (!newstyle)
359 				f_print(fout, "*");
360 			/* old style passes by reference */
361 			f_print(fout, ", ");
362 		}
363 	}
364 
365 	if (mtflag)  {
366 		ptype(proc->res_prefix, proc->res_type, 1);
367 		f_print(fout, "*, ");
368 	}
369 
370 	f_print(fout, "%s);\n", addargtype);
371 
372 }
373 
374 static void
375 penumdef(definition *def)
376 {
377 	const char *name = def->def_name;
378 	enumval_list *l;
379 	const char *last = NULL;
380 	int count = 0;
381 
382 	f_print(fout, "enum %s {\n", name);
383 	for (l = def->def.en.vals; l != NULL; l = l->next) {
384 		f_print(fout, "\t%s", l->name);
385 		if (l->assignment) {
386 			f_print(fout, " = %s", l->assignment);
387 			last = l->assignment;
388 			count = 1;
389 		} else {
390 			if (last == NULL) {
391 				f_print(fout, " = %d", count++);
392 			} else {
393 				f_print(fout, " = %s + %d", last, count++);
394 			}
395 		}
396 		if (l->next)
397 			f_print(fout, ",\n");
398 		else
399 			f_print(fout, "\n");
400 	}
401 	f_print(fout, "};\n");
402 	f_print(fout, "typedef enum %s %s;\n", name, name);
403 }
404 
405 static void
406 ptypedef(definition *def)
407 {
408 	const char *name = def->def_name;
409 	const char *old = def->def.ty.old_type;
410 	char prefix[8];	/* enough to contain "struct ", including NUL */
411 	relation rel = def->def.ty.rel;
412 
413 
414 	if (!streq(name, old)) {
415 		if (streq(old, "string")) {
416 			old = "char";
417 			rel = REL_POINTER;
418 		} else if (streq(old, "opaque")) {
419 			old = "char";
420 		} else if (streq(old, "bool")) {
421 			old = "bool_t";
422 		}
423 		if (undefined2(old, name) && def->def.ty.old_prefix) {
424 			s_print(prefix, "%s ", def->def.ty.old_prefix);
425 		} else {
426 			prefix[0] = 0;
427 		}
428 		f_print(fout, "typedef ");
429 		switch (rel) {
430 		case REL_ARRAY:
431 			f_print(fout, "struct {\n");
432 			f_print(fout, "\tu_int %s_len;\n", name);
433 			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
434 			f_print(fout, "} %s", name);
435 			break;
436 		case REL_POINTER:
437 			f_print(fout, "%s%s *%s", prefix, old, name);
438 			break;
439 		case REL_VECTOR:
440 			f_print(fout, "%s%s %s[%s]", prefix, old, name,
441 				def->def.ty.array_max);
442 			break;
443 		case REL_ALIAS:
444 			f_print(fout, "%s%s %s", prefix, old, name);
445 			break;
446 		}
447 		f_print(fout, ";\n");
448 	}
449 }
450 
451 void
452 pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
453 {
454 	char buf[8];	/* enough to hold "struct ", include NUL */
455 	const char *prefix;
456 	const char *type;
457 
458 	if (streq(dec->type, "void")) {
459 		return;
460 	}
461 	tabify(fout, tab);
462 	if (streq(dec->type, name) && !dec->prefix) {
463 		f_print(fout, "struct ");
464 	}
465 	if (streq(dec->type, "string")) {
466 		f_print(fout, "char *%s", dec->name);
467 	} else {
468 		prefix = "";
469 		if (streq(dec->type, "bool")) {
470 			type = "bool_t";
471 		} else if (streq(dec->type, "opaque")) {
472 			type = "char";
473 		} else {
474 			if (dec->prefix) {
475 				s_print(buf, "%s ", dec->prefix);
476 				prefix = buf;
477 			}
478 			type = dec->type;
479 		}
480 		switch (dec->rel) {
481 		case REL_ALIAS:
482 			f_print(fout, "%s%s %s", prefix, type, dec->name);
483 			break;
484 		case REL_VECTOR:
485 			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
486 				dec->array_max);
487 			break;
488 		case REL_POINTER:
489 			f_print(fout, "%s%s *%s", prefix, type, dec->name);
490 			break;
491 		case REL_ARRAY:
492 			f_print(fout, "struct {\n");
493 			tabify(fout, tab);
494 			f_print(fout, "\tu_int %s_len;\n", dec->name);
495 			tabify(fout, tab);
496 			f_print(fout,
497 				"\t%s%s *%s_val;\n", prefix, type, dec->name);
498 			tabify(fout, tab);
499 			f_print(fout, "} %s", dec->name);
500 			break;
501 		}
502 	}
503 	fputs(separator, fout);
504 }
505 
506 static int
507 undefined2(const char *type, const char *stop)
508 {
509 	list *l;
510 	definition *def;
511 
512 	for (l = defined; l != NULL; l = l->next) {
513 		def = (definition *) l->val;
514 		if (def->def_kind != DEF_PROGRAM) {
515 			if (streq(def->def_name, stop)) {
516 				return (1);
517 			} else if (streq(def->def_name, type)) {
518 				return (0);
519 			}
520 		}
521 	}
522 	return (1);
523 }
524