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