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