]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/configparser.cpp
Tidy up keywords on module methods.
[user/henk/code/inspircd.git] / src / configparser.cpp
index b6e3f7187e2814e7276056de0d5898eec8633469..ec6a6c8e53e9876fc79c663e724f0ca66704d12c 100644 (file)
@@ -30,9 +30,10 @@ struct Parser
        fpos last_tag;
        reference<ConfigTag> 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)
@@ -118,7 +119,7 @@ struct Parser
                while (1)
                {
                        ch = next();
-                       if (ch == '&' && (flags & FLAG_USE_XML))
+                       if (ch == '&' && !(flags & FLAG_USE_COMPAT))
                        {
                                std::string varname;
                                while (1)
@@ -140,7 +141,7 @@ struct Parser
                                        throw CoreException("Undefined XML entity reference '&" + varname + ";'");
                                value.append(var->second);
                        }
-                       else if (ch == '\\' && !(flags & FLAG_USE_XML))
+                       else if (ch == '\\' && (flags & FLAG_USE_COMPAT))
                        {
                                int esc = next();
                                if (esc == 'n')
@@ -184,6 +185,12 @@ struct Parser
 
                while (kv(items, seen));
 
+               if (name == mandatory_tag)
+               {
+                       // Found the mandatory tag
+                       mandatory_tag.clear();
+               }
+
                if (name == "include")
                {
                        stack.DoInclude(tag, flags);
@@ -204,7 +211,7 @@ struct Parser
                }
                else if (name == "define")
                {
-                       if (!(flags & FLAG_USE_XML))
+                       if (flags & FLAG_USE_COMPAT)
                                throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
                        std::string varname = tag->getString("name");
                        std::string value = tag->getString("value");
@@ -216,9 +223,9 @@ struct Parser
                {
                        std::string format = tag->getString("format");
                        if (format == "xml")
-                               flags |= FLAG_USE_XML;
+                               flags &= ~FLAG_USE_COMPAT;
                        else if (format == "compat")
-                               flags &= ~FLAG_USE_XML;
+                               flags |= FLAG_USE_COMPAT;
                        else if (!format.empty())
                                throw CoreException("Unknown configuration format " + format);
                }
@@ -241,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();
@@ -277,6 +286,10 @@ void ParseStack::DoInclude(ConfigTag* tag, int flags)
 {
        if (flags & FLAG_NO_INC)
                throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
+
+       std::string mandatorytag;
+       tag->readString("mandatorytag", mandatorytag);
+
        std::string name;
        if (tag->readString("file", name))
        {
@@ -284,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))
@@ -295,7 +308,7 @@ 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");
        }
 }
@@ -327,9 +340,9 @@ void ParseStack::DoReadFile(const std::string& key, const std::string& name, int
        }
 }
 
-bool ParseStack::ParseFile(const std::string& name, int flags)
+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());
+       ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading file %s", name.c_str());
        for (unsigned int t = 0; t < reading.size(); t++)
        {
                if (std::string(name) == reading[t])
@@ -345,15 +358,15 @@ 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());
+       ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading executable %s", name.c_str());
        for (unsigned int t = 0; t < reading.size(); t++)
        {
                if (std::string(name) == reading[t])
@@ -369,7 +382,7 @@ bool ParseStack::ParseExec(const std::string& name, int flags)
                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;
@@ -386,7 +399,7 @@ bool ConfigTag::readString(const std::string& key, std::string& value, bool allo
                value = j->second;
                if (!allow_lf && (value.find('\n') != std::string::npos))
                {
-                       ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
+                       ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
                                " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
                        for (std::string::iterator n = value.begin(); n != value.end(); n++)
                                if (*n == '\n')
@@ -449,7 +462,7 @@ bool ConfigTag::getBool(const std::string &key, bool def)
        if (result == "no" || result == "false" || result == "0" || result == "off")
                return false;
 
-       ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
+       ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
                " is not valid, ignoring");
        return def;
 }