Merge saidi@zizzer.eecs.umich.edu:/bk/linux
[gem5.git] / base / inifile.cc
1 /*
2 * Copyright (c) 2001-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #define USE_CPP
30 // #define CPP_PIPE
31
32
33 #ifdef USE_CPP
34 #include <sys/signal.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37
38 #if defined(__OpenBSD__) || defined(__APPLE__)
39 #include <libgen.h>
40 #endif
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #endif
45
46 #include <fstream>
47 #include <iostream>
48 #if __GNUC__ >= 3
49 #include <ext/stdio_filebuf.h>
50 #endif
51
52 #include <vector>
53 #include <string>
54
55 #include "base/inifile.hh"
56 #include "base/str.hh"
57
58 using namespace std;
59
60 IniFile::IniFile()
61 {}
62
63 IniFile::~IniFile()
64 {
65 SectionTable::iterator i = table.begin();
66 SectionTable::iterator end = table.end();
67
68 while (i != end) {
69 delete (*i).second;
70 ++i;
71 }
72 }
73
74
75 #ifdef USE_CPP
76 bool
77 IniFile::loadCPP(const string &file, vector<char *> &cppArgs)
78 {
79 int fd[2];
80
81 // Open the file just to verify that we can. Otherwise if the
82 // file doesn't exist or has bad permissions the user will get
83 // confusing errors from cpp/g++.
84 ifstream tmpf(file.c_str());
85
86 if (!tmpf.is_open())
87 return false;
88
89 tmpf.close();
90
91 const char *cfile = file.c_str();
92 char *dir = basename(cfile);
93 char *dir_arg = NULL;
94 if (*dir != '.' && dir != cfile) {
95 string arg = "-I";
96 arg += dir;
97
98 dir_arg = new char[arg.size() + 1];
99 strcpy(dir_arg, arg.c_str());
100 }
101
102 #ifdef CPP_PIPE
103 if (pipe(fd) == -1)
104 return false;
105 #else
106 char tempfile[] = "/tmp/configXXXXXX";
107 fd[0] = fd[1] = mkstemp(tempfile);
108 #endif
109
110 int pid = fork();
111
112 if (pid == -1)
113 return 1;
114
115 if (pid == 0) {
116 char filename[FILENAME_MAX];
117 string::size_type i = file.copy(filename, sizeof(filename) - 1);
118 filename[i] = '\0';
119
120 int arg_count = cppArgs.size();
121
122 char **args = new char *[arg_count + 20];
123
124 int nextArg = 0;
125 args[nextArg++] = "g++";
126 args[nextArg++] = "-E";
127 args[nextArg++] = "-P";
128 args[nextArg++] = "-nostdinc";
129 args[nextArg++] = "-nostdinc++";
130 args[nextArg++] = "-x";
131 args[nextArg++] = "c++";
132 args[nextArg++] = "-undef";
133
134 for (int i = 0; i < arg_count; i++)
135 args[nextArg++] = cppArgs[i];
136
137 if (dir_arg)
138 args[nextArg++] = dir_arg;
139
140 args[nextArg++] = filename;
141 args[nextArg++] = NULL;
142
143 close(STDOUT_FILENO);
144 if (dup2(fd[1], STDOUT_FILENO) == -1)
145 exit(1);
146
147 execvp("g++", args);
148
149 exit(0);
150 }
151
152 int retval;
153 waitpid(pid, &retval, 0);
154
155 delete [] dir_arg;
156
157 // check for normal completion of CPP
158 if (!WIFEXITED(retval) || WEXITSTATUS(retval) != 0)
159 return false;
160
161 #ifdef CPP_PIPE
162 close(fd[1]);
163 #else
164 lseek(fd[0], 0, SEEK_SET);
165 #endif
166
167 bool status = false;
168
169 #if __GNUC__ >= 3
170 using namespace __gnu_cxx;
171 stdio_filebuf<char> fbuf(fd[0], ios_base::in, true,
172 static_cast<stdio_filebuf<char>::int_type>(BUFSIZ));
173
174 if (fbuf.is_open()) {
175 istream f(&fbuf);
176 status = load(f);
177 }
178
179 #else
180 ifstream f(fd[0]);
181 if (f.is_open())
182 status = load(f);
183 #endif
184
185 #ifndef CPP_PIPE
186 unlink(tempfile);
187 #endif
188
189 return status;
190 }
191 #endif
192
193 bool
194 IniFile::load(const string &file)
195 {
196 ifstream f(file.c_str());
197
198 if (!f.is_open())
199 return false;
200
201 return load(f);
202 }
203
204
205 const string &
206 IniFile::Entry::getValue() const
207 {
208 referenced = true;
209 return value;
210 }
211
212
213 void
214 IniFile::Section::addEntry(const std::string &entryName,
215 const std::string &value,
216 bool append)
217 {
218 EntryTable::iterator ei = table.find(entryName);
219
220 if (ei == table.end()) {
221 // new entry
222 table[entryName] = new Entry(value);
223 }
224 else if (append) {
225 // append new reult to old entry
226 ei->second->appendValue(value);
227 }
228 else {
229 // override old entry
230 ei->second->setValue(value);
231 }
232 }
233
234
235 bool
236 IniFile::Section::add(const std::string &assignment)
237 {
238 string::size_type offset = assignment.find('=');
239 if (offset == string::npos) {
240 // no '=' found
241 cerr << "Can't parse .ini line " << assignment << endl;
242 return false;
243 }
244
245 // if "+=" rather than just "=" then append value
246 bool append = (assignment[offset-1] == '+');
247
248 string entryName = assignment.substr(0, append ? offset-1 : offset);
249 string value = assignment.substr(offset + 1);
250
251 eat_white(entryName);
252 eat_white(value);
253
254 addEntry(entryName, value, append);
255 return true;
256 }
257
258
259 IniFile::Entry *
260 IniFile::Section::findEntry(const std::string &entryName) const
261 {
262 referenced = true;
263
264 EntryTable::const_iterator ei = table.find(entryName);
265
266 return (ei == table.end()) ? NULL : ei->second;
267 }
268
269
270 IniFile::Section *
271 IniFile::addSection(const string &sectionName)
272 {
273 SectionTable::iterator i = table.find(sectionName);
274
275 if (i != table.end()) {
276 return i->second;
277 }
278 else {
279 // new entry
280 Section *sec = new Section();
281 table[sectionName] = sec;
282 return sec;
283 }
284 }
285
286
287 IniFile::Section *
288 IniFile::findSection(const string &sectionName) const
289 {
290 SectionTable::const_iterator i = table.find(sectionName);
291
292 return (i == table.end()) ? NULL : i->second;
293 }
294
295
296 // Take string of the form "<section>:<parameter>=<value>" and add to
297 // database. Return true if successful, false if parse error.
298 bool
299 IniFile::add(const string &str)
300 {
301 // find ':'
302 string::size_type offset = str.find(':');
303 if (offset == string::npos) // no ':' found
304 return false;
305
306 string sectionName = str.substr(0, offset);
307 string rest = str.substr(offset + 1);
308
309 eat_white(sectionName);
310 Section *s = addSection(sectionName);
311
312 return s->add(rest);
313 }
314
315 bool
316 IniFile::load(istream &f)
317 {
318 Section *section = NULL;
319
320 while (!f.eof()) {
321 f >> ws; // Eat whitespace
322 if (f.eof()) {
323 break;
324 }
325
326 string line;
327 getline(f, line);
328 if (line.size() == 0)
329 continue;
330
331 eat_end_white(line);
332 int last = line.size() - 1;
333
334 if (line[0] == '[' && line[last] == ']') {
335 string sectionName = line.substr(1, last - 1);
336 eat_white(sectionName);
337 section = addSection(sectionName);
338 continue;
339 }
340
341 if (section == NULL)
342 continue;
343
344 if (!section->add(line))
345 return false;
346 }
347
348 return true;
349 }
350
351 bool
352 IniFile::find(const string &sectionName, const string &entryName,
353 string &value) const
354 {
355 Section *section = findSection(sectionName);
356 if (section == NULL)
357 return false;
358
359 Entry *entry = section->findEntry(entryName);
360 if (entry == NULL)
361 return false;
362
363 value = entry->getValue();
364
365 return true;
366 }
367
368 bool
369 IniFile::findDefault(const string &_section, const string &entry,
370 string &value) const
371 {
372 string section = _section;
373 while (!findAppend(section, entry, value)) {
374 if (!find(section, "default", section)) {
375 return false;
376 }
377 }
378
379 return true;
380 }
381
382 bool
383 IniFile::findAppend(const string &_section, const string &entry,
384 string &value) const
385 {
386 string section = _section;
387 bool ret = false;
388 bool first = true;
389
390 do {
391 string val;
392 if (find(section, entry, val)) {
393 ret = true;
394 if (first) {
395 value = val;
396 first = false;
397 } else {
398 value += " ";
399 value += val;
400 }
401
402 }
403 } while (find(section, "append", section));
404
405 return ret;
406 }
407
408
409 bool
410 IniFile::sectionExists(const string &sectionName) const
411 {
412 return findSection(sectionName) != NULL;
413 }
414
415
416 bool
417 IniFile::Section::printUnreferenced(const string &sectionName)
418 {
419 bool unref = false;
420 bool search_unref_entries = false;
421 vector<string> unref_ok_entries;
422
423 Entry *entry = findEntry("unref_entries_ok");
424 if (entry != NULL) {
425 tokenize(unref_ok_entries, entry->getValue(), ' ');
426 if (unref_ok_entries.size()) {
427 search_unref_entries = true;
428 }
429 }
430
431 for (EntryTable::iterator ei = table.begin();
432 ei != table.end(); ++ei) {
433 const string &entryName = ei->first;
434 Entry *entry = ei->second;
435
436 if (entryName == "unref_section_ok" ||
437 entryName == "unref_entries_ok")
438 {
439 continue;
440 }
441
442 if (!entry->isReferenced()) {
443 if (search_unref_entries &&
444 (std::find(unref_ok_entries.begin(), unref_ok_entries.end(),
445 entryName) != unref_ok_entries.end()))
446 {
447 continue;
448 }
449
450 cerr << "Parameter " << sectionName << ":" << entryName
451 << " not referenced." << endl;
452 unref = true;
453 }
454 }
455
456 return unref;
457 }
458
459
460 bool
461 IniFile::printUnreferenced()
462 {
463 bool unref = false;
464
465 for (SectionTable::iterator i = table.begin();
466 i != table.end(); ++i) {
467 const string &sectionName = i->first;
468 Section *section = i->second;
469
470 if (!section->isReferenced()) {
471 if (section->findEntry("unref_section_ok") == NULL) {
472 cerr << "Section " << sectionName << " not referenced."
473 << endl;
474 unref = true;
475 }
476 }
477 else {
478 if (section->printUnreferenced(sectionName)) {
479 unref = true;
480 }
481 }
482 }
483
484 return unref;
485 }
486
487
488 void
489 IniFile::Section::dump(const string &sectionName)
490 {
491 for (EntryTable::iterator ei = table.begin();
492 ei != table.end(); ++ei) {
493 cout << sectionName << ": " << (*ei).first << " => "
494 << (*ei).second->getValue() << "\n";
495 }
496 }
497
498 void
499 IniFile::dump()
500 {
501 for (SectionTable::iterator i = table.begin();
502 i != table.end(); ++i) {
503 i->second->dump(i->first);
504 }
505 }