Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly into...
[dragonfly.git] / usr.bin / rpcgen / rpc_scan.c
blob573b5468a06d13ce39a3183f96e9e19b760e4c42
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.
8 *
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
29 * $FreeBSD: src/usr.bin/rpcgen/rpc_scan.c,v 1.4.8.1 2001/03/04 08:59:50 kris Exp $
30 * $DragonFly: src/usr.bin/rpcgen/rpc_scan.c,v 1.5 2008/10/16 01:52:33 swildner Exp $
32 * @(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI
35 #ident "@(#)rpc_scan.c 1.13 93/07/05 SMI"
38 * rpc_scan.c, Scanner for the RPC protocol compiler
39 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <stdio.h>
46 #include <ctype.h>
47 #include <string.h>
48 #include "rpc_scan.h"
49 #include "rpc_parse.h"
50 #include "rpc_util.h"
52 #define startcomment(where) (where[0] == '/' && where[1] == '*')
53 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
55 static int pushed = 0; /* is a token pushed */
56 static token lasttok; /* last token, if pushed */
58 static void unget_token(token *);
59 static void findstrconst(char **, char **);
60 static void findchrconst(char **, char **);
61 static void findconst(char **, char **);
62 static void findkind(char **, token *);
63 static int cppline(char *);
64 static int directive(char *);
65 static void printdirective(char *);
66 static void docppline(char *, int *, char **);
69 * scan expecting 1 given token
71 void
72 scan(tok_kind expect, token *tokp)
74 get_token(tokp);
75 if (tokp->kind != expect)
76 expected1(expect);
80 * scan expecting any of the 2 given tokens
82 void
83 scan2(tok_kind expect1, tok_kind expect2, token *tokp)
85 get_token(tokp);
86 if (tokp->kind != expect1 && tokp->kind != expect2)
87 expected2(expect1, expect2);
91 * scan expecting any of the 3 given token
93 void
94 scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
96 get_token(tokp);
97 if (tokp->kind != expect1 && tokp->kind != expect2 &&
98 tokp->kind != expect3)
99 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;
148 int stat = 0;
150 if (pushed) {
151 pushed = 0;
152 *tokp = lasttok;
153 return;
155 commenting = 0;
156 for (;;) {
157 if (*where == 0) {
158 for (;;) {
159 if (!fgets(curline, MAXLINESIZE, fin)) {
160 tokp->kind = TOK_EOF;
162 * now check if cpp returned
163 * non NULL value
165 waitpid(childpid, &stat, WUNTRACED);
166 if (stat > 0) {
168 * Set return value from rpcgen
170 nonfatalerrors = stat >> 8;
172 *where = 0;
173 return;
175 linenum++;
176 if (commenting)
177 break;
178 else if (cppline(curline))
179 docppline(curline, &linenum,
180 &infilename);
181 else if (directive(curline))
182 printdirective(curline);
183 else
184 break;
186 where = curline;
187 } else if (isspace(*where)) {
188 while (isspace(*where))
189 where++; /* eat */
190 } else if (commenting) {
191 for (where++; *where; where++) {
192 if (endcomment(where)) {
193 where++;
194 commenting--;
195 break;
198 } else if (startcomment(where)) {
199 where += 2;
200 commenting++;
201 } else {
202 break;
207 * 'where' is not whitespace, comment or directive Must be a token!
209 switch (*where) {
210 case ':':
211 tokp->kind = TOK_COLON;
212 where++;
213 break;
214 case ';':
215 tokp->kind = TOK_SEMICOLON;
216 where++;
217 break;
218 case ',':
219 tokp->kind = TOK_COMMA;
220 where++;
221 break;
222 case '=':
223 tokp->kind = TOK_EQUAL;
224 where++;
225 break;
226 case '*':
227 tokp->kind = TOK_STAR;
228 where++;
229 break;
230 case '[':
231 tokp->kind = TOK_LBRACKET;
232 where++;
233 break;
234 case ']':
235 tokp->kind = TOK_RBRACKET;
236 where++;
237 break;
238 case '{':
239 tokp->kind = TOK_LBRACE;
240 where++;
241 break;
242 case '}':
243 tokp->kind = TOK_RBRACE;
244 where++;
245 break;
246 case '(':
247 tokp->kind = TOK_LPAREN;
248 where++;
249 break;
250 case ')':
251 tokp->kind = TOK_RPAREN;
252 where++;
253 break;
254 case '<':
255 tokp->kind = TOK_LANGLE;
256 where++;
257 break;
258 case '>':
259 tokp->kind = TOK_RANGLE;
260 where++;
261 break;
263 case '"':
264 tokp->kind = TOK_STRCONST;
265 findstrconst(&where, &tokp->str);
266 break;
267 case '\'':
268 tokp->kind = TOK_CHARCONST;
269 findchrconst(&where, &tokp->str);
270 break;
272 case '-':
273 case '0':
274 case '1':
275 case '2':
276 case '3':
277 case '4':
278 case '5':
279 case '6':
280 case '7':
281 case '8':
282 case '9':
283 tokp->kind = TOK_IDENT;
284 findconst(&where, &tokp->str);
285 break;
287 default:
288 if (!(isalpha(*where) || *where == '_')) {
289 char buf[100];
290 char *p;
292 s_print(buf, "illegal character in file: ");
293 p = buf + strlen(buf);
294 if (isprint(*where)) {
295 s_print(p, "%c", *where);
296 } else {
297 s_print(p, "%d", *where);
299 error(buf);
301 findkind(&where, tokp);
302 break;
306 static void
307 unget_token(token *tokp)
309 lasttok = *tokp;
310 pushed = 1;
313 static void
314 findstrconst(char **str, char **val)
316 char *p;
317 int size;
319 p = *str;
320 do {
321 p++;
322 } while (*p && *p != '"');
323 if (*p == 0)
324 error("unterminated string constant");
325 p++;
326 size = p - *str;
327 *val = alloc(size + 1);
328 (void) strncpy(*val, *str, size);
329 (*val)[size] = 0;
330 *str = p;
333 static void
334 findchrconst(char **str, char **val)
336 char *p;
337 int size;
339 p = *str;
340 do {
341 p++;
342 } while (*p && *p != '\'');
343 if (*p == 0)
344 error("unterminated string constant");
345 p++;
346 size = p - *str;
347 if (size != 3)
348 error("empty char string");
349 *val = alloc(size + 1);
350 strncpy(*val, *str, size);
351 (*val)[size] = 0;
352 *str = p;
355 static void
356 findconst(char **str, char **val)
358 char *p;
359 int size;
361 p = *str;
362 if (*p == '0' && *(p + 1) == 'x') {
363 p++;
364 do {
365 p++;
366 } while (isxdigit(*p));
367 } else {
368 do {
369 p++;
370 } while (isdigit(*p));
372 size = p - *str;
373 *val = alloc(size + 1);
374 strncpy(*val, *str, size);
375 (*val)[size] = 0;
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_HYPER, "hyper"},
397 {TOK_FLOAT, "float"},
398 {TOK_DOUBLE, "double"},
399 {TOK_QUAD, "quadruple"},
400 {TOK_STRING, "string"},
401 {TOK_PROGRAM, "program"},
402 {TOK_VERSION, "version"},
403 {TOK_EOF, "??????"},
406 static void
407 findkind(char **mark, token *tokp)
409 int len;
410 token *s;
411 char *str;
413 str = *mark;
414 for (s = symbols; s->kind != TOK_EOF; s++) {
415 len = strlen(s->str);
416 if (strncmp(str, s->str, len) == 0) {
417 if (!isalnum(str[len]) && str[len] != '_') {
418 tokp->kind = s->kind;
419 tokp->str = s->str;
420 *mark = str + len;
421 return;
425 tokp->kind = TOK_IDENT;
426 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
427 tokp->str = alloc(len + 1);
428 strncpy(tokp->str, str, len);
429 tokp->str[len] = 0;
430 *mark = str + len;
433 static int
434 cppline(char *line)
436 return(line == curline && *line == '#');
439 static int
440 directive(char *line)
442 return(line == curline && *line == '%');
445 static void
446 printdirective(char *line)
448 f_print(fout, "%s", line + 1);
451 static void
452 docppline(char *line, int *lineno, char **fname)
454 char *file;
455 int num;
456 char *p;
458 line++;
459 while (isspace(*line))
460 line++;
461 num = atoi(line);
462 while (isdigit(*line))
463 line++;
464 while (isspace(*line))
465 line++;
466 if (*line != '"')
467 error("preprocessor error");
468 line++;
469 p = file = alloc(strlen(line) + 1);
470 while (*line && *line != '"')
471 *p++ = *line++;
472 if (*line == 0)
473 error("preprocessor error");
474 *p = 0;
475 if (*file == 0)
476 *fname = NULL;
477 else
478 *fname = file;
479 *lineno = num - 1;