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.
22 #pragma GCC diagnostic ignored "-Wsign-compare"
32 #include "pathnames.h"
42 static char *strbuf = NULL;
43 static size_t strbuf_sz = 1;
46 static volatile sig_atomic_t skipchars;
48 static void init_strbuf(void);
49 static void add_str(const char *);
50 static int bc_yyinput(char *, int);
53 #define YY_INPUT(buf,retval,max) \
54 (retval = bc_yyinput(buf, max))
58 %option always-interactive
65 %x comment string number
75 <<EOF>> fatal("end of file in comment");
78 \" BEGIN(string); init_strbuf();
80 [^"\n\\\[\]]+ add_str(yytext);
84 \n add_str("\n"); lineno++;
85 \" BEGIN(INITIAL); yylval.str = strbuf; return STRING;
86 <<EOF>> fatal("end of file in string");
102 {DIGIT}+ add_str(yytext);
118 if (strcmp(strbuf, ".") == 0)
128 "break" return BREAK;
129 "continue" return CONTINUE;
130 "define" return DEFINE;
132 "ibase" return IBASE;
136 "length" return LENGTH;
137 "obase" return OBASE;
138 "print" return PRINT;
140 "return" return RETURN;
141 "scale" return SCALE;
143 "while" return WHILE;
148 "%" return REMAINDER;
151 "&&" return BOOL_AND;
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;
166 "^=" yylval.str = "^"; return ASSIGN_OP;
170 ">=" return GREATER_EQ;
171 "!=" return UNEQUALS;
176 ";" return SEMICOLON;
188 /* alloc an extra byte for the type marker */
189 char *p = malloc(yyleng + 2);
192 strlcpy(p, yytext, yyleng + 1);
198 \n lineno++; return NEWLINE;
203 . yyerror("illegal character");
210 if (strbuf == NULL) {
211 strbuf = malloc(strbuf_sz);
219 add_str(const char *str)
223 arglen = strlen(str);
225 if (strlen(strbuf) + arglen + 1 > strbuf_sz) {
229 newsize = strbuf_sz + arglen + 1;
230 p = realloc(strbuf, newsize);
238 strlcat(strbuf, str, strbuf_sz);
245 const char str1[] = "[\n]P\n";
246 const char str2[] = "[^C\n]P\n";
248 const LineInfo *info;
252 write(STDOUT_FILENO, str2, sizeof(str2) - 1);
254 skipchars = info->lastchar - info->buffer;
256 write(STDOUT_FILENO, str1, sizeof(str1) - 1);
261 * Avoid the echo of ^D by the default code of editline and take
262 * into account skipchars to make ^D work when the cursor is at start of
266 bc_eof(EditLine *e, int ch)
268 const struct lineinfo *info = el_line(e);
270 if (info->buffer + skipchars == info->cursor &&
271 info->cursor == info->lastchar)
281 static YY_BUFFER_STATE buf;
283 if (fileindex == 0 && sargc > 0 && strcmp(sargv[0], _PATH_LIBB) == 0) {
284 filename = sargv[fileindex++];
285 yyin = fopen(filename, "r");
288 err(1, "cannot open %s", filename);
291 if (state == 0 && cmdexpr[0] != '\0') {
292 buf = yy_scan_string(cmdexpr);
295 filename = "command line";
297 } else if (state == 1) {
298 yy_delete_buffer(buf);
302 if (yyin != NULL && yyin != stdin)
304 if (fileindex < sargc) {
305 filename = sargv[fileindex++];
306 yyin = fopen(filename, "r");
309 err(1, "cannot open %s", filename);
311 } else if (fileindex == sargc) {
315 signal(SIGINT, abort_line);
316 signal(SIGTSTP, tstpcont);
326 bc_yyinput(char *buf, int maxlen)
331 el_get(el, EL_EDITMODE, &use_el);
333 if (yyin == stdin && interactive && use_el) {
337 if ((bp = el_gets(el, &num)) == NULL || num == 0)
340 sigaddset(&nset, SIGINT);
341 sigprocmask(SIG_BLOCK, &nset, &oset);
342 if (skipchars < num) {
347 sigprocmask(SIG_SETMASK, &oset, NULL);
349 el_push(el, (char *)(uintptr_t)(bp) + maxlen);
352 memcpy(buf, bp, num);
353 history(hist, &he, H_ENTER, bp);
354 el_get(el, EL_EDITMODE, &use_el);
357 for (num = 0; num < maxlen &&
358 (c = getc(yyin)) != EOF && c != '\n'; ++num)
361 buf[num++] = (char) c;
362 if (c == EOF && ferror(yyin))
363 YY_FATAL_ERROR( "input in flex scanner failed" );