Annotation of src/usr.bin/rpcgen/rpc_util.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: rpc_util.c,v 1.6 1995/08/29 23:05:57 cgd Exp $ */
! 2: /*
! 3: * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
! 4: * unrestricted use provided that this legend is included on all tape
! 5: * media and as a part of the software program in whole or part. Users
! 6: * may copy or modify Sun RPC without charge, but are not authorized
! 7: * to license or distribute it to anyone else except as part of a product or
! 8: * program developed by the user or with the express written consent of
! 9: * Sun Microsystems, Inc.
! 10: *
! 11: * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
! 12: * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
! 13: * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
! 14: *
! 15: * Sun RPC is provided with no support and without any obligation on the
! 16: * part of Sun Microsystems, Inc. to assist in its use, correction,
! 17: * modification or enhancement.
! 18: *
! 19: * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
! 20: * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
! 21: * OR ANY PART THEREOF.
! 22: *
! 23: * In no event will Sun Microsystems, Inc. be liable for any lost revenue
! 24: * or profits or other special, indirect and consequential damages, even if
! 25: * Sun has been advised of the possibility of such damages.
! 26: *
! 27: * Sun Microsystems, Inc.
! 28: * 2550 Garcia Avenue
! 29: * Mountain View, California 94043
! 30: */
! 31:
! 32: #ifndef lint
! 33: static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
! 34: #endif
! 35:
! 36: /*
! 37: * rpc_util.c, Utility routines for the RPC protocol compiler
! 38: */
! 39: #include <sys/cdefs.h>
! 40: #include <stdio.h>
! 41: #include <stdlib.h>
! 42: #include <string.h>
! 43: #include <ctype.h>
! 44: #include "rpc_scan.h"
! 45: #include "rpc_parse.h"
! 46: #include "rpc_util.h"
! 47:
! 48: #define ARGEXT "argument"
! 49:
! 50: static void printwhere __P((void));
! 51:
! 52: char curline[MAXLINESIZE]; /* current read line */
! 53: char *where = curline; /* current point in line */
! 54: int linenum = 0; /* current line number */
! 55:
! 56: char *infilename; /* input filename */
! 57:
! 58: #define NFILES 7
! 59: char *outfiles[NFILES]; /* output file names */
! 60: int nfiles;
! 61:
! 62: FILE *fout; /* file pointer of current output */
! 63: FILE *fin; /* file pointer of current input */
! 64:
! 65: list *defined; /* list of defined things */
! 66:
! 67: /*
! 68: * Reinitialize the world
! 69: */
! 70: reinitialize()
! 71: {
! 72: memset(curline, 0, MAXLINESIZE);
! 73: where = curline;
! 74: linenum = 0;
! 75: defined = NULL;
! 76: }
! 77:
! 78: /*
! 79: * string equality
! 80: */
! 81: streq(a, b)
! 82: char *a;
! 83: char *b;
! 84: {
! 85: return (strcmp(a, b) == 0);
! 86: }
! 87:
! 88: /*
! 89: * find a value in a list
! 90: */
! 91: definition *
! 92: findval(lst, val, cmp)
! 93: list *lst;
! 94: char *val;
! 95: int (*cmp) ();
! 96:
! 97: {
! 98:
! 99: for (; lst != NULL; lst = lst->next) {
! 100: if ((*cmp) (lst->val, val)) {
! 101: return (lst->val);
! 102: }
! 103: }
! 104: return (NULL);
! 105: }
! 106:
! 107: /*
! 108: * store a value in a list
! 109: */
! 110: void
! 111: storeval(lstp, val)
! 112: list **lstp;
! 113: definition *val;
! 114: {
! 115: list **l;
! 116: list *lst;
! 117:
! 118:
! 119: for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
! 120: lst = ALLOC(list);
! 121: lst->val = val;
! 122: lst->next = NULL;
! 123: *l = lst;
! 124: }
! 125:
! 126: static
! 127: findit(def, type)
! 128: definition *def;
! 129: char *type;
! 130: {
! 131: return (streq(def->def_name, type));
! 132: }
! 133:
! 134: static char *
! 135: fixit(type, orig)
! 136: char *type;
! 137: char *orig;
! 138: {
! 139: definition *def;
! 140:
! 141: def = (definition *) FINDVAL(defined, type, findit);
! 142: if (def == NULL || def->def_kind != DEF_TYPEDEF) {
! 143: return (orig);
! 144: }
! 145: switch (def->def.ty.rel) {
! 146: case REL_VECTOR:
! 147: return (def->def.ty.old_type);
! 148: case REL_ALIAS:
! 149: return (fixit(def->def.ty.old_type, orig));
! 150: default:
! 151: return (orig);
! 152: }
! 153: }
! 154:
! 155: char *
! 156: fixtype(type)
! 157: char *type;
! 158: {
! 159: return (fixit(type, type));
! 160: }
! 161:
! 162: char *
! 163: stringfix(type)
! 164: char *type;
! 165: {
! 166: if (streq(type, "string")) {
! 167: return ("wrapstring");
! 168: } else {
! 169: return (type);
! 170: }
! 171: }
! 172:
! 173: void
! 174: ptype(prefix, type, follow)
! 175: char *prefix;
! 176: char *type;
! 177: int follow;
! 178: {
! 179: if (prefix != NULL) {
! 180: if (streq(prefix, "enum")) {
! 181: f_print(fout, "enum ");
! 182: } else {
! 183: f_print(fout, "struct ");
! 184: }
! 185: }
! 186: if (streq(type, "bool")) {
! 187: f_print(fout, "bool_t ");
! 188: } else if (streq(type, "string")) {
! 189: f_print(fout, "char *");
! 190: } else {
! 191: f_print(fout, "%s ", follow ? fixtype(type) : type);
! 192: }
! 193: }
! 194:
! 195: static
! 196: typedefed(def, type)
! 197: definition *def;
! 198: char *type;
! 199: {
! 200: if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
! 201: return (0);
! 202: } else {
! 203: return (streq(def->def_name, type));
! 204: }
! 205: }
! 206:
! 207: isvectordef(type, rel)
! 208: char *type;
! 209: relation rel;
! 210: {
! 211: definition *def;
! 212:
! 213: for (;;) {
! 214: switch (rel) {
! 215: case REL_VECTOR:
! 216: return (!streq(type, "string"));
! 217: case REL_ARRAY:
! 218: return (0);
! 219: case REL_POINTER:
! 220: return (0);
! 221: case REL_ALIAS:
! 222: def = (definition *) FINDVAL(defined, type, typedefed);
! 223: if (def == NULL) {
! 224: return (0);
! 225: }
! 226: type = def->def.ty.old_type;
! 227: rel = def->def.ty.rel;
! 228: }
! 229: }
! 230: }
! 231:
! 232: char *
! 233: locase(str)
! 234: char *str;
! 235: {
! 236: char c;
! 237: static char buf[100];
! 238: char *p = buf;
! 239:
! 240: while (c = *str++) {
! 241: *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
! 242: }
! 243: *p = 0;
! 244: return (buf);
! 245: }
! 246:
! 247: void
! 248: pvname_svc(pname, vnum)
! 249: char *pname;
! 250: char *vnum;
! 251: {
! 252: f_print(fout, "%s_%s_svc", locase(pname), vnum);
! 253: }
! 254:
! 255: void
! 256: pvname(pname, vnum)
! 257: char *pname;
! 258: char *vnum;
! 259: {
! 260: f_print(fout, "%s_%s", locase(pname), vnum);
! 261: }
! 262:
! 263: /*
! 264: * print a useful (?) error message, and then die
! 265: */
! 266: void
! 267: error(msg)
! 268: char *msg;
! 269: {
! 270: printwhere();
! 271: f_print(stderr, "%s, line %d: ", infilename, linenum);
! 272: f_print(stderr, "%s\n", msg);
! 273: crash();
! 274: }
! 275:
! 276: /*
! 277: * Something went wrong, unlink any files that we may have created and then
! 278: * die.
! 279: */
! 280: crash()
! 281: {
! 282: int i;
! 283:
! 284: for (i = 0; i < nfiles; i++) {
! 285: (void) unlink(outfiles[i]);
! 286: }
! 287: exit(1);
! 288: }
! 289:
! 290: void
! 291: record_open(file)
! 292: char *file;
! 293: {
! 294: if (nfiles < NFILES) {
! 295: outfiles[nfiles++] = file;
! 296: } else {
! 297: f_print(stderr, "too many files!\n");
! 298: crash();
! 299: }
! 300: }
! 301:
! 302: static char expectbuf[100];
! 303: static char *toktostr();
! 304:
! 305: /*
! 306: * error, token encountered was not the expected one
! 307: */
! 308: void
! 309: expected1(exp1)
! 310: tok_kind exp1;
! 311: {
! 312: s_print(expectbuf, "expected '%s'",
! 313: toktostr(exp1));
! 314: error(expectbuf);
! 315: }
! 316:
! 317: /*
! 318: * error, token encountered was not one of two expected ones
! 319: */
! 320: void
! 321: expected2(exp1, exp2)
! 322: tok_kind exp1, exp2;
! 323: {
! 324: s_print(expectbuf, "expected '%s' or '%s'",
! 325: toktostr(exp1),
! 326: toktostr(exp2));
! 327: error(expectbuf);
! 328: }
! 329:
! 330: /*
! 331: * error, token encountered was not one of 3 expected ones
! 332: */
! 333: void
! 334: expected3(exp1, exp2, exp3)
! 335: tok_kind exp1, exp2, exp3;
! 336: {
! 337: s_print(expectbuf, "expected '%s', '%s' or '%s'",
! 338: toktostr(exp1),
! 339: toktostr(exp2),
! 340: toktostr(exp3));
! 341: error(expectbuf);
! 342: }
! 343:
! 344: void
! 345: tabify(f, tab)
! 346: FILE *f;
! 347: int tab;
! 348: {
! 349: while (tab--) {
! 350: (void) fputc('\t', f);
! 351: }
! 352: }
! 353:
! 354:
! 355: static token tokstrings[] = {
! 356: {TOK_IDENT, "identifier"},
! 357: {TOK_CONST, "const"},
! 358: {TOK_RPAREN, ")"},
! 359: {TOK_LPAREN, "("},
! 360: {TOK_RBRACE, "}"},
! 361: {TOK_LBRACE, "{"},
! 362: {TOK_LBRACKET, "["},
! 363: {TOK_RBRACKET, "]"},
! 364: {TOK_STAR, "*"},
! 365: {TOK_COMMA, ","},
! 366: {TOK_EQUAL, "="},
! 367: {TOK_COLON, ":"},
! 368: {TOK_SEMICOLON, ";"},
! 369: {TOK_UNION, "union"},
! 370: {TOK_STRUCT, "struct"},
! 371: {TOK_SWITCH, "switch"},
! 372: {TOK_CASE, "case"},
! 373: {TOK_DEFAULT, "default"},
! 374: {TOK_ENUM, "enum"},
! 375: {TOK_TYPEDEF, "typedef"},
! 376: {TOK_INT, "int"},
! 377: {TOK_SHORT, "short"},
! 378: {TOK_LONG, "long"},
! 379: {TOK_UNSIGNED, "unsigned"},
! 380: {TOK_DOUBLE, "double"},
! 381: {TOK_FLOAT, "float"},
! 382: {TOK_CHAR, "char"},
! 383: {TOK_STRING, "string"},
! 384: {TOK_OPAQUE, "opaque"},
! 385: {TOK_BOOL, "bool"},
! 386: {TOK_VOID, "void"},
! 387: {TOK_PROGRAM, "program"},
! 388: {TOK_VERSION, "version"},
! 389: {TOK_EOF, "??????"}
! 390: };
! 391:
! 392: static char *
! 393: toktostr(kind)
! 394: tok_kind kind;
! 395: {
! 396: token *sp;
! 397:
! 398: for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
! 399: return (sp->str);
! 400: }
! 401:
! 402: static
! 403: printbuf()
! 404: {
! 405: char c;
! 406: int i;
! 407: int cnt;
! 408:
! 409: # define TABSIZE 4
! 410:
! 411: for (i = 0; c = curline[i]; i++) {
! 412: if (c == '\t') {
! 413: cnt = 8 - (i % TABSIZE);
! 414: c = ' ';
! 415: } else {
! 416: cnt = 1;
! 417: }
! 418: while (cnt--) {
! 419: (void) fputc(c, stderr);
! 420: }
! 421: }
! 422: }
! 423:
! 424: static void
! 425: printwhere()
! 426: {
! 427: int i;
! 428: char c;
! 429: int cnt;
! 430:
! 431: printbuf();
! 432: for (i = 0; i < where - curline; i++) {
! 433: c = curline[i];
! 434: if (c == '\t') {
! 435: cnt = 8 - (i % TABSIZE);
! 436: } else {
! 437: cnt = 1;
! 438: }
! 439: while (cnt--) {
! 440: (void) fputc('^', stderr);
! 441: }
! 442: }
! 443: (void) fputc('\n', stderr);
! 444: }
! 445:
! 446: char *
! 447: make_argname(pname, vname)
! 448: char *pname;
! 449: char *vname;
! 450: {
! 451: char *name;
! 452:
! 453: name = (char *)malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
! 454: if (!name) {
! 455: fprintf(stderr, "failed in malloc");
! 456: exit(1);
! 457: }
! 458: sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
! 459: return(name);
! 460: }
! 461:
! 462: bas_type *typ_list_h;
! 463: bas_type *typ_list_t;
! 464:
! 465: void
! 466: add_type(len,type)
! 467: int len;
! 468: char *type;
! 469: {
! 470: bas_type *ptr;
! 471:
! 472: if ((ptr = (bas_type *)malloc(sizeof(bas_type))) == (bas_type *)NULL) {
! 473: fprintf(stderr, "failed in malloc");
! 474: exit(1);
! 475: }
! 476:
! 477: ptr->name=type;
! 478: ptr->length=len;
! 479: ptr->next=NULL;
! 480: if (typ_list_t == NULL) {
! 481: typ_list_t=ptr;
! 482: typ_list_h=ptr;
! 483: } else {
! 484: typ_list_t->next=ptr;
! 485: typ_list_t=ptr;
! 486: }
! 487: }
! 488:
! 489: bas_type *
! 490: find_type(type)
! 491: char *type;
! 492: {
! 493: bas_type * ptr;
! 494:
! 495: ptr=typ_list_h;
! 496:
! 497:
! 498: while (ptr != NULL) {
! 499: if (strcmp(ptr->name,type) == 0)
! 500: return(ptr);
! 501: else
! 502: ptr=ptr->next;
! 503: }
! 504: return(NULL);
! 505: }
! 506: