xref: /freebsd/usr.bin/rpcgen/rpc_util.c (revision 75863a6dbbcb1450be4b8774a56f059e70f534cd)
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  */
29ff49530fSBill Paul 
30ff49530fSBill Paul #ident	"@(#)rpc_util.c	1.14	93/07/05 SMI"
31ff49530fSBill Paul 
320e76f40dSPhilippe Charnier #if 0
3375863a6dSPhilippe Charnier #ifndef lint
34ff49530fSBill Paul static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
354e115012SGarrett Wollman #endif
360e76f40dSPhilippe Charnier #endif
374e115012SGarrett Wollman 
3875863a6dSPhilippe Charnier #include <sys/cdefs.h>
3975863a6dSPhilippe Charnier __FBSDID("$FreeBSD$");
4075863a6dSPhilippe Charnier 
414e115012SGarrett Wollman /*
424e115012SGarrett Wollman  * rpc_util.c, Utility routines for the RPC protocol compiler
43ff49530fSBill Paul  * Copyright (C) 1989, Sun Microsystems, Inc.
444e115012SGarrett Wollman  */
450e76f40dSPhilippe Charnier #include <err.h>
460e76f40dSPhilippe Charnier #include <ctype.h>
474e115012SGarrett Wollman #include <stdio.h>
48526195adSJordan K. Hubbard #include <string.h>
49526195adSJordan K. Hubbard #include <unistd.h>
504e115012SGarrett Wollman #include "rpc_scan.h"
514e115012SGarrett Wollman #include "rpc_parse.h"
524e115012SGarrett Wollman #include "rpc_util.h"
534e115012SGarrett Wollman 
54ff49530fSBill Paul #define	ARGEXT "argument"
55ff49530fSBill Paul 
564e115012SGarrett Wollman char curline[MAXLINESIZE];	/* current read line */
574e115012SGarrett Wollman char *where = curline;		/* current point in line */
584e115012SGarrett Wollman int linenum = 0;		/* current line number */
594e115012SGarrett Wollman 
604e115012SGarrett Wollman char *infilename;		/* input filename */
614e115012SGarrett Wollman 
62ff49530fSBill Paul #define	NFILES   7
634e115012SGarrett Wollman char *outfiles[NFILES];		/* output file names */
644e115012SGarrett Wollman int nfiles;
654e115012SGarrett Wollman 
664e115012SGarrett Wollman FILE *fout;			/* file pointer of current output */
674e115012SGarrett Wollman FILE *fin;			/* file pointer of current input */
684e115012SGarrett Wollman 
694e115012SGarrett Wollman list *defined;			/* list of defined things */
704e115012SGarrett Wollman 
71d3cb5dedSWarner Losh static void printwhere( void );
724e115012SGarrett Wollman 
734e115012SGarrett Wollman /*
744e115012SGarrett Wollman  * Reinitialize the world
754e115012SGarrett Wollman  */
76526195adSJordan K. Hubbard void
774e115012SGarrett Wollman reinitialize()
784e115012SGarrett Wollman {
79ff49530fSBill Paul 	memset(curline, 0, MAXLINESIZE);
804e115012SGarrett Wollman 	where = curline;
814e115012SGarrett Wollman 	linenum = 0;
824e115012SGarrett Wollman 	defined = NULL;
834e115012SGarrett Wollman }
844e115012SGarrett Wollman 
854e115012SGarrett Wollman /*
864e115012SGarrett Wollman  * string equality
874e115012SGarrett Wollman  */
88526195adSJordan K. Hubbard int
894e115012SGarrett Wollman streq(a, b)
904e115012SGarrett Wollman 	char *a;
914e115012SGarrett Wollman 	char *b;
924e115012SGarrett Wollman {
934e115012SGarrett Wollman 	return (strcmp(a, b) == 0);
944e115012SGarrett Wollman }
954e115012SGarrett Wollman 
964e115012SGarrett Wollman /*
974e115012SGarrett Wollman  * find a value in a list
984e115012SGarrett Wollman  */
99ff49530fSBill Paul definition *
1004e115012SGarrett Wollman findval(lst, val, cmp)
1014e115012SGarrett Wollman 	list *lst;
1024e115012SGarrett Wollman 	char *val;
1034e115012SGarrett Wollman 	int (*cmp) ();
1044e115012SGarrett Wollman 
1054e115012SGarrett Wollman {
1064e115012SGarrett Wollman 	for (; lst != NULL; lst = lst->next) {
1074e115012SGarrett Wollman 		if ((*cmp) (lst->val, val)) {
1084e115012SGarrett Wollman 			return (lst->val);
1094e115012SGarrett Wollman 		}
1104e115012SGarrett Wollman 	}
1114e115012SGarrett Wollman 	return (NULL);
1124e115012SGarrett Wollman }
1134e115012SGarrett Wollman 
1144e115012SGarrett Wollman /*
1154e115012SGarrett Wollman  * store a value in a list
1164e115012SGarrett Wollman  */
1174e115012SGarrett Wollman void
1184e115012SGarrett Wollman storeval(lstp, val)
1194e115012SGarrett Wollman 	list **lstp;
120ff49530fSBill Paul 	definition *val;
1214e115012SGarrett Wollman {
1224e115012SGarrett Wollman 	list **l;
1234e115012SGarrett Wollman 	list *lst;
1244e115012SGarrett Wollman 
1254e115012SGarrett Wollman 	for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
12675863a6dSPhilippe Charnier 	lst = XALLOC(list);
1274e115012SGarrett Wollman 	lst->val = val;
1284e115012SGarrett Wollman 	lst->next = NULL;
1294e115012SGarrett Wollman 	*l = lst;
1304e115012SGarrett Wollman }
1314e115012SGarrett Wollman 
132526195adSJordan K. Hubbard static int
1334e115012SGarrett Wollman findit(def, type)
1344e115012SGarrett Wollman 	definition *def;
1354e115012SGarrett Wollman 	char *type;
1364e115012SGarrett Wollman {
1374e115012SGarrett Wollman 	return (streq(def->def_name, type));
1384e115012SGarrett Wollman }
1394e115012SGarrett Wollman 
1404e115012SGarrett Wollman static char *
1414e115012SGarrett Wollman fixit(type, orig)
1424e115012SGarrett Wollman 	char *type;
1434e115012SGarrett Wollman 	char *orig;
1444e115012SGarrett Wollman {
1454e115012SGarrett Wollman 	definition *def;
1464e115012SGarrett Wollman 
1474e115012SGarrett Wollman 	def = (definition *) FINDVAL(defined, type, findit);
1484e115012SGarrett Wollman 	if (def == NULL || def->def_kind != DEF_TYPEDEF) {
1494e115012SGarrett Wollman 		return (orig);
1504e115012SGarrett Wollman 	}
1514e115012SGarrett Wollman 	switch (def->def.ty.rel) {
1524e115012SGarrett Wollman 	case REL_VECTOR:
153ff49530fSBill Paul 		if (streq(def->def.ty.old_type, "opaque"))
154ff49530fSBill Paul 			return ("char");
155ff49530fSBill Paul 		else
1564e115012SGarrett Wollman 			return (def->def.ty.old_type);
157ff49530fSBill Paul 
1584e115012SGarrett Wollman 	case REL_ALIAS:
1594e115012SGarrett Wollman 		return (fixit(def->def.ty.old_type, orig));
1604e115012SGarrett Wollman 	default:
1614e115012SGarrett Wollman 		return (orig);
1624e115012SGarrett Wollman 	}
1634e115012SGarrett Wollman }
1644e115012SGarrett Wollman 
1654e115012SGarrett Wollman char *
1664e115012SGarrett Wollman fixtype(type)
1674e115012SGarrett Wollman 	char *type;
1684e115012SGarrett Wollman {
1694e115012SGarrett Wollman 	return (fixit(type, type));
1704e115012SGarrett Wollman }
1714e115012SGarrett Wollman 
1724e115012SGarrett Wollman char *
1734e115012SGarrett Wollman stringfix(type)
1744e115012SGarrett Wollman 	char *type;
1754e115012SGarrett Wollman {
1764e115012SGarrett Wollman 	if (streq(type, "string")) {
1774e115012SGarrett Wollman 		return ("wrapstring");
1784e115012SGarrett Wollman 	} else {
1794e115012SGarrett Wollman 		return (type);
1804e115012SGarrett Wollman 	}
1814e115012SGarrett Wollman }
1824e115012SGarrett Wollman 
1834e115012SGarrett Wollman void
1844e115012SGarrett Wollman ptype(prefix, type, follow)
1854e115012SGarrett Wollman 	char *prefix;
1864e115012SGarrett Wollman 	char *type;
1874e115012SGarrett Wollman 	int follow;
1884e115012SGarrett Wollman {
1894e115012SGarrett Wollman 	if (prefix != NULL) {
1904e115012SGarrett Wollman 		if (streq(prefix, "enum")) {
1914e115012SGarrett Wollman 			f_print(fout, "enum ");
1924e115012SGarrett Wollman 		} else {
1934e115012SGarrett Wollman 			f_print(fout, "struct ");
1944e115012SGarrett Wollman 		}
1954e115012SGarrett Wollman 	}
1964e115012SGarrett Wollman 	if (streq(type, "bool")) {
1974e115012SGarrett Wollman 		f_print(fout, "bool_t ");
1984e115012SGarrett Wollman 	} else if (streq(type, "string")) {
1994e115012SGarrett Wollman 		f_print(fout, "char *");
2004e115012SGarrett Wollman 	} else {
2014e115012SGarrett Wollman 		f_print(fout, "%s ", follow ? fixtype(type) : type);
2024e115012SGarrett Wollman 	}
2034e115012SGarrett Wollman }
2044e115012SGarrett Wollman 
205526195adSJordan K. Hubbard static int
2064e115012SGarrett Wollman typedefed(def, type)
2074e115012SGarrett Wollman 	definition *def;
2084e115012SGarrett Wollman 	char *type;
2094e115012SGarrett Wollman {
2104e115012SGarrett Wollman 	if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
2114e115012SGarrett Wollman 		return (0);
2124e115012SGarrett Wollman 	} else {
2134e115012SGarrett Wollman 		return (streq(def->def_name, type));
2144e115012SGarrett Wollman 	}
2154e115012SGarrett Wollman }
2164e115012SGarrett Wollman 
217526195adSJordan K. Hubbard int
2184e115012SGarrett Wollman isvectordef(type, rel)
2194e115012SGarrett Wollman 	char *type;
2204e115012SGarrett Wollman 	relation rel;
2214e115012SGarrett Wollman {
2224e115012SGarrett Wollman 	definition *def;
2234e115012SGarrett Wollman 
2244e115012SGarrett Wollman 	for (;;) {
2254e115012SGarrett Wollman 		switch (rel) {
2264e115012SGarrett Wollman 		case REL_VECTOR:
2274e115012SGarrett Wollman 			return (!streq(type, "string"));
2284e115012SGarrett Wollman 		case REL_ARRAY:
2294e115012SGarrett Wollman 			return (0);
2304e115012SGarrett Wollman 		case REL_POINTER:
2314e115012SGarrett Wollman 			return (0);
2324e115012SGarrett Wollman 		case REL_ALIAS:
2334e115012SGarrett Wollman 			def = (definition *) FINDVAL(defined, type, typedefed);
2344e115012SGarrett Wollman 			if (def == NULL) {
2354e115012SGarrett Wollman 				return (0);
2364e115012SGarrett Wollman 			}
2374e115012SGarrett Wollman 			type = def->def.ty.old_type;
2384e115012SGarrett Wollman 			rel = def->def.ty.rel;
2394e115012SGarrett Wollman 		}
2404e115012SGarrett Wollman 	}
241526195adSJordan K. Hubbard 
242526195adSJordan K. Hubbard 	return (0);
2434e115012SGarrett Wollman }
2444e115012SGarrett Wollman 
245ff49530fSBill Paul char *
2464e115012SGarrett Wollman locase(str)
2474e115012SGarrett Wollman 	char *str;
2484e115012SGarrett Wollman {
2494e115012SGarrett Wollman 	char c;
2504e115012SGarrett Wollman 	static char buf[100];
2514e115012SGarrett Wollman 	char *p = buf;
2524e115012SGarrett Wollman 
253526195adSJordan K. Hubbard 	while ( (c = *str++) ) {
2544e115012SGarrett Wollman 		*p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
2554e115012SGarrett Wollman 	}
2564e115012SGarrett Wollman 	*p = 0;
2574e115012SGarrett Wollman 	return (buf);
2584e115012SGarrett Wollman }
2594e115012SGarrett Wollman 
260ff49530fSBill Paul void
261ff49530fSBill Paul pvname_svc(pname, vnum)
262ff49530fSBill Paul 	char *pname;
263ff49530fSBill Paul 	char *vnum;
264ff49530fSBill Paul {
265ff49530fSBill Paul 	f_print(fout, "%s_%s_svc", locase(pname), vnum);
266ff49530fSBill Paul }
2674e115012SGarrett Wollman 
2684e115012SGarrett Wollman void
2694e115012SGarrett Wollman pvname(pname, vnum)
2704e115012SGarrett Wollman 	char *pname;
2714e115012SGarrett Wollman 	char *vnum;
2724e115012SGarrett Wollman {
2734e115012SGarrett Wollman 	f_print(fout, "%s_%s", locase(pname), vnum);
2744e115012SGarrett Wollman }
2754e115012SGarrett Wollman 
2764e115012SGarrett Wollman /*
2774e115012SGarrett Wollman  * print a useful (?) error message, and then die
2784e115012SGarrett Wollman  */
2794e115012SGarrett Wollman void
2804e115012SGarrett Wollman error(msg)
2814e115012SGarrett Wollman 	char *msg;
2824e115012SGarrett Wollman {
2834e115012SGarrett Wollman 	printwhere();
2840e76f40dSPhilippe Charnier 	warnx("%s, line %d: %s", infilename, linenum, msg);
2854e115012SGarrett Wollman 	crash();
2864e115012SGarrett Wollman }
2874e115012SGarrett Wollman 
2884e115012SGarrett Wollman /*
2894e115012SGarrett Wollman  * Something went wrong, unlink any files that we may have created and then
2904e115012SGarrett Wollman  * die.
2914e115012SGarrett Wollman  */
292526195adSJordan K. Hubbard void
2934e115012SGarrett Wollman crash()
2944e115012SGarrett Wollman {
2954e115012SGarrett Wollman 	int i;
2964e115012SGarrett Wollman 
2974e115012SGarrett Wollman 	for (i = 0; i < nfiles; i++) {
2984e115012SGarrett Wollman 		(void) unlink(outfiles[i]);
2994e115012SGarrett Wollman 	}
3004e115012SGarrett Wollman 	exit(1);
3014e115012SGarrett Wollman }
3024e115012SGarrett Wollman 
3034e115012SGarrett Wollman void
3044e115012SGarrett Wollman record_open(file)
3054e115012SGarrett Wollman 	char *file;
3064e115012SGarrett Wollman {
3074e115012SGarrett Wollman 	if (nfiles < NFILES) {
3084e115012SGarrett Wollman 		outfiles[nfiles++] = file;
3094e115012SGarrett Wollman 	} else {
3100e76f40dSPhilippe Charnier 		warnx("too many files");
3114e115012SGarrett Wollman 		crash();
3124e115012SGarrett Wollman 	}
3134e115012SGarrett Wollman }
3144e115012SGarrett Wollman 
3154e115012SGarrett Wollman static char expectbuf[100];
3164e115012SGarrett Wollman static char *toktostr();
3174e115012SGarrett Wollman 
3184e115012SGarrett Wollman /*
3194e115012SGarrett Wollman  * error, token encountered was not the expected one
3204e115012SGarrett Wollman  */
3214e115012SGarrett Wollman void
3224e115012SGarrett Wollman expected1(exp1)
3234e115012SGarrett Wollman 	tok_kind exp1;
3244e115012SGarrett Wollman {
3254e115012SGarrett Wollman 	s_print(expectbuf, "expected '%s'",
3264e115012SGarrett Wollman 		toktostr(exp1));
3274e115012SGarrett Wollman 	error(expectbuf);
3284e115012SGarrett Wollman }
3294e115012SGarrett Wollman 
3304e115012SGarrett Wollman /*
3314e115012SGarrett Wollman  * error, token encountered was not one of two expected ones
3324e115012SGarrett Wollman  */
3334e115012SGarrett Wollman void
3344e115012SGarrett Wollman expected2(exp1, exp2)
3354e115012SGarrett Wollman 	tok_kind exp1, exp2;
3364e115012SGarrett Wollman {
3374e115012SGarrett Wollman 	s_print(expectbuf, "expected '%s' or '%s'",
3384e115012SGarrett Wollman 		toktostr(exp1),
3394e115012SGarrett Wollman 		toktostr(exp2));
3404e115012SGarrett Wollman 	error(expectbuf);
3414e115012SGarrett Wollman }
3424e115012SGarrett Wollman 
3434e115012SGarrett Wollman /*
3444e115012SGarrett Wollman  * error, token encountered was not one of 3 expected ones
3454e115012SGarrett Wollman  */
3464e115012SGarrett Wollman void
3474e115012SGarrett Wollman expected3(exp1, exp2, exp3)
3484e115012SGarrett Wollman 	tok_kind exp1, exp2, exp3;
3494e115012SGarrett Wollman {
3504e115012SGarrett Wollman 	s_print(expectbuf, "expected '%s', '%s' or '%s'",
3514e115012SGarrett Wollman 		toktostr(exp1),
3524e115012SGarrett Wollman 		toktostr(exp2),
3534e115012SGarrett Wollman 		toktostr(exp3));
3544e115012SGarrett Wollman 	error(expectbuf);
3554e115012SGarrett Wollman }
3564e115012SGarrett Wollman 
3574e115012SGarrett Wollman void
3584e115012SGarrett Wollman tabify(f, tab)
3594e115012SGarrett Wollman 	FILE *f;
3604e115012SGarrett Wollman 	int tab;
3614e115012SGarrett Wollman {
3624e115012SGarrett Wollman 	while (tab--) {
3634e115012SGarrett Wollman 		(void) fputc('\t', f);
3644e115012SGarrett Wollman 	}
3654e115012SGarrett Wollman }
3664e115012SGarrett Wollman 
3674e115012SGarrett Wollman 
3684e115012SGarrett Wollman static token tokstrings[] = {
3694e115012SGarrett Wollman 			{TOK_IDENT, "identifier"},
3704e115012SGarrett Wollman 			{TOK_CONST, "const"},
3714e115012SGarrett Wollman 			{TOK_RPAREN, ")"},
3724e115012SGarrett Wollman 			{TOK_LPAREN, "("},
3734e115012SGarrett Wollman 			{TOK_RBRACE, "}"},
3744e115012SGarrett Wollman 			{TOK_LBRACE, "{"},
3754e115012SGarrett Wollman 			{TOK_LBRACKET, "["},
3764e115012SGarrett Wollman 			{TOK_RBRACKET, "]"},
3774e115012SGarrett Wollman 			{TOK_STAR, "*"},
3784e115012SGarrett Wollman 			{TOK_COMMA, ","},
3794e115012SGarrett Wollman 			{TOK_EQUAL, "="},
3804e115012SGarrett Wollman 			{TOK_COLON, ":"},
3814e115012SGarrett Wollman 			{TOK_SEMICOLON, ";"},
3824e115012SGarrett Wollman 			{TOK_UNION, "union"},
3834e115012SGarrett Wollman 			{TOK_STRUCT, "struct"},
3844e115012SGarrett Wollman 			{TOK_SWITCH, "switch"},
3854e115012SGarrett Wollman 			{TOK_CASE, "case"},
3864e115012SGarrett Wollman 			{TOK_DEFAULT, "default"},
3874e115012SGarrett Wollman 			{TOK_ENUM, "enum"},
3884e115012SGarrett Wollman 			{TOK_TYPEDEF, "typedef"},
3894e115012SGarrett Wollman 			{TOK_INT, "int"},
3904e115012SGarrett Wollman 			{TOK_SHORT, "short"},
3914e115012SGarrett Wollman 			{TOK_LONG, "long"},
3924e115012SGarrett Wollman 			{TOK_UNSIGNED, "unsigned"},
3934e115012SGarrett Wollman 			{TOK_DOUBLE, "double"},
3944e115012SGarrett Wollman 			{TOK_FLOAT, "float"},
3954e115012SGarrett Wollman 			{TOK_CHAR, "char"},
3964e115012SGarrett Wollman 			{TOK_STRING, "string"},
3974e115012SGarrett Wollman 			{TOK_OPAQUE, "opaque"},
3984e115012SGarrett Wollman 			{TOK_BOOL, "bool"},
3994e115012SGarrett Wollman 			{TOK_VOID, "void"},
4004e115012SGarrett Wollman 			{TOK_PROGRAM, "program"},
4014e115012SGarrett Wollman 			{TOK_VERSION, "version"},
4024e115012SGarrett Wollman 			{TOK_EOF, "??????"}
4034e115012SGarrett Wollman };
4044e115012SGarrett Wollman 
4054e115012SGarrett Wollman static char *
4064e115012SGarrett Wollman toktostr(kind)
4074e115012SGarrett Wollman 	tok_kind kind;
4084e115012SGarrett Wollman {
4094e115012SGarrett Wollman 	token *sp;
4104e115012SGarrett Wollman 
4114e115012SGarrett Wollman 	for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
4124e115012SGarrett Wollman 	return (sp->str);
4134e115012SGarrett Wollman }
4144e115012SGarrett Wollman 
415526195adSJordan K. Hubbard static void
4164e115012SGarrett Wollman printbuf()
4174e115012SGarrett Wollman {
4184e115012SGarrett Wollman 	char c;
4194e115012SGarrett Wollman 	int i;
4204e115012SGarrett Wollman 	int cnt;
4214e115012SGarrett Wollman 
4224e115012SGarrett Wollman #	define TABSIZE 4
4234e115012SGarrett Wollman 
424526195adSJordan K. Hubbard 	for (i = 0; (c = curline[i]); i++) {
4254e115012SGarrett Wollman 		if (c == '\t') {
4264e115012SGarrett Wollman 			cnt = 8 - (i % TABSIZE);
4274e115012SGarrett Wollman 			c = ' ';
4284e115012SGarrett Wollman 		} else {
4294e115012SGarrett Wollman 			cnt = 1;
4304e115012SGarrett Wollman 		}
4314e115012SGarrett Wollman 		while (cnt--) {
4324e115012SGarrett Wollman 			(void) fputc(c, stderr);
4334e115012SGarrett Wollman 		}
4344e115012SGarrett Wollman 	}
4354e115012SGarrett Wollman }
4364e115012SGarrett Wollman 
437526195adSJordan K. Hubbard static void
4384e115012SGarrett Wollman printwhere()
4394e115012SGarrett Wollman {
4404e115012SGarrett Wollman 	int i;
4414e115012SGarrett Wollman 	char c;
4424e115012SGarrett Wollman 	int cnt;
4434e115012SGarrett Wollman 
4444e115012SGarrett Wollman 	printbuf();
4454e115012SGarrett Wollman 	for (i = 0; i < where - curline; i++) {
4464e115012SGarrett Wollman 		c = curline[i];
4474e115012SGarrett Wollman 		if (c == '\t') {
4484e115012SGarrett Wollman 			cnt = 8 - (i % TABSIZE);
4494e115012SGarrett Wollman 		} else {
4504e115012SGarrett Wollman 			cnt = 1;
4514e115012SGarrett Wollman 		}
4524e115012SGarrett Wollman 		while (cnt--) {
4534e115012SGarrett Wollman 			(void) fputc('^', stderr);
4544e115012SGarrett Wollman 		}
4554e115012SGarrett Wollman 	}
4564e115012SGarrett Wollman 	(void) fputc('\n', stderr);
4574e115012SGarrett Wollman }
458ff49530fSBill Paul 
459ff49530fSBill Paul char *
460ff49530fSBill Paul make_argname(pname, vname)
461ff49530fSBill Paul     char *pname;
462ff49530fSBill Paul     char *vname;
463ff49530fSBill Paul {
464ff49530fSBill Paul 	char *name;
465ff49530fSBill Paul 
46675863a6dSPhilippe Charnier 	name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
467ff49530fSBill Paul 	sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
468ff49530fSBill Paul 	return (name);
469ff49530fSBill Paul }
470ff49530fSBill Paul 
471ff49530fSBill Paul bas_type *typ_list_h;
472ff49530fSBill Paul bas_type *typ_list_t;
473ff49530fSBill Paul 
474526195adSJordan K. Hubbard void
475ff49530fSBill Paul add_type(len, type)
476ff49530fSBill Paul int len;
477ff49530fSBill Paul char *type;
478ff49530fSBill Paul {
479ff49530fSBill Paul 	bas_type *ptr;
480ff49530fSBill Paul 
48175863a6dSPhilippe Charnier 	ptr = XALLOC(bas_type);
482ff49530fSBill Paul 
483ff49530fSBill Paul 	ptr->name = type;
484ff49530fSBill Paul 	ptr->length = len;
485ff49530fSBill Paul 	ptr->next = NULL;
486ff49530fSBill Paul 	if (typ_list_t == NULL)
487ff49530fSBill Paul 	{
488ff49530fSBill Paul 
489ff49530fSBill Paul 		typ_list_t = ptr;
490ff49530fSBill Paul 		typ_list_h = ptr;
491ff49530fSBill Paul 	}
492ff49530fSBill Paul 	else
493ff49530fSBill Paul 	{
494ff49530fSBill Paul 		typ_list_t->next = ptr;
495ff49530fSBill Paul 		typ_list_t = ptr;
496ff49530fSBill Paul 	};
497ff49530fSBill Paul }
498ff49530fSBill Paul 
499ff49530fSBill Paul 
500ff49530fSBill Paul bas_type *find_type(type)
501ff49530fSBill Paul char *type;
502ff49530fSBill Paul {
503ff49530fSBill Paul 	bas_type * ptr;
504ff49530fSBill Paul 
505ff49530fSBill Paul 	ptr = typ_list_h;
506ff49530fSBill Paul 	while (ptr != NULL)
507ff49530fSBill Paul 	{
508ff49530fSBill Paul 		if (strcmp(ptr->name, type) == 0)
509ff49530fSBill Paul 			return (ptr);
510ff49530fSBill Paul 		else
511ff49530fSBill Paul 			ptr = ptr->next;
512ff49530fSBill Paul 	};
513ff49530fSBill Paul 	return (NULL);
514ff49530fSBill Paul }
51575863a6dSPhilippe Charnier 
51675863a6dSPhilippe Charnier void *
51775863a6dSPhilippe Charnier xmalloc(size_t size)
51875863a6dSPhilippe Charnier {
51975863a6dSPhilippe Charnier 	void *p;
52075863a6dSPhilippe Charnier 
52175863a6dSPhilippe Charnier 	if ((p = malloc(size)) == NULL) {
52275863a6dSPhilippe Charnier 		warnx("malloc failed");
52375863a6dSPhilippe Charnier 		crash();
52475863a6dSPhilippe Charnier 	}
52575863a6dSPhilippe Charnier 	return (p);
52675863a6dSPhilippe Charnier }
52775863a6dSPhilippe Charnier 
52875863a6dSPhilippe Charnier void *
52975863a6dSPhilippe Charnier xrealloc(void *ptr, size_t size)
53075863a6dSPhilippe Charnier {
53175863a6dSPhilippe Charnier 	void *p;
53275863a6dSPhilippe Charnier 
53375863a6dSPhilippe Charnier 	if ((p = realloc(ptr, size)) == NULL) {
53475863a6dSPhilippe Charnier 		warnx("realloc failed");
53575863a6dSPhilippe Charnier 		crash();
53675863a6dSPhilippe Charnier 	}
53775863a6dSPhilippe Charnier 	return (p);
53875863a6dSPhilippe Charnier }
53975863a6dSPhilippe Charnier 
54075863a6dSPhilippe Charnier char *
54175863a6dSPhilippe Charnier xstrdup(const char *str)
54275863a6dSPhilippe Charnier {
54375863a6dSPhilippe Charnier 	char *p;
54475863a6dSPhilippe Charnier 
54575863a6dSPhilippe Charnier 	if ((p = strdup(str)) == NULL) {
54675863a6dSPhilippe Charnier 		warnx("strdup failed");
54775863a6dSPhilippe Charnier 		crash();
54875863a6dSPhilippe Charnier 	}
54975863a6dSPhilippe Charnier 	return (p);
55075863a6dSPhilippe Charnier }
551