xref: /freebsd/usr.bin/rpcgen/rpc_hout.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
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( 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 pstructdef( definition * );
56 static void penumdef( definition * );
57 static void ptypedef( definition * );
58 static void pdefine( char *, char * );
59 static int undefined2( char *, char * );
60 static void parglist( proc_list *, char * );
61 static void pprocdef( proc_list *, version_list *, char *, int );
62 void pdeclaration( char *, declaration *, int, char * );
63 
64 /*
65  * Print the C-version of an xdr definition
66  */
67 void
68 print_datadef(definition *def, int headeronly)
69 {
70 
71 	if (def->def_kind == DEF_PROGRAM)  /* handle data only */
72 		return;
73 
74 	if (def->def_kind != DEF_CONST) {
75 		f_print(fout, "\n");
76 	}
77 	switch (def->def_kind) {
78 	case DEF_STRUCT:
79 		pstructdef(def);
80 		break;
81 	case DEF_UNION:
82 		puniondef(def);
83 		break;
84 	case DEF_ENUM:
85 		penumdef(def);
86 		break;
87 	case DEF_TYPEDEF:
88 		ptypedef(def);
89 		break;
90 	case DEF_PROGRAM:
91 		pprogramdef(def, headeronly);
92 		break;
93 	case DEF_CONST:
94 		pconstdef(def);
95 		break;
96 	}
97 	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
98 	    storexdrfuncdecl(def->def_name,
99 			     def->def_kind != DEF_TYPEDEF ||
100 			     !isvectordef(def->def.ty.old_type,
101 					  def->def.ty.rel));
102 	}
103 }
104 
105 
106 void
107 print_funcdef(definition *def, int headeronly)
108 {
109 	switch (def->def_kind) {
110 	case DEF_PROGRAM:
111 		f_print(fout, "\n");
112 		pprogramdef(def, headeronly);
113 		break;
114 	default:
115 		break;
116 	}
117 }
118 
119 /* store away enough information to allow the XDR functions to be spat
120     out at the end of the file */
121 
122 void
123 storexdrfuncdecl(name, pointerp)
124 char *name;
125 int pointerp;
126 {
127 	xdrfunc * xdrptr;
128 
129 	xdrptr = XALLOC(struct xdrfunc);
130 
131 	xdrptr->name = name;
132 	xdrptr->pointerp = pointerp;
133 	xdrptr->next = NULL;
134 
135 	if (xdrfunc_tail == NULL){
136 		xdrfunc_head = xdrptr;
137 		xdrfunc_tail = xdrptr;
138 	} else {
139 		xdrfunc_tail->next = xdrptr;
140 		xdrfunc_tail = xdrptr;
141 	}
142 
143 
144 }
145 
146 void
147 print_xdr_func_def(char *name, int pointerp)
148 {
149 	f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name,
150 		name, pointerp ? "*" : "");
151 }
152 
153 
154 static void
155 pconstdef(def)
156 	definition *def;
157 {
158 	pdefine(def->def_name, def->def.co);
159 }
160 
161 /* print out the definitions for the arguments of functions in the
162     header file
163 */
164 static void
165 pargdef(def)
166 	definition *def;
167 {
168 	decl_list *l;
169 	version_list *vers;
170 	char *name;
171 	proc_list *plist;
172 
173 
174 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
175 			for (plist = vers->procs; plist != NULL;
176 			    plist = plist->next) {
177 
178 				if (!newstyle || plist->arg_num < 2) {
179 					continue; /* old style or single args */
180 				}
181 				name = plist->args.argname;
182 				f_print(fout, "struct %s {\n", name);
183 				for (l = plist->args.decls;
184 				    l != NULL; l = l->next) {
185 					pdeclaration(name, &l->decl, 1,
186 						     ";\n");
187 				}
188 				f_print(fout, "};\n");
189 				f_print(fout, "typedef struct %s %s;\n",
190 					name, name);
191 				storexdrfuncdecl(name, 1);
192 				f_print(fout, "\n");
193 			}
194 		}
195 }
196 
197 
198 static void
199 pstructdef(def)
200 	definition *def;
201 {
202 	decl_list *l;
203 	char *name = def->def_name;
204 
205 	f_print(fout, "struct %s {\n", name);
206 	for (l = def->def.st.decls; l != NULL; l = l->next) {
207 		pdeclaration(name, &l->decl, 1, ";\n");
208 	}
209 	f_print(fout, "};\n");
210 	f_print(fout, "typedef struct %s %s;\n", name, name);
211 }
212 
213 static void
214 puniondef(def)
215 	definition *def;
216 {
217 	case_list *l;
218 	char *name = def->def_name;
219 	declaration *decl;
220 
221 	f_print(fout, "struct %s {\n", name);
222 	decl = &def->def.un.enum_decl;
223 	if (streq(decl->type, "bool")) {
224 		f_print(fout, "\tbool_t %s;\n", decl->name);
225 	} else {
226 		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
227 	}
228 	f_print(fout, "\tunion {\n");
229 	for (l = def->def.un.cases; l != NULL; l = l->next) {
230 	    if (l->contflag == 0)
231 		pdeclaration(name, &l->case_decl, 2, ";\n");
232 	}
233 	decl = def->def.un.default_decl;
234 	if (decl && !streq(decl->type, "void")) {
235 		pdeclaration(name, decl, 2, ";\n");
236 	}
237 	f_print(fout, "\t} %s_u;\n", name);
238 	f_print(fout, "};\n");
239 	f_print(fout, "typedef struct %s %s;\n", name, name);
240 }
241 
242 static void
243 pdefine(name, num)
244 	char *name;
245 	char *num;
246 {
247 	f_print(fout, "#define\t%s %s\n", name, num);
248 }
249 
250 static void
251 puldefine(name, num)
252 	char *name;
253 	char *num;
254 {
255 	f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
256 }
257 
258 static int
259 define_printed(stop, start)
260 	proc_list *stop;
261 	version_list *start;
262 {
263 	version_list *vers;
264 	proc_list *proc;
265 
266 	for (vers = start; vers != NULL; vers = vers->next) {
267 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
268 			if (proc == stop) {
269 				return (0);
270 			} else if (streq(proc->proc_name, stop->proc_name)) {
271 				return (1);
272 			}
273 		}
274 	}
275 	abort();
276 	/* NOTREACHED */
277 }
278 
279 static void
280 pfreeprocdef(char * name, char *vers)
281 {
282 	f_print(fout, "extern int ");
283 	pvname(name, vers);
284 	f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
285 }
286 
287 static void
288 pdispatch(char * name, char *vers)
289 {
290 
291 	f_print(fout, "void ");
292 	pvname(name, vers);
293 	f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
294 }
295 
296 static void
297 pprogramdef(definition *def, int headeronly)
298 {
299 	version_list *vers;
300 	proc_list *proc;
301 	char *ext;
302 
303 	pargdef(def);
304 
305 	puldefine(def->def_name, def->def.pr.prog_num);
306 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
307 		if (tblflag) {
308 			f_print(fout,
309 				"extern struct rpcgen_table %s_%s_table[];\n",
310 				locase(def->def_name), vers->vers_num);
311 			f_print(fout,
312 				"extern %s_%s_nproc;\n",
313 				locase(def->def_name), vers->vers_num);
314 		}
315 		puldefine(vers->vers_name, vers->vers_num);
316 
317 		f_print(fout, "\n");
318 		ext = "extern  ";
319 		if (headeronly) {
320 			f_print(fout, "%s", ext);
321 			pdispatch(def->def_name, vers->vers_num);
322 		}
323 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
324 			if (!define_printed(proc, def->def.pr.versions)) {
325 				puldefine(proc->proc_name, proc->proc_num);
326 			}
327 			f_print(fout, "%s", ext);
328 			pprocdef(proc, vers, "CLIENT *", 0);
329 			f_print(fout, "%s", ext);
330 			pprocdef(proc, vers, "struct svc_req *", 1);
331 		}
332 		pfreeprocdef(def->def_name, vers->vers_num);
333 	}
334 }
335 
336 static void
337 pprocdef(proc_list *proc, version_list *vp, char *addargtype, int server_p)
338 {
339 	if (mtflag) {/* Print MT style stubs */
340 		if (server_p)
341 			f_print(fout, "bool_t ");
342 		else
343 			f_print(fout, "enum clnt_stat ");
344 	} else {
345 		ptype(proc->res_prefix, proc->res_type, 1);
346 		f_print(fout, "* ");
347 	}
348 	if (server_p)
349 		pvname_svc(proc->proc_name, vp->vers_num);
350 	else
351 		pvname(proc->proc_name, vp->vers_num);
352 
353 	parglist(proc, addargtype);
354 }
355 
356 
357 
358 /* print out argument list of procedure */
359 static void
360 parglist(proc, addargtype)
361 	proc_list *proc;
362     char* addargtype;
363 {
364 	decl_list *dl;
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 	}
371 	else {
372 		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
373 			ptype(dl->decl.prefix, dl->decl.type, 1);
374 			if (!newstyle)
375 				f_print(fout, "*");
376 			/* old style passes by reference */
377 			f_print(fout, ", ");
378 		}
379 	}
380 
381 	if (mtflag)  {
382 		ptype(proc->res_prefix, proc->res_type, 1);
383 		f_print(fout, "*, ");
384 	}
385 
386 	f_print(fout, "%s);\n", addargtype);
387 
388 }
389 
390 static void
391 penumdef(def)
392 	definition *def;
393 {
394 	char *name = def->def_name;
395 	enumval_list *l;
396 	char *last = NULL;
397 	int count = 0;
398 
399 	f_print(fout, "enum %s {\n", name);
400 	for (l = def->def.en.vals; l != NULL; l = l->next) {
401 		f_print(fout, "\t%s", l->name);
402 		if (l->assignment) {
403 			f_print(fout, " = %s", l->assignment);
404 			last = l->assignment;
405 			count = 1;
406 		} else {
407 			if (last == NULL) {
408 				f_print(fout, " = %d", count++);
409 			} else {
410 				f_print(fout, " = %s + %d", last, count++);
411 			}
412 		}
413 		if (l->next)
414 			f_print(fout, ",\n");
415 		else
416 			f_print(fout, "\n");
417 	}
418 	f_print(fout, "};\n");
419 	f_print(fout, "typedef enum %s %s;\n", name, name);
420 }
421 
422 static void
423 ptypedef(def)
424 	definition *def;
425 {
426 	char *name = def->def_name;
427 	char *old = def->def.ty.old_type;
428 	char prefix[8];	/* enough to contain "struct ", including NUL */
429 	relation rel = def->def.ty.rel;
430 
431 
432 	if (!streq(name, old)) {
433 		if (streq(old, "string")) {
434 			old = "char";
435 			rel = REL_POINTER;
436 		} else if (streq(old, "opaque")) {
437 			old = "char";
438 		} else if (streq(old, "bool")) {
439 			old = "bool_t";
440 		}
441 		if (undefined2(old, name) && def->def.ty.old_prefix) {
442 			s_print(prefix, "%s ", def->def.ty.old_prefix);
443 		} else {
444 			prefix[0] = 0;
445 		}
446 		f_print(fout, "typedef ");
447 		switch (rel) {
448 		case REL_ARRAY:
449 			f_print(fout, "struct {\n");
450 			f_print(fout, "\tu_int %s_len;\n", name);
451 			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
452 			f_print(fout, "} %s", name);
453 			break;
454 		case REL_POINTER:
455 			f_print(fout, "%s%s *%s", prefix, old, name);
456 			break;
457 		case REL_VECTOR:
458 			f_print(fout, "%s%s %s[%s]", prefix, old, name,
459 				def->def.ty.array_max);
460 			break;
461 		case REL_ALIAS:
462 			f_print(fout, "%s%s %s", prefix, old, name);
463 			break;
464 		}
465 		f_print(fout, ";\n");
466 	}
467 }
468 
469 void
470 pdeclaration(name, dec, tab, separator)
471 	char *name;
472 	declaration *dec;
473 	int tab;
474 	char *separator;
475 {
476 	char buf[8];	/* enough to hold "struct ", include NUL */
477 	char *prefix;
478 	char *type;
479 
480 	if (streq(dec->type, "void")) {
481 		return;
482 	}
483 	tabify(fout, tab);
484 	if (streq(dec->type, name) && !dec->prefix) {
485 		f_print(fout, "struct ");
486 	}
487 	if (streq(dec->type, "string")) {
488 		f_print(fout, "char *%s", dec->name);
489 	} else {
490 		prefix = "";
491 		if (streq(dec->type, "bool")) {
492 			type = "bool_t";
493 		} else if (streq(dec->type, "opaque")) {
494 			type = "char";
495 		} else {
496 			if (dec->prefix) {
497 				s_print(buf, "%s ", dec->prefix);
498 				prefix = buf;
499 			}
500 			type = dec->type;
501 		}
502 		switch (dec->rel) {
503 		case REL_ALIAS:
504 			f_print(fout, "%s%s %s", prefix, type, dec->name);
505 			break;
506 		case REL_VECTOR:
507 			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
508 				dec->array_max);
509 			break;
510 		case REL_POINTER:
511 			f_print(fout, "%s%s *%s", prefix, type, dec->name);
512 			break;
513 		case REL_ARRAY:
514 			f_print(fout, "struct {\n");
515 			tabify(fout, tab);
516 			f_print(fout, "\tu_int %s_len;\n", dec->name);
517 			tabify(fout, tab);
518 			f_print(fout,
519 				"\t%s%s *%s_val;\n", prefix, type, dec->name);
520 			tabify(fout, tab);
521 			f_print(fout, "} %s", dec->name);
522 			break;
523 		}
524 	}
525 	f_print(fout, separator);
526 }
527 
528 static int
529 undefined2(type, stop)
530 	char *type;
531 	char *stop;
532 {
533 	list *l;
534 	definition *def;
535 
536 	for (l = defined; l != NULL; l = l->next) {
537 		def = (definition *) l->val;
538 		if (def->def_kind != DEF_PROGRAM) {
539 			if (streq(def->def_name, stop)) {
540 				return (1);
541 			} else if (streq(def->def_name, type)) {
542 				return (0);
543 			}
544 		}
545 	}
546 	return (1);
547 }
548