Annotation of src/usr.bin/awk/lex.c, Revision 1.9
1.9 ! hugh 1: /* $OpenBSD: lex.c,v 1.8 2004/12/30 01:52:48 millert Exp $ */
1.1 kstailey 2: /****************************************************************
3: Copyright (C) Lucent Technologies 1997
4: All Rights Reserved
5:
6: Permission to use, copy, modify, and distribute this software and
7: its documentation for any purpose and without fee is hereby
8: granted, provided that the above copyright notice appear in all
9: copies and that both that the copyright notice and this
10: permission notice and warranty disclaimer appear in supporting
11: documentation, and that the name Lucent Technologies or any of
12: its entities not be used in advertising or publicity pertaining
13: to distribution of the software without specific, written prior
14: permission.
15:
16: LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18: IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19: SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21: IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23: THIS SOFTWARE.
24: ****************************************************************/
25:
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <ctype.h>
30: #include "awk.h"
31: #include "ytab.h"
32:
33: extern YYSTYPE yylval;
34: extern int infunc;
35:
36: int lineno = 1;
37: int bracecnt = 0;
38: int brackcnt = 0;
39: int parencnt = 0;
40:
41: typedef struct Keyword {
1.6 millert 42: const char *word;
1.1 kstailey 43: int sub;
44: int type;
45: } Keyword;
46:
47: Keyword keywords[] ={ /* keep sorted: binary searched */
48: { "BEGIN", XBEGIN, XBEGIN },
49: { "END", XEND, XEND },
50: { "NF", VARNF, VARNF },
51: { "atan2", FATAN, BLTIN },
52: { "break", BREAK, BREAK },
53: { "close", CLOSE, CLOSE },
54: { "continue", CONTINUE, CONTINUE },
55: { "cos", FCOS, BLTIN },
56: { "delete", DELETE, DELETE },
57: { "do", DO, DO },
58: { "else", ELSE, ELSE },
59: { "exit", EXIT, EXIT },
60: { "exp", FEXP, BLTIN },
61: { "fflush", FFLUSH, BLTIN },
62: { "for", FOR, FOR },
63: { "func", FUNC, FUNC },
64: { "function", FUNC, FUNC },
65: { "getline", GETLINE, GETLINE },
66: { "gsub", GSUB, GSUB },
67: { "if", IF, IF },
68: { "in", IN, IN },
69: { "index", INDEX, INDEX },
70: { "int", FINT, BLTIN },
71: { "length", FLENGTH, BLTIN },
72: { "log", FLOG, BLTIN },
73: { "match", MATCHFCN, MATCHFCN },
74: { "next", NEXT, NEXT },
75: { "nextfile", NEXTFILE, NEXTFILE },
76: { "print", PRINT, PRINT },
77: { "printf", PRINTF, PRINTF },
78: { "rand", FRAND, BLTIN },
79: { "return", RETURN, RETURN },
80: { "sin", FSIN, BLTIN },
81: { "split", SPLIT, SPLIT },
82: { "sprintf", SPRINTF, SPRINTF },
83: { "sqrt", FSQRT, BLTIN },
84: { "srand", FSRAND, BLTIN },
85: { "sub", SUB, SUB },
86: { "substr", SUBSTR, SUBSTR },
87: { "system", FSYSTEM, BLTIN },
88: { "tolower", FTOLOWER, BLTIN },
89: { "toupper", FTOUPPER, BLTIN },
90: { "while", WHILE, WHILE },
91: };
92:
93: #define DEBUG
94: #ifdef DEBUG
95: #define RET(x) { if(dbg)printf("lex %s\n", tokname(x)); return(x); }
96: #else
97: #define RET(x) return(x)
98: #endif
1.7 deraadt 99:
100: int peek(void);
101: int gettok(char **, int *);
102: int binsearch(char *, Keyword *, int);
1.1 kstailey 103:
1.2 millert 104: int peek(void)
1.1 kstailey 105: {
106: int c = input();
107: unput(c);
108: return c;
109: }
110:
111: int gettok(char **pbuf, int *psz) /* get next input token */
112: {
1.6 millert 113: int c, retc;
1.1 kstailey 114: char *buf = *pbuf;
115: int sz = *psz;
116: char *bp = buf;
117:
118: c = input();
119: if (c == 0)
120: return 0;
121: buf[0] = c;
122: buf[1] = 0;
123: if (!isalnum(c) && c != '.' && c != '_')
124: return c;
125:
126: *bp++ = c;
127: if (isalpha(c) || c == '_') { /* it's a varname */
128: for ( ; (c = input()) != 0; ) {
129: if (bp-buf >= sz)
130: if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, 0))
1.4 millert 131: FATAL( "out of space for name %.10s...", buf );
1.1 kstailey 132: if (isalnum(c) || c == '_')
133: *bp++ = c;
134: else {
135: *bp = 0;
136: unput(c);
137: break;
138: }
139: }
1.4 millert 140: *bp = 0;
1.6 millert 141: retc = 'a'; /* alphanumeric */
1.1 kstailey 142: } else { /* it's a number */
143: char *rem;
144: /* read input until can't be a number */
145: for ( ; (c = input()) != 0; ) {
146: if (bp-buf >= sz)
147: if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, 0))
1.4 millert 148: FATAL( "out of space for number %.10s...", buf );
1.1 kstailey 149: if (isdigit(c) || c == 'e' || c == 'E'
150: || c == '.' || c == '+' || c == '-')
151: *bp++ = c;
152: else {
153: unput(c);
154: break;
155: }
156: }
1.2 millert 157: *bp = 0;
1.1 kstailey 158: strtod(buf, &rem); /* parse the number */
159: unputstr(rem); /* put rest back for later */
1.8 millert 160: /* printf("unputstr [%s], buf [%s]\n", rem, buf); */
1.6 millert 161: if (rem == buf) { /* it wasn't a valid number at all */
162: buf[1] = 0; /* so return one character as token */
163: retc = buf[0]; /* character is its own type */
164: } else { /* some prefix was a number */
165: rem[0] = 0; /* so truncate where failure started */
166: retc = '0'; /* number */
167: }
1.1 kstailey 168: }
169: *pbuf = buf;
170: *psz = sz;
1.6 millert 171: return retc;
1.1 kstailey 172: }
173:
174: int word(char *);
175: int string(void);
176: int regexpr(void);
177: int sc = 0; /* 1 => return a } right now */
178: int reg = 0; /* 1 => return a REGEXPR now */
179:
1.3 millert 180: int yylex(void)
1.1 kstailey 181: {
1.3 millert 182: int c;
1.1 kstailey 183: static char *buf = 0;
184: static int bufsize = 500;
185:
186: if (buf == 0 && (buf = (char *) malloc(bufsize)) == NULL)
1.4 millert 187: FATAL( "out of space in yylex" );
1.1 kstailey 188: if (sc) {
189: sc = 0;
190: RET('}');
191: }
192: if (reg) {
193: reg = 0;
194: return regexpr();
195: }
1.8 millert 196: /* printf("top\n"); */
1.1 kstailey 197: for (;;) {
198: c = gettok(&buf, &bufsize);
1.8 millert 199: /* printf("gettok [%s]\n", buf); */
1.1 kstailey 200: if (c == 0)
201: return 0;
202: if (isalpha(c) || c == '_')
203: return word(buf);
1.6 millert 204: if (isdigit(c)) {
1.1 kstailey 205: yylval.cp = setsymtab(buf, tostring(buf), atof(buf), CON|NUM, symtab);
206: /* should this also have STR set? */
207: RET(NUMBER);
208: }
209:
210: yylval.i = c;
211: switch (c) {
212: case '\n': /* {EOL} */
213: RET(NL);
214: case '\r': /* assume \n is coming */
215: case ' ': /* {WS}+ */
216: case '\t':
217: break;
218: case '#': /* #.* strip comments */
219: while ((c = input()) != '\n' && c != 0)
220: ;
221: unput(c);
222: break;
223: case ';':
224: RET(';');
225: case '\\':
226: if (peek() == '\n') {
1.3 millert 227: input();
1.1 kstailey 228: } else if (peek() == '\r') {
229: input(); input(); /* \n */
230: lineno++;
231: } else {
232: RET(c);
233: }
234: break;
235: case '&':
236: if (peek() == '&') {
237: input(); RET(AND);
238: } else
239: RET('&');
240: case '|':
241: if (peek() == '|') {
242: input(); RET(BOR);
243: } else
244: RET('|');
245: case '!':
246: if (peek() == '=') {
247: input(); yylval.i = NE; RET(NE);
248: } else if (peek() == '~') {
249: input(); yylval.i = NOTMATCH; RET(MATCHOP);
250: } else
251: RET(NOT);
252: case '~':
253: yylval.i = MATCH;
254: RET(MATCHOP);
255: case '<':
256: if (peek() == '=') {
257: input(); yylval.i = LE; RET(LE);
258: } else {
259: yylval.i = LT; RET(LT);
260: }
261: case '=':
262: if (peek() == '=') {
263: input(); yylval.i = EQ; RET(EQ);
264: } else {
265: yylval.i = ASSIGN; RET(ASGNOP);
266: }
267: case '>':
268: if (peek() == '=') {
269: input(); yylval.i = GE; RET(GE);
270: } else if (peek() == '>') {
271: input(); yylval.i = APPEND; RET(APPEND);
272: } else {
273: yylval.i = GT; RET(GT);
274: }
275: case '+':
276: if (peek() == '+') {
277: input(); yylval.i = INCR; RET(INCR);
278: } else if (peek() == '=') {
279: input(); yylval.i = ADDEQ; RET(ASGNOP);
280: } else
281: RET('+');
282: case '-':
283: if (peek() == '-') {
284: input(); yylval.i = DECR; RET(DECR);
285: } else if (peek() == '=') {
286: input(); yylval.i = SUBEQ; RET(ASGNOP);
287: } else
288: RET('-');
289: case '*':
290: if (peek() == '=') { /* *= */
291: input(); yylval.i = MULTEQ; RET(ASGNOP);
292: } else if (peek() == '*') { /* ** or **= */
293: input(); /* eat 2nd * */
294: if (peek() == '=') {
295: input(); yylval.i = POWEQ; RET(ASGNOP);
296: } else {
297: RET(POWER);
298: }
299: } else
300: RET('*');
301: case '/':
1.3 millert 302: RET('/');
1.1 kstailey 303: case '%':
304: if (peek() == '=') {
305: input(); yylval.i = MODEQ; RET(ASGNOP);
306: } else
307: RET('%');
308: case '^':
309: if (peek() == '=') {
310: input(); yylval.i = POWEQ; RET(ASGNOP);
311: } else
312: RET(POWER);
1.5 millert 313:
1.1 kstailey 314: case '$':
315: /* BUG: awkward, if not wrong */
316: c = gettok(&buf, &bufsize);
1.5 millert 317: if (isalpha(c)) {
1.1 kstailey 318: if (strcmp(buf, "NF") == 0) { /* very special */
319: unputstr("(NF)");
1.5 millert 320: RET(INDIRECT);
321: }
322: c = peek();
323: if (c == '(' || c == '[' || (infunc && isarg(buf) >= 0)) {
324: unputstr(buf);
1.1 kstailey 325: RET(INDIRECT);
326: }
327: yylval.cp = setsymtab(buf, "", 0.0, STR|NUM, symtab);
328: RET(IVAR);
1.6 millert 329: } else if (c == 0) { /* */
330: SYNTAX( "unexpected end of input after $" );
331: RET(';');
1.1 kstailey 332: } else {
333: unputstr(buf);
334: RET(INDIRECT);
335: }
336:
337: case '}':
338: if (--bracecnt < 0)
1.4 millert 339: SYNTAX( "extra }" );
1.1 kstailey 340: sc = 1;
341: RET(';');
342: case ']':
343: if (--brackcnt < 0)
1.4 millert 344: SYNTAX( "extra ]" );
1.1 kstailey 345: RET(']');
346: case ')':
347: if (--parencnt < 0)
1.4 millert 348: SYNTAX( "extra )" );
1.1 kstailey 349: RET(')');
350: case '{':
351: bracecnt++;
352: RET('{');
353: case '[':
354: brackcnt++;
355: RET('[');
356: case '(':
357: parencnt++;
358: RET('(');
359:
360: case '"':
361: return string(); /* BUG: should be like tran.c ? */
362:
363: default:
364: RET(c);
365: }
366: }
367: }
368:
1.3 millert 369: int string(void)
1.1 kstailey 370: {
371: int c, n;
372: char *s, *bp;
373: static char *buf = 0;
374: static int bufsz = 500;
375:
376: if (buf == 0 && (buf = (char *) malloc(bufsz)) == NULL)
1.4 millert 377: FATAL("out of space for strings");
1.1 kstailey 378: for (bp = buf; (c = input()) != '"'; ) {
379: if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, 0))
1.4 millert 380: FATAL("out of space for string %.10s...", buf);
1.1 kstailey 381: switch (c) {
382: case '\n':
383: case '\r':
384: case 0:
1.4 millert 385: SYNTAX( "non-terminated string %.10s...", buf );
1.1 kstailey 386: lineno++;
1.6 millert 387: if (c == 0) /* hopeless */
388: FATAL( "giving up" );
1.1 kstailey 389: break;
390: case '\\':
391: c = input();
392: switch (c) {
393: case '"': *bp++ = '"'; break;
394: case 'n': *bp++ = '\n'; break;
395: case 't': *bp++ = '\t'; break;
396: case 'f': *bp++ = '\f'; break;
397: case 'r': *bp++ = '\r'; break;
398: case 'b': *bp++ = '\b'; break;
399: case 'v': *bp++ = '\v'; break;
1.3 millert 400: case 'a': *bp++ = '\007'; break;
1.1 kstailey 401: case '\\': *bp++ = '\\'; break;
402:
403: case '0': case '1': case '2': /* octal: \d \dd \ddd */
404: case '3': case '4': case '5': case '6': case '7':
405: n = c - '0';
406: if ((c = peek()) >= '0' && c < '8') {
407: n = 8 * n + input() - '0';
408: if ((c = peek()) >= '0' && c < '8')
409: n = 8 * n + input() - '0';
410: }
411: *bp++ = n;
412: break;
413:
414: case 'x': /* hex \x0-9a-fA-F + */
415: { char xbuf[100], *px;
416: for (px = xbuf; (c = input()) != 0 && px-xbuf < 100-2; ) {
417: if (isdigit(c)
418: || (c >= 'a' && c <= 'f')
419: || (c >= 'A' && c <= 'F'))
420: *px++ = c;
421: else
422: break;
423: }
424: *px = 0;
425: unput(c);
426: sscanf(xbuf, "%x", &n);
427: *bp++ = n;
428: break;
429: }
430:
431: default:
432: *bp++ = c;
433: break;
434: }
435: break;
436: default:
437: *bp++ = c;
438: break;
439: }
440: }
441: *bp = 0;
442: s = tostring(buf);
443: *bp++ = ' '; *bp++ = 0;
444: yylval.cp = setsymtab(buf, s, 0.0, CON|STR|DONTFREE, symtab);
445: RET(STRING);
446: }
447:
448:
449: int binsearch(char *w, Keyword *kp, int n)
450: {
451: int cond, low, mid, high;
452:
453: low = 0;
454: high = n - 1;
455: while (low <= high) {
456: mid = (low + high) / 2;
457: if ((cond = strcmp(w, kp[mid].word)) < 0)
458: high = mid - 1;
459: else if (cond > 0)
460: low = mid + 1;
461: else
462: return mid;
463: }
464: return -1;
465: }
466:
467: int word(char *w)
468: {
469: Keyword *kp;
470: int c, n;
471:
472: n = binsearch(w, keywords, sizeof(keywords)/sizeof(keywords[0]));
473: kp = keywords + n;
474: if (n != -1) { /* found in table */
475: yylval.i = kp->sub;
476: switch (kp->type) { /* special handling */
477: case FSYSTEM:
478: if (safe)
1.4 millert 479: SYNTAX( "system is unsafe" );
1.1 kstailey 480: RET(kp->type);
481: case FUNC:
482: if (infunc)
1.4 millert 483: SYNTAX( "illegal nested function" );
1.1 kstailey 484: RET(kp->type);
485: case RETURN:
486: if (!infunc)
1.4 millert 487: SYNTAX( "return not in function" );
1.1 kstailey 488: RET(kp->type);
489: case VARNF:
490: yylval.cp = setsymtab("NF", "", 0.0, NUM, symtab);
491: RET(VARNF);
492: default:
493: RET(kp->type);
494: }
495: }
496: c = peek(); /* look for '(' */
497: if (c != '(' && infunc && (n=isarg(w)) >= 0) {
498: yylval.i = n;
499: RET(ARG);
500: } else {
501: yylval.cp = setsymtab(w, "", 0.0, STR|NUM|DONTFREE, symtab);
502: if (c == '(') {
503: RET(CALL);
504: } else {
505: RET(VAR);
506: }
507: }
508: }
509:
1.6 millert 510: void startreg(void) /* next call to yylex will return a regular expression */
1.1 kstailey 511: {
512: reg = 1;
513: }
514:
1.3 millert 515: int regexpr(void)
1.1 kstailey 516: {
1.9 ! hugh 517: int c, openclass = 0;
1.1 kstailey 518: static char *buf = 0;
519: static int bufsz = 500;
520: char *bp;
521:
522: if (buf == 0 && (buf = (char *) malloc(bufsz)) == NULL)
1.4 millert 523: FATAL("out of space for rex expr");
1.1 kstailey 524: bp = buf;
1.9 ! hugh 525: for ( ; ((c = input()) != '/' || openclass == 1) && c != 0; ) {
1.1 kstailey 526: if (!adjbuf(&buf, &bufsz, bp-buf+3, 500, &bp, 0))
1.4 millert 527: FATAL("out of space for reg expr %.10s...", buf);
1.1 kstailey 528: if (c == '\n') {
1.4 millert 529: SYNTAX( "newline in regular expression %.10s...", buf );
1.1 kstailey 530: unput('\n');
531: break;
532: } else if (c == '\\') {
533: *bp++ = '\\';
534: *bp++ = input();
535: } else {
1.9 ! hugh 536: if (c == '[')
! 537: openclass = 1;
! 538: else if (c == ']')
! 539: openclass = 0;
1.1 kstailey 540: *bp++ = c;
541: }
542: }
543: *bp = 0;
1.8 millert 544: if (c == 0)
545: SYNTAX("non-terminated regular expression %.10s...", buf);
1.1 kstailey 546: yylval.s = tostring(buf);
547: unput('/');
548: RET(REGEXPR);
549: }
550:
551: /* low-level lexical stuff, sort of inherited from lex */
552:
553: char ebuf[300];
554: char *ep = ebuf;
555: char yysbuf[100]; /* pushback buffer */
556: char *yysptr = yysbuf;
557: FILE *yyin = 0;
558:
559: int input(void) /* get next lexical input character */
560: {
561: int c;
562: extern char *lexprog;
563:
564: if (yysptr > yysbuf)
1.8 millert 565: c = (uschar)*--yysptr;
1.1 kstailey 566: else if (lexprog != NULL) { /* awk '...' */
1.8 millert 567: if ((c = (uschar)*lexprog) != 0)
1.1 kstailey 568: lexprog++;
569: } else /* awk -f ... */
570: c = pgetc();
571: if (c == '\n')
572: lineno++;
573: else if (c == EOF)
574: c = 0;
575: if (ep >= ebuf + sizeof ebuf)
576: ep = ebuf;
577: return *ep++ = c;
578: }
579:
580: void unput(int c) /* put lexical character back on input */
581: {
582: if (c == '\n')
583: lineno--;
584: if (yysptr >= yysbuf + sizeof(yysbuf))
1.4 millert 585: FATAL("pushed back too much: %.20s...", yysbuf);
1.1 kstailey 586: *yysptr++ = c;
587: if (--ep < ebuf)
588: ep = ebuf + sizeof(ebuf) - 1;
589: }
590:
1.6 millert 591: void unputstr(const char *s) /* put a string back on input */
1.1 kstailey 592: {
593: int i;
594:
595: for (i = strlen(s)-1; i >= 0; i--)
596: unput(s[i]);
597: }