xref: /freebsd/usr.bin/rpcgen/rpc_hout.c (revision 15df5e2d4379bc97dd1fcac1ce18e53d71fdd016)
14e115012SGarrett Wollman /*
24e115012SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
34e115012SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
44e115012SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
54e115012SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
64e115012SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
74e115012SGarrett Wollman  * program developed by the user.
84e115012SGarrett Wollman  *
94e115012SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
104e115012SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
114e115012SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
124e115012SGarrett Wollman  *
134e115012SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
144e115012SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
154e115012SGarrett Wollman  * modification or enhancement.
164e115012SGarrett Wollman  *
174e115012SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
184e115012SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
194e115012SGarrett Wollman  * OR ANY PART THEREOF.
204e115012SGarrett Wollman  *
214e115012SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
224e115012SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
234e115012SGarrett Wollman  * Sun has been advised of the possibility of such damages.
244e115012SGarrett Wollman  *
254e115012SGarrett Wollman  * Sun Microsystems, Inc.
264e115012SGarrett Wollman  * 2550 Garcia Avenue
274e115012SGarrett Wollman  * Mountain View, California  94043
284e115012SGarrett Wollman  */
294e115012SGarrett Wollman 
3040ad8885SAlfred Perlstein #if 0
3175863a6dSPhilippe Charnier #ifndef lint
3263f17371SStefan Farfeleder #ident	"@(#)rpc_hout.c	1.16	94/04/25 SMI"
33ff49530fSBill Paul static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
34ff49530fSBill Paul #endif
3540ad8885SAlfred Perlstein #endif
364e115012SGarrett Wollman 
3775863a6dSPhilippe Charnier #include <sys/cdefs.h>
3875863a6dSPhilippe Charnier __FBSDID("$FreeBSD$");
3975863a6dSPhilippe Charnier 
404e115012SGarrett Wollman /*
414e115012SGarrett Wollman  * rpc_hout.c, Header file outputter for the RPC protocol compiler
424e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
434e115012SGarrett Wollman  */
444e115012SGarrett Wollman #include <stdio.h>
454e115012SGarrett Wollman #include <ctype.h>
464e115012SGarrett Wollman #include "rpc_parse.h"
47d0cc804bSStefan Farfeleder #include "rpc_scan.h"
48ff49530fSBill Paul #include "rpc_util.h"
49ff49530fSBill Paul 
50d3cb5dedSWarner Losh void storexdrfuncdecl( char *, int );
51d3cb5dedSWarner Losh static void pconstdef( definition * );
52d3cb5dedSWarner Losh static void pstructdef( definition * );
53d3cb5dedSWarner Losh static void puniondef( definition * );
54ec06b5e8SStefan Farfeleder static void pprogramdef( definition *, int );
55d3cb5dedSWarner Losh static void pstructdef( definition * );
56d3cb5dedSWarner Losh static void penumdef( definition * );
57d3cb5dedSWarner Losh static void ptypedef( definition * );
58d3cb5dedSWarner Losh static void pdefine( char *, char * );
59d3cb5dedSWarner Losh static int undefined2( char *, char * );
60d3cb5dedSWarner Losh static void parglist( proc_list *, char * );
6115df5e2dSStefan Farfeleder static void pprocdef( proc_list *, version_list *, char *, int );
62d3cb5dedSWarner Losh void pdeclaration( char *, declaration *, int, char * );
63ff49530fSBill Paul 
644e115012SGarrett Wollman /*
654e115012SGarrett Wollman  * Print the C-version of an xdr definition
664e115012SGarrett Wollman  */
674e115012SGarrett Wollman void
68ec06b5e8SStefan Farfeleder print_datadef(definition *def, int headeronly)
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:
91ec06b5e8SStefan Farfeleder 		pprogramdef(def, headeronly);
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
107ec06b5e8SStefan Farfeleder print_funcdef(definition *def, int headeronly)
108ff49530fSBill Paul {
109ff49530fSBill Paul 	switch (def->def_kind) {
110ff49530fSBill Paul 	case DEF_PROGRAM:
1114e115012SGarrett Wollman 		f_print(fout, "\n");
112ec06b5e8SStefan Farfeleder 		pprogramdef(def, headeronly);
113ff49530fSBill Paul 		break;
114526195adSJordan K. Hubbard 	default:
11540ad8885SAlfred Perlstein 		break;
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 
12975863a6dSPhilippe Charnier 	xdrptr = XALLOC(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
14715df5e2dSStefan Farfeleder print_xdr_func_def(char *name, int pointerp)
148ff49530fSBill Paul {
149ff49530fSBill Paul 	f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name,
150ff49530fSBill Paul 		name, pointerp ? "*" : "");
151ff49530fSBill Paul }
152ff49530fSBill Paul 
153ff49530fSBill Paul 
154526195adSJordan K. Hubbard static void
1554e115012SGarrett Wollman pconstdef(def)
1564e115012SGarrett Wollman 	definition *def;
1574e115012SGarrett Wollman {
1584e115012SGarrett Wollman 	pdefine(def->def_name, def->def.co);
1594e115012SGarrett Wollman }
1604e115012SGarrett Wollman 
161ff49530fSBill Paul /* print out the definitions for the arguments of functions in the
162ff49530fSBill Paul     header file
163ff49530fSBill Paul */
164526195adSJordan K. Hubbard static void
165ff49530fSBill Paul pargdef(def)
166ff49530fSBill Paul 	definition *def;
167ff49530fSBill Paul {
168ff49530fSBill Paul 	decl_list *l;
169ff49530fSBill Paul 	version_list *vers;
170ff49530fSBill Paul 	char *name;
171ff49530fSBill Paul 	proc_list *plist;
172ff49530fSBill Paul 
173ff49530fSBill Paul 
174ff49530fSBill Paul 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
175ff49530fSBill Paul 			for (plist = vers->procs; plist != NULL;
176ff49530fSBill Paul 			    plist = plist->next) {
177ff49530fSBill Paul 
178ff49530fSBill Paul 				if (!newstyle || plist->arg_num < 2) {
179ff49530fSBill Paul 					continue; /* old style or single args */
180ff49530fSBill Paul 				}
181ff49530fSBill Paul 				name = plist->args.argname;
182ff49530fSBill Paul 				f_print(fout, "struct %s {\n", name);
183ff49530fSBill Paul 				for (l = plist->args.decls;
184ff49530fSBill Paul 				    l != NULL; l = l->next) {
185ff49530fSBill Paul 					pdeclaration(name, &l->decl, 1,
186ff49530fSBill Paul 						     ";\n");
187ff49530fSBill Paul 				}
188ff49530fSBill Paul 				f_print(fout, "};\n");
189ff49530fSBill Paul 				f_print(fout, "typedef struct %s %s;\n",
190ff49530fSBill Paul 					name, name);
191ff49530fSBill Paul 				storexdrfuncdecl(name, 1);
192ff49530fSBill Paul 				f_print(fout, "\n");
193ff49530fSBill Paul 			}
194ff49530fSBill Paul 		}
195ff49530fSBill Paul }
196ff49530fSBill Paul 
197ff49530fSBill Paul 
198526195adSJordan K. Hubbard static void
1994e115012SGarrett Wollman pstructdef(def)
2004e115012SGarrett Wollman 	definition *def;
2014e115012SGarrett Wollman {
2024e115012SGarrett Wollman 	decl_list *l;
2034e115012SGarrett Wollman 	char *name = def->def_name;
2044e115012SGarrett Wollman 
2054e115012SGarrett Wollman 	f_print(fout, "struct %s {\n", name);
2064e115012SGarrett Wollman 	for (l = def->def.st.decls; l != NULL; l = l->next) {
207ff49530fSBill Paul 		pdeclaration(name, &l->decl, 1, ";\n");
2084e115012SGarrett Wollman 	}
2094e115012SGarrett Wollman 	f_print(fout, "};\n");
2104e115012SGarrett Wollman 	f_print(fout, "typedef struct %s %s;\n", name, name);
2114e115012SGarrett Wollman }
2124e115012SGarrett Wollman 
213526195adSJordan K. Hubbard static void
2144e115012SGarrett Wollman puniondef(def)
2154e115012SGarrett Wollman 	definition *def;
2164e115012SGarrett Wollman {
2174e115012SGarrett Wollman 	case_list *l;
2184e115012SGarrett Wollman 	char *name = def->def_name;
2194e115012SGarrett Wollman 	declaration *decl;
2204e115012SGarrett Wollman 
2214e115012SGarrett Wollman 	f_print(fout, "struct %s {\n", name);
2224e115012SGarrett Wollman 	decl = &def->def.un.enum_decl;
2234e115012SGarrett Wollman 	if (streq(decl->type, "bool")) {
2244e115012SGarrett Wollman 		f_print(fout, "\tbool_t %s;\n", decl->name);
2254e115012SGarrett Wollman 	} else {
2264e115012SGarrett Wollman 		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
2274e115012SGarrett Wollman 	}
2284e115012SGarrett Wollman 	f_print(fout, "\tunion {\n");
2294e115012SGarrett Wollman 	for (l = def->def.un.cases; l != NULL; l = l->next) {
230ff49530fSBill Paul 	    if (l->contflag == 0)
231ff49530fSBill Paul 		pdeclaration(name, &l->case_decl, 2, ";\n");
2324e115012SGarrett Wollman 	}
2334e115012SGarrett Wollman 	decl = def->def.un.default_decl;
2344e115012SGarrett Wollman 	if (decl && !streq(decl->type, "void")) {
235ff49530fSBill Paul 		pdeclaration(name, decl, 2, ";\n");
2364e115012SGarrett Wollman 	}
2374e115012SGarrett Wollman 	f_print(fout, "\t} %s_u;\n", name);
2384e115012SGarrett Wollman 	f_print(fout, "};\n");
2394e115012SGarrett Wollman 	f_print(fout, "typedef struct %s %s;\n", name, name);
2404e115012SGarrett Wollman }
2414e115012SGarrett Wollman 
242526195adSJordan K. Hubbard static void
2434e115012SGarrett Wollman pdefine(name, num)
2444e115012SGarrett Wollman 	char *name;
2454e115012SGarrett Wollman 	char *num;
2464e115012SGarrett Wollman {
247ff49530fSBill Paul 	f_print(fout, "#define\t%s %s\n", name, num);
2484e115012SGarrett Wollman }
2494e115012SGarrett Wollman 
250526195adSJordan K. Hubbard static void
2514e115012SGarrett Wollman puldefine(name, num)
2524e115012SGarrett Wollman 	char *name;
2534e115012SGarrett Wollman 	char *num;
2544e115012SGarrett Wollman {
255ff49530fSBill Paul 	f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
2564e115012SGarrett Wollman }
2574e115012SGarrett Wollman 
258526195adSJordan K. Hubbard static int
2594e115012SGarrett Wollman define_printed(stop, start)
2604e115012SGarrett Wollman 	proc_list *stop;
2614e115012SGarrett Wollman 	version_list *start;
2624e115012SGarrett Wollman {
2634e115012SGarrett Wollman 	version_list *vers;
2644e115012SGarrett Wollman 	proc_list *proc;
2654e115012SGarrett Wollman 
2664e115012SGarrett Wollman 	for (vers = start; vers != NULL; vers = vers->next) {
2674e115012SGarrett Wollman 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
2684e115012SGarrett Wollman 			if (proc == stop) {
2694e115012SGarrett Wollman 				return (0);
2704e115012SGarrett Wollman 			} else if (streq(proc->proc_name, stop->proc_name)) {
2714e115012SGarrett Wollman 				return (1);
2724e115012SGarrett Wollman 			}
2734e115012SGarrett Wollman 		}
2744e115012SGarrett Wollman 	}
2754e115012SGarrett Wollman 	abort();
2764e115012SGarrett Wollman 	/* NOTREACHED */
2774e115012SGarrett Wollman }
2784e115012SGarrett Wollman 
279526195adSJordan K. Hubbard static void
28015df5e2dSStefan Farfeleder pfreeprocdef(char * name, char *vers)
281ff49530fSBill Paul {
282ff49530fSBill Paul 	f_print(fout, "extern int ");
283ff49530fSBill Paul 	pvname(name, vers);
284ff49530fSBill Paul 	f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
285ff49530fSBill Paul }
2864e115012SGarrett Wollman 
287526195adSJordan K. Hubbard static void
28815df5e2dSStefan Farfeleder pdispatch(char * name, char *vers)
2897e440a74SAlfred Perlstein {
2907e440a74SAlfred Perlstein 
2917e440a74SAlfred Perlstein 	f_print(fout, "void ");
2927e440a74SAlfred Perlstein 	pvname(name, vers);
29315df5e2dSStefan Farfeleder 	f_print(fout, "(struct svc_req *rqstp, register SVCXPRT *transp);\n");
2947e440a74SAlfred Perlstein }
2957e440a74SAlfred Perlstein 
2967e440a74SAlfred Perlstein static void
297ec06b5e8SStefan Farfeleder pprogramdef(definition *def, int headeronly)
2984e115012SGarrett Wollman {
2994e115012SGarrett Wollman 	version_list *vers;
3004e115012SGarrett Wollman 	proc_list *proc;
301ff49530fSBill Paul 	char *ext;
302ff49530fSBill Paul 
303ff49530fSBill Paul 	pargdef(def);
3044e115012SGarrett Wollman 
3054e115012SGarrett Wollman 	puldefine(def->def_name, def->def.pr.prog_num);
3064e115012SGarrett Wollman 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
307ff49530fSBill Paul 		if (tblflag) {
308ff49530fSBill Paul 			f_print(fout,
309ff49530fSBill Paul 				"extern struct rpcgen_table %s_%s_table[];\n",
310ff49530fSBill Paul 				locase(def->def_name), vers->vers_num);
311ff49530fSBill Paul 			f_print(fout,
312ff49530fSBill Paul 				"extern %s_%s_nproc;\n",
313ff49530fSBill Paul 				locase(def->def_name), vers->vers_num);
314ff49530fSBill Paul 		}
3154e115012SGarrett Wollman 		puldefine(vers->vers_name, vers->vers_num);
316ff49530fSBill Paul 
31715df5e2dSStefan Farfeleder 		f_print(fout, "\n");
318ff49530fSBill Paul 		ext = "extern  ";
319ec06b5e8SStefan Farfeleder 		if (headeronly) {
3207e440a74SAlfred Perlstein 			f_print(fout, "%s", ext);
32115df5e2dSStefan Farfeleder 			pdispatch(def->def_name, vers->vers_num);
322ec06b5e8SStefan Farfeleder 		}
32315df5e2dSStefan Farfeleder 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
32415df5e2dSStefan Farfeleder 			if (!define_printed(proc, def->def.pr.versions)) {
32515df5e2dSStefan Farfeleder 				puldefine(proc->proc_name, proc->proc_num);
3264e115012SGarrett Wollman 			}
327ff49530fSBill Paul 			f_print(fout, "%s", ext);
32815df5e2dSStefan Farfeleder 			pprocdef(proc, vers, "CLIENT *", 0);
329ff49530fSBill Paul 			f_print(fout, "%s", ext);
33015df5e2dSStefan Farfeleder 			pprocdef(proc, vers, "struct svc_req *", 1);
331ff49530fSBill Paul 		}
33215df5e2dSStefan Farfeleder 		pfreeprocdef(def->def_name, vers->vers_num);
3334e115012SGarrett Wollman 	}
3344e115012SGarrett Wollman }
3354e115012SGarrett Wollman 
336526195adSJordan K. Hubbard static void
33715df5e2dSStefan Farfeleder pprocdef(proc_list *proc, version_list *vp, char *addargtype, int server_p)
3384e115012SGarrett Wollman {
339ff49530fSBill Paul 	if (mtflag) {/* Print MT style stubs */
340ff49530fSBill Paul 		if (server_p)
341ff49530fSBill Paul 			f_print(fout, "bool_t ");
342ff49530fSBill Paul 		else
343ff49530fSBill Paul 			f_print(fout, "enum clnt_stat ");
3444e115012SGarrett Wollman 	} else {
345ff49530fSBill Paul 		ptype(proc->res_prefix, proc->res_type, 1);
346ff49530fSBill Paul 		f_print(fout, "* ");
3474e115012SGarrett Wollman 	}
348ff49530fSBill Paul 	if (server_p)
349ff49530fSBill Paul 		pvname_svc(proc->proc_name, vp->vers_num);
350ff49530fSBill Paul 	else
3514e115012SGarrett Wollman 		pvname(proc->proc_name, vp->vers_num);
352ff49530fSBill Paul 
353ff49530fSBill Paul 	parglist(proc, addargtype);
354ff49530fSBill Paul }
355ff49530fSBill Paul 
356ff49530fSBill Paul 
357ff49530fSBill Paul 
358ff49530fSBill Paul /* print out argument list of procedure */
359526195adSJordan K. Hubbard static void
360ff49530fSBill Paul parglist(proc, addargtype)
361ff49530fSBill Paul 	proc_list *proc;
362ff49530fSBill Paul     char* addargtype;
363ff49530fSBill Paul {
364ff49530fSBill Paul 	decl_list *dl;
365ff49530fSBill Paul 
366ff49530fSBill Paul 	f_print(fout, "(");
367ff49530fSBill Paul 	if (proc->arg_num < 2 && newstyle &&
368ff49530fSBill Paul 	    streq(proc->args.decls->decl.type, "void")) {
369ff49530fSBill Paul 		/* 0 argument in new style:  do nothing*/
370ff49530fSBill Paul 	}
371ff49530fSBill Paul 	else {
372ff49530fSBill Paul 		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
373ff49530fSBill Paul 			ptype(dl->decl.prefix, dl->decl.type, 1);
374ff49530fSBill Paul 			if (!newstyle)
375ff49530fSBill Paul 				f_print(fout, "*");
376ff49530fSBill Paul 			/* old style passes by reference */
377ff49530fSBill Paul 			f_print(fout, ", ");
378ff49530fSBill Paul 		}
379ff49530fSBill Paul 	}
380ff49530fSBill Paul 
381ff49530fSBill Paul 	if (mtflag)  {
382ff49530fSBill Paul 		ptype(proc->res_prefix, proc->res_type, 1);
383ff49530fSBill Paul 		f_print(fout, "*, ");
384ff49530fSBill Paul 	}
385ff49530fSBill Paul 
386ff49530fSBill Paul 	f_print(fout, "%s);\n", addargtype);
387ff49530fSBill Paul 
3884e115012SGarrett Wollman }
3894e115012SGarrett Wollman 
390526195adSJordan K. Hubbard static void
3914e115012SGarrett Wollman penumdef(def)
3924e115012SGarrett Wollman 	definition *def;
3934e115012SGarrett Wollman {
3944e115012SGarrett Wollman 	char *name = def->def_name;
3954e115012SGarrett Wollman 	enumval_list *l;
3964e115012SGarrett Wollman 	char *last = NULL;
3974e115012SGarrett Wollman 	int count = 0;
3984e115012SGarrett Wollman 
3994e115012SGarrett Wollman 	f_print(fout, "enum %s {\n", name);
4004e115012SGarrett Wollman 	for (l = def->def.en.vals; l != NULL; l = l->next) {
4014e115012SGarrett Wollman 		f_print(fout, "\t%s", l->name);
4024e115012SGarrett Wollman 		if (l->assignment) {
4034e115012SGarrett Wollman 			f_print(fout, " = %s", l->assignment);
4044e115012SGarrett Wollman 			last = l->assignment;
4054e115012SGarrett Wollman 			count = 1;
4064e115012SGarrett Wollman 		} else {
4074e115012SGarrett Wollman 			if (last == NULL) {
4084e115012SGarrett Wollman 				f_print(fout, " = %d", count++);
4094e115012SGarrett Wollman 			} else {
4104e115012SGarrett Wollman 				f_print(fout, " = %s + %d", last, count++);
4114e115012SGarrett Wollman 			}
4124e115012SGarrett Wollman 		}
413ff49530fSBill Paul 		if (l->next)
4144e115012SGarrett Wollman 			f_print(fout, ",\n");
415ff49530fSBill Paul 		else
416ff49530fSBill Paul 			f_print(fout, "\n");
4174e115012SGarrett Wollman 	}
4184e115012SGarrett Wollman 	f_print(fout, "};\n");
4194e115012SGarrett Wollman 	f_print(fout, "typedef enum %s %s;\n", name, name);
4204e115012SGarrett Wollman }
4214e115012SGarrett Wollman 
422526195adSJordan K. Hubbard static void
4234e115012SGarrett Wollman ptypedef(def)
4244e115012SGarrett Wollman 	definition *def;
4254e115012SGarrett Wollman {
4264e115012SGarrett Wollman 	char *name = def->def_name;
4274e115012SGarrett Wollman 	char *old = def->def.ty.old_type;
4284e115012SGarrett Wollman 	char prefix[8];	/* enough to contain "struct ", including NUL */
4294e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
4304e115012SGarrett Wollman 
4314e115012SGarrett Wollman 
4324e115012SGarrett Wollman 	if (!streq(name, old)) {
4334e115012SGarrett Wollman 		if (streq(old, "string")) {
4344e115012SGarrett Wollman 			old = "char";
4354e115012SGarrett Wollman 			rel = REL_POINTER;
4364e115012SGarrett Wollman 		} else if (streq(old, "opaque")) {
4374e115012SGarrett Wollman 			old = "char";
4384e115012SGarrett Wollman 		} else if (streq(old, "bool")) {
4394e115012SGarrett Wollman 			old = "bool_t";
4404e115012SGarrett Wollman 		}
4414e115012SGarrett Wollman 		if (undefined2(old, name) && def->def.ty.old_prefix) {
4424e115012SGarrett Wollman 			s_print(prefix, "%s ", def->def.ty.old_prefix);
4434e115012SGarrett Wollman 		} else {
4444e115012SGarrett Wollman 			prefix[0] = 0;
4454e115012SGarrett Wollman 		}
4464e115012SGarrett Wollman 		f_print(fout, "typedef ");
4474e115012SGarrett Wollman 		switch (rel) {
4484e115012SGarrett Wollman 		case REL_ARRAY:
4494e115012SGarrett Wollman 			f_print(fout, "struct {\n");
4504e115012SGarrett Wollman 			f_print(fout, "\tu_int %s_len;\n", name);
4514e115012SGarrett Wollman 			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
4524e115012SGarrett Wollman 			f_print(fout, "} %s", name);
4534e115012SGarrett Wollman 			break;
4544e115012SGarrett Wollman 		case REL_POINTER:
4554e115012SGarrett Wollman 			f_print(fout, "%s%s *%s", prefix, old, name);
4564e115012SGarrett Wollman 			break;
4574e115012SGarrett Wollman 		case REL_VECTOR:
4584e115012SGarrett Wollman 			f_print(fout, "%s%s %s[%s]", prefix, old, name,
4594e115012SGarrett Wollman 				def->def.ty.array_max);
4604e115012SGarrett Wollman 			break;
4614e115012SGarrett Wollman 		case REL_ALIAS:
4624e115012SGarrett Wollman 			f_print(fout, "%s%s %s", prefix, old, name);
4634e115012SGarrett Wollman 			break;
4644e115012SGarrett Wollman 		}
4654e115012SGarrett Wollman 		f_print(fout, ";\n");
4664e115012SGarrett Wollman 	}
4674e115012SGarrett Wollman }
4684e115012SGarrett Wollman 
469526195adSJordan K. Hubbard void
470ff49530fSBill Paul pdeclaration(name, dec, tab, separator)
4714e115012SGarrett Wollman 	char *name;
4724e115012SGarrett Wollman 	declaration *dec;
4734e115012SGarrett Wollman 	int tab;
474ff49530fSBill Paul 	char *separator;
4754e115012SGarrett Wollman {
4764e115012SGarrett Wollman 	char buf[8];	/* enough to hold "struct ", include NUL */
4774e115012SGarrett Wollman 	char *prefix;
4784e115012SGarrett Wollman 	char *type;
4794e115012SGarrett Wollman 
4804e115012SGarrett Wollman 	if (streq(dec->type, "void")) {
4814e115012SGarrett Wollman 		return;
4824e115012SGarrett Wollman 	}
4834e115012SGarrett Wollman 	tabify(fout, tab);
4844e115012SGarrett Wollman 	if (streq(dec->type, name) && !dec->prefix) {
4854e115012SGarrett Wollman 		f_print(fout, "struct ");
4864e115012SGarrett Wollman 	}
4874e115012SGarrett Wollman 	if (streq(dec->type, "string")) {
4884e115012SGarrett Wollman 		f_print(fout, "char *%s", dec->name);
4894e115012SGarrett Wollman 	} else {
4904e115012SGarrett Wollman 		prefix = "";
4914e115012SGarrett Wollman 		if (streq(dec->type, "bool")) {
4924e115012SGarrett Wollman 			type = "bool_t";
4934e115012SGarrett Wollman 		} else if (streq(dec->type, "opaque")) {
4944e115012SGarrett Wollman 			type = "char";
4954e115012SGarrett Wollman 		} else {
4964e115012SGarrett Wollman 			if (dec->prefix) {
4974e115012SGarrett Wollman 				s_print(buf, "%s ", dec->prefix);
4984e115012SGarrett Wollman 				prefix = buf;
4994e115012SGarrett Wollman 			}
5004e115012SGarrett Wollman 			type = dec->type;
5014e115012SGarrett Wollman 		}
5024e115012SGarrett Wollman 		switch (dec->rel) {
5034e115012SGarrett Wollman 		case REL_ALIAS:
5044e115012SGarrett Wollman 			f_print(fout, "%s%s %s", prefix, type, dec->name);
5054e115012SGarrett Wollman 			break;
5064e115012SGarrett Wollman 		case REL_VECTOR:
5074e115012SGarrett Wollman 			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
5084e115012SGarrett Wollman 				dec->array_max);
5094e115012SGarrett Wollman 			break;
5104e115012SGarrett Wollman 		case REL_POINTER:
5114e115012SGarrett Wollman 			f_print(fout, "%s%s *%s", prefix, type, dec->name);
5124e115012SGarrett Wollman 			break;
5134e115012SGarrett Wollman 		case REL_ARRAY:
5144e115012SGarrett Wollman 			f_print(fout, "struct {\n");
5154e115012SGarrett Wollman 			tabify(fout, tab);
5164e115012SGarrett Wollman 			f_print(fout, "\tu_int %s_len;\n", dec->name);
5174e115012SGarrett Wollman 			tabify(fout, tab);
518ff49530fSBill Paul 			f_print(fout,
519ff49530fSBill Paul 				"\t%s%s *%s_val;\n", prefix, type, dec->name);
5204e115012SGarrett Wollman 			tabify(fout, tab);
5214e115012SGarrett Wollman 			f_print(fout, "} %s", dec->name);
5224e115012SGarrett Wollman 			break;
5234e115012SGarrett Wollman 		}
5244e115012SGarrett Wollman 	}
525ff49530fSBill Paul 	f_print(fout, separator);
5264e115012SGarrett Wollman }
5274e115012SGarrett Wollman 
528526195adSJordan K. Hubbard static int
5294e115012SGarrett Wollman undefined2(type, stop)
5304e115012SGarrett Wollman 	char *type;
5314e115012SGarrett Wollman 	char *stop;
5324e115012SGarrett Wollman {
5334e115012SGarrett Wollman 	list *l;
5344e115012SGarrett Wollman 	definition *def;
5354e115012SGarrett Wollman 
5364e115012SGarrett Wollman 	for (l = defined; l != NULL; l = l->next) {
5374e115012SGarrett Wollman 		def = (definition *) l->val;
5384e115012SGarrett Wollman 		if (def->def_kind != DEF_PROGRAM) {
5394e115012SGarrett Wollman 			if (streq(def->def_name, stop)) {
5404e115012SGarrett Wollman 				return (1);
5414e115012SGarrett Wollman 			} else if (streq(def->def_name, type)) {
5424e115012SGarrett Wollman 				return (0);
5434e115012SGarrett Wollman 			}
5444e115012SGarrett Wollman 		}
5454e115012SGarrett Wollman 	}
5464e115012SGarrett Wollman 	return (1);
5474e115012SGarrett Wollman }
548