PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.bin/awk/lex.c

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