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
304e115012SGarrett Wollman /*
314e115012SGarrett Wollman * rpc_util.c, Utility routines for the RPC protocol compiler
32ff49530fSBill Paul * Copyright (C) 1989, Sun Microsystems, Inc.
334e115012SGarrett Wollman */
340e76f40dSPhilippe Charnier #include <err.h>
35821df508SXin LI #include <ctype.h>
364e115012SGarrett Wollman #include <stdio.h>
37526195adSJordan K. Hubbard #include <string.h>
38526195adSJordan K. Hubbard #include <unistd.h>
394e115012SGarrett Wollman #include "rpc_parse.h"
40e390e3afSDavid Malone #include "rpc_scan.h"
414e115012SGarrett Wollman #include "rpc_util.h"
424e115012SGarrett Wollman
43ff49530fSBill Paul #define ARGEXT "argument"
44ff49530fSBill Paul
454e115012SGarrett Wollman char curline[MAXLINESIZE]; /* current read line */
464e115012SGarrett Wollman char *where = curline; /* current point in line */
474e115012SGarrett Wollman int linenum = 0; /* current line number */
484e115012SGarrett Wollman
49e390e3afSDavid Malone const char *infilename; /* input filename */
504e115012SGarrett Wollman
51ff49530fSBill Paul #define NFILES 7
52bf70beceSEd Schouten static const char *outfiles[NFILES]; /* output file names */
53bf70beceSEd Schouten static int nfiles;
544e115012SGarrett Wollman
554e115012SGarrett Wollman FILE *fout; /* file pointer of current output */
564e115012SGarrett Wollman FILE *fin; /* file pointer of current input */
574e115012SGarrett Wollman
584e115012SGarrett Wollman list *defined; /* list of defined things */
594e115012SGarrett Wollman
60d3cb5dedSWarner Losh static void printwhere( void );
614e115012SGarrett Wollman
624e115012SGarrett Wollman /*
634e115012SGarrett Wollman * Reinitialize the world
644e115012SGarrett Wollman */
65526195adSJordan K. Hubbard void
reinitialize(void)66e390e3afSDavid Malone reinitialize(void)
674e115012SGarrett Wollman {
68ff49530fSBill Paul memset(curline, 0, MAXLINESIZE);
694e115012SGarrett Wollman where = curline;
704e115012SGarrett Wollman linenum = 0;
714e115012SGarrett Wollman defined = NULL;
724e115012SGarrett Wollman }
734e115012SGarrett Wollman
744e115012SGarrett Wollman /*
754e115012SGarrett Wollman * string equality
764e115012SGarrett Wollman */
77526195adSJordan K. Hubbard int
streq(const char * a,const char * b)78e390e3afSDavid Malone streq(const char *a, const char *b)
794e115012SGarrett Wollman {
804e115012SGarrett Wollman return (strcmp(a, b) == 0);
814e115012SGarrett Wollman }
824e115012SGarrett Wollman
834e115012SGarrett Wollman /*
844e115012SGarrett Wollman * find a value in a list
854e115012SGarrett Wollman */
86ff49530fSBill Paul definition *
findval(list * lst,const char * val,int (* cmp)(definition *,const char *))87e390e3afSDavid Malone findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
884e115012SGarrett Wollman {
894e115012SGarrett Wollman for (; lst != NULL; lst = lst->next) {
904e115012SGarrett Wollman if ((*cmp) (lst->val, val)) {
914e115012SGarrett Wollman return (lst->val);
924e115012SGarrett Wollman }
934e115012SGarrett Wollman }
944e115012SGarrett Wollman return (NULL);
954e115012SGarrett Wollman }
964e115012SGarrett Wollman
974e115012SGarrett Wollman /*
984e115012SGarrett Wollman * store a value in a list
994e115012SGarrett Wollman */
1004e115012SGarrett Wollman void
storeval(list ** lstp,definition * val)101e390e3afSDavid Malone storeval(list **lstp, definition *val)
1024e115012SGarrett Wollman {
1034e115012SGarrett Wollman list **l;
1044e115012SGarrett Wollman list *lst;
1054e115012SGarrett Wollman
1064e115012SGarrett Wollman for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
10775863a6dSPhilippe Charnier lst = XALLOC(list);
1084e115012SGarrett Wollman lst->val = val;
1094e115012SGarrett Wollman lst->next = NULL;
1104e115012SGarrett Wollman *l = lst;
1114e115012SGarrett Wollman }
1124e115012SGarrett Wollman
113526195adSJordan K. Hubbard static int
findit(definition * def,const char * type)114e390e3afSDavid Malone findit(definition *def, const char *type)
1154e115012SGarrett Wollman {
1164e115012SGarrett Wollman return (streq(def->def_name, type));
1174e115012SGarrett Wollman }
1184e115012SGarrett Wollman
119e390e3afSDavid Malone static const char *
fixit(const char * type,const char * orig)120e390e3afSDavid Malone fixit(const char *type, const char *orig)
1214e115012SGarrett Wollman {
1224e115012SGarrett Wollman definition *def;
1234e115012SGarrett Wollman
1244e115012SGarrett Wollman def = (definition *) FINDVAL(defined, type, findit);
1254e115012SGarrett Wollman if (def == NULL || def->def_kind != DEF_TYPEDEF) {
1264e115012SGarrett Wollman return (orig);
1274e115012SGarrett Wollman }
1284e115012SGarrett Wollman switch (def->def.ty.rel) {
1294e115012SGarrett Wollman case REL_VECTOR:
130ff49530fSBill Paul if (streq(def->def.ty.old_type, "opaque"))
131ff49530fSBill Paul return ("char");
132ff49530fSBill Paul else
1334e115012SGarrett Wollman return (def->def.ty.old_type);
134ff49530fSBill Paul
1354e115012SGarrett Wollman case REL_ALIAS:
1364e115012SGarrett Wollman return (fixit(def->def.ty.old_type, orig));
1374e115012SGarrett Wollman default:
1384e115012SGarrett Wollman return (orig);
1394e115012SGarrett Wollman }
1404e115012SGarrett Wollman }
1414e115012SGarrett Wollman
142e390e3afSDavid Malone const char *
fixtype(const char * type)143e390e3afSDavid Malone fixtype(const char *type)
1444e115012SGarrett Wollman {
1454e115012SGarrett Wollman return (fixit(type, type));
1464e115012SGarrett Wollman }
1474e115012SGarrett Wollman
148e390e3afSDavid Malone const char *
stringfix(const char * type)149e390e3afSDavid Malone stringfix(const char *type)
1504e115012SGarrett Wollman {
1514e115012SGarrett Wollman if (streq(type, "string")) {
1524e115012SGarrett Wollman return ("wrapstring");
1534e115012SGarrett Wollman } else {
1544e115012SGarrett Wollman return (type);
1554e115012SGarrett Wollman }
1564e115012SGarrett Wollman }
1574e115012SGarrett Wollman
1584e115012SGarrett Wollman void
ptype(const char * prefix,const char * type,int follow)159e390e3afSDavid Malone ptype(const char *prefix, const char *type, int follow)
1604e115012SGarrett Wollman {
1614e115012SGarrett Wollman if (prefix != NULL) {
1624e115012SGarrett Wollman if (streq(prefix, "enum")) {
1634e115012SGarrett Wollman f_print(fout, "enum ");
1644e115012SGarrett Wollman } else {
1654e115012SGarrett Wollman f_print(fout, "struct ");
1664e115012SGarrett Wollman }
1674e115012SGarrett Wollman }
1684e115012SGarrett Wollman if (streq(type, "bool")) {
1694e115012SGarrett Wollman f_print(fout, "bool_t ");
1704e115012SGarrett Wollman } else if (streq(type, "string")) {
1714e115012SGarrett Wollman f_print(fout, "char *");
1724e115012SGarrett Wollman } else {
1734e115012SGarrett Wollman f_print(fout, "%s ", follow ? fixtype(type) : type);
1744e115012SGarrett Wollman }
1754e115012SGarrett Wollman }
1764e115012SGarrett Wollman
177526195adSJordan K. Hubbard static int
typedefed(definition * def,const char * type)178e390e3afSDavid Malone typedefed(definition *def, const char *type)
1794e115012SGarrett Wollman {
1804e115012SGarrett Wollman if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
1814e115012SGarrett Wollman return (0);
1824e115012SGarrett Wollman } else {
1834e115012SGarrett Wollman return (streq(def->def_name, type));
1844e115012SGarrett Wollman }
1854e115012SGarrett Wollman }
1864e115012SGarrett Wollman
187526195adSJordan K. Hubbard int
isvectordef(const char * type,relation rel)188e390e3afSDavid Malone isvectordef(const char *type, relation rel)
1894e115012SGarrett Wollman {
1904e115012SGarrett Wollman definition *def;
1914e115012SGarrett Wollman
1924e115012SGarrett Wollman for (;;) {
1934e115012SGarrett Wollman switch (rel) {
1944e115012SGarrett Wollman case REL_VECTOR:
1954e115012SGarrett Wollman return (!streq(type, "string"));
1964e115012SGarrett Wollman case REL_ARRAY:
1974e115012SGarrett Wollman return (0);
1984e115012SGarrett Wollman case REL_POINTER:
1994e115012SGarrett Wollman return (0);
2004e115012SGarrett Wollman case REL_ALIAS:
2014e115012SGarrett Wollman def = (definition *) FINDVAL(defined, type, typedefed);
2024e115012SGarrett Wollman if (def == NULL) {
2034e115012SGarrett Wollman return (0);
2044e115012SGarrett Wollman }
2054e115012SGarrett Wollman type = def->def.ty.old_type;
2064e115012SGarrett Wollman rel = def->def.ty.rel;
2074e115012SGarrett Wollman }
2084e115012SGarrett Wollman }
209526195adSJordan K. Hubbard
210526195adSJordan K. Hubbard return (0);
2114e115012SGarrett Wollman }
2124e115012SGarrett Wollman
213ff49530fSBill Paul char *
locase(const char * str)214e390e3afSDavid Malone locase(const char *str)
2154e115012SGarrett Wollman {
2164e115012SGarrett Wollman char c;
2174e115012SGarrett Wollman static char buf[100];
2184e115012SGarrett Wollman char *p = buf;
2194e115012SGarrett Wollman
220526195adSJordan K. Hubbard while ( (c = *str++) ) {
2214e115012SGarrett Wollman *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
2224e115012SGarrett Wollman }
2234e115012SGarrett Wollman *p = 0;
2244e115012SGarrett Wollman return (buf);
2254e115012SGarrett Wollman }
2264e115012SGarrett Wollman
227ff49530fSBill Paul void
pvname_svc(const char * pname,const char * vnum)228e390e3afSDavid Malone pvname_svc(const char *pname, const char *vnum)
229ff49530fSBill Paul {
230ff49530fSBill Paul f_print(fout, "%s_%s_svc", locase(pname), vnum);
231ff49530fSBill Paul }
2324e115012SGarrett Wollman
2334e115012SGarrett Wollman void
pvname(const char * pname,const char * vnum)234e390e3afSDavid Malone pvname(const char *pname, const char *vnum)
2354e115012SGarrett Wollman {
2364e115012SGarrett Wollman f_print(fout, "%s_%s", locase(pname), vnum);
2374e115012SGarrett Wollman }
2384e115012SGarrett Wollman
2394e115012SGarrett Wollman /*
2404e115012SGarrett Wollman * print a useful (?) error message, and then die
2414e115012SGarrett Wollman */
2424e115012SGarrett Wollman void
error(const char * msg)243e390e3afSDavid Malone error(const char *msg)
2444e115012SGarrett Wollman {
2454e115012SGarrett Wollman printwhere();
2460e76f40dSPhilippe Charnier warnx("%s, line %d: %s", infilename, linenum, msg);
2474e115012SGarrett Wollman crash();
2484e115012SGarrett Wollman }
2494e115012SGarrett Wollman
2504e115012SGarrett Wollman /*
2514e115012SGarrett Wollman * Something went wrong, unlink any files that we may have created and then
2524e115012SGarrett Wollman * die.
2534e115012SGarrett Wollman */
254*1d1694a7SConrad Meyer void __dead2
crash(void)255e390e3afSDavid Malone crash(void)
2564e115012SGarrett Wollman {
2574e115012SGarrett Wollman int i;
2584e115012SGarrett Wollman
2594e115012SGarrett Wollman for (i = 0; i < nfiles; i++) {
2604e115012SGarrett Wollman (void) unlink(outfiles[i]);
2614e115012SGarrett Wollman }
2624e115012SGarrett Wollman exit(1);
2634e115012SGarrett Wollman }
2644e115012SGarrett Wollman
2654e115012SGarrett Wollman void
record_open(const char * file)266e390e3afSDavid Malone record_open(const char *file)
2674e115012SGarrett Wollman {
2684e115012SGarrett Wollman if (nfiles < NFILES) {
2694e115012SGarrett Wollman outfiles[nfiles++] = file;
2704e115012SGarrett Wollman } else {
2710e76f40dSPhilippe Charnier warnx("too many files");
2724e115012SGarrett Wollman crash();
2734e115012SGarrett Wollman }
2744e115012SGarrett Wollman }
2754e115012SGarrett Wollman
2764e115012SGarrett Wollman static char expectbuf[100];
277e390e3afSDavid Malone static const char *toktostr(tok_kind kind);
2784e115012SGarrett Wollman
2794e115012SGarrett Wollman /*
2804e115012SGarrett Wollman * error, token encountered was not the expected one
2814e115012SGarrett Wollman */
2824e115012SGarrett Wollman void
expected1(tok_kind exp1)283e390e3afSDavid Malone expected1(tok_kind exp1)
2844e115012SGarrett Wollman {
2854e115012SGarrett Wollman s_print(expectbuf, "expected '%s'",
2864e115012SGarrett Wollman toktostr(exp1));
2874e115012SGarrett Wollman error(expectbuf);
2884e115012SGarrett Wollman }
2894e115012SGarrett Wollman
2904e115012SGarrett Wollman /*
2914e115012SGarrett Wollman * error, token encountered was not one of two expected ones
2924e115012SGarrett Wollman */
2934e115012SGarrett Wollman void
expected2(tok_kind exp1,tok_kind exp2)294e390e3afSDavid Malone expected2(tok_kind exp1, tok_kind exp2)
2954e115012SGarrett Wollman {
2964e115012SGarrett Wollman s_print(expectbuf, "expected '%s' or '%s'",
2974e115012SGarrett Wollman toktostr(exp1),
2984e115012SGarrett Wollman toktostr(exp2));
2994e115012SGarrett Wollman error(expectbuf);
3004e115012SGarrett Wollman }
3014e115012SGarrett Wollman
3024e115012SGarrett Wollman /*
3034e115012SGarrett Wollman * error, token encountered was not one of 3 expected ones
3044e115012SGarrett Wollman */
3054e115012SGarrett Wollman void
expected3(tok_kind exp1,tok_kind exp2,tok_kind exp3)306e390e3afSDavid Malone expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
3074e115012SGarrett Wollman {
3084e115012SGarrett Wollman s_print(expectbuf, "expected '%s', '%s' or '%s'",
3094e115012SGarrett Wollman toktostr(exp1),
3104e115012SGarrett Wollman toktostr(exp2),
3114e115012SGarrett Wollman toktostr(exp3));
3124e115012SGarrett Wollman error(expectbuf);
3134e115012SGarrett Wollman }
3144e115012SGarrett Wollman
3154e115012SGarrett Wollman void
tabify(FILE * f,int tab)316e390e3afSDavid Malone tabify(FILE *f, int tab)
3174e115012SGarrett Wollman {
3184e115012SGarrett Wollman while (tab--) {
3194e115012SGarrett Wollman (void) fputc('\t', f);
3204e115012SGarrett Wollman }
3214e115012SGarrett Wollman }
3224e115012SGarrett Wollman
3234e115012SGarrett Wollman
3244e115012SGarrett Wollman static token tokstrings[] = {
3254e115012SGarrett Wollman {TOK_IDENT, "identifier"},
3264e115012SGarrett Wollman {TOK_CONST, "const"},
3274e115012SGarrett Wollman {TOK_RPAREN, ")"},
3284e115012SGarrett Wollman {TOK_LPAREN, "("},
3294e115012SGarrett Wollman {TOK_RBRACE, "}"},
3304e115012SGarrett Wollman {TOK_LBRACE, "{"},
3314e115012SGarrett Wollman {TOK_LBRACKET, "["},
3324e115012SGarrett Wollman {TOK_RBRACKET, "]"},
3334e115012SGarrett Wollman {TOK_STAR, "*"},
3344e115012SGarrett Wollman {TOK_COMMA, ","},
3354e115012SGarrett Wollman {TOK_EQUAL, "="},
3364e115012SGarrett Wollman {TOK_COLON, ":"},
3374e115012SGarrett Wollman {TOK_SEMICOLON, ";"},
3384e115012SGarrett Wollman {TOK_UNION, "union"},
3394e115012SGarrett Wollman {TOK_STRUCT, "struct"},
3404e115012SGarrett Wollman {TOK_SWITCH, "switch"},
3414e115012SGarrett Wollman {TOK_CASE, "case"},
3424e115012SGarrett Wollman {TOK_DEFAULT, "default"},
3434e115012SGarrett Wollman {TOK_ENUM, "enum"},
3444e115012SGarrett Wollman {TOK_TYPEDEF, "typedef"},
3454e115012SGarrett Wollman {TOK_INT, "int"},
3464e115012SGarrett Wollman {TOK_SHORT, "short"},
3474e115012SGarrett Wollman {TOK_LONG, "long"},
3484e115012SGarrett Wollman {TOK_UNSIGNED, "unsigned"},
3494e115012SGarrett Wollman {TOK_DOUBLE, "double"},
3504e115012SGarrett Wollman {TOK_FLOAT, "float"},
3514e115012SGarrett Wollman {TOK_CHAR, "char"},
3524e115012SGarrett Wollman {TOK_STRING, "string"},
3534e115012SGarrett Wollman {TOK_OPAQUE, "opaque"},
3544e115012SGarrett Wollman {TOK_BOOL, "bool"},
3554e115012SGarrett Wollman {TOK_VOID, "void"},
3564e115012SGarrett Wollman {TOK_PROGRAM, "program"},
3574e115012SGarrett Wollman {TOK_VERSION, "version"},
3584e115012SGarrett Wollman {TOK_EOF, "??????"}
3594e115012SGarrett Wollman };
3604e115012SGarrett Wollman
361e390e3afSDavid Malone static const char *
toktostr(tok_kind kind)362e390e3afSDavid Malone toktostr(tok_kind kind)
3634e115012SGarrett Wollman {
3644e115012SGarrett Wollman token *sp;
3654e115012SGarrett Wollman
3664e115012SGarrett Wollman for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
3674e115012SGarrett Wollman return (sp->str);
3684e115012SGarrett Wollman }
3694e115012SGarrett Wollman
370526195adSJordan K. Hubbard static void
printbuf(void)371e390e3afSDavid Malone printbuf(void)
3724e115012SGarrett Wollman {
3734e115012SGarrett Wollman char c;
3744e115012SGarrett Wollman int i;
3754e115012SGarrett Wollman int cnt;
3764e115012SGarrett Wollman
3774e115012SGarrett Wollman # define TABSIZE 4
3784e115012SGarrett Wollman
379526195adSJordan K. Hubbard for (i = 0; (c = curline[i]); i++) {
3804e115012SGarrett Wollman if (c == '\t') {
3814e115012SGarrett Wollman cnt = 8 - (i % TABSIZE);
3824e115012SGarrett Wollman c = ' ';
3834e115012SGarrett Wollman } else {
3844e115012SGarrett Wollman cnt = 1;
3854e115012SGarrett Wollman }
3864e115012SGarrett Wollman while (cnt--) {
3874e115012SGarrett Wollman (void) fputc(c, stderr);
3884e115012SGarrett Wollman }
3894e115012SGarrett Wollman }
3904e115012SGarrett Wollman }
3914e115012SGarrett Wollman
392526195adSJordan K. Hubbard static void
printwhere(void)393e390e3afSDavid Malone printwhere(void)
3944e115012SGarrett Wollman {
3954e115012SGarrett Wollman int i;
3964e115012SGarrett Wollman char c;
3974e115012SGarrett Wollman int cnt;
3984e115012SGarrett Wollman
3994e115012SGarrett Wollman printbuf();
4004e115012SGarrett Wollman for (i = 0; i < where - curline; i++) {
4014e115012SGarrett Wollman c = curline[i];
4024e115012SGarrett Wollman if (c == '\t') {
4034e115012SGarrett Wollman cnt = 8 - (i % TABSIZE);
4044e115012SGarrett Wollman } else {
4054e115012SGarrett Wollman cnt = 1;
4064e115012SGarrett Wollman }
4074e115012SGarrett Wollman while (cnt--) {
4084e115012SGarrett Wollman (void) fputc('^', stderr);
4094e115012SGarrett Wollman }
4104e115012SGarrett Wollman }
4114e115012SGarrett Wollman (void) fputc('\n', stderr);
4124e115012SGarrett Wollman }
413ff49530fSBill Paul
414ff49530fSBill Paul char *
make_argname(const char * pname,const char * vname)415e390e3afSDavid Malone make_argname(const char *pname, const char *vname)
416ff49530fSBill Paul {
417ff49530fSBill Paul char *name;
418ff49530fSBill Paul
41975863a6dSPhilippe Charnier name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
420ff49530fSBill Paul sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
421ff49530fSBill Paul return (name);
422ff49530fSBill Paul }
423ff49530fSBill Paul
424ff49530fSBill Paul bas_type *typ_list_h;
425ff49530fSBill Paul bas_type *typ_list_t;
426ff49530fSBill Paul
427526195adSJordan K. Hubbard void
add_type(int len,const char * type)428e390e3afSDavid Malone add_type(int len, const char *type)
429ff49530fSBill Paul {
430ff49530fSBill Paul bas_type *ptr;
431ff49530fSBill Paul
43275863a6dSPhilippe Charnier ptr = XALLOC(bas_type);
433ff49530fSBill Paul
434ff49530fSBill Paul ptr->name = type;
435ff49530fSBill Paul ptr->length = len;
436ff49530fSBill Paul ptr->next = NULL;
437ff49530fSBill Paul if (typ_list_t == NULL)
438ff49530fSBill Paul {
439ff49530fSBill Paul
440ff49530fSBill Paul typ_list_t = ptr;
441ff49530fSBill Paul typ_list_h = ptr;
442ff49530fSBill Paul }
443ff49530fSBill Paul else
444ff49530fSBill Paul {
445ff49530fSBill Paul typ_list_t->next = ptr;
446ff49530fSBill Paul typ_list_t = ptr;
44780c7cc1cSPedro F. Giffuni }
448ff49530fSBill Paul }
449ff49530fSBill Paul
450ff49530fSBill Paul
451e390e3afSDavid Malone bas_type *
find_type(const char * type)452e390e3afSDavid Malone find_type(const char *type)
453ff49530fSBill Paul {
454ff49530fSBill Paul bas_type * ptr;
455ff49530fSBill Paul
456ff49530fSBill Paul ptr = typ_list_h;
457ff49530fSBill Paul while (ptr != NULL)
458ff49530fSBill Paul {
459ff49530fSBill Paul if (strcmp(ptr->name, type) == 0)
460ff49530fSBill Paul return (ptr);
461ff49530fSBill Paul else
462ff49530fSBill Paul ptr = ptr->next;
46380c7cc1cSPedro F. Giffuni }
464ff49530fSBill Paul return (NULL);
465ff49530fSBill Paul }
46675863a6dSPhilippe Charnier
46775863a6dSPhilippe Charnier void *
xmalloc(size_t size)46875863a6dSPhilippe Charnier xmalloc(size_t size)
46975863a6dSPhilippe Charnier {
47075863a6dSPhilippe Charnier void *p;
47175863a6dSPhilippe Charnier
47275863a6dSPhilippe Charnier if ((p = malloc(size)) == NULL) {
47375863a6dSPhilippe Charnier warnx("malloc failed");
47475863a6dSPhilippe Charnier crash();
47575863a6dSPhilippe Charnier }
47675863a6dSPhilippe Charnier return (p);
47775863a6dSPhilippe Charnier }
47875863a6dSPhilippe Charnier
47975863a6dSPhilippe Charnier void *
xrealloc(void * ptr,size_t size)48075863a6dSPhilippe Charnier xrealloc(void *ptr, size_t size)
48175863a6dSPhilippe Charnier {
48275863a6dSPhilippe Charnier void *p;
48375863a6dSPhilippe Charnier
48475863a6dSPhilippe Charnier if ((p = realloc(ptr, size)) == NULL) {
48575863a6dSPhilippe Charnier warnx("realloc failed");
48675863a6dSPhilippe Charnier crash();
48775863a6dSPhilippe Charnier }
48875863a6dSPhilippe Charnier return (p);
48975863a6dSPhilippe Charnier }
49075863a6dSPhilippe Charnier
49175863a6dSPhilippe Charnier char *
xstrdup(const char * str)49275863a6dSPhilippe Charnier xstrdup(const char *str)
49375863a6dSPhilippe Charnier {
49475863a6dSPhilippe Charnier char *p;
49575863a6dSPhilippe Charnier
49675863a6dSPhilippe Charnier if ((p = strdup(str)) == NULL) {
49775863a6dSPhilippe Charnier warnx("strdup failed");
49875863a6dSPhilippe Charnier crash();
49975863a6dSPhilippe Charnier }
50075863a6dSPhilippe Charnier return (p);
50175863a6dSPhilippe Charnier }
502