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.
46 #include "rpc_parse.h"
50 #define startcomment(where) (where[0] == '/' && where[1] == '*')
51 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
53 static int pushed
= 0; /* is a token pushed */
54 static token lasttok
; /* last token, if pushed */
56 static void unget_token (token
* tokp
);
57 static void findstrconst (const char **str
, const char **val
);
58 static void findchrconst (const char **str
, const char **val
);
59 static void findconst (const char **str
, const char **val
);
60 static void findkind (const char **mark
, token
* tokp
);
61 static int cppline (const char *line
);
62 static int directive (const char *line
);
63 static void printdirective (const char *line
);
64 static void docppline (const char *line
, int *lineno
, const char **fname
);
67 * scan expecting 1 given token
70 scan (tok_kind expect
, token
* tokp
)
73 if (tokp
->kind
!= expect
)
78 * scan expecting any of the 2 given tokens
81 scan2 (tok_kind expect1
, tok_kind expect2
, token
* tokp
)
84 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
)
86 expected2 (expect1
, expect2
);
91 * scan expecting any of the 3 given token
94 scan3 (tok_kind expect1
, tok_kind expect2
, tok_kind expect3
, token
* tokp
)
97 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
98 && tokp
->kind
!= expect3
)
100 expected3 (expect1
, expect2
, expect3
);
105 * scan expecting a constant, possibly symbolic
108 scan_num (token
*tokp
)
116 error (_("constant or identifier expected"));
121 * Peek at the next token
131 * Peek at the next token and scan it if it matches what you expect
134 peekscan (tok_kind expect
, token
*tokp
)
137 if (tokp
->kind
== expect
)
146 * Get the next token, printing out any directive that are encountered.
149 get_token (token
*tokp
)
166 if (!fgets (curline
, MAXLINESIZE
, fin
))
168 tokp
->kind
= TOK_EOF
;
178 else if (cppline (curline
))
180 docppline (curline
, &linenum
,
183 else if (directive (curline
))
185 printdirective (curline
);
194 else if (isspace (*where
))
196 while (isspace (*where
))
203 for (where
++; *where
; where
++)
205 if (endcomment (where
))
213 else if (startcomment (where
))
225 * 'where' is not whitespace, comment or directive Must be a token!
230 tokp
->kind
= TOK_COLON
;
234 tokp
->kind
= TOK_SEMICOLON
;
238 tokp
->kind
= TOK_COMMA
;
242 tokp
->kind
= TOK_EQUAL
;
246 tokp
->kind
= TOK_STAR
;
250 tokp
->kind
= TOK_LBRACKET
;
254 tokp
->kind
= TOK_RBRACKET
;
258 tokp
->kind
= TOK_LBRACE
;
262 tokp
->kind
= TOK_RBRACE
;
266 tokp
->kind
= TOK_LPAREN
;
270 tokp
->kind
= TOK_RPAREN
;
274 tokp
->kind
= TOK_LANGLE
;
278 tokp
->kind
= TOK_RANGLE
;
283 tokp
->kind
= TOK_STRCONST
;
284 findstrconst (&where
, &tokp
->str
);
287 tokp
->kind
= TOK_CHARCONST
;
288 findchrconst (&where
, &tokp
->str
);
302 tokp
->kind
= TOK_IDENT
;
303 findconst (&where
, &tokp
->str
);
307 if (!(isalpha (*where
) || *where
== '_'))
312 s_print (buf
, _("illegal character in file: "));
313 p
= buf
+ strlen (buf
);
314 if (isprint (*where
))
316 s_print (p
, "%c", *where
);
320 s_print (p
, "%d", *where
);
324 findkind (&where
, tokp
);
330 unget_token (token
* tokp
)
337 findstrconst (const char **str
, const char **val
)
348 while (*p
&& *p
!= '"');
351 error (_("unterminated string constant"));
355 tmp
= alloc (size
+ 1);
356 strncpy (tmp
, *str
, size
);
363 findchrconst (const char **str
, const char **val
)
374 while (*p
&& *p
!= '\'');
377 error (_("unterminated string constant"));
383 error (_("empty char string"));
385 tmp
= alloc (size
+ 1);
386 strncpy (tmp
, *str
, size
);
393 findconst (const char **str
, const char **val
)
400 if (*p
== '0' && *(p
+ 1) == 'x')
407 while (isxdigit (*p
));
415 while (isdigit (*p
));
418 tmp
= alloc (size
+ 1);
419 strncpy (tmp
, *str
, size
);
425 static const token symbols
[] =
427 {TOK_CONST
, "const"},
428 {TOK_UNION
, "union"},
429 {TOK_SWITCH
, "switch"},
431 {TOK_DEFAULT
, "default"},
432 {TOK_STRUCT
, "struct"},
433 {TOK_TYPEDEF
, "typedef"},
435 {TOK_OPAQUE
, "opaque"},
440 {TOK_UNSIGNED
, "unsigned"},
441 {TOK_SHORT
, "short"},
443 {TOK_FLOAT
, "float"},
444 {TOK_DOUBLE
, "double"},
445 {TOK_STRING
, "string"},
446 {TOK_PROGRAM
, "program"},
447 {TOK_VERSION
, "version"},
452 findkind (const char **mark
, token
*tokp
)
460 for (s
= symbols
; s
->kind
!= TOK_EOF
; s
++)
462 len
= strlen (s
->str
);
463 if (strncmp (str
, s
->str
, len
) == 0)
465 if (!isalnum (str
[len
]) && str
[len
] != '_')
467 tokp
->kind
= s
->kind
;
474 tokp
->kind
= TOK_IDENT
;
475 for (len
= 0; isalnum (str
[len
]) || str
[len
] == '_'; len
++);
476 tmp
= alloc (len
+ 1);
477 strncpy (tmp
, str
, len
);
484 cppline (const char *line
)
486 return line
== curline
&& *line
== '#';
490 directive (const char *line
)
492 return line
== curline
&& *line
== '%';
496 printdirective (const char *line
)
498 f_print (fout
, "%s", line
+ 1);
502 docppline (const char *line
, int *lineno
, const char **fname
)
509 while (isspace (*line
))
514 while (isdigit (*line
))
518 while (isspace (*line
))
524 error (_("preprocessor error"));
527 p
= file
= alloc (strlen (line
) + 1);
528 while (*line
&& *line
!= '"')
534 error (_("preprocessor error"));