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
32 #ident "@(#)rpc_scan.c 1.13 93/07/05 SMI"
33 static char sccsid[] = "@(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI";
37 #include <sys/cdefs.h>
39 * rpc_scan.c, Scanner for the RPC protocol compiler
40 * Copyright (C) 1987, Sun Microsystems, Inc.
43 #include <sys/types.h>
49 #include "rpc_parse.h"
53 #define startcomment(where) (where[0] == '/' && where[1] == '*')
54 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
56 static int pushed = 0; /* is a token pushed */
57 static token lasttok; /* last token, if pushed */
59 static void unget_token( token * );
60 static void findstrconst(char **, const char **);
61 static void findchrconst(char **, const char **);
62 static void findconst(char **, const char **);
63 static void findkind( char **, token * );
64 static int cppline( char * );
65 static int directive( char * );
66 static void printdirective( char * );
67 static void docppline(char *, int *, const char **);
70 * scan expecting 1 given token
73 scan(tok_kind expect, token *tokp)
76 if (tokp->kind != expect) {
82 * scan expecting any of the 2 given tokens
85 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
88 if (tokp->kind != expect1 && tokp->kind != expect2) {
89 expected2(expect1, expect2);
94 * scan expecting any of the 3 given token
97 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
100 if (tokp->kind != expect1 && tokp->kind != expect2
101 && tokp->kind != expect3) {
102 expected3(expect1, expect2, expect3);
107 * scan expecting a constant, possibly symbolic
110 scan_num(token *tokp)
113 switch (tokp->kind) {
117 error("constant or identifier expected");
122 * Peek at the next token
132 * Peek at the next token and scan it if it matches what you expect
135 peekscan(tok_kind expect, token *tokp)
138 if (tokp->kind == expect) {
146 * Get the next token, printing out any directive that are encountered.
149 get_token(token *tokp)
164 if (!fgets(curline, MAXLINESIZE, fin)) {
165 tokp->kind = TOK_EOF;
166 /* now check if cpp returned non NULL value */
167 waitpid(childpid, &stat, WUNTRACED);
169 /* Set return value from rpcgen */
170 nonfatalerrors = stat >> 8;
178 } else if (cppline(curline)) {
179 docppline(curline, &linenum,
181 } else if (directive(curline)) {
182 printdirective(curline);
188 } else if (isspace(*where)) {
189 while (isspace(*where)) {
192 } else if (commenting) {
193 for (where++; *where; where++) {
194 if (endcomment(where)) {
200 } else if (startcomment(where)) {
209 * 'where' is not whitespace, comment or directive Must be a token!
213 tokp->kind = TOK_COLON;
217 tokp->kind = TOK_SEMICOLON;
221 tokp->kind = TOK_COMMA;
225 tokp->kind = TOK_EQUAL;
229 tokp->kind = TOK_STAR;
233 tokp->kind = TOK_LBRACKET;
237 tokp->kind = TOK_RBRACKET;
241 tokp->kind = TOK_LBRACE;
245 tokp->kind = TOK_RBRACE;
249 tokp->kind = TOK_LPAREN;
253 tokp->kind = TOK_RPAREN;
257 tokp->kind = TOK_LANGLE;
261 tokp->kind = TOK_RANGLE;
266 tokp->kind = TOK_STRCONST;
267 findstrconst(&where, &tokp->str);
270 tokp->kind = TOK_CHARCONST;
271 findchrconst(&where, &tokp->str);
285 tokp->kind = TOK_IDENT;
286 findconst(&where, &tokp->str);
290 if (!(isalpha(*where) || *where == '_')) {
294 s_print(buf, "illegal character in file: ");
295 p = buf + strlen(buf);
296 if (isprint(*where)) {
297 s_print(p, "%c", *where);
299 s_print(p, "%d", *where);
303 findkind(&where, tokp);
309 unget_token(token *tokp)
316 findstrconst(char **str, const char **val)
325 } while (*p && *p != '"');
327 error("unterminated string constant");
332 (void) strlcpy(tmp, *str, size);
338 findchrconst(char **str, const char **val)
347 } while (*p && *p != '\'');
349 error("unterminated string constant");
354 error("empty char string");
357 (void) strlcpy(tmp, *str, size);
363 findconst(char **str, const char **val)
370 if (*p == '0' && *(p + 1) == 'x') {
374 } while (isxdigit(*p));
378 } while (isdigit(*p));
382 (void) strlcpy(tmp, *str, size);
387 static token symbols[] = {
388 {TOK_CONST, "const"},
389 {TOK_UNION, "union"},
390 {TOK_SWITCH, "switch"},
392 {TOK_DEFAULT, "default"},
393 {TOK_STRUCT, "struct"},
394 {TOK_TYPEDEF, "typedef"},
396 {TOK_OPAQUE, "opaque"},
401 {TOK_UNSIGNED, "unsigned"},
402 {TOK_SHORT, "short"},
404 {TOK_HYPER, "hyper"},
405 {TOK_FLOAT, "float"},
406 {TOK_DOUBLE, "double"},
407 {TOK_QUAD, "quadruple"},
408 {TOK_STRING, "string"},
409 {TOK_PROGRAM, "program"},
410 {TOK_VERSION, "version"},
415 findkind(char **mark, token *tokp)
422 for (s = symbols; s->kind != TOK_EOF; s++) {
423 len = strlen(s->str);
424 if (strncmp(str, s->str, len) == 0) {
425 if (!isalnum(str[len]) && str[len] != '_') {
426 tokp->kind = s->kind;
433 tokp->kind = TOK_IDENT;
434 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
435 tmp = xmalloc(len + 1);
436 (void) strlcpy(tmp, str, len + 1);
444 return (line == curline && *line == '#');
448 directive(char *line)
450 return (line == curline && *line == '%');
454 printdirective(char *line)
456 f_print(fout, "%s", line + 1);
460 docppline(char *line, int *lineno, const char **fname)
467 while (isspace(*line)) {
471 while (isdigit(*line)) {
474 while (isspace(*line)) {
478 error("preprocessor error");
481 p = file = xmalloc(strlen(line) + 1);
482 while (*line && *line != '"') {
486 error("preprocessor error");