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 300e76f40dSPhilippe Charnier #if 0 3175863a6dSPhilippe Charnier #ifndef lint 3263f17371SStefan Farfeleder #ident "@(#)rpc_util.c 1.14 93/07/05 SMI" 33ff49530fSBill Paul static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI"; 344e115012SGarrett Wollman #endif 350e76f40dSPhilippe Charnier #endif 364e115012SGarrett Wollman 3775863a6dSPhilippe Charnier #include <sys/cdefs.h> 3875863a6dSPhilippe Charnier __FBSDID("$FreeBSD$"); 3975863a6dSPhilippe Charnier 404e115012SGarrett Wollman /* 414e115012SGarrett Wollman * rpc_util.c, Utility routines for the RPC protocol compiler 42ff49530fSBill Paul * Copyright (C) 1989, Sun Microsystems, Inc. 434e115012SGarrett Wollman */ 440e76f40dSPhilippe Charnier #include <err.h> 45821df508SXin LI #include <ctype.h> 464e115012SGarrett Wollman #include <stdio.h> 47526195adSJordan K. Hubbard #include <string.h> 48526195adSJordan K. Hubbard #include <unistd.h> 494e115012SGarrett Wollman #include "rpc_parse.h" 50e390e3afSDavid Malone #include "rpc_scan.h" 514e115012SGarrett Wollman #include "rpc_util.h" 524e115012SGarrett Wollman 53ff49530fSBill Paul #define ARGEXT "argument" 54ff49530fSBill Paul 554e115012SGarrett Wollman char curline[MAXLINESIZE]; /* current read line */ 564e115012SGarrett Wollman char *where = curline; /* current point in line */ 574e115012SGarrett Wollman int linenum = 0; /* current line number */ 584e115012SGarrett Wollman 59e390e3afSDavid Malone const char *infilename; /* input filename */ 604e115012SGarrett Wollman 61ff49530fSBill Paul #define NFILES 7 62*bf70beceSEd Schouten static const char *outfiles[NFILES]; /* output file names */ 63*bf70beceSEd Schouten static int nfiles; 644e115012SGarrett Wollman 654e115012SGarrett Wollman FILE *fout; /* file pointer of current output */ 664e115012SGarrett Wollman FILE *fin; /* file pointer of current input */ 674e115012SGarrett Wollman 684e115012SGarrett Wollman list *defined; /* list of defined things */ 694e115012SGarrett Wollman 70d3cb5dedSWarner Losh static void printwhere( void ); 714e115012SGarrett Wollman 724e115012SGarrett Wollman /* 734e115012SGarrett Wollman * Reinitialize the world 744e115012SGarrett Wollman */ 75526195adSJordan K. Hubbard void 76e390e3afSDavid Malone reinitialize(void) 774e115012SGarrett Wollman { 78ff49530fSBill Paul memset(curline, 0, MAXLINESIZE); 794e115012SGarrett Wollman where = curline; 804e115012SGarrett Wollman linenum = 0; 814e115012SGarrett Wollman defined = NULL; 824e115012SGarrett Wollman } 834e115012SGarrett Wollman 844e115012SGarrett Wollman /* 854e115012SGarrett Wollman * string equality 864e115012SGarrett Wollman */ 87526195adSJordan K. Hubbard int 88e390e3afSDavid Malone streq(const char *a, const char *b) 894e115012SGarrett Wollman { 904e115012SGarrett Wollman return (strcmp(a, b) == 0); 914e115012SGarrett Wollman } 924e115012SGarrett Wollman 934e115012SGarrett Wollman /* 944e115012SGarrett Wollman * find a value in a list 954e115012SGarrett Wollman */ 96ff49530fSBill Paul definition * 97e390e3afSDavid Malone findval(list *lst, const char *val, int (*cmp)(definition *, const char *)) 984e115012SGarrett Wollman { 994e115012SGarrett Wollman for (; lst != NULL; lst = lst->next) { 1004e115012SGarrett Wollman if ((*cmp) (lst->val, val)) { 1014e115012SGarrett Wollman return (lst->val); 1024e115012SGarrett Wollman } 1034e115012SGarrett Wollman } 1044e115012SGarrett Wollman return (NULL); 1054e115012SGarrett Wollman } 1064e115012SGarrett Wollman 1074e115012SGarrett Wollman /* 1084e115012SGarrett Wollman * store a value in a list 1094e115012SGarrett Wollman */ 1104e115012SGarrett Wollman void 111e390e3afSDavid Malone storeval(list **lstp, definition *val) 1124e115012SGarrett Wollman { 1134e115012SGarrett Wollman list **l; 1144e115012SGarrett Wollman list *lst; 1154e115012SGarrett Wollman 1164e115012SGarrett Wollman for (l = lstp; *l != NULL; l = (list **) & (*l)->next); 11775863a6dSPhilippe Charnier lst = XALLOC(list); 1184e115012SGarrett Wollman lst->val = val; 1194e115012SGarrett Wollman lst->next = NULL; 1204e115012SGarrett Wollman *l = lst; 1214e115012SGarrett Wollman } 1224e115012SGarrett Wollman 123526195adSJordan K. Hubbard static int 124e390e3afSDavid Malone findit(definition *def, const char *type) 1254e115012SGarrett Wollman { 1264e115012SGarrett Wollman return (streq(def->def_name, type)); 1274e115012SGarrett Wollman } 1284e115012SGarrett Wollman 129e390e3afSDavid Malone static const char * 130e390e3afSDavid Malone fixit(const char *type, const char *orig) 1314e115012SGarrett Wollman { 1324e115012SGarrett Wollman definition *def; 1334e115012SGarrett Wollman 1344e115012SGarrett Wollman def = (definition *) FINDVAL(defined, type, findit); 1354e115012SGarrett Wollman if (def == NULL || def->def_kind != DEF_TYPEDEF) { 1364e115012SGarrett Wollman return (orig); 1374e115012SGarrett Wollman } 1384e115012SGarrett Wollman switch (def->def.ty.rel) { 1394e115012SGarrett Wollman case REL_VECTOR: 140ff49530fSBill Paul if (streq(def->def.ty.old_type, "opaque")) 141ff49530fSBill Paul return ("char"); 142ff49530fSBill Paul else 1434e115012SGarrett Wollman return (def->def.ty.old_type); 144ff49530fSBill Paul 1454e115012SGarrett Wollman case REL_ALIAS: 1464e115012SGarrett Wollman return (fixit(def->def.ty.old_type, orig)); 1474e115012SGarrett Wollman default: 1484e115012SGarrett Wollman return (orig); 1494e115012SGarrett Wollman } 1504e115012SGarrett Wollman } 1514e115012SGarrett Wollman 152e390e3afSDavid Malone const char * 153e390e3afSDavid Malone fixtype(const char *type) 1544e115012SGarrett Wollman { 1554e115012SGarrett Wollman return (fixit(type, type)); 1564e115012SGarrett Wollman } 1574e115012SGarrett Wollman 158e390e3afSDavid Malone const char * 159e390e3afSDavid Malone stringfix(const char *type) 1604e115012SGarrett Wollman { 1614e115012SGarrett Wollman if (streq(type, "string")) { 1624e115012SGarrett Wollman return ("wrapstring"); 1634e115012SGarrett Wollman } else { 1644e115012SGarrett Wollman return (type); 1654e115012SGarrett Wollman } 1664e115012SGarrett Wollman } 1674e115012SGarrett Wollman 1684e115012SGarrett Wollman void 169e390e3afSDavid Malone ptype(const char *prefix, const char *type, int follow) 1704e115012SGarrett Wollman { 1714e115012SGarrett Wollman if (prefix != NULL) { 1724e115012SGarrett Wollman if (streq(prefix, "enum")) { 1734e115012SGarrett Wollman f_print(fout, "enum "); 1744e115012SGarrett Wollman } else { 1754e115012SGarrett Wollman f_print(fout, "struct "); 1764e115012SGarrett Wollman } 1774e115012SGarrett Wollman } 1784e115012SGarrett Wollman if (streq(type, "bool")) { 1794e115012SGarrett Wollman f_print(fout, "bool_t "); 1804e115012SGarrett Wollman } else if (streq(type, "string")) { 1814e115012SGarrett Wollman f_print(fout, "char *"); 1824e115012SGarrett Wollman } else { 1834e115012SGarrett Wollman f_print(fout, "%s ", follow ? fixtype(type) : type); 1844e115012SGarrett Wollman } 1854e115012SGarrett Wollman } 1864e115012SGarrett Wollman 187526195adSJordan K. Hubbard static int 188e390e3afSDavid Malone typedefed(definition *def, const char *type) 1894e115012SGarrett Wollman { 1904e115012SGarrett Wollman if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) { 1914e115012SGarrett Wollman return (0); 1924e115012SGarrett Wollman } else { 1934e115012SGarrett Wollman return (streq(def->def_name, type)); 1944e115012SGarrett Wollman } 1954e115012SGarrett Wollman } 1964e115012SGarrett Wollman 197526195adSJordan K. Hubbard int 198e390e3afSDavid Malone isvectordef(const char *type, relation rel) 1994e115012SGarrett Wollman { 2004e115012SGarrett Wollman definition *def; 2014e115012SGarrett Wollman 2024e115012SGarrett Wollman for (;;) { 2034e115012SGarrett Wollman switch (rel) { 2044e115012SGarrett Wollman case REL_VECTOR: 2054e115012SGarrett Wollman return (!streq(type, "string")); 2064e115012SGarrett Wollman case REL_ARRAY: 2074e115012SGarrett Wollman return (0); 2084e115012SGarrett Wollman case REL_POINTER: 2094e115012SGarrett Wollman return (0); 2104e115012SGarrett Wollman case REL_ALIAS: 2114e115012SGarrett Wollman def = (definition *) FINDVAL(defined, type, typedefed); 2124e115012SGarrett Wollman if (def == NULL) { 2134e115012SGarrett Wollman return (0); 2144e115012SGarrett Wollman } 2154e115012SGarrett Wollman type = def->def.ty.old_type; 2164e115012SGarrett Wollman rel = def->def.ty.rel; 2174e115012SGarrett Wollman } 2184e115012SGarrett Wollman } 219526195adSJordan K. Hubbard 220526195adSJordan K. Hubbard return (0); 2214e115012SGarrett Wollman } 2224e115012SGarrett Wollman 223ff49530fSBill Paul char * 224e390e3afSDavid Malone locase(const char *str) 2254e115012SGarrett Wollman { 2264e115012SGarrett Wollman char c; 2274e115012SGarrett Wollman static char buf[100]; 2284e115012SGarrett Wollman char *p = buf; 2294e115012SGarrett Wollman 230526195adSJordan K. Hubbard while ( (c = *str++) ) { 2314e115012SGarrett Wollman *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c; 2324e115012SGarrett Wollman } 2334e115012SGarrett Wollman *p = 0; 2344e115012SGarrett Wollman return (buf); 2354e115012SGarrett Wollman } 2364e115012SGarrett Wollman 237ff49530fSBill Paul void 238e390e3afSDavid Malone pvname_svc(const char *pname, const char *vnum) 239ff49530fSBill Paul { 240ff49530fSBill Paul f_print(fout, "%s_%s_svc", locase(pname), vnum); 241ff49530fSBill Paul } 2424e115012SGarrett Wollman 2434e115012SGarrett Wollman void 244e390e3afSDavid Malone pvname(const char *pname, const char *vnum) 2454e115012SGarrett Wollman { 2464e115012SGarrett Wollman f_print(fout, "%s_%s", locase(pname), vnum); 2474e115012SGarrett Wollman } 2484e115012SGarrett Wollman 2494e115012SGarrett Wollman /* 2504e115012SGarrett Wollman * print a useful (?) error message, and then die 2514e115012SGarrett Wollman */ 2524e115012SGarrett Wollman void 253e390e3afSDavid Malone error(const char *msg) 2544e115012SGarrett Wollman { 2554e115012SGarrett Wollman printwhere(); 2560e76f40dSPhilippe Charnier warnx("%s, line %d: %s", infilename, linenum, msg); 2574e115012SGarrett Wollman crash(); 2584e115012SGarrett Wollman } 2594e115012SGarrett Wollman 2604e115012SGarrett Wollman /* 2614e115012SGarrett Wollman * Something went wrong, unlink any files that we may have created and then 2624e115012SGarrett Wollman * die. 2634e115012SGarrett Wollman */ 264526195adSJordan K. Hubbard void 265e390e3afSDavid Malone crash(void) 2664e115012SGarrett Wollman { 2674e115012SGarrett Wollman int i; 2684e115012SGarrett Wollman 2694e115012SGarrett Wollman for (i = 0; i < nfiles; i++) { 2704e115012SGarrett Wollman (void) unlink(outfiles[i]); 2714e115012SGarrett Wollman } 2724e115012SGarrett Wollman exit(1); 2734e115012SGarrett Wollman } 2744e115012SGarrett Wollman 2754e115012SGarrett Wollman void 276e390e3afSDavid Malone record_open(const char *file) 2774e115012SGarrett Wollman { 2784e115012SGarrett Wollman if (nfiles < NFILES) { 2794e115012SGarrett Wollman outfiles[nfiles++] = file; 2804e115012SGarrett Wollman } else { 2810e76f40dSPhilippe Charnier warnx("too many files"); 2824e115012SGarrett Wollman crash(); 2834e115012SGarrett Wollman } 2844e115012SGarrett Wollman } 2854e115012SGarrett Wollman 2864e115012SGarrett Wollman static char expectbuf[100]; 287e390e3afSDavid Malone static const char *toktostr(tok_kind kind); 2884e115012SGarrett Wollman 2894e115012SGarrett Wollman /* 2904e115012SGarrett Wollman * error, token encountered was not the expected one 2914e115012SGarrett Wollman */ 2924e115012SGarrett Wollman void 293e390e3afSDavid Malone expected1(tok_kind exp1) 2944e115012SGarrett Wollman { 2954e115012SGarrett Wollman s_print(expectbuf, "expected '%s'", 2964e115012SGarrett Wollman toktostr(exp1)); 2974e115012SGarrett Wollman error(expectbuf); 2984e115012SGarrett Wollman } 2994e115012SGarrett Wollman 3004e115012SGarrett Wollman /* 3014e115012SGarrett Wollman * error, token encountered was not one of two expected ones 3024e115012SGarrett Wollman */ 3034e115012SGarrett Wollman void 304e390e3afSDavid Malone expected2(tok_kind exp1, tok_kind exp2) 3054e115012SGarrett Wollman { 3064e115012SGarrett Wollman s_print(expectbuf, "expected '%s' or '%s'", 3074e115012SGarrett Wollman toktostr(exp1), 3084e115012SGarrett Wollman toktostr(exp2)); 3094e115012SGarrett Wollman error(expectbuf); 3104e115012SGarrett Wollman } 3114e115012SGarrett Wollman 3124e115012SGarrett Wollman /* 3134e115012SGarrett Wollman * error, token encountered was not one of 3 expected ones 3144e115012SGarrett Wollman */ 3154e115012SGarrett Wollman void 316e390e3afSDavid Malone expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3) 3174e115012SGarrett Wollman { 3184e115012SGarrett Wollman s_print(expectbuf, "expected '%s', '%s' or '%s'", 3194e115012SGarrett Wollman toktostr(exp1), 3204e115012SGarrett Wollman toktostr(exp2), 3214e115012SGarrett Wollman toktostr(exp3)); 3224e115012SGarrett Wollman error(expectbuf); 3234e115012SGarrett Wollman } 3244e115012SGarrett Wollman 3254e115012SGarrett Wollman void 326e390e3afSDavid Malone tabify(FILE *f, int tab) 3274e115012SGarrett Wollman { 3284e115012SGarrett Wollman while (tab--) { 3294e115012SGarrett Wollman (void) fputc('\t', f); 3304e115012SGarrett Wollman } 3314e115012SGarrett Wollman } 3324e115012SGarrett Wollman 3334e115012SGarrett Wollman 3344e115012SGarrett Wollman static token tokstrings[] = { 3354e115012SGarrett Wollman {TOK_IDENT, "identifier"}, 3364e115012SGarrett Wollman {TOK_CONST, "const"}, 3374e115012SGarrett Wollman {TOK_RPAREN, ")"}, 3384e115012SGarrett Wollman {TOK_LPAREN, "("}, 3394e115012SGarrett Wollman {TOK_RBRACE, "}"}, 3404e115012SGarrett Wollman {TOK_LBRACE, "{"}, 3414e115012SGarrett Wollman {TOK_LBRACKET, "["}, 3424e115012SGarrett Wollman {TOK_RBRACKET, "]"}, 3434e115012SGarrett Wollman {TOK_STAR, "*"}, 3444e115012SGarrett Wollman {TOK_COMMA, ","}, 3454e115012SGarrett Wollman {TOK_EQUAL, "="}, 3464e115012SGarrett Wollman {TOK_COLON, ":"}, 3474e115012SGarrett Wollman {TOK_SEMICOLON, ";"}, 3484e115012SGarrett Wollman {TOK_UNION, "union"}, 3494e115012SGarrett Wollman {TOK_STRUCT, "struct"}, 3504e115012SGarrett Wollman {TOK_SWITCH, "switch"}, 3514e115012SGarrett Wollman {TOK_CASE, "case"}, 3524e115012SGarrett Wollman {TOK_DEFAULT, "default"}, 3534e115012SGarrett Wollman {TOK_ENUM, "enum"}, 3544e115012SGarrett Wollman {TOK_TYPEDEF, "typedef"}, 3554e115012SGarrett Wollman {TOK_INT, "int"}, 3564e115012SGarrett Wollman {TOK_SHORT, "short"}, 3574e115012SGarrett Wollman {TOK_LONG, "long"}, 3584e115012SGarrett Wollman {TOK_UNSIGNED, "unsigned"}, 3594e115012SGarrett Wollman {TOK_DOUBLE, "double"}, 3604e115012SGarrett Wollman {TOK_FLOAT, "float"}, 3614e115012SGarrett Wollman {TOK_CHAR, "char"}, 3624e115012SGarrett Wollman {TOK_STRING, "string"}, 3634e115012SGarrett Wollman {TOK_OPAQUE, "opaque"}, 3644e115012SGarrett Wollman {TOK_BOOL, "bool"}, 3654e115012SGarrett Wollman {TOK_VOID, "void"}, 3664e115012SGarrett Wollman {TOK_PROGRAM, "program"}, 3674e115012SGarrett Wollman {TOK_VERSION, "version"}, 3684e115012SGarrett Wollman {TOK_EOF, "??????"} 3694e115012SGarrett Wollman }; 3704e115012SGarrett Wollman 371e390e3afSDavid Malone static const char * 372e390e3afSDavid Malone toktostr(tok_kind kind) 3734e115012SGarrett Wollman { 3744e115012SGarrett Wollman token *sp; 3754e115012SGarrett Wollman 3764e115012SGarrett Wollman for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++); 3774e115012SGarrett Wollman return (sp->str); 3784e115012SGarrett Wollman } 3794e115012SGarrett Wollman 380526195adSJordan K. Hubbard static void 381e390e3afSDavid Malone printbuf(void) 3824e115012SGarrett Wollman { 3834e115012SGarrett Wollman char c; 3844e115012SGarrett Wollman int i; 3854e115012SGarrett Wollman int cnt; 3864e115012SGarrett Wollman 3874e115012SGarrett Wollman # define TABSIZE 4 3884e115012SGarrett Wollman 389526195adSJordan K. Hubbard for (i = 0; (c = curline[i]); i++) { 3904e115012SGarrett Wollman if (c == '\t') { 3914e115012SGarrett Wollman cnt = 8 - (i % TABSIZE); 3924e115012SGarrett Wollman c = ' '; 3934e115012SGarrett Wollman } else { 3944e115012SGarrett Wollman cnt = 1; 3954e115012SGarrett Wollman } 3964e115012SGarrett Wollman while (cnt--) { 3974e115012SGarrett Wollman (void) fputc(c, stderr); 3984e115012SGarrett Wollman } 3994e115012SGarrett Wollman } 4004e115012SGarrett Wollman } 4014e115012SGarrett Wollman 402526195adSJordan K. Hubbard static void 403e390e3afSDavid Malone printwhere(void) 4044e115012SGarrett Wollman { 4054e115012SGarrett Wollman int i; 4064e115012SGarrett Wollman char c; 4074e115012SGarrett Wollman int cnt; 4084e115012SGarrett Wollman 4094e115012SGarrett Wollman printbuf(); 4104e115012SGarrett Wollman for (i = 0; i < where - curline; i++) { 4114e115012SGarrett Wollman c = curline[i]; 4124e115012SGarrett Wollman if (c == '\t') { 4134e115012SGarrett Wollman cnt = 8 - (i % TABSIZE); 4144e115012SGarrett Wollman } else { 4154e115012SGarrett Wollman cnt = 1; 4164e115012SGarrett Wollman } 4174e115012SGarrett Wollman while (cnt--) { 4184e115012SGarrett Wollman (void) fputc('^', stderr); 4194e115012SGarrett Wollman } 4204e115012SGarrett Wollman } 4214e115012SGarrett Wollman (void) fputc('\n', stderr); 4224e115012SGarrett Wollman } 423ff49530fSBill Paul 424ff49530fSBill Paul char * 425e390e3afSDavid Malone make_argname(const char *pname, const char *vname) 426ff49530fSBill Paul { 427ff49530fSBill Paul char *name; 428ff49530fSBill Paul 42975863a6dSPhilippe Charnier name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3); 430ff49530fSBill Paul sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT); 431ff49530fSBill Paul return (name); 432ff49530fSBill Paul } 433ff49530fSBill Paul 434ff49530fSBill Paul bas_type *typ_list_h; 435ff49530fSBill Paul bas_type *typ_list_t; 436ff49530fSBill Paul 437526195adSJordan K. Hubbard void 438e390e3afSDavid Malone add_type(int len, const char *type) 439ff49530fSBill Paul { 440ff49530fSBill Paul bas_type *ptr; 441ff49530fSBill Paul 44275863a6dSPhilippe Charnier ptr = XALLOC(bas_type); 443ff49530fSBill Paul 444ff49530fSBill Paul ptr->name = type; 445ff49530fSBill Paul ptr->length = len; 446ff49530fSBill Paul ptr->next = NULL; 447ff49530fSBill Paul if (typ_list_t == NULL) 448ff49530fSBill Paul { 449ff49530fSBill Paul 450ff49530fSBill Paul typ_list_t = ptr; 451ff49530fSBill Paul typ_list_h = ptr; 452ff49530fSBill Paul } 453ff49530fSBill Paul else 454ff49530fSBill Paul { 455ff49530fSBill Paul typ_list_t->next = ptr; 456ff49530fSBill Paul typ_list_t = ptr; 457ff49530fSBill Paul }; 458ff49530fSBill Paul } 459ff49530fSBill Paul 460ff49530fSBill Paul 461e390e3afSDavid Malone bas_type * 462e390e3afSDavid Malone find_type(const char *type) 463ff49530fSBill Paul { 464ff49530fSBill Paul bas_type * ptr; 465ff49530fSBill Paul 466ff49530fSBill Paul ptr = typ_list_h; 467ff49530fSBill Paul while (ptr != NULL) 468ff49530fSBill Paul { 469ff49530fSBill Paul if (strcmp(ptr->name, type) == 0) 470ff49530fSBill Paul return (ptr); 471ff49530fSBill Paul else 472ff49530fSBill Paul ptr = ptr->next; 473ff49530fSBill Paul }; 474ff49530fSBill Paul return (NULL); 475ff49530fSBill Paul } 47675863a6dSPhilippe Charnier 47775863a6dSPhilippe Charnier void * 47875863a6dSPhilippe Charnier xmalloc(size_t size) 47975863a6dSPhilippe Charnier { 48075863a6dSPhilippe Charnier void *p; 48175863a6dSPhilippe Charnier 48275863a6dSPhilippe Charnier if ((p = malloc(size)) == NULL) { 48375863a6dSPhilippe Charnier warnx("malloc failed"); 48475863a6dSPhilippe Charnier crash(); 48575863a6dSPhilippe Charnier } 48675863a6dSPhilippe Charnier return (p); 48775863a6dSPhilippe Charnier } 48875863a6dSPhilippe Charnier 48975863a6dSPhilippe Charnier void * 49075863a6dSPhilippe Charnier xrealloc(void *ptr, size_t size) 49175863a6dSPhilippe Charnier { 49275863a6dSPhilippe Charnier void *p; 49375863a6dSPhilippe Charnier 49475863a6dSPhilippe Charnier if ((p = realloc(ptr, size)) == NULL) { 49575863a6dSPhilippe Charnier warnx("realloc failed"); 49675863a6dSPhilippe Charnier crash(); 49775863a6dSPhilippe Charnier } 49875863a6dSPhilippe Charnier return (p); 49975863a6dSPhilippe Charnier } 50075863a6dSPhilippe Charnier 50175863a6dSPhilippe Charnier char * 50275863a6dSPhilippe Charnier xstrdup(const char *str) 50375863a6dSPhilippe Charnier { 50475863a6dSPhilippe Charnier char *p; 50575863a6dSPhilippe Charnier 50675863a6dSPhilippe Charnier if ((p = strdup(str)) == NULL) { 50775863a6dSPhilippe Charnier warnx("strdup failed"); 50875863a6dSPhilippe Charnier crash(); 50975863a6dSPhilippe Charnier } 51075863a6dSPhilippe Charnier return (p); 51175863a6dSPhilippe Charnier } 512