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 * @(#)rpc_scan.c 1.13 93/07/05 SMI; 1.11 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_scan.c,v 1.10 2005/11/13 21:17:24 dwmalone Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_scan.c,v 1.5 2008/10/16 01:52:33 swildner Exp $
35 * rpc_scan.c, Scanner for the RPC protocol compiler
36 * Copyright (C) 1987, Sun Microsystems, Inc.
39 #include <sys/types.h>
45 #include "rpc_parse.h"
49 #define startcomment(where) (where[0] == '/' && where[1] == '*')
50 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
52 static int pushed = 0; /* is a token pushed */
53 static token lasttok; /* last token, if pushed */
55 static void unget_token(token *);
56 static void findstrconst(char **, const char **);
57 static void findchrconst(char **, const char **);
58 static void findconst(char **, const char **);
59 static void findkind(char **, token *);
60 static int cppline(char *);
61 static int directive(char *);
62 static void printdirective(char *);
63 static void docppline(char *, int *, const char **);
66 * scan expecting 1 given token
69 scan(tok_kind expect, token *tokp)
72 if (tokp->kind != expect)
77 * scan expecting any of the 2 given tokens
80 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
83 if (tokp->kind != expect1 && tokp->kind != expect2)
84 expected2(expect1, expect2);
88 * scan expecting any of the 3 given token
91 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
94 if (tokp->kind != expect1 && tokp->kind != expect2 &&
95 tokp->kind != expect3)
96 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)
156 if (!fgets(curline, MAXLINESIZE, fin)) {
157 tokp->kind = TOK_EOF;
159 * now check if cpp returned
162 waitpid(childpid, &stat, WUNTRACED);
165 * Set return value from rpcgen
167 nonfatalerrors = stat >> 8;
175 else if (cppline(curline))
176 docppline(curline, &linenum,
178 else if (directive(curline))
179 printdirective(curline);
184 } else if (isspace(*where)) {
185 while (isspace(*where))
187 } else if (commenting) {
188 for (where++; *where; where++) {
189 if (endcomment(where)) {
195 } else if (startcomment(where)) {
204 * 'where' is not whitespace, comment or directive Must be a token!
208 tokp->kind = TOK_COLON;
212 tokp->kind = TOK_SEMICOLON;
216 tokp->kind = TOK_COMMA;
220 tokp->kind = TOK_EQUAL;
224 tokp->kind = TOK_STAR;
228 tokp->kind = TOK_LBRACKET;
232 tokp->kind = TOK_RBRACKET;
236 tokp->kind = TOK_LBRACE;
240 tokp->kind = TOK_RBRACE;
244 tokp->kind = TOK_LPAREN;
248 tokp->kind = TOK_RPAREN;
252 tokp->kind = TOK_LANGLE;
256 tokp->kind = TOK_RANGLE;
261 tokp->kind = TOK_STRCONST;
262 findstrconst(&where, &tokp->str);
265 tokp->kind = TOK_CHARCONST;
266 findchrconst(&where, &tokp->str);
280 tokp->kind = TOK_IDENT;
281 findconst(&where, &tokp->str);
285 if (!(isalpha(*where) || *where == '_')) {
289 s_print(buf, "illegal character in file: ");
290 p = buf + strlen(buf);
291 if (isprint(*where)) {
292 s_print(p, "%c", *where);
294 s_print(p, "%d", *where);
298 findkind(&where, tokp);
304 unget_token(token *tokp)
311 findstrconst(char **str, const char **val)
320 } while (*p && *p != '"');
322 error("unterminated string constant");
325 tmp = xmalloc(size + 1);
326 strncpy(tmp, *str, size);
333 findchrconst(char **str, const char **val)
342 } while (*p && *p != '\'');
344 error("unterminated string constant");
348 error("empty char string");
349 tmp = xmalloc(size + 1);
350 strncpy(tmp, *str, size);
357 findconst(char **str, const char **val)
364 if (*p == '0' && *(p + 1) == 'x') {
368 } while (isxdigit(*p));
372 } while (isdigit(*p));
375 tmp = xmalloc(size + 1);
376 strncpy(tmp, *str, size);
382 static token symbols[] = {
383 {TOK_CONST, "const"},
384 {TOK_UNION, "union"},
385 {TOK_SWITCH, "switch"},
387 {TOK_DEFAULT, "default"},
388 {TOK_STRUCT, "struct"},
389 {TOK_TYPEDEF, "typedef"},
391 {TOK_OPAQUE, "opaque"},
396 {TOK_UNSIGNED, "unsigned"},
397 {TOK_SHORT, "short"},
399 {TOK_HYPER, "hyper"},
400 {TOK_FLOAT, "float"},
401 {TOK_DOUBLE, "double"},
402 {TOK_QUAD, "quadruple"},
403 {TOK_STRING, "string"},
404 {TOK_PROGRAM, "program"},
405 {TOK_VERSION, "version"},
410 findkind(char **mark, token *tokp)
417 for (s = symbols; s->kind != TOK_EOF; s++) {
418 len = strlen(s->str);
419 if (strncmp(str, s->str, len) == 0) {
420 if (!isalnum(str[len]) && str[len] != '_') {
421 tokp->kind = s->kind;
428 tokp->kind = TOK_IDENT;
429 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
430 tmp = xmalloc(len + 1);
431 strncpy(tmp, str, len);
440 return(line == curline && *line == '#');
444 directive(char *line)
446 return(line == curline && *line == '%');
450 printdirective(char *line)
452 f_print(fout, "%s", line + 1);
456 docppline(char *line, int *lineno, const char **fname)
463 while (isspace(*line))
466 while (isdigit(*line))
468 while (isspace(*line))
471 error("preprocessor error");
473 p = file = xmalloc(strlen(line) + 1);
474 while (*line && *line != '"')
477 error("preprocessor error");