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. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 */ 29 30 #ident "@(#)rpc_util.c 1.14 93/07/05 SMI" 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 * Copyright (C) 1989, Sun Microsystems, Inc. 39 */ 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 char curline[MAXLINESIZE]; /* current read line */ 49 char *where = curline; /* current point in line */ 50 int linenum = 0; /* current line number */ 51 52 char *infilename; /* input filename */ 53 54 #define NFILES 7 55 char *outfiles[NFILES]; /* output file names */ 56 int nfiles; 57 58 FILE *fout; /* file pointer of current output */ 59 FILE *fin; /* file pointer of current input */ 60 61 list *defined; /* list of defined things */ 62 63 static int printwhere __P(( void )); 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 for (; lst != NULL; lst = lst->next) { 97 if ((*cmp) (lst->val, val)) { 98 return (lst->val); 99 } 100 } 101 return (NULL); 102 } 103 104 /* 105 * store a value in a list 106 */ 107 void 108 storeval(lstp, val) 109 list **lstp; 110 definition *val; 111 { 112 list **l; 113 list *lst; 114 115 for (l = lstp; *l != NULL; l = (list **) & (*l)->next); 116 lst = ALLOC(list); 117 lst->val = val; 118 lst->next = NULL; 119 *l = lst; 120 } 121 122 static 123 findit(def, type) 124 definition *def; 125 char *type; 126 { 127 return (streq(def->def_name, type)); 128 } 129 130 static char * 131 fixit(type, orig) 132 char *type; 133 char *orig; 134 { 135 definition *def; 136 137 def = (definition *) FINDVAL(defined, type, findit); 138 if (def == NULL || def->def_kind != DEF_TYPEDEF) { 139 return (orig); 140 } 141 switch (def->def.ty.rel) { 142 case REL_VECTOR: 143 if (streq(def->def.ty.old_type, "opaque")) 144 return ("char"); 145 else 146 return (def->def.ty.old_type); 147 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 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 = 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 add_type(len, type) 466 int len; 467 char *type; 468 { 469 bas_type *ptr; 470 471 if ((ptr = (bas_type *) malloc(sizeof (bas_type))) == 472 (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 { 482 483 typ_list_t = ptr; 484 typ_list_h = ptr; 485 } 486 else 487 { 488 typ_list_t->next = ptr; 489 typ_list_t = ptr; 490 }; 491 } 492 493 494 bas_type *find_type(type) 495 char *type; 496 { 497 bas_type * ptr; 498 499 ptr = typ_list_h; 500 while (ptr != NULL) 501 { 502 if (strcmp(ptr->name, type) == 0) 503 return (ptr); 504 else 505 ptr = ptr->next; 506 }; 507 return (NULL); 508 } 509