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>
38 __FBSDID("$FreeBSD$");
41 * rpc_scan.c, Scanner for the RPC protocol compiler
42 * Copyright (C) 1987, Sun Microsystems, Inc.
45 #include <sys/types.h>
51 #include "rpc_parse.h"
55 #define startcomment(where) (where[0] == '/' && where[1] == '*')
56 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
58 static int pushed = 0; /* is a token pushed */
59 static token lasttok; /* last token, if pushed */
61 static void unget_token( token * );
62 static void findstrconst(char **, const char **);
63 static void findchrconst(char **, const char **);
64 static void findconst(char **, const char **);
65 static void findkind( char **, token * );
66 static int cppline( char * );
67 static int directive( char * );
68 static void printdirective( char * );
69 static void docppline(char *, int *, const char **);
72 * scan expecting 1 given token
75 scan(tok_kind expect, token *tokp)
78 if (tokp->kind != expect) {
84 * scan expecting any of the 2 given tokens
87 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
90 if (tokp->kind != expect1 && tokp->kind != expect2) {
91 expected2(expect1, expect2);
96 * scan expecting any of the 3 given token
99 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
102 if (tokp->kind != expect1 && tokp->kind != expect2
103 && tokp->kind != expect3) {
104 expected3(expect1, expect2, expect3);
109 * scan expecting a constant, possibly symbolic
112 scan_num(token *tokp)
115 switch (tokp->kind) {
119 error("constant or identifier expected");
124 * Peek at the next token
134 * Peek at the next token and scan it if it matches what you expect
137 peekscan(tok_kind expect, token *tokp)
140 if (tokp->kind == expect) {
148 * Get the next token, printing out any directive that are encountered.
151 get_token(token *tokp)
166 if (!fgets(curline, MAXLINESIZE, fin)) {
167 tokp->kind = TOK_EOF;
168 /* now check if cpp returned non NULL value */
169 waitpid(childpid, &stat, WUNTRACED);
171 /* Set return value from rpcgen */
172 nonfatalerrors = stat >> 8;
180 } else if (cppline(curline)) {
181 docppline(curline, &linenum,
183 } else if (directive(curline)) {
184 printdirective(curline);
190 } else if (isspace(*where)) {
191 while (isspace(*where)) {
194 } else if (commenting) {
195 for (where++; *where; where++) {
196 if (endcomment(where)) {
202 } else if (startcomment(where)) {
211 * 'where' is not whitespace, comment or directive Must be a token!
215 tokp->kind = TOK_COLON;
219 tokp->kind = TOK_SEMICOLON;
223 tokp->kind = TOK_COMMA;
227 tokp->kind = TOK_EQUAL;
231 tokp->kind = TOK_STAR;
235 tokp->kind = TOK_LBRACKET;
239 tokp->kind = TOK_RBRACKET;
243 tokp->kind = TOK_LBRACE;
247 tokp->kind = TOK_RBRACE;
251 tokp->kind = TOK_LPAREN;
255 tokp->kind = TOK_RPAREN;
259 tokp->kind = TOK_LANGLE;
263 tokp->kind = TOK_RANGLE;
268 tokp->kind = TOK_STRCONST;
269 findstrconst(&where, &tokp->str);
272 tokp->kind = TOK_CHARCONST;
273 findchrconst(&where, &tokp->str);
287 tokp->kind = TOK_IDENT;
288 findconst(&where, &tokp->str);
292 if (!(isalpha(*where) || *where == '_')) {
296 s_print(buf, "illegal character in file: ");
297 p = buf + strlen(buf);
298 if (isprint(*where)) {
299 s_print(p, "%c", *where);
301 s_print(p, "%d", *where);
305 findkind(&where, tokp);
311 unget_token(token *tokp)
318 findstrconst(char **str, const char **val)
327 } while (*p && *p != '"');
329 error("unterminated string constant");
334 (void) strlcpy(tmp, *str, size);
340 findchrconst(char **str, const char **val)
349 } while (*p && *p != '\'');
351 error("unterminated string constant");
356 error("empty char string");
359 (void) strlcpy(tmp, *str, size);
365 findconst(char **str, const char **val)
372 if (*p == '0' && *(p + 1) == 'x') {
376 } while (isxdigit(*p));
380 } while (isdigit(*p));
384 (void) strlcpy(tmp, *str, size);
389 static token symbols[] = {
390 {TOK_CONST, "const"},
391 {TOK_UNION, "union"},
392 {TOK_SWITCH, "switch"},
394 {TOK_DEFAULT, "default"},
395 {TOK_STRUCT, "struct"},
396 {TOK_TYPEDEF, "typedef"},
398 {TOK_OPAQUE, "opaque"},
403 {TOK_UNSIGNED, "unsigned"},
404 {TOK_SHORT, "short"},
406 {TOK_HYPER, "hyper"},
407 {TOK_FLOAT, "float"},
408 {TOK_DOUBLE, "double"},
409 {TOK_QUAD, "quadruple"},
410 {TOK_STRING, "string"},
411 {TOK_PROGRAM, "program"},
412 {TOK_VERSION, "version"},
417 findkind(char **mark, token *tokp)
424 for (s = symbols; s->kind != TOK_EOF; s++) {
425 len = strlen(s->str);
426 if (strncmp(str, s->str, len) == 0) {
427 if (!isalnum(str[len]) && str[len] != '_') {
428 tokp->kind = s->kind;
435 tokp->kind = TOK_IDENT;
436 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
437 tmp = xmalloc(len + 1);
438 (void) strlcpy(tmp, str, len + 1);
446 return (line == curline && *line == '#');
450 directive(char *line)
452 return (line == curline && *line == '%');
456 printdirective(char *line)
458 f_print(fout, "%s", line + 1);
462 docppline(char *line, int *lineno, const char **fname)
469 while (isspace(*line)) {
473 while (isdigit(*line)) {
476 while (isspace(*line)) {
480 error("preprocessor error");
483 p = file = xmalloc(strlen(line) + 1);
484 while (*line && *line != '"') {
488 error("preprocessor error");