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.
35 #include <afsconfig.h>
36 #include <afs/param.h>
53 #define startcomment(where) (where[0] == '/' && where[1] == '*')
54 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
55 #define verbatimstart(p) (*(p) == '@' && *((p) + 1) == '{')
56 #define verbatimend(p) (*(p) == '@' && *((p) + 1) == '}')
57 int pushed = 0; /* is a token pushed */
58 token lasttok; /* last token, if pushed */
61 static findstrconst();
68 * scan expecting 1 given token
76 if (tokp->kind != expect) {
82 * scan expecting 2 given tokens
85 scan2(expect1, expect2, tokp)
91 if (tokp->kind != expect1 && tokp->kind != expect2) {
92 expected2(expect1, expect2);
97 * scan expecting 3 given token
100 scan3(expect1, expect2, expect3, tokp)
107 if (tokp->kind != expect1 && tokp->kind != expect2
108 && tokp->kind != expect3) {
109 expected3(expect1, expect2, expect3);
115 * scan expecting 4 given token
118 scan4(expect1,expect2,expect3,expect4,tokp)
126 if (tokp->kind != expect1 && tokp->kind != expect2
127 && tokp->kind != expect3 && tokp->kind != expect4) {
128 expected4(expect1,expect2,expect3,expect4);
133 * scan expecting a constant, possibly symbolic
140 switch (tokp->kind) {
144 error("constant or identifier expected");
150 * Peek at the next token
162 * Peek at the next token and scan it if it matches what you expect
165 peekscan(expect, tokp)
170 if (tokp->kind == expect) {
180 * Get the next token, printing out any directive that are encountered.
198 if (!fgets(curline, MAXLINESIZE, fin)) {
199 tokp->kind = TOK_EOF;
205 fputs(curline, fout);
210 } else if (cppline(curline)) {
211 docppline(curline, &linenum,
213 } else if (directive(curline)) {
214 printdirective(curline);
220 } else if (isspace(*where)) {
221 while (isspace(*where)) {
224 } else if (verbatim) {
226 if (verbatimend(where)) {
230 } else if (verbatimstart(where)) {
233 } else if (commenting) {
235 if (endcomment(where)) {
239 } else if (startcomment(where)) {
248 * 'where' is not whitespace, comment or directive Must be a token!
252 tokp->kind = TOK_COLON;
256 tokp->kind = TOK_SEMICOLON;
260 tokp->kind = TOK_COMMA;
264 tokp->kind = TOK_EQUAL;
268 tokp->kind = TOK_STAR;
272 tokp->kind = TOK_LBRACKET;
276 tokp->kind = TOK_RBRACKET;
280 tokp->kind = TOK_LBRACE;
284 tokp->kind = TOK_RBRACE;
288 tokp->kind = TOK_LPAREN;
292 tokp->kind = TOK_RPAREN;
296 tokp->kind = TOK_LANGLE;
300 tokp->kind = TOK_RANGLE;
305 tokp->kind = TOK_STRCONST;
306 findstrconst(&where, &tokp->str);
320 tokp->kind = TOK_IDENT;
321 findconst(&where, &tokp->str);
326 if (!(isalpha(*where) || *where == '_')) {
330 s_print(buf, "illegal character in file: ");
331 p = buf + strlen(buf);
332 if (isprint(*where)) {
333 s_print(p, "%c", *where);
335 s_print(p, "%d", *where);
339 findkind(&where, tokp);
354 findstrconst(str, val)
364 } while (*p && *p != '"');
366 error("unterminated string constant");
370 *val = alloc(size + 1);
371 (void) strncpy(*val, *str, size);
385 if (*p == '0' && *(p + 1) == 'x') {
389 } while (isxdigit(*p));
393 } while (isdigit(*p));
396 *val = alloc(size + 1);
397 (void) strncpy(*val, *str, size);
404 static token symbols[] = {
405 {TOK_CONST, "const"},
406 {TOK_UNION, "union"},
407 {TOK_SWITCH, "switch"},
409 {TOK_DEFAULT, "default"},
410 {TOK_STRUCT, "struct"},
411 {TOK_TYPEDEF, "typedef"},
413 {TOK_OPAQUE, "opaque"},
418 {TOK_UNSIGNED, "unsigned"},
419 {TOK_SHORT, "short"},
420 {TOK_INT32, "afs_int32"},
421 {TOK_FLOAT, "float"},
422 {TOK_DOUBLE, "double"},
423 {TOK_STRING, "string"},
424 {TOK_PROGRAM, "program"},
425 {TOK_VERSION, "version"},
426 { TOK_PACKAGE, "package" },
427 { TOK_PREFIX, "prefix" },
428 { TOK_STATINDEX, "statindex" },
429 { TOK_SPECIAL, "special" },
430 { TOK_STARTINGOPCODE, "startingopcode" },
431 { TOK_CUSTOMIZED, "customized" },
432 { TOK_PROC, "proc" },
433 { TOK_SPLITPREFIX, "splitprefix" },
434 { TOK_SPLIT, "split" },
435 { TOK_MULTI, "multi" },
438 { TOK_INOUT, "INOUT" },
439 { TOK_AFSUUID, "afsUUID" },
454 for (s = symbols; s->kind != TOK_EOF; s++) {
455 len = strlen(s->str);
456 if (strncmp(str, s->str, len) == 0) {
457 if (!isalnum(str[len]) && str[len] != '_') {
458 tokp->kind = s->kind;
465 tokp->kind = TOK_IDENT;
466 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
467 tokp->str = alloc(len + 1);
468 (void) strncpy(tokp->str, str, len);
477 return (line == curline && *line == '#');
484 return (line == curline && *line == '%');
490 f_print(fout, "%s", line + 1);
494 docppline(line, lineno, fname)
504 while (isspace(*line)) {
508 while (isdigit(*line)) {
511 while (isspace(*line)) {
523 if (!strncmp(line, "line", 4)) {
531 error("preprocessor error");
534 p = file = alloc(strlen(line) + 1);
535 while (*line && *line != '"') {
539 error("preprocessor error");
554 for (where += 2; ! verbatimend(where) ; where++) {
556 if (! fgets(curline, MAXLINESIZE, fin)) {
557 error("unterminated code: %} is missing");
561 if (verbatimend(curline)) {
565 fputs(curline, fout);