config/ConfigFile: use std::exception on syntax error
This commit is contained in:
17
src/Main.cxx
17
src/Main.cxx
@@ -444,11 +444,11 @@ int mpd_main(int argc, char *argv[])
|
||||
io_thread_init();
|
||||
config_global_init();
|
||||
|
||||
try {
|
||||
#ifdef ANDROID
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
{
|
||||
const auto sdcard = Environment::getExternalStorageDirectory();
|
||||
if (!sdcard.IsNull()) {
|
||||
const auto config_path =
|
||||
@@ -459,13 +459,16 @@ int mpd_main(int argc, char *argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (!parse_cmdline(argc, argv, &options, error)) {
|
||||
LogError(error);
|
||||
if (!parse_cmdline(argc, argv, &options, error)) {
|
||||
LogError(error);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#endif
|
||||
} catch (const std::exception &e) {
|
||||
LogError(e);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_DAEMON
|
||||
if (!glue_daemonize_init(&options, error)) {
|
||||
|
@@ -27,6 +27,7 @@
|
||||
#include "util/StringUtil.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
#include "util/RuntimeError.hxx"
|
||||
#include "fs/Path.hxx"
|
||||
#include "fs/io/FileReader.hxx"
|
||||
#include "fs/io/BufferedReader.hxx"
|
||||
@@ -54,27 +55,20 @@ config_read_name_value(ConfigBlock &block, char *input, unsigned line,
|
||||
|
||||
const char *value = tokenizer.NextString(error);
|
||||
if (value == nullptr) {
|
||||
if (tokenizer.IsEnd()) {
|
||||
error.Set(config_file_domain, "Value missing");
|
||||
} else {
|
||||
assert(error.IsDefined());
|
||||
}
|
||||
if (tokenizer.IsEnd())
|
||||
throw std::runtime_error("Value missing");
|
||||
|
||||
assert(error.IsDefined());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) {
|
||||
error.Set(config_file_domain, "Unknown tokens after value");
|
||||
return false;
|
||||
}
|
||||
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT)
|
||||
throw std::runtime_error("Unknown tokens after value");
|
||||
|
||||
const BlockParam *bp = block.GetBlockParam(name);
|
||||
if (bp != nullptr) {
|
||||
error.Format(config_file_domain,
|
||||
"\"%s\" is duplicate, first defined on line %i",
|
||||
name, bp->line);
|
||||
return false;
|
||||
}
|
||||
if (bp != nullptr)
|
||||
throw FormatRuntimeError("\"%s\" is duplicate, first defined on line %i",
|
||||
name, bp->line);
|
||||
|
||||
block.AddBlockParam(name, value, line);
|
||||
return true;
|
||||
@@ -82,15 +76,14 @@ config_read_name_value(ConfigBlock &block, char *input, unsigned line,
|
||||
|
||||
static ConfigBlock *
|
||||
config_read_block(BufferedReader &reader, Error &error)
|
||||
{
|
||||
try {
|
||||
std::unique_ptr<ConfigBlock> block(new ConfigBlock(reader.GetLineNumber()));
|
||||
|
||||
while (true) {
|
||||
char *line = reader.ReadLine();
|
||||
if (line == nullptr) {
|
||||
if (reader.Check(error))
|
||||
error.Set(config_file_domain,
|
||||
"Expected '}' before end-of-file");
|
||||
throw std::runtime_error("Expected '}' before end-of-file");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -103,12 +96,8 @@ config_read_block(BufferedReader &reader, Error &error)
|
||||
(and from this "while" loop) */
|
||||
|
||||
line = StripLeft(line + 1);
|
||||
if (*line != 0 && *line != CONF_COMMENT) {
|
||||
error.Format(config_file_domain,
|
||||
"line %u: Unknown tokens after '}'",
|
||||
reader.GetLineNumber());
|
||||
return nullptr;
|
||||
}
|
||||
if (*line != 0 && *line != CONF_COMMENT)
|
||||
throw std::runtime_error("Unknown tokens after '}'");
|
||||
|
||||
return block.release();
|
||||
}
|
||||
@@ -123,6 +112,8 @@ config_read_block(BufferedReader &reader, Error &error)
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
} catch (...) {
|
||||
std::throw_with_nested(FormatRuntimeError("Error in line %u", reader.GetLineNumber()));
|
||||
}
|
||||
|
||||
gcc_nonnull_all
|
||||
@@ -150,30 +141,22 @@ ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
|
||||
|
||||
if (head != nullptr && !option.repeatable) {
|
||||
ConfigBlock *block = head;
|
||||
error.Format(config_file_domain,
|
||||
"config parameter \"%s\" is first defined "
|
||||
"on line %d and redefined on line %u\n",
|
||||
name, block->line,
|
||||
reader.GetLineNumber());
|
||||
return false;
|
||||
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
||||
"on line %d and redefined on line %u\n",
|
||||
name, block->line,
|
||||
reader.GetLineNumber());
|
||||
}
|
||||
|
||||
/* now parse the block or the value */
|
||||
|
||||
if (tokenizer.CurrentChar() != '{') {
|
||||
error.Format(config_file_domain,
|
||||
"line %u: '{' expected",
|
||||
reader.GetLineNumber());
|
||||
return false;
|
||||
}
|
||||
if (tokenizer.CurrentChar() != '{')
|
||||
throw FormatRuntimeError("line %u: '{' expected",
|
||||
reader.GetLineNumber());
|
||||
|
||||
char *line = StripLeft(tokenizer.Rest() + 1);
|
||||
if (*line != 0 && *line != CONF_COMMENT) {
|
||||
error.Format(config_file_domain,
|
||||
"line %u: Unknown tokens after '{'",
|
||||
reader.GetLineNumber());
|
||||
return false;
|
||||
}
|
||||
if (*line != 0 && *line != CONF_COMMENT)
|
||||
throw FormatRuntimeError("line %u: Unknown tokens after '{'",
|
||||
reader.GetLineNumber());
|
||||
|
||||
auto *param = config_read_block(reader, error);
|
||||
if (param == nullptr)
|
||||
@@ -208,12 +191,10 @@ ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
|
||||
|
||||
if (head != nullptr && !option.repeatable) {
|
||||
struct config_param *param = head;
|
||||
error.Format(config_file_domain,
|
||||
"config parameter \"%s\" is first defined "
|
||||
"on line %d and redefined on line %u\n",
|
||||
name, param->line,
|
||||
reader.GetLineNumber());
|
||||
return false;
|
||||
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
||||
"on line %d and redefined on line %u\n",
|
||||
name, param->line,
|
||||
reader.GetLineNumber());
|
||||
}
|
||||
|
||||
/* now parse the block or the value */
|
||||
@@ -221,23 +202,16 @@ ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
|
||||
const char *value = tokenizer.NextString(error);
|
||||
if (value == nullptr) {
|
||||
if (tokenizer.IsEnd())
|
||||
error.Format(config_file_domain,
|
||||
"line %u: Value missing",
|
||||
reader.GetLineNumber());
|
||||
else
|
||||
error.FormatPrefix("line %u: ",
|
||||
reader.GetLineNumber());
|
||||
throw FormatRuntimeError("line %u: Value missing",
|
||||
reader.GetLineNumber());
|
||||
|
||||
error.FormatPrefix("line %u: ", reader.GetLineNumber());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!tokenizer.IsEnd() &&
|
||||
tokenizer.CurrentChar() != CONF_COMMENT) {
|
||||
error.Format(config_file_domain,
|
||||
"line %u: Unknown tokens after value",
|
||||
reader.GetLineNumber());
|
||||
return false;
|
||||
}
|
||||
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT)
|
||||
throw FormatRuntimeError("line %u: Unknown tokens after value",
|
||||
reader.GetLineNumber());
|
||||
|
||||
auto *param = new config_param(value, reader.GetLineNumber());
|
||||
Append(head, param);
|
||||
@@ -281,11 +255,9 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||
tokenizer, error))
|
||||
return false;
|
||||
} else {
|
||||
error.Format(config_file_domain,
|
||||
"unrecognized parameter in config file at "
|
||||
"line %u: %s\n",
|
||||
reader.GetLineNumber(), name);
|
||||
return false;
|
||||
throw FormatRuntimeError("unrecognized parameter in config file at "
|
||||
"line %u: %s\n",
|
||||
reader.GetLineNumber(), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
45
src/util/RuntimeError.hxx
Normal file
45
src/util/RuntimeError.hxx
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2013-2015 Max Kellermann <max@duempel.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef RUNTIME_ERROR_HXX
|
||||
#define RUNTIME_ERROR_HXX
|
||||
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
template<typename... Args>
|
||||
static inline std::runtime_error
|
||||
FormatRuntimeError(const char *fmt, Args&&... args) noexcept
|
||||
{
|
||||
char buffer[1024];
|
||||
snprintf(buffer, sizeof(buffer), fmt, std::forward<Args>(args)...);
|
||||
return std::runtime_error(buffer);
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user