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