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
)
79 * scan expecting any of the 2 given tokens
82 scan2 (tok_kind expect1
, tok_kind expect2
, token
* tokp
)
85 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
)
87 expected2 (expect1
, expect2
);
92 * scan expecting any of the 3 given token
95 scan3 (tok_kind expect1
, tok_kind expect2
, tok_kind expect3
, token
* tokp
)
98 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
99 && tokp
->kind
!= expect3
)
101 expected3 (expect1
, expect2
, expect3
);
106 * scan expecting a constant, possibly symbolic
109 scan_num (token
* tokp
)
117 error ("constant or identifier expected");
122 * Peek at the next token
132 * Peek at the next token and scan it if it matches what you expect
135 peekscan (tok_kind expect
, token
* tokp
)
138 if (tokp
->kind
== expect
)
147 * Get the next token, printing out any directive that are encountered.
150 get_token (token
* tokp
)
167 if (!fgets (curline
, MAXLINESIZE
, fin
))
169 tokp
->kind
= TOK_EOF
;
179 else if (cppline (curline
))
181 docppline (curline
, &linenum
,
184 else if (directive (curline
))
186 printdirective (curline
);
195 else if (isspace (*where
))
197 while (isspace (*where
))
204 for (where
++; *where
; where
++)
206 if (endcomment (where
))
214 else if (startcomment (where
))
226 * 'where' is not whitespace, comment or directive Must be a token!
231 tokp
->kind
= TOK_COLON
;
235 tokp
->kind
= TOK_SEMICOLON
;
239 tokp
->kind
= TOK_COMMA
;
243 tokp
->kind
= TOK_EQUAL
;
247 tokp
->kind
= TOK_STAR
;
251 tokp
->kind
= TOK_LBRACKET
;
255 tokp
->kind
= TOK_RBRACKET
;
259 tokp
->kind
= TOK_LBRACE
;
263 tokp
->kind
= TOK_RBRACE
;
267 tokp
->kind
= TOK_LPAREN
;
271 tokp
->kind
= TOK_RPAREN
;
275 tokp
->kind
= TOK_LANGLE
;
279 tokp
->kind
= TOK_RANGLE
;
284 tokp
->kind
= TOK_STRCONST
;
285 findstrconst (&where
, &tokp
->str
);
288 tokp
->kind
= TOK_CHARCONST
;
289 findchrconst (&where
, &tokp
->str
);
303 tokp
->kind
= TOK_IDENT
;
304 findconst (&where
, &tokp
->str
);
308 if (!(isalpha (*where
) || *where
== '_'))
313 s_print (buf
, _("illegal character in file: "));
314 p
= buf
+ strlen (buf
);
315 if (isprint (*where
))
317 s_print (p
, "%c", *where
);
321 s_print (p
, "%d", *where
);
325 findkind (&where
, tokp
);
331 unget_token (token
* tokp
)
338 findstrconst (const char **str
, const char **val
)
349 while (*p
&& *p
!= '"');
352 error (_("unterminated string constant"));
356 tmp
= alloc (size
+ 1);
357 strncpy (tmp
, *str
, size
);
364 findchrconst (const char **str
, const char **val
)
375 while (*p
&& *p
!= '\'');
378 error (_("unterminated string constant"));
384 error (_("empty char string"));
386 tmp
= alloc (size
+ 1);
387 strncpy (tmp
, *str
, size
);
394 findconst (const char **str
, const char **val
)
401 if (*p
== '0' && *(p
+ 1) == 'x')
408 while (isxdigit (*p
));
416 while (isdigit (*p
));
419 tmp
= alloc (size
+ 1);
420 strncpy (tmp
, *str
, size
);
426 static const token symbols
[] =
428 {TOK_CONST
, "const"},
429 {TOK_UNION
, "union"},
430 {TOK_SWITCH
, "switch"},
432 {TOK_DEFAULT
, "default"},
433 {TOK_STRUCT
, "struct"},
434 {TOK_TYPEDEF
, "typedef"},
436 {TOK_OPAQUE
, "opaque"},
441 {TOK_UNSIGNED
, "unsigned"},
442 {TOK_SHORT
, "short"},
444 {TOK_FLOAT
, "float"},
445 {TOK_DOUBLE
, "double"},
446 {TOK_STRING
, "string"},
447 {TOK_PROGRAM
, "program"},
448 {TOK_VERSION
, "version"},
453 findkind (const char **mark
, token
* tokp
)
461 for (s
= symbols
; s
->kind
!= TOK_EOF
; s
++)
463 len
= strlen (s
->str
);
464 if (strncmp (str
, s
->str
, len
) == 0)
466 if (!isalnum (str
[len
]) && str
[len
] != '_')
468 tokp
->kind
= s
->kind
;
475 tokp
->kind
= TOK_IDENT
;
476 for (len
= 0; isalnum (str
[len
]) || str
[len
] == '_'; len
++);
477 tmp
= alloc (len
+ 1);
478 strncpy (tmp
, str
, len
);
485 cppline (const char *line
)
487 return line
== curline
&& *line
== '#';
491 directive (const char *line
)
493 return line
== curline
&& *line
== '%';
497 printdirective (const char *line
)
499 f_print (fout
, "%s", line
+ 1);
503 docppline (const char *line
, int *lineno
, const char **fname
)
510 while (isspace (*line
))
515 while (isdigit (*line
))
519 while (isspace (*line
))
525 error (_("preprocessor error"));
528 p
= file
= alloc (strlen (line
) + 1);
529 while (*line
&& *line
!= '"')
535 error (_("preprocessor error"));