1 /* @(#)rpc_scan.c 1.1 87/11/04 3.9 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
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 * rpc_scan.c, Scanner for the RPC protocol compiler
33 * Copyright (C) 1987, Sun Microsystems, Inc.
36 /* Portions Copyright (c) 2003 Apple Computer, Inc. */
37 #include <afsconfig.h>
38 #include <afs/param.h>
49 #include "rpc_parse.h"
52 #define startcomment(where) (where[0] == '/' && where[1] == '*')
53 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
54 #define verbatimstart(p) (*(p) == '@' && *((p) + 1) == '{')
55 #define verbatimend(p) (*(p) == '@' && *((p) + 1) == '}')
57 int pushed = 0; /* is a token pushed */
58 token lasttok; /* last token, if pushed */
61 /* static prototypes */
62 static void findstrconst(char **str, char **val);
63 static void findconst(char **str, char **val);
64 static int cppline(char *line);
65 static int directive(char *line);
66 static void docppline(char *line, int *lineno, char **fname);
68 static void deverbatim(void);
73 * scan expecting 1 given token
76 scan(tok_kind expect, token * tokp)
79 if (tokp->kind != expect) {
85 * scan expecting 2 given tokens
88 scan2(tok_kind expect1, tok_kind expect2, token * tokp)
91 if (tokp->kind != expect1 && tokp->kind != expect2) {
92 expected2(expect1, expect2);
97 * scan expecting 3 given token
100 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token * tokp)
103 if (tokp->kind != expect1 && tokp->kind != expect2
104 && tokp->kind != expect3) {
105 expected3(expect1, expect2, expect3);
111 * scan expecting 4 given token
114 scan4(tok_kind expect1, tok_kind expect2, tok_kind expect3, tok_kind expect4,
118 if (tokp->kind != expect1 && tokp->kind != expect2
119 && tokp->kind != expect3 && tokp->kind != expect4) {
120 expected4(expect1, expect2, expect3, expect4);
125 * scan expecting a constant, possibly symbolic
128 scan_num(token * tokp)
131 switch (tokp->kind) {
135 error("constant or identifier expected");
141 * Peek at the next token
152 * Peek at the next token and scan it if it matches what you expect
155 peekscan(tok_kind expect, token * tokp)
158 if (tokp->kind == expect) {
168 * Get the next token, printing out any directive that are encountered.
171 get_token(token * tokp)
185 if (!fgets(curline, MAXLINESIZE, fin)) {
186 tokp->kind = TOK_EOF;
192 fputs(curline, fout);
197 } else if (cppline(curline)) {
198 #if defined(AFS_DARWIN_ENV)
199 if (strncmp(curline, "#pragma", 7) == 0)
201 #endif /* defined(AFS_DARWIN_ENV) */
202 docppline(curline, &linenum, &infilename);
203 } else if (directive(curline)) {
204 printdirective(curline);
210 } else if (isspace(*where)) {
211 while (isspace(*where)) {
214 } else if (verbatim) {
216 if (verbatimend(where)) {
220 } else if (verbatimstart(where)) {
223 } else if (commenting) {
225 if (endcomment(where)) {
229 } else if (startcomment(where)) {
238 * 'where' is not whitespace, comment or directive Must be a token!
242 tokp->kind = TOK_COLON;
246 tokp->kind = TOK_SEMICOLON;
250 tokp->kind = TOK_COMMA;
254 tokp->kind = TOK_EQUAL;
258 tokp->kind = TOK_STAR;
262 tokp->kind = TOK_LBRACKET;
266 tokp->kind = TOK_RBRACKET;
270 tokp->kind = TOK_LBRACE;
274 tokp->kind = TOK_RBRACE;
278 tokp->kind = TOK_LPAREN;
282 tokp->kind = TOK_RPAREN;
286 tokp->kind = TOK_LANGLE;
290 tokp->kind = TOK_RANGLE;
295 tokp->kind = TOK_STRCONST;
296 findstrconst(&where, &tokp->str);
310 tokp->kind = TOK_IDENT;
311 findconst(&where, &tokp->str);
316 if (!(isalpha(*where) || *where == '_')) {
320 s_print(buf, "illegal character in file: ");
321 p = buf + strlen(buf);
322 if (isprint(*where)) {
323 s_print(p, "%c", *where);
325 s_print(p, "%d", *where);
329 findkind(&where, tokp);
336 unget_token(token * tokp)
344 findstrconst(char **str, char **val)
352 } while (*p && *p != '"');
354 error("unterminated string constant");
357 size = (int)(p - *str);
358 *val = alloc(size + 1);
359 (void)strncpy(*val, *str, size);
365 findconst(char **str, char **val)
371 if (*p == '0' && *(p + 1) == 'x') {
375 } while (isxdigit(*p));
379 } while (isdigit(*p));
381 size = (int)(p - *str);
382 *val = alloc(size + 1);
383 (void)strncpy(*val, *str, size);
390 static token symbols[] = {
391 {TOK_CONST, "const"},
392 {TOK_UNION, "union"},
393 {TOK_SWITCH, "switch"},
395 {TOK_DEFAULT, "default"},
396 {TOK_STRUCT, "struct"},
397 {TOK_TYPEDEF, "typedef"},
399 {TOK_OPAQUE, "opaque"},
404 {TOK_UNSIGNED, "unsigned"},
405 {TOK_SHORT, "short"},
406 {TOK_INT32, "afs_int32"},
407 {TOK_FLOAT, "float"},
408 {TOK_DOUBLE, "double"},
409 {TOK_STRING, "string"},
410 {TOK_PROGRAM, "program"},
411 {TOK_VERSION, "version"},
412 {TOK_PACKAGE, "package"},
413 {TOK_PREFIX, "prefix"},
414 {TOK_STATINDEX, "statindex"},
415 {TOK_SPECIAL, "special"},
416 {TOK_STARTINGOPCODE, "startingopcode"},
417 {TOK_CUSTOMIZED, "customized"},
419 {TOK_SPLITPREFIX, "splitprefix"},
420 {TOK_SPLIT, "split"},
421 {TOK_MULTI, "multi"},
424 {TOK_INOUT, "INOUT"},
425 {TOK_AFSUUID, "afsUUID"},
431 findkind(char **mark, token * tokp)
439 for (s = symbols; s->kind != TOK_EOF; s++) {
440 len = (int)strlen(s->str);
441 if (strncmp(str, s->str, len) == 0) {
442 if (!isalnum(str[len]) && str[len] != '_') {
443 tokp->kind = s->kind;
450 tokp->kind = TOK_IDENT;
451 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
452 tokp->str = alloc(len + 1);
453 (void)strncpy(tokp->str, str, len);
461 return (line == curline && *line == '#');
465 directive(char *line)
467 return (line == curline && *line == '%');
471 printdirective(char *line)
473 f_print(fout, "%s", line + 1);
477 docppline(char *line, int *lineno, char **fname)
484 while (isspace(*line)) {
488 while (isdigit(*line)) {
491 while (isspace(*line)) {
503 if (!strncmp(line, "line", 4)) {
511 error("preprocessor error");
514 p = file = alloc(strlen(line) + 1);
515 while (*line && *line != '"') {
519 error("preprocessor error");
532 /* doesn't appear to be used */
536 for (where += 2; !verbatimend(where); where++) {
538 if (!fgets(curline, MAXLINESIZE, fin)) {
539 error("unterminated code: %} is missing");
543 if (verbatimend(curline)) {
547 fputs(curline, fout);