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