]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configparser.cpp
...because every now and again, i have to do a massive commit.
[user/henk/code/inspircd.git] / src / configparser.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <fstream>
16 #include "configparser.h"
17
18 struct Parser
19 {
20         ParseStack& stack;
21         int flags;
22         FILE* const file;
23         fpos current;
24         fpos last_tag;
25         reference<ConfigTag> tag;
26         int ungot;
27
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)
30         { }
31
32         int next(bool eof_ok = false)
33         {
34                 if (ungot != -1)
35                 {
36                         int ch = ungot;
37                         ungot = -1;
38                         return ch;
39                 }
40                 int ch = fgetc(file);
41                 if (ch == EOF && !eof_ok)
42                 {
43                         throw CoreException("Unexpected end-of-file");
44                 }
45                 else if (ch == '\n')
46                 {
47                         current.line++;
48                         current.col = 0;
49                 }
50                 else
51                 {
52                         current.col++;
53                 }
54                 return ch;
55         }
56
57         void unget(int ch)
58         {
59                 if (ungot != -1)
60                         throw CoreException("INTERNAL ERROR: cannot unget twice");
61                 ungot = ch;
62         }
63
64         void comment()
65         {
66                 while (1)
67                 {
68                         int ch = next();
69                         if (ch == '\n')
70                                 return;
71                 }
72         }
73
74         void nextword(std::string& rv)
75         {
76                 int ch = next();
77                 while (isspace(ch))
78                         ch = next();
79                 while (isalnum(ch) || ch == '_')
80                 {
81                         rv.push_back(ch);
82                         ch = next();
83                 }
84                 unget(ch);
85         }
86
87         bool kv(std::vector<KeyVal>* items)
88         {
89                 std::string key;
90                 nextword(key);
91                 int ch = next();
92                 if (ch == '>' && key.empty())
93                 {
94                         return false;
95                 }
96                 else if (ch == '#' && key.empty())
97                 {
98                         comment();
99                         return true;
100                 }
101                 else if (ch != '=')
102                 {
103                         throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")");
104                 }
105
106                 std::string value;
107                 ch = next();
108                 if (ch != '"')
109                 {
110                         throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">");
111                 }
112                 while (1)
113                 {
114                         ch = next();
115                         if (ch == '&' && (flags & FLAG_USE_XML))
116                         {
117                                 std::string varname;
118                                 while (1)
119                                 {
120                                         ch = next();
121                                         if (isalnum(ch))
122                                                 varname.push_back(ch);
123                                         else if (ch == ';')
124                                                 break;
125                                         else
126                                         {
127                                                 stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n"
128                                                         << "To include an ampersand or quote, use &amp; or &quot;\n";
129                                                 throw CoreException("Parse error");
130                                         }
131                                 }
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);
136                         }
137                         else if (ch == '\\' && !(flags & FLAG_USE_XML))
138                         {
139                                 int esc = next();
140                                 if (esc == 'n')
141                                         value.push_back('\n');
142                                 else if (isalpha(esc))
143                                         throw CoreException("Unknown escape character \\" + std::string(1, esc));
144                                 else
145                                         value.push_back(esc);
146                         }
147                         else if (ch == '"')
148                                 break;
149                         else
150                                 value.push_back(ch);
151                 }
152                 items->push_back(KeyVal(key, value));
153                 return true;
154         }
155
156         void dotag()
157         {
158                 last_tag = current;
159                 std::string name;
160                 nextword(name);
161
162                 int spc = next();
163                 if (spc == '>')
164                         unget(spc);
165                 else if (!isspace(spc))
166                         throw CoreException("Invalid character in tag name");
167
168                 if (name.empty())
169                         throw CoreException("Empty tag name");
170
171                 std::vector<KeyVal>* items;
172                 tag = ConfigTag::create(name, current.filename, current.line, items);
173
174                 while (kv(items));
175
176                 if (name == "include")
177                 {
178                         stack.DoInclude(tag, flags);
179                 }
180                 else if (name == "define")
181                 {
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");
186                         if (varname.empty())
187                                 throw CoreException("Variable definition must include variable name");
188                         stack.vars[varname] = value;
189                 }
190                 else if (name == "config")
191                 {
192                         std::string format = tag->getString("format");
193                         if (format == "xml")
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);
199                 }
200                 else
201                 {
202                         stack.output.insert(std::make_pair(name, tag));
203                 }
204                 // this is not a leak; reference<> takes care of the delete
205                 tag = NULL;
206         }
207
208         bool outer_parse()
209         {
210                 try
211                 {
212                         while (1)
213                         {
214                                 int ch = next(true);
215                                 switch (ch)
216                                 {
217                                         case EOF:
218                                                 // this is the one place where an EOF is not an error
219                                                 return true;
220                                         case '#':
221                                                 comment();
222                                                 break;
223                                         case '<':
224                                                 dotag();
225                                                 break;
226                                         case ' ':
227                                         case '\r':
228                                         case '\t':
229                                         case '\n':
230                                                 break;
231                                         case 0xFE:
232                                         case 0xFF:
233                                                 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
234                                         default:
235                                                 throw CoreException("Syntax error - start of tag expected");
236                                 }
237                         }
238                 }
239                 catch (CoreException& err)
240                 {
241                         stack.errstr << err.GetReason() << " at " << current.str();
242                         if (tag)
243                                 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
244                         else
245                                 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
246                 }
247                 return false;
248         }
249 };
250
251 void ParseStack::DoInclude(ConfigTag* tag, int flags)
252 {
253         if (flags & FLAG_NO_INC)
254                 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
255         std::string name;
256         if (tag->readString("file", name))
257         {
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");
264         }
265         else if (tag->readString("executable", name))
266         {
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");
275         }
276 }
277
278 bool ParseStack::ParseFile(const std::string& name, int flags)
279 {
280         ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
281         for (unsigned int t = 0; t < reading.size(); t++)
282         {
283                 if (std::string(name) == reading[t])
284                 {
285                         throw CoreException("File " + name + " is included recursively (looped inclusion)");
286                 }
287         }
288
289         /* It's not already included, add it to the list of files we've loaded */
290
291         FileWrapper file(fopen(name.c_str(), "r"));
292         if (!file)
293                 throw CoreException("Could not read \"" + name + "\" for include");
294
295         reading.push_back(name);
296         Parser p(*this, flags, file, name);
297         bool ok = p.outer_parse();
298         reading.pop_back();
299         return ok;
300 }
301
302 bool ParseStack::ParseExec(const std::string& name, int flags)
303 {
304         ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str());
305         for (unsigned int t = 0; t < reading.size(); t++)
306         {
307                 if (std::string(name) == reading[t])
308                 {
309                         throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
310                 }
311         }
312
313         /* It's not already included, add it to the list of files we've loaded */
314
315         FileWrapper file(popen(name.c_str(), "r"));
316         if (!file)
317                 throw CoreException("Could not open executable \"" + name + "\" for include");
318
319         reading.push_back(name);
320         Parser p(*this, flags, file, name);
321         bool ok = p.outer_parse();
322         reading.pop_back();
323         return ok;
324 }
325
326 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
327 {
328         if (!this)
329                 return false;
330         for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
331         {
332                 if(j->first != key)
333                         continue;
334                 value = j->second;
335                 if (!allow_lf && (value.find('\n') != std::string::npos))
336                 {
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++)
340                                 if (*n == '\n')
341                                         *n = ' ';
342                 }
343                 return true;
344         }
345         return false;
346 }
347
348 std::string ConfigTag::getString(const std::string& key, const std::string& def)
349 {
350         std::string res = def;
351         readString(key, res);
352         return res;
353 }
354
355 long ConfigTag::getInt(const std::string &key, long def)
356 {
357         std::string result;
358         if(!readString(key, result))
359                 return def;
360
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)
365                 return def;
366         switch (toupper(*res_tail))
367         {
368                 case 'K':
369                         res= res* 1024;
370                         break;
371                 case 'M':
372                         res= res* 1024 * 1024;
373                         break;
374                 case 'G':
375                         res= res* 1024 * 1024 * 1024;
376                         break;
377         }
378         return res;
379 }
380
381 double ConfigTag::getFloat(const std::string &key, double def)
382 {
383         std::string result;
384         if (!readString(key, result))
385                 return def;
386         return strtod(result.c_str(), NULL);
387 }
388
389 bool ConfigTag::getBool(const std::string &key, bool def)
390 {
391         std::string result;
392         if(!readString(key, result))
393                 return def;
394
395         return (result == "yes" || result == "true" || result == "1" || result == "on");
396 }
397
398 std::string ConfigTag::getTagLocation()
399 {
400         return src_name + ":" + ConvToStr(src_line);
401 }
402
403 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
404 {
405         ConfigTag* rv = new ConfigTag(Tag, file, line);
406         items = &rv->items;
407         return rv;
408 }
409
410 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
411         : tag(Tag), src_name(file), src_line(line)
412 {
413 }
414
415 std::string OperInfo::getConfig(const std::string& key)
416 {
417         std::string rv;
418         if (type_block)
419                 type_block->readString(key, rv);
420         if (oper_block)
421                 oper_block->readString(key, rv);
422         return rv;
423 }