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