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 * From: @(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI
38 * rpc_scan.c, Scanner for the RPC protocol compiler
39 * Copyright (C) 1987, Sun Microsystems, Inc.
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
*tokp
);
56 static void findstrconst(const char **str
, const char **val
);
57 static void findchrconst(const char **str
, const char **val
);
58 static void findconst(const char **str
, const char **val
);
59 static void findkind(const char **mark
, token
*tokp
);
60 static int cppline(const char *line
);
61 static int directive(const char *line
);
62 static void printdirective(const char *line
);
63 static void docppline(const char *line
, int *lineno
, const char **fname
);
66 * scan expecting 1 given token
69 scan(tok_kind expect
, token
*tokp
)
72 if (tokp
->kind
!= expect
) {
78 * scan expecting any of the 2 given tokens
81 scan2(tok_kind expect1
, tok_kind expect2
, token
*tokp
)
84 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
) {
85 expected2(expect1
, expect2
);
90 * scan expecting any of the 3 given token
93 scan3(tok_kind expect1
, tok_kind expect2
, tok_kind expect3
, token
*tokp
)
96 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
97 && tokp
->kind
!= expect3
) {
98 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
)
158 if (!fgets(curline
, MAXLINESIZE
, fin
)) {
159 tokp
->kind
= TOK_EOF
;
167 } else if (cppline(curline
)) {
168 docppline(curline
, &linenum
,
170 } else if (directive(curline
)) {
171 printdirective(curline
);
177 } else if (isspace(*where
)) {
178 while (isspace(*where
)) {
181 } else if (commenting
) {
182 for (where
++; *where
; where
++) {
183 if (endcomment(where
)) {
189 } else if (startcomment(where
)) {
198 * 'where' is not whitespace, comment or directive Must be a token!
202 tokp
->kind
= TOK_COLON
;
206 tokp
->kind
= TOK_SEMICOLON
;
210 tokp
->kind
= TOK_COMMA
;
214 tokp
->kind
= TOK_EQUAL
;
218 tokp
->kind
= TOK_STAR
;
222 tokp
->kind
= TOK_LBRACKET
;
226 tokp
->kind
= TOK_RBRACKET
;
230 tokp
->kind
= TOK_LBRACE
;
234 tokp
->kind
= TOK_RBRACE
;
238 tokp
->kind
= TOK_LPAREN
;
242 tokp
->kind
= TOK_RPAREN
;
246 tokp
->kind
= TOK_LANGLE
;
250 tokp
->kind
= TOK_RANGLE
;
255 tokp
->kind
= TOK_STRCONST
;
256 findstrconst(&where
, &tokp
->str
);
259 tokp
->kind
= TOK_CHARCONST
;
260 findchrconst(&where
, &tokp
->str
);
274 tokp
->kind
= TOK_IDENT
;
275 findconst(&where
, &tokp
->str
);
279 if (!(isalpha(*where
) || *where
== '_')) {
283 s_print(buf
, "illegal character in file: ");
284 p
= buf
+ strlen(buf
);
285 if (isprint(*where
)) {
286 s_print(p
, "%c", *where
);
288 s_print(p
, "%d", *where
);
292 findkind(&where
, tokp
);
298 unget_token(token
*tokp
)
305 findstrconst(const char **str
, const char **val
)
314 } while (*p
&& *p
!= '"');
316 error("unterminated string constant");
320 tmp
= alloc(size
+ 1);
321 strncpy(tmp
, *str
, size
);
328 findchrconst(const char **str
, const char **val
)
337 } while (*p
&& *p
!= '\'');
339 error("unterminated string constant");
344 error("empty char string");
346 tmp
= alloc(size
+ 1);
347 strncpy(tmp
, *str
, size
);
354 findconst(const char **str
, const char **val
)
361 if (*p
== '0' && *(p
+ 1) == 'x') {
365 } while (isxdigit(*p
));
369 } while (isdigit(*p
));
372 tmp
= alloc(size
+ 1);
373 strncpy(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_FLOAT
, "float"},
397 {TOK_DOUBLE
, "double"},
398 {TOK_STRING
, "string"},
399 {TOK_PROGRAM
, "program"},
400 {TOK_VERSION
, "version"},
405 findkind(const char **mark
, token
*tokp
)
413 for (s
= symbols
; s
->kind
!= TOK_EOF
; s
++) {
414 len
= strlen(s
->str
);
415 if (strncmp(str
, s
->str
, len
) == 0) {
416 if (!isalnum(str
[len
]) && str
[len
] != '_') {
417 tokp
->kind
= s
->kind
;
424 tokp
->kind
= TOK_IDENT
;
425 for (len
= 0; isalnum(str
[len
]) || str
[len
] == '_'; len
++);
426 tmp
= alloc(len
+ 1);
427 strncpy(tmp
, str
, len
);
434 cppline(const char *line
)
436 return (line
== curline
&& *line
== '#');
440 directive(const char *line
)
442 return (line
== curline
&& *line
== '%');
446 printdirective(const char *line
)
448 f_print(fout
, "%s", line
+ 1);
452 docppline(const 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
= alloc(strlen(line
) + 1);
474 while (*line
&& *line
!= '"') {
478 error("preprocessor error");