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
29 * $FreeBSD: src/usr.bin/rpcgen/rpc_scan.c,v 1.4.8.1 2001/03/04 08:59:50 kris Exp $
30 * $DragonFly: src/usr.bin/rpcgen/rpc_scan.c,v 1.4 2004/06/19 16:40:36 joerg Exp $
32 * @(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI
35 #ident "@(#)rpc_scan.c 1.13 93/07/05 SMI"
38 * rpc_scan.c, Scanner for the RPC protocol compiler
39 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include <sys/types.h>
49 #include "rpc_parse.h"
52 #define startcomment(where) (where[0] == '/' && where[1] == '*')
53 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
55 static int pushed = 0; /* is a token pushed */
56 static token lasttok; /* last token, if pushed */
58 static void unget_token(token *);
59 static void findstrconst(char **, char **);
60 static void findchrconst(char **, char **);
61 static void findconst(char **, char **);
62 static void findkind(char **, token *);
63 static int cppline(char *);
64 static int directive(char *);
65 static void printdirective(char *);
66 static void docppline(char *, int *, char **);
69 * scan expecting 1 given token
72 scan(tok_kind expect, token *tokp)
75 if (tokp->kind != expect)
80 * scan expecting any of the 2 given tokens
83 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
86 if (tokp->kind != expect1 && tokp->kind != expect2)
87 expected2(expect1, expect2);
91 * scan expecting any of the 3 given token
94 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
97 if (tokp->kind != expect1 && tokp->kind != expect2 &&
98 tokp->kind != expect3)
99 expected3(expect1, expect2, expect3);
103 * scan expecting a constant, possibly symbolic
106 scan_num(token *tokp)
109 switch (tokp->kind) {
113 error("constant or identifier expected");
118 * Peek at the next token
128 * Peek at the next token and scan it if it matches what you expect
131 peekscan(tok_kind expect, token *tokp)
134 if (tokp->kind == expect) {
142 * Get the next token, printing out any directive that are encountered.
145 get_token(token *tokp)
159 if (!fgets(curline, MAXLINESIZE, fin)) {
160 tokp->kind = TOK_EOF;
162 * now check if cpp returned
165 waitpid(childpid, &stat, WUNTRACED);
168 * 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);
187 } else if (isspace(*where)) {
188 while (isspace(*where))
190 } else if (commenting) {
191 for (where++; *where; where++) {
192 if (endcomment(where)) {
198 } else if (startcomment(where)) {
207 * 'where' is not whitespace, comment or directive Must be a token!
211 tokp->kind = TOK_COLON;
215 tokp->kind = TOK_SEMICOLON;
219 tokp->kind = TOK_COMMA;
223 tokp->kind = TOK_EQUAL;
227 tokp->kind = TOK_STAR;
231 tokp->kind = TOK_LBRACKET;
235 tokp->kind = TOK_RBRACKET;
239 tokp->kind = TOK_LBRACE;
243 tokp->kind = TOK_RBRACE;
247 tokp->kind = TOK_LPAREN;
251 tokp->kind = TOK_RPAREN;
255 tokp->kind = TOK_LANGLE;
259 tokp->kind = TOK_RANGLE;
264 tokp->kind = TOK_STRCONST;
265 findstrconst(&where, &tokp->str);
268 tokp->kind = TOK_CHARCONST;
269 findchrconst(&where, &tokp->str);
283 tokp->kind = TOK_IDENT;
284 findconst(&where, &tokp->str);
288 if (!(isalpha(*where) || *where == '_')) {
292 s_print(buf, "illegal character in file: ");
293 p = buf + strlen(buf);
294 if (isprint(*where)) {
295 s_print(p, "%c", *where);
297 s_print(p, "%d", *where);
301 findkind(&where, tokp);
307 unget_token(token *tokp)
314 findstrconst(char **str, char **val)
322 } while (*p && *p != '"');
324 error("unterminated string constant");
327 *val = alloc(size + 1);
328 (void) strncpy(*val, *str, size);
334 findchrconst(char **str, char **val)
342 } while (*p && *p != '\'');
344 error("unterminated string constant");
348 error("empty char string");
349 *val = alloc(size + 1);
350 strncpy(*val, *str, size);
364 if (*p == '0' && *(p + 1) == 'x') {
368 } while (isxdigit(*p));
372 } while (isdigit(*p));
375 *val = alloc(size + 1);
376 strncpy(*val, *str, size);
381 static token symbols[] = {
382 {TOK_CONST, "const"},
383 {TOK_UNION, "union"},
384 {TOK_SWITCH, "switch"},
386 {TOK_DEFAULT, "default"},
387 {TOK_STRUCT, "struct"},
388 {TOK_TYPEDEF, "typedef"},
390 {TOK_OPAQUE, "opaque"},
395 {TOK_UNSIGNED, "unsigned"},
396 {TOK_SHORT, "short"},
398 {TOK_HYPER, "hyper"},
399 {TOK_FLOAT, "float"},
400 {TOK_DOUBLE, "double"},
401 {TOK_QUAD, "quadruple"},
402 {TOK_STRING, "string"},
403 {TOK_PROGRAM, "program"},
404 {TOK_VERSION, "version"},
409 findkind(char **mark, token *tokp)
416 for (s = symbols; s->kind != TOK_EOF; s++) {
417 len = strlen(s->str);
418 if (strncmp(str, s->str, len) == 0) {
419 if (!isalnum(str[len]) && str[len] != '_') {
420 tokp->kind = s->kind;
427 tokp->kind = TOK_IDENT;
428 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
429 tokp->str = alloc(len + 1);
430 strncpy(tokp->str, str, len);
438 return(line == curline && *line == '#');
442 directive(char *line)
444 return(line == curline && *line == '%');
448 printdirective(char *line)
450 f_print(fout, "%s", line + 1);
454 docppline(char *line, int *lineno, char **fname)
461 while (isspace(*line))
464 while (isdigit(*line))
466 while (isspace(*line))
469 error("preprocessor error");
471 p = file = alloc(strlen(line) + 1);
472 while (*line && *line != '"')
475 error("preprocessor error");