(__opendir): Don't block on FIFOs etc.
[glibc.git] / sunrpc / rpc_scan.c
blobc0293a33161d6b69e86125a8bfe066a92fccae6a
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
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.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From: @(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI
34 char scan_rcsid[] =
35 "$Id$";
38 * rpc_scan.c, Scanner for the RPC protocol compiler
39 * Copyright (C) 1987, Sun Microsystems, Inc.
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include "rpc_scan.h"
45 #include "rpc_parse.h"
46 #include "rpc_util.h"
47 #include "proto.h"
49 #define startcomment(where) (where[0] == '/' && where[1] == '*')
50 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
52 static int pushed = 0; /* is a token pushed */
53 static token lasttok; /* last token, if pushed */
55 static void unget_token(token *tokp);
56 static void findstrconst(const char **str, const char **val);
57 static void findchrconst(const char **str, const char **val);
58 static void findconst(const char **str, const char **val);
59 static void findkind(const char **mark, token *tokp);
60 static int cppline(const char *line);
61 static int directive(const char *line);
62 static void printdirective(const char *line);
63 static void docppline(const char *line, int *lineno, const char **fname);
66 * scan expecting 1 given token
68 void
69 scan(tok_kind expect, token *tokp)
71 get_token(tokp);
72 if (tokp->kind != expect) {
73 expected1(expect);
78 * scan expecting any of the 2 given tokens
80 void
81 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
83 get_token(tokp);
84 if (tokp->kind != expect1 && tokp->kind != expect2) {
85 expected2(expect1, expect2);
90 * scan expecting any of the 3 given token
92 void
93 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
95 get_token(tokp);
96 if (tokp->kind != expect1 && tokp->kind != expect2
97 && tokp->kind != expect3) {
98 expected3(expect1, expect2, expect3);
103 * scan expecting a constant, possibly symbolic
105 void
106 scan_num(token *tokp)
108 get_token(tokp);
109 switch (tokp->kind) {
110 case TOK_IDENT:
111 break;
112 default:
113 error("constant or identifier expected");
118 * Peek at the next token
120 void
121 peek(token *tokp)
123 get_token(tokp);
124 unget_token(tokp);
128 * Peek at the next token and scan it if it matches what you expect
131 peekscan(tok_kind expect, token *tokp)
133 peek(tokp);
134 if (tokp->kind == expect) {
135 get_token(tokp);
136 return (1);
138 return (0);
142 * Get the next token, printing out any directive that are encountered.
144 void
145 get_token(token *tokp)
147 int commenting;
149 if (pushed) {
150 pushed = 0;
151 *tokp = lasttok;
152 return;
154 commenting = 0;
155 for (;;) {
156 if (*where == 0) {
157 for (;;) {
158 if (!fgets(curline, MAXLINESIZE, fin)) {
159 tokp->kind = TOK_EOF;
160 *curline = 0;
161 where = curline;
162 return;
164 linenum++;
165 if (commenting) {
166 break;
167 } else if (cppline(curline)) {
168 docppline(curline, &linenum,
169 &infilename);
170 } else if (directive(curline)) {
171 printdirective(curline);
172 } else {
173 break;
176 where = curline;
177 } else if (isspace(*where)) {
178 while (isspace(*where)) {
179 where++; /* eat */
181 } else if (commenting) {
182 for (where++; *where; where++) {
183 if (endcomment(where)) {
184 where++;
185 commenting--;
186 break;
189 } else if (startcomment(where)) {
190 where += 2;
191 commenting++;
192 } else {
193 break;
198 * 'where' is not whitespace, comment or directive Must be a token!
200 switch (*where) {
201 case ':':
202 tokp->kind = TOK_COLON;
203 where++;
204 break;
205 case ';':
206 tokp->kind = TOK_SEMICOLON;
207 where++;
208 break;
209 case ',':
210 tokp->kind = TOK_COMMA;
211 where++;
212 break;
213 case '=':
214 tokp->kind = TOK_EQUAL;
215 where++;
216 break;
217 case '*':
218 tokp->kind = TOK_STAR;
219 where++;
220 break;
221 case '[':
222 tokp->kind = TOK_LBRACKET;
223 where++;
224 break;
225 case ']':
226 tokp->kind = TOK_RBRACKET;
227 where++;
228 break;
229 case '{':
230 tokp->kind = TOK_LBRACE;
231 where++;
232 break;
233 case '}':
234 tokp->kind = TOK_RBRACE;
235 where++;
236 break;
237 case '(':
238 tokp->kind = TOK_LPAREN;
239 where++;
240 break;
241 case ')':
242 tokp->kind = TOK_RPAREN;
243 where++;
244 break;
245 case '<':
246 tokp->kind = TOK_LANGLE;
247 where++;
248 break;
249 case '>':
250 tokp->kind = TOK_RANGLE;
251 where++;
252 break;
254 case '"':
255 tokp->kind = TOK_STRCONST;
256 findstrconst(&where, &tokp->str);
257 break;
258 case '\'':
259 tokp->kind = TOK_CHARCONST;
260 findchrconst(&where, &tokp->str);
261 break;
263 case '-':
264 case '0':
265 case '1':
266 case '2':
267 case '3':
268 case '4':
269 case '5':
270 case '6':
271 case '7':
272 case '8':
273 case '9':
274 tokp->kind = TOK_IDENT;
275 findconst(&where, &tokp->str);
276 break;
278 default:
279 if (!(isalpha(*where) || *where == '_')) {
280 char buf[100];
281 char *p;
283 s_print(buf, "illegal character in file: ");
284 p = buf + strlen(buf);
285 if (isprint(*where)) {
286 s_print(p, "%c", *where);
287 } else {
288 s_print(p, "%d", *where);
290 error(buf);
292 findkind(&where, tokp);
293 break;
297 static void
298 unget_token(token *tokp)
300 lasttok = *tokp;
301 pushed = 1;
304 static void
305 findstrconst(const char **str, const char **val)
307 const char *p;
308 char *tmp;
309 int size;
311 p = *str;
312 do {
313 p++;
314 } while (*p && *p != '"');
315 if (*p == 0) {
316 error("unterminated string constant");
318 p++;
319 size = p - *str;
320 tmp = alloc(size + 1);
321 strncpy(tmp, *str, size);
322 tmp[size] = 0;
323 *val = tmp;
324 *str = p;
327 static void
328 findchrconst(const char **str, const char **val)
330 const char *p;
331 char *tmp;
332 int size;
334 p = *str;
335 do {
336 p++;
337 } while (*p && *p != '\'');
338 if (*p == 0) {
339 error("unterminated string constant");
341 p++;
342 size = p - *str;
343 if (size != 3) {
344 error("empty char string");
346 tmp = alloc(size + 1);
347 strncpy(tmp, *str, size);
348 tmp[size] = 0;
349 *val = tmp;
350 *str = p;
353 static void
354 findconst(const char **str, const char **val)
356 const char *p;
357 char *tmp;
358 int size;
360 p = *str;
361 if (*p == '0' && *(p + 1) == 'x') {
362 p++;
363 do {
364 p++;
365 } while (isxdigit(*p));
366 } else {
367 do {
368 p++;
369 } while (isdigit(*p));
371 size = p - *str;
372 tmp = alloc(size + 1);
373 strncpy(tmp, *str, size);
374 tmp[size] = 0;
375 *val = tmp;
376 *str = p;
379 static token symbols[] = {
380 {TOK_CONST, "const"},
381 {TOK_UNION, "union"},
382 {TOK_SWITCH, "switch"},
383 {TOK_CASE, "case"},
384 {TOK_DEFAULT, "default"},
385 {TOK_STRUCT, "struct"},
386 {TOK_TYPEDEF, "typedef"},
387 {TOK_ENUM, "enum"},
388 {TOK_OPAQUE, "opaque"},
389 {TOK_BOOL, "bool"},
390 {TOK_VOID, "void"},
391 {TOK_CHAR, "char"},
392 {TOK_INT, "int"},
393 {TOK_UNSIGNED, "unsigned"},
394 {TOK_SHORT, "short"},
395 {TOK_LONG, "long"},
396 {TOK_FLOAT, "float"},
397 {TOK_DOUBLE, "double"},
398 {TOK_STRING, "string"},
399 {TOK_PROGRAM, "program"},
400 {TOK_VERSION, "version"},
401 {TOK_EOF, "??????"},
404 static void
405 findkind(const char **mark, token *tokp)
407 int len;
408 token *s;
409 const char *str;
410 char *tmp;
412 str = *mark;
413 for (s = symbols; s->kind != TOK_EOF; s++) {
414 len = strlen(s->str);
415 if (strncmp(str, s->str, len) == 0) {
416 if (!isalnum(str[len]) && str[len] != '_') {
417 tokp->kind = s->kind;
418 tokp->str = s->str;
419 *mark = str + len;
420 return;
424 tokp->kind = TOK_IDENT;
425 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
426 tmp = alloc(len + 1);
427 strncpy(tmp, str, len);
428 tmp[len] = 0;
429 tokp->str = tmp;
430 *mark = str + len;
433 static int
434 cppline(const char *line)
436 return (line == curline && *line == '#');
439 static int
440 directive(const char *line)
442 return (line == curline && *line == '%');
445 static void
446 printdirective(const char *line)
448 f_print(fout, "%s", line + 1);
451 static void
452 docppline(const char *line, int *lineno, const char **fname)
454 char *file;
455 int num;
456 char *p;
458 line++;
459 while (isspace(*line)) {
460 line++;
462 num = atoi(line);
463 while (isdigit(*line)) {
464 line++;
466 while (isspace(*line)) {
467 line++;
469 if (*line != '"') {
470 error("preprocessor error");
472 line++;
473 p = file = alloc(strlen(line) + 1);
474 while (*line && *line != '"') {
475 *p++ = *line++;
477 if (*line == 0) {
478 error("preprocessor error");
480 *p = 0;
481 if (*file == 0) {
482 *fname = NULL;
483 } else {
484 *fname = file;
486 *lineno = num - 1;