rpc_hout.c revision 1897
1/* @(#)rpc_hout.c	2.1 88/08/01 4.0 RPCSRC */
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#ifndef lint
31/*static char sccsid[] = "from: @(#)rpc_hout.c 1.6 87/07/28 (C) 1987 SMI";*/
32static char rcsid[] = "$Id: rpc_hout.c,v 1.1 1993/09/13 23:20:14 jtc Exp $";
33#endif
34
35static int pconstdef(), pstructdef(), puniondef(), pdefine(), pprogramdef(),
36	   penumdef(), ptypedef(), pdeclaration(), undefined2();
37
38/*
39 * rpc_hout.c, Header file outputter for the RPC protocol compiler
40 * Copyright (C) 1987, Sun Microsystems, Inc.
41 */
42#include <stdio.h>
43#include <ctype.h>
44#include "rpc_util.h"
45#include "rpc_parse.h"
46
47
48/*
49 * Print the C-version of an xdr definition
50 */
51void
52print_datadef(def)
53	definition *def;
54{
55	if (def->def_kind != DEF_CONST) {
56		f_print(fout, "\n");
57	}
58	switch (def->def_kind) {
59	case DEF_STRUCT:
60		pstructdef(def);
61		break;
62	case DEF_UNION:
63		puniondef(def);
64		break;
65	case DEF_ENUM:
66		penumdef(def);
67		break;
68	case DEF_TYPEDEF:
69		ptypedef(def);
70		break;
71	case DEF_PROGRAM:
72		pprogramdef(def);
73		break;
74	case DEF_CONST:
75		pconstdef(def);
76		break;
77	}
78	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
79		f_print(fout, "bool_t xdr_%s();\n", def->def_name);
80	}
81	if (def->def_kind != DEF_CONST) {
82		f_print(fout, "\n");
83	}
84}
85
86static
87pconstdef(def)
88	definition *def;
89{
90	pdefine(def->def_name, def->def.co);
91}
92
93static
94pstructdef(def)
95	definition *def;
96{
97	decl_list *l;
98	char *name = def->def_name;
99
100	f_print(fout, "struct %s {\n", name);
101	for (l = def->def.st.decls; l != NULL; l = l->next) {
102		pdeclaration(name, &l->decl, 1);
103	}
104	f_print(fout, "};\n");
105	f_print(fout, "typedef struct %s %s;\n", name, name);
106}
107
108static
109puniondef(def)
110	definition *def;
111{
112	case_list *l;
113	char *name = def->def_name;
114	declaration *decl;
115
116	f_print(fout, "struct %s {\n", name);
117	decl = &def->def.un.enum_decl;
118	if (streq(decl->type, "bool")) {
119		f_print(fout, "\tbool_t %s;\n", decl->name);
120	} else {
121		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
122	}
123	f_print(fout, "\tunion {\n");
124	for (l = def->def.un.cases; l != NULL; l = l->next) {
125		pdeclaration(name, &l->case_decl, 2);
126	}
127	decl = def->def.un.default_decl;
128	if (decl && !streq(decl->type, "void")) {
129		pdeclaration(name, decl, 2);
130	}
131	f_print(fout, "\t} %s_u;\n", name);
132	f_print(fout, "};\n");
133	f_print(fout, "typedef struct %s %s;\n", name, name);
134}
135
136
137
138static
139pdefine(name, num)
140	char *name;
141	char *num;
142{
143	f_print(fout, "#define %s %s\n", name, num);
144}
145
146static
147puldefine(name, num)
148	char *name;
149	char *num;
150{
151	f_print(fout, "#define %s ((u_long)%s)\n", name, num);
152}
153
154static
155define_printed(stop, start)
156	proc_list *stop;
157	version_list *start;
158{
159	version_list *vers;
160	proc_list *proc;
161
162	for (vers = start; vers != NULL; vers = vers->next) {
163		for (proc = vers->procs; proc != NULL; proc = proc->next) {
164			if (proc == stop) {
165				return (0);
166			} else if (streq(proc->proc_name, stop->proc_name)) {
167				return (1);
168			}
169		}
170	}
171	abort();
172	/* NOTREACHED */
173}
174
175
176static
177pprogramdef(def)
178	definition *def;
179{
180	version_list *vers;
181	proc_list *proc;
182
183	puldefine(def->def_name, def->def.pr.prog_num);
184	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
185		puldefine(vers->vers_name, vers->vers_num);
186		for (proc = vers->procs; proc != NULL; proc = proc->next) {
187			if (!define_printed(proc, def->def.pr.versions)) {
188				puldefine(proc->proc_name, proc->proc_num);
189			}
190			pprocdef(proc, vers);
191		}
192	}
193}
194
195
196pprocdef(proc, vp)
197	proc_list *proc;
198	version_list *vp;
199{
200	f_print(fout, "extern ");
201	if (proc->res_prefix) {
202		if (streq(proc->res_prefix, "enum")) {
203			f_print(fout, "enum ");
204		} else {
205			f_print(fout, "struct ");
206		}
207	}
208	if (streq(proc->res_type, "bool")) {
209		f_print(fout, "bool_t *");
210	} else if (streq(proc->res_type, "string")) {
211		f_print(fout, "char **");
212	} else {
213		f_print(fout, "%s *", fixtype(proc->res_type));
214	}
215	pvname(proc->proc_name, vp->vers_num);
216	f_print(fout, "();\n");
217}
218
219static
220penumdef(def)
221	definition *def;
222{
223	char *name = def->def_name;
224	enumval_list *l;
225	char *last = NULL;
226	int count = 0;
227
228	f_print(fout, "enum %s {\n", name);
229	for (l = def->def.en.vals; l != NULL; l = l->next) {
230		f_print(fout, "\t%s", l->name);
231		if (l->assignment) {
232			f_print(fout, " = %s", l->assignment);
233			last = l->assignment;
234			count = 1;
235		} else {
236			if (last == NULL) {
237				f_print(fout, " = %d", count++);
238			} else {
239				f_print(fout, " = %s + %d", last, count++);
240			}
241		}
242		f_print(fout, ",\n");
243	}
244	f_print(fout, "};\n");
245	f_print(fout, "typedef enum %s %s;\n", name, name);
246}
247
248static
249ptypedef(def)
250	definition *def;
251{
252	char *name = def->def_name;
253	char *old = def->def.ty.old_type;
254	char prefix[8];	/* enough to contain "struct ", including NUL */
255	relation rel = def->def.ty.rel;
256
257
258	if (!streq(name, old)) {
259		if (streq(old, "string")) {
260			old = "char";
261			rel = REL_POINTER;
262		} else if (streq(old, "opaque")) {
263			old = "char";
264		} else if (streq(old, "bool")) {
265			old = "bool_t";
266		}
267		if (undefined2(old, name) && def->def.ty.old_prefix) {
268			s_print(prefix, "%s ", def->def.ty.old_prefix);
269		} else {
270			prefix[0] = 0;
271		}
272		f_print(fout, "typedef ");
273		switch (rel) {
274		case REL_ARRAY:
275			f_print(fout, "struct {\n");
276			f_print(fout, "\tu_int %s_len;\n", name);
277			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
278			f_print(fout, "} %s", name);
279			break;
280		case REL_POINTER:
281			f_print(fout, "%s%s *%s", prefix, old, name);
282			break;
283		case REL_VECTOR:
284			f_print(fout, "%s%s %s[%s]", prefix, old, name,
285				def->def.ty.array_max);
286			break;
287		case REL_ALIAS:
288 			f_print(fout, "%s%s %s", prefix, old, name);
289			break;
290		}
291		f_print(fout, ";\n");
292	}
293}
294
295
296static
297pdeclaration(name, dec, tab)
298	char *name;
299	declaration *dec;
300	int tab;
301{
302	char buf[8];	/* enough to hold "struct ", include NUL */
303	char *prefix;
304	char *type;
305
306	if (streq(dec->type, "void")) {
307		return;
308	}
309	tabify(fout, tab);
310	if (streq(dec->type, name) && !dec->prefix) {
311		f_print(fout, "struct ");
312	}
313	if (streq(dec->type, "string")) {
314		f_print(fout, "char *%s", dec->name);
315	} else {
316		prefix = "";
317		if (streq(dec->type, "bool")) {
318			type = "bool_t";
319		} else if (streq(dec->type, "opaque")) {
320			type = "char";
321		} else {
322			if (dec->prefix) {
323				s_print(buf, "%s ", dec->prefix);
324				prefix = buf;
325			}
326			type = dec->type;
327		}
328		switch (dec->rel) {
329		case REL_ALIAS:
330			f_print(fout, "%s%s %s", prefix, type, dec->name);
331			break;
332		case REL_VECTOR:
333			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
334				dec->array_max);
335			break;
336		case REL_POINTER:
337			f_print(fout, "%s%s *%s", prefix, type, dec->name);
338			break;
339		case REL_ARRAY:
340			f_print(fout, "struct {\n");
341			tabify(fout, tab);
342			f_print(fout, "\tu_int %s_len;\n", dec->name);
343			tabify(fout, tab);
344			f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
345			tabify(fout, tab);
346			f_print(fout, "} %s", dec->name);
347			break;
348		}
349	}
350	f_print(fout, ";\n");
351}
352
353
354
355static
356undefined2(type, stop)
357	char *type;
358	char *stop;
359{
360	list *l;
361	definition *def;
362
363	for (l = defined; l != NULL; l = l->next) {
364		def = (definition *) l->val;
365		if (def->def_kind != DEF_PROGRAM) {
366			if (streq(def->def_name, stop)) {
367				return (1);
368			} else if (streq(def->def_name, type)) {
369				return (0);
370			}
371		}
372	}
373	return (1);
374}
375