pgc.l 17.4 KB
Newer Older
M
Marc G. Fournier 已提交
1
/* This is a modified version of src/backend/parser/scan.l */
2
%{
3
#include "config.h"
M
Marc G. Fournier 已提交
4 5

#include <ctype.h>
M
Marc G. Fournier 已提交
6
#include <sys/types.h>
7
#include <limits.h>
8 9 10 11 12 13

#ifndef PATH_MAX
#include <sys/param.h>
#define PATH_MAX MAXPATHLEN
#endif

14 15 16 17 18
#if defined(HAVE_STRING_H)
#include <string.h>
#else
#include <strings.h>
#endif
M
Marc G. Fournier 已提交
19
#include <errno.h>
20

M
Marc G. Fournier 已提交
21 22 23 24 25 26
#include "postgres.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "nodes/parsenodes.h"
#include "parser/gramparse.h"
#include "parser/scansup.h"
27
#include "type.h"
M
 
Marc G. Fournier 已提交
28
#include "extern.h"
29
#include "y.tab.h"
M
Marc G. Fournier 已提交
30
#include "utils/builtins.h"
31

M
Marc G. Fournier 已提交
32 33 34 35 36 37 38 39
/* some versions of lex define this as a macro */
#if defined(yywrap)
#undef yywrap
#endif /* yywrap */

extern YYSTYPE yylval;
int llen;
char literal[MAX_PARSE_BUFFER];
40
int before_comment;
M
Marc G. Fournier 已提交
41

42 43 44 45 46 47
struct _yy_buffer { YY_BUFFER_STATE 	buffer;
		    long		lineno;
		    char	      * filename;
		    struct _yy_buffer * next;
		  } *yy_buffer = NULL;

M
 
Marc G. Fournier 已提交
48 49 50
struct _defines *defines = NULL;
static char *old;

51
%}
52
%option yylineno
M
 
Marc G. Fournier 已提交
53
%s C SQL incl def def_ident
M
Marc G. Fournier 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* OK, here is a short description of lex/flex rules behavior.
 * The longest pattern which matches an input string is always chosen.
 * For equal-length patterns, the first occurring in the rules list is chosen.
 * INITIAL is the starting condition, to which all non-conditional rules apply.
 * When in an exclusive condition, only those rules defined for that condition apply.
 *
 * Exclusive states change parsing rules while the state is active.
 * There are exclusive states for quoted strings, extended comments,
 *  and to eliminate parsing troubles for numeric strings.
 * Exclusive states:
 *  <xb> binary numeric string - thomas 1997-11-16
 *  <xc> extended C-style comments - tgl 1997-07-12
 *  <xd> delimited identifiers (double-quoted identifiers) - tgl 1997-10-27
 *  <xh> hexadecimal numeric string - thomas 1997-11-16
 *  <xm> numeric strings with embedded minus sign - tgl 1997-09-05
 *  <xq> quoted strings - tgl 1997-07-30
 *
 * The "extended comment" syntax closely resembles allowable operator syntax.
 * So, when in condition <xc>, only strings which would terminate the
 *  "extended comment" trigger any action other than "ignore".
 * Be sure to match _any_ candidate comment, including those with appended
 *	operator-like symbols. - thomas 1997-07-14
 */

%x xb
%x xc
%x xd
81
%x xdc
M
Marc G. Fournier 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
%x xh
%x xm
%x xq

/* Binary number
 */
