X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fconfigparser.cpp;h=409ebdd395e67fe38de058edd0e823e73d28d3f2;hb=571714e28b26cc59cbc8d27098a5ba981240ee2d;hp=22c3679abdaf01e2f8de5972ae9d437462a64b46;hpb=b332dda6a32f4033bfd7d9dd86150e1c23e3cf62;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/configparser.cpp b/src/configparser.cpp index 22c3679ab..409ebdd39 100644 --- a/src/configparser.cpp +++ b/src/configparser.cpp @@ -1,16 +1,22 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits + * Copyright (C) 2009-2010 Daniel De Graaf * - * This program is free but copyrighted software; see - * the file COPYING for details. + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. * - * --------------------------------------------------- + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + #include "inspircd.h" #include #include "configparser.h" @@ -18,15 +24,16 @@ struct Parser { ParseStack& stack; - const int flags; + int flags; FILE* const file; fpos current; fpos last_tag; reference tag; int ungot; + std::string mandatory_tag; - Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name) - : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1) + Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name, const std::string& mandatorytag) + : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1), mandatory_tag(mandatorytag) { } int next(bool eof_ok = false) @@ -76,7 +83,7 @@ struct Parser int ch = next(); while (isspace(ch)) ch = next(); - while (isalnum(ch) || ch == '_') + while (isalnum(ch) || ch == '_'|| ch == '-') { rv.push_back(ch); ch = next(); @@ -84,7 +91,7 @@ struct Parser unget(ch); } - bool kv() + bool kv(std::vector* items, std::set& seen) { std::string key; nextword(key); @@ -112,7 +119,7 @@ struct Parser while (1) { ch = next(); - if (ch == '&') + if (ch == '&' && (flags & FLAG_USE_XML)) { std::string varname; while (1) @@ -134,11 +141,26 @@ struct Parser throw CoreException("Undefined XML entity reference '&" + varname + ";'"); value.append(var->second); } + else if (ch == '\\' && !(flags & FLAG_USE_XML)) + { + int esc = next(); + if (esc == 'n') + value.push_back('\n'); + else if (isalpha(esc)) + throw CoreException("Unknown escape character \\" + std::string(1, esc)); + else + value.push_back(esc); + } else if (ch == '"') break; - value.push_back(ch); + else + value.push_back(ch); } - tag->items.push_back(KeyVal(key, value)); + + if (!seen.insert(key).second) + throw CoreException("Duplicate key '" + key + "' found"); + + items->push_back(KeyVal(key, value)); return true; } @@ -157,22 +179,56 @@ struct Parser if (name.empty()) throw CoreException("Empty tag name"); - tag = new ConfigTag(name, current.filename, current.line); + std::vector* items; + std::set seen; + tag = ConfigTag::create(name, current.filename, current.line, items); - while (kv()); + while (kv(items, seen)); + + if (name == mandatory_tag) + { + // Found the mandatory tag + mandatory_tag.clear(); + } if (name == "include") { stack.DoInclude(tag, flags); } + else if (name == "files") + { + for(std::vector::iterator i = items->begin(); i != items->end(); i++) + { + stack.DoReadFile(i->first, i->second, flags, false); + } + } + else if (name == "execfiles") + { + for(std::vector::iterator i = items->begin(); i != items->end(); i++) + { + stack.DoReadFile(i->first, i->second, flags, true); + } + } else if (name == "define") { + if (!(flags & FLAG_USE_XML)) + throw CoreException(" tags may only be used in XML-style config (add )"); std::string varname = tag->getString("name"); std::string value = tag->getString("value"); if (varname.empty()) throw CoreException("Variable definition must include variable name"); stack.vars[varname] = value; } + else if (name == "config") + { + std::string format = tag->getString("format"); + if (format == "xml") + flags |= FLAG_USE_XML; + else if (format == "compat") + flags &= ~FLAG_USE_XML; + else if (!format.empty()) + throw CoreException("Unknown configuration format " + format); + } else { stack.output.insert(std::make_pair(name, tag)); @@ -192,6 +248,8 @@ struct Parser { case EOF: // this is the one place where an EOF is not an error + if (!mandatory_tag.empty()) + throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found"); return true; case '#': comment(); @@ -228,6 +286,10 @@ void ParseStack::DoInclude(ConfigTag* tag, int flags) { if (flags & FLAG_NO_INC) throw CoreException("Invalid tag in file included with noinclude=\"yes\""); + + std::string mandatorytag; + tag->readString("mandatorytag", mandatorytag); + std::string name; if (tag->readString("file", name)) { @@ -235,7 +297,7 @@ void ParseStack::DoInclude(ConfigTag* tag, int flags) flags |= FLAG_NO_INC; if (tag->getBool("noexec", false)) flags |= FLAG_NO_EXEC; - if (!ParseFile(name, flags)) + if (!ParseFile(name, flags, mandatorytag)) throw CoreException("Included"); } else if (tag->readString("executable", name)) @@ -246,12 +308,39 @@ void ParseStack::DoInclude(ConfigTag* tag, int flags) flags |= FLAG_NO_INC; if (tag->getBool("noexec", true)) flags |= FLAG_NO_EXEC; - if (!ParseExec(name, flags)) + if (!ParseExec(name, flags, mandatorytag)) throw CoreException("Included"); } } -bool ParseStack::ParseFile(const std::string& name, int flags) +void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec) +{ + if (flags & FLAG_NO_INC) + throw CoreException("Invalid tag in file included with noinclude=\"yes\""); + if (exec && (flags & FLAG_NO_EXEC)) + throw CoreException("Invalid tag in file included with noexec=\"yes\""); + + FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"), exec); + if (!file) + throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file"); + + file_cache& cache = FilesOutput[key]; + cache.clear(); + + char linebuf[MAXBUF*10]; + while (fgets(linebuf, sizeof(linebuf), file)) + { + size_t len = strlen(linebuf); + if (len) + { + if (linebuf[len-1] == '\n') + len--; + cache.push_back(std::string(linebuf, len)); + } + } +} + +bool ParseStack::ParseFile(const std::string& name, int flags, const std::string& mandatory_tag) { ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str()); for (unsigned int t = 0; t < reading.size(); t++) @@ -269,13 +358,13 @@ bool ParseStack::ParseFile(const std::string& name, int flags) throw CoreException("Could not read \"" + name + "\" for include"); reading.push_back(name); - Parser p(*this, flags, file, name); + Parser p(*this, flags, file, name, mandatory_tag); bool ok = p.outer_parse(); reading.pop_back(); return ok; } -bool ParseStack::ParseExec(const std::string& name, int flags) +bool ParseStack::ParseExec(const std::string& name, int flags, const std::string& mandatory_tag) { ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str()); for (unsigned int t = 0; t < reading.size(); t++) @@ -288,19 +377,29 @@ bool ParseStack::ParseExec(const std::string& name, int flags) /* It's not already included, add it to the list of files we've loaded */ - FileWrapper file(popen(name.c_str(), "r")); + FileWrapper file(popen(name.c_str(), "r"), true); if (!file) throw CoreException("Could not open executable \"" + name + "\" for include"); reading.push_back(name); - Parser p(*this, flags, file, name); + Parser p(*this, flags, file, name, mandatory_tag); bool ok = p.outer_parse(); reading.pop_back(); return ok; } +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunknown-pragmas" +# pragma clang diagnostic ignored "-Wundefined-bool-conversion" +#elif defined __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpragmas" +# pragma GCC diagnostic ignored "-Wnonnull-compare" +#endif bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf) { + // TODO: this is undefined behaviour but changing the API is too risky for 2.0. if (!this) return false; for(std::vector::iterator j = items.begin(); j != items.end(); ++j) @@ -320,6 +419,11 @@ bool ConfigTag::readString(const std::string& key, std::string& value, bool allo } return false; } +#ifdef __clang__ +# pragma clang diagnostic pop +#elif defined __GNUC__ +# pragma GCC diagnostic pop +#endif std::string ConfigTag::getString(const std::string& key, const std::string& def) { @@ -368,7 +472,14 @@ bool ConfigTag::getBool(const std::string &key, bool def) if(!readString(key, result)) return def; - return (result == "yes" || result == "true" || result == "1" || result == "on"); + if (result == "yes" || result == "true" || result == "1" || result == "on") + return true; + if (result == "no" || result == "false" || result == "0" || result == "off") + return false; + + ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() + + " is not valid, ignoring"); + return def; } std::string ConfigTag::getTagLocation() @@ -376,6 +487,18 @@ std::string ConfigTag::getTagLocation() return src_name + ":" + ConvToStr(src_line); } +ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector*& Items) +{ + ConfigTag* rv = new ConfigTag(Tag, file, line); + Items = &rv->items; + return rv; +} + +ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line) + : tag(Tag), src_name(file), src_line(line) +{ +} + std::string OperInfo::getConfig(const std::string& key) { std::string rv;