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