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 $
32 #ident "@(#)rpc_scan.c 1.13 93/07/05 SMI"
35 static char sccsid[] = "@(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI";
39 * rpc_scan.c, Scanner for the RPC protocol compiler
40 * Copyright (C) 1987, Sun Microsystems, Inc.
43 #include <sys/types.h>
50 #include "rpc_parse.h"
53 #define startcomment(where) (where[0] == '/' && where[1] == '*')
54 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
56 static int pushed = 0; /* is a token pushed */
57 static token lasttok; /* last token, if pushed */
59 static void unget_token __P(( token * ));
60 static void findstrconst __P(( char **, char **));
61 static void findchrconst __P(( char **, char **));
62 static void findconst __P(( char **, char **));
63 static void findkind __P(( char **, token * ));
64 static int cppline __P(( char * ));
65 static int directive __P(( char * ));
66 static void printdirective __P(( char * ));
67 static void docppline __P(( char *, int *, char ** ));
70 * scan expecting 1 given token
78 if (tokp->kind != expect) {
84 * scan expecting any of the 2 given tokens
87 scan2(expect1, expect2, tokp)
93 if (tokp->kind != expect1 && tokp->kind != expect2) {
94 expected2(expect1, expect2);
99 * scan expecting any of the 3 given token
102 scan3(expect1, expect2, expect3, tokp)
109 if (tokp->kind != expect1 && tokp->kind != expect2
110 && tokp->kind != expect3) {
111 expected3(expect1, expect2, expect3);
116 * scan expecting a constant, possibly symbolic
123 switch (tokp->kind) {
127 error("constant or identifier expected");
132 * Peek at the next token
143 * Peek at the next token and scan it if it matches what you expect
146 peekscan(expect, tokp)
151 if (tokp->kind == expect) {
159 * Get the next token, printing out any directive that are encountered.
178 if (!fgets(curline, MAXLINESIZE, fin)) {
179 tokp->kind = TOK_EOF;
180 /* now check if cpp returned non NULL value */
181 waitpid(childpid, &stat, WUNTRACED);
183 /* Set return value from rpcgen */
184 nonfatalerrors = stat >> 8;
192 } else if (cppline(curline)) {
193 docppline(curline, &linenum,
195 } else if (directive(curline)) {
196 printdirective(curline);
202 } else if (isspace(*where)) {
203 while (isspace(*where)) {
206 } else if (commenting) {
207 for (where++; *where; where++) {
208 if (endcomment(where)) {
214 } else if (startcomment(where)) {
223 * 'where' is not whitespace, comment or directive Must be a token!
227 tokp->kind = TOK_COLON;
231 tokp->kind = TOK_SEMICOLON;
235 tokp->kind = TOK_COMMA;
239 tokp->kind = TOK_EQUAL;
243 tokp->kind = TOK_STAR;
247 tokp->kind = TOK_LBRACKET;
251 tokp->kind = TOK_RBRACKET;
255 tokp->kind = TOK_LBRACE;
259 tokp->kind = TOK_RBRACE;
263 tokp->kind = TOK_LPAREN;
267 tokp->kind = TOK_RPAREN;
271 tokp->kind = TOK_LANGLE;
275 tokp->kind = TOK_RANGLE;
280 tokp->kind = TOK_STRCONST;
281 findstrconst(&where, &tokp->str);
284 tokp->kind = TOK_CHARCONST;
285 findchrconst(&where, &tokp->str);
299 tokp->kind = TOK_IDENT;
300 findconst(&where, &tokp->str);
304 if (!(isalpha(*where) || *where == '_')) {
308 s_print(buf, "illegal character in file: ");
309 p = buf + strlen(buf);
310 if (isprint(*where)) {
311 s_print(p, "%c", *where);
313 s_print(p, "%d", *where);
317 findkind(&where, tokp);
331 findstrconst(str, val)
341 } while (*p && *p != '"');
343 error("unterminated string constant");
347 *val = alloc(size + 1);
348 (void) strncpy(*val, *str, size);
354 findchrconst(str, val)
364 } while (*p && *p != '\'');
366 error("unterminated string constant");
371 error("empty char string");
373 *val = alloc(size + 1);
374 (void) strncpy(*val, *str, size);
388 if (*p == '0' && *(p + 1) == 'x') {
392 } while (isxdigit(*p));
396 } while (isdigit(*p));
399 *val = alloc(size + 1);
400 (void) strncpy(*val, *str, size);
405 static token symbols[] = {
406 {TOK_CONST, "const"},
407 {TOK_UNION, "union"},
408 {TOK_SWITCH, "switch"},
410 {TOK_DEFAULT, "default"},
411 {TOK_STRUCT, "struct"},
412 {TOK_TYPEDEF, "typedef"},
414 {TOK_OPAQUE, "opaque"},
419 {TOK_UNSIGNED, "unsigned"},
420 {TOK_SHORT, "short"},
422 {TOK_HYPER, "hyper"},
423 {TOK_FLOAT, "float"},
424 {TOK_DOUBLE, "double"},
425 {TOK_QUAD, "quadruple"},
426 {TOK_STRING, "string"},
427 {TOK_PROGRAM, "program"},
428 {TOK_VERSION, "version"},
442 for (s = symbols; s->kind != TOK_EOF; s++) {
443 len = strlen(s->str);
444 if (strncmp(str, s->str, len) == 0) {
445 if (!isalnum(str[len]) && str[len] != '_') {
446 tokp->kind = s->kind;
453 tokp->kind = TOK_IDENT;
454 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
455 tokp->str = alloc(len + 1);
456 (void) strncpy(tokp->str, str, len);
465 return (line == curline && *line == '#');
472 return (line == curline && *line == '%');
479 f_print(fout, "%s", line + 1);
483 docppline(line, lineno, fname)
493 while (isspace(*line)) {
497 while (isdigit(*line)) {
500 while (isspace(*line)) {
504 error("preprocessor error");
507 p = file = alloc(strlen(line) + 1);
508 while (*line && *line != '"') {
512 error("preprocessor error");