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