summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2014-09-07 11:11:32 -0400
committerAdrian Thurston <thurston@complang.org>2014-09-07 11:11:32 -0400
commit0494f274342db2881936ec0e790b041d9812c622 (patch)
tree20c36f25e3a87446522747ff8afc245073c98c90
parentb9b55a76f81c832bf0be62c80b20bf60ca332e96 (diff)
downloadcolm-0494f274342db2881936ec0e790b041d9812c622.tar.gz
do an open check in the include load
-rw-r--r--src/global.h1
-rw-r--r--src/loadcolm.cc4
-rw-r--r--src/main.cc20
3 files changed, 20 insertions, 5 deletions
diff --git a/src/global.h b/src/global.h
index 2dd6e4f1..553d580e 100644
--- a/src/global.h
+++ b/src/global.h
@@ -100,5 +100,6 @@ void checkMachines( );
void xmlEscapeHost( std::ostream &out, char *data, int len );
void openOutput();
void escapeLiteralString( std::ostream &out, const char *data );
+bool readCheck( const char *fn );
#endif
diff --git a/src/loadcolm.cc b/src/loadcolm.cc
index 39ea9944..ec98a666 100644
--- a/src/loadcolm.cc
+++ b/src/loadcolm.cc
@@ -751,6 +751,10 @@ struct LoadColm
String file = unescape( lit );
+ /* Check if we can open the input file for reading. */
+ if ( ! readCheck( file.data ) )
+ error() << "could not open " << file.data << " for reading" << endp;
+
const char *argv[3];
argv[0] = "load-include";
argv[1] = file.data;
diff --git a/src/main.cc b/src/main.cc
index c7f929f6..486d0e17 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -568,6 +568,19 @@ void processArgs( int argc, const char **argv )
}
}
+bool readCheck( const char *fn )
+{
+ int result = true;
+
+ /* Check if we can open the input file for reading. */
+ ifstream *inFile = new ifstream( fn );
+ if ( ! inFile->is_open() )
+ result = false;
+
+ delete inFile;
+ return result;
+}
+
/* Main, process args and call yyparse to start scanning input. */
int main(int argc, const char **argv)
{
@@ -588,18 +601,15 @@ int main(int argc, const char **argv)
"\" is the same as the input file" << endl;
}
-
#if defined(LOAD_INIT) || defined(LOAD_COLM)
/* Open the input file for reading. */
if ( inputFn == 0 ) {
error() << "colm: no input file given" << endl;
}
else {
- /* Open the input file for reading. */
- ifstream *inFile = new ifstream( inputFn );
- if ( ! inFile->is_open() )
+ /* Check if we can open the input file for reading. */
+ if ( ! readCheck( inputFn ) )
error() << "could not open " << inputFn << " for reading" << endl;
- delete inFile;
}
#endif