xref: /freebsd/usr.bin/rpcgen/rpc_hout.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
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 
304e115012SGarrett Wollman /*
314e115012SGarrett Wollman  * rpc_hout.c, Header file outputter for the RPC protocol compiler
324e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
334e115012SGarrett Wollman  */
344e115012SGarrett Wollman #include <stdio.h>
35821df508SXin LI #include <ctype.h>
364e115012SGarrett Wollman #include "rpc_parse.h"
37d0cc804bSStefan Farfeleder #include "rpc_scan.h"
38ff49530fSBill Paul #include "rpc_util.h"
39ff49530fSBill Paul 
40e390e3afSDavid Malone void storexdrfuncdecl(const char *, int );
41d3cb5dedSWarner Losh static void pconstdef( definition * );
42d3cb5dedSWarner Losh static void pstructdef( definition * );
43d3cb5dedSWarner Losh static void puniondef( definition * );
44ec06b5e8SStefan Farfeleder static void pprogramdef( definition *, int );
45d3cb5dedSWarner Losh static void penumdef( definition * );
46d3cb5dedSWarner Losh static void ptypedef( definition * );
47e390e3afSDavid Malone static void pdefine(const char *, const char *);
48e390e3afSDavid Malone static int undefined2(const char *, const char *);
49e390e3afSDavid Malone static void parglist(proc_list *, const char *);
50e390e3afSDavid Malone static void pprocdef(proc_list *, version_list *, const char *, int);
51ff49530fSBill Paul 
524e115012SGarrett Wollman /*
534e115012SGarrett Wollman  * Print the C-version of an xdr definition
544e115012SGarrett Wollman  */
554e115012SGarrett Wollman void
print_datadef(definition * def,int headeronly)56ec06b5e8SStefan Farfeleder print_datadef(definition *def, int headeronly)
574e115012SGarrett Wollman {
58ff49530fSBill Paul 
59ff49530fSBill Paul 	if (def->def_kind == DEF_PROGRAM)  /* handle data only */
60ff49530fSBill Paul 		return;
61ff49530fSBill Paul 
624e115012SGarrett Wollman 	if (def->def_kind != DEF_CONST) {
634e115012SGarrett Wollman 		f_print(fout, "\n");
644e115012SGarrett Wollman 	}
654e115012SGarrett Wollman 	switch (def->def_kind) {
664e115012SGarrett Wollman 	case DEF_STRUCT:
674e115012SGarrett Wollman 		pstructdef(def);
684e115012SGarrett Wollman 		break;
694e115012SGarrett Wollman 	case DEF_UNION:
704e115012SGarrett Wollman 		puniondef(def);
714e115012SGarrett Wollman 		break;
724e115012SGarrett Wollman 	case DEF_ENUM:
734e115012SGarrett Wollman 		penumdef(def);
744e115012SGarrett Wollman 		break;
754e115012SGarrett Wollman 	case DEF_TYPEDEF:
764e115012SGarrett Wollman 		ptypedef(def);
774e115012SGarrett Wollman 		break;
784e115012SGarrett Wollman 	case DEF_PROGRAM:
79ec06b5e8SStefan Farfeleder 		pprogramdef(def, headeronly);
804e115012SGarrett Wollman 		break;
814e115012SGarrett Wollman 	case DEF_CONST:
824e115012SGarrett Wollman 		pconstdef(def);
834e115012SGarrett Wollman 		break;
844e115012SGarrett Wollman 	}
854e115012SGarrett Wollman 	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
86ff49530fSBill Paul 	    storexdrfuncdecl(def->def_name,
87ff49530fSBill Paul 			     def->def_kind != DEF_TYPEDEF ||
88ff49530fSBill Paul 			     !isvectordef(def->def.ty.old_type,
89ff49530fSBill Paul 					  def->def.ty.rel));
904e115012SGarrett Wollman 	}
91ff49530fSBill Paul }
92ff49530fSBill Paul 
93ff49530fSBill Paul 
94ff49530fSBill Paul void
print_funcdef(definition * def,int headeronly)95ec06b5e8SStefan Farfeleder print_funcdef(definition *def, int headeronly)
96ff49530fSBill Paul {
97ff49530fSBill Paul 	switch (def->def_kind) {
98ff49530fSBill Paul 	case DEF_PROGRAM:
994e115012SGarrett Wollman 		f_print(fout, "\n");
100ec06b5e8SStefan Farfeleder 		pprogramdef(def, headeronly);
101ff49530fSBill Paul 		break;
102526195adSJordan K. Hubbard 	default:
10340ad8885SAlfred Perlstein 		break;
1044e115012SGarrett Wollman 	}
1054e115012SGarrett Wollman }
1064e115012SGarrett Wollman 
107ff49530fSBill Paul /* store away enough information to allow the XDR functions to be spat
108ff49530fSBill Paul     out at the end of the file */
109ff49530fSBill Paul 
110ff49530fSBill Paul void
storexdrfuncdecl(const char * name,int pointerp)111e390e3afSDavid Malone storexdrfuncdecl(const char *name, int pointerp)
112ff49530fSBill Paul {
113ff49530fSBill Paul 	xdrfunc * xdrptr;
114ff49530fSBill Paul 
11575863a6dSPhilippe Charnier 	xdrptr = XALLOC(struct xdrfunc);
116ff49530fSBill Paul 
117ff49530fSBill Paul 	xdrptr->name = name;
118ff49530fSBill Paul 	xdrptr->pointerp = pointerp;
119ff49530fSBill Paul 	xdrptr->next = NULL;
120ff49530fSBill Paul 
121ff49530fSBill Paul 	if (xdrfunc_tail == NULL){
122ff49530fSBill Paul 		xdrfunc_head = xdrptr;
123ff49530fSBill Paul 		xdrfunc_tail = xdrptr;
124ff49530fSBill Paul 	} else {
125ff49530fSBill Paul 		xdrfunc_tail->next = xdrptr;
126ff49530fSBill Paul 		xdrfunc_tail = xdrptr;
127ff49530fSBill Paul 	}
128ff49530fSBill Paul 
129ff49530fSBill Paul 
130ff49530fSBill Paul }
131ff49530fSBill Paul 
132ff49530fSBill Paul void
print_xdr_func_def(const char * name,int pointerp)133e390e3afSDavid Malone print_xdr_func_def(const char *name, int pointerp)
134ff49530fSBill Paul {
135ff49530fSBill Paul 	f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name,
136ff49530fSBill Paul 		name, pointerp ? "*" : "");
137ff49530fSBill Paul }
138ff49530fSBill Paul 
139ff49530fSBill Paul 
140526195adSJordan K. Hubbard static void
pconstdef(definition * def)141ef636796SEd Schouten pconstdef(definition *def)
1424e115012SGarrett Wollman {
1434e115012SGarrett Wollman 	pdefine(def->def_name, def->def.co);
1444e115012SGarrett Wollman }
1454e115012SGarrett Wollman 
146ff49530fSBill Paul /* print out the definitions for the arguments of functions in the
147ff49530fSBill Paul     header file
148ff49530fSBill Paul */
149526195adSJordan K. Hubbard static void
pargdef(definition * def)150e390e3afSDavid Malone pargdef(definition *def)
151ff49530fSBill Paul {
152ff49530fSBill Paul 	decl_list *l;
153ff49530fSBill Paul 	version_list *vers;
154ff49530fSBill Paul 	char *name;
155ff49530fSBill Paul 	proc_list *plist;
156ff49530fSBill Paul 
157ff49530fSBill Paul 
158ff49530fSBill Paul 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
159ff49530fSBill Paul 			for (plist = vers->procs; plist != NULL;
160ff49530fSBill Paul 			    plist = plist->next) {
161ff49530fSBill Paul 
162ff49530fSBill Paul 				if (!newstyle || plist->arg_num < 2) {
163ff49530fSBill Paul 					continue; /* old style or single args */
164ff49530fSBill Paul 				}
165ff49530fSBill Paul 				name = plist->args.argname;
166ff49530fSBill Paul 				f_print(fout, "struct %s {\n", name);
167ff49530fSBill Paul 				for (l = plist->args.decls;
168ff49530fSBill Paul 				    l != NULL; l = l->next) {
169ff49530fSBill Paul 					pdeclaration(name, &l->decl, 1,
170ff49530fSBill Paul 						     ";\n");
171ff49530fSBill Paul 				}
172ff49530fSBill Paul 				f_print(fout, "};\n");
173ff49530fSBill Paul 				f_print(fout, "typedef struct %s %s;\n",
174ff49530fSBill Paul 					name, name);
175ff49530fSBill Paul 				storexdrfuncdecl(name, 1);
176ff49530fSBill Paul 				f_print(fout, "\n");
177ff49530fSBill Paul 			}
178ff49530fSBill Paul 		}
179ff49530fSBill Paul }
180ff49530fSBill Paul 
181ff49530fSBill Paul 
182526195adSJordan K. Hubbard static void
pstructdef(definition * def)183e390e3afSDavid Malone pstructdef(definition *def)
1844e115012SGarrett Wollman {
1854e115012SGarrett Wollman 	decl_list *l;
186e390e3afSDavid Malone 	const char *name = def->def_name;
1874e115012SGarrett Wollman 
1884e115012SGarrett Wollman 	f_print(fout, "struct %s {\n", name);
1894e115012SGarrett Wollman 	for (l = def->def.st.decls; l != NULL; l = l->next) {
190ff49530fSBill Paul 		pdeclaration(name, &l->decl, 1, ";\n");
1914e115012SGarrett Wollman 	}
1924e115012SGarrett Wollman 	f_print(fout, "};\n");
1934e115012SGarrett Wollman 	f_print(fout, "typedef struct %s %s;\n", name, name);
1944e115012SGarrett Wollman }
1954e115012SGarrett Wollman 
196526195adSJordan K. Hubbard static void
puniondef(definition * def)197ef636796SEd Schouten puniondef(definition *def)
1984e115012SGarrett Wollman {
1994e115012SGarrett Wollman 	case_list *l;
200e390e3afSDavid Malone 	const char *name = def->def_name;
2014e115012SGarrett Wollman 	declaration *decl;
2024e115012SGarrett Wollman 
2034e115012SGarrett Wollman 	f_print(fout, "struct %s {\n", name);
2044e115012SGarrett Wollman 	decl = &def->def.un.enum_decl;
2054e115012SGarrett Wollman 	if (streq(decl->type, "bool")) {
2064e115012SGarrett Wollman 		f_print(fout, "\tbool_t %s;\n", decl->name);
2074e115012SGarrett Wollman 	} else {
2084e115012SGarrett Wollman 		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
2094e115012SGarrett Wollman 	}
2104e115012SGarrett Wollman 	f_print(fout, "\tunion {\n");
2114e115012SGarrett Wollman 	for (l = def->def.un.cases; l != NULL; l = l->next) {
212ff49530fSBill Paul 	    if (l->contflag == 0)
213ff49530fSBill Paul 		pdeclaration(name, &l->case_decl, 2, ";\n");
2144e115012SGarrett Wollman 	}
2154e115012SGarrett Wollman 	decl = def->def.un.default_decl;
2164e115012SGarrett Wollman 	if (decl && !streq(decl->type, "void")) {
217ff49530fSBill Paul 		pdeclaration(name, decl, 2, ";\n");
2184e115012SGarrett Wollman 	}
2194e115012SGarrett Wollman 	f_print(fout, "\t} %s_u;\n", name);
2204e115012SGarrett Wollman 	f_print(fout, "};\n");
2214e115012SGarrett Wollman 	f_print(fout, "typedef struct %s %s;\n", name, name);
2224e115012SGarrett Wollman }
2234e115012SGarrett Wollman 
224526195adSJordan K. Hubbard static void
pdefine(const char * name,const char * num)225e390e3afSDavid Malone pdefine(const char *name, const char *num)
2264e115012SGarrett Wollman {
227ff49530fSBill Paul 	f_print(fout, "#define\t%s %s\n", name, num);
2284e115012SGarrett Wollman }
2294e115012SGarrett Wollman 
230526195adSJordan K. Hubbard static void
puldefine(const char * name,const char * num)231e390e3afSDavid Malone puldefine(const char *name, const char *num)
2324e115012SGarrett Wollman {
233ff49530fSBill Paul 	f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
2344e115012SGarrett Wollman }
2354e115012SGarrett Wollman 
236526195adSJordan K. Hubbard static int
define_printed(proc_list * stop,version_list * start)237e390e3afSDavid Malone define_printed(proc_list *stop, version_list *start)
2384e115012SGarrett Wollman {
2394e115012SGarrett Wollman 	version_list *vers;
2404e115012SGarrett Wollman 	proc_list *proc;
2414e115012SGarrett Wollman 
2424e115012SGarrett Wollman 	for (vers = start; vers != NULL; vers = vers->next) {
2434e115012SGarrett Wollman 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
2444e115012SGarrett Wollman 			if (proc == stop) {
2454e115012SGarrett Wollman 				return (0);
2464e115012SGarrett Wollman 			} else if (streq(proc->proc_name, stop->proc_name)) {
2474e115012SGarrett Wollman 				return (1);
2484e115012SGarrett Wollman 			}
2494e115012SGarrett Wollman 		}
2504e115012SGarrett Wollman 	}
2514e115012SGarrett Wollman 	abort();
2524e115012SGarrett Wollman 	/* NOTREACHED */
2534e115012SGarrett Wollman }
2544e115012SGarrett Wollman 
255526195adSJordan K. Hubbard static void
pfreeprocdef(const char * name,const char * vers)256e390e3afSDavid Malone pfreeprocdef(const char * name, const char *vers)
257ff49530fSBill Paul {
258ff49530fSBill Paul 	f_print(fout, "extern int ");
259ff49530fSBill Paul 	pvname(name, vers);
260ff49530fSBill Paul 	f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
261ff49530fSBill Paul }
2624e115012SGarrett Wollman 
263526195adSJordan K. Hubbard static void
pdispatch(const char * name,const char * vers)264e390e3afSDavid Malone pdispatch(const char * name, const char *vers)
2657e440a74SAlfred Perlstein {
2667e440a74SAlfred Perlstein 
2677e440a74SAlfred Perlstein 	f_print(fout, "void ");
2687e440a74SAlfred Perlstein 	pvname(name, vers);
269896cdc31SStefan Farfeleder 	f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
2707e440a74SAlfred Perlstein }
2717e440a74SAlfred Perlstein 
2727e440a74SAlfred Perlstein static void
pprogramdef(definition * def,int headeronly)273ec06b5e8SStefan Farfeleder pprogramdef(definition *def, int headeronly)
2744e115012SGarrett Wollman {
2754e115012SGarrett Wollman 	version_list *vers;
2764e115012SGarrett Wollman 	proc_list *proc;
277e390e3afSDavid Malone 	const char *ext;
278ff49530fSBill Paul 
279ff49530fSBill Paul 	pargdef(def);
2804e115012SGarrett Wollman 
2814e115012SGarrett Wollman 	puldefine(def->def_name, def->def.pr.prog_num);
2824e115012SGarrett Wollman 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
283ff49530fSBill Paul 		if (tblflag) {
284ff49530fSBill Paul 			f_print(fout,
285ff49530fSBill Paul 				"extern struct rpcgen_table %s_%s_table[];\n",
286ff49530fSBill Paul 				locase(def->def_name), vers->vers_num);
287ff49530fSBill Paul 			f_print(fout,
288ff49530fSBill Paul 				"extern %s_%s_nproc;\n",
289ff49530fSBill Paul 				locase(def->def_name), vers->vers_num);
290ff49530fSBill Paul 		}
2914e115012SGarrett Wollman 		puldefine(vers->vers_name, vers->vers_num);
292ff49530fSBill Paul 
29315df5e2dSStefan Farfeleder 		f_print(fout, "\n");
294ff49530fSBill Paul 		ext = "extern  ";
295ec06b5e8SStefan Farfeleder 		if (headeronly) {
2967e440a74SAlfred Perlstein 			f_print(fout, "%s", ext);
29715df5e2dSStefan Farfeleder 			pdispatch(def->def_name, vers->vers_num);
298ec06b5e8SStefan Farfeleder 		}
29915df5e2dSStefan Farfeleder 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
30015df5e2dSStefan Farfeleder 			if (!define_printed(proc, def->def.pr.versions)) {
30115df5e2dSStefan Farfeleder 				puldefine(proc->proc_name, proc->proc_num);
3024e115012SGarrett Wollman 			}
303ff49530fSBill Paul 			f_print(fout, "%s", ext);
30415df5e2dSStefan Farfeleder 			pprocdef(proc, vers, "CLIENT *", 0);
305ff49530fSBill Paul 			f_print(fout, "%s", ext);
30615df5e2dSStefan Farfeleder 			pprocdef(proc, vers, "struct svc_req *", 1);
307ff49530fSBill Paul 		}
30815df5e2dSStefan Farfeleder 		pfreeprocdef(def->def_name, vers->vers_num);
3094e115012SGarrett Wollman 	}
3104e115012SGarrett Wollman }
3114e115012SGarrett Wollman 
312526195adSJordan K. Hubbard static void
pprocdef(proc_list * proc,version_list * vp,const char * addargtype,int server_p)313e390e3afSDavid Malone pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
3144e115012SGarrett Wollman {
315ff49530fSBill Paul 	if (mtflag) {/* Print MT style stubs */
316ff49530fSBill Paul 		if (server_p)
317ff49530fSBill Paul 			f_print(fout, "bool_t ");
318ff49530fSBill Paul 		else
319ff49530fSBill Paul 			f_print(fout, "enum clnt_stat ");
3204e115012SGarrett Wollman 	} else {
321ff49530fSBill Paul 		ptype(proc->res_prefix, proc->res_type, 1);
322ff49530fSBill Paul 		f_print(fout, "* ");
3234e115012SGarrett Wollman 	}
324ff49530fSBill Paul 	if (server_p)
325ff49530fSBill Paul 		pvname_svc(proc->proc_name, vp->vers_num);
326ff49530fSBill Paul 	else
3274e115012SGarrett Wollman 		pvname(proc->proc_name, vp->vers_num);
328ff49530fSBill Paul 
329ff49530fSBill Paul 	parglist(proc, addargtype);
330ff49530fSBill Paul }
331ff49530fSBill Paul 
332ff49530fSBill Paul 
333ff49530fSBill Paul 
334ff49530fSBill Paul /* print out argument list of procedure */
335526195adSJordan K. Hubbard static void
parglist(proc_list * proc,const char * addargtype)336e390e3afSDavid Malone parglist(proc_list *proc, const char *addargtype)
337ff49530fSBill Paul {
338ff49530fSBill Paul 	decl_list *dl;
339ff49530fSBill Paul 
340ff49530fSBill Paul 	f_print(fout, "(");
341ff49530fSBill Paul 	if (proc->arg_num < 2 && newstyle &&
342ff49530fSBill Paul 	    streq(proc->args.decls->decl.type, "void")) {
343ff49530fSBill Paul 		/* 0 argument in new style:  do nothing*/
344ff49530fSBill Paul 	}
345ff49530fSBill Paul 	else {
346ff49530fSBill Paul 		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
347ff49530fSBill Paul 			ptype(dl->decl.prefix, dl->decl.type, 1);
348ff49530fSBill Paul 			if (!newstyle)
349ff49530fSBill Paul 				f_print(fout, "*");
350ff49530fSBill Paul 			/* old style passes by reference */
351ff49530fSBill Paul 			f_print(fout, ", ");
352ff49530fSBill Paul 		}
353ff49530fSBill Paul 	}
354ff49530fSBill Paul 
355ff49530fSBill Paul 	if (mtflag)  {
356ff49530fSBill Paul 		ptype(proc->res_prefix, proc->res_type, 1);
357ff49530fSBill Paul 		f_print(fout, "*, ");
358ff49530fSBill Paul 	}
359ff49530fSBill Paul 
360ff49530fSBill Paul 	f_print(fout, "%s);\n", addargtype);
361ff49530fSBill Paul 
3624e115012SGarrett Wollman }
3634e115012SGarrett Wollman 
364526195adSJordan K. Hubbard static void
penumdef(definition * def)365ef636796SEd Schouten penumdef(definition *def)
3664e115012SGarrett Wollman {
367e390e3afSDavid Malone 	const char *name = def->def_name;
3684e115012SGarrett Wollman 	enumval_list *l;
369e390e3afSDavid Malone 	const char *last = NULL;
3704e115012SGarrett Wollman 	int count = 0;
3714e115012SGarrett Wollman 
3724e115012SGarrett Wollman 	f_print(fout, "enum %s {\n", name);
3734e115012SGarrett Wollman 	for (l = def->def.en.vals; l != NULL; l = l->next) {
3744e115012SGarrett Wollman 		f_print(fout, "\t%s", l->name);
3754e115012SGarrett Wollman 		if (l->assignment) {
3764e115012SGarrett Wollman 			f_print(fout, " = %s", l->assignment);
3774e115012SGarrett Wollman 			last = l->assignment;
3784e115012SGarrett Wollman 			count = 1;
3794e115012SGarrett Wollman 		} else {
3804e115012SGarrett Wollman 			if (last == NULL) {
3814e115012SGarrett Wollman 				f_print(fout, " = %d", count++);
3824e115012SGarrett Wollman 			} else {
3834e115012SGarrett Wollman 				f_print(fout, " = %s + %d", last, count++);
3844e115012SGarrett Wollman 			}
3854e115012SGarrett Wollman 		}
386ff49530fSBill Paul 		if (l->next)
3874e115012SGarrett Wollman 			f_print(fout, ",\n");
388ff49530fSBill Paul 		else
389ff49530fSBill Paul 			f_print(fout, "\n");
3904e115012SGarrett Wollman 	}
3914e115012SGarrett Wollman 	f_print(fout, "};\n");
3924e115012SGarrett Wollman 	f_print(fout, "typedef enum %s %s;\n", name, name);
3934e115012SGarrett Wollman }
3944e115012SGarrett Wollman 
395526195adSJordan K. Hubbard static void
ptypedef(definition * def)396ef636796SEd Schouten ptypedef(definition *def)
3974e115012SGarrett Wollman {
398e390e3afSDavid Malone 	const char *name = def->def_name;
399e390e3afSDavid Malone 	const char *old = def->def.ty.old_type;
4004e115012SGarrett Wollman 	char prefix[8];	/* enough to contain "struct ", including NUL */
4014e115012SGarrett Wollman 	relation rel = def->def.ty.rel;
4024e115012SGarrett Wollman 
4034e115012SGarrett Wollman 
4044e115012SGarrett Wollman 	if (!streq(name, old)) {
4054e115012SGarrett Wollman 		if (streq(old, "string")) {
4064e115012SGarrett Wollman 			old = "char";
4074e115012SGarrett Wollman 			rel = REL_POINTER;
4084e115012SGarrett Wollman 		} else if (streq(old, "opaque")) {
4094e115012SGarrett Wollman 			old = "char";
4104e115012SGarrett Wollman 		} else if (streq(old, "bool")) {
4114e115012SGarrett Wollman 			old = "bool_t";
4124e115012SGarrett Wollman 		}
4134e115012SGarrett Wollman 		if (undefined2(old, name) && def->def.ty.old_prefix) {
4144e115012SGarrett Wollman 			s_print(prefix, "%s ", def->def.ty.old_prefix);
4154e115012SGarrett Wollman 		} else {
4164e115012SGarrett Wollman 			prefix[0] = 0;
4174e115012SGarrett Wollman 		}
4184e115012SGarrett Wollman 		f_print(fout, "typedef ");
4194e115012SGarrett Wollman 		switch (rel) {
4204e115012SGarrett Wollman 		case REL_ARRAY:
4214e115012SGarrett Wollman 			f_print(fout, "struct {\n");
4224e115012SGarrett Wollman 			f_print(fout, "\tu_int %s_len;\n", name);
4234e115012SGarrett Wollman 			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
4244e115012SGarrett Wollman 			f_print(fout, "} %s", name);
4254e115012SGarrett Wollman 			break;
4264e115012SGarrett Wollman 		case REL_POINTER:
4274e115012SGarrett Wollman 			f_print(fout, "%s%s *%s", prefix, old, name);
4284e115012SGarrett Wollman 			break;
4294e115012SGarrett Wollman 		case REL_VECTOR:
4304e115012SGarrett Wollman 			f_print(fout, "%s%s %s[%s]", prefix, old, name,
4314e115012SGarrett Wollman 				def->def.ty.array_max);
4324e115012SGarrett Wollman 			break;
4334e115012SGarrett Wollman 		case REL_ALIAS:
4344e115012SGarrett Wollman 			f_print(fout, "%s%s %s", prefix, old, name);
4354e115012SGarrett Wollman 			break;
4364e115012SGarrett Wollman 		}
4374e115012SGarrett Wollman 		f_print(fout, ";\n");
4384e115012SGarrett Wollman 	}
4394e115012SGarrett Wollman }
4404e115012SGarrett Wollman 
441526195adSJordan K. Hubbard void
pdeclaration(const char * name,declaration * dec,int tab,const char * separator)442e390e3afSDavid Malone pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
4434e115012SGarrett Wollman {
4444e115012SGarrett Wollman 	char buf[8];	/* enough to hold "struct ", include NUL */
445e390e3afSDavid Malone 	const char *prefix;
446e390e3afSDavid Malone 	const char *type;
4474e115012SGarrett Wollman 
4484e115012SGarrett Wollman 	if (streq(dec->type, "void")) {
4494e115012SGarrett Wollman 		return;
4504e115012SGarrett Wollman 	}
4514e115012SGarrett Wollman 	tabify(fout, tab);
4524e115012SGarrett Wollman 	if (streq(dec->type, name) && !dec->prefix) {
4534e115012SGarrett Wollman 		f_print(fout, "struct ");
4544e115012SGarrett Wollman 	}
4554e115012SGarrett Wollman 	if (streq(dec->type, "string")) {
4564e115012SGarrett Wollman 		f_print(fout, "char *%s", dec->name);
4574e115012SGarrett Wollman 	} else {
4584e115012SGarrett Wollman 		prefix = "";
4594e115012SGarrett Wollman 		if (streq(dec->type, "bool")) {
4604e115012SGarrett Wollman 			type = "bool_t";
4614e115012SGarrett Wollman 		} else if (streq(dec->type, "opaque")) {
4624e115012SGarrett Wollman 			type = "char";
4634e115012SGarrett Wollman 		} else {
4644e115012SGarrett Wollman 			if (dec->prefix) {
4654e115012SGarrett Wollman 				s_print(buf, "%s ", dec->prefix);
4664e115012SGarrett Wollman 				prefix = buf;
4674e115012SGarrett Wollman 			}
4684e115012SGarrett Wollman 			type = dec->type;
4694e115012SGarrett Wollman 		}
4704e115012SGarrett Wollman 		switch (dec->rel) {
4714e115012SGarrett Wollman 		case REL_ALIAS:
4724e115012SGarrett Wollman 			f_print(fout, "%s%s %s", prefix, type, dec->name);
4734e115012SGarrett Wollman 			break;
4744e115012SGarrett Wollman 		case REL_VECTOR:
4754e115012SGarrett Wollman 			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
4764e115012SGarrett Wollman 				dec->array_max);
4774e115012SGarrett Wollman 			break;
4784e115012SGarrett Wollman 		case REL_POINTER:
4794e115012SGarrett Wollman 			f_print(fout, "%s%s *%s", prefix, type, dec->name);
4804e115012SGarrett Wollman 			break;
4814e115012SGarrett Wollman 		case REL_ARRAY:
4824e115012SGarrett Wollman 			f_print(fout, "struct {\n");
4834e115012SGarrett Wollman 			tabify(fout, tab);
4844e115012SGarrett Wollman 			f_print(fout, "\tu_int %s_len;\n", dec->name);
4854e115012SGarrett Wollman 			tabify(fout, tab);
486ff49530fSBill Paul 			f_print(fout,
487ff49530fSBill Paul 				"\t%s%s *%s_val;\n", prefix, type, dec->name);
4884e115012SGarrett Wollman 			tabify(fout, tab);
4894e115012SGarrett Wollman 			f_print(fout, "} %s", dec->name);
4904e115012SGarrett Wollman 			break;
4914e115012SGarrett Wollman 		}
4924e115012SGarrett Wollman 	}
493*9bde021aSBen Laurie 	fputs(separator, fout);
4944e115012SGarrett Wollman }
4954e115012SGarrett Wollman 
496526195adSJordan K. Hubbard static int
undefined2(const char * type,const char * stop)497e390e3afSDavid Malone undefined2(const char *type, const char *stop)
4984e115012SGarrett Wollman {
4994e115012SGarrett Wollman 	list *l;
5004e115012SGarrett Wollman 	definition *def;
5014e115012SGarrett Wollman 
5024e115012SGarrett Wollman 	for (l = defined; l != NULL; l = l->next) {
5034e115012SGarrett Wollman 		def = (definition *) l->val;
5044e115012SGarrett Wollman 		if (def->def_kind != DEF_PROGRAM) {
5054e115012SGarrett Wollman 			if (streq(def->def_name, stop)) {
5064e115012SGarrett Wollman 				return (1);
5074e115012SGarrett Wollman 			} else if (streq(def->def_name, type)) {
5084e115012SGarrett Wollman 				return (0);
5094e115012SGarrett Wollman 			}
5104e115012SGarrett Wollman 		}
5114e115012SGarrett Wollman 	}
5124e115012SGarrett Wollman 	return (1);
5134e115012SGarrett Wollman }
514