Home | History | Annotate | Line # | Download | only in director
testlang_conf.l revision 1.1
      1 %{
      2 /*	$NetBSD 	*/
      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 <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 *include_path; 	/* from director.c */
     48 extern char *cur_file;		/* from director.c */
     49 
     50 static int include_stack[MAX_INCLUDES];
     51 static char *include_files[MAX_INCLUDES];
     52 static int include_ptr = 0;
     53 
     54 static char *
     55 dequote(const char *s, size_t *len)
     56 {
     57 	const char *p;
     58 	char *buf, *q, c;
     59 
     60 	*len = 0;
     61 	p = s;
     62 	while (*p) {
     63 		if (*p == '\\' && *(p+1)) {
     64 			if (isdigit(*(p+1)) && *(p+2) && isdigit(*(p+2)) &&
     65 			    *(p+3) && isdigit(*(p+3)))
     66 				p += 3;
     67 			else
     68 				++p;
     69 		}
     70 		++(*len);
     71 		++p;
     72 	}
     73 
     74 	buf = malloc(*len + 1);
     75 	if (buf == NULL)
     76 		return NULL;
     77 
     78 	p = s;
     79 	q = buf;
     80 	while (*p) {
     81 		if (*p == '\\' && *(p+1)) {
     82 			++p;
     83 			if (isdigit(*p)) {
     84 				if (*(p+1) && isdigit(*(p+1)) && *(p+2) &&
     85 				    isdigit(*(p+2))) {
     86 					*q++ = ((*p - '0') * 8 + (*(p+1) - '0')) * 8 + (*(p+2) - '0');
     87 					p += 3;
     88 				} else {
     89 					*q++ = *p++;
     90 				}
     91 			} else {
     92 				switch (*p) {
     93 				case 'e':
     94 					/* escape */
     95 					*q++ = '\e';
     96 					p++;
     97 					break;
     98 
     99 				case 'n':
    100 					/* newline */
    101 					*q++ = '\n';
    102 					p++;
    103 					break;
    104 
    105 				case 't':
    106 					/* tab */
    107 					*q++ = '\t';
    108 					p++;
    109 					break;
    110 
    111 				case '\\':
    112 					/* backslash */
    113 					*q++ = '\\';
    114 					p++;
    115 					break;
    116 
    117 				default:
    118 					*q++ = *p++;
    119 				}
    120 			}
    121 		} else
    122 			*q++ = *p++;
    123 	}
    124 	*q++ = '\0';
    125 
    126 	return buf;
    127 }
    128 %}
    129 
    130 HEX		0[xX][0-9a-zA-Z]+
    131 STRING		[0-9a-z!#-&(-^ \t%._\\]+
    132 numeric		[-0-9]+
    133 PCHAR           (\\.|[^ \t\n])
    134 ASSIGN		[aA][sS][sS][iI][gG][nN]
    135 CALL2		[cC][aA][lL][lL]2
    136 CALL3		[cC][aA][lL][lL]3
    137 CALL4		[cC][aA][lL][lL]4
    138 CALL		[cC][aA][lL][lL]
    139 CHECK		[cC][hH][eE][cC][kK]
    140 DELAY		[dD][eE][lL][aA][yY]
    141 INPUT		[iI][nN][pP][uU][tT]
    142 NOINPUT		[nN][oO][iI][nN][pP][uU][tT]
    143 OK_RET		[oO][kK]
    144 ERR_RET		[eE][rR][rR]
    145 COMPARE		[cC][oO][mM][pP][aA][rR][eE]
    146 COMPAREND	[cC][oO][mM][pP][aA][rR][eE][Nn][Dd]
    147 FILENAME	[A-Za-z0-9.][A-Za-z0-9./_-]+
    148 VARNAME		[A-Za-z][A-Za-z0-9_-]+
    149 NULL_RET	NULL
    150 NON_NULL	NON_NULL
    151 BYTE		BYTE
    152 OR		\|
    153 LHB		\(
    154 RHB		\)
    155 
    156 %x incl
    157 
    158 %%
    159 
    160 include		BEGIN(incl);
    161 
    162 <incl>[ \t]*      /* eat the whitespace */
    163 <incl>[^ \t\n]+   { /* got the include file name */
    164 		char inc_file[MAXPATHLEN];
    165 
    166 		if (include_ptr > MAX_INCLUDES) {
    167 			fprintf(stderr,
    168 				"Maximum number of nested includes exceeded "
    169 				"at line %d of file %s\n", line, cur_file);
    170 				exit(2);
    171 		}
    172 
    173 		if (yytext[0] != '/') {
    174 			if (strlcpy(inc_file, include_path, sizeof(inc_file))
    175 			    >= sizeof(inc_file))
    176 				err(2, "CHECK_PATH too long");
    177 			if ((include_path[strlen(include_path) - 1] != '/') &&
    178 			    ((strlcat(inc_file, "/", sizeof(inc_file))
    179 			    >= sizeof(inc_file))))
    180 				err(2, "Could not append / to include file path");
    181 		} else {
    182 			inc_file[0] = '\0';
    183 		}
    184 
    185 		if (strlcat(inc_file, yytext, sizeof(inc_file))
    186 		    >= sizeof(inc_file))
    187 			err(2, "Path to include file path overflowed");
    188 
    189 		yyin = fopen(inc_file, "r" );
    190 
    191 		if (!yyin) {
    192 			fprintf(stderr, "Error opening %s: ", inc_file);
    193 			perror(NULL);
    194 			exit(2);
    195 		}
    196 
    197 		yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
    198 
    199 		include_stack[include_ptr] = line;
    200 		include_files[include_ptr++] = cur_file;
    201 		cur_file = malloc(strlen(inc_file));
    202 		if (cur_file == NULL)
    203 			err(2, "Cannot allocate new include file string");
    204 
    205 		strlcpy(cur_file, inc_file, sizeof(inc_file));
    206 		line = 0;
    207 		BEGIN(INITIAL);
    208 	}
    209 
    210 <<EOF>>	{
    211 		yypop_buffer_state();
    212 
    213 		if ( !YY_CURRENT_BUFFER )
    214 		{
    215 			yyterminate();
    216 		}
    217 
    218 		if (--include_ptr < 0)
    219 			err(2, "Include stack underflow");
    220 
    221 		free(cur_file);
    222 		cur_file = include_files[include_ptr];
    223 		line = include_stack[include_ptr];
    224 	}
    225 
    226 {ASSIGN}	{
    227 			return ASSIGN;
    228 		}
    229 
    230 {CALL2}		{
    231 			return CALL2;
    232 		}
    233 
    234 {CALL3}		{
    235 			return CALL3;
    236 		}
    237 
    238 {CALL4}		{
    239 			return CALL4;
    240 		}
    241 
    242 {CALL}		{
    243 			return CALL;
    244 		}
    245 
    246 {CHECK}		{
    247 			return CHECK;
    248 		}
    249 
    250 {DELAY}		{
    251 			return DELAY;
    252 		}
    253 
    254 {INPUT}		{
    255 			return INPUT;
    256 		}
    257 
    258 {NOINPUT}		{
    259 			return NOINPUT;
    260 		}
    261 
    262 {COMPARE}	{
    263 			return COMPARE;
    264 		}
    265 
    266 {COMPAREND}	{
    267 			return COMPAREND;
    268 		}
    269 
    270 {NON_NULL}	{
    271 			return NON_NULL;
    272 		}
    273 
    274 {NULL_RET}		{
    275 			return NULL_RET;
    276 		}
    277 
    278 {OK_RET}		{
    279 			return OK_RET;
    280 		}
    281 
    282 {ERR_RET}		{
    283 			return ERR_RET;
    284 		}
    285 
    286 {OR}		{
    287 			return OR;
    288 		}
    289 
    290 {LHB}		{
    291 			return LHB;
    292 		}
    293 
    294 {RHB}		{
    295 			return RHB;
    296 		}
    297 
    298 {HEX}		{
    299 			/* Hex value, convert to decimal and return numeric */
    300 			unsigned long val;
    301 
    302 			if (sscanf(yytext, "%lx", &val) != 1)
    303 				err(1, "Bad hex conversion");
    304 
    305 			asprintf(&yylval.string, "%ld", val);
    306 			return numeric;
    307 		}
    308 
    309 
    310 {numeric}		{
    311 			if ((yylval.string = strdup(yytext)) == NULL)
    312 				err(1, "Cannot allocate numeric string");
    313 			return numeric;
    314 }
    315 
    316 {VARNAME}	{
    317 			if ((yylval.string = strdup(yytext)) == NULL)
    318 				err(1, "Cannot allocate string for varname");
    319 			return VARNAME;
    320 		}
    321 
    322 {FILENAME}	{
    323 			size_t len;
    324 
    325 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    326 				err(1, "Cannot allocate filename string");
    327 			return FILENAME;
    328 		}
    329 
    330 	/* path */
    331 \/{PCHAR}+	{
    332 			size_t len;
    333 			if ((yylval.string = dequote(yytext, &len)) == NULL)
    334 				err(1, "Cannot allocate string");
    335 			return PATH;
    336 		}
    337 
    338 \'{STRING}\' 	{
    339 			char *p;
    340 			size_t len;
    341 
    342 			if ((yylval.retval = malloc(sizeof(returns_t))) == NULL)
    343 				err(1, "Cannot allocate return struct");
    344 			p = yytext;
    345 			p++; /* skip the leading ' */
    346 			if ((yylval.retval->return_value = dequote(p, &len))
    347 			     == NULL)
    348 				err(1, "Cannot allocate string");
    349 
    350 			yylval.retval->return_type = ret_byte;
    351 			/* trim trailing ' */
    352 			yylval.retval->return_len = len - 1;
    353 			return BYTE;
    354 		}
    355 
    356 \`{STRING}\` 	{
    357 			char *p, *str;
    358 			size_t len, chlen;
    359 			int i;
    360 			chtype *rv;
    361 
    362 			if ((yylval.retval = malloc(sizeof(returns_t))) == NULL)
    363 				err(1, "Cannot allocate return struct");
    364 			p = yytext;
    365 			p++; /* skip the leading ' */
    366 			if ((str = dequote(p, &len)) == NULL)
    367 				err(1, "Cannot allocate string");
    368 			len--; /* trim trailing ` */
    369 			if ((len % 2) != 0)
    370 				len--;
    371 
    372 			chlen = ((len / 2) + 1) * sizeof(chtype);
    373 			if ((yylval.retval->return_value = malloc(chlen))
    374 			    == NULL)
    375 				err(1, "Cannot allocate chtype array");
    376 
    377 			rv = yylval.retval->return_value;
    378 			for (i = 0; i < len; i += 2)
    379 				*rv++ = (str[i] << 8) | str[i+1];
    380 			*rv = __NORMAL | '\0'; /* terminates chtype array */
    381 			yylval.retval->return_type = ret_byte;
    382 			yylval.retval->return_len = chlen;
    383 			return BYTE;
    384 		}
    385 
    386 \"{STRING}\" 	{
    387 			char *p;
    388 			size_t len;
    389 
    390 			p = yytext;
    391 			p++; /* skip the leading " */
    392 			if ((yylval.string = dequote(p, &len)) == NULL)
    393 				err(1, "Cannot allocate string");
    394 
    395 			/* remove trailing " */
    396 			yylval.string[len - 1] = '\0';
    397 			return STRING;
    398 		}
    399 
    400 \${VARNAME}	{
    401 			char *p;
    402 
    403 			p = yytext;
    404 			p++; /* skip $ before var name */
    405 			if ((yylval.string = strdup(p)) == NULL)
    406 				err(1, "Cannot allocate string for varname");
    407 			return VARIABLE;
    408 		}
    409 
    410 	/* comments, white-outs */
    411 [ \t\r]		|
    412 #.*		;
    413 ^#.*\n		|
    414 #.*\n		|
    415 \\\n		|
    416 ^\n		{
    417 line++; }
    418 
    419 	/* eol on a line with data. need to process, return eol */
    420 \n		{
    421 			line++;
    422 			return EOL;
    423 		}
    424 
    425 .		{
    426 		}
    427 
    428 %%
    429 
    430 int
    431 yywrap(void)
    432 {
    433 	return 1;
    434 }
    435