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_XML))
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_XML))
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 if (name == mandatory_tag)
190 // Found the mandatory tag
191 mandatory_tag.clear();
194 if (name == "include")
196 stack.DoInclude(tag, flags);
198 else if (name == "files")
200 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
202 stack.DoReadFile(i->first, i->second, flags, false);
205 else if (name == "execfiles")
207 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
209 stack.DoReadFile(i->first, i->second, flags, true);
212 else if (name == "define")
214 if (!(flags & FLAG_USE_XML))
215 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
216 std::string varname = tag->getString("name");
217 std::string value = tag->getString("value");
219 throw CoreException("Variable definition must include variable name");
220 stack.vars[varname] = value;
222 else if (name == "config")
224 std::string format = tag->getString("format");
226 flags |= FLAG_USE_XML;
227 else if (format == "compat")
228 flags &= ~FLAG_USE_XML;
229 else if (!format.empty())
230 throw CoreException("Unknown configuration format " + format);
234 stack.output.insert(std::make_pair(name, tag));
236 // this is not a leak; reference<> takes care of the delete
250 // this is the one place where an EOF is not an error
251 if (!mandatory_tag.empty())
252 throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
267 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
269 throw CoreException("Syntax error - start of tag expected");
273 catch (CoreException& err)
275 stack.errstr << err.GetReason() << " at " << current.str();
277 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
279 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
285 void ParseStack::DoInclude(ConfigTag* tag, int flags)
287 if (flags & FLAG_NO_INC)
288 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
290 std::string mandatorytag;
291 tag->readString("mandatorytag", mandatorytag);
294 if (tag->readString("file", name))
296 if (tag->getBool("noinclude", false))
297 flags |= FLAG_NO_INC;
298 if (tag->getBool("noexec", false))
299 flags |= FLAG_NO_EXEC;
300 if (!ParseFile(name, flags, mandatorytag))
301 throw CoreException("Included");
303 else if (tag->readString("executable", name))
305 if (flags & FLAG_NO_EXEC)
306 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
307 if (tag->getBool("noinclude", false))
308 flags |= FLAG_NO_INC;
309 if (tag->getBool("noexec", true))
310 flags |= FLAG_NO_EXEC;
311 if (!ParseExec(name, flags, mandatorytag))
312 throw CoreException("Included");
316 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
318 if (flags & FLAG_NO_INC)
319 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
320 if (exec && (flags & FLAG_NO_EXEC))
321 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
323 FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"), exec);
325 throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
327 file_cache& cache = FilesOutput[key];
330 char linebuf[MAXBUF*10];
331 while (fgets(linebuf, sizeof(linebuf), file))
333 size_t len = strlen(linebuf);
336 if (linebuf[len-1] == '\n')
338 cache.push_back(std::string(linebuf, len));
343 bool ParseStack::ParseFile(const std::string& name, int flags, const std::string& mandatory_tag)
345 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
346 for (unsigned int t = 0; t < reading.size(); t++)
348 if (std::string(name) == reading[t])
350 throw CoreException("File " + name + " is included recursively (looped inclusion)");
354 /* It's not already included, add it to the list of files we've loaded */
356 FileWrapper file(fopen(name.c_str(), "r"));
358 throw CoreException("Could not read \"" + name + "\" for include");
360 reading.push_back(name);
361 Parser p(*this, flags, file, name, mandatory_tag);
362 bool ok = p.outer_parse();
367 bool ParseStack::ParseExec(const std::string& name, int flags, const std::string& mandatory_tag)
369 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str());
370 for (unsigned int t = 0; t < reading.size(); t++)
372 if (std::string(name) == reading[t])
374 throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
378 /* It's not already included, add it to the list of files we've loaded */
380 FileWrapper file(popen(name.c_str(), "r"), true);
382 throw CoreException("Could not open executable \"" + name + "\" for include");
384 reading.push_back(name);
385 Parser p(*this, flags, file, name, mandatory_tag);
386 bool ok = p.outer_parse();
391 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
395 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
400 if (!allow_lf && (value.find('\n') != std::string::npos))
402 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
403 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
404 for (std::string::iterator n = value.begin(); n != value.end(); n++)
413 std::string ConfigTag::getString(const std::string& key, const std::string& def)
415 std::string res = def;
416 readString(key, res);
420 long ConfigTag::getInt(const std::string &key, long def)
423 if(!readString(key, result))
426 const char* res_cstr = result.c_str();
427 char* res_tail = NULL;
428 long res = strtol(res_cstr, &res_tail, 0);
429 if (res_tail == res_cstr)
431 switch (toupper(*res_tail))
437 res= res* 1024 * 1024;
440 res= res* 1024 * 1024 * 1024;
446 double ConfigTag::getFloat(const std::string &key, double def)
449 if (!readString(key, result))
451 return strtod(result.c_str(), NULL);
454 bool ConfigTag::getBool(const std::string &key, bool def)
457 if(!readString(key, result))
460 if (result == "yes" || result == "true" || result == "1" || result == "on")
462 if (result == "no" || result == "false" || result == "0" || result == "off")
465 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
466 " is not valid, ignoring");
470 std::string ConfigTag::getTagLocation()
472 return src_name + ":" + ConvToStr(src_line);
475 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
477 ConfigTag* rv = new ConfigTag(Tag, file, line);
482 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
483 : tag(Tag), src_name(file), src_line(line)
487 std::string OperInfo::getConfig(const std::string& key)
491 type_block->readString(key, rv);
493 oper_block->readString(key, rv);