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
36 * rpc_scan.c, Scanner for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsystems, Inc.
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
* tokp
);
55 static void findstrconst (const char **str
, const char **val
);
56 static void findchrconst (const char **str
, const char **val
);
57 static void findconst (const char **str
, const char **val
);
58 static void findkind (const char **mark
, token
* tokp
);
59 static int cppline (const char *line
);
60 static int directive (const char *line
);
61 static void printdirective (const char *line
);
62 static void docppline (const char *line
, int *lineno
, const char **fname
);
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
)
84 expected2 (expect1
, expect2
);
89 * scan expecting any of the 3 given token
92 scan3 (tok_kind expect1
, tok_kind expect2
, tok_kind expect3
, token
* tokp
)
95 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
96 && tokp
->kind
!= expect3
)
98 expected3 (expect1
, expect2
, expect3
);
103 * scan expecting a constant, possibly symbolic
106 scan_num (token
*tokp
)
114 error (_("constant or identifier expected"));
119 * Peek at the next token
129 * Peek at the next token and scan it if it matches what you expect
132 peekscan (tok_kind expect
, token
*tokp
)
135 if (tokp
->kind
== expect
)
144 * Get the next token, printing out any directive that are encountered.
147 get_token (token
*tokp
)
164 if (!fgets (curline
, MAXLINESIZE
, fin
))
166 tokp
->kind
= TOK_EOF
;
176 else if (cppline (curline
))
178 docppline (curline
, &linenum
,
181 else if (directive (curline
))
183 printdirective (curline
);
192 else if (isspace (*where
))
194 while (isspace (*where
))
201 for (where
++; *where
; where
++)
203 if (endcomment (where
))
211 else if (startcomment (where
))
223 * 'where' is not whitespace, comment or directive Must be a token!
228 tokp
->kind
= TOK_COLON
;
232 tokp
->kind
= TOK_SEMICOLON
;
236 tokp
->kind
= TOK_COMMA
;
240 tokp
->kind
= TOK_EQUAL
;
244 tokp
->kind
= TOK_STAR
;
248 tokp
->kind
= TOK_LBRACKET
;
252 tokp
->kind
= TOK_RBRACKET
;
256 tokp
->kind
= TOK_LBRACE
;
260 tokp
->kind
= TOK_RBRACE
;
264 tokp
->kind
= TOK_LPAREN
;
268 tokp
->kind
= TOK_RPAREN
;
272 tokp
->kind
= TOK_LANGLE
;
276 tokp
->kind
= TOK_RANGLE
;
281 tokp
->kind
= TOK_STRCONST
;
282 findstrconst (&where
, &tokp
->str
);
285 tokp
->kind
= TOK_CHARCONST
;
286 findchrconst (&where
, &tokp
->str
);
300 tokp
->kind
= TOK_IDENT
;
301 findconst (&where
, &tokp
->str
);
305 if (!(isalpha (*where
) || *where
== '_'))
310 s_print (buf
, _("illegal character in file: "));
311 p
= buf
+ strlen (buf
);
312 if (isprint (*where
))
314 s_print (p
, "%c", *where
);
318 s_print (p
, "%d", *where
);
322 findkind (&where
, tokp
);
328 unget_token (token
* tokp
)
335 findstrconst (const char **str
, const char **val
)
346 while (*p
&& *p
!= '"');
349 error (_("unterminated string constant"));
353 tmp
= alloc (size
+ 1);
354 strncpy (tmp
, *str
, size
);
361 findchrconst (const char **str
, const char **val
)
372 while (*p
&& *p
!= '\'');
375 error (_("unterminated string constant"));
381 error (_("empty char string"));
383 tmp
= alloc (size
+ 1);
384 strncpy (tmp
, *str
, size
);
391 findconst (const char **str
, const char **val
)
398 if (*p
== '0' && *(p
+ 1) == 'x')
405 while (isxdigit (*p
));
413 while (isdigit (*p
));
416 tmp
= alloc (size
+ 1);
417 strncpy (tmp
, *str
, size
);
423 static const token symbols
[] =
425 {TOK_CONST
, "const"},
426 {TOK_UNION
, "union"},
427 {TOK_SWITCH
, "switch"},
429 {TOK_DEFAULT
, "default"},
430 {TOK_STRUCT
, "struct"},
431 {TOK_TYPEDEF
, "typedef"},
433 {TOK_OPAQUE
, "opaque"},
438 {TOK_UNSIGNED
, "unsigned"},
439 {TOK_SHORT
, "short"},
441 {TOK_HYPER
, "hyper"},
442 {TOK_FLOAT
, "float"},
443 {TOK_DOUBLE
, "double"},
444 {TOK_STRING
, "string"},
445 {TOK_PROGRAM
, "program"},
446 {TOK_VERSION
, "version"},
451 findkind (const char **mark
, token
*tokp
)
459 for (s
= symbols
; s
->kind
!= TOK_EOF
; s
++)
461 len
= strlen (s
->str
);
462 if (strncmp (str
, s
->str
, len
) == 0)
464 if (!isalnum (str
[len
]) && str
[len
] != '_')
466 tokp
->kind
= s
->kind
;
473 tokp
->kind
= TOK_IDENT
;
474 for (len
= 0; isalnum (str
[len
]) || str
[len
] == '_'; len
++);
475 tmp
= alloc (len
+ 1);
476 strncpy (tmp
, str
, len
);
483 cppline (const char *line
)
485 return line
== curline
&& *line
== '#';
489 directive (const char *line
)
491 return line
== curline
&& *line
== '%';
495 printdirective (const char *line
)
497 f_print (fout
, "%s", line
+ 1);
501 docppline (const char *line
, int *lineno
, const char **fname
)
508 while (isspace (*line
))
513 while (isdigit (*line
))
517 while (isspace (*line
))
523 error (_("preprocessor error"));
526 p
= file
= alloc (strlen (line
) + 1);
527 while (*line
&& *line
!= '"')
533 error (_("preprocessor error"));