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, std::set<std::string>& seen)
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);
153 if (!seen.insert(key).second)
154 throw CoreException("Duplicate key '" + key + "' found");
156 items->push_back(KeyVal(key, value));
169 else if (!isspace(spc))
170 throw CoreException("Invalid character in tag name");
173 throw CoreException("Empty tag name");
175 std::vector<KeyVal>* items;
176 std::set<std::string> seen;
177 tag = ConfigTag::create(name, current.filename, current.line, items);
179 while (kv(items, seen));
181 if (name == "include")
183 stack.DoInclude(tag, flags);
185 else if (name == "files")
187 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
189 stack.DoReadFile(i->first, i->second, flags, false);
192 else if (name == "execfiles")
194 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
196 stack.DoReadFile(i->first, i->second, flags, true);
199 else if (name == "define")
201 if (!(flags & FLAG_USE_XML))
202 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
203 std::string varname = tag->getString("name");
204 std::string value = tag->getString("value");
206 throw CoreException("Variable definition must include variable name");
207 stack.vars[varname] = value;
209 else if (name == "config")
211 std::string format = tag->getString("format");
213 flags |= FLAG_USE_XML;
214 else if (format == "compat")
215 flags &= ~FLAG_USE_XML;
216 else if (!format.empty())
217 throw CoreException("Unknown configuration format " + format);
221 stack.output.insert(std::make_pair(name, tag));
223 // this is not a leak; reference<> takes care of the delete
237 // this is the one place where an EOF is not an error
252 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
254 throw CoreException("Syntax error - start of tag expected");
258 catch (CoreException& err)
260 stack.errstr << err.GetReason() << " at " << current.str();
262 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
264 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
270 void ParseStack::DoInclude(ConfigTag* tag, int flags)
272 if (flags & FLAG_NO_INC)
273 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
275 if (tag->readString("file", name))
277 if (tag->getBool("noinclude", false))
278 flags |= FLAG_NO_INC;
279 if (tag->getBool("noexec", false))
280 flags |= FLAG_NO_EXEC;
281 if (!ParseFile(name, flags))
282 throw CoreException("Included");
284 else if (tag->readString("executable", name))
286 if (flags & FLAG_NO_EXEC)
287 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
288 if (tag->getBool("noinclude", false))
289 flags |= FLAG_NO_INC;
290 if (tag->getBool("noexec", true))
291 flags |= FLAG_NO_EXEC;
292 if (!ParseExec(name, flags))
293 throw CoreException("Included");
297 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
299 if (flags & FLAG_NO_INC)
300 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
301 if (exec && (flags & FLAG_NO_EXEC))
302 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
304 FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"));
306 throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
308 file_cache& cache = FilesOutput[key];
311 char linebuf[MAXBUF*10];
312 while (fgets(linebuf, sizeof(linebuf), file))
314 int len = strlen(linebuf);
316 cache.push_back(std::string(linebuf, len - 1));
320 bool ParseStack::ParseFile(const std::string& name, int flags)
322 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
323 for (unsigned int t = 0; t < reading.size(); t++)
325 if (std::string(name) == reading[t])
327 throw CoreException("File " + name + " is included recursively (looped inclusion)");
331 /* It's not already included, add it to the list of files we've loaded */
333 FileWrapper file(fopen(name.c_str(), "r"));
335 throw CoreException("Could not read \"" + name + "\" for include");
337 reading.push_back(name);
338 Parser p(*this, flags, file, name);
339 bool ok = p.outer_parse();
344 bool ParseStack::ParseExec(const std::string& name, int flags)
346 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str());
347 for (unsigned int t = 0; t < reading.size(); t++)
349 if (std::string(name) == reading[t])
351 throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
355 /* It's not already included, add it to the list of files we've loaded */
357 FileWrapper file(popen(name.c_str(), "r"));
359 throw CoreException("Could not open executable \"" + name + "\" for include");
361 reading.push_back(name);
362 Parser p(*this, flags, file, name);
363 bool ok = p.outer_parse();
368 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
372 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
377 if (!allow_lf && (value.find('\n') != std::string::npos))
379 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
380 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
381 for (std::string::iterator n = value.begin(); n != value.end(); n++)
390 std::string ConfigTag::getString(const std::string& key, const std::string& def)
392 std::string res = def;
393 readString(key, res);
397 long ConfigTag::getInt(const std::string &key, long def)
400 if(!readString(key, result))
403 const char* res_cstr = result.c_str();
404 char* res_tail = NULL;
405 long res = strtol(res_cstr, &res_tail, 0);
406 if (res_tail == res_cstr)
408 switch (toupper(*res_tail))
414 res= res* 1024 * 1024;
417 res= res* 1024 * 1024 * 1024;
423 double ConfigTag::getFloat(const std::string &key, double def)
426 if (!readString(key, result))
428 return strtod(result.c_str(), NULL);
431 bool ConfigTag::getBool(const std::string &key, bool def)
434 if(!readString(key, result))
437 if (result == "yes" || result == "true" || result == "1" || result == "on")
439 if (result == "no" || result == "false" || result == "0" || result == "off")
442 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
443 " is not valid, ignoring");
447 std::string ConfigTag::getTagLocation()
449 return src_name + ":" + ConvToStr(src_line);
452 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
454 ConfigTag* rv = new ConfigTag(Tag, file, line);
459 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
460 : tag(Tag), src_name(file), src_line(line)
464 std::string OperInfo::getConfig(const std::string& key)
468 type_block->readString(key, rv);
470 oper_block->readString(key, rv);