diff --git a/src/scanner.l b/src/scanner.l index 2f5b6db..e8c4f10 100644 --- a/src/scanner.l +++ b/src/scanner.l @@ -7,6 +7,7 @@ #include "pheading.h" #include "memory.h" #include "tac.h" +#include "switches.h" /* tokens for language */ #include "parser.h" @@ -47,7 +48,38 @@ integer {digit}+ text \"({ascii_char}|{escaped_char})*\" id @?({letter}|{digit}|[\^\-])+ +/* the "incl" state is used for picking up the name of an include file + */ +%x incl inclend %% +include BEGIN(incl); +[ \t]*\" /* eat the whitespace */ +[^\"]+ { /* got the include file name */ + /* try to open, using scytherdirs environment variable as well. */ + yyin = openFileSearch (yytext, NULL); + if (! yyin) + { + error ("Could not open include file %s.", yytext); + } + yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE )); + BEGIN(INITIAL); + } + +\";? { /* eat the closing things */ + BEGIN(INITIAL); + } + +<> { + yypop_buffer_state(); + if (! YY_CURRENT_BUFFER ) + { + yyterminate(); + } + else + { + BEGIN(inclend); + } + } "/*" { register int c; diff --git a/src/switches.c b/src/switches.c index f881898..19c30e9 100644 --- a/src/switches.c +++ b/src/switches.c @@ -92,12 +92,14 @@ switchesDone (void) { } -//! Open a (protocol) file instead of stdin +//! Open a (protocol) file. /** * Uses the environment variable SCYTHERDIR to also search for files + * + * If reopener == NULL then we open a new file pointer, otherwise reopen this one. */ -int -openFileStdin (char *filename) +FILE * +openFileSearch (char *filename, FILE * reopener) { const char *separators = ":;\n"; char *dirs; @@ -139,9 +141,20 @@ openFileStdin (char *filename) } // Now try to open it - if (freopen (buffer, "r", stdin) != NULL) + if (reopener != NULL) { - result = true; + if (freopen (buffer, "r", reopener) != NULL) + { + result = true; + } + } + else + { + reopener = fopen (buffer, "r"); + if (reopener != NULL) + { + result = true; + } } free (buffer); @@ -152,7 +165,7 @@ openFileStdin (char *filename) if (try ("")) { - return true; + return reopener; } // Now try the environment variable @@ -164,7 +177,7 @@ openFileStdin (char *filename) // try this one if (try (dirs)) { - return true; + return reopener; } // skip to next dirs = strpbrk (dirs, separators); @@ -181,7 +194,21 @@ openFileStdin (char *filename) } // Nope - return false; + return NULL; +} + +//! Open a (protocol) file instead of stdin +int +openFileStdin (char *filename) +{ + if (openFileSearch (filename, stdin) == NULL) + { + return false; + } + else + { + return true; + } } //! Process a single switch or generate help text diff --git a/src/switches.h b/src/switches.h index babe7e5..8d0738f 100644 --- a/src/switches.h +++ b/src/switches.h @@ -61,4 +61,6 @@ struct switchdata extern struct switchdata switches; //!< pointer to switchdata structure +FILE *openFileSearch (char *filename, FILE * reopener); + #endif