3 * $OpenBSD: scan.l,v 1.21 2006/03/18 20:44:43 otto Exp $
7 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include "pathnames.h"
41 static char *strbuf = NULL;
42 static size_t strbuf_sz = 1;
45 static volatile sig_atomic_t skipchars;
47 static void init_strbuf(void);
48 static void add_str(const char *);
49 static int bc_yyinput(char *, int);
52 #define YY_INPUT(buf,retval,max) \
53 (retval = bc_yyinput(buf, max))
57 %option always-interactive
64 %x comment string number
74 <<EOF>> fatal("end of file in comment");
77 \" BEGIN(string); init_strbuf();
79 [^"\n\\\[\]]+ add_str(yytext);
83 \n add_str("\n"); lineno++;
84 \" BEGIN(INITIAL); yylval.str = strbuf; return STRING;
85 <<EOF>> fatal("end of file in string");
101 {DIGIT}+ add_str(yytext);
117 if (strcmp(strbuf, ".") == 0)
127 "break" return BREAK;
128 "continue" return CONTINUE;
129 "define" return DEFINE;
131 "ibase" return IBASE;
135 "length" return LENGTH;
136 "obase" return OBASE;
137 "print" return PRINT;
139 "return" return RETURN;
140 "scale" return SCALE;
142 "while" return WHILE;
147 "%" return REMAINDER;
150 "&&" return BOOL_AND;
159 "=" yylval.str = ""; return ASSIGN_OP;
160 "+=" yylval.str = "+"; return ASSIGN_OP;
161 "-=" yylval.str = "-"; return ASSIGN_OP;
162 "*=" yylval.str = "*"; return ASSIGN_OP;
163 "/=" yylval.str = "/"; return ASSIGN_OP;
164 "%=" yylval.str = "%"; return ASSIGN_OP;
165 "^=" yylval.str = "^"; return ASSIGN_OP;
169 ">=" return GREATER_EQ;
170 "!=" return UNEQUALS;
175 ";" return SEMICOLON;
187 /* alloc an extra byte for the type marker */
188 char *p = malloc(yyleng + 2);
191 strlcpy(p, yytext, yyleng + 1);
197 \n lineno++; return NEWLINE;
202 . yyerror("illegal character");
209 if (strbuf == NULL) {
210 strbuf = malloc(strbuf_sz);
218 add_str(const char *str)
222 arglen = strlen(str);
224 if (strlen(strbuf) + arglen + 1 > strbuf_sz) {
228 newsize = strbuf_sz + arglen + 1;
229 p = realloc(strbuf, newsize);
237 strlcat(strbuf, str, strbuf_sz);
244 const char str1[] = "[\n]P\n";
245 const char str2[] = "[^C\n]P\n";
247 const LineInfo *info;
251 write(STDOUT_FILENO, str2, sizeof(str2) - 1);
253 skipchars = info->lastchar - info->buffer;
255 write(STDOUT_FILENO, str1, sizeof(str1) - 1);
260 * Avoid the echo of ^D by the default code of editline and take
261 * into account skipchars to make ^D work when the cursor is at start of
265 bc_eof(EditLine *e, int ch)
267 const struct lineinfo *info = el_line(e);
269 if (info->buffer + skipchars == info->cursor &&
270 info->cursor == info->lastchar)
280 static YY_BUFFER_STATE buf;
282 if (fileindex == 0 && sargc > 0 && strcmp(sargv[0], _PATH_LIBB) == 0) {
283 filename = sargv[fileindex++];
284 yyin = fopen(filename, "r");
287 err(1, "cannot open %s", filename);
290 if (state == 0 && cmdexpr[0] != '\0') {
291 buf = yy_scan_string(cmdexpr);
294 filename = "command line";
296 } else if (state == 1) {
297 yy_delete_buffer(buf);
301 if (yyin != NULL && yyin != stdin)
303 if (fileindex < sargc) {
304 filename = sargv[fileindex++];
305 yyin = fopen(filename, "r");
308 err(1, "cannot open %s", filename);
310 } else if (fileindex == sargc) {
314 signal(SIGINT, abort_line);
315 signal(SIGTSTP, tstpcont);
325 bc_yyinput(char *buf, int maxlen)
330 el_get(el, EL_EDITMODE, &use_el);
332 if (yyin == stdin && interactive && use_el) {
336 if ((bp = el_gets(el, &num)) == NULL || num == 0)
339 sigaddset(&nset, SIGINT);
340 sigprocmask(SIG_BLOCK, &nset, &oset);
341 if (skipchars < num) {
346 sigprocmask(SIG_SETMASK, &oset, NULL);
348 el_push(el, (char *)(uintptr_t)(bp) + maxlen);
351 memcpy(buf, bp, num);
352 history(hist, &he, H_ENTER, bp);
353 el_get(el, EL_EDITMODE, &use_el);
356 for (num = 0; num < maxlen &&
357 (c = getc(yyin)) != EOF && c != '\n'; ++num)
360 buf[num++] = (char) c;
361 if (c == EOF && ferror(yyin))
362 YY_FATAL_ERROR( "input in flex scanner failed" );