From 034fb89db3a39529bc1c38b9f83947cf2ad53114 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Mon, 15 May 2017 11:06:47 +0200 Subject: [PATCH] Abort with a clear error message for invalid uses of --config inside a config file The options.cxx code is not ready to handle recursive use of --config (for config files). Instead of failing in an ugly way, abort with a clear error message in such situations. See discussion at . Note: it *is* possible to load XML PropertyList files from config files, so --config is not entirely "banned" from config files. + add missing include --- src/Main/options.cxx | 17 +++++++++++++---- src/Main/options.hxx | 6 ++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Main/options.cxx b/src/Main/options.cxx index 4f3120f90..5a97e20b2 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -39,6 +39,7 @@ #include // atof(), atoi() #include // strcmp() #include +#include #include #include @@ -2020,7 +2021,7 @@ void Options::init(int argc, char **argv, const SGPath& appDataPath) continue; } - int result = parseOption(argv[i]); + int result = parseOption(argv[i], /* fromConfigFile */ false); processArgResult(result); } else { // XML properties file @@ -2231,7 +2232,7 @@ void Options::readConfig(const SGPath& path) break; line = line.substr( 0, i ); - if ( parseOption( line ) == FG_OPTIONS_ERROR ) { + if ( parseOption(line, /* fromConfigFile */ true) == FG_OPTIONS_ERROR ) { cerr << endl << "Config file parse error: " << path << " '" << line << "'" << endl; p->showHelp = true; @@ -2242,7 +2243,7 @@ void Options::readConfig(const SGPath& path) p->insertGroupMarker(); // each config file is a group } -int Options::parseOption(const string& s) +int Options::parseOption(const string& s, bool fromConfigFile) { if ((s == "--help") || (s=="-h")) { return FG_OPTIONS_HELP; @@ -2276,7 +2277,15 @@ int Options::parseOption(const string& s) SGPath path = s.substr(9); if (path.extension() == "xml") { p->propertyFiles.push_back(path); - } else { + } else if (fromConfigFile) { + flightgear::fatalMessageBoxThenExit( + "FlightGear", + "Invalid use of the --config option.", + "Sorry, it is currently not supported to load a configuration file " + "using --config from another configuration file.\n\n" + "Note: this does not apply to loading of XML PropertyList files " + "with --config."); + } else { // the --config option comes from the command line p->configFiles.push_back(path); } diff --git a/src/Main/options.hxx b/src/Main/options.hxx index ad8d848bf..ee3434a99 100644 --- a/src/Main/options.hxx +++ b/src/Main/options.hxx @@ -171,8 +171,10 @@ private: // paths, etc. to stdout in JSON format, using the UTF-8 encoding. void printJSONReport() const; - int parseOption(const std::string& s); - + // The 'fromConfigFile' parameter indicates whether the option comes from a + // config file or directly from the command line. + int parseOption(const std::string& s, bool fromConfigFile); + void processArgResult(int result); /**