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