rpc_hout.c revision 149695
1174195Srwatson/*
2174195Srwatson * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3174195Srwatson * unrestricted use provided that this legend is included on all tape
4174195Srwatson * media and as a part of the software program in whole or part.  Users
5174195Srwatson * may copy or modify Sun RPC without charge, but are not authorized
6174195Srwatson * to license or distribute it to anyone else except as part of a product or
7174195Srwatson * program developed by the user.
8174195Srwatson *
9174195Srwatson * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10174195Srwatson * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11174195Srwatson * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12174195Srwatson *
13174195Srwatson * Sun RPC is provided with no support and without any obligation on the
14174195Srwatson * part of Sun Microsystems, Inc. to assist in its use, correction,
15174195Srwatson * modification or enhancement.
16174195Srwatson *
17174195Srwatson * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18174195Srwatson * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19174195Srwatson * OR ANY PART THEREOF.
20174195Srwatson *
21174195Srwatson * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22174195Srwatson * or profits or other special, indirect and consequential damages, even if
23174195Srwatson * Sun has been advised of the possibility of such damages.
24174195Srwatson *
25174195Srwatson * Sun Microsystems, Inc.
26174195Srwatson * 2550 Garcia Avenue
27174195Srwatson * Mountain View, California  94043
28174195Srwatson */
29174195Srwatson
30174195Srwatson#if 0
31174195Srwatson#ifndef lint
32174195Srwatson#ident	"@(#)rpc_hout.c	1.16	94/04/25 SMI"
33174195Srwatsonstatic char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
34174195Srwatson#endif
35174195Srwatson#endif
36174195Srwatson
37174195Srwatson#include <sys/cdefs.h>
38174195Srwatson__FBSDID("$FreeBSD: head/usr.bin/rpcgen/rpc_hout.c 149695 2005-09-01 19:16:25Z stefanf $");
39174195Srwatson
40174195Srwatson/*
41174195Srwatson * rpc_hout.c, Header file outputter for the RPC protocol compiler
42174195Srwatson * Copyright (C) 1987, Sun Microsystems, Inc.
43286396Skib */
44174195Srwatson#include <stdio.h>
45174195Srwatson#include <ctype.h>
46174195Srwatson#include "rpc_parse.h"
47174195Srwatson#include "rpc_scan.h"
48174195Srwatson#include "rpc_util.h"
49174195Srwatson
50174195Srwatsonvoid storexdrfuncdecl( char *, int );
51174195Srwatsonstatic void pconstdef( definition * );
52174195Srwatsonstatic void pstructdef( definition * );
53174195Srwatsonstatic void puniondef( definition * );
54174195Srwatsonstatic void pprogramdef( definition *, int );
55174195Srwatsonstatic void pstructdef( definition * );
56174195Srwatsonstatic void penumdef( definition * );
57174195Srwatsonstatic void ptypedef( definition * );
58174195Srwatsonstatic void pdefine( char *, char * );
59286396Skibstatic int undefined2( char *, char * );
60286396Skibstatic void parglist( proc_list *, char * );
61174195Srwatsonstatic void pprocdef( proc_list *, version_list *, char *, int, int );
62174195Srwatsonvoid pdeclaration( char *, declaration *, int, char * );
63174195Srwatson
64174195Srwatson/*
65174195Srwatson * Print the C-version of an xdr definition
66174195Srwatson */
67174195Srwatsonvoid
68174195Srwatsonprint_datadef(definition *def, int headeronly)
69174195Srwatson{
70174195Srwatson
71174195Srwatson	if (def->def_kind == DEF_PROGRAM)  /* handle data only */
72174195Srwatson		return;
73174195Srwatson
74174195Srwatson	if (def->def_kind != DEF_CONST) {
75174195Srwatson		f_print(fout, "\n");
76174195Srwatson	}
77286396Skib	switch (def->def_kind) {
78174195Srwatson	case DEF_STRUCT:
79174195Srwatson		pstructdef(def);
80174195Srwatson		break;
81174195Srwatson	case DEF_UNION:
82174195Srwatson		puniondef(def);
83174195Srwatson		break;
84174195Srwatson	case DEF_ENUM:
85174195Srwatson		penumdef(def);
86286396Skib		break;
87174195Srwatson	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
106void
107print_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
122void
123storexdrfuncdecl(name, pointerp)
124char *name;
125int 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
146void
147print_xdr_func_def(name, pointerp, i)
148char* name;
149int pointerp;
150int i;
151{
152	if (i == 2) {
153		f_print(fout, "extern bool_t xdr_%s();\n", name);
154		return;
155	}
156	else
157		f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name,
158			name, pointerp ? "*" : "");
159
160
161}
162
163
164static void
165pconstdef(def)
166	definition *def;
167{
168	pdefine(def->def_name, def->def.co);
169}
170
171/* print out the definitions for the arguments of functions in the
172    header file
173*/
174static void
175pargdef(def)
176	definition *def;
177{
178	decl_list *l;
179	version_list *vers;
180	char *name;
181	proc_list *plist;
182
183
184	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
185			for (plist = vers->procs; plist != NULL;
186			    plist = plist->next) {
187
188				if (!newstyle || plist->arg_num < 2) {
189					continue; /* old style or single args */
190				}
191				name = plist->args.argname;
192				f_print(fout, "struct %s {\n", name);
193				for (l = plist->args.decls;
194				    l != NULL; l = l->next) {
195					pdeclaration(name, &l->decl, 1,
196						     ";\n");
197				}
198				f_print(fout, "};\n");
199				f_print(fout, "typedef struct %s %s;\n",
200					name, name);
201				storexdrfuncdecl(name, 1);
202				f_print(fout, "\n");
203			}
204		}
205}
206
207
208static void
209pstructdef(def)
210	definition *def;
211{
212	decl_list *l;
213	char *name = def->def_name;
214
215	f_print(fout, "struct %s {\n", name);
216	for (l = def->def.st.decls; l != NULL; l = l->next) {
217		pdeclaration(name, &l->decl, 1, ";\n");
218	}
219	f_print(fout, "};\n");
220	f_print(fout, "typedef struct %s %s;\n", name, name);
221}
222
223static void
224puniondef(def)
225	definition *def;
226{
227	case_list *l;
228	char *name = def->def_name;
229	declaration *decl;
230
231	f_print(fout, "struct %s {\n", name);
232	decl = &def->def.un.enum_decl;
233	if (streq(decl->type, "bool")) {
234		f_print(fout, "\tbool_t %s;\n", decl->name);
235	} else {
236		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
237	}
238	f_print(fout, "\tunion {\n");
239	for (l = def->def.un.cases; l != NULL; l = l->next) {
240	    if (l->contflag == 0)
241		pdeclaration(name, &l->case_decl, 2, ";\n");
242	}
243	decl = def->def.un.default_decl;
244	if (decl && !streq(decl->type, "void")) {
245		pdeclaration(name, decl, 2, ";\n");
246	}
247	f_print(fout, "\t} %s_u;\n", name);
248	f_print(fout, "};\n");
249	f_print(fout, "typedef struct %s %s;\n", name, name);
250}
251
252static void
253pdefine(name, num)
254	char *name;
255	char *num;
256{
257	f_print(fout, "#define\t%s %s\n", name, num);
258}
259
260static void
261puldefine(name, num)
262	char *name;
263	char *num;
264{
265	f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
266}
267
268static int
269define_printed(stop, start)
270	proc_list *stop;
271	version_list *start;
272{
273	version_list *vers;
274	proc_list *proc;
275
276	for (vers = start; vers != NULL; vers = vers->next) {
277		for (proc = vers->procs; proc != NULL; proc = proc->next) {
278			if (proc == stop) {
279				return (0);
280			} else if (streq(proc->proc_name, stop->proc_name)) {
281				return (1);
282			}
283		}
284	}
285	abort();
286	/* NOTREACHED */
287}
288
289static void
290pfreeprocdef(char * name, char *vers, int mode)
291{
292	f_print(fout, "extern int ");
293	pvname(name, vers);
294	if (mode == 1)
295		f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
296	else
297		f_print(fout,"_freeresult();\n");
298
299
300}
301
302static void
303pdispatch(char * name, char *vers, int mode)
304{
305
306	f_print(fout, "void ");
307	pvname(name, vers);
308	if (mode == 1)
309		f_print(fout,
310		    "(struct svc_req *rqstp, register SVCXPRT *transp);\n");
311	else
312		f_print(fout,"();\n");
313
314}
315
316static void
317pprogramdef(definition *def, int headeronly)
318{
319	version_list *vers;
320	proc_list *proc;
321	int i;
322	char *ext;
323
324	pargdef(def);
325
326	puldefine(def->def_name, def->def.pr.prog_num);
327	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
328		if (tblflag) {
329			f_print(fout,
330				"extern struct rpcgen_table %s_%s_table[];\n",
331				locase(def->def_name), vers->vers_num);
332			f_print(fout,
333				"extern %s_%s_nproc;\n",
334				locase(def->def_name), vers->vers_num);
335		}
336		puldefine(vers->vers_name, vers->vers_num);
337
338		/*
339		 * Print out 2 definitions, one for ANSI-C, another for
340		 * old K & R C
341		 */
342
343		if(!Cflag){
344			ext = "extern  ";
345			if (headeronly) {
346				f_print(fout, "%s", ext);
347				pdispatch(def->def_name, vers->vers_num, 2);
348			}
349			for (proc = vers->procs; proc != NULL;
350			     proc = proc->next) {
351				if (!define_printed(proc,
352						    def->def.pr.versions)) {
353					puldefine(proc->proc_name,
354						  proc->proc_num);
355				}
356				f_print(fout, "%s", ext);
357				pprocdef(proc, vers, NULL, 0, 2);
358
359				if (mtflag) {
360					f_print(fout, "%s", ext);
361					pprocdef(proc, vers, NULL, 1, 2);
362				}
363			}
364			pfreeprocdef(def->def_name, vers->vers_num, 2);
365
366		} else {
367			for (i = 1; i < 3; i++){
368				if (i == 1){
369					f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
370					ext = "extern  ";
371				}else{
372					f_print(fout, "\n#else /* K&R C */\n");
373					ext = "extern  ";
374				}
375
376				if (headeronly) {
377					f_print(fout, "%s", ext);
378					pdispatch(def->def_name, vers->vers_num,
379					    i);
380				}
381				for (proc = vers->procs; proc != NULL;
382				     proc = proc->next) {
383					if (!define_printed(proc,
384							    def->def.pr.versions)) {
385						puldefine(proc->proc_name,
386							  proc->proc_num);
387					}
388					f_print(fout, "%s", ext);
389					pprocdef(proc, vers, "CLIENT *", 0, i);
390					f_print(fout, "%s", ext);
391					pprocdef(proc, vers, "struct svc_req *", 1, i);
392				}
393			pfreeprocdef(def->def_name, vers->vers_num, i);
394			}
395			f_print(fout, "#endif /* K&R C */\n");
396		}
397	}
398}
399
400static void
401pprocdef(proc, vp, addargtype, server_p, mode)
402	proc_list *proc;
403	version_list *vp;
404	char* addargtype;
405	int server_p;
406	int mode;
407{
408	if (mtflag) {/* Print MT style stubs */
409		if (server_p)
410			f_print(fout, "bool_t ");
411		else
412			f_print(fout, "enum clnt_stat ");
413	} else {
414		ptype(proc->res_prefix, proc->res_type, 1);
415		f_print(fout, "* ");
416	}
417	if (server_p)
418		pvname_svc(proc->proc_name, vp->vers_num);
419	else
420		pvname(proc->proc_name, vp->vers_num);
421
422	/*
423	 *  mode  1 = ANSI-C, mode 2 = K&R C
424	 */
425	if ( mode == 1)
426		parglist(proc, addargtype);
427	else
428		f_print(fout, "();\n");
429}
430
431
432
433/* print out argument list of procedure */
434static void
435parglist(proc, addargtype)
436	proc_list *proc;
437    char* addargtype;
438{
439	decl_list *dl;
440
441	f_print(fout, "(");
442	if (proc->arg_num < 2 && newstyle &&
443	    streq(proc->args.decls->decl.type, "void")) {
444		/* 0 argument in new style:  do nothing*/
445	}
446	else {
447		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
448			ptype(dl->decl.prefix, dl->decl.type, 1);
449			if (!newstyle)
450				f_print(fout, "*");
451			/* old style passes by reference */
452			f_print(fout, ", ");
453		}
454	}
455
456	if (mtflag)  {
457		ptype(proc->res_prefix, proc->res_type, 1);
458		f_print(fout, "*, ");
459	}
460
461	f_print(fout, "%s);\n", addargtype);
462
463}
464
465static void
466penumdef(def)
467	definition *def;
468{
469	char *name = def->def_name;
470	enumval_list *l;
471	char *last = NULL;
472	int count = 0;
473
474	f_print(fout, "enum %s {\n", name);
475	for (l = def->def.en.vals; l != NULL; l = l->next) {
476		f_print(fout, "\t%s", l->name);
477		if (l->assignment) {
478			f_print(fout, " = %s", l->assignment);
479			last = l->assignment;
480			count = 1;
481		} else {
482			if (last == NULL) {
483				f_print(fout, " = %d", count++);
484			} else {
485				f_print(fout, " = %s + %d", last, count++);
486			}
487		}
488		if (l->next)
489			f_print(fout, ",\n");
490		else
491			f_print(fout, "\n");
492	}
493	f_print(fout, "};\n");
494	f_print(fout, "typedef enum %s %s;\n", name, name);
495}
496
497static void
498ptypedef(def)
499	definition *def;
500{
501	char *name = def->def_name;
502	char *old = def->def.ty.old_type;
503	char prefix[8];	/* enough to contain "struct ", including NUL */
504	relation rel = def->def.ty.rel;
505
506
507	if (!streq(name, old)) {
508		if (streq(old, "string")) {
509			old = "char";
510			rel = REL_POINTER;
511		} else if (streq(old, "opaque")) {
512			old = "char";
513		} else if (streq(old, "bool")) {
514			old = "bool_t";
515		}
516		if (undefined2(old, name) && def->def.ty.old_prefix) {
517			s_print(prefix, "%s ", def->def.ty.old_prefix);
518		} else {
519			prefix[0] = 0;
520		}
521		f_print(fout, "typedef ");
522		switch (rel) {
523		case REL_ARRAY:
524			f_print(fout, "struct {\n");
525			f_print(fout, "\tu_int %s_len;\n", name);
526			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
527			f_print(fout, "} %s", name);
528			break;
529		case REL_POINTER:
530			f_print(fout, "%s%s *%s", prefix, old, name);
531			break;
532		case REL_VECTOR:
533			f_print(fout, "%s%s %s[%s]", prefix, old, name,
534				def->def.ty.array_max);
535			break;
536		case REL_ALIAS:
537			f_print(fout, "%s%s %s", prefix, old, name);
538			break;
539		}
540		f_print(fout, ";\n");
541	}
542}
543
544void
545pdeclaration(name, dec, tab, separator)
546	char *name;
547	declaration *dec;
548	int tab;
549	char *separator;
550{
551	char buf[8];	/* enough to hold "struct ", include NUL */
552	char *prefix;
553	char *type;
554
555	if (streq(dec->type, "void")) {
556		return;
557	}
558	tabify(fout, tab);
559	if (streq(dec->type, name) && !dec->prefix) {
560		f_print(fout, "struct ");
561	}
562	if (streq(dec->type, "string")) {
563		f_print(fout, "char *%s", dec->name);
564	} else {
565		prefix = "";
566		if (streq(dec->type, "bool")) {
567			type = "bool_t";
568		} else if (streq(dec->type, "opaque")) {
569			type = "char";
570		} else {
571			if (dec->prefix) {
572				s_print(buf, "%s ", dec->prefix);
573				prefix = buf;
574			}
575			type = dec->type;
576		}
577		switch (dec->rel) {
578		case REL_ALIAS:
579			f_print(fout, "%s%s %s", prefix, type, dec->name);
580			break;
581		case REL_VECTOR:
582			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
583				dec->array_max);
584			break;
585		case REL_POINTER:
586			f_print(fout, "%s%s *%s", prefix, type, dec->name);
587			break;
588		case REL_ARRAY:
589			f_print(fout, "struct {\n");
590			tabify(fout, tab);
591			f_print(fout, "\tu_int %s_len;\n", dec->name);
592			tabify(fout, tab);
593			f_print(fout,
594				"\t%s%s *%s_val;\n", prefix, type, dec->name);
595			tabify(fout, tab);
596			f_print(fout, "} %s", dec->name);
597			break;
598		}
599	}
600	f_print(fout, separator);
601}
602
603static int
604undefined2(type, stop)
605	char *type;
606	char *stop;
607{
608	list *l;
609	definition *def;
610
611	for (l = defined; l != NULL; l = l->next) {
612		def = (definition *) l->val;
613		if (def->def_kind != DEF_PROGRAM) {
614			if (streq(def->def_name, stop)) {
615				return (1);
616			} else if (streq(def->def_name, type)) {
617				return (0);
618			}
619		}
620	}
621	return (1);
622}
623