Home | History | Annotate | Line # | Download | only in director
testlang_conf.l revision 1.7.26.1
      1 %{
      2 /*	$NetBSD: testlang_conf.l,v 1.7.26.1 2020/04/13 08:05:28 martin 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 BYTE		BYTE
    160 OR		\|
    161 LHB		\(
    162 RHB		\)
    163 LHSB		\[
    164 RHSB		\]
    165 MULTIPLIER	\*
    166 
    167 %x incl
    168 %option noinput nounput
    169 
    170 %%
    171 
    172 include		BEGIN(incl);
    173 
    174 <incl>[ \t]*      /* eat the whitespace */
    175 <incl>[^ \t\n]+   { /* got the include file name */
    176 		char inc_file[MAXPATHLEN];
    177 
    178 		if (include_ptr > MAX_INCLUDES) {
    179 			fprintf(stderr,
    180 				"Maximum number of nested includes exceeded "
    181 				"at line %zu of file %s\n", line, cur_file);
    182 				exit(2);
    183 		}
    184 
    185 		if (yytext[0] != '/') {
    186 			if (strlcpy(inc_file, include_path, sizeof(inc_file))
    187 			    >= sizeof(inc_file))
    188 				err(2, "CHECK_PATH too long");
    189 			if ((include_path[strlen(include_path) - 1] != '/') &&
    190 			    ((strlcat(inc_file, "/", sizeof(inc_file))
    191 			    >= sizeof(inc_file))))
    192 				err(2, "Could not append / to include file path");
    193 		} else {
    194 			inc_file[0] = '\0';
    195 		}
    196 
    197 		if (strlcat(inc_file, yytext, sizeof(inc_file))
    198 		    >= sizeof(inc_file))
    199 			err(2, "Path to include file path overflowed");
    200 
    201 		yyin = fopen(inc_file, "r" );
    202 
    203 		if (!yyin)
    204 			err(1, "Error opening %s", inc_file);
    205 
    206 		yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
    207 
    208 		include_stack[include_ptr] = line;
    209 		include_files[include_ptr++] = cur_file;
    210 		cur_file = strdup(inc_file);
    211 		if (cur_file == NULL)
    212 			err(2, "Cannot allocate new include file string");
    213 		line = 0;
    214 		BEGIN(INITIAL);
    215 	}
    216 
    217 <<EOF>>	{
    218 		yypop_buffer_state();
    219 
    220 		if ( !YY_CURRENT_BUFFER )
    221 		{
    222 			yyterminate();
    223 		}
    224 
    225 		if (--include_ptr < 0)
    226 			err(2, "Include stack underflow");
    227 
    228 		free(cur_file);
    229 		cur_file = include_files[include_ptr];
    230 		line = include_stack[include_ptr];
    231 	}
    232 
    233 {ASSIGN}	{
    234 			return ASSIGN;
    235 		}
    236 
    237 {CALL2}		{
    238 			return CALL2;
    239 		}
    240 
    241 {CALL3}		{
    242 			return CALL3;
    243 		}
    244 
    245 {CALL4}		{
    246 			return CALL4;
    247 		}
    248 
    249 {CALL}		{
    250 			return CALL;
    251 		}
    252 
    253 {CHECK}		{
    254 			return CHECK;
    255 		}
    256 
    257 {DELAY}		{
    258 			return DELAY;
    259 		}
    260 
    261 {INPUT}		{
    262 			return INPUT;
    263 		}
    264 
    265 {NOINPUT}		{
    266 			return NOINPUT;
    267 		}
    268 
    269 {COMPARE}	{
    270 			return COMPARE;
    271 		}
    272 
    273 {COMPAREND}	{
    274 			return COMPAREND;
    275 		}
    276 
    277 {NON_NULL}	{
    278 			return NON_NULL;
    279 		}
    280 
    281 {NULL_RET}		{
    282 			return NULL_RET;
    283 		}
    284 
    285 {OK_RET}		{
    286 			return OK_RET;
    287 		}
    288 
    289 {ERR_RET}		{
    290 			return ERR_RET;
    291 		}
    292 
    293 {MULTIPLIER}	{
    294 			return MULTIPLIER;
    295 		}
    296 
    297 {CCHAR}	{
    298 			return CCHAR;
    299 		}
    300 
    301 {OR}		{
    302 			return OR;
    303 		}
    304 
    305 {LHB}		{
    306 			return LHB;
    307 		}
    308 
    309 {RHB}		{
    310 			return RHB;
    311 		}
    312 
    313 {LHSB}		{
    314 			return LHSB;
    315 		}
    316 
    317 {RHSB}		{
    318 			return RHSB;
    319 		}
    320 
    321 {HEX}		{
    322 			/* Hex value, convert to decimal and return numeric */
    323 			unsigned long val;
    324 
    325 			if (sscanf(yytext, "%lx", &val) != 1)
    326 				err(1, "Bad hex conversion");
    327 
    328 			asprintf(&yylval.string, "%ld", val);
    329 			return numeric;
    330 		}
    331 
    332 
    333 {numeric}		{
    334 			if ((yylval.string = strdup(yytext)) == NULL)
    335 				err(1, "Cannot allocate numeric string");
    336 			return numeric;
    337 }
    338 
    339 {VARNAME}	{
    340 			if ((yylval.string = strdup(yytext)) == NULL)
    341 				err(1, "Cannot allocate string for varname");
    342 			return VARNAME;
    343 		}
    344 
    345 {FILENAME}	{
    346 			size_t len;
    347 
    348 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    349 				err(1, "Cannot allocate filename string");
    350 			return FILENAME;
    351 		}
    352 
    353 	/* path */
    354 \/{PCHAR}+	{
    355 			size_t len;
    356 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    357 				err(1, "Cannot allocate string");
    358 			return PATH;
    359 		}
    360 
    361 \'{STRING}\' 	{
    362 			char *p;
    363 			size_t len;
    364 
    365 			if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
    366 				err(1, "Cannot allocate return struct");
    367 			p = yytext;
    368 			p++; /* skip the leading ' */
    369 			if ((yylval.retval->data_value = dequote(p, &len))
    370 			     == NULL)
    371 				err(1, "Cannot allocate string");
    372 
    373 			yylval.retval->data_type = data_byte;
    374 			/* trim trailing ' */
    375 			yylval.retval->data_len = len - 1;
    376 			return BYTE;
    377 		}
    378 
    379 \`{STRING}\` 	{
    380 			char *p, *str;
    381 			size_t len, chlen;
    382 			size_t i;
    383 			chtype *rv;
    384 
    385 			if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
    386 				err(1, "Cannot allocate return struct");
    387 			p = yytext;
    388 			p++; /* skip the leading ' */
    389 			if ((str = dequote(p, &len)) == NULL)
    390 				err(1, "Cannot allocate string");
    391 			len--; /* trim trailing ` */
    392 			if ((len % 2) != 0)
    393 				len--;
    394 
    395 			chlen = ((len / 2) + 1) * sizeof(chtype);
    396 			if ((yylval.retval->data_value = malloc(chlen))
    397 			    == NULL)
    398 				err(1, "Cannot allocate chtype array");
    399 
    400 			rv = yylval.retval->data_value;
    401 			for (i = 0; i < len; i += 2)
    402 				*rv++ = (str[i] << 8) | str[i+1];
    403 			*rv = __NORMAL | '\0'; /* terminates chtype array */
    404 			yylval.retval->data_type = data_byte;
    405 			yylval.retval->data_len = chlen;
    406 			return BYTE;
    407 		}
    408 
    409 \"{STRING}\" 	{
    410 			char *p;
    411 			size_t len;
    412 
    413 			p = yytext;
    414 			p++; /* skip the leading " */
    415 			if ((yylval.string = dequote(p, &len)) == NULL)
    416 				err(1, "Cannot allocate string");
    417 
    418 			/* remove trailing " */
    419 			yylval.string[len - 1] = '\0';
    420 			return STRING;
    421 		}
    422 
    423 \${VARNAME}	{
    424 			char *p;
    425 
    426 			p = yytext;
    427 			p++; /* skip $ before var name */
    428 			if ((yylval.string = strdup(p)) == NULL)
    429 				err(1, "Cannot allocate string for varname");
    430 			return VARIABLE;
    431 		}
    432 
    433 	/* comments, white-outs */
    434 [ \t\r]		|
    435 #.*		;
    436 ^#.*\n		|
    437 #.*\n		|
    438 \\\n		|
    439 ^\n		{
    440 line++; }
    441 
    442 	/* eol on a line with data. need to process, return eol */
    443 \n		{
    444 			line++;
    445 			return EOL;
    446 		}
    447 
    448 .		{
    449 		}
    450 
    451 %%
    452 
    453 int
    454 yywrap(void)
    455 {
    456 	return 1;
    457 }
    458