Merge ktlim@zizzer:/bk/newmem
[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 char **args = new 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("g++", args);
140
141 exit(0);
142 }
143
144 int retval;
145 waitpid(pid, &retval, 0);
146
147 delete [] dir_arg;
148
149 // check for normal completion of CPP
150 if (!WIFEXITED(retval) || WEXITSTATUS(retval) != 0)
151 return false;
152
153 close(tmp_fd);
154
155 bool status = false;
156
157 status = load(tempfile);
158
159 unlink(tempfile);
160
161 return status;
162 }
163 #endif
164
165 bool
166 IniFile::load(const string &file)
167 {
168 ifstream f(file.c_str());
169
170 if (!f.is_open())
171 return false;
172
173 return load(f);
174 }
175
176
177 const string &
178 IniFile::Entry::getValue() const
179 {
180 referenced = true;
181 return value;
182 }
183
184
185 void
186 IniFile::Section::addEntry(const std::string &entryName,
187 const std::string &value,
188 bool append)
189 {
190 EntryTable::iterator ei = table.find(entryName);
191
192 if (ei == table.end()) {
193 // new entry
194 table[entryName] = new Entry(value);
195 }
196 else if (append) {
197 // append new reult to old entry
198 ei->second->appendValue(value);
199 }
200 else {
201 // override old entry
202 ei->second->setValue(value);
203 }
204 }
205
206
207 bool
208 IniFile::Section::add(const std::string &assignment)
209 {
210 string::size_type offset = assignment.find('=');
211 if (offset == string::npos) {
212 // no '=' found
213 cerr << "Can't parse .ini line " << assignment << endl;
214 return false;
215 }
216
217 // if "+=" rather than just "=" then append value
218 bool append = (assignment[offset-1] == '+');
219
220 string entryName = assignment.substr(0, append ? offset-1 : offset);
221 string value = assignment.substr(offset + 1);
222
223 eat_white(entryName);
224 eat_white(value);
225
226 addEntry(entryName, value, append);
227 return true;
228 }
229
230
231 IniFile::Entry *
232 IniFile::Section::findEntry(const std::string &entryName) const
233 {
234 referenced = true;
235
236 EntryTable::const_iterator ei = table.find(entryName);
237
238 return (ei == table.end()) ? NULL : ei->second;
239 }
240
241
242 IniFile::Section *
243 IniFile::addSection(const string &sectionName)
244 {
245 SectionTable::iterator i = table.find(sectionName);
246
247 if (i != table.end()) {
248 return i->second;
249 }
250 else {
251 // new entry
252 Section *sec = new Section();
253 table[sectionName] = sec;
254 return sec;
255 }
256 }
257
258
259 IniFile::Section *
260 IniFile::findSection(const string &sectionName) const
261 {
262 SectionTable::const_iterator i = table.find(sectionName);
263
264 return (i == table.end()) ? NULL : i->second;
265 }
266
267
268 // Take string of the form "<section>:<parameter>=<value>" and add to
269 // database. Return true if successful, false if parse error.
270 bool
271 IniFile::add(const string &str)
272 {
273 // find ':'
274 string::size_type offset = str.find(':');
275 if (offset == string::npos) // no ':' found
276 return false;
277
278 string sectionName = str.substr(0, offset);
279 string rest = str.substr(offset + 1);
280
281 eat_white(sectionName);
282 Section *s = addSection(sectionName);
283
284 return s->add(rest);
285 }
286
287 bool
288 IniFile::load(istream &f)
289 {
290 Section *section = NULL;
291
292 while (!f.eof()) {
293 f >> ws; // Eat whitespace
294 if (f.eof()) {
295 break;
296 }
297
298 string line;
299 getline(f, line);
300 if (line.size() == 0)
301 continue;
302
303 eat_end_white(line);
304 int last = line.size() - 1;
305
306 if (line[0] == '[' && line[last] == ']') {
307 string sectionName = line.substr(1, last - 1);
308 eat_white(sectionName);
309 section = addSection(sectionName);
310 continue;
311 }
312
313 if (section == NULL)
314 continue;
315
316 if (!section->add(line))
317 return false;
318 }
319
320 return true;
321 }
322
323 bool
324 IniFile::find(const string &sectionName, const string &entryName,
325 string &value) const
326 {
327 Section *section = findSection(sectionName);
328 if (section == NULL)
329 return false;
330
331 Entry *entry = section->findEntry(entryName);
332 if (entry == NULL)
333 return false;
334
335 value = entry->getValue();
336
337 return true;
338 }
339
340 bool
341 IniFile::sectionExists(const string &sectionName) const
342 {
343 return findSection(sectionName) != NULL;
344 }
345
346
347 bool
348 IniFile::Section::printUnreferenced(const string &sectionName)
349 {
350 bool unref = false;
351 bool search_unref_entries = false;
352 vector<string> unref_ok_entries;
353
354 Entry *entry = findEntry("unref_entries_ok");
355 if (entry != NULL) {
356 tokenize(unref_ok_entries, entry->getValue(), ' ');
357 if (unref_ok_entries.size()) {
358 search_unref_entries = true;
359 }
360 }
361
362 for (EntryTable::iterator ei = table.begin();
363 ei != table.end(); ++ei) {
364 const string &entryName = ei->first;
365 Entry *entry = ei->second;
366
367 if (entryName == "unref_section_ok" ||
368 entryName == "unref_entries_ok")
369 {
370 continue;
371 }
372
373 if (!entry->isReferenced()) {
374 if (search_unref_entries &&
375 (std::find(unref_ok_entries.begin(), unref_ok_entries.end(),
376 entryName) != unref_ok_entries.end()))
377 {
378 continue;
379 }
380
381 cerr << "Parameter " << sectionName << ":" << entryName
382 << " not referenced." << endl;
383 unref = true;
384 }
385 }
386
387 return unref;
388 }
389
390
391 bool
392 IniFile::printUnreferenced()
393 {
394 bool unref = false;
395
396 for (SectionTable::iterator i = table.begin();
397 i != table.end(); ++i) {
398 const string &sectionName = i->first;
399 Section *section = i->second;
400
401 if (!section->isReferenced()) {
402 if (section->findEntry("unref_section_ok") == NULL) {
403 cerr << "Section " << sectionName << " not referenced."
404 << endl;
405 unref = true;
406 }
407 }
408 else {
409 if (section->printUnreferenced(sectionName)) {
410 unref = true;
411 }
412 }
413 }
414
415 return unref;
416 }
417
418
419 void
420 IniFile::Section::dump(const string &sectionName)
421 {
422 for (EntryTable::iterator ei = table.begin();
423 ei != table.end(); ++ei) {
424 cout << sectionName << ": " << (*ei).first << " => "
425 << (*ei).second->getValue() << "\n";
426 }
427 }
428
429 void
430 IniFile::dump()
431 {
432 for (SectionTable::iterator i = table.begin();
433 i != table.end(); ++i) {
434 i->second->dump(i->first);
435 }
436 }