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_scan.c 1.13 93/07/05 SMI" 33static char sccsid[] = "@(#)rpc_scan.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_scan.c 152398 2005-11-13 21:17:24Z dwmalone $"); |
39 40/* 41 * rpc_scan.c, Scanner for the RPC protocol compiler 42 * Copyright (C) 1987, Sun Microsystems, Inc. 43 */ 44 45#include <sys/types.h> 46 47#include <sys/wait.h> 48#include <stdio.h> 49#include <ctype.h> 50#include <string.h> |
51#include "rpc_parse.h" |
52#include "rpc_scan.h" |
53#include "rpc_util.h" 54 55#define startcomment(where) (where[0] == '/' && where[1] == '*') 56#define endcomment(where) (where[-1] == '*' && where[0] == '/') 57 58static int pushed = 0; /* is a token pushed */ 59static token lasttok; /* last token, if pushed */ 60 61static void unget_token( token * ); |
62static void findstrconst(char **, const char **); 63static void findchrconst(char **, const char **); 64static void findconst(char **, const char **); |
65static void findkind( char **, token * ); 66static int cppline( char * ); 67static int directive( char * ); 68static void printdirective( char * ); |
69static void docppline(char *, int *, const char **); |
70 71/* 72 * scan expecting 1 given token 73 */ 74void |
75scan(tok_kind expect, token *tokp) |
76{ 77 get_token(tokp); 78 if (tokp->kind != expect) { 79 expected1(expect); 80 } 81} 82 83/* 84 * scan expecting any of the 2 given tokens 85 */ 86void |
87scan2(tok_kind expect1, tok_kind expect2, token *tokp) |
88{ 89 get_token(tokp); 90 if (tokp->kind != expect1 && tokp->kind != expect2) { 91 expected2(expect1, expect2); 92 } 93} 94 95/* 96 * scan expecting any of the 3 given token 97 */ 98void |
99scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp) |
100{ 101 get_token(tokp); 102 if (tokp->kind != expect1 && tokp->kind != expect2 103 && tokp->kind != expect3) { 104 expected3(expect1, expect2, expect3); 105 } 106} 107 108/* 109 * scan expecting a constant, possibly symbolic 110 */ 111void |
112scan_num(token *tokp) |
113{ 114 get_token(tokp); 115 switch (tokp->kind) { 116 case TOK_IDENT: 117 break; 118 default: 119 error("constant or identifier expected"); 120 } 121} 122 123/* 124 * Peek at the next token 125 */ 126void |
127peek(token *tokp) |
128{ 129 get_token(tokp); 130 unget_token(tokp); 131} 132 133/* 134 * Peek at the next token and scan it if it matches what you expect 135 */ 136int |
137peekscan(tok_kind expect, token *tokp) |
138{ 139 peek(tokp); 140 if (tokp->kind == expect) { 141 get_token(tokp); 142 return (1); 143 } 144 return (0); 145} 146 147/* 148 * Get the next token, printing out any directive that are encountered. 149 */ 150void |
151get_token(token *tokp) |
152{ 153 int commenting; 154 int stat = 0; 155 156 157 if (pushed) { 158 pushed = 0; 159 *tokp = lasttok; --- 143 unchanged lines hidden (view full) --- 303 error(buf); 304 } 305 findkind(&where, tokp); 306 break; 307 } 308} 309 310static void |
311unget_token(token *tokp) |
312{ 313 lasttok = *tokp; 314 pushed = 1; 315} 316 317static void |
318findstrconst(char **str, const char **val) |
319{ 320 char *p; |
321 char *tmp; |
322 int size; 323 324 p = *str; 325 do { 326 p++; 327 } while (*p && *p != '"'); 328 if (*p == 0) { 329 error("unterminated string constant"); 330 } 331 p++; 332 size = p - *str; |
333 tmp = xmalloc(size + 1); 334 (void) strncpy(tmp, *str, size); 335 tmp[size] = 0; 336 *val = tmp; |
337 *str = p; 338} 339 340static void |
341findchrconst(char **str, const char **val) |
342{ 343 char *p; |
344 char *tmp; |
345 int size; 346 347 p = *str; 348 do { 349 p++; 350 } while (*p && *p != '\''); 351 if (*p == 0) { 352 error("unterminated string constant"); 353 } 354 p++; 355 size = p - *str; 356 if (size != 3) { 357 error("empty char string"); 358 } |
359 tmp = xmalloc(size + 1); 360 (void) strncpy(tmp, *str, size); 361 tmp[size] = 0; 362 *val = tmp; |
363 *str = p; 364} 365 366static void |
367findconst(char **str, const char **val) |
368{ 369 char *p; |
370 char *tmp; |
371 int size; 372 373 p = *str; 374 if (*p == '0' && *(p + 1) == 'x') { 375 p++; 376 do { 377 p++; 378 } while (isxdigit(*p)); 379 } else { 380 do { 381 p++; 382 } while (isdigit(*p)); 383 } 384 size = p - *str; |
385 tmp = xmalloc(size + 1); 386 (void) strncpy(tmp, *str, size); 387 tmp[size] = 0; 388 *val = tmp; |
389 *str = p; 390} 391 392static token symbols[] = { 393 {TOK_CONST, "const"}, 394 {TOK_UNION, "union"}, 395 {TOK_SWITCH, "switch"}, 396 {TOK_CASE, "case"}, --- 15 unchanged lines hidden (view full) --- 412 {TOK_QUAD, "quadruple"}, 413 {TOK_STRING, "string"}, 414 {TOK_PROGRAM, "program"}, 415 {TOK_VERSION, "version"}, 416 {TOK_EOF, "??????"}, 417}; 418 419static void |
420findkind(char **mark, token *tokp) |
421{ 422 int len; 423 token *s; |
424 char *str, *tmp; |
425 426 str = *mark; 427 for (s = symbols; s->kind != TOK_EOF; s++) { 428 len = strlen(s->str); 429 if (strncmp(str, s->str, len) == 0) { 430 if (!isalnum(str[len]) && str[len] != '_') { 431 tokp->kind = s->kind; 432 tokp->str = s->str; 433 *mark = str + len; 434 return; 435 } 436 } 437 } 438 tokp->kind = TOK_IDENT; 439 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++); |
440 tmp = xmalloc(len + 1); 441 (void) strncpy(tmp, str, len); 442 tmp[len] = 0; 443 tokp->str = tmp; |
444 *mark = str + len; 445} 446 447static int |
448cppline(char *line) |
449{ 450 return (line == curline && *line == '#'); 451} 452 453static int |
454directive(char *line) |
455{ 456 return (line == curline && *line == '%'); 457} 458 459static void |
460printdirective(char *line) |
461{ 462 f_print(fout, "%s", line + 1); 463} 464 465static void |
466docppline(char *line, int *lineno, const char **fname) |
467{ 468 char *file; 469 int num; 470 char *p; 471 472 line++; 473 while (isspace(*line)) { 474 line++; --- 27 unchanged lines hidden --- |