xbstart			[bB]{quote}
xbstop			{quote}
xbinside		[^']*
xbcat			{quote}{space}*\n{space}*{quote}

/* Hexadecimal number
 */
xhstart			[xX]{quote}
xhstop			{quote}
xhinside		[^']*
xhcat			{quote}{space}*\n{space}*{quote}

/* Extended quote
 * xqdouble implements SQL92 embedded quote
 * xqcat allows strings to cross input lines
 */
quote			'
xqstart			{quote}
xqstop			{quote}
xqdouble		{quote}{quote}
xqinside		[^\\']*
xqembedded		"\\'"
xqliteral		[\\](.|\n)
xqcat			{quote}{space}*\n{space}*{quote}

/* Delimited quote
 * Allows embedded spaces and other special characters into identifiers.
 */
dquote			\"
xdstart			{dquote}
xdstop			{dquote}
xdinside		[^"]*

/* Comments
 * Ignored by the scanner and parser.
 */
xcline			[\/][\*].*[\*][\/]{space}*\n*
xcstart			[\/][\*]{op_and_self}*
xcstop			{op_and_self}*[\*][\/]({space}*|\n)
xcinside		[^*]*
xcstar			[^/]

digit			[0-9]
number			[-+.0-9Ee]
letter			[\200-\377_A-Za-z]
letter_or_digit	[\200-\377_A-Za-z0-9]

identifier		{letter}{letter_or_digit}*

typecast		"::"

self			[,()\[\].$\:\+\-\*\/\<\>\=\|]
op_and_self		[\~\!\@\#\%\^\&\|\`\?\$\:\+\-\*\/\<\>\=]
operator		{op_and_self}+

xmstop			-

M
 
Marc G. Fournier 已提交
145 146 147 148 149
integer			[\-]?{digit}+
/*
real			[\-]?{digit}+\.{digit}+([Ee][-+]?{digit}+)?
*/
real			[\-]?(((({digit}*\.{digit}+)|({digit}+\.{digit}*))([Ee][-+]?{digit}+)?)|({digit}+[Ee][-+]?{digit}+))
M
Marc G. Fournier 已提交
150 151 152 153 154 155 156 157 158

param			\${integer}

comment			("--"|"//").*\n

space			[ \t\n\f]
other			.

/* some stuff needed for ecpg */
159
ccomment	"//".*\n
M
Marc G. Fournier 已提交
160
exec    [eE][xX][eE][cC]
M
 
Marc G. Fournier 已提交
161
define	[dD][eE][fF][iI][nN][eE]
162
include [iI][nN][cC][lL][uU][dD][eE]
M
Marc G. Fournier 已提交
163 164
sql     [sS][qQ][lL]

M
 
Marc G. Fournier 已提交
165 166
cppline		{space}*#.*(\\{space}*\n)*\n*

M
Marc G. Fournier 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179
/* DO NOT PUT ANY COMMENTS IN THE FOLLOWING SECTION.
 * AT&T lex does not properly handle C-style comments in this second lex block.
 * So, put comments here. tgl - 1997-09-08
 *
 * Quoted strings must allow some special characters such as single-quote
 *  and newline.
 * Embedded single-quotes are implemented both in the SQL/92-standard
 *  style of two adjacent single quotes "''" and in the Postgres/Java style
 *  of escaped-quote "\'".
 * Other embedded escaped characters are matched explicitly and the leading
 *  backslash is dropped from the string. - thomas 1997-09-24
 */

180
%%
M
Marc G. Fournier 已提交
181 182
<SQL>{comment}		{ /* ignore */ }

183
{xcline}		{ /* ignore */ }
M
Marc G. Fournier 已提交
184

185 186 187 188 189
<xc>{xcstar}		{ /* ignore */ }
{xcstart}		{
				before_comment = YYSTATE;
	 			BEGIN(xc);
			}
M
Marc G. Fournier 已提交
190

M
 
Marc G. Fournier 已提交
191
<xc>{xcstop}	{ BEGIN(before_comment); }
M
Marc G. Fournier 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

<xc>{xcinside}	{ /* ignore */ }

<SQL>{xbstart}		{
					BEGIN(xb);
					llen = 0;
					*literal = '\0';
				}
<xb>{xbstop}	{
					char* endptr;

					BEGIN(SQL);
					errno = 0;
					yylval.ival = strtol((char *)literal,&endptr,2);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad binary integer input!");
208
					return ICONST;
M
Marc G. Fournier 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
				}
<xh>{xhinside}	|
<xb>{xbinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
<xh>{xhcat}		|
<xb>{xbcat}		{
				}

<SQL>{xhstart}		{
					BEGIN(xh);
					llen = 0;
					*literal = '\0';
				}
<xh>{xhstop}	{
					char* endptr;

					BEGIN(SQL);
					errno = 0;
					yylval.ival = strtol((char *)literal,&endptr,16);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad hexadecimal integer input");
234
					return ICONST;
M
Marc G. Fournier 已提交
235 236 237 238 239 240 241 242 243 244
				}

<SQL>{xqstart}		{
					BEGIN(xq);
					llen = 0;
					*literal = '\0';
				}
<xq>{xqstop}	{
					BEGIN(SQL);
					yylval.str = strdup(scanstr(literal));
245
					return SCONST;
M
Marc G. Fournier 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
				}
<xq>{xqdouble}	|
<xq>{xqinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
<xq>{xqembedded} {
					if ((llen+yyleng-1) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					*(literal+llen) = '\'';
					llen += yyleng;
				}

<xq>{xqliteral} {
					if ((llen+yyleng-1) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
<xq>{xqcat}		{
				}


<SQL>{xdstart}		{
					BEGIN(xd);
					llen = 0;
					*literal = '\0';
				}
<xd>{xdstop}	{
					BEGIN(SQL);
					yylval.str = strdup(literal);
280
					return CSTRING;
M
Marc G. Fournier 已提交
281 282 283 284 285 286 287
				}
<xd>{xdinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
M
 
Marc G. Fournier 已提交
288
{xdstart}		{
289 290 291 292 293 294 295
					BEGIN(xdc);
					llen = 0;
					*literal = '\0';
				}
<xdc>{xdstop}	{
					BEGIN(C);
					yylval.str = strdup(literal);
296
					return CSTRING;
297 298 299 300 301 302 303
				}
<xdc>{xdinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
M
Marc G. Fournier 已提交
304 305 306 307

<xm>{space}*	{ /* ignore */ }
<xm>{xmstop}	{
					BEGIN(SQL);
308
					return yytext[0];
M
Marc G. Fournier 已提交
309 310 311 312
				}


<SQL>{typecast}			{ 	return TYPECAST; }
M
 
Marc G. Fournier 已提交
313 314
<SQL>{self}/{space}*-[\.0-9]	{
					BEGIN(xm);
315
					return yytext[0];
M
Marc G. Fournier 已提交
316
				}
317
<SQL>{self}				{ 	return yytext[0]; }
M
Marc G. Fournier 已提交
318 319
<SQL>{operator}/-[\.0-9]	{
					yylval.str = strdup((char*)yytext);
320
					return Op;
M
Marc G. Fournier 已提交
321 322 323 324 325 326
				}
<SQL>{operator}		{
					if (strcmp((char*)yytext,"!=") == 0)
						yylval.str = strdup("<>"); /* compatability */
					else
						yylval.str = strdup((char*)yytext);
327
					return Op;
M
Marc G. Fournier 已提交
328 329 330
				}
<SQL>{param}			{
					yylval.ival = atoi((char*)&yytext[1]);
331
					return PARAM;
M
Marc G. Fournier 已提交
332 333 334 335
				}
<SQL>{identifier}/{space}*-{number}	{
					int i;
					ScanKeyword		*keyword;
M
 
Marc G. Fournier 已提交
336
					char lower_text[NAMEDATALEN];
M
Marc G. Fournier 已提交
337 338

					BEGIN(xm);
M
 
Marc G. Fournier 已提交
339 340 341 342 343 344 345 346
					/* this should leave the last byte set to '\0' */
					strncpy(lower_text, yytext, NAMEDATALEN-1);
					for(i = 0; lower_text[i]; i++)
						if (isascii((unsigned char)lower_text[i]) && isupper(lower_text[i]))
							lower_text[i] = tolower(lower_text[i]);

printf("yyt= %s, lt = %s\n", yytext, lower_text);
					keyword = ScanKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
347
					if (keyword != NULL) {
348
						return keyword->value;
M
Marc G. Fournier 已提交
349 350 351
					}
					else
					{
M
 
Marc G. Fournier 已提交
352
						keyword = ScanECPGKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
353
						if (keyword != NULL) {
354
							return keyword->value;
M
Marc G. Fournier 已提交
355 356 357
						}
						else
						{
M
 
Marc G. Fournier 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
							struct _defines *ptr;

							for (ptr = defines; ptr; ptr = ptr->next)
							{
								if (strcmp(yytext, ptr->old) == 0)
								{
									struct _yy_buffer *yb;

									yb = mm_alloc(sizeof(struct _yy_buffer));

						                        yb->buffer =  YY_CURRENT_BUFFER;
						                        yb->lineno = yylineno;
						                        yb->filename = strdup(input_filename);
						                        yb->next = yy_buffer;

						                        yy_buffer = yb;

 									yy_scan_string(ptr->new);
									break;
								}
							}
							if (ptr == NULL) 
							{
								yylval.str = strdup((char*)yytext);
382
								return IDENT;
M
 
Marc G. Fournier 已提交
383
							}
M
Marc G. Fournier 已提交
384 385 386
						}
					}
				}
M
 
Marc G. Fournier 已提交
387
<SQL>{integer}/{space}*-{number}	{
M
Marc G. Fournier 已提交
388 389 390 391 392 393 394 395 396 397 398 399
					char* endptr;

					BEGIN(xm);
					errno = 0;
					yylval.ival = strtol((char *)yytext,&endptr,10);
					if (*endptr != '\0' || errno == ERANGE)
					{
						errno = 0;
						yylval.dval = strtod(((char *)yytext),&endptr);
						if (*endptr != '\0' || errno == ERANGE)
							yyerror("ERROR: Bad integer input");
						yyerror("WARNING: Integer input is out of range; promoted to float");
400
						return FCONST;
M
Marc G. Fournier 已提交
401
					}
402
					return ICONST;
M
Marc G. Fournier 已提交
403
				}
M
 
Marc G. Fournier 已提交
404
<SQL>{real}/{space}*-{number} {
M
Marc G. Fournier 已提交
405 406 407 408 409 410 411
					char* endptr;

					BEGIN(xm);
					errno = 0;
					yylval.dval = strtod(((char *)yytext),&endptr);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad float8 input");
412
					return FCONST;
M
Marc G. Fournier 已提交
413
				}
M
 
Marc G. Fournier 已提交
414
<SQL>{integer}		{
M
Marc G. Fournier 已提交
415 416 417 418 419 420 421 422 423 424 425
					char* endptr;

					errno = 0;
					yylval.ival = strtol((char *)yytext,&endptr,10);
					if (*endptr != '\0' || errno == ERANGE)
					{
						errno = 0;
						yylval.dval = strtod(((char *)yytext),&endptr);
						if (*endptr != '\0' || errno == ERANGE)
							yyerror("ERROR: Bad integer input");
						yyerror("WARNING: Integer input is out of range; promoted to float");
426
						return FCONST;
M
Marc G. Fournier 已提交
427
					}
428
					return ICONST;
M
Marc G. Fournier 已提交
429
				}
M
 
Marc G. Fournier 已提交
430
<SQL>{real}			{
M
Marc G. Fournier 已提交
431 432 433 434 435 436
					char* endptr;

					errno = 0;
					yylval.dval = strtod((char *)yytext,&endptr);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad float input");
437
					return FCONST;
M
Marc G. Fournier 已提交
438
				}
M
 
Marc G. Fournier 已提交
439 440
<C>{integer}/{space}*-{number}	{
					char* endptr;
M
Marc G. Fournier 已提交
441

M
 
Marc G. Fournier 已提交
442 443 444 445 446 447 448 449 450 451
					BEGIN(xm);
					errno = 0;
					yylval.ival = strtol((char *)yytext,&endptr,10);
					if (*endptr != '\0' || errno == ERANGE)
					{
						errno = 0;
						yylval.dval = strtod(((char *)yytext),&endptr);
						if (*endptr != '\0' || errno == ERANGE)
							yyerror("ERROR: Bad integer input");
						yyerror("WARNING: Integer input is out of range; promoted to float");
452
						return FCONST;
M
 
Marc G. Fournier 已提交
453
					}
454
					return ICONST;
M
 
Marc G. Fournier 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467
				}
<C>{integer}		{
					char* endptr;

					errno = 0;
					yylval.ival = strtol((char *)yytext,&endptr,10);
					if (*endptr != '\0' || errno == ERANGE)
					{
						errno = 0;
						yylval.dval = strtod(((char *)yytext),&endptr);
						if (*endptr != '\0' || errno == ERANGE)
							yyerror("ERROR: Bad integer input");
						yyerror("WARNING: Integer input is out of range; promoted to float");
468
						return FCONST;
M
 
Marc G. Fournier 已提交
469
					}
470
					return ICONST;
M
 
Marc G. Fournier 已提交
471
				}
472
<SQL>:{identifier}(("->"|\.){identifier})*	{
473 474 475
					yylval.str = strdup((char*)yytext+1);
					return(CVARIABLE);
			}
M
Marc G. Fournier 已提交
476 477 478
<SQL>{identifier}	{
					int i;
					ScanKeyword		*keyword;
M
 
Marc G. Fournier 已提交
479
					char lower_text[NAMEDATALEN];
M
Marc G. Fournier 已提交
480

M
 
Marc G. Fournier 已提交
481 482 483 484 485
					/* this should leave the last byte set to '\0' */
					strncpy(lower_text, yytext, NAMEDATALEN-1);
					for(i = 0; lower_text[i]; i++)
						if (isascii((unsigned char)lower_text[i]) && isupper(lower_text[i]))
							lower_text[i] = tolower(lower_text[i]);
M
 
Marc G. Fournier 已提交
486

M
 
Marc G. Fournier 已提交
487 488
printf("yyt= %s, lt = %s\n", yytext, lower_text);
					keyword = ScanKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
489
					if (keyword != NULL) {
490
						return keyword->value;
M
Marc G. Fournier 已提交
491 492 493
					}
					else
					{
M
 
Marc G. Fournier 已提交
494
						keyword = ScanECPGKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
495
						if (keyword != NULL) {
496
							return keyword->value;
M
Marc G. Fournier 已提交
497 498 499
						}
						else
						{
M
 
Marc G. Fournier 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
							struct _defines *ptr;

							for (ptr = defines; ptr; ptr = ptr->next)
							{
								if (strcmp(yytext, ptr->old) == 0)
								{
									struct _yy_buffer *yb;

									yb = mm_alloc(sizeof(struct _yy_buffer));

						                        yb->buffer =  YY_CURRENT_BUFFER;
						                        yb->lineno = yylineno;
						                        yb->filename = strdup(input_filename);
						                        yb->next = yy_buffer;

						                        yy_buffer = yb;

 									yy_scan_string(ptr->new);
									break;
								}
							}
							if (ptr == NULL) 
							{
								yylval.str = strdup((char*)yytext);
524
								return IDENT;
M
 
Marc G. Fournier 已提交
525
							}
M
Marc G. Fournier 已提交
526 527 528 529 530
						}
					}
				}
<SQL>{space}			{ /* ignore */ }
<SQL>";"	                { BEGIN C; return SQL_SEMI; }
531
<SQL>{other}			{ return yytext[0]; }
M
Marc G. Fournier 已提交
532
<C>{exec}{space}{sql}		{ BEGIN SQL; return SQL_START; }
533
<C>{ccomment}			{ /* ignore */ } 
M
 
Marc G. Fournier 已提交
534 535 536 537
<C>{cppline}			{
					yylval.str = strdup((char*)yytext);
					return(CPP_LINE);
				}
M
Marc G. Fournier 已提交
538 539 540 541 542
<C>{identifier}	{
					ScanKeyword		*keyword;

					keyword = ScanCKeywordLookup((char*)yytext);
					if (keyword != NULL) {
543
						return keyword->value;
M
Marc G. Fournier 已提交
544 545 546
					}
					else
					{
M
 
Marc G. Fournier 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
						struct _defines *ptr;

						for (ptr = defines; ptr; ptr = ptr->next)
						{
							if (strcmp(yytext, ptr->old) == 0)
							{
								struct _yy_buffer *yb;

								yb = mm_alloc(sizeof(struct _yy_buffer));

					                        yb->buffer =  YY_CURRENT_BUFFER;
					                        yb->lineno = yylineno;
					                        yb->filename = strdup(input_filename);
					                        yb->next = yy_buffer;

					                        yy_buffer = yb;

								yy_scan_string(ptr->new);
								break;
							}
						}
						if (ptr == NULL) 
						{
							yylval.str = strdup((char*)yytext);
571
							return IDENT;
M
 
Marc G. Fournier 已提交
572
						}
M
Marc G. Fournier 已提交
573 574 575
					}
				}
<C>";"	      	        { return(';'); }
576 577
<C>","	      	        { return(','); }
<C>"*"	      	        { return('*'); }
M
Marc G. Fournier 已提交
578
<C>{space}		{ ECHO; }
579 580 581 582 583
<C>\{			{ return('{'); }
<C>\}			{ return('}'); }
<C>\[			{ return('['); }
<C>\]			{ return(']'); }
<C>\=			{ return('='); }
584
<C>{other}			{ return S_ANYTHING; }
M
 
Marc G. Fournier 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
<C>{exec}{space}{sql}{space}{define}	{BEGIN(def_ident);}
<def_ident>{space}	{}
<def_ident>{identifier}	{
				old = strdup(yytext);
				BEGIN(def);
				llen = 0;
				*literal = '\0';
			}
<def>{space}		/* eat the whitespace */
<def>";"		{
				struct _defines *ptr, *this;
        
                                for (ptr = defines; ptr != NULL; ptr = ptr->next)
                                {
                                     if (strcmp(old, ptr->old) == 0)
                                     {
					free(ptr->new);
					ptr->new = strdup(scanstr(literal));
                                     }
                                }
				if (ptr == NULL)
				{                        
                                        this = (struct _defines *) mm_alloc(sizeof(struct _defines));

                                        /* initial definition */
                                        this->old = old;
                                        this->new = strdup(scanstr(literal));
					this->next = defines;
					defines = this;
				}

				BEGIN(C);
			}
<def>[^";"]		{
				if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
					yyerror("ERROR: define statement parse buffer exceeded");
				memcpy(literal+llen, yytext, yyleng+1);
				llen += yyleng;
			}
M
Marc G. Fournier 已提交
624 625
<C>{exec}{space}{sql}{space}{include}	{ BEGIN(incl); }
<incl>{space}		/* eat the whitespace */
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
<incl>[^ \t\n]+ 	{ /* got the include file name */
			  struct _yy_buffer *yb;
			  struct _include_path *ip;
			  char inc_file[PATH_MAX];

			  yb = mm_alloc(sizeof(struct _yy_buffer));

			  yb->buffer =  YY_CURRENT_BUFFER;
			  yb->lineno = yylineno;
			  yb->filename = input_filename;
			  yb->next = yy_buffer;

			  yy_buffer = yb;

			  if (yytext[strlen(yytext) - 1] == ';')
				yytext[strlen(yytext) - 1] = '\0';

			  yyin = NULL;
			  for (ip = include_paths; yyin == NULL && ip != NULL; ip = ip->next)
			  {
				if (strlen(ip->path) + strlen(yytext) + 3 > PATH_MAX)
				{
M
Marc G. Fournier 已提交
648
					fprintf(stderr, "Error: Path %s/%s is too long in line %d, skipping.\n", ip->path, yytext, yylineno);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
					continue;
				}
			  	sprintf (inc_file, "%s/%s", ip->path, yytext);
		          	yyin = fopen( inc_file, "r" );
			  	if (!yyin)
				{
					if (strcmp(inc_file + strlen(inc_file) - 2, ".h"))
					{
						strcat(inc_file, ".h");
						yyin = fopen( inc_file, "r" );
					}

				}
			  }
			  if (!yyin)
			  {
M
Marc G. Fournier 已提交
665
				fprintf(stderr, "Error: Cannot open include file %s in line %d\n", yytext, yylineno);
666
				exit(NO_INCLUDE_FILE); 
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
			  }

			  input_filename = strdup(inc_file);
			  yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE ));
			  yylineno = 0;

			  BEGIN C;
			}
<incl>";"		{ BEGIN C; }
<<EOF>>			{ if (yy_buffer == NULL)
				yyterminate();
			  else
			  {
				struct _yy_buffer *yb = yy_buffer;

				if (yyin != NULL)
					fclose(yyin);

				yy_delete_buffer( YY_CURRENT_BUFFER );
				yy_switch_to_buffer(yy_buffer->buffer);

				yylineno = yy_buffer->lineno;

				free(input_filename);
				input_filename = yy_buffer->filename;

				yy_buffer = yy_buffer->next;
				free(yb);
			  }
			}

698 699
%%
void
700
lex_init(void)
701
{
702
    braces_open = 0;
703 704 705
    BEGIN C;
}

706
int yywrap(void) 
707 708 709
{ 
    return 1;
}