Use O_CLOEXEC in tzfile handling
[glibc.git] / sunrpc / rpc_scan.c
blob0a88bafe768bee45f9992704bbc18706176205d5
1 /*
2 * From: @(#)rpc_scan.c 1.11 89/02/22
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_scan.c, Scanner for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <string.h>
40 #include <libintl.h>
41 #include "rpc_scan.h"
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 #include "proto.h"
46 #define startcomment(where) (where[0] == '/' && where[1] == '*')
47 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
49 static int pushed = 0; /* is a token pushed */
50 static token lasttok; /* last token, if pushed */
52 static void unget_token (token * tokp);
53 static void findstrconst (const char **str, const char **val);
54 static void findchrconst (const char **str, const char **val);
55 static void findconst (const char **str, const char **val);
56 static void findkind (const char **mark, token * tokp);
57 static int cppline (const char *line);
58 static int directive (const char *line);
59 static void printdirective (const char *line);
60 static void docppline (const char *line, int *lineno, const char **fname);
63 * scan expecting 1 given token
65 void
66 scan (tok_kind expect, token * tokp)
68 get_token (tokp);
69 if (tokp->kind != expect)
70 expected1 (expect);
74 * scan expecting any of the 2 given tokens
76 void
77 scan2 (tok_kind expect1, tok_kind expect2, token * tokp)
79 get_token (tokp);
80 if (tokp->kind != expect1 && tokp->kind != expect2)
82 expected2 (expect1, expect2);
87 * scan expecting any of the 3 given token
89 void
90 scan3 (tok_kind expect1, tok_kind expect2, tok_kind expect3, token * tokp)
92 get_token (tokp);
93 if (tokp->kind != expect1 && tokp->kind != expect2
94 && tokp->kind != expect3)
96 expected3 (expect1, expect2, expect3);
101 * scan expecting a constant, possibly symbolic
103 void
104 scan_num (token *tokp)
106 get_token (tokp);
107 switch (tokp->kind)
109 case TOK_IDENT:
110 break;
111 default:
112 error (_("constant or identifier expected"));
117 * Peek at the next token
119 void
120 peek (token *tokp)
122 get_token (tokp);
123 unget_token (tokp);
127 * Peek at the next token and scan it if it matches what you expect
130 peekscan (tok_kind expect, token *tokp)
132 peek (tokp);
133 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)
151 pushed = 0;
152 *tokp = lasttok;
153 return;
155 commenting = 0;
156 for (;;)
158 if (*where == 0)
160 for (;;)
162 if (!fgets (curline, MAXLINESIZE, fin))
164 tokp->kind = TOK_EOF;
165 *curline = 0;
166 where = curline;
167 return;
169 linenum++;
170 if (commenting)
172 break;
174 else if (cppline (curline))
176 docppline (curline, &linenum,
177 &infilename);
179 else if (directive (curline))
181 printdirective (curline);
183 else
185 break;
188 where = curline;
190 else if (isspace (*where))
192 while (isspace (*where))
194 where++; /* eat */
197 else if (commenting)
199 for (where++; *where; where++)
201 if (endcomment (where))
203 where++;
204 commenting--;
205 break;
209 else if (startcomment (where))
211 where += 2;
212 commenting++;
214 else
216 break;
221 * 'where' is not whitespace, comment or directive Must be a token!
223 switch (*where)
225 case ':':
226 tokp->kind = TOK_COLON;
227 where++;
228 break;
229 case ';':
230 tokp->kind = TOK_SEMICOLON;
231 where++;
232 break;
233 case ',':
234 tokp->kind = TOK_COMMA;
235 where++;
236 break;
237 case '=':
238 tokp->kind = TOK_EQUAL;
239 where++;
240 break;
241 case '*':
242 tokp->kind = TOK_STAR;
243 where++;
244 break;
245 case '[':
246 tokp->kind = TOK_LBRACKET;
247 where++;
248 break;
249 case ']':
250 tokp->kind = TOK_RBRACKET;
251 where++;
252 break;
253 case '{':
254 tokp->kind = TOK_LBRACE;
255 where++;
256 break;
257 case '}':
258 tokp->kind = TOK_RBRACE;
259 where++;
260 break;
261 case '(':
262 tokp->kind = TOK_LPAREN;
263 where++;
264 break;
265 case ')':
266 tokp->kind = TOK_RPAREN;
267 where++;
268 break;
269 case '<':
270 tokp->kind = TOK_LANGLE;
271 where++;
272 break;
273 case '>':
274 tokp->kind = TOK_RANGLE;
275 where++;
276 break;
278 case '"':
279 tokp->kind = TOK_STRCONST;
280 findstrconst (&where, &tokp->str);
281 break;
282 case '\'':
283 tokp->kind = TOK_CHARCONST;
284 findchrconst (&where, &tokp->str);
285 break;
287 case '-':
288 case '0':
289 case '1':
290 case '2':
291 case '3':
292 case '4':
293 case '5':
294 case '6':
295 case '7':
296 case '8':
297 case '9':
298 tokp->kind = TOK_IDENT;
299 findconst (&where, &tokp->str);
300 break;
302 default:
303 if (!(isalpha (*where) || *where == '_'))
305 char buf[100];
306 char *p;
308 s_print (buf, _("illegal character in file: "));
309 p = buf + strlen (buf);
310 if (isprint (*where))
312 s_print (p, "%c", *where);
314 else
316 s_print (p, "%d", *where);
318 error (buf);
320 findkind (&where, tokp);
321 break;
325 static void
326 unget_token (token * tokp)
328 lasttok = *tokp;
329 pushed = 1;
332 static void
333 findstrconst (const char **str, const char **val)
335 const char *p;
336 char *tmp;
337 int size;
339 p = *str;
342 p++;
344 while (*p && *p != '"');
345 if (*p == 0)
347 error (_("unterminated string constant"));
349 p++;
350 size = p - *str;
351 tmp = alloc (size + 1);
352 strncpy (tmp, *str, size);
353 tmp[size] = 0;
354 *val = tmp;
355 *str = p;
358 static void
359 findchrconst (const char **str, const char **val)
361 const char *p;
362 char *tmp;
363 int size;
365 p = *str;
368 p++;
370 while (*p && *p != '\'');
371 if (*p == 0)
373 error (_("unterminated string constant"));
375 p++;
376 size = p - *str;
377 if (size != 3)
379 error (_("empty char string"));
381 tmp = alloc (size + 1);
382 strncpy (tmp, *str, size);
383 tmp[size] = 0;
384 *val = tmp;
385 *str = p;
388 static void
389 findconst (const char **str, const char **val)
391 const char *p;
392 char *tmp;
393 int size;
395 p = *str;
396 if (*p == '0' && *(p + 1) == 'x')
398 p++;
401 p++;
403 while (isxdigit (*p));
405 else
409 p++;
411 while (isdigit (*p));
413 size = p - *str;
414 tmp = alloc (size + 1);
415 strncpy (tmp, *str, size);
416 tmp[size] = 0;
417 *val = tmp;
418 *str = p;
421 static const token symbols[] =
423 {TOK_CONST, "const"},
424 {TOK_UNION, "union"},
425 {TOK_SWITCH, "switch"},
426 {TOK_CASE, "case"},
427 {TOK_DEFAULT, "default"},
428 {TOK_STRUCT, "struct"},
429 {TOK_TYPEDEF, "typedef"},
430 {TOK_ENUM, "enum"},
431 {TOK_OPAQUE, "opaque"},
432 {TOK_BOOL, "bool"},
433 {TOK_VOID, "void"},
434 {TOK_CHAR, "char"},
435 {TOK_INT, "int"},
436 {TOK_UNSIGNED, "unsigned"},
437 {TOK_SHORT, "short"},
438 {TOK_LONG, "long"},
439 {TOK_HYPER, "hyper"},
440 {TOK_FLOAT, "float"},
441 {TOK_DOUBLE, "double"},
442 {TOK_STRING, "string"},
443 {TOK_PROGRAM, "program"},
444 {TOK_VERSION, "version"},
445 {TOK_EOF, "??????"},
448 static void
449 findkind (const char **mark, token *tokp)
451 int len;
452 const token *s;
453 const char *str;
454 char *tmp;
456 str = *mark;
457 for (s = symbols; s->kind != TOK_EOF; s++)
459 len = strlen (s->str);
460 if (strncmp (str, s->str, len) == 0)
462 if (!isalnum (str[len]) && str[len] != '_')
464 tokp->kind = s->kind;
465 tokp->str = s->str;
466 *mark = str + len;
467 return;
471 tokp->kind = TOK_IDENT;
472 for (len = 0; isalnum (str[len]) || str[len] == '_'; len++);
473 tmp = alloc (len + 1);
474 strncpy (tmp, str, len);
475 tmp[len] = 0;
476 tokp->str = tmp;
477 *mark = str + len;
480 static int
481 cppline (const char *line)
483 return line == curline && *line == '#';
486 static int
487 directive (const char *line)
489 return line == curline && *line == '%';
492 static void
493 printdirective (const char *line)
495 f_print (fout, "%s", line + 1);
498 static void
499 docppline (const char *line, int *lineno, const char **fname)
501 char *file;
502 int num;
503 char *p;
505 line++;
506 while (isspace (*line))
508 line++;
510 num = atoi (line);
511 while (isdigit (*line))
513 line++;
515 while (isspace (*line))
517 line++;
519 if (*line != '"')
521 error (_("preprocessor error"));
523 line++;
524 p = file = alloc (strlen (line) + 1);
525 while (*line && *line != '"')
527 *p++ = *line++;
529 if (*line == 0)
531 error (_("preprocessor error"));
533 *p = 0;
534 if (*file == 0)
536 free (file);
537 *fname = NULL;
539 else
541 *fname = file;
543 *lineno = num - 1;