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 or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 static char sccsid[] = "@(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI";
36 * rpc_scan.c, Scanner for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsystems, Inc.
43 #include "rpc_parse.h"
46 static void unget_token(token *tokp);
47 static void findstrconst(char **str, char **val);
48 static void findchrconst(char **str, char **val);
49 static void findconst(char **str, char **val);
50 static void findkind(char **mark, token *tokp);
51 static int cppline(char *line);
52 static int directive(char *line);
53 static void printdirective(char *line);
54 static void docppline(char *line, int *lineno, char **fname);
56 #define startcomment(where) (where[0] == '/' && where[1] == '*')
57 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
59 static int pushed = 0; /* is a token pushed */
60 static token lasttok; /* last token, if pushed */
63 * scan expecting 1 given token
66 scan(tok_kind expect, token *tokp)
69 if (tokp->kind != expect) {
75 * scan expecting any of the 2 given tokens
78 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
81 if (tokp->kind != expect1 && tokp->kind != expect2) {
82 expected2(expect1, expect2);
87 * scan expecting any of the 3 given token
90 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
93 if (tokp->kind != expect1 && tokp->kind != expect2
94 && tokp->kind != expect3) {
95 expected3(expect1, expect2, expect3);
100 * scan expecting a constant, possibly symbolic
103 scan_num(token *tokp)
106 switch (tokp->kind) {
110 error("constant or identifier expected");
115 * Peek at the next token
125 * Peek at the next token and scan it if it matches what you expect
128 peekscan(tok_kind expect, token *tokp)
131 if (tokp->kind == expect) {
139 * Get the next token, printing out any directive that are encountered.
142 get_token(token *tokp)
155 if (!fgets(curline, MAXLINESIZE, fin)) {
156 tokp->kind = TOK_EOF;
163 } else if (cppline(curline)) {
164 docppline(curline, &linenum,
166 } else if (directive(curline)) {
167 printdirective(curline);
173 } else if (isspace(*where)) {
174 while (isspace(*where)) {
177 } else if (commenting) {
178 for (where++; *where; where++) {
179 if (endcomment(where)) {
185 } else if (startcomment(where)) {
194 * 'where' is not whitespace, comment or directive Must be a token!
198 tokp->kind = TOK_COLON;
202 tokp->kind = TOK_SEMICOLON;
206 tokp->kind = TOK_COMMA;
210 tokp->kind = TOK_EQUAL;
214 tokp->kind = TOK_STAR;
218 tokp->kind = TOK_LBRACKET;
222 tokp->kind = TOK_RBRACKET;
226 tokp->kind = TOK_LBRACE;
230 tokp->kind = TOK_RBRACE;
234 tokp->kind = TOK_LPAREN;
238 tokp->kind = TOK_RPAREN;
242 tokp->kind = TOK_LANGLE;
246 tokp->kind = TOK_RANGLE;
251 tokp->kind = TOK_STRCONST;
252 findstrconst(&where, &tokp->str);
255 tokp->kind = TOK_CHARCONST;
256 findchrconst(&where, &tokp->str);
270 tokp->kind = TOK_IDENT;
271 findconst(&where, &tokp->str);
275 if (!(isalpha(*where) || *where == '_')) {
279 s_print(buf, "illegal character in file: ");
280 p = buf + strlen(buf);
281 if (isprint(*where)) {
282 s_print(p, "%c", *where);
284 s_print(p, "%d", *where);
288 findkind(&where, tokp);
294 unget_token(token *tokp)
301 findstrconst(char **str, char **val)
309 } while (*p && *p != '"');
311 error("unterminated string constant");
315 *val = alloc(size + 1);
316 (void) strncpy(*val, *str, size);
322 findchrconst(char **str, char **val)
330 } while (*p && *p != '\'');
332 error("unterminated string constant");
337 error("empty char string");
339 *val = alloc(size + 1);
340 (void) strncpy(*val, *str, size);
346 findconst(char **str, char **val)
352 if (*p == '0' && *(p + 1) == 'x') {
356 } while (isxdigit(*p));
360 } while (isdigit(*p));
363 *val = alloc(size + 1);
364 (void) strncpy(*val, *str, size);
369 static token symbols[] = {
370 {TOK_CONST, "const"},
371 {TOK_UNION, "union"},
372 {TOK_SWITCH, "switch"},
374 {TOK_DEFAULT, "default"},
375 {TOK_STRUCT, "struct"},
376 {TOK_TYPEDEF, "typedef"},
378 {TOK_OPAQUE, "opaque"},
383 {TOK_UNSIGNED, "unsigned"},
384 {TOK_SHORT, "short"},
385 {TOK_INT32, "int32"},
386 {TOK_FLOAT, "float"},
387 {TOK_DOUBLE, "double"},
388 {TOK_STRING, "string"},
389 {TOK_PROGRAM, "program"},
390 {TOK_VERSION, "version"},
395 findkind(char **mark, token *tokp)
402 for (s = symbols; s->kind != TOK_EOF; s++) {
403 len = strlen(s->str);
404 if (strncmp(str, s->str, len) == 0) {
405 if (!isalnum(str[len]) && str[len] != '_') {
406 tokp->kind = s->kind;
413 tokp->kind = TOK_IDENT;
414 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
415 tokp->str = alloc(len + 1);
416 (void) strncpy(tokp->str, str, len);
424 return (line == curline && *line == '#');
428 directive(char *line)
430 return (line == curline && *line == '%');
434 printdirective(char *line)
436 f_print(fout, "%s", line + 1);
440 docppline(char *line, int *lineno, char **fname)
447 while (isspace(*line)) {
451 while (isdigit(*line)) {
454 while (isspace(*line)) {
458 error("preprocessor error");
461 p = file = alloc(strlen(line) + 1);
462 while (*line && *line != '"') {
466 error("preprocessor error");