10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 21*132Srobinson */ 22*132Srobinson 23*132Srobinson /* 24*132Srobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * rpc_util.c, Utility routines for the RPC protocol compiler 430Sstevel@tonic-gate */ 440Sstevel@tonic-gate #include <stdio.h> 45*132Srobinson #include <stdlib.h> 46*132Srobinson #include <unistd.h> 470Sstevel@tonic-gate #include <ctype.h> 48*132Srobinson #include <string.h> 490Sstevel@tonic-gate #include "rpc_scan.h" 500Sstevel@tonic-gate #include "rpc_parse.h" 510Sstevel@tonic-gate #include "rpc_util.h" 520Sstevel@tonic-gate 53*132Srobinson extern void crash(void); 54*132Srobinson 55*132Srobinson static void printwhere(void); 56*132Srobinson 570Sstevel@tonic-gate #define ARGEXT "argument" 580Sstevel@tonic-gate 590Sstevel@tonic-gate char curline[MAXLINESIZE]; /* current read line */ 600Sstevel@tonic-gate char *where = curline; /* current point in line */ 610Sstevel@tonic-gate int linenum = 0; /* current line number */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate char *infilename; /* input filename */ 640Sstevel@tonic-gate 650Sstevel@tonic-gate #define NFILES 15 660Sstevel@tonic-gate char *outfiles[NFILES]; /* output file names */ 670Sstevel@tonic-gate int nfiles; 680Sstevel@tonic-gate 690Sstevel@tonic-gate FILE *fout; /* file pointer of current output */ 700Sstevel@tonic-gate FILE *fin; /* file pointer of current input */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate list *defined; /* list of defined things */ 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Reinitialize the world 760Sstevel@tonic-gate */ 77*132Srobinson void 78*132Srobinson reinitialize(void) 790Sstevel@tonic-gate { 80*132Srobinson (void) memset(curline, 0, MAXLINESIZE); 810Sstevel@tonic-gate where = curline; 820Sstevel@tonic-gate linenum = 0; 830Sstevel@tonic-gate defined = NULL; 840Sstevel@tonic-gate } 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * string equality 880Sstevel@tonic-gate */ 89*132Srobinson int 90*132Srobinson streq(char *a, char *b) 910Sstevel@tonic-gate { 920Sstevel@tonic-gate return (strcmp(a, b) == 0); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * find a value in a list 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate definition * 99*132Srobinson findval(list *lst, char *val, int (*cmp)()) 1000Sstevel@tonic-gate { 1010Sstevel@tonic-gate for (; lst != NULL; lst = lst->next) { 102*132Srobinson if ((*cmp) (lst->val, val)) 1030Sstevel@tonic-gate return (lst->val); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate return (NULL); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* 1090Sstevel@tonic-gate * store a value in a list 1100Sstevel@tonic-gate */ 1110Sstevel@tonic-gate void 112*132Srobinson storeval(list **lstp, definition *val) 1130Sstevel@tonic-gate { 1140Sstevel@tonic-gate list **l; 1150Sstevel@tonic-gate list *lst; 1160Sstevel@tonic-gate 117*132Srobinson for (l = lstp; *l != NULL; l = (list **)&(*l)->next); 118*132Srobinson lst = calloc(1, sizeof (list)); 1190Sstevel@tonic-gate lst->val = val; 1200Sstevel@tonic-gate lst->next = NULL; 1210Sstevel@tonic-gate *l = lst; 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 124*132Srobinson static int 125*132Srobinson findit(definition *def, char *type) 1260Sstevel@tonic-gate { 1270Sstevel@tonic-gate return (streq(def->def_name, type)); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate static char * 131*132Srobinson fixit(char *type, char *orig) 1320Sstevel@tonic-gate { 1330Sstevel@tonic-gate definition *def; 1340Sstevel@tonic-gate 135*132Srobinson def = (definition *)FINDVAL(defined, type, findit); 136*132Srobinson if (def == NULL || def->def_kind != DEF_TYPEDEF) 1370Sstevel@tonic-gate return (orig); 1380Sstevel@tonic-gate switch (def->def.ty.rel) { 1390Sstevel@tonic-gate case REL_VECTOR: 1400Sstevel@tonic-gate if (streq(def->def.ty.old_type, "opaque")) 1410Sstevel@tonic-gate return ("char"); 142*132Srobinson return (def->def.ty.old_type); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate case REL_ALIAS: 1450Sstevel@tonic-gate return (fixit(def->def.ty.old_type, orig)); 1460Sstevel@tonic-gate default: 1470Sstevel@tonic-gate return (orig); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate char * 152*132Srobinson fixtype(char *type) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate return (fixit(type, type)); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate char * 158*132Srobinson stringfix(char *type) 1590Sstevel@tonic-gate { 160*132Srobinson if (streq(type, "string")) 1610Sstevel@tonic-gate return ("wrapstring"); 162*132Srobinson return (type); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate void 166*132Srobinson ptype(char *prefix, char *type, int follow) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate if (prefix != NULL) { 1690Sstevel@tonic-gate if (streq(prefix, "enum")) { 1700Sstevel@tonic-gate f_print(fout, "enum "); 1710Sstevel@tonic-gate } else { 1720Sstevel@tonic-gate f_print(fout, "struct "); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate if (streq(type, "bool")) { 1760Sstevel@tonic-gate f_print(fout, "bool_t "); 1770Sstevel@tonic-gate } else if (streq(type, "string")) { 1780Sstevel@tonic-gate f_print(fout, "char *"); 1790Sstevel@tonic-gate } else if (streq(type, "oneway")) { 1800Sstevel@tonic-gate f_print(fout, "void "); 1810Sstevel@tonic-gate } else { 1820Sstevel@tonic-gate f_print(fout, "%s ", follow ? fixtype(type) : type); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 186*132Srobinson static int 187*132Srobinson typedefed(definition *def, char *type) 1880Sstevel@tonic-gate { 189*132Srobinson if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) 1900Sstevel@tonic-gate return (0); 191*132Srobinson return (streq(def->def_name, type)); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 194*132Srobinson int 195*132Srobinson isvectordef(char *type, relation rel) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate definition *def; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate for (;;) { 2000Sstevel@tonic-gate switch (rel) { 2010Sstevel@tonic-gate case REL_VECTOR: 2020Sstevel@tonic-gate return (!streq(type, "string")); 2030Sstevel@tonic-gate case REL_ARRAY: 2040Sstevel@tonic-gate return (0); 2050Sstevel@tonic-gate case REL_POINTER: 2060Sstevel@tonic-gate return (0); 2070Sstevel@tonic-gate case REL_ALIAS: 208*132Srobinson def = (definition *)FINDVAL(defined, type, typedefed); 2090Sstevel@tonic-gate if (def == NULL) 2100Sstevel@tonic-gate return (0); 2110Sstevel@tonic-gate type = def->def.ty.old_type; 2120Sstevel@tonic-gate rel = def->def.ty.rel; 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate char * 218*132Srobinson locase(char *str) 2190Sstevel@tonic-gate { 2200Sstevel@tonic-gate char c; 2210Sstevel@tonic-gate static char buf[100]; 2220Sstevel@tonic-gate char *p = buf; 2230Sstevel@tonic-gate 224*132Srobinson while (c = *str++) 2250Sstevel@tonic-gate *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c; 2260Sstevel@tonic-gate *p = 0; 2270Sstevel@tonic-gate return (buf); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate void 231*132Srobinson pvname_svc(char *pname, char *vnum) 2320Sstevel@tonic-gate { 2330Sstevel@tonic-gate f_print(fout, "%s_%s_svc", locase(pname), vnum); 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate void 237*132Srobinson pvname(char *pname, char *vnum) 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate f_print(fout, "%s_%s", locase(pname), vnum); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * print a useful (?) error message, and then die 2440Sstevel@tonic-gate */ 2450Sstevel@tonic-gate void 246*132Srobinson error(char *msg) 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate printwhere(); 2490Sstevel@tonic-gate f_print(stderr, "%s, line %d: ", infilename, linenum); 2500Sstevel@tonic-gate f_print(stderr, "%s\n", msg); 2510Sstevel@tonic-gate crash(); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * Something went wrong, unlink any files that we may have created and then 2560Sstevel@tonic-gate * die. 2570Sstevel@tonic-gate */ 258*132Srobinson void 259*132Srobinson crash(void) 2600Sstevel@tonic-gate { 2610Sstevel@tonic-gate int i; 2620Sstevel@tonic-gate 263*132Srobinson for (i = 0; i < nfiles; i++) 2640Sstevel@tonic-gate (void) unlink(outfiles[i]); 2650Sstevel@tonic-gate exit(1); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate void 269*132Srobinson record_open(char *file) 2700Sstevel@tonic-gate { 2710Sstevel@tonic-gate if (nfiles < NFILES) { 2720Sstevel@tonic-gate outfiles[nfiles++] = file; 273*132Srobinson return; 2740Sstevel@tonic-gate } 275*132Srobinson f_print(stderr, "too many files!\n"); 276*132Srobinson crash(); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate static char expectbuf[100]; 2800Sstevel@tonic-gate static char *toktostr(); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /* 2830Sstevel@tonic-gate * error, token encountered was not the expected one 2840Sstevel@tonic-gate */ 2850Sstevel@tonic-gate void 286*132Srobinson expected1(tok_kind exp1) 2870Sstevel@tonic-gate { 288*132Srobinson (void) snprintf(expectbuf, 289*132Srobinson sizeof (expectbuf), "expected '%s'", toktostr(exp1)); 2900Sstevel@tonic-gate error(expectbuf); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate /* 2940Sstevel@tonic-gate * error, token encountered was not one of two expected ones 2950Sstevel@tonic-gate */ 2960Sstevel@tonic-gate void 297*132Srobinson expected2(tok_kind exp1, tok_kind exp2) 2980Sstevel@tonic-gate { 299*132Srobinson (void) snprintf(expectbuf, 300*132Srobinson sizeof (expectbuf), "expected '%s' or '%s'", toktostr(exp1), 3010Sstevel@tonic-gate toktostr(exp2)); 3020Sstevel@tonic-gate error(expectbuf); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* 3060Sstevel@tonic-gate * error, token encountered was not one of 3 expected ones 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate void 309*132Srobinson expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3) 3100Sstevel@tonic-gate { 311*132Srobinson (void) snprintf(expectbuf, 312*132Srobinson sizeof (expectbuf), "expected '%s', '%s' or '%s'", 313*132Srobinson toktostr(exp1), toktostr(exp2), toktostr(exp3)); 3140Sstevel@tonic-gate error(expectbuf); 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate void 318*132Srobinson tabify(FILE *f, int tab) 3190Sstevel@tonic-gate { 320*132Srobinson while (tab--) 3210Sstevel@tonic-gate (void) fputc('\t', f); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate static token tokstrings[] = { 3250Sstevel@tonic-gate {TOK_IDENT, "identifier"}, 3260Sstevel@tonic-gate {TOK_CONST, "const"}, 3270Sstevel@tonic-gate {TOK_RPAREN, ")"}, 3280Sstevel@tonic-gate {TOK_LPAREN, "("}, 3290Sstevel@tonic-gate {TOK_RBRACE, "}"}, 3300Sstevel@tonic-gate {TOK_LBRACE, "{"}, 3310Sstevel@tonic-gate {TOK_LBRACKET, "["}, 3320Sstevel@tonic-gate {TOK_RBRACKET, "]"}, 3330Sstevel@tonic-gate {TOK_STAR, "*"}, 3340Sstevel@tonic-gate {TOK_COMMA, ","}, 3350Sstevel@tonic-gate {TOK_EQUAL, "="}, 3360Sstevel@tonic-gate {TOK_COLON, ":"}, 3370Sstevel@tonic-gate {TOK_SEMICOLON, ";"}, 3380Sstevel@tonic-gate {TOK_UNION, "union"}, 3390Sstevel@tonic-gate {TOK_STRUCT, "struct"}, 3400Sstevel@tonic-gate {TOK_SWITCH, "switch"}, 3410Sstevel@tonic-gate {TOK_CASE, "case"}, 3420Sstevel@tonic-gate {TOK_DEFAULT, "default"}, 3430Sstevel@tonic-gate {TOK_ENUM, "enum"}, 3440Sstevel@tonic-gate {TOK_TYPEDEF, "typedef"}, 3450Sstevel@tonic-gate {TOK_INT, "int"}, 3460Sstevel@tonic-gate {TOK_SHORT, "short"}, 3470Sstevel@tonic-gate {TOK_LONG, "long"}, 3480Sstevel@tonic-gate {TOK_UNSIGNED, "unsigned"}, 3490Sstevel@tonic-gate {TOK_DOUBLE, "double"}, 3500Sstevel@tonic-gate {TOK_FLOAT, "float"}, 3510Sstevel@tonic-gate {TOK_CHAR, "char"}, 3520Sstevel@tonic-gate {TOK_STRING, "string"}, 3530Sstevel@tonic-gate {TOK_OPAQUE, "opaque"}, 3540Sstevel@tonic-gate {TOK_BOOL, "bool"}, 3550Sstevel@tonic-gate {TOK_VOID, "void"}, 3560Sstevel@tonic-gate {TOK_PROGRAM, "program"}, 3570Sstevel@tonic-gate {TOK_VERSION, "version"}, 3580Sstevel@tonic-gate {TOK_EOF, "??????"} 3590Sstevel@tonic-gate }; 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate static char * 362*132Srobinson toktostr(tok_kind kind) 3630Sstevel@tonic-gate { 3640Sstevel@tonic-gate token *sp; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++); 3670Sstevel@tonic-gate return (sp->str); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 370*132Srobinson static void 371*132Srobinson printbuf(void) 3720Sstevel@tonic-gate { 3730Sstevel@tonic-gate char c; 3740Sstevel@tonic-gate int i; 3750Sstevel@tonic-gate int cnt; 3760Sstevel@tonic-gate 377*132Srobinson #define TABSIZE 4 3780Sstevel@tonic-gate 379*132Srobinson for (i = 0; (c = curline[i]) != '\0'; i++) { 3800Sstevel@tonic-gate if (c == '\t') { 3810Sstevel@tonic-gate cnt = 8 - (i % TABSIZE); 3820Sstevel@tonic-gate c = ' '; 3830Sstevel@tonic-gate } else { 3840Sstevel@tonic-gate cnt = 1; 3850Sstevel@tonic-gate } 386*132Srobinson while (cnt--) 3870Sstevel@tonic-gate (void) fputc(c, stderr); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate 391*132Srobinson static void 392*132Srobinson printwhere(void) 3930Sstevel@tonic-gate { 3940Sstevel@tonic-gate int i; 3950Sstevel@tonic-gate char c; 3960Sstevel@tonic-gate int cnt; 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate printbuf(); 3990Sstevel@tonic-gate for (i = 0; i < where - curline; i++) { 4000Sstevel@tonic-gate c = curline[i]; 4010Sstevel@tonic-gate if (c == '\t') { 4020Sstevel@tonic-gate cnt = 8 - (i % TABSIZE); 4030Sstevel@tonic-gate } else { 4040Sstevel@tonic-gate cnt = 1; 4050Sstevel@tonic-gate } 406*132Srobinson while (cnt--) 4070Sstevel@tonic-gate (void) fputc('^', stderr); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate (void) fputc('\n', stderr); 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate char * 413*132Srobinson make_argname(char *pname, char *vname) 4140Sstevel@tonic-gate { 4150Sstevel@tonic-gate char *name; 416*132Srobinson size_t nlen; 4170Sstevel@tonic-gate 418*132Srobinson nlen = strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3; 419*132Srobinson name = malloc(nlen); 420*132Srobinson if (name == NULL) { 421*132Srobinson (void) fprintf(stderr, "failed in malloc"); 4220Sstevel@tonic-gate exit(1); 4230Sstevel@tonic-gate } 424*132Srobinson (void) snprintf(name, nlen, "%s_%s_%s", locase(pname), vname, ARGEXT); 4250Sstevel@tonic-gate return (name); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate bas_type *typ_list_h; 4290Sstevel@tonic-gate bas_type *typ_list_t; 4300Sstevel@tonic-gate 431*132Srobinson void 432*132Srobinson add_type(int len, char *type) 4330Sstevel@tonic-gate { 4340Sstevel@tonic-gate bas_type *ptr; 4350Sstevel@tonic-gate 436*132Srobinson if ((ptr = malloc(sizeof (bas_type))) == NULL) { 437*132Srobinson (void) fprintf(stderr, "failed in malloc"); 4380Sstevel@tonic-gate exit(1); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate ptr->name = type; 4420Sstevel@tonic-gate ptr->length = len; 4430Sstevel@tonic-gate ptr->next = NULL; 444*132Srobinson if (typ_list_t == NULL) { 4450Sstevel@tonic-gate typ_list_t = ptr; 4460Sstevel@tonic-gate typ_list_h = ptr; 447*132Srobinson } else { 4480Sstevel@tonic-gate typ_list_t->next = ptr; 4490Sstevel@tonic-gate typ_list_t = ptr; 450*132Srobinson } 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate 454*132Srobinson bas_type * 455*132Srobinson find_type(char *type) 4560Sstevel@tonic-gate { 457*132Srobinson bas_type *ptr; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate ptr = typ_list_h; 460*132Srobinson while (ptr != NULL) { 4610Sstevel@tonic-gate if (strcmp(ptr->name, type) == 0) 4620Sstevel@tonic-gate return (ptr); 463*132Srobinson ptr = ptr->next; 464*132Srobinson } 4650Sstevel@tonic-gate return (NULL); 4660Sstevel@tonic-gate } 467