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