1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include "configparser.h"
25 reference<ConfigTag> tag;
28 Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name)
29 : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1)
32 int next(bool eof_ok = false)
41 if (ch == EOF && !eof_ok)
43 throw CoreException("Unexpected end-of-file");
60 throw CoreException("INTERNAL ERROR: cannot unget twice");
74 void nextword(std::string& rv)
79 while (isalnum(ch) || ch == '_'|| ch == '-')
87 bool kv(std::vector<KeyVal>* items)
92 if (ch == '>' && key.empty())
96 else if (ch == '#' && key.empty())
103 throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")");
110 throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">");
115 if (ch == '&' && (flags & FLAG_USE_XML))
122 varname.push_back(ch);
127 stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n"
128 << "To include an ampersand or quote, use & or "\n";
129 throw CoreException("Parse error");
132 std::map<std::string, std::string>::iterator var = stack.vars.find(varname);
133 if (var == stack.vars.end())
134 throw CoreException("Undefined XML entity reference '&" + varname + ";'");
135 value.append(var->second);
137 else if (ch == '\\' && !(flags & FLAG_USE_XML))
141 value.push_back('\n');
142 else if (isalpha(esc))
143 throw CoreException("Unknown escape character \\" + std::string(1, esc));
145 value.push_back(esc);
152 items->push_back(KeyVal(key, value));
165 else if (!isspace(spc))
166 throw CoreException("Invalid character in tag name");
169 throw CoreException("Empty tag name");
171 std::vector<KeyVal>* items;
172 tag = ConfigTag::create(name, current.filename, current.line, items);
176 if (name == "include")
178 stack.DoInclude(tag, flags);
180 else if (name == "files")
182 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
184 stack.DoReadFile(i->first, i->second, flags, false);
187 else if (name == "execfiles")
189 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
191 stack.DoReadFile(i->first, i->second, flags, true);
194 else if (name == "define")
196 if (!(flags & FLAG_USE_XML))
197 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
198 std::string varname = tag->getString("name");
199 std::string value = tag->getString("value");
201 throw CoreException("Variable definition must include variable name");
202 stack.vars[varname] = value;
204 else if (name == "config")
206 std::string format = tag->getString("format");
208 flags |= FLAG_USE_XML;
209 else if (format == "compat")
210 flags &= ~FLAG_USE_XML;
211 else if (!format.empty())
212 throw CoreException("Unknown configuration format " + format);
216 stack.output.insert(std::make_pair(name, tag));
218 // this is not a leak; reference<> takes care of the delete
232 // this is the one place where an EOF is not an error
247 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
249 throw CoreException("Syntax error - start of tag expected");
253 catch (CoreException& err)
255 stack.errstr << err.GetReason() << " at " << current.str();
257 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
259 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
265 void ParseStack::DoInclude(ConfigTag* tag, int flags)
267 if (flags & FLAG_NO_INC)
268 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
270 if (tag->readString("file", name))
272 if (tag->getBool("noinclude", false))
273 flags |= FLAG_NO_INC;
274 if (tag->getBool("noexec", false))
275 flags |= FLAG_NO_EXEC;
276 if (!ParseFile(name, flags))
277 throw CoreException("Included");
279 else if (tag->readString("executable", name))
281 if (flags & FLAG_NO_EXEC)
282 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
283 if (tag->getBool("noinclude", false))
284 flags |= FLAG_NO_INC;
285 if (tag->getBool("noexec", true))
286 flags |= FLAG_NO_EXEC;
287 if (!ParseExec(name, flags))
288 throw CoreException("Included");
292 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
294 if (flags & FLAG_NO_INC)
295 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
296 if (exec && (flags & FLAG_NO_EXEC))
297 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
299 FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"));
301 throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
303 file_cache& cache = FilesOutput[key];
306 char linebuf[MAXBUF*10];
307 while (fgets(linebuf, sizeof(linebuf), file))
309 int len = strlen(linebuf);
311 cache.push_back(std::string(linebuf, len - 1));
315 bool ParseStack::ParseFile(const std::string& name, int flags)
317 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
318 for (unsigned int t = 0; t < reading.size(); t++)
320 if (std::string(name) == reading[t])
322 throw CoreException("File " + name + " is included recursively (looped inclusion)");
326 /* It's not already included, add it to the list of files we've loaded */
328 FileWrapper file(fopen(name.c_str(), "r"));
330 throw CoreException("Could not read \"" + name + "\" for include");
332 reading.push_back(name);
333 Parser p(*this, flags, file, name);
334 bool ok = p.outer_parse();
339 bool ParseStack::ParseExec(const std::string& name, int flags)
341 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str());
342 for (unsigned int t = 0; t < reading.size(); t++)
344 if (std::string(name) == reading[t])
346 throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
350 /* It's not already included, add it to the list of files we've loaded */
352 FileWrapper file(popen(name.c_str(), "r"));
354 throw CoreException("Could not open executable \"" + name + "\" for include");
356 reading.push_back(name);
357 Parser p(*this, flags, file, name);
358 bool ok = p.outer_parse();
363 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
367 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
372 if (!allow_lf && (value.find('\n') != std::string::npos))
374 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
375 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
376 for (std::string::iterator n = value.begin(); n != value.end(); n++)
385 std::string ConfigTag::getString(const std::string& key, const std::string& def)
387 std::string res = def;
388 readString(key, res);
392 long ConfigTag::getInt(const std::string &key, long def)
395 if(!readString(key, result))
398 const char* res_cstr = result.c_str();
399 char* res_tail = NULL;
400 long res = strtol(res_cstr, &res_tail, 0);
401 if (res_tail == res_cstr)
403 switch (toupper(*res_tail))
409 res= res* 1024 * 1024;
412 res= res* 1024 * 1024 * 1024;
418 double ConfigTag::getFloat(const std::string &key, double def)
421 if (!readString(key, result))
423 return strtod(result.c_str(), NULL);
426 bool ConfigTag::getBool(const std::string &key, bool def)
429 if(!readString(key, result))
432 if (result == "yes" || result == "true" || result == "1" || result == "on")
434 if (result == "no" || result == "false" || result == "0" || result == "off")
437 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
438 " is not valid, ignoring");
442 std::string ConfigTag::getTagLocation()
444 return src_name + ":" + ConvToStr(src_line);
447 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
449 ConfigTag* rv = new ConfigTag(Tag, file, line);
454 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
455 : tag(Tag), src_name(file), src_line(line)
459 std::string OperInfo::getConfig(const std::string& key)
463 type_block->readString(key, rv);
465 oper_block->readString(key, rv);