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 == '_')
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 == "define")
182 if (!(flags & FLAG_USE_XML))
183 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
184 std::string varname = tag->getString("name");
185 std::string value = tag->getString("value");
187 throw CoreException("Variable definition must include variable name");
188 stack.vars[varname] = value;
190 else if (name == "config")
192 std::string format = tag->getString("format");
194 flags |= FLAG_USE_XML;
195 else if (format == "compat")
196 flags &= ~FLAG_USE_XML;
197 else if (!format.empty())
198 throw CoreException("Unknown configuration format " + format);
202 stack.output.insert(std::make_pair(name, tag));
204 // this is not a leak; reference<> takes care of the delete
218 // this is the one place where an EOF is not an error
233 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
235 throw CoreException("Syntax error - start of tag expected");
239 catch (CoreException& err)
241 stack.errstr << err.GetReason() << " at " << current.str();
243 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
245 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
251 void ParseStack::DoInclude(ConfigTag* tag, int flags)
253 if (flags & FLAG_NO_INC)
254 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
256 if (tag->readString("file", name))
258 if (tag->getBool("noinclude", false))
259 flags |= FLAG_NO_INC;
260 if (tag->getBool("noexec", false))
261 flags |= FLAG_NO_EXEC;
262 if (!ParseFile(name, flags))
263 throw CoreException("Included");
265 else if (tag->readString("executable", name))
267 if (flags & FLAG_NO_EXEC)
268 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
269 if (tag->getBool("noinclude", false))
270 flags |= FLAG_NO_INC;
271 if (tag->getBool("noexec", true))
272 flags |= FLAG_NO_EXEC;
273 if (!ParseExec(name, flags))
274 throw CoreException("Included");
278 bool ParseStack::ParseFile(const std::string& name, int flags)
280 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
281 for (unsigned int t = 0; t < reading.size(); t++)
283 if (std::string(name) == reading[t])
285 throw CoreException("File " + name + " is included recursively (looped inclusion)");
289 /* It's not already included, add it to the list of files we've loaded */
291 FileWrapper file(fopen(name.c_str(), "r"));
293 throw CoreException("Could not read \"" + name + "\" for include");
295 reading.push_back(name);
296 Parser p(*this, flags, file, name);
297 bool ok = p.outer_parse();
302 bool ParseStack::ParseExec(const std::string& name, int flags)
304 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str());
305 for (unsigned int t = 0; t < reading.size(); t++)
307 if (std::string(name) == reading[t])
309 throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
313 /* It's not already included, add it to the list of files we've loaded */
315 FileWrapper file(popen(name.c_str(), "r"));
317 throw CoreException("Could not open executable \"" + name + "\" for include");
319 reading.push_back(name);
320 Parser p(*this, flags, file, name);
321 bool ok = p.outer_parse();
326 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
330 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
335 if (!allow_lf && (value.find('\n') != std::string::npos))
337 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
338 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
339 for (std::string::iterator n = value.begin(); n != value.end(); n++)
348 std::string ConfigTag::getString(const std::string& key, const std::string& def)
350 std::string res = def;
351 readString(key, res);
355 long ConfigTag::getInt(const std::string &key, long def)
358 if(!readString(key, result))
361 const char* res_cstr = result.c_str();
362 char* res_tail = NULL;
363 long res = strtol(res_cstr, &res_tail, 0);
364 if (res_tail == res_cstr)
366 switch (toupper(*res_tail))
372 res= res* 1024 * 1024;
375 res= res* 1024 * 1024 * 1024;
381 double ConfigTag::getFloat(const std::string &key, double def)
384 if (!readString(key, result))
386 return strtod(result.c_str(), NULL);
389 bool ConfigTag::getBool(const std::string &key, bool def)
392 if(!readString(key, result))
395 return (result == "yes" || result == "true" || result == "1" || result == "on");
398 std::string ConfigTag::getTagLocation()
400 return src_name + ":" + ConvToStr(src_line);
403 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
405 ConfigTag* rv = new ConfigTag(Tag, file, line);
410 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
411 : tag(Tag), src_name(file), src_line(line)
415 std::string OperInfo::getConfig(const std::string& key)
419 type_block->readString(key, rv);
421 oper_block->readString(key, rv);