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
31 * rpc_scan.c, Scanner for the RPC protocol compiler
32 * Copyright (C) 1987, Sun Microsystems, Inc.
35 #include <sys/types.h>
41 #include "rpc_parse.h"
45 #define startcomment(where) (where[0] == '/' && where[1] == '*')
46 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
48 static int pushed = 0; /* is a token pushed */
49 static token lasttok; /* last token, if pushed */
51 static void unget_token( token * );
52 static void findstrconst(char **, const char **);
53 static void findchrconst(char **, const char **);
54 static void findconst(char **, const char **);
55 static void findkind( char **, token * );
56 static int cppline( char * );
57 static int directive( char * );
58 static void printdirective( char * );
59 static void docppline(char *, int *, const char **);
62 * scan expecting 1 given token
65 scan(tok_kind expect, token *tokp)
68 if (tokp->kind != expect) {
74 * scan expecting any of the 2 given tokens
77 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
80 if (tokp->kind != expect1 && tokp->kind != expect2) {
81 expected2(expect1, expect2);
86 * scan expecting any of the 3 given token
89 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
92 if (tokp->kind != expect1 && tokp->kind != expect2
93 && tokp->kind != expect3) {
94 expected3(expect1, expect2, expect3);
99 * scan expecting a constant, possibly symbolic
102 scan_num(token *tokp)
105 switch (tokp->kind) {
109 error("constant or identifier expected");
114 * Peek at the next token
124 * Peek at the next token and scan it if it matches what you expect
127 peekscan(tok_kind expect, token *tokp)
130 if (tokp->kind == expect) {
138 * Get the next token, printing out any directive that are encountered.
141 get_token(token *tokp)
156 if (!fgets(curline, MAXLINESIZE, fin)) {
157 tokp->kind = TOK_EOF;
158 /* now check if cpp returned non NULL value */
159 waitpid(childpid, &stat, WUNTRACED);
161 /* Set return value from rpcgen */
162 nonfatalerrors = stat >> 8;
170 } else if (cppline(curline)) {
171 docppline(curline, &linenum,
173 } else if (directive(curline)) {
174 printdirective(curline);
180 } else if (isspace(*where)) {
181 while (isspace(*where)) {
184 } else if (commenting) {
185 for (where++; *where; where++) {
186 if (endcomment(where)) {
192 } else if (startcomment(where)) {
201 * 'where' is not whitespace, comment or directive Must be a token!
205 tokp->kind = TOK_COLON;
209 tokp->kind = TOK_SEMICOLON;
213 tokp->kind = TOK_COMMA;
217 tokp->kind = TOK_EQUAL;
221 tokp->kind = TOK_STAR;
225 tokp->kind = TOK_LBRACKET;
229 tokp->kind = TOK_RBRACKET;
233 tokp->kind = TOK_LBRACE;
237 tokp->kind = TOK_RBRACE;
241 tokp->kind = TOK_LPAREN;
245 tokp->kind = TOK_RPAREN;
249 tokp->kind = TOK_LANGLE;
253 tokp->kind = TOK_RANGLE;
258 tokp->kind = TOK_STRCONST;
259 findstrconst(&where, &tokp->str);
262 tokp->kind = TOK_CHARCONST;
263 findchrconst(&where, &tokp->str);
277 tokp->kind = TOK_IDENT;
278 findconst(&where, &tokp->str);
282 if (!(isalpha(*where) || *where == '_')) {
286 s_print(buf, "illegal character in file: ");
287 p = buf + strlen(buf);
288 if (isprint(*where)) {
289 s_print(p, "%c", *where);
291 s_print(p, "%d", *where);
295 findkind(&where, tokp);
301 unget_token(token *tokp)
308 findstrconst(char **str, const char **val)
317 } while (*p && *p != '"');
319 error("unterminated string constant");
324 (void) strlcpy(tmp, *str, size);
330 findchrconst(char **str, const char **val)
339 } while (*p && *p != '\'');
341 error("unterminated string constant");
346 error("empty char string");
349 (void) strlcpy(tmp, *str, size);
355 findconst(char **str, const char **val)
362 if (*p == '0' && *(p + 1) == 'x') {
366 } while (isxdigit(*p));
370 } while (isdigit(*p));
374 (void) strlcpy(tmp, *str, size);
379 static token symbols[] = {
380 {TOK_CONST, "const"},
381 {TOK_UNION, "union"},
382 {TOK_SWITCH, "switch"},
384 {TOK_DEFAULT, "default"},
385 {TOK_STRUCT, "struct"},
386 {TOK_TYPEDEF, "typedef"},
388 {TOK_OPAQUE, "opaque"},
393 {TOK_UNSIGNED, "unsigned"},
394 {TOK_SHORT, "short"},
396 {TOK_HYPER, "hyper"},
397 {TOK_FLOAT, "float"},
398 {TOK_DOUBLE, "double"},
399 {TOK_QUAD, "quadruple"},
400 {TOK_STRING, "string"},
401 {TOK_PROGRAM, "program"},
402 {TOK_VERSION, "version"},
407 findkind(char **mark, token *tokp)
414 for (s = symbols; s->kind != TOK_EOF; s++) {
415 len = strlen(s->str);
416 if (strncmp(str, s->str, len) == 0) {
417 if (!isalnum(str[len]) && str[len] != '_') {
418 tokp->kind = s->kind;
425 tokp->kind = TOK_IDENT;
426 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
427 tmp = xmalloc(len + 1);
428 (void) strlcpy(tmp, str, len + 1);
436 return (line == curline && *line == '#');
440 directive(char *line)
442 return (line == curline && *line == '%');
446 printdirective(char *line)
448 f_print(fout, "%s", line + 1);
452 docppline(char *line, int *lineno, const char **fname)
459 while (isspace(*line)) {
463 while (isdigit(*line)) {
466 while (isspace(*line)) {
470 error("preprocessor error");
473 p = file = xmalloc(strlen(line) + 1);
474 while (*line && *line != '"') {
478 error("preprocessor error");