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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char scan_rcsid
[] =
40 * rpc_scan.c, Scanner for the RPC protocol compiler
41 * Copyright (C) 1987, Sun Microsystems, Inc.
48 #include "rpc_parse.h"
52 #define startcomment(where) (where[0] == '/' && where[1] == '*')
53 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
55 static int pushed
= 0; /* is a token pushed */
56 static token lasttok
; /* last token, if pushed */
58 static void unget_token (token
* tokp
);
59 static void findstrconst (const char **str
, const char **val
);
60 static void findchrconst (const char **str
, const char **val
);
61 static void findconst (const char **str
, const char **val
);
62 static void findkind (const char **mark
, token
* tokp
);
63 static int cppline (const char *line
);
64 static int directive (const char *line
);
65 static void printdirective (const char *line
);
66 static void docppline (const char *line
, int *lineno
, const char **fname
);
69 * scan expecting 1 given token
72 scan (tok_kind expect
, token
* tokp
)
75 if (tokp
->kind
!= expect
)
80 * scan expecting any of the 2 given tokens
83 scan2 (tok_kind expect1
, tok_kind expect2
, token
* tokp
)
86 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
)
88 expected2 (expect1
, expect2
);
93 * scan expecting any of the 3 given token
96 scan3 (tok_kind expect1
, tok_kind expect2
, tok_kind expect3
, token
* tokp
)
99 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
100 && tokp
->kind
!= expect3
)
102 expected3 (expect1
, expect2
, expect3
);
107 * scan expecting a constant, possibly symbolic
110 scan_num (token
*tokp
)
118 error (_("constant or identifier expected"));
123 * Peek at the next token
133 * Peek at the next token and scan it if it matches what you expect
136 peekscan (tok_kind expect
, token
*tokp
)
139 if (tokp
->kind
== expect
)
148 * Get the next token, printing out any directive that are encountered.
151 get_token (token
*tokp
)
168 if (!fgets (curline
, MAXLINESIZE
, fin
))
170 tokp
->kind
= TOK_EOF
;
180 else if (cppline (curline
))
182 docppline (curline
, &linenum
,
185 else if (directive (curline
))
187 printdirective (curline
);
196 else if (isspace (*where
))
198 while (isspace (*where
))
205 for (where
++; *where
; where
++)
207 if (endcomment (where
))
215 else if (startcomment (where
))
227 * 'where' is not whitespace, comment or directive Must be a token!
232 tokp
->kind
= TOK_COLON
;
236 tokp
->kind
= TOK_SEMICOLON
;
240 tokp
->kind
= TOK_COMMA
;
244 tokp
->kind
= TOK_EQUAL
;
248 tokp
->kind
= TOK_STAR
;
252 tokp
->kind
= TOK_LBRACKET
;
256 tokp
->kind
= TOK_RBRACKET
;
260 tokp
->kind
= TOK_LBRACE
;
264 tokp
->kind
= TOK_RBRACE
;
268 tokp
->kind
= TOK_LPAREN
;
272 tokp
->kind
= TOK_RPAREN
;
276 tokp
->kind
= TOK_LANGLE
;
280 tokp
->kind
= TOK_RANGLE
;
285 tokp
->kind
= TOK_STRCONST
;
286 findstrconst (&where
, &tokp
->str
);
289 tokp
->kind
= TOK_CHARCONST
;
290 findchrconst (&where
, &tokp
->str
);
304 tokp
->kind
= TOK_IDENT
;
305 findconst (&where
, &tokp
->str
);
309 if (!(isalpha (*where
) || *where
== '_'))
314 s_print (buf
, _("illegal character in file: "));
315 p
= buf
+ strlen (buf
);
316 if (isprint (*where
))
318 s_print (p
, "%c", *where
);
322 s_print (p
, "%d", *where
);
326 findkind (&where
, tokp
);
332 unget_token (token
* tokp
)
339 findstrconst (const char **str
, const char **val
)
350 while (*p
&& *p
!= '"');
353 error (_("unterminated string constant"));
357 tmp
= alloc (size
+ 1);
358 strncpy (tmp
, *str
, size
);
365 findchrconst (const char **str
, const char **val
)
376 while (*p
&& *p
!= '\'');
379 error (_("unterminated string constant"));
385 error (_("empty char string"));
387 tmp
= alloc (size
+ 1);
388 strncpy (tmp
, *str
, size
);
395 findconst (const char **str
, const char **val
)
402 if (*p
== '0' && *(p
+ 1) == 'x')
409 while (isxdigit (*p
));
417 while (isdigit (*p
));
420 tmp
= alloc (size
+ 1);
421 strncpy (tmp
, *str
, size
);
427 static const token symbols
[] =
429 {TOK_CONST
, "const"},
430 {TOK_UNION
, "union"},
431 {TOK_SWITCH
, "switch"},
433 {TOK_DEFAULT
, "default"},
434 {TOK_STRUCT
, "struct"},
435 {TOK_TYPEDEF
, "typedef"},
437 {TOK_OPAQUE
, "opaque"},
442 {TOK_UNSIGNED
, "unsigned"},
443 {TOK_SHORT
, "short"},
445 {TOK_HYPER
, "hyper"},
446 {TOK_FLOAT
, "float"},
447 {TOK_DOUBLE
, "double"},
448 {TOK_STRING
, "string"},
449 {TOK_PROGRAM
, "program"},
450 {TOK_VERSION
, "version"},
455 findkind (const char **mark
, token
*tokp
)
463 for (s
= symbols
; s
->kind
!= TOK_EOF
; s
++)
465 len
= strlen (s
->str
);
466 if (strncmp (str
, s
->str
, len
) == 0)
468 if (!isalnum (str
[len
]) && str
[len
] != '_')
470 tokp
->kind
= s
->kind
;
477 tokp
->kind
= TOK_IDENT
;
478 for (len
= 0; isalnum (str
[len
]) || str
[len
] == '_'; len
++);
479 tmp
= alloc (len
+ 1);
480 strncpy (tmp
, str
, len
);
487 cppline (const char *line
)
489 return line
== curline
&& *line
== '#';
493 directive (const char *line
)
495 return line
== curline
&& *line
== '%';
499 printdirective (const char *line
)
501 f_print (fout
, "%s", line
+ 1);
505 docppline (const char *line
, int *lineno
, const char **fname
)
512 while (isspace (*line
))
517 while (isdigit (*line
))
521 while (isspace (*line
))
527 error (_("preprocessor error"));
530 p
= file
= alloc (strlen (line
) + 1);
531 while (*line
&& *line
!= '"')
537 error (_("preprocessor error"));