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