summaryrefslogtreecommitdiff
path: root/src/lexer.l
diff options
context:
space:
mode:
authorMarius Kintel <marius@kintel.net>2011-09-10 23:53:25 (GMT)
committerMarius Kintel <marius@kintel.net>2011-09-10 23:53:25 (GMT)
commitb087e68e5430c3dde6adfe452becbaba0f680196 (patch)
tree5772d63d6bbb5a976b576d077b600738457a59a3 /src/lexer.l
parent328897c1f28e0d438aa678891f8d5a45b114f267 (diff)
Don't leave files open on parse errors
Diffstat (limited to 'src/lexer.l')
-rw-r--r--src/lexer.l24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/lexer.l b/src/lexer.l
index 4c0ddea..3cd4a19 100644
--- a/src/lexer.l
+++ b/src/lexer.l
@@ -33,6 +33,7 @@
#include <QStack>
#include <QFileInfo>
#include <QDir>
+#include <assert.h>
//isatty for visual c++
#ifdef _MSC_VER
@@ -71,6 +72,7 @@ extern const char *parser_source_path;
void includefile();
QDir sourcepath();
QStack<QDir> path_stack;
+QStack<FILE*> openfiles;
QString filename;
QString filepath;
@@ -122,6 +124,7 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" {
if (!yyin) {
PRINTF("WARNING: Can't open input file `%s'.", filename);
} else {
+ openfiles.append(yyin);
yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE ));
BEGIN(INITIAL);
}
@@ -129,10 +132,12 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" {
}
<<EOF>> {
- if(!path_stack.isEmpty())
+ if(!path_stack.empty())
path_stack.pop();
- if (yyin && yyin != stdin)
- fclose(yyin);
+ if (yyin && yyin != stdin) {
+ assert(!openfiles.empty());
+ fclose(openfiles.pop());
+ }
yypop_buffer_state();
if (!YY_CURRENT_BUFFER)
yyterminate();
@@ -183,7 +188,7 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" {
QDir sourcepath()
{
- if(!path_stack.isEmpty())
+ if(!path_stack.empty())
return path_stack.top();
return QDir(parser_source_path);
@@ -214,8 +219,19 @@ void includefile()
path_stack.pop();
return;
}
+ openfiles.append(yyin);
filename.clear();
yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE ));
}
+/*!
+ In case of an error, this will make sure we clean up our custom data structures
+ and close all files.
+*/
+void lexerdestroy()
+{
+ foreach (FILE *f, openfiles) fclose(f);
+ openfiles.clear();
+ path_stack.clear();
+}
contact: Jan Huwald // Impressum