Home | History | Annotate | Line # | Download | only in director
testlang_conf.l revision 1.22
      1 %{
      2 /*	$NetBSD: testlang_conf.l,v 1.22 2021/02/25 00:25:31 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)) && *(p+2) && isdigit(*(p+2)) &&
     64 			    *(p+3) && isdigit(*(p+3)))
     65 				p += 3;
     66 			else
     67 				++p;
     68 		}
     69 		++(*len);
     70 		++p;
     71 	}
     72 
     73 	buf = malloc(*len + 1);
     74 	if (buf == NULL)
     75 		return NULL;
     76 
     77 	p = (const unsigned char *)s;
     78 	q = buf;
     79 	while (*p) {
     80 		if (*p == '\\' && *(p+1)) {
     81 			++p;
     82 			if (isdigit(*p)) {
     83 				if (*(p+1) && isdigit(*(p+1)) && *(p+2) &&
     84 				    isdigit(*(p+2))) {
     85 					*q++ = ((*p - '0') * 8 + (*(p+1) - '0')) * 8 + (*(p+2) - '0');
     86 					p += 3;
     87 				} else {
     88 					*q++ = *p++;
     89 				}
     90 			} else {
     91 				switch (*p) {
     92 				case 'e':
     93 					/* escape */
     94 					*q++ = '\e';
     95 					p++;
     96 					break;
     97 
     98 				case 'n':
     99 					/* newline */
    100 					*q++ = '\n';
    101 					p++;
    102 					break;
    103 
    104 				case 'r':
    105 					/* carriage return */
    106 					*q++ = '\r';
    107 					p++;
    108 					break;
    109 
    110 				case 't':
    111 					/* tab */
    112 					*q++ = '\t';
    113 					p++;
    114 					break;
    115 
    116 				case '\\':
    117 					/* backslash */
    118 					*q++ = '\\';
    119 					p++;
    120 					break;
    121 
    122 				default:
    123 					if (isalpha((unsigned char)*p))
    124 						errx(2,
    125 						    "%s:%zu: invalid escape sequence '\\%c' in string literal",
    126 						    cur_file, line, *p);
    127 					*q++ = *p++;
    128 				}
    129 			}
    130 		} else
    131 			*q++ = *p++;
    132 	}
    133 	*q++ = '\0';
    134 
    135 	return buf;
    136 }
    137 %}
    138 
    139 HEX		0[xX][0-9a-zA-Z]+
    140 STRING		[0-9a-z!#-&(-^ \t%._\\]+
    141 numeric		[-0-9]+
    142 PCHAR           (\\.|[!-~])
    143 ASSIGN		assign
    144 CALL2		call2
    145 CALL3		call3
    146 CALL4		call4
    147 CALL		call
    148 CHECK		check
    149 DELAY		delay
    150 INPUT		input
    151 NOINPUT		noinput
    152 OK_RET		OK
    153 ERR_RET		ERR
    154 COMPARE		compare
    155 COMPAREND	comparend
    156 FILENAME	[A-Za-z0-9.][A-Za-z0-9./_-]+
    157 VARNAME		[A-Za-z][A-Za-z0-9_-]+
    158 NULL_RET	NULL
    159 NON_NULL	NON_NULL
    160 CCHAR		cchar
    161 WCHAR		wchar
    162 BYTE		BYTE
    163 OR		\|
    164 LPAREN		\(
    165 RPAREN		\)
    166 LBRACK		\[
    167 RBRACK		\]
    168 MULTIPLIER	\*
    169 COMMA		,
    170 
    171 %x incl
    172 %option noinput nounput
    173 
    174 %%
    175 
    176 include		BEGIN(incl);
    177 
    178 <incl>[ \t]*	/* eat the whitespace */
    179 <incl>[^ \t\n]+ { /* got the include file name */
    180 		char *inc_file;
    181 
    182 		if (include_ptr > MAX_INCLUDES) {
    183 			fprintf(stderr,
    184 				"Maximum number of nested includes exceeded "
    185 				"at line %zu of file %s\n", line, cur_file);
    186 				exit(2);
    187 		}
    188 
    189 		const char *dir_begin;
    190 		int dir_len;
    191 		if (yytext[0] == '/') {
    192 			dir_begin = "";
    193 			dir_len = 0;
    194 		} else {
    195 			dir_begin = cur_file;
    196 			const char *dir_end = strrchr(cur_file, '/');
    197 			if (dir_end != NULL) {
    198 				dir_len = (int)(dir_end + 1 - dir_begin);
    199 			} else {
    200 				dir_begin = ".";
    201 				dir_len = 1;
    202 			}
    203 		}
    204 
    205 		if (asprintf(&inc_file, "%.*s%s",
    206 		    dir_len, dir_begin, yytext) == -1)
    207 			err(2, "Cannot construct include path");
    208 
    209 		yyin = fopen(inc_file, "r");
    210 
    211 		if (!yyin)
    212 			err(1, "Error opening %s", inc_file);
    213 
    214 		yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
    215 
    216 		include_stack[include_ptr] = line;
    217 		include_files[include_ptr++] = cur_file;
    218 		cur_file = inc_file;
    219 		line = 1;
    220 		BEGIN(INITIAL);
    221 	}
    222 
    223 <<EOF>>	{
    224 		yypop_buffer_state();
    225 
    226 		if (!YY_CURRENT_BUFFER)
    227 			yyterminate();
    228 
    229 		if (--include_ptr < 0)
    230 			errx(2, "Include stack underflow");
    231 
    232 		free(cur_file);
    233 		cur_file = include_files[include_ptr];
    234 		line = include_stack[include_ptr];
    235 	}
    236 
    237 {ASSIGN}	return ASSIGN;
    238 {CALL2}		return CALL2;
    239 {CALL3}		return CALL3;
    240 {CALL4}		return CALL4;
    241 {CALL}		return CALL;
    242 {CHECK}		return CHECK;
    243 {DELAY}		return DELAY;
    244 {INPUT}		return INPUT;
    245 {NOINPUT}	return NOINPUT;
    246 {COMPARE}	return COMPARE;
    247 {COMPAREND}	return COMPAREND;
    248 {NON_NULL}	return NON_NULL;
    249 {NULL_RET}	return NULL_RET;
    250 {OK_RET}	return OK_RET;
    251 {ERR_RET}	return ERR_RET;
    252 {MULTIPLIER}	return MULTIPLIER;
    253 {COMMA}		return COMMA;
    254 {CCHAR}		return CCHAR;
    255 {WCHAR}		return WCHAR;
    256 {OR}		return OR;
    257 {LPAREN}	return LPAREN;
    258 {RPAREN}	return RPAREN;
    259 {LBRACK}	return LBRACK;
    260 {RBRACK}	return RBRACK;
    261 
    262 {HEX}		{
    263 			/* Hex value, convert to decimal and return numeric */
    264 			unsigned long val;
    265 
    266 			if (sscanf(yytext, "%lx", &val) != 1)
    267 				errx(1, "Bad hex conversion");
    268 
    269 			asprintf(&yylval.string, "%ld", val);
    270 			return numeric;
    271 		}
    272 
    273 {numeric}	{
    274 			if ((yylval.string = strdup(yytext)) == NULL)
    275 				err(1, "Cannot allocate numeric string");
    276 			return numeric;
    277 		}
    278 
    279 {VARNAME}	{
    280 			if ((yylval.string = strdup(yytext)) == NULL)
    281 				err(1, "Cannot allocate string for varname");
    282 			return VARNAME;
    283 		}
    284 
    285 {FILENAME}	{
    286 			size_t len;
    287 
    288 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    289 				err(1, "Cannot allocate filename string");
    290 			return FILENAME;
    291 		}
    292 
    293 	/* path */
    294 \/{PCHAR}+	{
    295 			size_t len;
    296 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    297 				err(1, "Cannot allocate string");
    298 			return PATH;
    299 		}
    300 
    301 \'{STRING}\' 	{
    302 			char *p;
    303 			size_t len;
    304 
    305 			if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
    306 				err(1, "Cannot allocate return struct");
    307 			p = yytext;
    308 			p++; /* skip the leading ' */
    309 			if ((yylval.retval->data_value = dequote(p, &len))
    310 			     == NULL)
    311 				err(1, "Cannot allocate string");
    312 
    313 			yylval.retval->data_type = data_byte;
    314 			/* trim trailing ' */
    315 			yylval.retval->data_len = len - 1;
    316 			return BYTE;
    317 		}
    318 
    319 \`{STRING}\` 	{
    320 			char *p, *str;
    321 			size_t len, chlen;
    322 			size_t i;
    323 			chtype *rv;
    324 
    325 			if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
    326 				err(1, "Cannot allocate return struct");
    327 			p = yytext;
    328 			p++; /* skip the leading ` */
    329 			if ((str = dequote(p, &len)) == NULL)
    330 				err(1, "Cannot allocate string");
    331 			len--; /* trim trailing ` */
    332 			if ((len % 2) != 0)
    333 				len--;
    334 
    335 			chlen = ((len / 2) + 1) * sizeof(chtype);
    336 			if ((yylval.retval->data_value = malloc(chlen))
    337 			    == NULL)
    338 				err(1, "Cannot allocate chtype array");
    339 
    340 			rv = yylval.retval->data_value;
    341 			for (i = 0; i < len; i += 2)
    342 				*rv++ = (str[i] << 8) | str[i+1];
    343 			*rv = __NORMAL | '\0'; /* terminates chtype array */
    344 			yylval.retval->data_type = data_byte;
    345 			yylval.retval->data_len = chlen;
    346 			return BYTE;
    347 		}
    348 
    349 \"{STRING}\" 	{
    350 			char *p;
    351 			size_t len;
    352 
    353 			p = yytext;
    354 			p++; /* skip the leading " */
    355 			if ((yylval.string = dequote(p, &len)) == NULL)
    356 				err(1, "Cannot allocate string");
    357 
    358 			/* remove trailing " */
    359 			yylval.string[len - 1] = '\0';
    360 			return STRING;
    361 		}
    362 
    363 \${VARNAME}	{
    364 			char *p;
    365 
    366 			p = yytext;
    367 			p++; /* skip $ before var name */
    368 			if ((yylval.string = strdup(p)) == NULL)
    369 				err(1, "Cannot allocate string for varname");
    370 			return VARIABLE;
    371 		}
    372 
    373 	/* whitespace, comments */
    374 [ \t\r]		|
    375 #.*		;
    376 
    377 ^[ \t\r]*#.*\n	|
    378 \\\n		|
    379 ^\n		line++;
    380 
    381 	/* eol on a line with data. need to process, return eol */
    382 #.*\n		|
    383 \n		{
    384 			line++;
    385 			return EOL;
    386 		}
    387 
    388 .		{
    389 			if (isprint((unsigned char)yytext[0]))
    390 				errx(1, "%s:%zu: Invalid character '%c'",
    391 				    cur_file, line + 1, yytext[0]);
    392 			else
    393 				errx(1, "%s:%zu: Invalid character '0x%02x'",
    394 				    cur_file, line + 1, yytext[0]);
    395 		}
    396 
    397 %%
    398 
    399 int
    400 yywrap(void)
    401 {
    402 	return 1;
    403 }
    404