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;
34 Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name)
35 : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1)
38 int next(bool eof_ok = false)
47 if (ch == EOF && !eof_ok)
49 throw CoreException("Unexpected end-of-file");
66 throw CoreException("INTERNAL ERROR: cannot unget twice");
80 void nextword(std::string& rv)
85 while (isalnum(ch) || ch == '_'|| ch == '-')
93 bool kv(std::vector<KeyVal>* items, std::set<std::string>& seen)
98 if (ch == '>' && key.empty())
102 else if (ch == '#' && key.empty())
109 throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")");
116 throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">");
121 if (ch == '&' && (flags & FLAG_USE_XML))
128 varname.push_back(ch);
133 stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n"
134 << "To include an ampersand or quote, use & or "\n";
135 throw CoreException("Parse error");
138 std::map<std::string, std::string>::iterator var = stack.vars.find(varname);
139 if (var == stack.vars.end())
140 throw CoreException("Undefined XML entity reference '&" + varname + ";'");
141 value.append(var->second);
143 else if (ch == '\\' && !(flags & FLAG_USE_XML))
147 value.push_back('\n');
148 else if (isalpha(esc))
149 throw CoreException("Unknown escape character \\" + std::string(1, esc));
151 value.push_back(esc);
159 if (!seen.insert(key).second)
160 throw CoreException("Duplicate key '" + key + "' found");
162 items->push_back(KeyVal(key, value));
175 else if (!isspace(spc))
176 throw CoreException("Invalid character in tag name");
179 throw CoreException("Empty tag name");
181 std::vector<KeyVal>* items;
182 std::set<std::string> seen;
183 tag = ConfigTag::create(name, current.filename, current.line, items);
185 while (kv(items, seen));
187 if (name == "include")
189 stack.DoInclude(tag, flags);
191 else if (name == "files")
193 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
195 stack.DoReadFile(i->first, i->second, flags, false);
198 else if (name == "execfiles")
200 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
202 stack.DoReadFile(i->first, i->second, flags, true);
205 else if (name == "define")
207 if (!(flags & FLAG_USE_XML))
208 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
209 std::string varname = tag->getString("name");
210 std::string value = tag->getString("value");
212 throw CoreException("Variable definition must include variable name");
213 stack.vars[varname] = value;
215 else if (name == "config")
217 std::string format = tag->getString("format");
219 flags |= FLAG_USE_XML;
220 else if (format == "compat")
221 flags &= ~FLAG_USE_XML;
222 else if (!format.empty())
223 throw CoreException("Unknown configuration format " + format);
227 stack.output.insert(std::make_pair(name, tag));
229 // this is not a leak; reference<> takes care of the delete
243 // this is the one place where an EOF is not an error
258 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
260 throw CoreException("Syntax error - start of tag expected");
264 catch (CoreException& err)
266 stack.errstr << err.GetReason() << " at " << current.str();
268 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
270 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
276 void ParseStack::DoInclude(ConfigTag* tag, int flags)
278 if (flags & FLAG_NO_INC)
279 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
281 if (tag->readString("file", name))
283 if (tag->getBool("noinclude", false))
284 flags |= FLAG_NO_INC;
285 if (tag->getBool("noexec", false))
286 flags |= FLAG_NO_EXEC;
287 if (!ParseFile(name, flags))
288 throw CoreException("Included");
290 else if (tag->readString("executable", name))
292 if (flags & FLAG_NO_EXEC)
293 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
294 if (tag->getBool("noinclude", false))
295 flags |= FLAG_NO_INC;
296 if (tag->getBool("noexec", true))
297 flags |= FLAG_NO_EXEC;
298 if (!ParseExec(name, flags))
299 throw CoreException("Included");
303 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
305 if (flags & FLAG_NO_INC)
306 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
307 if (exec && (flags & FLAG_NO_EXEC))
308 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
310 FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"));
312 throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
314 file_cache& cache = FilesOutput[key];
317 char linebuf[MAXBUF*10];
318 while (fgets(linebuf, sizeof(linebuf), file))
320 size_t len = strlen(linebuf);
323 if (linebuf[len-1] == '\n')
325 cache.push_back(std::string(linebuf, len));
330 bool ParseStack::ParseFile(const std::string& name, int flags)
332 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
333 for (unsigned int t = 0; t < reading.size(); t++)
335 if (std::string(name) == reading[t])
337 throw CoreException("File " + name + " is included recursively (looped inclusion)");
341 /* It's not already included, add it to the list of files we've loaded */
343 FileWrapper file(fopen(name.c_str(), "r"));
345 throw CoreException("Could not read \"" + name + "\" for include");
347 reading.push_back(name);
348 Parser p(*this, flags, file, name);
349 bool ok = p.outer_parse();
354 bool ParseStack::ParseExec(const std::string& name, int flags)
356 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str());
357 for (unsigned int t = 0; t < reading.size(); t++)
359 if (std::string(name) == reading[t])
361 throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
365 /* It's not already included, add it to the list of files we've loaded */
367 FileWrapper file(popen(name.c_str(), "r"));
369 throw CoreException("Could not open executable \"" + name + "\" for include");
371 reading.push_back(name);
372 Parser p(*this, flags, file, name);
373 bool ok = p.outer_parse();
378 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
382 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
387 if (!allow_lf && (value.find('\n') != std::string::npos))
389 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
390 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
391 for (std::string::iterator n = value.begin(); n != value.end(); n++)
400 std::string ConfigTag::getString(const std::string& key, const std::string& def)
402 std::string res = def;
403 readString(key, res);
407 long ConfigTag::getInt(const std::string &key, long def)
410 if(!readString(key, result))
413 const char* res_cstr = result.c_str();
414 char* res_tail = NULL;
415 long res = strtol(res_cstr, &res_tail, 0);
416 if (res_tail == res_cstr)
418 switch (toupper(*res_tail))
424 res= res* 1024 * 1024;
427 res= res* 1024 * 1024 * 1024;
433 double ConfigTag::getFloat(const std::string &key, double def)
436 if (!readString(key, result))
438 return strtod(result.c_str(), NULL);
441 bool ConfigTag::getBool(const std::string &key, bool def)
444 if(!readString(key, result))
447 if (result == "yes" || result == "true" || result == "1" || result == "on")
449 if (result == "no" || result == "false" || result == "0" || result == "off")
452 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
453 " is not valid, ignoring");
457 std::string ConfigTag::getTagLocation()
459 return src_name + ":" + ConvToStr(src_line);
462 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
464 ConfigTag* rv = new ConfigTag(Tag, file, line);
469 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
470 : tag(Tag), src_name(file), src_line(line)
474 std::string OperInfo::getConfig(const std::string& key)
478 type_block->readString(key, rv);
480 oper_block->readString(key, rv);