venus: Remove dedebug
[openafs.git] / src / rxgen / rpc_scan.c
1 /* @(#)rpc_scan.c       1.1 87/11/04 3.9 RPCSRC */
2 /*
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.
9  *
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.
13  *
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.
17  *
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.
21  *
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.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 /*
32  * rpc_scan.c, Scanner for the RPC protocol compiler
33  * Copyright (C) 1987, Sun Microsystems, Inc.
34  */
35
36 /* Portions Copyright (c) 2003 Apple Computer, Inc. */
37 #include <afsconfig.h>
38 #include <afs/param.h>
39
40 #include <roken.h>
41
42 #include <ctype.h>
43
44 #include "rpc_scan.h"
45 #include "rpc_parse.h"
46 #include "rpc_util.h"
47
48 #define startcomment(where) (where[0] == '/' && where[1] == '*')
49 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
50 #define verbatimstart(p) (*(p) == '@' && *((p) + 1) == '{')
51 #define verbatimend(p)  (*(p) == '@' && *((p) + 1) == '}')
52
53 int pushed = 0;                 /* is a token pushed */
54 token lasttok;                  /* last token, if pushed */
55 int scan_print = 1;
56
57 /* static prototypes */
58 static void findstrconst(char **str, char **val);
59 static void findconst(char **str, char **val);
60 static int cppline(char *line);
61 static int directive(char *line);
62 static void docppline(char *line, int *lineno, char **fname);
63
64 /*
65  * scan expecting 1 given token
66  */
67 void
68 scan(tok_kind expect, token * tokp)
69 {
70     get_token(tokp);
71     if (tokp->kind != expect) {
72         expected1(expect);
73     }
74 }
75
76 /*
77  * scan expecting 2 given tokens
78  */
79 void
80 scan2(tok_kind expect1, tok_kind expect2, token * tokp)
81 {
82     get_token(tokp);
83     if (tokp->kind != expect1 && tokp->kind != expect2) {
84         expected2(expect1, expect2);
85     }
86 }
87
88 /*
89  * scan expecting 3 given token
90  */
91 void
92 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token * tokp)
93 {
94     get_token(tokp);
95     if (tokp->kind != expect1 && tokp->kind != expect2
96         && tokp->kind != expect3) {
97         expected3(expect1, expect2, expect3);
98     }
99 }
100
101
102 /*
103  * scan expecting 4 given token
104  */
105 void
106 scan4(tok_kind expect1, tok_kind expect2, tok_kind expect3, tok_kind expect4,
107       token * tokp)
108 {
109     get_token(tokp);
110     if (tokp->kind != expect1 && tokp->kind != expect2
111         && tokp->kind != expect3 && tokp->kind != expect4) {
112         expected4(expect1, expect2, expect3, expect4);
113     }
114 }
115
116 /*
117  * scan expecting a constant, possibly symbolic
118  */
119 void
120 scan_num(token * tokp)
121 {
122     get_token(tokp);
123     switch (tokp->kind) {
124     case TOK_IDENT:
125         break;
126     default:
127         error("constant or identifier expected");
128     }
129 }
130
131
132 /*
133  * Peek at the next token
134  */
135 void
136 peek(token * tokp)
137 {
138     get_token(tokp);
139     unget_token(tokp);
140 }
141
142
143 /*
144  * Peek at the next token and scan it if it matches what you expect
145  */
146 int
147 peekscan(tok_kind expect, token * tokp)
148 {
149     peek(tokp);
150     if (tokp->kind == expect) {
151         get_token(tokp);
152         return (1);
153     }
154     return (0);
155 }
156
157
158
159 /*
160  * Get the next token, printing out any directive that are encountered.
161  */
162 void
163 get_token(token * tokp)
164 {
165     int commenting;
166     int verbatim = 0;
167
168     if (pushed) {
169         pushed = 0;
170         *tokp = lasttok;
171         return;
172     }
173     commenting = 0;
174     for (;;) {
175         if (*where == 0) {
176             for (;;) {
177                 if (!fgets(curline, MAXLINESIZE, fin)) {
178                     tokp->kind = TOK_EOF;
179                     *where = 0;
180                     return;
181                 }
182                 linenum++;
183                 if (verbatim) {
184                     fputs(curline, fout);
185                     break;
186                 }
187                 if (commenting) {
188                     break;
189                 } else if (cppline(curline)) {
190 #if defined(AFS_DARWIN_ENV)
191                     if (strncmp(curline, "#pragma", 7) == 0)
192                         continue;
193 #endif /* defined(AFS_DARWIN_ENV) */
194                     docppline(curline, &linenum, &infilename);
195                 } else if (directive(curline)) {
196                     printdirective(curline);
197                 } else {
198                     break;
199                 }
200             }
201             where = curline;
202         } else if (isspace(*where)) {
203             while (isspace(*where)) {
204                 where++;        /* eat */
205             }
206         } else if (verbatim) {
207             where++;
208             if (verbatimend(where)) {
209                 where++;
210                 verbatim--;
211             }
212         } else if (verbatimstart(where)) {
213             where += 2;
214             verbatim++;
215         } else if (commenting) {
216             where++;
217             if (endcomment(where)) {
218                 where++;
219                 commenting--;
220             }
221         } else if (startcomment(where)) {
222             where += 2;
223             commenting++;
224         } else {
225             break;
226         }
227     }
228
229     /*
230      * 'where' is not whitespace, comment or directive Must be a token!
231      */
232     switch (*where) {
233     case ':':
234         tokp->kind = TOK_COLON;
235         where++;
236         break;
237     case ';':
238         tokp->kind = TOK_SEMICOLON;
239         where++;
240         break;
241     case ',':
242         tokp->kind = TOK_COMMA;
243         where++;
244         break;
245     case '=':
246         tokp->kind = TOK_EQUAL;
247         where++;
248         break;
249     case '*':
250         tokp->kind = TOK_STAR;
251         where++;
252         break;
253     case '[':
254         tokp->kind = TOK_LBRACKET;
255         where++;
256         break;
257     case ']':
258         tokp->kind = TOK_RBRACKET;
259         where++;
260         break;
261     case '{':
262         tokp->kind = TOK_LBRACE;
263         where++;
264         break;
265     case '}':
266         tokp->kind = TOK_RBRACE;
267         where++;
268         break;
269     case '(':
270         tokp->kind = TOK_LPAREN;
271         where++;
272         break;
273     case ')':
274         tokp->kind = TOK_RPAREN;
275         where++;
276         break;
277     case '<':
278         tokp->kind = TOK_LANGLE;
279         where++;
280         break;
281     case '>':
282         tokp->kind = TOK_RANGLE;
283         where++;
284         break;
285
286     case '"':
287         tokp->kind = TOK_STRCONST;
288         findstrconst(&where, &tokp->str);
289         break;
290
291     case '-':
292     case '0':
293     case '1':
294     case '2':
295     case '3':
296     case '4':
297     case '5':
298     case '6':
299     case '7':
300     case '8':
301     case '9':
302         tokp->kind = TOK_IDENT;
303         findconst(&where, &tokp->str);
304         break;
305
306
307     default:
308         if (!(isalpha(*where) || *where == '_')) {
309             char buf[100];
310             char *p;
311
312             s_print(buf, "illegal character in file: ");
313             p = buf + strlen(buf);
314             if (isprint(*where)) {
315                 s_print(p, "%c", *where);
316             } else {
317                 s_print(p, "%d", *where);
318             }
319             error(buf);
320         }
321         findkind(&where, tokp);
322         break;
323     }
324 }
325
326
327 void
328 unget_token(token * tokp)
329 {
330     lasttok = *tokp;
331     pushed = 1;
332 }
333
334
335 static void
336 findstrconst(char **str, char **val)
337 {
338     char *p;
339     int size;
340
341     p = *str;
342     do {
343         p++;
344     } while (*p && *p != '"');
345     if (*p == 0) {
346         error("unterminated string constant");
347     }
348     p++;
349     size = (int)(p - *str);
350     *val = alloc(size + 1);
351     (void)strncpy(*val, *str, size);
352     (*val)[size] = 0;
353     *str = p;
354 }
355
356 static void
357 findconst(char **str, char **val)
358 {
359     char *p;
360     int size;
361
362     p = *str;
363     if (*p == '0' && *(p + 1) == 'x') {
364         p++;
365         do {
366             p++;
367         } while (isxdigit(*p));
368     } else {
369         do {
370             p++;
371         } while (isdigit(*p));
372     }
373     size = (int)(p - *str);
374     *val = alloc(size + 1);
375     (void)strncpy(*val, *str, size);
376     (*val)[size] = 0;
377     *str = p;
378 }
379
380
381
382 static token symbols[] = {
383     {TOK_CONST, "const"},
384     {TOK_UNION, "union"},
385     {TOK_SWITCH, "switch"},
386     {TOK_CASE, "case"},
387     {TOK_DEFAULT, "default"},
388     {TOK_STRUCT, "struct"},
389     {TOK_TYPEDEF, "typedef"},
390     {TOK_ENUM, "enum"},
391     {TOK_OPAQUE, "opaque"},
392     {TOK_BOOL, "bool"},
393     {TOK_VOID, "void"},
394     {TOK_CHAR, "char"},
395     {TOK_INT, "int"},
396     {TOK_UNSIGNED, "unsigned"},
397     {TOK_SHORT, "short"},
398     {TOK_INT32, "afs_int32"},
399     {TOK_FLOAT, "float"},
400     {TOK_DOUBLE, "double"},
401     {TOK_STRING, "string"},
402     {TOK_PACKAGE, "package"},
403     {TOK_PREFIX, "prefix"},
404     {TOK_STATINDEX, "statindex"},
405     {TOK_SPECIAL, "special"},
406     {TOK_STARTINGOPCODE, "startingopcode"},
407     {TOK_CUSTOMIZED, "customized"},
408     {TOK_PROC, "proc"},
409     {TOK_SPLITPREFIX, "splitprefix"},
410     {TOK_SPLIT, "split"},
411     {TOK_MULTI, "multi"},
412     {TOK_IN, "IN"},
413     {TOK_OUT, "OUT"},
414     {TOK_INOUT, "INOUT"},
415     {TOK_AFSUUID, "afsUUID"},
416     {TOK_EOF, "??????"},
417 };
418
419
420 void
421 findkind(char **mark, token * tokp)
422 {
423
424     int len;
425     token *s;
426     char *str;
427
428     str = *mark;
429     for (s = symbols; s->kind != TOK_EOF; s++) {
430         len = (int)strlen(s->str);
431         if (strncmp(str, s->str, len) == 0) {
432             if (!isalnum(str[len]) && str[len] != '_') {
433                 tokp->kind = s->kind;
434                 tokp->str = s->str;
435                 *mark = str + len;
436                 return;
437             }
438         }
439     }
440     tokp->kind = TOK_IDENT;
441     for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
442     tokp->str = alloc(len + 1);
443     (void)strncpy(tokp->str, str, len);
444     tokp->str[len] = 0;
445     *mark = str + len;
446 }
447
448 static int
449 cppline(char *line)
450 {
451     return (line == curline && *line == '#');
452 }
453
454 static int
455 directive(char *line)
456 {
457     return (line == curline && *line == '%');
458 }
459
460 void
461 printdirective(char *line)
462 {
463     f_print(fout, "%s", line + 1);
464 }
465
466 static void
467 docppline(char *line, int *lineno, char **fname)
468 {
469     char *file;
470     int num;
471     char *p;
472
473     line++;
474     while (isspace(*line)) {
475         line++;
476     }
477     num = atoi(line);
478     while (isdigit(*line)) {
479         line++;
480     }
481     while (isspace(*line)) {
482         line++;
483     }
484 #ifdef  AFS_HPUX_ENV
485     if (*line == '\0') {
486         *fname = NULL;
487         *lineno = num - 1;
488         return;
489     }
490 #endif
491     if (*line != '"') {
492 #ifdef  AFS_AIX41_ENV
493         if (!strncmp(line, "line", 4)) {
494             while (*line)
495                 *line++;
496             *fname = NULL;
497             *lineno = num - 1;
498             return;
499         }
500 #endif
501         error("preprocessor error");
502     }
503     line++;
504     p = file = alloc(strlen(line) + 1);
505     while (*line && *line != '"') {
506         *p++ = *line++;
507     }
508     if (*line == 0) {
509         error("preprocessor error");
510     }
511     *p = 0;
512     if (*file == 0) {
513         *fname = NULL;
514         free(file);
515     } else {
516         *fname = file;
517     }
518     *lineno = num - 1;
519 }