rpc_hout.c revision 99980
1/* $FreeBSD: head/usr.bin/rpcgen/rpc_hout.c 99980 2002-07-14 17:55:35Z alfred $ */
2/*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part.  Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
9 *
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 *
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
17 *
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
21 *
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California  94043
29 */
30
31#ident	"@(#)rpc_hout.c	1.16	94/04/25 SMI"
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
36#endif
37#endif
38
39/*
40 * rpc_hout.c, Header file outputter for the RPC protocol compiler
41 * Copyright (C) 1987, Sun Microsystems, Inc.
42 */
43#include <stdio.h>
44#include <ctype.h>
45#include "rpc_parse.h"
46#include "rpc_util.h"
47
48void storexdrfuncdecl( char *, int );
49static void pconstdef( definition * );
50static void pstructdef( definition * );
51static void puniondef( definition * );
52static void pprogramdef( definition * );
53static void pstructdef( definition * );
54static void penumdef( definition * );
55static void ptypedef( definition * );
56static void pdefine( char *, char * );
57static int undefined2( char *, char * );
58static void parglist( proc_list *, char * );
59static void pprocdef( proc_list *, version_list *, char *, int, int );
60void pdeclaration( char *, declaration *, int, char * );
61
62/*
63 * Print the C-version of an xdr definition
64 */
65void
66print_datadef(def)
67	definition *def;
68{
69
70	if (def->def_kind == DEF_PROGRAM)  /* handle data only */
71		return;
72
73	if (def->def_kind != DEF_CONST) {
74		f_print(fout, "\n");
75	}
76	switch (def->def_kind) {
77	case DEF_STRUCT:
78		pstructdef(def);
79		break;
80	case DEF_UNION:
81		puniondef(def);
82		break;
83	case DEF_ENUM:
84		penumdef(def);
85		break;
86	case DEF_TYPEDEF:
87		ptypedef(def);
88		break;
89	case DEF_PROGRAM:
90		pprogramdef(def);
91		break;
92	case DEF_CONST:
93		pconstdef(def);
94		break;
95	}
96	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
97	    storexdrfuncdecl(def->def_name,
98			     def->def_kind != DEF_TYPEDEF ||
99			     !isvectordef(def->def.ty.old_type,
100					  def->def.ty.rel));
101	}
102}
103
104
105void
106print_funcdef(def)
107	definition *def;
108{
109	switch (def->def_kind) {
110	case DEF_PROGRAM:
111		f_print(fout, "\n");
112		pprogramdef(def);
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 = (xdrfunc *) malloc(sizeof (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(def)
318	definition *def;
319{
320	version_list *vers;
321	proc_list *proc;
322	int i;
323	char *ext;
324
325	pargdef(def);
326
327	puldefine(def->def_name, def->def.pr.prog_num);
328	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
329		if (tblflag) {
330			f_print(fout,
331				"extern struct rpcgen_table %s_%s_table[];\n",
332				locase(def->def_name), vers->vers_num);
333			f_print(fout,
334				"extern %s_%s_nproc;\n",
335				locase(def->def_name), vers->vers_num);
336		}
337		puldefine(vers->vers_name, vers->vers_num);
338
339		/*
340		 * Print out 2 definitions, one for ANSI-C, another for
341		 * old K & R C
342		 */
343
344		if(!Cflag){
345			ext = "extern  ";
346			f_print(fout, "%s", ext);
347			pdispatch(def->def_name, vers->vers_num, 2);
348			for (proc = vers->procs; proc != NULL;
349			     proc = proc->next) {
350				if (!define_printed(proc,
351						    def->def.pr.versions)) {
352					puldefine(proc->proc_name,
353						  proc->proc_num);
354				}
355				f_print(fout, "%s", ext);
356				pprocdef(proc, vers, NULL, 0, 2);
357
358				if (mtflag) {
359					f_print(fout, "%s", ext);
360					pprocdef(proc, vers, NULL, 1, 2);
361				}
362			}
363			pfreeprocdef(def->def_name, vers->vers_num, 2);
364
365		} else {
366			for (i = 1; i < 3; i++){
367				if (i == 1){
368					f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
369					ext = "extern  ";
370				}else{
371					f_print(fout, "\n#else /* K&R C */\n");
372					ext = "extern  ";
373				}
374
375				f_print(fout, "%s", ext);
376				pdispatch(def->def_name, vers->vers_num, i);
377				for (proc = vers->procs; proc != NULL;
378				     proc = proc->next) {
379					if (!define_printed(proc,
380							    def->def.pr.versions)) {
381						puldefine(proc->proc_name,
382							  proc->proc_num);
383					}
384					f_print(fout, "%s", ext);
385					pprocdef(proc, vers, "CLIENT *", 0, i);
386					f_print(fout, "%s", ext);
387					pprocdef(proc, vers, "struct svc_req *", 1, i);
388				}
389			pfreeprocdef(def->def_name, vers->vers_num, i);
390			}
391			f_print(fout, "#endif /* K&R C */\n");
392		}
393	}
394}
395
396static void
397pprocdef(proc, vp, addargtype, server_p, mode)
398	proc_list *proc;
399	version_list *vp;
400	char* addargtype;
401	int server_p;
402	int mode;
403{
404	if (mtflag) {/* Print MT style stubs */
405		if (server_p)
406			f_print(fout, "bool_t ");
407		else
408			f_print(fout, "enum clnt_stat ");
409	} else {
410		ptype(proc->res_prefix, proc->res_type, 1);
411		f_print(fout, "* ");
412	}
413	if (server_p)
414		pvname_svc(proc->proc_name, vp->vers_num);
415	else
416		pvname(proc->proc_name, vp->vers_num);
417
418	/*
419	 *  mode  1 = ANSI-C, mode 2 = K&R C
420	 */
421	if ( mode == 1)
422		parglist(proc, addargtype);
423	else
424		f_print(fout, "();\n");
425}
426
427
428
429/* print out argument list of procedure */
430static void
431parglist(proc, addargtype)
432	proc_list *proc;
433    char* addargtype;
434{
435	decl_list *dl;
436
437	f_print(fout, "(");
438	if (proc->arg_num < 2 && newstyle &&
439	    streq(proc->args.decls->decl.type, "void")) {
440		/* 0 argument in new style:  do nothing*/
441	}
442	else {
443		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
444			ptype(dl->decl.prefix, dl->decl.type, 1);
445			if (!newstyle)
446				f_print(fout, "*");
447			/* old style passes by reference */
448			f_print(fout, ", ");
449		}
450	}
451
452	if (mtflag)  {
453		ptype(proc->res_prefix, proc->res_type, 1);
454		f_print(fout, "*, ");
455	}
456
457	f_print(fout, "%s);\n", addargtype);
458
459}
460
461static void
462penumdef(def)
463	definition *def;
464{
465	char *name = def->def_name;
466	enumval_list *l;
467	char *last = NULL;
468	int count = 0;
469
470	f_print(fout, "enum %s {\n", name);
471	for (l = def->def.en.vals; l != NULL; l = l->next) {
472		f_print(fout, "\t%s", l->name);
473		if (l->assignment) {
474			f_print(fout, " = %s", l->assignment);
475			last = l->assignment;
476			count = 1;
477		} else {
478			if (last == NULL) {
479				f_print(fout, " = %d", count++);
480			} else {
481				f_print(fout, " = %s + %d", last, count++);
482			}
483		}
484		if (l->next)
485			f_print(fout, ",\n");
486		else
487			f_print(fout, "\n");
488	}
489	f_print(fout, "};\n");
490	f_print(fout, "typedef enum %s %s;\n", name, name);
491}
492
493static void
494ptypedef(def)
495	definition *def;
496{
497	char *name = def->def_name;
498	char *old = def->def.ty.old_type;
499	char prefix[8];	/* enough to contain "struct ", including NUL */
500	relation rel = def->def.ty.rel;
501
502
503	if (!streq(name, old)) {
504		if (streq(old, "string")) {
505			old = "char";
506			rel = REL_POINTER;
507		} else if (streq(old, "opaque")) {
508			old = "char";
509		} else if (streq(old, "bool")) {
510			old = "bool_t";
511		}
512		if (undefined2(old, name) && def->def.ty.old_prefix) {
513			s_print(prefix, "%s ", def->def.ty.old_prefix);
514		} else {
515			prefix[0] = 0;
516		}
517		f_print(fout, "typedef ");
518		switch (rel) {
519		case REL_ARRAY:
520			f_print(fout, "struct {\n");
521			f_print(fout, "\tu_int %s_len;\n", name);
522			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
523			f_print(fout, "} %s", name);
524			break;
525		case REL_POINTER:
526			f_print(fout, "%s%s *%s", prefix, old, name);
527			break;
528		case REL_VECTOR:
529			f_print(fout, "%s%s %s[%s]", prefix, old, name,
530				def->def.ty.array_max);
531			break;
532		case REL_ALIAS:
533			f_print(fout, "%s%s %s", prefix, old, name);
534			break;
535		}
536		f_print(fout, ";\n");
537	}
538}
539
540void
541pdeclaration(name, dec, tab, separator)
542	char *name;
543	declaration *dec;
544	int tab;
545	char *separator;
546{
547	char buf[8];	/* enough to hold "struct ", include NUL */
548	char *prefix;
549	char *type;
550
551	if (streq(dec->type, "void")) {
552		return;
553	}
554	tabify(fout, tab);
555	if (streq(dec->type, name) && !dec->prefix) {
556		f_print(fout, "struct ");
557	}
558	if (streq(dec->type, "string")) {
559		f_print(fout, "char *%s", dec->name);
560	} else {
561		prefix = "";
562		if (streq(dec->type, "bool")) {
563			type = "bool_t";
564		} else if (streq(dec->type, "opaque")) {
565			type = "char";
566		} else {
567			if (dec->prefix) {
568				s_print(buf, "%s ", dec->prefix);
569				prefix = buf;
570			}
571			type = dec->type;
572		}
573		switch (dec->rel) {
574		case REL_ALIAS:
575			f_print(fout, "%s%s %s", prefix, type, dec->name);
576			break;
577		case REL_VECTOR:
578			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
579				dec->array_max);
580			break;
581		case REL_POINTER:
582			f_print(fout, "%s%s *%s", prefix, type, dec->name);
583			break;
584		case REL_ARRAY:
585			f_print(fout, "struct {\n");
586			tabify(fout, tab);
587			f_print(fout, "\tu_int %s_len;\n", dec->name);
588			tabify(fout, tab);
589			f_print(fout,
590				"\t%s%s *%s_val;\n", prefix, type, dec->name);
591			tabify(fout, tab);
592			f_print(fout, "} %s", dec->name);
593			break;
594		}
595	}
596	f_print(fout, separator);
597}
598
599static int
600undefined2(type, stop)
601	char *type;
602	char *stop;
603{
604	list *l;
605	definition *def;
606
607	for (l = defined; l != NULL; l = l->next) {
608		def = (definition *) l->val;
609		if (def->def_kind != DEF_PROGRAM) {
610			if (streq(def->def_name, stop)) {
611				return (1);
612			} else if (streq(def->def_name, type)) {
613				return (0);
614			}
615		}
616	}
617	return (1);
618}
619