]>
git.cameronkatri.com Git - apple_cmds.git/blob - developer_cmds/rpcgen/rpc_scan.c
1 /* $NetBSD: rpc_scan.c,v 1.6 1997/10/18 10:54:05 lukem 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 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI";
37 __RCSID("$NetBSD: rpc_scan.c,v 1.6 1997/10/18 10:54:05 lukem Exp $");
42 * rpc_scan.c, Scanner for the RPC protocol compiler
43 * Copyright (C) 1987, Sun Microsystems, Inc.
50 #include "rpc_parse.h"
53 #define startcomment(where) (where[0] == '/' && where[1] == '*')
54 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
56 static void unget_token
__P((token
*));
57 static void findstrconst
__P((char **, char **));
58 static void findchrconst
__P((char **, char **));
59 static void findconst
__P((char **, char **));
60 static void findkind
__P((char **, token
*));
61 static int cppline
__P((char *));
62 static int directive
__P((char *));
63 static void printdirective
__P((char *));
64 static void docppline
__P((char *, int *, char **));
66 static int pushed
= 0; /* is a token pushed */
67 static token lasttok
; /* last token, if pushed */
70 * scan expecting 1 given token
78 if (tokp
->kind
!= expect
) {
83 * scan expecting any of the 2 given tokens
86 scan2(expect1
, expect2
, tokp
)
92 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
) {
93 expected2(expect1
, expect2
);
97 * scan expecting any of the 3 given token
100 scan3(expect1
, expect2
, expect3
, tokp
)
107 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
108 && tokp
->kind
!= expect3
) {
109 expected3(expect1
, expect2
, expect3
);
113 * scan expecting a constant, possibly symbolic
120 switch (tokp
->kind
) {
124 error("constant or identifier expected");
128 * Peek at the next token
138 * Peek at the next token and scan it if it matches what you expect
141 peekscan(expect
, tokp
)
146 if (tokp
->kind
== expect
) {
153 * Get the next token, printing out any directive that are encountered.
171 if (!fgets(curline
, MAXLINESIZE
, fin
)) {
172 tokp
->kind
= TOK_EOF
;
173 /* now check if cpp returned non NULL value */
174 waitpid(childpid
, &stat
, WUNTRACED
);
176 /* Set return value from rpcgen */
177 nonfatalerrors
= stat
>> 8;
186 /* skip lines beginning with #pragma */
187 if (!strncmp(curline
, "#pragma", 7)) *curline
= 0;
188 else if (cppline(curline
)) {
189 docppline(curline
, &linenum
,
192 if (directive(curline
)) {
193 printdirective(curline
);
200 if (isspace(*where
)) {
201 while (isspace(*where
)) {
206 for (where
++; *where
; where
++) {
207 if (endcomment(where
)) {
214 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");