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");
333 tmp = xmalloc(size + 1);
334 (void) strncpy(tmp, *str, size);
341 findchrconst(char **str, const char **val)
350 } while (*p && *p != '\'');
352 error("unterminated string constant");
357 error("empty char string");
359 tmp = xmalloc(size + 1);
360 (void) strncpy(tmp, *str, size);
367 findconst(char **str, const char **val)
374 if (*p == '0' && *(p + 1) == 'x') {
378 } while (isxdigit(*p));
382 } while (isdigit(*p));
385 tmp = xmalloc(size + 1);
386 (void) strncpy(tmp, *str, size);
392 static token symbols[] = {
393 {TOK_CONST, "const"},
394 {TOK_UNION, "union"},
395 {TOK_SWITCH, "switch"},
397 {TOK_DEFAULT, "default"},
398 {TOK_STRUCT, "struct"},
399 {TOK_TYPEDEF, "typedef"},
401 {TOK_OPAQUE, "opaque"},
406 {TOK_UNSIGNED, "unsigned"},
407 {TOK_SHORT, "short"},
409 {TOK_HYPER, "hyper"},
410 {TOK_FLOAT, "float"},
411 {TOK_DOUBLE, "double"},
412 {TOK_QUAD, "quadruple"},
413 {TOK_STRING, "string"},
414 {TOK_PROGRAM, "program"},
415 {TOK_VERSION, "version"},
420 findkind(char **mark, token *tokp)
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;
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);
450 return (line == curline && *line == '#');
454 directive(char *line)
456 return (line == curline && *line == '%');
460 printdirective(char *line)
462 f_print(fout, "%s", line + 1);
466 docppline(char *line, int *lineno, const char **fname)
473 while (isspace(*line)) {
477 while (isdigit(*line)) {
480 while (isspace(*line)) {
484 error("preprocessor error");
487 p = file = xmalloc(strlen(line) + 1);
488 while (*line && *line != '"') {
492 error("preprocessor error");