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 <afs/param.h>
36 #include <afsconfig.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) == '}')
56 int pushed = 0; /* is a token pushed */
57 token lasttok; /* last token, if pushed */
60 static findstrconst();
67 * scan expecting 1 given token
75 if (tokp->kind != expect) {
81 * scan expecting 2 given tokens
84 scan2(expect1, expect2, tokp)
90 if (tokp->kind != expect1 && tokp->kind != expect2) {
91 expected2(expect1, expect2);
96 * scan expecting 3 given token
99 scan3(expect1, expect2, expect3, tokp)
106 if (tokp->kind != expect1 && tokp->kind != expect2
107 && tokp->kind != expect3) {
108 expected3(expect1, expect2, expect3);
114 * scan expecting 4 given token
117 scan4(expect1,expect2,expect3,expect4,tokp)
125 if (tokp->kind != expect1 && tokp->kind != expect2
126 && tokp->kind != expect3 && tokp->kind != expect4) {
127 expected4(expect1,expect2,expect3,expect4);
132 * scan expecting a constant, possibly symbolic
139 switch (tokp->kind) {
143 error("constant or identifier expected");
149 * Peek at the next token
161 * Peek at the next token and scan it if it matches what you expect
164 peekscan(expect, tokp)
169 if (tokp->kind == expect) {
179 * Get the next token, printing out any directive that are encountered.
197 if (!fgets(curline, MAXLINESIZE, fin)) {
198 tokp->kind = TOK_EOF;
204 fputs(curline, fout);
209 } else if (cppline(curline)) {
210 docppline(curline, &linenum,
212 } else if (directive(curline)) {
213 printdirective(curline);
219 } else if (isspace(*where)) {
220 while (isspace(*where)) {
223 } else if (verbatim) {
225 if (verbatimend(where)) {
229 } else if (verbatimstart(where)) {
232 } else if (commenting) {
234 if (endcomment(where)) {
238 } else if (startcomment(where)) {
247 * 'where' is not whitespace, comment or directive Must be a token!
251 tokp->kind = TOK_COLON;
255 tokp->kind = TOK_SEMICOLON;
259 tokp->kind = TOK_COMMA;
263 tokp->kind = TOK_EQUAL;
267 tokp->kind = TOK_STAR;
271 tokp->kind = TOK_LBRACKET;
275 tokp->kind = TOK_RBRACKET;
279 tokp->kind = TOK_LBRACE;
283 tokp->kind = TOK_RBRACE;
287 tokp->kind = TOK_LPAREN;
291 tokp->kind = TOK_RPAREN;
295 tokp->kind = TOK_LANGLE;
299 tokp->kind = TOK_RANGLE;
304 tokp->kind = TOK_STRCONST;
305 findstrconst(&where, &tokp->str);
319 tokp->kind = TOK_IDENT;
320 findconst(&where, &tokp->str);
325 if (!(isalpha(*where) || *where == '_')) {
329 s_print(buf, "illegal character in file: ");
330 p = buf + strlen(buf);
331 if (isprint(*where)) {
332 s_print(p, "%c", *where);
334 s_print(p, "%d", *where);
338 findkind(&where, tokp);
353 findstrconst(str, val)
363 } while (*p && *p != '"');
365 error("unterminated string constant");
369 *val = alloc(size + 1);
370 (void) strncpy(*val, *str, size);
384 if (*p == '0' && *(p + 1) == 'x') {
388 } while (isxdigit(*p));
392 } while (isdigit(*p));
395 *val = alloc(size + 1);
396 (void) strncpy(*val, *str, size);
403 static token symbols[] = {
404 {TOK_CONST, "const"},
405 {TOK_UNION, "union"},
406 {TOK_SWITCH, "switch"},
408 {TOK_DEFAULT, "default"},
409 {TOK_STRUCT, "struct"},
410 {TOK_TYPEDEF, "typedef"},
412 {TOK_OPAQUE, "opaque"},
417 {TOK_UNSIGNED, "unsigned"},
418 {TOK_SHORT, "short"},
419 {TOK_INT32, "afs_int32"},
420 {TOK_FLOAT, "float"},
421 {TOK_DOUBLE, "double"},
422 {TOK_STRING, "string"},
423 {TOK_PROGRAM, "program"},
424 {TOK_VERSION, "version"},
425 { TOK_PACKAGE, "package" },
426 { TOK_PREFIX, "prefix" },
427 { TOK_STATINDEX, "statindex" },
428 { TOK_SPECIAL, "special" },
429 { TOK_STARTINGOPCODE, "startingopcode" },
430 { TOK_CUSTOMIZED, "customized" },
431 { TOK_PROC, "proc" },
432 { TOK_SPLITPREFIX, "splitprefix" },
433 { TOK_SPLIT, "split" },
434 { TOK_MULTI, "multi" },
437 { TOK_INOUT, "INOUT" },
438 { TOK_AFSUUID, "afsUUID" },
453 for (s = symbols; s->kind != TOK_EOF; s++) {
454 len = strlen(s->str);
455 if (strncmp(str, s->str, len) == 0) {
456 if (!isalnum(str[len]) && str[len] != '_') {
457 tokp->kind = s->kind;
464 tokp->kind = TOK_IDENT;
465 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
466 tokp->str = alloc(len + 1);
467 (void) strncpy(tokp->str, str, len);
476 return (line == curline && *line == '#');
483 return (line == curline && *line == '%');
489 f_print(fout, "%s", line + 1);
493 docppline(line, lineno, fname)
503 while (isspace(*line)) {
507 while (isdigit(*line)) {
510 while (isspace(*line)) {
522 if (!strncmp(line, "line", 4)) {
530 error("preprocessor error");
533 p = file = alloc(strlen(line) + 1);
534 while (*line && *line != '"') {
538 error("preprocessor error");
553 for (where += 2; ! verbatimend(where) ; where++) {
555 if (! fgets(curline, MAXLINESIZE, fin)) {
556 error("unterminated code: %} is missing");
560 if (verbatimend(curline)) {
564 fputs(curline, fout);