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