1 /* $NetBSD: rpc_scan.c,v 1.10 2006/03/20 17:01:26 elad Exp $ */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user or with the express written consent of
9 * Sun Microsystems, Inc.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI";
41 __RCSID("$NetBSD: rpc_scan.c,v 1.10 2006/03/20 17:01:26 elad Exp $");
46 * rpc_scan.c, Scanner for the RPC protocol compiler
47 * Copyright (C) 1987, Sun Microsystems, Inc.
54 #include "rpc_parse.h"
57 #define startcomment(where) (where[0] == '/' && where[1] == '*')
58 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
60 static void unget_token
__P((token
*));
61 static void findstrconst
__P((char **, char **));
62 static void findchrconst
__P((char **, char **));
63 static void findconst
__P((char **, char **));
64 static void findkind
__P((char **, token
*));
65 static int cppline
__P((char *));
66 static int directive
__P((char *));
67 static void printdirective
__P((char *));
68 static void docppline
__P((char *, int *, char **));
70 static int pushed
= 0; /* is a token pushed */
71 static token lasttok
; /* last token, if pushed */
74 * scan expecting 1 given token
82 if (tokp
->kind
!= expect
) {
87 * scan expecting any of the 2 given tokens
90 scan2(expect1
, expect2
, tokp
)
96 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
) {
97 expected2(expect1
, expect2
);
101 * scan expecting any of the 3 given token
104 scan3(expect1
, expect2
, expect3
, tokp
)
111 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
112 && tokp
->kind
!= expect3
) {
113 expected3(expect1
, expect2
, expect3
);
117 * scan expecting a constant, possibly symbolic
124 switch (tokp
->kind
) {
128 error("constant or identifier expected");
132 * Peek at the next token
142 * Peek at the next token and scan it if it matches what you expect
145 peekscan(expect
, tokp
)
150 if (tokp
->kind
== expect
) {
157 * Get the next token, printing out any directive that are encountered.
174 if (!fgets(curline
, MAXLINESIZE
, fin
)) {
175 tokp
->kind
= TOK_EOF
;
183 if (cppline(curline
)) {
184 docppline(curline
, &linenum
,
187 if (directive(curline
)) {
188 printdirective(curline
);
195 if (isspace((unsigned char)*where
)) {
196 while (isspace((unsigned char)*where
)) {
201 for (where
++; *where
; where
++) {
202 if (endcomment(where
)) {
209 if (startcomment(where
)) {
218 * 'where' is not whitespace, comment or directive Must be a token!
222 tokp
->kind
= TOK_COLON
;
226 tokp
->kind
= TOK_SEMICOLON
;
230 tokp
->kind
= TOK_COMMA
;
234 tokp
->kind
= TOK_EQUAL
;
238 tokp
->kind
= TOK_STAR
;
242 tokp
->kind
= TOK_LBRACKET
;
246 tokp
->kind
= TOK_RBRACKET
;
250 tokp
->kind
= TOK_LBRACE
;
254 tokp
->kind
= TOK_RBRACE
;
258 tokp
->kind
= TOK_LPAREN
;
262 tokp
->kind
= TOK_RPAREN
;
266 tokp
->kind
= TOK_LANGLE
;
270 tokp
->kind
= TOK_RANGLE
;
275 tokp
->kind
= TOK_STRCONST
;
276 findstrconst(&where
, &tokp
->str
);
279 tokp
->kind
= TOK_CHARCONST
;
280 findchrconst(&where
, &tokp
->str
);
294 tokp
->kind
= TOK_IDENT
;
295 findconst(&where
, &tokp
->str
);
299 if (!(isalpha((unsigned char)*where
) || *where
== '_')) {
303 s_print(buf
, "illegal character in file: ");
304 p
= buf
+ strlen(buf
);
305 if (isprint((unsigned char)*where
)) {
306 s_print(p
, "%c", *where
);
308 s_print(p
, "%d", *where
);
312 findkind(&where
, tokp
);
326 findstrconst(str
, val
)
336 } while (*p
&& *p
!= '"');
338 error("unterminated string constant");
342 *val
= alloc(size
+ 1);
343 (void) strncpy(*val
, *str
, size
);
349 findchrconst(str
, val
)
359 } while (*p
&& *p
!= '\'');
361 error("unterminated string constant");
366 error("empty char string");
368 *val
= alloc(size
+ 1);
369 (void) strncpy(*val
, *str
, size
);
383 if (*p
== '0' && *(p
+ 1) == 'x') {
387 } while (isxdigit((unsigned char)*p
));
391 } while (isdigit((unsigned char)*p
));
394 *val
= alloc(size
+ 1);
395 (void) strncpy(*val
, *str
, size
);
400 static token symbols
[] = {
401 {TOK_CONST
, "const"},
402 {TOK_UNION
, "union"},
403 {TOK_SWITCH
, "switch"},
405 {TOK_DEFAULT
, "default"},
406 {TOK_STRUCT
, "struct"},
407 {TOK_TYPEDEF
, "typedef"},
409 {TOK_OPAQUE
, "opaque"},
414 {TOK_UNSIGNED
, "unsigned"},
415 {TOK_SHORT
, "short"},
417 {TOK_HYPER
, "hyper"},
418 {TOK_FLOAT
, "float"},
419 {TOK_DOUBLE
, "double"},
420 {TOK_QUAD
, "quadruple"},
421 {TOK_STRING
, "string"},
422 {TOK_PROGRAM
, "program"},
423 {TOK_VERSION
, "version"},
437 for (s
= symbols
; s
->kind
!= TOK_EOF
; s
++) {
438 len
= strlen(s
->str
);
439 if (strncmp(str
, s
->str
, len
) == 0) {
440 if (!isalnum((unsigned char)str
[len
]) &&
442 tokp
->kind
= s
->kind
;
449 tokp
->kind
= TOK_IDENT
;
450 for (len
= 0; isalnum((unsigned char)str
[len
]) ||
451 str
[len
] == '_'; len
++);
452 tokp
->str
= alloc(len
+ 1);
453 (void) strncpy(tokp
->str
, str
, len
);
462 return (line
== curline
&& *line
== '#');
469 return (line
== curline
&& *line
== '%');
476 f_print(fout
, "%s", line
+ 1);
480 docppline(line
, lineno
, fname
)
490 while (isspace((unsigned char)*line
)) {
494 while (isdigit((unsigned char)*line
)) {
497 while (isspace((unsigned char)*line
)) {
501 error("preprocessor error");
504 p
= file
= alloc(strlen(line
) + 1);
505 while (*line
&& *line
!= '"') {
509 error("preprocessor error");