xref: /freebsd/usr.bin/rpcgen/rpc_hout.c (revision 7e440a74e8343065aa3aca4e24b37ff233cb00c2)
17e440a74SAlfred Perlstein /* $FreeBSD$ */
24e115012SGarrett Wollman /*
34e115012SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
44e115012SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
54e115012SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
64e115012SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
74e115012SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
84e115012SGarrett Wollman  * program developed by the user.
94e115012SGarrett Wollman  *
104e115012SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
114e115012SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
124e115012SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
134e115012SGarrett Wollman  *
144e115012SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
154e115012SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
164e115012SGarrett Wollman  * modification or enhancement.
174e115012SGarrett Wollman  *
184e115012SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
194e115012SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
204e115012SGarrett Wollman  * OR ANY PART THEREOF.
214e115012SGarrett Wollman  *
224e115012SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
234e115012SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
244e115012SGarrett Wollman  * Sun has been advised of the possibility of such damages.
254e115012SGarrett Wollman  *
264e115012SGarrett Wollman  * Sun Microsystems, Inc.
274e115012SGarrett Wollman  * 2550 Garcia Avenue
284e115012SGarrett Wollman  * Mountain View, California  94043
294e115012SGarrett Wollman  */
304e115012SGarrett Wollman 
31ff49530fSBill Paul #ident	"@(#)rpc_hout.c	1.16	94/04/25 SMI"
32ff49530fSBill Paul 
33ff49530fSBill Paul #ifndef lint
34ff49530fSBill Paul static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
35ff49530fSBill Paul #endif
364e115012SGarrett Wollman 
374e115012SGarrett Wollman /*
384e115012SGarrett Wollman  * rpc_hout.c, Header file outputter for the RPC protocol compiler
394e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
404e115012SGarrett Wollman  */
414e115012SGarrett Wollman #include <stdio.h>
424e115012SGarrett Wollman #include <ctype.h>
434e115012SGarrett Wollman #include "rpc_parse.h"
44ff49530fSBill Paul #include "rpc_util.h"
45ff49530fSBill Paul 
46ff49530fSBill Paul void storexdrfuncdecl __P(( char *, int ));
47526195adSJordan K. Hubbard static void pconstdef __P(( definition * ));
48526195adSJordan K. Hubbard static void pstructdef __P(( definition * ));
49526195adSJordan K. Hubbard static void puniondef __P(( definition * ));
50526195adSJordan K. Hubbard static void pprogramdef __P(( definition * ));
51526195adSJordan K. Hubbard static void pstructdef __P(( definition * ));
52526195adSJordan K. Hubbard static void penumdef __P(( definition * ));
53526195adSJordan K. Hubbard static void ptypedef __P(( definition * ));
54526195adSJordan K. Hubbard static void pdefine __P(( char *, char * ));
55ff49530fSBill Paul static int undefined2 __P(( char *, char * ));
56526195adSJordan K. Hubbard static void parglist __P(( proc_list *, char * ));
57526195adSJordan K. Hubbard static void pprocdef __P(( proc_list *, version_list *, char *, int, int ));
58526195adSJordan K. Hubbard void pdeclaration __P(( char *, declaration *, int, char * ));
59ff49530fSBill Paul 
60ff49530fSBill Paul static char RESULT[] = "clnt_res";
614e115012SGarrett Wollman 
624e115012SGarrett Wollman 
634e115012SGarrett Wollman /*
644e115012SGarrett Wollman  * Print the C-version of an xdr definition
654e115012SGarrett Wollman  */
664e115012SGarrett Wollman void
674e115012SGarrett Wollman print_datadef(def)
684e115012SGarrett Wollman 	definition *def;
694e115012SGarrett Wollman {
70ff49530fSBill Paul 
71ff49530fSBill Paul 	if (def->def_kind == DEF_PROGRAM)  /* handle data only */
72ff49530fSBill Paul 		return;
73ff49530fSBill Paul 
744e115012SGarrett Wollman 	if (def->def_kind != DEF_CONST) {
754e115012SGarrett Wollman 		f_print(fout, "\n");
764e115012SGarrett Wollman 	}
774e115012SGarrett Wollman 	switch (def->def_kind) {
784e115012SGarrett Wollman 	case DEF_STRUCT:
794e115012SGarrett Wollman 		pstructdef(def);
804e115012SGarrett Wollman 		break;
814e115012SGarrett Wollman 	case DEF_UNION:
824e115012SGarrett Wollman 		puniondef(def);
834e115012SGarrett Wollman 		break;
844e115012SGarrett Wollman 	case DEF_ENUM:
854e115012SGarrett Wollman 		penumdef(def);
864e115012SGarrett Wollman 		break;
874e115012SGarrett Wollman 	case DEF_TYPEDEF:
884e115012SGarrett Wollman 		ptypedef(def);
894e115012SGarrett Wollman 		break;
904e115012SGarrett Wollman 	case DEF_PROGRAM:
914e115012SGarrett Wollman 		pprogramdef(def);
924e115012SGarrett Wollman 		break;
934e115012SGarrett Wollman 	case DEF_CONST:
944e115012SGarrett Wollman 		pconstdef(def);
954e115012SGarrett Wollman 		break;
964e115012SGarrett Wollman 	}
974e115012SGarrett Wollman 	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
98ff49530fSBill Paul 	    storexdrfuncdecl(def->def_name,
99ff49530fSBill Paul 			     def->def_kind != DEF_TYPEDEF ||
100ff49530fSBill Paul 			     !isvectordef(def->def.ty.old_type,
101ff49530fSBill Paul 					  def->def.ty.rel));
1024e115012SGarrett Wollman 	}
103ff49530fSBill Paul }
104ff49530fSBill Paul 
105ff49530fSBill Paul 
106ff49530fSBill Paul void
107ff49530fSBill Paul print_funcdef(def)
108ff49530fSBill Paul 	definition *def;
109ff49530fSBill Paul {
110ff49530fSBill Paul 	switch (def->def_kind) {
111ff49530fSBill Paul 	case DEF_PROGRAM:
1124e115012SGarrett Wollman 		f_print(fout, "\n");
113ff49530fSBill Paul 		pprogramdef(def);
114ff49530fSBill Paul 		break;
115526195adSJordan K. Hubbard 	default:
1164e115012SGarrett Wollman 	}
1174e115012SGarrett Wollman }
1184e115012SGarrett Wollman 
119ff49530fSBill Paul /* store away enough information to allow the XDR functions to be spat
120ff49530fSBill Paul     out at the end of the file */
121ff49530fSBill Paul 
122ff49530fSBill Paul void
123ff49530fSBill Paul storexdrfuncdecl(name, pointerp)
124ff49530fSBill Paul char *name;
125ff49530fSBill Paul int pointerp;
126ff49530fSBill Paul {
127ff49530fSBill Paul 	xdrfunc * xdrptr;
128ff49530fSBill Paul 
129ff49530fSBill Paul 	xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
130ff49530fSBill Paul 
131ff49530fSBill Paul 	xdrptr->name = name;
132ff49530fSBill Paul 	xdrptr->pointerp = pointerp;
133ff49530fSBill Paul 	xdrptr->next = NULL;
134ff49530fSBill Paul 
135ff49530fSBill Paul 	if (xdrfunc_tail == NULL){
136ff49530fSBill Paul 		xdrfunc_head = xdrptr;
137ff49530fSBill Paul 		xdrfunc_tail = xdrptr;
138ff49530fSBill Paul 	} else {
139ff49530fSBill Paul 		xdrfunc_tail->next = xdrptr;
140ff49530fSBill Paul 		xdrfunc_tail = xdrptr;
141ff49530fSBill Paul 	}
142ff49530fSBill Paul 
143ff49530fSBill Paul 
144ff49530fSBill Paul }
145ff49530fSBill Paul 
146ff49530fSBill Paul void
147ff49530fSBill Paul print_xdr_func_def(name, pointerp, i)
148ff49530fSBill Paul char* name;
149ff49530fSBill Paul int pointerp;
150ff49530fSBill Paul int i;
151ff49530fSBill Paul {
152ff49530fSBill Paul 	if (i == 2) {
153ff49530fSBill Paul 		f_print(fout, "extern bool_t xdr_%s();\n", name);
154ff49530fSBill Paul 		return;
155ff49530fSBill Paul 	}
156ff49530fSBill Paul 	else
157ff49530fSBill Paul 		f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name,
158ff49530fSBill Paul 			name, pointerp ? "*" : "");
159ff49530fSBill Paul 
160ff49530fSBill Paul 
161ff49530fSBill Paul }
162ff49530fSBill Paul 
163ff49530fSBill Paul 
164526195adSJordan K. Hubbard static void
1654e115012SGarrett Wollman pconstdef(def)
1664e115012SGarrett Wollman 	definition *def;
1674e115012SGarrett Wollman {
1684e115012SGarrett Wollman 	pdefine(def->def_name, def->def.co);
1694e115012SGarrett Wollman }
1704e115012SGarrett Wollman 
171ff49530fSBill Paul /* print out the definitions for the arguments of functions in the
172ff49530fSBill Paul     header file
173ff49530fSBill Paul */
174526195adSJordan K. Hubbard static void
175ff49530fSBill Paul pargdef(def)
176ff49530fSBill Paul 	definition *def;
177ff49530fSBill Paul {
178ff49530fSBill Paul 	decl_list *l;
179ff49530fSBill Paul 	version_list *vers;
180ff49530fSBill Paul 	char *name;
181ff49530fSBill Paul 	proc_list *plist;
182ff49530fSBill Paul 
183ff49530fSBill Paul 
184ff49530fSBill Paul 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
185ff49530fSBill Paul 			for (plist = vers->procs; plist != NULL;
186ff49530fSBill Paul 			    plist = plist->next) {
187ff49530fSBill Paul 
188ff49530fSBill Paul 				if (!newstyle || plist->arg_num < 2) {
189ff49530fSBill Paul 					continue; /* old style or single args */
190ff49530fSBill Paul 				}
191ff49530fSBill Paul 				name = plist->args.argname;
192ff49530fSBill Paul 				f_print(fout, "struct %s {\n", name);
193ff49530fSBill Paul 				for (l = plist->args.decls;
194ff49530fSBill Paul 				    l != NULL; l = l->next) {
195ff49530fSBill Paul 					pdeclaration(name, &l->decl, 1,
196ff49530fSBill Paul 						     ";\n");
197ff49530fSBill Paul 				}
198ff49530fSBill Paul 				f_print(fout, "};\n");
199ff49530fSBill Paul 				f_print(fout, "typedef struct %s %s;\n",
200ff49530fSBill Paul 					name, name);
201ff49530fSBill Paul 				storexdrfuncdecl(name, 1);
202ff49530fSBill Paul 				f_print(fout, "\n");
203ff49530fSBill Paul 			}
204ff49530fSBill Paul 		}
205ff49530fSBill Paul }
206ff49530fSBill Paul 
207ff49530fSBill Paul 
208526195adSJordan K. Hubbard static void
2094e115012SGarrett Wollman pstructdef(def)
2104e115012SGarrett Wollman 	definition *def;
2114e115012SGarrett Wollman {
2124e115012SGarrett Wollman 	decl_list *l;
2134e115012SGarrett Wollman 	char *name = def->def_name;
2144e115012SGarrett Wollman 
2154e115012SGarrett Wollman 	f_print(fout, "struct %s {\n", name);
2164e115012SGarrett Wollman 	for (l = def->def.st.decls; l != NULL; l = l->next) {
217ff49530fSBill Paul 		pdeclaration(name, &l->decl, 1, ";\n");
2184e115012SGarrett Wollman 	}
2194e115012SGarrett Wollman 	f_print(fout, "};\n");
2204e115012SGarrett Wollman 	f_print(fout, "typedef struct %s %s;\n", name, name);
2214e115012SGarrett Wollman }
2224e115012SGarrett Wollman 
223526195adSJordan K. Hubbard static void
2244e115012SGarrett Wollman puniondef(def)
2254e115012SGarrett Wollman 	definition *def;
2264e115012SGarrett Wollman {
2274e115012SGarrett Wollman 	case_list *l;
2284e115012SGarrett Wollman 	char *name = def->def_name;
2294e115012SGarrett Wollman 	declaration *decl;
2304e115012SGarrett Wollman 
2314e115012SGarrett Wollman 	f_print(fout, "struct %s {\n", name);
2324e115012SGarrett Wollman 	decl = &def->def.un.enum_decl;
2334e115012SGarrett Wollman 	if (streq(decl->type, "bool")) {
2344e115012SGarrett Wollman 		f_print(fout, "\tbool_t %s;\n", decl->name);
2354e115012SGarrett Wollman 	} else {
2364e115012SGarrett Wollman 		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
2374e115012SGarrett Wollman 	}
2384e115012SGarrett Wollman 	f_print(fout, "\tunion {\n");
2394e115012SGarrett Wollman 	for (l = def->def.un.cases; l != NULL; l = l->next) {
240ff49530fSBill Paul 	    if (l->contflag == 0)
241ff49530fSBill Paul 		pdeclaration(name, &l->case_decl, 2, ";\n");
2424e115012SGarrett Wollman 	}
2434e115012SGarrett Wollman 	decl = def->def.un.default_decl;
2444e115012SGarrett Wollman 	if (decl && !streq(decl->type, "void")) {
245ff49530fSBill Paul 		pdeclaration(name, decl, 2, ";\n");
2464e115012SGarrett Wollman 	}
2474e115012SGarrett Wollman 	f_print(fout, "\t} %s_u;\n", name);
2484e115012SGarrett Wollman 	f_print(fout, "};\n");
2494e115012SGarrett Wollman 	f_print(fout, "typedef struct %s %s;\n", name, name);
2504e115012SGarrett Wollman }
2514e115012SGarrett Wollman 
252526195adSJordan K. Hubbard static void
2534e115012SGarrett Wollman pdefine(name, num)
2544e115012SGarrett Wollman 	char *name;
2554e115012SGarrett Wollman 	char *num;
2564e115012SGarrett Wollman {
257ff49530fSBill Paul 	f_print(fout, "#define\t%s %s\n", name, num);
2584e115012SGarrett Wollman }
2594e115012SGarrett Wollman 
260526195adSJordan K. Hubbard static void
2614e115012SGarrett Wollman puldefine(name, num)
2624e115012SGarrett Wollman 	char *name;
2634e115012SGarrett Wollman 	char *num;
2644e115012SGarrett Wollman {
265ff49530fSBill Paul 	f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
2664e115012SGarrett Wollman }
2674e115012SGarrett Wollman 
268526195adSJordan K. Hubbard static int
2694e115012SGarrett Wollman define_printed(stop, start)
2704e115012SGarrett Wollman 	proc_list *stop;
2714e115012SGarrett Wollman 	version_list *start;
2724e115012SGarrett Wollman {
2734e115012SGarrett Wollman 	version_list *vers;
2744e115012SGarrett Wollman 	proc_list *proc;
2754e115012SGarrett Wollman 
2764e115012SGarrett Wollman 	for (vers = start; vers != NULL; vers = vers->next) {
2774e115012SGarrett Wollman 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
2784e115012SGarrett Wollman 			if (proc == stop) {
2794e115012SGarrett Wollman 				return (0);
2804e115012SGarrett Wollman 			} else if (streq(proc->proc_name, stop->proc_name)) {
2814e115012SGarrett Wollman 				return (1);
2824e115012SGarrett Wollman 			}
2834e115012SGarrett Wollman 		}
2844e115012SGarrett Wollman 	}
2854e115012SGarrett Wollman 	abort();
2864e115012SGarrett Wollman 	/* NOTREACHED */
2874e115012SGarrett Wollman }
2884e115012SGarrett Wollman 
289526195adSJordan K. Hubbard static void
290ff49530fSBill Paul pfreeprocdef(char * name, char *vers, int mode)
291ff49530fSBill Paul {
292ff49530fSBill Paul 	f_print(fout, "extern int ");
293ff49530fSBill Paul 	pvname(name, vers);
294ff49530fSBill Paul 	if (mode == 1)
295ff49530fSBill Paul 		f_print(fout,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
296ff49530fSBill Paul 	else
297ff49530fSBill Paul 		f_print(fout,"_freeresult();\n");
298ff49530fSBill Paul 
299ff49530fSBill Paul 
300ff49530fSBill Paul }
3014e115012SGarrett Wollman 
302526195adSJordan K. Hubbard static void
3037e440a74SAlfred Perlstein pdispatch(char * name, char *vers, int mode)
3047e440a74SAlfred Perlstein {
3057e440a74SAlfred Perlstein 
3067e440a74SAlfred Perlstein 	f_print(fout, "void ");
3077e440a74SAlfred Perlstein 	pvname(name, vers);
3087e440a74SAlfred Perlstein 	if (mode == 1)
3097e440a74SAlfred Perlstein 		f_print(fout,
3107e440a74SAlfred Perlstein 		    "(struct svc_req *rqstp, register SVCXPRT *transp);\n");
3117e440a74SAlfred Perlstein 	else
3127e440a74SAlfred Perlstein 		f_print(fout,"();\n");
3137e440a74SAlfred Perlstein 
3147e440a74SAlfred Perlstein }
3157e440a74SAlfred Perlstein 
3167e440a74SAlfred Perlstein static void
3174e115012SGarrett Wollman pprogramdef(def)
3184e115012SGarrett Wollman 	definition *def;
3194e115012SGarrett Wollman {
3204e115012SGarrett Wollman 	version_list *vers;
3214e115012SGarrett Wollman 	proc_list *proc;
322ff49530fSBill Paul 	int i;
323ff49530fSBill Paul 	char *ext;
324ff49530fSBill Paul 
325ff49530fSBill Paul 	pargdef(def);
3264e115012SGarrett Wollman 
3274e115012SGarrett Wollman 	puldefine(def->def_name, def->def.pr.prog_num);
3284e115012SGarrett Wollman 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
329ff49530fSBill Paul 		if (tblflag) {
330ff49530fSBill Paul 			f_print(fout,
331ff49530fSBill Paul 				"extern struct rpcgen_table %s_%s_table[];\n",
332ff49530fSBill Paul 				locase(def->def_name), vers->vers_num);
333ff49530fSBill Paul 			f_print(fout,
334ff49530fSBill Paul 				"extern %s_%s_nproc;\n",
335ff49530fSBill Paul 				locase(def->def_name), vers->vers_num);
336ff49530fSBill Paul 		}
3374e115012SGarrett Wollman 		puldefine(vers->vers_name, vers->vers_num);
338ff49530fSBill Paul 
339ff49530fSBill Paul 		/*
340ff49530fSBill Paul 		 * Print out 2 definitions, one for ANSI-C, another for
341ff49530fSBill Paul 		 * old K & R C
342ff49530fSBill Paul 		 */
343ff49530fSBill Paul 
344ff49530fSBill Paul 		if(!Cflag){
345ff49530fSBill Paul 			ext = "extern  ";
3467e440a74SAlfred Perlstein 			f_print(fout, "%s", ext);
3477e440a74SAlfred Perlstein 			pdispatch(def->def_name, vers->vers_num, 2);
348ff49530fSBill Paul 			for (proc = vers->procs; proc != NULL;
349ff49530fSBill Paul 			     proc = proc->next) {
350ff49530fSBill Paul 				if (!define_printed(proc,
351ff49530fSBill Paul 						    def->def.pr.versions)) {
352ff49530fSBill Paul 					puldefine(proc->proc_name,
353ff49530fSBill Paul 						  proc->proc_num);
3544e115012SGarrett Wollman 				}
355ff49530fSBill Paul 				f_print(fout, "%s", ext);
356ff49530fSBill Paul 				pprocdef(proc, vers, NULL, 0, 2);
357ff49530fSBill Paul 
358ff49530fSBill Paul 				if (mtflag) {
359ff49530fSBill Paul 					f_print(fout, "%s", ext);
360ff49530fSBill Paul 					pprocdef(proc, vers, NULL, 1, 2);
361ff49530fSBill Paul 				}
362ff49530fSBill Paul 			}
363ff49530fSBill Paul 			pfreeprocdef(def->def_name, vers->vers_num, 2);
364ff49530fSBill Paul 
365ff49530fSBill Paul 		} else {
366ff49530fSBill Paul 			for (i = 1; i < 3; i++){
367ff49530fSBill Paul 				if (i == 1){
368ff49530fSBill Paul 					f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
369ff49530fSBill Paul 					ext = "extern  ";
370ff49530fSBill Paul 				}else{
371ff49530fSBill Paul 					f_print(fout, "\n#else /* K&R C */\n");
372ff49530fSBill Paul 					ext = "extern  ";
373ff49530fSBill Paul 				}
374ff49530fSBill Paul 
3757e440a74SAlfred Perlstein 				f_print(fout, "%s", ext);
3767e440a74SAlfred Perlstein 				pdispatch(def->def_name, vers->vers_num, i);
377ff49530fSBill Paul 				for (proc = vers->procs; proc != NULL;
378ff49530fSBill Paul 				     proc = proc->next) {
379ff49530fSBill Paul 					if (!define_printed(proc,
380ff49530fSBill Paul 							    def->def.pr.versions)) {
381ff49530fSBill Paul 						puldefine(proc->proc_name,
382ff49530fSBill Paul 							  proc->proc_num);
383ff49530fSBill Paul 					}
384ff49530fSBill Paul 					f_print(fout, "%s", ext);
385ff49530fSBill Paul 					pprocdef(proc, vers, "CLIENT *", 0, i);
386ff49530fSBill Paul 					f_print(fout, "%s", ext);
387ff49530fSBill Paul 					pprocdef(proc, vers, "struct svc_req *", 1, i);
388ff49530fSBill Paul 				}
389ff49530fSBill Paul 			pfreeprocdef(def->def_name, vers->vers_num, i);
390ff49530fSBill Paul 			}
391ff49530fSBill Paul 			f_print(fout, "#endif /* K&R C */\n");
3924e115012SGarrett Wollman 		}
3934e115012SGarrett Wollman 	}
3944e115012SGarrett Wollman }
3954e115012SGarrett Wollman 
396526195adSJordan K. Hubbard static void
397ff49530fSBill Paul pprocdef(proc, vp, addargtype, server_p, mode)
3984e115012SGarrett Wollman 	proc_list *proc;
3994e115012SGarrett Wollman 	version_list *vp;
400ff49530fSBill Paul 	char* addargtype;
401ff49530fSBill Paul 	int server_p;
402ff49530fSBill Paul 	int mode;
4034e115012SGarrett Wollman {
404ff49530fSBill Paul 	if (mtflag) {/* Print MT style stubs */
405ff49530fSBill Paul 		if (server_p)
406ff49530fSBill Paul 			f_print(fout, "bool_t ");
407ff49530fSBill Paul 		else
408ff49530fSBill Paul 			f_print(fout, "enum clnt_stat ");
4094e115012SGarrett Wollman 	} else {
410ff49530fSBill Paul 		ptype(proc->res_prefix, proc->res_type, 1);
411ff49530fSBill Paul 		f_print(fout, "* ");
4124e115012SGarrett Wollman 	}
413ff49530fSBill Paul 	if (server_p)
414ff49530fSBill Paul 		pvname_svc(proc->proc_name, vp->vers_num);
415ff49530fSBill Paul 	else
4164e115012SGarrett Wollman 		pvname(proc->proc_name, vp->vers_num);
417ff49530fSBill Paul 
418ff49530fSBill Paul 	/*
419ff49530fSBill Paul 	 *  mode  1 = ANSI-C, mode 2 = K&R C
420ff49530fSBill Paul 	 */
421ff49530fSBill Paul 	if ( mode == 1)
422ff49530fSBill Paul 		parglist(proc, addargtype);
423ff49530fSBill Paul 	else
4244e115012SGarrett Wollman 		f_print(fout, "();\n");
425ff49530fSBill Paul }
426ff49530fSBill Paul 
427ff49530fSBill Paul 
428ff49530fSBill Paul 
429ff49530fSBill Paul /* print out argument list of procedure */
430526195adSJordan K. Hubbard static void
431ff49530fSBill Paul parglist(proc, addargtype)
432ff49530fSBill Paul 	proc_list *proc;
433ff49530fSBill Paul     char* addargtype;
434ff49530fSBill Paul {
435ff49530fSBill Paul 	decl_list *dl;
436ff49530fSBill Paul 
437ff49530fSBill Paul 	f_print(fout, "(");
438ff49530fSBill Paul 	if (proc->arg_num < 2 && newstyle &&
439ff49530fSBill Paul 	    streq(proc->args.decls->decl.type, "void")) {
440ff49530fSBill Paul 		/* 0 argument in new style:  do nothing*/
441ff49530fSBill Paul 	}
442ff49530fSBill Paul 	else {
443ff49530fSBill Paul 		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
444ff49530fSBill Paul 			ptype(dl->decl.prefix, dl->decl.type, 1);
445ff49530fSBill Paul 			if (!newstyle)
446ff49530fSBill Paul 				f_print(fout, "*");
447ff49530fSBill Paul 			/* old style passes by reference */
448ff49530fSBill Paul 			f_print(fout, ", ");
449ff49530fSBill Paul 		}
450ff49530fSBill Paul 	}
451ff49530fSBill Paul 
452ff49530fSBill Paul 	if (mtflag)  {
453ff49530fSBill Paul 		ptype(proc->res_prefix, proc->res_type, 1);
454ff49530fSBill Paul 		f_print(fout, "*, ");
455ff49530fSBill Paul 	}
456ff49530fSBill Paul 
457ff49530fSBill Paul 	f_print(fout, "%s);\n", addargtype);
458ff49530fSBill Paul 
4594e115012SGarrett Wollman }
4604e115012SGarrett Wollman 
461526195adSJordan K. Hubbard static void
4624e115012SGarrett Wollman penumdef(def)
4634e115012SGarrett Wollman 	definition *def;
4644e115012SGarrett Wollman {
4654e115012SGarrett Wollman 	char *name = def->def_name;
4664e115012SGarrett Wollman 	enumval_list *l;
4674e115012SGarrett Wollman 	char *last = NULL;
4684e115012SGarrett Wollman 	int count = 0;
4694e115012SGarrett Wollman 
4704e115012SGarrett Wollman 	f_print(fout, "enum %s {\n", name);
4714e115012SGarrett Wollman 	for (l = def->def.en.vals; l != NULL; l = l->next) {
4724e115012SGarrett Wollman 		f_print(fout, "\t%s", l->name);
4734e115012SGarrett Wollman 		if (l->assignment) {
4744e115012SGarrett Wollman 			f_print(fout, " = %s", l->assignment);
4754e115012SGarrett Wollman 			last = l->assignment;
4764e115012SGarrett Wollman 			count = 1;
4774e115012SGarrett Wollman 		} else {
4784e115012SGarrett Wollman 			if (last == NULL) {
4794e115012SGarrett Wollman 				f_print(fout, " = %d", count++);
4804e115012SGarrett Wollman 			} else {
4814e115012SGarrett Wollman 				f_print(fout, " = %s + %d", last, count++);
4824e115012SGarrett Wollman 			}
4834e115012SGarrett Wollman 		}
484ff49530fSBill Paul 		if (l->next)
4854e115012SGarrett Wollman 			f_print(fout, ",\n");
486ff49530fSBill Paul 		else
487ff49530fSBill Paul 			f_print(fout, "\n");
4884e115012SGarrett Wollman 	}
4894e115012SGarrett Wollman 	f_print(fout, "};\n");
4904e115012SGarrett Wollman 	f_print(fout, "typedef enum %s %s;\n", name, name);
4914e115012SGarrett Wollman }
4924e115012SGarrett Wollman 
493526195adSJordan K. Hubbard static void
4944e115012SGarrett Wollman ptypedef(def)
4954e115012SGarrett Wollman 	definition *def;
4964e115012SGarrett Wollman {
4974e115012SGarrett Wollman 	char *name = def->def_name;
4984e115012SGarrett Wollman 	char *old = def->def.ty.old_type;
4994e115012SGarrett Wollman 	char prefix[8];	/* enough to contain "struct ", including NUL */
5004e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
5014e115012SGarrett Wollman 
5024e115012SGarrett Wollman 
5034e115012SGarrett Wollman 	if (!streq(name, old)) {
5044e115012SGarrett Wollman 		if (streq(old, "string")) {
5054e115012SGarrett Wollman 			old = "char";
5064e115012SGarrett Wollman 			rel = REL_POINTER;
5074e115012SGarrett Wollman 		} else if (streq(old, "opaque")) {
5084e115012SGarrett Wollman 			old = "char";
5094e115012SGarrett Wollman 		} else if (streq(old, "bool")) {
5104e115012SGarrett Wollman 			old = "bool_t";
5114e115012SGarrett Wollman 		}
5124e115012SGarrett Wollman 		if (undefined2(old, name) && def->def.ty.old_prefix) {
5134e115012SGarrett Wollman 			s_print(prefix, "%s ", def->def.ty.old_prefix);
5144e115012SGarrett Wollman 		} else {
5154e115012SGarrett Wollman 			prefix[0] = 0;
5164e115012SGarrett Wollman 		}
5174e115012SGarrett Wollman 		f_print(fout, "typedef ");
5184e115012SGarrett Wollman 		switch (rel) {
5194e115012SGarrett Wollman 		case REL_ARRAY:
5204e115012SGarrett Wollman 			f_print(fout, "struct {\n");
5214e115012SGarrett Wollman 			f_print(fout, "\tu_int %s_len;\n", name);
5224e115012SGarrett Wollman 			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
5234e115012SGarrett Wollman 			f_print(fout, "} %s", name);
5244e115012SGarrett Wollman 			break;
5254e115012SGarrett Wollman 		case REL_POINTER:
5264e115012SGarrett Wollman 			f_print(fout, "%s%s *%s", prefix, old, name);
5274e115012SGarrett Wollman 			break;
5284e115012SGarrett Wollman 		case REL_VECTOR:
5294e115012SGarrett Wollman 			f_print(fout, "%s%s %s[%s]", prefix, old, name,
5304e115012SGarrett Wollman 				def->def.ty.array_max);
5314e115012SGarrett Wollman 			break;
5324e115012SGarrett Wollman 		case REL_ALIAS:
5334e115012SGarrett Wollman 			f_print(fout, "%s%s %s", prefix, old, name);
5344e115012SGarrett Wollman 			break;
5354e115012SGarrett Wollman 		}
5364e115012SGarrett Wollman 		f_print(fout, ";\n");
5374e115012SGarrett Wollman 	}
5384e115012SGarrett Wollman }
5394e115012SGarrett Wollman 
540526195adSJordan K. Hubbard void
541ff49530fSBill Paul pdeclaration(name, dec, tab, separator)
5424e115012SGarrett Wollman 	char *name;
5434e115012SGarrett Wollman 	declaration *dec;
5444e115012SGarrett Wollman 	int tab;
545ff49530fSBill Paul 	char *separator;
5464e115012SGarrett Wollman {
5474e115012SGarrett Wollman 	char buf[8];	/* enough to hold "struct ", include NUL */
5484e115012SGarrett Wollman 	char *prefix;
5494e115012SGarrett Wollman 	char *type;
5504e115012SGarrett Wollman 
5514e115012SGarrett Wollman 	if (streq(dec->type, "void")) {
5524e115012SGarrett Wollman 		return;
5534e115012SGarrett Wollman 	}
5544e115012SGarrett Wollman 	tabify(fout, tab);
5554e115012SGarrett Wollman 	if (streq(dec->type, name) && !dec->prefix) {
5564e115012SGarrett Wollman 		f_print(fout, "struct ");
5574e115012SGarrett Wollman 	}
5584e115012SGarrett Wollman 	if (streq(dec->type, "string")) {
5594e115012SGarrett Wollman 		f_print(fout, "char *%s", dec->name);
5604e115012SGarrett Wollman 	} else {
5614e115012SGarrett Wollman 		prefix = "";
5624e115012SGarrett Wollman 		if (streq(dec->type, "bool")) {
5634e115012SGarrett Wollman 			type = "bool_t";
5644e115012SGarrett Wollman 		} else if (streq(dec->type, "opaque")) {
5654e115012SGarrett Wollman 			type = "char";
5664e115012SGarrett Wollman 		} else {
5674e115012SGarrett Wollman 			if (dec->prefix) {
5684e115012SGarrett Wollman 				s_print(buf, "%s ", dec->prefix);
5694e115012SGarrett Wollman 				prefix = buf;
5704e115012SGarrett Wollman 			}
5714e115012SGarrett Wollman 			type = dec->type;
5724e115012SGarrett Wollman 		}
5734e115012SGarrett Wollman 		switch (dec->rel) {
5744e115012SGarrett Wollman 		case REL_ALIAS:
5754e115012SGarrett Wollman 			f_print(fout, "%s%s %s", prefix, type, dec->name);
5764e115012SGarrett Wollman 			break;
5774e115012SGarrett Wollman 		case REL_VECTOR:
5784e115012SGarrett Wollman 			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
5794e115012SGarrett Wollman 				dec->array_max);
5804e115012SGarrett Wollman 			break;
5814e115012SGarrett Wollman 		case REL_POINTER:
5824e115012SGarrett Wollman 			f_print(fout, "%s%s *%s", prefix, type, dec->name);
5834e115012SGarrett Wollman 			break;
5844e115012SGarrett Wollman 		case REL_ARRAY:
5854e115012SGarrett Wollman 			f_print(fout, "struct {\n");
5864e115012SGarrett Wollman 			tabify(fout, tab);
5874e115012SGarrett Wollman 			f_print(fout, "\tu_int %s_len;\n", dec->name);
5884e115012SGarrett Wollman 			tabify(fout, tab);
589ff49530fSBill Paul 			f_print(fout,
590ff49530fSBill Paul 				"\t%s%s *%s_val;\n", prefix, type, dec->name);
5914e115012SGarrett Wollman 			tabify(fout, tab);
5924e115012SGarrett Wollman 			f_print(fout, "} %s", dec->name);
5934e115012SGarrett Wollman 			break;
5944e115012SGarrett Wollman 		}
5954e115012SGarrett Wollman 	}
596ff49530fSBill Paul 	f_print(fout, separator);
5974e115012SGarrett Wollman }
5984e115012SGarrett Wollman 
599526195adSJordan K. Hubbard static int
6004e115012SGarrett Wollman undefined2(type, stop)
6014e115012SGarrett Wollman 	char *type;
6024e115012SGarrett Wollman 	char *stop;
6034e115012SGarrett Wollman {
6044e115012SGarrett Wollman 	list *l;
6054e115012SGarrett Wollman 	definition *def;
6064e115012SGarrett Wollman 
6074e115012SGarrett Wollman 	for (l = defined; l != NULL; l = l->next) {
6084e115012SGarrett Wollman 		def = (definition *) l->val;
6094e115012SGarrett Wollman 		if (def->def_kind != DEF_PROGRAM) {
6104e115012SGarrett Wollman 			if (streq(def->def_name, stop)) {
6114e115012SGarrett Wollman 				return (1);
6124e115012SGarrett Wollman 			} else if (streq(def->def_name, type)) {
6134e115012SGarrett Wollman 				return (0);
6144e115012SGarrett Wollman 			}
6154e115012SGarrett Wollman 		}
6164e115012SGarrett Wollman 	}
6174e115012SGarrett Wollman 	return (1);
6184e115012SGarrett Wollman }
619