Home | History | Annotate | Line # | Download | only in rpcgen
rpc_util.c revision 1.14
      1 /*	$NetBSD: rpc_util.c,v 1.14 2015/05/09 21:44:47 christos 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 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(__RCSID) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
     40 #else
     41 __RCSID("$NetBSD: rpc_util.c,v 1.14 2015/05/09 21:44:47 christos Exp $");
     42 #endif
     43 #endif
     44 
     45 /*
     46  * rpc_util.c, Utility routines for the RPC protocol compiler
     47  */
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <unistd.h>
     51 #include <string.h>
     52 #include <err.h>
     53 #include <ctype.h>
     54 #include "rpc_scan.h"
     55 #include "rpc_parse.h"
     56 #include "rpc_util.h"
     57 
     58 #define ARGEXT "argument"
     59 
     60 static void printwhere(void);
     61 
     62 char    curline[MAXLINESIZE];	/* current read line */
     63 char   *where = curline;	/* current point in line */
     64 int     linenum = 0;		/* current line number */
     65 
     66 const char *infilename;		/* input filename */
     67 
     68 #define NFILES 7
     69 static const char *outfiles[NFILES];	/* output file names */
     70 int     nfiles;
     71 
     72 FILE   *fout;			/* file pointer of current output */
     73 FILE   *fin;			/* file pointer of current input */
     74 
     75 list   *defined;		/* list of defined things */
     76 
     77 static const char *toktostr(tok_kind);
     78 static void printbuf(void);
     79 static void printwhere(void);
     80 static int findit(definition *, const char *);
     81 static const char *fixit(const char *, const char *);
     82 static int typedefed(definition *, const char *);
     83 
     84 /*
     85  * Reinitialize the world
     86  */
     87 void
     88 reinitialize(void)
     89 {
     90 	memset(curline, 0, MAXLINESIZE);
     91 	where = curline;
     92 	linenum = 0;
     93 	defined = NULL;
     94 }
     95 /*
     96  * string equality
     97  */
     98 int
     99 streq(const char *a, const char *b)
    100 {
    101 	return (strcmp(a, b) == 0);
    102 }
    103 /*
    104  * find a value in a list
    105  */
    106 definition *
    107 findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
    108 {
    109 
    110 	for (; lst != NULL; lst = lst->next) {
    111 		if ((*cmp) (lst->val, val)) {
    112 			return (lst->val);
    113 		}
    114 	}
    115 	return (NULL);
    116 }
    117 /*
    118  * store a value in a list
    119  */
    120 void
    121 storeval(list **lstp, definition *val)
    122 {
    123 	list  **l;
    124 	list   *lst;
    125 
    126 
    127 	for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
    128 	lst = ALLOC(list);
    129 	lst->val = val;
    130 	lst->next = NULL;
    131 	*l = lst;
    132 }
    133 
    134 static int
    135 findit(definition *def, const char *type)
    136 {
    137 	return (streq(def->def_name, type));
    138 }
    139 
    140 static const char *
    141 fixit(const char *type, const char *orig)
    142 {
    143 	definition *def;
    144 
    145 	def = (definition *) FINDVAL(defined, type, findit);
    146 	if (def == NULL || def->def_kind != DEF_TYPEDEF) {
    147 		return (orig);
    148 	}
    149 	switch (def->def.ty.rel) {
    150 	case REL_VECTOR:
    151 		return (def->def.ty.old_type);
    152 	case REL_ALIAS:
    153 		return (fixit(def->def.ty.old_type, orig));
    154 	default:
    155 		return (orig);
    156 	}
    157 }
    158 
    159 const char *
    160 fixtype(const char *type)
    161 {
    162 	return (fixit(type, type));
    163 }
    164 
    165 const char *
    166 stringfix(const char *type)
    167 {
    168 	if (streq(type, "string")) {
    169 		return ("wrapstring");
    170 	} else {
    171 		return (type);
    172 	}
    173 }
    174 
    175 void
    176 ptype(const char *prefix, const char *type, int follow)
    177 {
    178 	if (prefix != NULL) {
    179 		if (streq(prefix, "enum")) {
    180 			f_print(fout, "enum ");
    181 		} else {
    182 			f_print(fout, "struct ");
    183 		}
    184 	}
    185 	if (streq(type, "bool")) {
    186 		f_print(fout, "bool_t ");
    187 	} else
    188 		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 int
    196 typedefed(definition *def, const char *type)
    197 {
    198 	if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
    199 		return (0);
    200 	} else {
    201 		return (streq(def->def_name, type));
    202 	}
    203 }
    204 
    205 int
    206 isvectordef(const char *type, relation rel)
    207 {
    208 	definition *def;
    209 
    210 	for (;;) {
    211 		switch (rel) {
    212 		case REL_VECTOR:
    213 			return (!streq(type, "string"));
    214 		case REL_ARRAY:
    215 			return (0);
    216 		case REL_POINTER:
    217 			return (0);
    218 		case REL_ALIAS:
    219 			def = (definition *) FINDVAL(defined, type, typedefed);
    220 			if (def == NULL) {
    221 				return (0);
    222 			}
    223 			type = def->def.ty.old_type;
    224 			rel = def->def.ty.rel;
    225 		}
    226 	}
    227 }
    228 
    229 char   *
    230 locase(const char *str)
    231 {
    232 	char    c;
    233 	static char buf[100];
    234 	char   *p = buf;
    235 
    236 	while ((c = *str++) != '\0') {
    237 		*p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
    238 	}
    239 	*p = 0;
    240 	return (buf);
    241 }
    242 
    243 void
    244 pvname_svc(const char *pname, const char *vnum)
    245 {
    246 	f_print(fout, "%s_%s_svc", locase(pname), vnum);
    247 }
    248 
    249 void
    250 pvname(const char *pname, const char *vnum)
    251 {
    252 	f_print(fout, "%s_%s", locase(pname), vnum);
    253 }
    254 /*
    255  * print a useful (?) error message, and then die
    256  */
    257 void
    258 error(const char *msg)
    259 {
    260 	printwhere();
    261 	errx(EXIT_FAILURE, "%s, line %d: %s", infilename, linenum, msg);
    262 }
    263 /*
    264  * Something went wrong, unlink any files that we may have created and then
    265  * die.
    266  */
    267 void
    268 crash(void)
    269 {
    270 	int     i;
    271 
    272 	if (!docleanup)
    273 		return;
    274 
    275 	for (i = 0; i < nfiles; i++) {
    276 		(void) unlink(outfiles[i]);
    277 	}
    278 }
    279 
    280 void
    281 record_open(const char *file)
    282 {
    283 	if (nfiles < NFILES) {
    284 		outfiles[nfiles++] = file;
    285 	} else {
    286 		errx(EXIT_FAILURE, "too many files!");
    287 	}
    288 }
    289 
    290 static char expectbuf[100];
    291 
    292 /*
    293  * error, token encountered was not the expected one
    294  */
    295 void
    296 expected1(tok_kind exp1)
    297 {
    298 	s_print(expectbuf, "expected '%s'",
    299 	    toktostr(exp1));
    300 	error(expectbuf);
    301 }
    302 /*
    303  * error, token encountered was not one of two expected ones
    304  */
    305 void
    306 expected2(tok_kind exp1, tok_kind exp2)
    307 {
    308 	s_print(expectbuf, "expected '%s' or '%s'",
    309 	    toktostr(exp1),
    310 	    toktostr(exp2));
    311 	error(expectbuf);
    312 }
    313 /*
    314  * error, token encountered was not one of 3 expected ones
    315  */
    316 void
    317 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
    318 {
    319 	s_print(expectbuf, "expected '%s', '%s' or '%s'",
    320 	    toktostr(exp1),
    321 	    toktostr(exp2),
    322 	    toktostr(exp3));
    323 	error(expectbuf);
    324 }
    325 
    326 void
    327 tabify(FILE *f, int tab)
    328 {
    329 	while (tab--) {
    330 		(void) fputc('\t', f);
    331 	}
    332 }
    333 
    334 
    335 static token tokstrings[] = {
    336 	{TOK_IDENT, "identifier"},
    337 	{TOK_CONST, "const"},
    338 	{TOK_RPAREN, ")"},
    339 	{TOK_LPAREN, "("},
    340 	{TOK_RBRACE, "}"},
    341 	{TOK_LBRACE, "{"},
    342 	{TOK_LBRACKET, "["},
    343 	{TOK_RBRACKET, "]"},
    344 	{TOK_STAR, "*"},
    345 	{TOK_COMMA, ","},
    346 	{TOK_EQUAL, "="},
    347 	{TOK_COLON, ":"},
    348 	{TOK_SEMICOLON, ";"},
    349 	{TOK_UNION, "union"},
    350 	{TOK_STRUCT, "struct"},
    351 	{TOK_SWITCH, "switch"},
    352 	{TOK_CASE, "case"},
    353 	{TOK_DEFAULT, "default"},
    354 	{TOK_ENUM, "enum"},
    355 	{TOK_TYPEDEF, "typedef"},
    356 	{TOK_INT, "int"},
    357 	{TOK_SHORT, "short"},
    358 	{TOK_LONG, "long"},
    359 	{TOK_UNSIGNED, "unsigned"},
    360 	{TOK_DOUBLE, "double"},
    361 	{TOK_FLOAT, "float"},
    362 	{TOK_CHAR, "char"},
    363 	{TOK_STRING, "string"},
    364 	{TOK_OPAQUE, "opaque"},
    365 	{TOK_BOOL, "bool"},
    366 	{TOK_VOID, "void"},
    367 	{TOK_PROGRAM, "program"},
    368 	{TOK_VERSION, "version"},
    369 	{TOK_EOF, "??????"}
    370 };
    371 
    372 static const char *
    373 toktostr(tok_kind kind)
    374 {
    375 	token  *sp;
    376 
    377 	for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
    378 	return (sp->str);
    379 }
    380 
    381 static void
    382 printbuf(void)
    383 {
    384 	char    c;
    385 	int     i;
    386 	int     cnt;
    387 
    388 #define TABSIZE 4
    389 
    390 	for (i = 0; (c = curline[i]) != '\0'; i++) {
    391 		if (c == '\t') {
    392 			cnt = 8 - (i % TABSIZE);
    393 			c = ' ';
    394 		} else {
    395 			cnt = 1;
    396 		}
    397 		while (cnt--) {
    398 			(void) fputc(c, stderr);
    399 		}
    400 	}
    401 }
    402 
    403 static void
    404 printwhere(void)
    405 {
    406 	int     i;
    407 	char    c;
    408 	int     cnt;
    409 
    410 	printbuf();
    411 	for (i = 0; i < where - curline; i++) {
    412 		c = curline[i];
    413 		if (c == '\t') {
    414 			cnt = 8 - (i % TABSIZE);
    415 		} else {
    416 			cnt = 1;
    417 		}
    418 		while (cnt--) {
    419 			(void) fputc('^', stderr);
    420 		}
    421 	}
    422 	(void) fputc('\n', stderr);
    423 }
    424 
    425 char   *
    426 make_argname(const char *pname, const char *vname)
    427 {
    428 	char   *name;
    429 	size_t len;
    430 
    431 	len = strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3;
    432 	name = malloc(len);
    433 	if (!name) {
    434 		fprintf(stderr, "failed in malloc");
    435 		exit(1);
    436 	}
    437 	snprintf(name, len, "%s_%s_%s", locase(pname), vname, ARGEXT);
    438 	return (name);
    439 }
    440 
    441 bas_type *typ_list_h;
    442 bas_type *typ_list_t;
    443 
    444 void
    445 add_type(int len, const char *type)
    446 {
    447 	bas_type *ptr;
    448 
    449 	if ((ptr = malloc(sizeof(bas_type))) == NULL) {
    450 		fprintf(stderr, "failed in malloc");
    451 		exit(1);
    452 	}
    453 	ptr->name = type;
    454 	ptr->length = len;
    455 	ptr->next = NULL;
    456 	if (typ_list_t == NULL) {
    457 		typ_list_t = ptr;
    458 		typ_list_h = ptr;
    459 	} else {
    460 		typ_list_t->next = ptr;
    461 		typ_list_t = ptr;
    462 	}
    463 }
    464 
    465 bas_type *
    466 find_type(const char *type)
    467 {
    468 	bas_type *ptr;
    469 
    470 	ptr = typ_list_h;
    471 
    472 
    473 	while (ptr != NULL) {
    474 		if (strcmp(ptr->name, type) == 0)
    475 			return (ptr);
    476 		else
    477 			ptr = ptr->next;
    478 	}
    479 	return (NULL);
    480 }
    481