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