Home | History | Annotate | Line # | Download | only in director
testlang_conf.l revision 1.25
      1 %{
      2 /*	$NetBSD: testlang_conf.l,v 1.25 2021/02/25 00:50:10 rillig Exp $ 	*/
      3 
      4 /*-
      5  * Copyright 2009 Brett Lymn <blymn (at) NetBSD.org>
      6  * Copyright 2021 Roland Illig <rillig (at) NetBSD.org>
      7  *
      8  * All rights reserved.
      9  *
     10  * This code has been donated to The NetBSD Foundation by the Author.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <curses.h>
     33 #include <ctype.h>
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 #include <sys/param.h>
     38 #include <err.h>
     39 #include "returns.h"
     40 #include "testlang_parse.h"
     41 
     42 #define MAX_INCLUDES 32 /* limit for the number of nested includes */
     43 
     44 int yylex(void);
     45 
     46 extern size_t line;
     47 extern char *cur_file;		/* from director.c */
     48 
     49 static int include_stack[MAX_INCLUDES];
     50 static char *include_files[MAX_INCLUDES];
     51 static int include_ptr = 0;
     52 
     53 static char *
     54 dequote(const char *s, size_t *len)
     55 {
     56 	const unsigned char *p;
     57 	char *buf, *q;
     58 
     59 	*len = 0;
     60 	p = (const unsigned char *)s;
     61 	while (*p) {
     62 		if (*p == '\\' && p[1]) {
     63 			if (isdigit(p[1]) && isdigit(p[2]) && isdigit(p[3]))
     64 				p += 3;
     65 			else
     66 				++p;
     67 		}
     68 		++(*len);
     69 		++p;
     70 	}
     71 
     72 	buf = malloc(*len + 1);
     73 	if (buf == NULL)
     74 		return NULL;
     75 
     76 	p = (const unsigned char *)s;
     77 	q = buf;
     78 	while (*p) {
     79 		if (*p == '\\' && p[1]) {
     80 			++p;
     81 
     82 			if (isdigit(p[0])) {
     83 				if (isdigit(p[1]) && isdigit(p[2])) {
     84 					*q++ = (p[0] - '0') * 64 +
     85 					    (p[1] - '0') * 8 +
     86 					    (p[2] - '0');
     87 					p += 3;
     88 				} else {
     89 					*q++ = *p++;
     90 				}
     91 				continue;
     92 			}
     93 
     94 			switch (*p) {
     95 			case 'e':
     96 				/* escape */
     97 				*q++ = '\e';
     98 				p++;
     99 				break;
    100 
    101 			case 'n':
    102 				/* newline */
    103 				*q++ = '\n';
    104 				p++;
    105 				break;
    106 
    107 			case 'r':
    108 				/* carriage return */
    109 				*q++ = '\r';
    110 				p++;
    111 				break;
    112 
    113 			case 't':
    114 				/* tab */
    115 				*q++ = '\t';
    116 				p++;
    117 				break;
    118 
    119 			case '\\':
    120 				/* backslash */
    121 				*q++ = '\\';
    122 				p++;
    123 				break;
    124 
    125 			default:
    126 				if (isalpha(*p))
    127 					errx(2,
    128 					    "%s:%zu: Invalid escape sequence "
    129 					    "'\\%c' in string literal",
    130 					    cur_file, line, *p);
    131 				*q++ = *p++;
    132 			}
    133 		} else
    134 			*q++ = *p++;
    135 	}
    136 	*q++ = '\0';
    137 
    138 	return buf;
    139 }
    140 %}
    141 
    142 HEX		0[xX][0-9a-zA-Z]+
    143 STRING		[0-9a-z!#-&(-^ \t%._\\]+
    144 numeric		[-0-9]+
    145 PCHAR           (\\.|[!-~])
    146 ASSIGN		assign
    147 CALL2		call2
    148 CALL3		call3
    149 CALL4		call4
    150 CALL		call
    151 CHECK		check
    152 DELAY		delay
    153 INPUT		input
    154 NOINPUT		noinput
    155 OK_RET		OK
    156 ERR_RET		ERR
    157 COMPARE		compare
    158 COMPAREND	comparend
    159 FILENAME	[A-Za-z0-9.][A-Za-z0-9./_-]+
    160 VARNAME		[A-Za-z][A-Za-z0-9_-]+
    161 NULL_RET	NULL
    162 NON_NULL	NON_NULL
    163 CCHAR		cchar
    164 WCHAR		wchar
    165 BYTE		BYTE
    166 OR		\|
    167 LPAREN		\(
    168 RPAREN		\)
    169 LBRACK		\[
    170 RBRACK		\]
    171 MULTIPLIER	\*
    172 COMMA		,
    173 
    174 %x incl
    175 %option noinput nounput
    176 
    177 %%
    178 
    179 include		BEGIN(incl);
    180 
    181 <incl>[ \t]*	/* eat the whitespace */
    182 <incl>[^ \t\n]+ { /* got the include file name */
    183 		char *inc_file;
    184 
    185 		if (include_ptr > MAX_INCLUDES) {
    186 			errx(2,
    187 			    "%s:%zu: Maximum number of nested includes "
    188 			    "exceeded", cur_file, line);
    189 		}
    190 
    191 		const char *dir_begin;
    192 		int dir_len;
    193 		if (yytext[0] == '/') {
    194 			dir_begin = "";
    195 			dir_len = 0;
    196 		} else {
    197 			dir_begin = cur_file;
    198 			const char *dir_end = strrchr(cur_file, '/');
    199 			if (dir_end != NULL) {
    200 				dir_len = (int)(dir_end + 1 - dir_begin);
    201 			} else {
    202 				dir_begin = ".";
    203 				dir_len = 1;
    204 			}
    205 		}
    206 
    207 		if (asprintf(&inc_file, "%.*s%s",
    208 		    dir_len, dir_begin, yytext) == -1)
    209 			err(2, "Cannot construct include path");
    210 
    211 		yyin = fopen(inc_file, "r");
    212 
    213 		if (!yyin)
    214 			err(1, "Error opening %s", inc_file);
    215 
    216 		yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
    217 
    218 		include_stack[include_ptr] = line;
    219 		include_files[include_ptr++] = cur_file;
    220 		cur_file = inc_file;
    221 		line = 1;
    222 		BEGIN(INITIAL);
    223 	}
    224 
    225 <<EOF>>	{
    226 		yypop_buffer_state();
    227 
    228 		if (!YY_CURRENT_BUFFER)
    229 			yyterminate();
    230 
    231 		if (--include_ptr < 0)
    232 			errx(2, "Include stack underflow");
    233 
    234 		free(cur_file);
    235 		cur_file = include_files[include_ptr];
    236 		line = include_stack[include_ptr];
    237 	}
    238 
    239 {ASSIGN}	return ASSIGN;
    240 {CALL2}		return CALL2;
    241 {CALL3}		return CALL3;
    242 {CALL4}		return CALL4;
    243 {CALL}		return CALL;
    244 {CHECK}		return CHECK;
    245 {DELAY}		return DELAY;
    246 {INPUT}		return INPUT;
    247 {NOINPUT}	return NOINPUT;
    248 {COMPARE}	return COMPARE;
    249 {COMPAREND}	return COMPAREND;
    250 {NON_NULL}	return NON_NULL;
    251 {NULL_RET}	return NULL_RET;
    252 {OK_RET}	return OK_RET;
    253 {ERR_RET}	return ERR_RET;
    254 {MULTIPLIER}	return MULTIPLIER;
    255 {COMMA}		return COMMA;
    256 {CCHAR}		return CCHAR;
    257 {WCHAR}		return WCHAR;
    258 {OR}		return OR;
    259 {LPAREN}	return LPAREN;
    260 {RPAREN}	return RPAREN;
    261 {LBRACK}	return LBRACK;
    262 {RBRACK}	return RBRACK;
    263 
    264 {HEX}		{
    265 			/* Hex value, convert to decimal and return numeric */
    266 			unsigned long val;
    267 
    268 			if (sscanf(yytext, "%lx", &val) != 1)
    269 				errx(1, "Bad hex conversion");
    270 
    271 			asprintf(&yylval.string, "%ld", val);
    272 			return numeric;
    273 		}
    274 
    275 {numeric}	{
    276 			if ((yylval.string = strdup(yytext)) == NULL)
    277 				err(1, "Cannot allocate numeric string");
    278 			return numeric;
    279 		}
    280 
    281 {VARNAME}	{
    282 			if ((yylval.string = strdup(yytext)) == NULL)
    283 				err(1, "Cannot allocate string for varname");
    284 			return VARNAME;
    285 		}
    286 
    287 {FILENAME}	{
    288 			size_t len;
    289 
    290 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    291 				err(1, "Cannot allocate filename string");
    292 			return FILENAME;
    293 		}
    294 
    295 	/* path */
    296 \/{PCHAR}+	{
    297 			size_t len;
    298 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    299 				err(1, "Cannot allocate string");
    300 			return PATH;
    301 		}
    302 
    303 \'{STRING}\' 	{
    304 			char *p;
    305 			size_t len;
    306 
    307 			if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
    308 				err(1, "Cannot allocate return struct");
    309 			p = yytext;
    310 			p++; /* skip the leading ' */
    311 			if ((yylval.retval->data_value = dequote(p, &len))
    312 			     == NULL)
    313 				err(1, "Cannot allocate string");
    314 
    315 			yylval.retval->data_type = data_byte;
    316 			/* trim trailing ' */
    317 			yylval.retval->data_len = len - 1;
    318 			return BYTE;
    319 		}
    320 
    321 \`{STRING}\` 	{
    322 			char *p, *str;
    323 			size_t len, chlen;
    324 			size_t i;
    325 			chtype *rv;
    326 
    327 			if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
    328 				err(1, "Cannot allocate return struct");
    329 			p = yytext;
    330 			p++; /* skip the leading ` */
    331 			if ((str = dequote(p, &len)) == NULL)
    332 				err(1, "Cannot allocate string");
    333 			len--; /* trim trailing ` */
    334 			if ((len % 2) != 0)
    335 				len--;
    336 
    337 			chlen = ((len / 2) + 1) * sizeof(chtype);
    338 			if ((yylval.retval->data_value = malloc(chlen))
    339 			    == NULL)
    340 				err(1, "Cannot allocate chtype array");
    341 
    342 			rv = yylval.retval->data_value;
    343 			for (i = 0; i < len; i += 2)
    344 				*rv++ = (str[i] << 8) | str[i+1];
    345 			*rv = __NORMAL | '\0'; /* terminates chtype array */
    346 			yylval.retval->data_type = data_byte;
    347 			yylval.retval->data_len = chlen;
    348 			return BYTE;
    349 		}
    350 
    351 \"{STRING}\" 	{
    352 			char *p;
    353 			size_t len;
    354 
    355 			p = yytext;
    356 			p++; /* skip the leading " */
    357 			if ((yylval.string = dequote(p, &len)) == NULL)
    358 				err(1, "Cannot allocate string");
    359 
    360 			/* remove trailing " */
    361 			yylval.string[len - 1] = '\0';
    362 			return STRING;
    363 		}
    364 
    365 \${VARNAME}	{
    366 			char *p;
    367 
    368 			p = yytext;
    369 			p++; /* skip $ before var name */
    370 			if ((yylval.string = strdup(p)) == NULL)
    371 				err(1, "Cannot allocate string for varname");
    372 			return VARIABLE;
    373 		}
    374 
    375 	/* whitespace, comments */
    376 [ \t\r]		|
    377 #.*		;
    378 
    379 ^[ \t\r]*#.*\n	|
    380 \\\n		|
    381 ^\n		line++;
    382 
    383 	/* eol on a line with data. need to process, return eol */
    384 #.*\n		|
    385 \n		{
    386 			line++;
    387 			return EOL;
    388 		}
    389 
    390 .		{
    391 			if (isprint((unsigned char)yytext[0]))
    392 				errx(1, "%s:%zu: Invalid character '%c'",
    393 				    cur_file, line + 1, yytext[0]);
    394 			else
    395 				errx(1, "%s:%zu: Invalid character '0x%02x'",
    396 				    cur_file, line + 1, yytext[0]);
    397 		}
    398 
    399 %%
    400 
    401 int
    402 yywrap(void)
    403 {
    404 	return 1;
    405 }
    406