809cbe172c521ea1bf9bab9472044dffcec05feb
[gem5.git] / src / base / inifile.cc
1 /*
2 * Copyright (c) 2001-2005 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 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 */
31
32 #define USE_CPP
33
34 #ifdef USE_CPP
35 #include <sys/signal.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38
39 #include <libgen.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #endif
44
45 #include <fstream>
46 #include <iostream>
47
48 #include <vector>
49 #include <string>
50
51 #include "base/inifile.hh"
52 #include "base/str.hh"
53
54 using namespace std;
55
56 IniFile::IniFile()
57 {}
58
59 IniFile::~IniFile()
60 {
61 SectionTable::iterator i = table.begin();
62 SectionTable::iterator end = table.end();
63
64 while (i != end) {
65 delete (*i).second;
66 ++i;
67 }
68 }
69
70
71 #ifdef USE_CPP
72 bool
73 IniFile::loadCPP(const string &file, vector<char *> &cppArgs)
74 {
75 // Open the file just to verify that we can. Otherwise if the
76 // file doesn't exist or has bad permissions the user will get
77 // confusing errors from cpp/g++.
78 ifstream tmpf(file.c_str());
79
80 if (!tmpf.is_open())
81 return false;
82
83 tmpf.close();
84
85 char *cfile = strncpy(new char[file.size() + 1], file.c_str(),
86 file.size());
87 char *dir = dirname(cfile);
88 char *dir_arg = NULL;
89 if (*dir != '.') {
90 string arg = "-I";
91 arg += dir;
92
93 dir_arg = new char[arg.size() + 1];
94 strncpy(dir_arg, arg.c_str(), arg.size());
95 }
96
97 delete [] cfile;
98
99 char tempfile[] = "/tmp/configXXXXXX";
100 int tmp_fd = mkstemp(tempfile);
101
102 int pid = fork();
103
104 if (pid == -1)
105 return false;
106
107 if (pid == 0) {
108 char filename[FILENAME_MAX];
109 string::size_type i = file.copy(filename, sizeof(filename) - 1);
110 filename[i] = '\0';
111
112 int arg_count = cppArgs.size();
113
114 const char **args = new const char *[arg_count + 20];
115
116 int nextArg = 0;
117 args[nextArg++] = "g++";
118 args[nextArg++] = "-E";
119 args[nextArg++] = "-P";
120 args[nextArg++] = "-nostdinc";
121 args[nextArg++] = "-nostdinc++";
122 args[nextArg++] = "-x";
123 args[nextArg++] = "c++";
124 args[nextArg++] = "-undef";
125
126 for (int i = 0; i < arg_count; i++)
127 args[nextArg++] = cppArgs[i];
128
129 if (dir_arg)
130 args[nextArg++] = dir_arg;
131
132 args[nextArg++] = filename;
133 args[nextArg++] = NULL;
134
135 close(STDOUT_FILENO);
136 if (dup2(tmp_fd, STDOUT_FILENO) == -1)
137 exit(1);
138
139 // execvp signature is intentionally broken wrt const-ness for
140 // backwards compatibility... see man page
141 execvp("g++", const_cast<char * const *>(args));
142
143 exit(0);
144 }
145
146 int retval;
147 waitpid(pid, &retval, 0);
148
149 delete [] dir_arg;
150
151 // check for normal completion of CPP
152 if (!WIFEXITED(retval) || WEXITSTATUS(retval) != 0)
153 return false;
154
155 close(tmp_fd);
156
157 bool status = false;
158
159 status = load(tempfile);
160
161 unlink(tempfile);
162
163 return status;
164 }
165 #endif
166
167 bool
168 IniFile::load(const string &file)
169 {
170 ifstream f(file.c_str());
171
172 if (!f.is_open())
173 return false;
174
175 return load(f);
176 }
177
178
179 const string &
180 IniFile::Entry::getValue() const
181 {
182 referenced = true;
183 return value;
184 }
185
186
187 void
188 IniFile::Section::addEntry(const std::string &entryName,
189 const std::string &value,
190 bool append)
191 {
192 EntryTable::iterator ei = table.find(entryName);
193
194 if (ei == table.end()) {
195 // new entry
196 table[entryName] = new Entry(value);
197 }
198 else if (append) {
199 // append new reult to old entry
200 ei->second->appendValue(value);
201 }
202 else {
203 // override old entry
204 ei->second->setValue(value);
205 }
206 }
207
208
209 bool
210 IniFile::Section::add(const std::string &assignment)
211 {
212 string::size_type offset = assignment.find('=');
213 if (offset == string::npos) {
214 // no '=' found
215 cerr << "Can't parse .ini line " << assignment << endl;
216 return false;
217 }
218
219 // if "+=" rather than just "=" then append value
220 bool append = (assignment[offset-1] == '+');
221
222 string entryName = assignment.substr(0, append ? offset-1 : offset);
223 string value = assignment.substr(offset + 1);
224
225 eat_white(entryName);
226 eat_white(value);
227
228 addEntry(entryName, value, append);
229 return true;
230 }
231
232
233 IniFile::Entry *
234 IniFile::Section::findEntry(const std::string &entryName) const
235 {
236 referenced = true;
237
238 EntryTable::const_iterator ei = table.find(entryName);
239
240 return (ei == table.end()) ? NULL : ei->second;
241 }
242
243
244 IniFile::Section *
245 IniFile::addSection(const string &sectionName)
246 {
247 SectionTable::iterator i = table.find(sectionName);
248
249 if (i != table.end()) {
250 return i->second;
251 }
252 else {
253 // new entry
254 Section *sec = new Section();
255 table[sectionName] = sec;
256 return sec;
257 }
258 }
259
260
261 IniFile::Section *
262 IniFile::findSection(const string &sectionName) const
263 {
264 SectionTable::const_iterator i = table.find(sectionName);
265
266 return (i == table.end()) ? NULL : i->second;
267 }
268
269
270 // Take string of the form "<section>:<parameter>=<value>" and add to
271 // database. Return true if successful, false if parse error.
272 bool
273 IniFile::add(const string &str)
274 {
275 // find ':'
276 string::size_type offset = str.find(':');
277 if (offset == string::npos) // no ':' found
278 return false;
279
280 string sectionName = str.substr(0, offset);
281 string rest = str.substr(offset + 1);
282
283 eat_white(sectionName);
284 Section *s = addSection(sectionName);
285
286 return s->add(rest);
287 }
288
289 bool
290 IniFile::load(istream &f)
291 {
292 Section *section = NULL;
293
294 while (!f.eof()) {
295 f >> ws; // Eat whitespace
296 if (f.eof()) {
297 break;
298 }
299
300 string line;
301 getline(f, line);
302 if (line.size() == 0)
303 continue;
304
305 eat_end_white(line);
306 int last = line.size() - 1;
307
308 if (line[0] == '[' && line[last] == ']') {
309 string sectionName = line.substr(1, last - 1);
310 eat_white(sectionName);
311 section = addSection(sectionName);
312 continue;
313 }
314
315 if (section == NULL)
316 continue;
317
318 if (!section->add(line))
319 return false;
320 }
321
322 return true;
323 }
324
325 bool
326 IniFile::find(const string &sectionName, const string &entryName,
327 string &value) const
328 {
329 Section *section = findSection(sectionName);
330 if (section == NULL)
331 return false;
332
333 Entry *entry = section->findEntry(entryName);
334 if (entry == NULL)
335 return false;
336
337 value = entry->getValue();
338
339 return true;
340 }
341
342 bool
343 IniFile::sectionExists(const string &sectionName) const
344 {
345 return findSection(sectionName) != NULL;
346 }
347
348
349 bool
350 IniFile::Section::printUnreferenced(const string &sectionName)
351 {
352 bool unref = false;
353 bool search_unref_entries = false;
354 vector<string> unref_ok_entries;
355
356 Entry *entry = findEntry("unref_entries_ok");
357 if (entry != NULL) {
358 tokenize(unref_ok_entries, entry->getValue(), ' ');
359 if (unref_ok_entries.size()) {
360 search_unref_entries = true;
361 }
362 }
363
364 for (EntryTable::iterator ei = table.begin();
365 ei != table.end(); ++ei) {
366 const string &entryName = ei->first;
367 Entry *entry = ei->second;
368
369 if (entryName == "unref_section_ok" ||
370 entryName == "unref_entries_ok")
371 {
372 continue;
373 }
374
375 if (!entry->isReferenced()) {
376 if (search_unref_entries &&
377 (std::find(unref_ok_entries.begin(), unref_ok_entries.end(),
378 entryName) != unref_ok_entries.end()))
379 {
380 continue;
381 }
382
383 cerr << "Parameter " << sectionName << ":" << entryName
384 << " not referenced." << endl;
385 unref = true;
386 }
387 }
388
389 return unref;
390 }
391
392
393 bool
394 IniFile::printUnreferenced()
395 {
396 bool unref = false;
397
398 for (SectionTable::iterator i = table.begin();
399 i != table.end(); ++i) {
400 const string &sectionName = i->first;
401 Section *section = i->second;
402
403 if (!section->isReferenced()) {
404 if (section->findEntry("unref_section_ok") == NULL) {
405 cerr << "Section " << sectionName << " not referenced."
406 << endl;
407 unref = true;
408 }
409 }
410 else {
411 if (section->printUnreferenced(sectionName)) {
412 unref = true;
413 }
414 }
415 }
416
417 return unref;
418 }
419
420
421 void
422 IniFile::Section::dump(const string &sectionName)
423 {
424 for (EntryTable::iterator ei = table.begin();
425 ei != table.end(); ++ei) {
426 cout << sectionName << ": " << (*ei).first << " => "
427 << (*ei).second->getValue() << "\n";
428 }
429 }
430
431 void
432 IniFile::dump()
433 {
434 for (SectionTable::iterator i = table.begin();
435 i != table.end(); ++i) {
436 i->second->dump(i->first);
437 }
438 }