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, MERCHANTABILITY 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 $
34 * rpc_scan.c, Scanner for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
38 #include <sys/types.h>
44 #include "rpc_parse.h"
48 #define startcomment(where) (where[0] == '/' && where[1] == '*')
49 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
51 static int pushed
= 0; /* is a token pushed */
52 static token lasttok
; /* last token, if pushed */
54 static void unget_token(token
*);
55 static void findstrconst(char **, const char **);
56 static void findchrconst(char **, const char **);
57 static void findconst(char **, const char **);
58 static void findkind(char **, token
*);
59 static int cppline(char *);
60 static int directive(char *);
61 static void printdirective(char *);
62 static void docppline(char *, int *, const char **);
65 * scan expecting 1 given token
68 scan(tok_kind expect
, token
*tokp
)
71 if (tokp
->kind
!= expect
)
76 * scan expecting any of the 2 given tokens
79 scan2(tok_kind expect1
, tok_kind expect2
, token
*tokp
)
82 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
)
83 expected2(expect1
, expect2
);
87 * scan expecting any of the 3 given token
90 scan3(tok_kind expect1
, tok_kind expect2
, tok_kind expect3
, token
*tokp
)
93 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
&&
94 tokp
->kind
!= expect3
)
95 expected3(expect1
, expect2
, expect3
);
99 * scan expecting a constant, possibly symbolic
102 scan_num(token
*tokp
)
105 switch (tokp
->kind
) {
109 error("constant or identifier expected");
114 * Peek at the next token
124 * Peek at the next token and scan it if it matches what you expect
127 peekscan(tok_kind expect
, token
*tokp
)
130 if (tokp
->kind
== expect
) {
138 * Get the next token, printing out any directive that are encountered.
141 get_token(token
*tokp
)
155 if (!fgets(curline
, MAXLINESIZE
, fin
)) {
156 tokp
->kind
= TOK_EOF
;
158 * now check if cpp returned
161 waitpid(childpid
, &stat
, WUNTRACED
);
164 * Set return value from rpcgen
166 nonfatalerrors
= stat
>> 8;
174 else if (cppline(curline
))
175 docppline(curline
, &linenum
,
177 else if (directive(curline
))
178 printdirective(curline
);
183 } else if (isspace(*where
)) {
184 while (isspace(*where
))
186 } else if (commenting
) {
187 for (where
++; *where
; where
++) {
188 if (endcomment(where
)) {
194 } else if (startcomment(where
)) {
203 * 'where' is not whitespace, comment or directive Must be a token!
207 tokp
->kind
= TOK_COLON
;
211 tokp
->kind
= TOK_SEMICOLON
;
215 tokp
->kind
= TOK_COMMA
;
219 tokp
->kind
= TOK_EQUAL
;
223 tokp
->kind
= TOK_STAR
;
227 tokp
->kind
= TOK_LBRACKET
;
231 tokp
->kind
= TOK_RBRACKET
;
235 tokp
->kind
= TOK_LBRACE
;
239 tokp
->kind
= TOK_RBRACE
;
243 tokp
->kind
= TOK_LPAREN
;
247 tokp
->kind
= TOK_RPAREN
;
251 tokp
->kind
= TOK_LANGLE
;
255 tokp
->kind
= TOK_RANGLE
;
260 tokp
->kind
= TOK_STRCONST
;
261 findstrconst(&where
, &tokp
->str
);
264 tokp
->kind
= TOK_CHARCONST
;
265 findchrconst(&where
, &tokp
->str
);
279 tokp
->kind
= TOK_IDENT
;
280 findconst(&where
, &tokp
->str
);
284 if (!(isalpha(*where
) || *where
== '_')) {
288 s_print(buf
, "illegal character in file: ");
289 p
= buf
+ strlen(buf
);
290 if (isprint(*where
)) {
291 s_print(p
, "%c", *where
);
293 s_print(p
, "%d", *where
);
297 findkind(&where
, tokp
);
303 unget_token(token
*tokp
)
310 findstrconst(char **str
, const char **val
)
319 } while (*p
&& *p
!= '"');
321 error("unterminated string constant");
324 tmp
= xmalloc(size
+ 1);
325 strncpy(tmp
, *str
, size
);
332 findchrconst(char **str
, const char **val
)
341 } while (*p
&& *p
!= '\'');
343 error("unterminated string constant");
347 error("empty char string");
348 tmp
= xmalloc(size
+ 1);
349 strncpy(tmp
, *str
, size
);
356 findconst(char **str
, const char **val
)
363 if (*p
== '0' && *(p
+ 1) == 'x') {
367 } while (isxdigit(*p
));
371 } while (isdigit(*p
));
374 tmp
= xmalloc(size
+ 1);
375 strncpy(tmp
, *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 tmp
= xmalloc(len
+ 1);
430 strncpy(tmp
, str
, len
);
439 return(line
== curline
&& *line
== '#');
443 directive(char *line
)
445 return(line
== curline
&& *line
== '%');
449 printdirective(char *line
)
451 f_print(fout
, "%s", line
+ 1);
455 docppline(char *line
, int *lineno
, const char **fname
)
462 while (isspace(*line
))
465 while (isdigit(*line
))
467 while (isspace(*line
))
470 error("preprocessor error");
472 p
= file
= xmalloc(strlen(line
) + 1);
473 while (*line
&& *line
!= '"')
476 error("preprocessor error");