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
)
77 * scan expecting any of the 2 given tokens
80 scan2 (tok_kind expect1
, tok_kind expect2
, token
* tokp
)
83 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
)
99 expected3 (expect1
, expect2
, expect3
);
104 * scan expecting a constant, possibly symbolic
107 scan_num (token
*tokp
)
115 error (_("constant or identifier expected"));
120 * Peek at the next token
130 * Peek at the next token and scan it if it matches what you expect
133 peekscan (tok_kind expect
, token
*tokp
)
136 if (tokp
->kind
== expect
)
145 * Get the next token, printing out any directive that are encountered.
148 get_token (token
*tokp
)
165 if (!fgets (curline
, MAXLINESIZE
, fin
))
167 tokp
->kind
= TOK_EOF
;
177 else if (cppline (curline
))
179 docppline (curline
, &linenum
,
182 else if (directive (curline
))
184 printdirective (curline
);
193 else if (isspace (*where
))
195 while (isspace (*where
))
202 for (where
++; *where
; where
++)
204 if (endcomment (where
))
212 else if (startcomment (where
))
224 * 'where' is not whitespace, comment or directive Must be a token!
229 tokp
->kind
= TOK_COLON
;
233 tokp
->kind
= TOK_SEMICOLON
;
237 tokp
->kind
= TOK_COMMA
;
241 tokp
->kind
= TOK_EQUAL
;
245 tokp
->kind
= TOK_STAR
;
249 tokp
->kind
= TOK_LBRACKET
;
253 tokp
->kind
= TOK_RBRACKET
;
257 tokp
->kind
= TOK_LBRACE
;
261 tokp
->kind
= TOK_RBRACE
;
265 tokp
->kind
= TOK_LPAREN
;
269 tokp
->kind
= TOK_RPAREN
;
273 tokp
->kind
= TOK_LANGLE
;
277 tokp
->kind
= TOK_RANGLE
;
282 tokp
->kind
= TOK_STRCONST
;
283 findstrconst (&where
, &tokp
->str
);
286 tokp
->kind
= TOK_CHARCONST
;
287 findchrconst (&where
, &tokp
->str
);
301 tokp
->kind
= TOK_IDENT
;
302 findconst (&where
, &tokp
->str
);
306 if (!(isalpha (*where
) || *where
== '_'))
311 s_print (buf
, _("illegal character in file: "));
312 p
= buf
+ strlen (buf
);
313 if (isprint (*where
))
315 s_print (p
, "%c", *where
);
319 s_print (p
, "%d", *where
);
323 findkind (&where
, tokp
);
329 unget_token (token
* tokp
)
336 findstrconst (const char **str
, const char **val
)
347 while (*p
&& *p
!= '"');
350 error (_("unterminated string constant"));
354 tmp
= alloc (size
+ 1);
355 strncpy (tmp
, *str
, size
);
362 findchrconst (const char **str
, const char **val
)
373 while (*p
&& *p
!= '\'');
376 error (_("unterminated string constant"));
382 error (_("empty char string"));
384 tmp
= alloc (size
+ 1);
385 strncpy (tmp
, *str
, size
);
392 findconst (const char **str
, const char **val
)
399 if (*p
== '0' && *(p
+ 1) == 'x')
406 while (isxdigit (*p
));
414 while (isdigit (*p
));
417 tmp
= alloc (size
+ 1);
418 strncpy (tmp
, *str
, size
);
424 static const token symbols
[] =
426 {TOK_CONST
, "const"},
427 {TOK_UNION
, "union"},
428 {TOK_SWITCH
, "switch"},
430 {TOK_DEFAULT
, "default"},
431 {TOK_STRUCT
, "struct"},
432 {TOK_TYPEDEF
, "typedef"},
434 {TOK_OPAQUE
, "opaque"},
439 {TOK_UNSIGNED
, "unsigned"},
440 {TOK_SHORT
, "short"},
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"));