2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "configparser.h"
31 reference<ConfigTag> tag;
33 std::string mandatory_tag;
35 Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name, const std::string& mandatorytag)
36 : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1), mandatory_tag(mandatorytag)
39 int next(bool eof_ok = false)
48 if (ch == EOF && !eof_ok)
50 throw CoreException("Unexpected end-of-file");
67 throw CoreException("INTERNAL ERROR: cannot unget twice");
81 void nextword(std::string& rv)
86 while (isalnum(ch) || ch == '_'|| ch == '-')
94 bool kv(std::vector<KeyVal>* items, std::set<std::string>& seen)
99 if (ch == '>' && key.empty())
103 else if (ch == '#' && key.empty())
110 throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")");
117 throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">");
122 if (ch == '&' && !(flags & FLAG_USE_COMPAT))
129 varname.push_back(ch);
134 stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n"
135 << "To include an ampersand or quote, use & or "\n";
136 throw CoreException("Parse error");
139 std::map<std::string, std::string>::iterator var = stack.vars.find(varname);
140 if (var == stack.vars.end())
141 throw CoreException("Undefined XML entity reference '&" + varname + ";'");
142 value.append(var->second);
144 else if (ch == '\\' && (flags & FLAG_USE_COMPAT))
148 value.push_back('\n');
149 else if (isalpha(esc))
150 throw CoreException("Unknown escape character \\" + std::string(1, esc));
152 value.push_back(esc);
160 if (!seen.insert(key).second)
161 throw CoreException("Duplicate key '" + key + "' found");
163 items->push_back(KeyVal(key, value));
176 else if (!isspace(spc))
177 throw CoreException("Invalid character in tag name");
180 throw CoreException("Empty tag name");
182 std::vector<KeyVal>* items;
183 std::set<std::string> seen;
184 tag = ConfigTag::create(name, current.filename, current.line, items);
186 while (kv(items, seen))
188 // Do nothing here (silences a GCC warning).
191 if (name == mandatory_tag)
193 // Found the mandatory tag
194 mandatory_tag.clear();
197 if (name == "include")
199 stack.DoInclude(tag, flags);
201 else if (name == "files")
203 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
205 stack.DoReadFile(i->first, i->second, flags, false);
208 else if (name == "execfiles")
210 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
212 stack.DoReadFile(i->first, i->second, flags, true);
215 else if (name == "define")
217 if (flags & FLAG_USE_COMPAT)
218 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
219 std::string varname = tag->getString("name");
220 std::string value = tag->getString("value");
222 throw CoreException("Variable definition must include variable name");
223 stack.vars[varname] = value;
225 else if (name == "config")
227 std::string format = tag->getString("format");
229 flags &= ~FLAG_USE_COMPAT;
230 else if (format == "compat")
231 flags |= FLAG_USE_COMPAT;
232 else if (!format.empty())
233 throw CoreException("Unknown configuration format " + format);
237 stack.output.insert(std::make_pair(name, tag));
239 // this is not a leak; reference<> takes care of the delete
253 // this is the one place where an EOF is not an error
254 if (!mandatory_tag.empty())
255 throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
270 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
272 throw CoreException("Syntax error - start of tag expected");
276 catch (CoreException& err)
278 stack.errstr << err.GetReason() << " at " << current.str();
280 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
282 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
288 void ParseStack::DoInclude(ConfigTag* tag, int flags)
290 if (flags & FLAG_NO_INC)
291 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
293 std::string mandatorytag;
294 tag->readString("mandatorytag", mandatorytag);
297 if (tag->readString("file", name))
299 if (tag->getBool("noinclude", false))
300 flags |= FLAG_NO_INC;
301 if (tag->getBool("noexec", false))
302 flags |= FLAG_NO_EXEC;
303 if (!ParseFile(ServerInstance->Config->Paths.PrependConfig(name), flags, mandatorytag))
304 throw CoreException("Included");
306 else if (tag->readString("executable", name))
308 if (flags & FLAG_NO_EXEC)
309 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
310 if (tag->getBool("noinclude", false))
311 flags |= FLAG_NO_INC;
312 if (tag->getBool("noexec", true))
313 flags |= FLAG_NO_EXEC;
314 if (!ParseExec(name, flags, mandatorytag))
315 throw CoreException("Included");
319 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
321 if (flags & FLAG_NO_INC)
322 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
323 if (exec && (flags & FLAG_NO_EXEC))
324 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
326 std::string path = ServerInstance->Config->Paths.PrependConfig(name);
327 FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(path.c_str(), "r"), exec);
329 throw CoreException("Could not read \"" + path + "\" for \"" + key + "\" file");
331 file_cache& cache = FilesOutput[key];
335 while (fgets(linebuf, sizeof(linebuf), file))
337 size_t len = strlen(linebuf);
340 if (linebuf[len-1] == '\n')
342 cache.push_back(std::string(linebuf, len));
347 bool ParseStack::ParseFile(const std::string& path, int flags, const std::string& mandatory_tag)
349 ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading file %s", path.c_str());
350 for (unsigned int t = 0; t < reading.size(); t++)
352 if (path == reading[t])
354 throw CoreException("File " + path + " is included recursively (looped inclusion)");
358 /* It's not already included, add it to the list of files we've loaded */
360 FileWrapper file(fopen(path.c_str(), "r"));
362 throw CoreException("Could not read \"" + path + "\" for include");
364 reading.push_back(path);
365 Parser p(*this, flags, file, path, mandatory_tag);
366 bool ok = p.outer_parse();
371 bool ParseStack::ParseExec(const std::string& name, int flags, const std::string& mandatory_tag)
373 ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading executable %s", name.c_str());
374 for (unsigned int t = 0; t < reading.size(); t++)
376 if (std::string(name) == reading[t])
378 throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
382 /* It's not already included, add it to the list of files we've loaded */
384 FileWrapper file(popen(name.c_str(), "r"), true);
386 throw CoreException("Could not open executable \"" + name + "\" for include");
388 reading.push_back(name);
389 Parser p(*this, flags, file, name, mandatory_tag);
390 bool ok = p.outer_parse();
395 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
399 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
404 if (!allow_lf && (value.find('\n') != std::string::npos))
406 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
407 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
408 for (std::string::iterator n = value.begin(); n != value.end(); n++)
417 std::string ConfigTag::getString(const std::string& key, const std::string& def)
419 std::string res = def;
420 readString(key, res);
424 long ConfigTag::getInt(const std::string &key, long def, long min, long max)
427 if(!readString(key, result))
430 const char* res_cstr = result.c_str();
431 char* res_tail = NULL;
432 long res = strtol(res_cstr, &res_tail, 0);
433 if (res_tail == res_cstr)
435 switch (toupper(*res_tail))
441 res = res * 1024 * 1024;
444 res = res * 1024 * 1024 * 1024;
448 CheckRange(key, res, def, min, max);
452 void ConfigTag::CheckRange(const std::string& key, long& res, long def, long min, long max)
454 if (res < min || res > max)
456 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: <%s:%s> value of %ld is not between %ld and %ld; set to %ld.",
457 tag.c_str(), key.c_str(), res, min, max, def);
462 long ConfigTag::getDuration(const std::string& key, long def, long min, long max)
464 std::string duration;
465 if (!readString(key, duration))
468 long ret = InspIRCd::Duration(duration);
469 CheckRange(key, ret, def, min, max);
473 double ConfigTag::getFloat(const std::string &key, double def)
476 if (!readString(key, result))
478 return strtod(result.c_str(), NULL);
481 bool ConfigTag::getBool(const std::string &key, bool def)
484 if(!readString(key, result))
487 if (result == "yes" || result == "true" || result == "1" || result == "on")
489 if (result == "no" || result == "false" || result == "0" || result == "off")
492 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
493 " is not valid, ignoring");
497 std::string ConfigTag::getTagLocation()
499 return src_name + ":" + ConvToStr(src_line);
502 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*& Items)
504 ConfigTag* rv = new ConfigTag(Tag, file, line);
509 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
510 : tag(Tag), src_name(file), src_line(line)
514 std::string OperInfo::getConfig(const std::string& key)
518 type_block->readString(key, rv);
520 oper_block->readString(key, rv);