xref: /titanic_52/usr/src/cmd/rpcgen/rpc_util.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
29*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
30*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
33*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
34*7c478bd9Sstevel@tonic-gate  * contributors.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * rpc_util.c, Utility routines for the RPC protocol compiler
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate #include <stdio.h>
43*7c478bd9Sstevel@tonic-gate #include <ctype.h>
44*7c478bd9Sstevel@tonic-gate #include "rpc_scan.h"
45*7c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
46*7c478bd9Sstevel@tonic-gate #include "rpc_util.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	ARGEXT "argument"
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate char curline[MAXLINESIZE];	/* current read line */
51*7c478bd9Sstevel@tonic-gate char *where = curline;		/* current point in line */
52*7c478bd9Sstevel@tonic-gate int linenum = 0;		/* current line number */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate char *infilename;		/* input filename */
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #define	NFILES   15
57*7c478bd9Sstevel@tonic-gate char *outfiles[NFILES];		/* output file names */
58*7c478bd9Sstevel@tonic-gate int nfiles;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate FILE *fout;			/* file pointer of current output */
61*7c478bd9Sstevel@tonic-gate FILE *fin;			/* file pointer of current input */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate list *defined;			/* list of defined things */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate  * Reinitialize the world
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate reinitialize()
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	memset(curline, 0, MAXLINESIZE);
71*7c478bd9Sstevel@tonic-gate 	where = curline;
72*7c478bd9Sstevel@tonic-gate 	linenum = 0;
73*7c478bd9Sstevel@tonic-gate 	defined = NULL;
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * string equality
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate streq(a, b)
80*7c478bd9Sstevel@tonic-gate 	char *a;
81*7c478bd9Sstevel@tonic-gate 	char *b;
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	return (strcmp(a, b) == 0);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /*
87*7c478bd9Sstevel@tonic-gate  * find a value in a list
88*7c478bd9Sstevel@tonic-gate  */
89*7c478bd9Sstevel@tonic-gate definition *
90*7c478bd9Sstevel@tonic-gate findval(lst, val, cmp)
91*7c478bd9Sstevel@tonic-gate 	list *lst;
92*7c478bd9Sstevel@tonic-gate 	char *val;
93*7c478bd9Sstevel@tonic-gate 	int (*cmp) ();
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	for (; lst != NULL; lst = lst->next) {
97*7c478bd9Sstevel@tonic-gate 		if ((*cmp) (lst->val, val)) {
98*7c478bd9Sstevel@tonic-gate 			return (lst->val);
99*7c478bd9Sstevel@tonic-gate 		}
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	return (NULL);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * store a value in a list
106*7c478bd9Sstevel@tonic-gate  */
107*7c478bd9Sstevel@tonic-gate void
108*7c478bd9Sstevel@tonic-gate storeval(lstp, val)
109*7c478bd9Sstevel@tonic-gate 	list **lstp;
110*7c478bd9Sstevel@tonic-gate 	definition *val;
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	list **l;
113*7c478bd9Sstevel@tonic-gate 	list *lst;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
116*7c478bd9Sstevel@tonic-gate 	lst = ALLOC(list);
117*7c478bd9Sstevel@tonic-gate 	lst->val = val;
118*7c478bd9Sstevel@tonic-gate 	lst->next = NULL;
119*7c478bd9Sstevel@tonic-gate 	*l = lst;
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate static
123*7c478bd9Sstevel@tonic-gate findit(def, type)
124*7c478bd9Sstevel@tonic-gate 	definition *def;
125*7c478bd9Sstevel@tonic-gate 	char *type;
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	return (streq(def->def_name, type));
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate static char *
131*7c478bd9Sstevel@tonic-gate fixit(type, orig)
132*7c478bd9Sstevel@tonic-gate 	char *type;
133*7c478bd9Sstevel@tonic-gate 	char *orig;
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	definition *def;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	def = (definition *) FINDVAL(defined, type, findit);
138*7c478bd9Sstevel@tonic-gate 	if (def == NULL || def->def_kind != DEF_TYPEDEF) {
139*7c478bd9Sstevel@tonic-gate 		return (orig);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	switch (def->def.ty.rel) {
142*7c478bd9Sstevel@tonic-gate 	case REL_VECTOR:
143*7c478bd9Sstevel@tonic-gate 		if (streq(def->def.ty.old_type, "opaque"))
144*7c478bd9Sstevel@tonic-gate 			return ("char");
145*7c478bd9Sstevel@tonic-gate 		else
146*7c478bd9Sstevel@tonic-gate 			return (def->def.ty.old_type);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	case REL_ALIAS:
149*7c478bd9Sstevel@tonic-gate 		return (fixit(def->def.ty.old_type, orig));
150*7c478bd9Sstevel@tonic-gate 	default:
151*7c478bd9Sstevel@tonic-gate 		return (orig);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate char *
156*7c478bd9Sstevel@tonic-gate fixtype(type)
157*7c478bd9Sstevel@tonic-gate 	char *type;
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	return (fixit(type, type));
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate char *
163*7c478bd9Sstevel@tonic-gate stringfix(type)
164*7c478bd9Sstevel@tonic-gate 	char *type;
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	if (streq(type, "string")) {
167*7c478bd9Sstevel@tonic-gate 		return ("wrapstring");
168*7c478bd9Sstevel@tonic-gate 	} else {
169*7c478bd9Sstevel@tonic-gate 		return (type);
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate void
174*7c478bd9Sstevel@tonic-gate ptype(prefix, type, follow)
175*7c478bd9Sstevel@tonic-gate 	char *prefix;
176*7c478bd9Sstevel@tonic-gate 	char *type;
177*7c478bd9Sstevel@tonic-gate 	int follow;
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	if (prefix != NULL) {
180*7c478bd9Sstevel@tonic-gate 		if (streq(prefix, "enum")) {
181*7c478bd9Sstevel@tonic-gate 			f_print(fout, "enum ");
182*7c478bd9Sstevel@tonic-gate 		} else {
183*7c478bd9Sstevel@tonic-gate 			f_print(fout, "struct ");
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 	if (streq(type, "bool")) {
187*7c478bd9Sstevel@tonic-gate 		f_print(fout, "bool_t ");
188*7c478bd9Sstevel@tonic-gate 	} else if (streq(type, "string")) {
189*7c478bd9Sstevel@tonic-gate 		f_print(fout, "char *");
190*7c478bd9Sstevel@tonic-gate 	} else if (streq(type, "oneway")) {
191*7c478bd9Sstevel@tonic-gate 		f_print(fout, "void ");
192*7c478bd9Sstevel@tonic-gate 	} else {
193*7c478bd9Sstevel@tonic-gate 		f_print(fout, "%s ", follow ? fixtype(type) : type);
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate static
198*7c478bd9Sstevel@tonic-gate typedefed(def, type)
199*7c478bd9Sstevel@tonic-gate 	definition *def;
200*7c478bd9Sstevel@tonic-gate 	char *type;
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
203*7c478bd9Sstevel@tonic-gate 		return (0);
204*7c478bd9Sstevel@tonic-gate 	} else {
205*7c478bd9Sstevel@tonic-gate 		return (streq(def->def_name, type));
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate isvectordef(type, rel)
210*7c478bd9Sstevel@tonic-gate 	char *type;
211*7c478bd9Sstevel@tonic-gate 	relation rel;
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	definition *def;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	for (;;) {
216*7c478bd9Sstevel@tonic-gate 		switch (rel) {
217*7c478bd9Sstevel@tonic-gate 		case REL_VECTOR:
218*7c478bd9Sstevel@tonic-gate 			return (!streq(type, "string"));
219*7c478bd9Sstevel@tonic-gate 		case REL_ARRAY:
220*7c478bd9Sstevel@tonic-gate 			return (0);
221*7c478bd9Sstevel@tonic-gate 		case REL_POINTER:
222*7c478bd9Sstevel@tonic-gate 			return (0);
223*7c478bd9Sstevel@tonic-gate 		case REL_ALIAS:
224*7c478bd9Sstevel@tonic-gate 			def = (definition *) FINDVAL(defined, type, typedefed);
225*7c478bd9Sstevel@tonic-gate 			if (def == NULL)
226*7c478bd9Sstevel@tonic-gate 				return (0);
227*7c478bd9Sstevel@tonic-gate 			type = def->def.ty.old_type;
228*7c478bd9Sstevel@tonic-gate 			rel = def->def.ty.rel;
229*7c478bd9Sstevel@tonic-gate 		}
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate char *
234*7c478bd9Sstevel@tonic-gate locase(str)
235*7c478bd9Sstevel@tonic-gate 	char *str;
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate 	char c;
238*7c478bd9Sstevel@tonic-gate 	static char buf[100];
239*7c478bd9Sstevel@tonic-gate 	char *p = buf;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	while (c = *str++) {
242*7c478bd9Sstevel@tonic-gate 		*p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate 	*p = 0;
245*7c478bd9Sstevel@tonic-gate 	return (buf);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate void
249*7c478bd9Sstevel@tonic-gate pvname_svc(pname, vnum)
250*7c478bd9Sstevel@tonic-gate 	char *pname;
251*7c478bd9Sstevel@tonic-gate 	char *vnum;
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate 	f_print(fout, "%s_%s_svc", locase(pname), vnum);
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate void
257*7c478bd9Sstevel@tonic-gate pvname(pname, vnum)
258*7c478bd9Sstevel@tonic-gate 	char *pname;
259*7c478bd9Sstevel@tonic-gate 	char *vnum;
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	f_print(fout, "%s_%s", locase(pname), vnum);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate /*
265*7c478bd9Sstevel@tonic-gate  * print a useful (?) error message, and then die
266*7c478bd9Sstevel@tonic-gate  */
267*7c478bd9Sstevel@tonic-gate void
268*7c478bd9Sstevel@tonic-gate error(msg)
269*7c478bd9Sstevel@tonic-gate 	char *msg;
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate 	printwhere();
272*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "%s, line %d: ", infilename, linenum);
273*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "%s\n", msg);
274*7c478bd9Sstevel@tonic-gate 	crash();
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate /*
278*7c478bd9Sstevel@tonic-gate  * Something went wrong, unlink any files that we may have created and then
279*7c478bd9Sstevel@tonic-gate  * die.
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate crash()
282*7c478bd9Sstevel@tonic-gate {
283*7c478bd9Sstevel@tonic-gate 	int i;
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfiles; i++) {
286*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfiles[i]);
287*7c478bd9Sstevel@tonic-gate 	}
288*7c478bd9Sstevel@tonic-gate 	exit(1);
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate void
292*7c478bd9Sstevel@tonic-gate record_open(file)
293*7c478bd9Sstevel@tonic-gate 	char *file;
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate 	if (nfiles < NFILES) {
296*7c478bd9Sstevel@tonic-gate 		outfiles[nfiles++] = file;
297*7c478bd9Sstevel@tonic-gate 	} else {
298*7c478bd9Sstevel@tonic-gate 		f_print(stderr, "too many files!\n");
299*7c478bd9Sstevel@tonic-gate 		crash();
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate static char expectbuf[100];
304*7c478bd9Sstevel@tonic-gate static char *toktostr();
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate /*
307*7c478bd9Sstevel@tonic-gate  * error, token encountered was not the expected one
308*7c478bd9Sstevel@tonic-gate  */
309*7c478bd9Sstevel@tonic-gate void
310*7c478bd9Sstevel@tonic-gate expected1(exp1)
311*7c478bd9Sstevel@tonic-gate 	tok_kind exp1;
312*7c478bd9Sstevel@tonic-gate {
313*7c478bd9Sstevel@tonic-gate 	s_print(expectbuf, "expected '%s'",
314*7c478bd9Sstevel@tonic-gate 		toktostr(exp1));
315*7c478bd9Sstevel@tonic-gate 	error(expectbuf);
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate /*
319*7c478bd9Sstevel@tonic-gate  * error, token encountered was not one of two expected ones
320*7c478bd9Sstevel@tonic-gate  */
321*7c478bd9Sstevel@tonic-gate void
322*7c478bd9Sstevel@tonic-gate expected2(exp1, exp2)
323*7c478bd9Sstevel@tonic-gate 	tok_kind exp1, exp2;
324*7c478bd9Sstevel@tonic-gate {
325*7c478bd9Sstevel@tonic-gate 	s_print(expectbuf, "expected '%s' or '%s'",
326*7c478bd9Sstevel@tonic-gate 		toktostr(exp1),
327*7c478bd9Sstevel@tonic-gate 		toktostr(exp2));
328*7c478bd9Sstevel@tonic-gate 	error(expectbuf);
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate /*
332*7c478bd9Sstevel@tonic-gate  * error, token encountered was not one of 3 expected ones
333*7c478bd9Sstevel@tonic-gate  */
334*7c478bd9Sstevel@tonic-gate void
335*7c478bd9Sstevel@tonic-gate expected3(exp1, exp2, exp3)
336*7c478bd9Sstevel@tonic-gate 	tok_kind exp1, exp2, exp3;
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 	s_print(expectbuf, "expected '%s', '%s' or '%s'",
339*7c478bd9Sstevel@tonic-gate 		toktostr(exp1),
340*7c478bd9Sstevel@tonic-gate 		toktostr(exp2),
341*7c478bd9Sstevel@tonic-gate 		toktostr(exp3));
342*7c478bd9Sstevel@tonic-gate 	error(expectbuf);
343*7c478bd9Sstevel@tonic-gate }
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate void
346*7c478bd9Sstevel@tonic-gate tabify(f, tab)
347*7c478bd9Sstevel@tonic-gate 	FILE *f;
348*7c478bd9Sstevel@tonic-gate 	int tab;
349*7c478bd9Sstevel@tonic-gate {
350*7c478bd9Sstevel@tonic-gate 	while (tab--) {
351*7c478bd9Sstevel@tonic-gate 		(void) fputc('\t', f);
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate static token tokstrings[] = {
357*7c478bd9Sstevel@tonic-gate 			{TOK_IDENT, "identifier"},
358*7c478bd9Sstevel@tonic-gate 			{TOK_CONST, "const"},
359*7c478bd9Sstevel@tonic-gate 			{TOK_RPAREN, ")"},
360*7c478bd9Sstevel@tonic-gate 			{TOK_LPAREN, "("},
361*7c478bd9Sstevel@tonic-gate 			{TOK_RBRACE, "}"},
362*7c478bd9Sstevel@tonic-gate 			{TOK_LBRACE, "{"},
363*7c478bd9Sstevel@tonic-gate 			{TOK_LBRACKET, "["},
364*7c478bd9Sstevel@tonic-gate 			{TOK_RBRACKET, "]"},
365*7c478bd9Sstevel@tonic-gate 			{TOK_STAR, "*"},
366*7c478bd9Sstevel@tonic-gate 			{TOK_COMMA, ","},
367*7c478bd9Sstevel@tonic-gate 			{TOK_EQUAL, "="},
368*7c478bd9Sstevel@tonic-gate 			{TOK_COLON, ":"},
369*7c478bd9Sstevel@tonic-gate 			{TOK_SEMICOLON, ";"},
370*7c478bd9Sstevel@tonic-gate 			{TOK_UNION, "union"},
371*7c478bd9Sstevel@tonic-gate 			{TOK_STRUCT, "struct"},
372*7c478bd9Sstevel@tonic-gate 			{TOK_SWITCH, "switch"},
373*7c478bd9Sstevel@tonic-gate 			{TOK_CASE, "case"},
374*7c478bd9Sstevel@tonic-gate 			{TOK_DEFAULT, "default"},
375*7c478bd9Sstevel@tonic-gate 			{TOK_ENUM, "enum"},
376*7c478bd9Sstevel@tonic-gate 			{TOK_TYPEDEF, "typedef"},
377*7c478bd9Sstevel@tonic-gate 			{TOK_INT, "int"},
378*7c478bd9Sstevel@tonic-gate 			{TOK_SHORT, "short"},
379*7c478bd9Sstevel@tonic-gate 			{TOK_LONG, "long"},
380*7c478bd9Sstevel@tonic-gate 			{TOK_UNSIGNED, "unsigned"},
381*7c478bd9Sstevel@tonic-gate 			{TOK_DOUBLE, "double"},
382*7c478bd9Sstevel@tonic-gate 			{TOK_FLOAT, "float"},
383*7c478bd9Sstevel@tonic-gate 			{TOK_CHAR, "char"},
384*7c478bd9Sstevel@tonic-gate 			{TOK_STRING, "string"},
385*7c478bd9Sstevel@tonic-gate 			{TOK_OPAQUE, "opaque"},
386*7c478bd9Sstevel@tonic-gate 			{TOK_BOOL, "bool"},
387*7c478bd9Sstevel@tonic-gate 			{TOK_VOID, "void"},
388*7c478bd9Sstevel@tonic-gate 			{TOK_PROGRAM, "program"},
389*7c478bd9Sstevel@tonic-gate 			{TOK_VERSION, "version"},
390*7c478bd9Sstevel@tonic-gate 			{TOK_EOF, "??????"}
391*7c478bd9Sstevel@tonic-gate };
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate static char *
394*7c478bd9Sstevel@tonic-gate toktostr(kind)
395*7c478bd9Sstevel@tonic-gate 	tok_kind kind;
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate 	token *sp;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
400*7c478bd9Sstevel@tonic-gate 	return (sp->str);
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate static
404*7c478bd9Sstevel@tonic-gate printbuf()
405*7c478bd9Sstevel@tonic-gate {
406*7c478bd9Sstevel@tonic-gate 	char c;
407*7c478bd9Sstevel@tonic-gate 	int i;
408*7c478bd9Sstevel@tonic-gate 	int cnt;
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate #	define TABSIZE 4
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	for (i = 0; c = curline[i]; i++) {
413*7c478bd9Sstevel@tonic-gate 		if (c == '\t') {
414*7c478bd9Sstevel@tonic-gate 			cnt = 8 - (i % TABSIZE);
415*7c478bd9Sstevel@tonic-gate 			c = ' ';
416*7c478bd9Sstevel@tonic-gate 		} else {
417*7c478bd9Sstevel@tonic-gate 			cnt = 1;
418*7c478bd9Sstevel@tonic-gate 		}
419*7c478bd9Sstevel@tonic-gate 		while (cnt--) {
420*7c478bd9Sstevel@tonic-gate 			(void) fputc(c, stderr);
421*7c478bd9Sstevel@tonic-gate 		}
422*7c478bd9Sstevel@tonic-gate 	}
423*7c478bd9Sstevel@tonic-gate }
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate static
426*7c478bd9Sstevel@tonic-gate printwhere()
427*7c478bd9Sstevel@tonic-gate {
428*7c478bd9Sstevel@tonic-gate 	int i;
429*7c478bd9Sstevel@tonic-gate 	char c;
430*7c478bd9Sstevel@tonic-gate 	int cnt;
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	printbuf();
433*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < where - curline; i++) {
434*7c478bd9Sstevel@tonic-gate 		c = curline[i];
435*7c478bd9Sstevel@tonic-gate 		if (c == '\t') {
436*7c478bd9Sstevel@tonic-gate 			cnt = 8 - (i % TABSIZE);
437*7c478bd9Sstevel@tonic-gate 		} else {
438*7c478bd9Sstevel@tonic-gate 			cnt = 1;
439*7c478bd9Sstevel@tonic-gate 		}
440*7c478bd9Sstevel@tonic-gate 		while (cnt--) {
441*7c478bd9Sstevel@tonic-gate 			(void) fputc('^', stderr);
442*7c478bd9Sstevel@tonic-gate 		}
443*7c478bd9Sstevel@tonic-gate 	}
444*7c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stderr);
445*7c478bd9Sstevel@tonic-gate }
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate char *
448*7c478bd9Sstevel@tonic-gate make_argname(pname, vname)
449*7c478bd9Sstevel@tonic-gate     char *pname;
450*7c478bd9Sstevel@tonic-gate     char *vname;
451*7c478bd9Sstevel@tonic-gate {
452*7c478bd9Sstevel@tonic-gate 	char *name;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 	name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
455*7c478bd9Sstevel@tonic-gate 	if (!name) {
456*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "failed in malloc");
457*7c478bd9Sstevel@tonic-gate 		exit(1);
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 	sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
460*7c478bd9Sstevel@tonic-gate 	return (name);
461*7c478bd9Sstevel@tonic-gate }
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate bas_type *typ_list_h;
464*7c478bd9Sstevel@tonic-gate bas_type *typ_list_t;
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate add_type(len, type)
467*7c478bd9Sstevel@tonic-gate int len;
468*7c478bd9Sstevel@tonic-gate char *type;
469*7c478bd9Sstevel@tonic-gate {
470*7c478bd9Sstevel@tonic-gate 	bas_type *ptr;
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	if ((ptr = (bas_type *) malloc(sizeof (bas_type))) ==
473*7c478bd9Sstevel@tonic-gate 	    (bas_type *)NULL) {
474*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "failed in malloc");
475*7c478bd9Sstevel@tonic-gate 		exit(1);
476*7c478bd9Sstevel@tonic-gate 	}
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	ptr->name = type;
479*7c478bd9Sstevel@tonic-gate 	ptr->length = len;
480*7c478bd9Sstevel@tonic-gate 	ptr->next = NULL;
481*7c478bd9Sstevel@tonic-gate 	if (typ_list_t == NULL)
482*7c478bd9Sstevel@tonic-gate 	{
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 		typ_list_t = ptr;
485*7c478bd9Sstevel@tonic-gate 		typ_list_h = ptr;
486*7c478bd9Sstevel@tonic-gate 	}
487*7c478bd9Sstevel@tonic-gate 	else
488*7c478bd9Sstevel@tonic-gate 	{
489*7c478bd9Sstevel@tonic-gate 		typ_list_t->next = ptr;
490*7c478bd9Sstevel@tonic-gate 		typ_list_t = ptr;
491*7c478bd9Sstevel@tonic-gate 	};
492*7c478bd9Sstevel@tonic-gate }
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate bas_type *find_type(type)
496*7c478bd9Sstevel@tonic-gate char *type;
497*7c478bd9Sstevel@tonic-gate {
498*7c478bd9Sstevel@tonic-gate 	bas_type * ptr;
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 	ptr = typ_list_h;
501*7c478bd9Sstevel@tonic-gate 	while (ptr != NULL)
502*7c478bd9Sstevel@tonic-gate 	{
503*7c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->name, type) == 0)
504*7c478bd9Sstevel@tonic-gate 			return (ptr);
505*7c478bd9Sstevel@tonic-gate 		else
506*7c478bd9Sstevel@tonic-gate 			ptr = ptr->next;
507*7c478bd9Sstevel@tonic-gate 	};
508*7c478bd9Sstevel@tonic-gate 	return (NULL);
509*7c478bd9Sstevel@tonic-gate }
510