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