xref: /illumos-gate/usr/src/cmd/rpcgen/rpc_util.c (revision a2f144d1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a2f144d1SJordan Brown  * Common Development and Distribution License (the "License").
6*a2f144d1SJordan Brown  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2061961e0fSrobinson  */
2161961e0fSrobinson 
2261961e0fSrobinson /*
23*a2f144d1SJordan Brown  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * rpc_util.c, Utility routines for the RPC protocol compiler
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
4261961e0fSrobinson #include <stdlib.h>
4361961e0fSrobinson #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <ctype.h>
4561961e0fSrobinson #include <string.h>
467c478bd9Sstevel@tonic-gate #include "rpc_scan.h"
477c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
487c478bd9Sstevel@tonic-gate #include "rpc_util.h"
497c478bd9Sstevel@tonic-gate 
5061961e0fSrobinson extern void crash(void);
5161961e0fSrobinson 
5261961e0fSrobinson static void printwhere(void);
5361961e0fSrobinson 
547c478bd9Sstevel@tonic-gate #define	ARGEXT "argument"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate char curline[MAXLINESIZE];	/* current read line */
577c478bd9Sstevel@tonic-gate char *where = curline;		/* current point in line */
587c478bd9Sstevel@tonic-gate int linenum = 0;		/* current line number */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate char *infilename;		/* input filename */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	NFILES   15
637c478bd9Sstevel@tonic-gate char *outfiles[NFILES];		/* output file names */
647c478bd9Sstevel@tonic-gate int nfiles;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate FILE *fout;			/* file pointer of current output */
677c478bd9Sstevel@tonic-gate FILE *fin;			/* file pointer of current input */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate list *defined;			/* list of defined things */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Reinitialize the world
737c478bd9Sstevel@tonic-gate  */
7461961e0fSrobinson void
reinitialize(void)7561961e0fSrobinson reinitialize(void)
767c478bd9Sstevel@tonic-gate {
7761961e0fSrobinson 	(void) memset(curline, 0, MAXLINESIZE);
787c478bd9Sstevel@tonic-gate 	where = curline;
797c478bd9Sstevel@tonic-gate 	linenum = 0;
807c478bd9Sstevel@tonic-gate 	defined = NULL;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * string equality
857c478bd9Sstevel@tonic-gate  */
8661961e0fSrobinson int
streq(char * a,char * b)8761961e0fSrobinson streq(char *a, char *b)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	return (strcmp(a, b) == 0);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * find a value in a list
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate definition *
findval(list * lst,char * val,int (* cmp)())9661961e0fSrobinson findval(list *lst, char *val, int (*cmp)())
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	for (; lst != NULL; lst = lst->next) {
9961961e0fSrobinson 		if ((*cmp) (lst->val, val))
1007c478bd9Sstevel@tonic-gate 			return (lst->val);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	return (NULL);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * store a value in a list
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate void
storeval(list ** lstp,definition * val)10961961e0fSrobinson storeval(list **lstp, definition *val)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	list **l;
1127c478bd9Sstevel@tonic-gate 	list *lst;
1137c478bd9Sstevel@tonic-gate 
114*a2f144d1SJordan Brown 	for (l = lstp; *l != NULL; l = (list **)&(*l)->next)
115*a2f144d1SJordan Brown 		/* LOOP */;
11661961e0fSrobinson 	lst = calloc(1, sizeof (list));
1177c478bd9Sstevel@tonic-gate 	lst->val = val;
1187c478bd9Sstevel@tonic-gate 	lst->next = NULL;
1197c478bd9Sstevel@tonic-gate 	*l = lst;
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
12261961e0fSrobinson static int
findit(definition * def,char * type)12361961e0fSrobinson findit(definition *def, char *type)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	return (streq(def->def_name, type));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static char *
fixit(char * type,char * orig)12961961e0fSrobinson fixit(char *type, char *orig)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	definition *def;
1327c478bd9Sstevel@tonic-gate 
13361961e0fSrobinson 	def = (definition *)FINDVAL(defined, type, findit);
13461961e0fSrobinson 	if (def == NULL || def->def_kind != DEF_TYPEDEF)
1357c478bd9Sstevel@tonic-gate 		return (orig);
1367c478bd9Sstevel@tonic-gate 	switch (def->def.ty.rel) {
1377c478bd9Sstevel@tonic-gate 	case REL_VECTOR:
1387c478bd9Sstevel@tonic-gate 		if (streq(def->def.ty.old_type, "opaque"))
1397c478bd9Sstevel@tonic-gate 			return ("char");
14061961e0fSrobinson 		return (def->def.ty.old_type);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	case REL_ALIAS:
1437c478bd9Sstevel@tonic-gate 		return (fixit(def->def.ty.old_type, orig));
1447c478bd9Sstevel@tonic-gate 	default:
1457c478bd9Sstevel@tonic-gate 		return (orig);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate char *
fixtype(char * type)15061961e0fSrobinson fixtype(char *type)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	return (fixit(type, type));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate char *
stringfix(char * type)15661961e0fSrobinson stringfix(char *type)
1577c478bd9Sstevel@tonic-gate {
15861961e0fSrobinson 	if (streq(type, "string"))
1597c478bd9Sstevel@tonic-gate 		return ("wrapstring");
16061961e0fSrobinson 	return (type);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate void
ptype(char * prefix,char * type,int follow)16461961e0fSrobinson ptype(char *prefix, char *type, int follow)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	if (prefix != NULL) {
1677c478bd9Sstevel@tonic-gate 		if (streq(prefix, "enum")) {
1687c478bd9Sstevel@tonic-gate 			f_print(fout, "enum ");
1697c478bd9Sstevel@tonic-gate 		} else {
1707c478bd9Sstevel@tonic-gate 			f_print(fout, "struct ");
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	if (streq(type, "bool")) {
1747c478bd9Sstevel@tonic-gate 		f_print(fout, "bool_t ");
1757c478bd9Sstevel@tonic-gate 	} else if (streq(type, "string")) {
1767c478bd9Sstevel@tonic-gate 		f_print(fout, "char *");
1777c478bd9Sstevel@tonic-gate 	} else if (streq(type, "oneway")) {
1787c478bd9Sstevel@tonic-gate 		f_print(fout, "void ");
1797c478bd9Sstevel@tonic-gate 	} else {
1807c478bd9Sstevel@tonic-gate 		f_print(fout, "%s ", follow ? fixtype(type) : type);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
18461961e0fSrobinson static int
typedefed(definition * def,char * type)18561961e0fSrobinson typedefed(definition *def, char *type)
1867c478bd9Sstevel@tonic-gate {
18761961e0fSrobinson 	if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
1887c478bd9Sstevel@tonic-gate 		return (0);
18961961e0fSrobinson 	return (streq(def->def_name, type));
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
19261961e0fSrobinson int
isvectordef(char * type,relation rel)19361961e0fSrobinson isvectordef(char *type, relation rel)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	definition *def;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	for (;;) {
1987c478bd9Sstevel@tonic-gate 		switch (rel) {
1997c478bd9Sstevel@tonic-gate 		case REL_VECTOR:
2007c478bd9Sstevel@tonic-gate 			return (!streq(type, "string"));
2017c478bd9Sstevel@tonic-gate 		case REL_ARRAY:
2027c478bd9Sstevel@tonic-gate 			return (0);
2037c478bd9Sstevel@tonic-gate 		case REL_POINTER:
2047c478bd9Sstevel@tonic-gate 			return (0);
2057c478bd9Sstevel@tonic-gate 		case REL_ALIAS:
20661961e0fSrobinson 			def = (definition *)FINDVAL(defined, type, typedefed);
2077c478bd9Sstevel@tonic-gate 			if (def == NULL)
2087c478bd9Sstevel@tonic-gate 				return (0);
2097c478bd9Sstevel@tonic-gate 			type = def->def.ty.old_type;
2107c478bd9Sstevel@tonic-gate 			rel = def->def.ty.rel;
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate char *
locase(char * str)21661961e0fSrobinson locase(char *str)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	char c;
2197c478bd9Sstevel@tonic-gate 	static char buf[100];
2207c478bd9Sstevel@tonic-gate 	char *p = buf;
2217c478bd9Sstevel@tonic-gate 
22261961e0fSrobinson 	while (c = *str++)
2237c478bd9Sstevel@tonic-gate 		*p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
2247c478bd9Sstevel@tonic-gate 	*p = 0;
2257c478bd9Sstevel@tonic-gate 	return (buf);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate void
pvname_svc(char * pname,char * vnum)22961961e0fSrobinson pvname_svc(char *pname, char *vnum)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	f_print(fout, "%s_%s_svc", locase(pname), vnum);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate void
pvname(char * pname,char * vnum)23561961e0fSrobinson pvname(char *pname, char *vnum)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	f_print(fout, "%s_%s", locase(pname), vnum);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * print a useful (?) error message, and then die
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate void
error(char * msg)24461961e0fSrobinson error(char *msg)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	printwhere();
2477c478bd9Sstevel@tonic-gate 	f_print(stderr, "%s, line %d: ", infilename, linenum);
2487c478bd9Sstevel@tonic-gate 	f_print(stderr, "%s\n", msg);
2497c478bd9Sstevel@tonic-gate 	crash();
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * Something went wrong, unlink any files that we may have created and then
2547c478bd9Sstevel@tonic-gate  * die.
2557c478bd9Sstevel@tonic-gate  */
25661961e0fSrobinson void
crash(void)25761961e0fSrobinson crash(void)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	int i;
2607c478bd9Sstevel@tonic-gate 
26161961e0fSrobinson 	for (i = 0; i < nfiles; i++)
2627c478bd9Sstevel@tonic-gate 		(void) unlink(outfiles[i]);
2637c478bd9Sstevel@tonic-gate 	exit(1);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate void
record_open(char * file)26761961e0fSrobinson record_open(char *file)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	if (nfiles < NFILES) {
2707c478bd9Sstevel@tonic-gate 		outfiles[nfiles++] = file;
27161961e0fSrobinson 		return;
2727c478bd9Sstevel@tonic-gate 	}
27361961e0fSrobinson 	f_print(stderr, "too many files!\n");
27461961e0fSrobinson 	crash();
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static char expectbuf[100];
2787c478bd9Sstevel@tonic-gate static char *toktostr();
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * error, token encountered was not the expected one
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate void
expected1(tok_kind exp1)28461961e0fSrobinson expected1(tok_kind exp1)
2857c478bd9Sstevel@tonic-gate {
28661961e0fSrobinson 	(void) snprintf(expectbuf,
287*a2f144d1SJordan Brown 	    sizeof (expectbuf), "expected '%s'", toktostr(exp1));
2887c478bd9Sstevel@tonic-gate 	error(expectbuf);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * error, token encountered was not one of two expected ones
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate void
expected2(tok_kind exp1,tok_kind exp2)29561961e0fSrobinson expected2(tok_kind exp1, tok_kind exp2)
2967c478bd9Sstevel@tonic-gate {
29761961e0fSrobinson 	(void) snprintf(expectbuf,
298*a2f144d1SJordan Brown 	    sizeof (expectbuf), "expected '%s' or '%s'", toktostr(exp1),
299*a2f144d1SJordan Brown 	    toktostr(exp2));
3007c478bd9Sstevel@tonic-gate 	error(expectbuf);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * error, token encountered was not one of 3 expected ones
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate void
expected3(tok_kind exp1,tok_kind exp2,tok_kind exp3)30761961e0fSrobinson expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
3087c478bd9Sstevel@tonic-gate {
30961961e0fSrobinson 	(void) snprintf(expectbuf,
310*a2f144d1SJordan Brown 	    sizeof (expectbuf), "expected '%s', '%s' or '%s'",
311*a2f144d1SJordan Brown 	    toktostr(exp1), toktostr(exp2), toktostr(exp3));
3127c478bd9Sstevel@tonic-gate 	error(expectbuf);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate void
tabify(FILE * f,int tab)31661961e0fSrobinson tabify(FILE *f, int tab)
3177c478bd9Sstevel@tonic-gate {
31861961e0fSrobinson 	while (tab--)
3197c478bd9Sstevel@tonic-gate 		(void) fputc('\t', f);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate static token tokstrings[] = {
3237c478bd9Sstevel@tonic-gate 			{TOK_IDENT, "identifier"},
3247c478bd9Sstevel@tonic-gate 			{TOK_CONST, "const"},
3257c478bd9Sstevel@tonic-gate 			{TOK_RPAREN, ")"},
3267c478bd9Sstevel@tonic-gate 			{TOK_LPAREN, "("},
3277c478bd9Sstevel@tonic-gate 			{TOK_RBRACE, "}"},
3287c478bd9Sstevel@tonic-gate 			{TOK_LBRACE, "{"},
3297c478bd9Sstevel@tonic-gate 			{TOK_LBRACKET, "["},
3307c478bd9Sstevel@tonic-gate 			{TOK_RBRACKET, "]"},
3317c478bd9Sstevel@tonic-gate 			{TOK_STAR, "*"},
3327c478bd9Sstevel@tonic-gate 			{TOK_COMMA, ","},
3337c478bd9Sstevel@tonic-gate 			{TOK_EQUAL, "="},
3347c478bd9Sstevel@tonic-gate 			{TOK_COLON, ":"},
3357c478bd9Sstevel@tonic-gate 			{TOK_SEMICOLON, ";"},
3367c478bd9Sstevel@tonic-gate 			{TOK_UNION, "union"},
3377c478bd9Sstevel@tonic-gate 			{TOK_STRUCT, "struct"},
3387c478bd9Sstevel@tonic-gate 			{TOK_SWITCH, "switch"},
3397c478bd9Sstevel@tonic-gate 			{TOK_CASE, "case"},
3407c478bd9Sstevel@tonic-gate 			{TOK_DEFAULT, "default"},
3417c478bd9Sstevel@tonic-gate 			{TOK_ENUM, "enum"},
3427c478bd9Sstevel@tonic-gate 			{TOK_TYPEDEF, "typedef"},
3437c478bd9Sstevel@tonic-gate 			{TOK_INT, "int"},
3447c478bd9Sstevel@tonic-gate 			{TOK_SHORT, "short"},
3457c478bd9Sstevel@tonic-gate 			{TOK_LONG, "long"},
3467c478bd9Sstevel@tonic-gate 			{TOK_UNSIGNED, "unsigned"},
3477c478bd9Sstevel@tonic-gate 			{TOK_DOUBLE, "double"},
3487c478bd9Sstevel@tonic-gate 			{TOK_FLOAT, "float"},
3497c478bd9Sstevel@tonic-gate 			{TOK_CHAR, "char"},
3507c478bd9Sstevel@tonic-gate 			{TOK_STRING, "string"},
3517c478bd9Sstevel@tonic-gate 			{TOK_OPAQUE, "opaque"},
3527c478bd9Sstevel@tonic-gate 			{TOK_BOOL, "bool"},
3537c478bd9Sstevel@tonic-gate 			{TOK_VOID, "void"},
3547c478bd9Sstevel@tonic-gate 			{TOK_PROGRAM, "program"},
3557c478bd9Sstevel@tonic-gate 			{TOK_VERSION, "version"},
3567c478bd9Sstevel@tonic-gate 			{TOK_EOF, "??????"}
3577c478bd9Sstevel@tonic-gate };
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static char *
toktostr(tok_kind kind)36061961e0fSrobinson toktostr(tok_kind kind)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	token *sp;
3637c478bd9Sstevel@tonic-gate 
364*a2f144d1SJordan Brown 	for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++)
365*a2f144d1SJordan Brown 		/* LOOP */;
3667c478bd9Sstevel@tonic-gate 	return (sp->str);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
36961961e0fSrobinson static void
printbuf(void)37061961e0fSrobinson printbuf(void)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	char c;
3737c478bd9Sstevel@tonic-gate 	int i;
3747c478bd9Sstevel@tonic-gate 	int cnt;
3757c478bd9Sstevel@tonic-gate 
37661961e0fSrobinson #define	TABSIZE 4
3777c478bd9Sstevel@tonic-gate 
37861961e0fSrobinson 	for (i = 0; (c = curline[i]) != '\0'; i++) {
3797c478bd9Sstevel@tonic-gate 		if (c == '\t') {
3807c478bd9Sstevel@tonic-gate 			cnt = 8 - (i % TABSIZE);
3817c478bd9Sstevel@tonic-gate 			c = ' ';
3827c478bd9Sstevel@tonic-gate 		} else {
3837c478bd9Sstevel@tonic-gate 			cnt = 1;
3847c478bd9Sstevel@tonic-gate 		}
38561961e0fSrobinson 		while (cnt--)
3867c478bd9Sstevel@tonic-gate 			(void) fputc(c, stderr);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
39061961e0fSrobinson static void
printwhere(void)39161961e0fSrobinson printwhere(void)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	int i;
3947c478bd9Sstevel@tonic-gate 	char c;
3957c478bd9Sstevel@tonic-gate 	int cnt;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	printbuf();
3987c478bd9Sstevel@tonic-gate 	for (i = 0; i < where - curline; i++) {
3997c478bd9Sstevel@tonic-gate 		c = curline[i];
4007c478bd9Sstevel@tonic-gate 		if (c == '\t') {
4017c478bd9Sstevel@tonic-gate 			cnt = 8 - (i % TABSIZE);
4027c478bd9Sstevel@tonic-gate 		} else {
4037c478bd9Sstevel@tonic-gate 			cnt = 1;
4047c478bd9Sstevel@tonic-gate 		}
40561961e0fSrobinson 		while (cnt--)
4067c478bd9Sstevel@tonic-gate 			(void) fputc('^', stderr);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stderr);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate char *
make_argname(char * pname,char * vname)41261961e0fSrobinson make_argname(char *pname, char *vname)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	char *name;
41561961e0fSrobinson 	size_t nlen;
4167c478bd9Sstevel@tonic-gate 
41761961e0fSrobinson 	nlen = strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3;
41861961e0fSrobinson 	name = malloc(nlen);
41961961e0fSrobinson 	if (name == NULL) {
42061961e0fSrobinson 		(void) fprintf(stderr, "failed in malloc");
4217c478bd9Sstevel@tonic-gate 		exit(1);
4227c478bd9Sstevel@tonic-gate 	}
42361961e0fSrobinson 	(void) snprintf(name, nlen, "%s_%s_%s", locase(pname), vname, ARGEXT);
4247c478bd9Sstevel@tonic-gate 	return (name);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate bas_type *typ_list_h;
4287c478bd9Sstevel@tonic-gate bas_type *typ_list_t;
4297c478bd9Sstevel@tonic-gate 
43061961e0fSrobinson void
add_type(int len,char * type)43161961e0fSrobinson add_type(int len, char *type)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	bas_type *ptr;
4347c478bd9Sstevel@tonic-gate 
43561961e0fSrobinson 	if ((ptr = malloc(sizeof (bas_type))) == NULL) {
43661961e0fSrobinson 		(void) fprintf(stderr, "failed in malloc");
4377c478bd9Sstevel@tonic-gate 		exit(1);
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	ptr->name = type;
4417c478bd9Sstevel@tonic-gate 	ptr->length = len;
4427c478bd9Sstevel@tonic-gate 	ptr->next = NULL;
44361961e0fSrobinson 	if (typ_list_t == NULL) {
4447c478bd9Sstevel@tonic-gate 		typ_list_t = ptr;
4457c478bd9Sstevel@tonic-gate 		typ_list_h = ptr;
44661961e0fSrobinson 	} else {
4477c478bd9Sstevel@tonic-gate 		typ_list_t->next = ptr;
4487c478bd9Sstevel@tonic-gate 		typ_list_t = ptr;
44961961e0fSrobinson 	}
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 
45361961e0fSrobinson bas_type *
find_type(char * type)45461961e0fSrobinson find_type(char *type)
4557c478bd9Sstevel@tonic-gate {
45661961e0fSrobinson 	bas_type *ptr;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	ptr = typ_list_h;
45961961e0fSrobinson 	while (ptr != NULL) {
4607c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->name, type) == 0)
4617c478bd9Sstevel@tonic-gate 			return (ptr);
46261961e0fSrobinson 		ptr = ptr->next;
46361961e0fSrobinson 	}
4647c478bd9Sstevel@tonic-gate 	return (NULL);
4657c478bd9Sstevel@tonic-gate }
466