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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * $FreeBSD: src/usr.bin/rpcgen/rpc_scan.c,v 1.4.8.1 2001/03/04 08:59:50 kris Exp $
30 * $DragonFly: src/usr.bin/rpcgen/rpc_scan.c,v 1.2 2003/06/17 04:29:31 dillon Exp $
32 * @(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI
35 #ident "@(#)rpc_scan.c 1.13 93/07/05 SMI"
38 * rpc_scan.c, Scanner for the RPC protocol compiler
39 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include <sys/types.h>
49 #include "rpc_parse.h"
52 #define startcomment(where) (where[0] == '/' && where[1] == '*')
53 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
55 static int pushed = 0; /* is a token pushed */
56 static token lasttok; /* last token, if pushed */
58 static void unget_token __P(( token * ));
59 static void findstrconst __P(( char **, char **));
60 static void findchrconst __P(( char **, char **));
61 static void findconst __P(( char **, char **));
62 static void findkind __P(( char **, token * ));
63 static int cppline __P(( char * ));
64 static int directive __P(( char * ));
65 static void printdirective __P(( char * ));
66 static void docppline __P(( char *, int *, char ** ));
69 * scan expecting 1 given token
77 if (tokp->kind != expect) {
83 * scan expecting any of the 2 given tokens
86 scan2(expect1, expect2, tokp)
92 if (tokp->kind != expect1 && tokp->kind != expect2) {
93 expected2(expect1, expect2);
98 * scan expecting any of the 3 given token
101 scan3(expect1, expect2, expect3, tokp)
108 if (tokp->kind != expect1 && tokp->kind != expect2
109 && tokp->kind != expect3) {
110 expected3(expect1, expect2, expect3);
115 * scan expecting a constant, possibly symbolic
122 switch (tokp->kind) {
126 error("constant or identifier expected");
131 * Peek at the next token
142 * Peek at the next token and scan it if it matches what you expect
145 peekscan(expect, tokp)
150 if (tokp->kind == expect) {
158 * Get the next token, printing out any directive that are encountered.
177 if (!fgets(curline, MAXLINESIZE, fin)) {
178 tokp->kind = TOK_EOF;
179 /* now check if cpp returned non NULL value */
180 waitpid(childpid, &stat, WUNTRACED);
182 /* Set return value from rpcgen */
183 nonfatalerrors = stat >> 8;
191 } else if (cppline(curline)) {
192 docppline(curline, &linenum,
194 } else if (directive(curline)) {
195 printdirective(curline);
201 } else if (isspace(*where)) {
202 while (isspace(*where)) {
205 } else if (commenting) {
206 for (where++; *where; where++) {
207 if (endcomment(where)) {
213 } else if (startcomment(where)) {
222 * 'where' is not whitespace, comment or directive Must be a token!
226 tokp->kind = TOK_COLON;
230 tokp->kind = TOK_SEMICOLON;
234 tokp->kind = TOK_COMMA;
238 tokp->kind = TOK_EQUAL;
242 tokp->kind = TOK_STAR;
246 tokp->kind = TOK_LBRACKET;
250 tokp->kind = TOK_RBRACKET;
254 tokp->kind = TOK_LBRACE;
258 tokp->kind = TOK_RBRACE;
262 tokp->kind = TOK_LPAREN;
266 tokp->kind = TOK_RPAREN;
270 tokp->kind = TOK_LANGLE;
274 tokp->kind = TOK_RANGLE;
279 tokp->kind = TOK_STRCONST;
280 findstrconst(&where, &tokp->str);
283 tokp->kind = TOK_CHARCONST;
284 findchrconst(&where, &tokp->str);
298 tokp->kind = TOK_IDENT;
299 findconst(&where, &tokp->str);
303 if (!(isalpha(*where) || *where == '_')) {
307 s_print(buf, "illegal character in file: ");
308 p = buf + strlen(buf);
309 if (isprint(*where)) {
310 s_print(p, "%c", *where);
312 s_print(p, "%d", *where);
316 findkind(&where, tokp);
330 findstrconst(str, val)
340 } while (*p && *p != '"');
342 error("unterminated string constant");
346 *val = alloc(size + 1);
347 (void) strncpy(*val, *str, size);
353 findchrconst(str, val)
363 } while (*p && *p != '\'');
365 error("unterminated string constant");
370 error("empty char string");
372 *val = alloc(size + 1);
373 (void) strncpy(*val, *str, size);
387 if (*p == '0' && *(p + 1) == 'x') {
391 } while (isxdigit(*p));
395 } while (isdigit(*p));
398 *val = alloc(size + 1);
399 (void) strncpy(*val, *str, size);
404 static token symbols[] = {
405 {TOK_CONST, "const"},
406 {TOK_UNION, "union"},
407 {TOK_SWITCH, "switch"},
409 {TOK_DEFAULT, "default"},
410 {TOK_STRUCT, "struct"},
411 {TOK_TYPEDEF, "typedef"},
413 {TOK_OPAQUE, "opaque"},
418 {TOK_UNSIGNED, "unsigned"},
419 {TOK_SHORT, "short"},
421 {TOK_HYPER, "hyper"},
422 {TOK_FLOAT, "float"},
423 {TOK_DOUBLE, "double"},
424 {TOK_QUAD, "quadruple"},
425 {TOK_STRING, "string"},
426 {TOK_PROGRAM, "program"},
427 {TOK_VERSION, "version"},
441 for (s = symbols; s->kind != TOK_EOF; s++) {
442 len = strlen(s->str);
443 if (strncmp(str, s->str, len) == 0) {
444 if (!isalnum(str[len]) && str[len] != '_') {
445 tokp->kind = s->kind;
452 tokp->kind = TOK_IDENT;
453 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
454 tokp->str = alloc(len + 1);
455 (void) strncpy(tokp->str, str, len);
464 return (line == curline && *line == '#');
471 return (line == curline && *line == '%');
478 f_print(fout, "%s", line + 1);
482 docppline(line, lineno, fname)
492 while (isspace(*line)) {
496 while (isdigit(*line)) {
499 while (isspace(*line)) {
503 error("preprocessor error");
506 p = file = alloc(strlen(line) + 1);
507 while (*line && *line != '"') {
511 error("preprocessor error");