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>
47 #include "rpc_parse.h"
50 #define startcomment(where) (where[0] == '/' && where[1] == '*')
51 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
52 #define verbatimstart(p) (*(p) == '@' && *((p) + 1) == '{')
53 #define verbatimend(p) (*(p) == '@' && *((p) + 1) == '}')
55 int pushed = 0; /* is a token pushed */
56 token lasttok; /* last token, if pushed */
59 /* static prototypes */
60 static void findstrconst(char **str, char **val);
61 static void findconst(char **str, char **val);
62 static int cppline(char *line);
63 static int directive(char *line);
64 static void docppline(char *line, int *lineno, char **fname);
66 static void deverbatim(void);
71 * scan expecting 1 given token
74 scan(tok_kind expect, token * tokp)
77 if (tokp->kind != expect) {
83 * scan expecting 2 given tokens
86 scan2(tok_kind expect1, tok_kind expect2, token * tokp)
89 if (tokp->kind != expect1 && tokp->kind != expect2) {
90 expected2(expect1, expect2);
95 * scan expecting 3 given token
98 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token * tokp)
101 if (tokp->kind != expect1 && tokp->kind != expect2
102 && tokp->kind != expect3) {
103 expected3(expect1, expect2, expect3);
109 * scan expecting 4 given token
112 scan4(tok_kind expect1, tok_kind expect2, tok_kind expect3, tok_kind expect4,
116 if (tokp->kind != expect1 && tokp->kind != expect2
117 && tokp->kind != expect3 && tokp->kind != expect4) {
118 expected4(expect1, expect2, expect3, expect4);
123 * scan expecting a constant, possibly symbolic
126 scan_num(token * tokp)
129 switch (tokp->kind) {
133 error("constant or identifier expected");
139 * Peek at the next token
150 * Peek at the next token and scan it if it matches what you expect
153 peekscan(tok_kind expect, token * tokp)
156 if (tokp->kind == expect) {
166 * Get the next token, printing out any directive that are encountered.
169 get_token(token * tokp)
183 if (!fgets(curline, MAXLINESIZE, fin)) {
184 tokp->kind = TOK_EOF;
190 fputs(curline, fout);
195 } else if (cppline(curline)) {
196 #if defined(AFS_DARWIN_ENV)
197 if (strncmp(curline, "#pragma", 7) == 0)
199 #endif /* defined(AFS_DARWIN_ENV) */
200 docppline(curline, &linenum, &infilename);
201 } else if (directive(curline)) {
202 printdirective(curline);
208 } else if (isspace(*where)) {
209 while (isspace(*where)) {
212 } else if (verbatim) {
214 if (verbatimend(where)) {
218 } else if (verbatimstart(where)) {
221 } else if (commenting) {
223 if (endcomment(where)) {
227 } else if (startcomment(where)) {
236 * 'where' is not whitespace, comment or directive Must be a token!
240 tokp->kind = TOK_COLON;
244 tokp->kind = TOK_SEMICOLON;
248 tokp->kind = TOK_COMMA;
252 tokp->kind = TOK_EQUAL;
256 tokp->kind = TOK_STAR;
260 tokp->kind = TOK_LBRACKET;
264 tokp->kind = TOK_RBRACKET;
268 tokp->kind = TOK_LBRACE;
272 tokp->kind = TOK_RBRACE;
276 tokp->kind = TOK_LPAREN;
280 tokp->kind = TOK_RPAREN;
284 tokp->kind = TOK_LANGLE;
288 tokp->kind = TOK_RANGLE;
293 tokp->kind = TOK_STRCONST;
294 findstrconst(&where, &tokp->str);
308 tokp->kind = TOK_IDENT;
309 findconst(&where, &tokp->str);
314 if (!(isalpha(*where) || *where == '_')) {
318 s_print(buf, "illegal character in file: ");
319 p = buf + strlen(buf);
320 if (isprint(*where)) {
321 s_print(p, "%c", *where);
323 s_print(p, "%d", *where);
327 findkind(&where, tokp);
334 unget_token(token * tokp)
342 findstrconst(char **str, char **val)
350 } while (*p && *p != '"');
352 error("unterminated string constant");
355 size = (int)(p - *str);
356 *val = alloc(size + 1);
357 (void)strncpy(*val, *str, size);
363 findconst(char **str, char **val)
369 if (*p == '0' && *(p + 1) == 'x') {
373 } while (isxdigit(*p));
377 } while (isdigit(*p));
379 size = (int)(p - *str);
380 *val = alloc(size + 1);
381 (void)strncpy(*val, *str, size);
388 static token symbols[] = {
389 {TOK_CONST, "const"},
390 {TOK_UNION, "union"},
391 {TOK_SWITCH, "switch"},
393 {TOK_DEFAULT, "default"},
394 {TOK_STRUCT, "struct"},
395 {TOK_TYPEDEF, "typedef"},
397 {TOK_OPAQUE, "opaque"},
402 {TOK_UNSIGNED, "unsigned"},
403 {TOK_SHORT, "short"},
404 {TOK_INT32, "afs_int32"},
405 {TOK_FLOAT, "float"},
406 {TOK_DOUBLE, "double"},
407 {TOK_STRING, "string"},
408 {TOK_PROGRAM, "program"},
409 {TOK_VERSION, "version"},
410 {TOK_PACKAGE, "package"},
411 {TOK_PREFIX, "prefix"},
412 {TOK_STATINDEX, "statindex"},
413 {TOK_SPECIAL, "special"},
414 {TOK_STARTINGOPCODE, "startingopcode"},
415 {TOK_CUSTOMIZED, "customized"},
417 {TOK_SPLITPREFIX, "splitprefix"},
418 {TOK_SPLIT, "split"},
419 {TOK_MULTI, "multi"},
422 {TOK_INOUT, "INOUT"},
423 {TOK_AFSUUID, "afsUUID"},
429 findkind(char **mark, token * tokp)
437 for (s = symbols; s->kind != TOK_EOF; s++) {
438 len = (int)strlen(s->str);
439 if (strncmp(str, s->str, len) == 0) {
440 if (!isalnum(str[len]) && str[len] != '_') {
441 tokp->kind = s->kind;
448 tokp->kind = TOK_IDENT;
449 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
450 tokp->str = alloc(len + 1);
451 (void)strncpy(tokp->str, str, len);
459 return (line == curline && *line == '#');
463 directive(char *line)
465 return (line == curline && *line == '%');
469 printdirective(char *line)
471 f_print(fout, "%s", line + 1);
475 docppline(char *line, int *lineno, char **fname)
482 while (isspace(*line)) {
486 while (isdigit(*line)) {
489 while (isspace(*line)) {
501 if (!strncmp(line, "line", 4)) {
509 error("preprocessor error");
512 p = file = alloc(strlen(line) + 1);
513 while (*line && *line != '"') {
517 error("preprocessor error");
530 /* doesn't appear to be used */
534 for (where += 2; !verbatimend(where); where++) {
536 if (!fgets(curline, MAXLINESIZE, fin)) {
537 error("unterminated code: %} is missing");
541 if (verbatimend(curline)) {
545 fputs(curline, fout);