2 * Copyright (c) 2001-2004 The Regents of The University of Michigan
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.
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.
34 #include <sys/signal.h>
35 #include <sys/types.h>
38 #if defined(__OpenBSD__) || defined(__APPLE__)
49 #include <ext/stdio_filebuf.h>
55 #include "base/inifile.hh"
56 #include "base/str.hh"
65 SectionTable::iterator i
= table
.begin();
66 SectionTable::iterator end
= table
.end();
77 IniFile::loadCPP(const string
&file
, vector
<char *> &cppArgs
)
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());
91 const char *cfile
= file
.c_str();
92 char *dir
= basename(cfile
);
94 if (*dir
!= '.' && dir
!= cfile
) {
98 dir_arg
= new char[arg
.size() + 1];
99 strcpy(dir_arg
, arg
.c_str());
106 char tempfile
[] = "/tmp/configXXXXXX";
107 fd
[0] = fd
[1] = mkstemp(tempfile
);
116 char filename
[FILENAME_MAX
];
117 string::size_type i
= file
.copy(filename
, sizeof(filename
) - 1);
120 int arg_count
= cppArgs
.size();
122 char **args
= new char *[arg_count
+ 20];
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";
134 for (int i
= 0; i
< arg_count
; i
++)
135 args
[nextArg
++] = cppArgs
[i
];
138 args
[nextArg
++] = dir_arg
;
140 args
[nextArg
++] = filename
;
141 args
[nextArg
++] = NULL
;
143 close(STDOUT_FILENO
);
144 if (dup2(fd
[1], STDOUT_FILENO
) == -1)
153 waitpid(pid
, &retval
, 0);
157 // check for normal completion of CPP
158 if (!WIFEXITED(retval
) || WEXITSTATUS(retval
) != 0)
164 lseek(fd
[0], 0, SEEK_SET
);
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
));
174 if (fbuf
.is_open()) {
194 IniFile::load(const string
&file
)
196 ifstream
f(file
.c_str());
206 IniFile::Entry::getValue() const
214 IniFile::Section::addEntry(const std::string
&entryName
,
215 const std::string
&value
,
218 EntryTable::iterator ei
= table
.find(entryName
);
220 if (ei
== table
.end()) {
222 table
[entryName
] = new Entry(value
);
225 // append new reult to old entry
226 ei
->second
->appendValue(value
);
229 // override old entry
230 ei
->second
->setValue(value
);
236 IniFile::Section::add(const std::string
&assignment
)
238 string::size_type offset
= assignment
.find('=');
239 if (offset
== string::npos
) {
241 cerr
<< "Can't parse .ini line " << assignment
<< endl
;
245 // if "+=" rather than just "=" then append value
246 bool append
= (assignment
[offset
-1] == '+');
248 string entryName
= assignment
.substr(0, append
? offset
-1 : offset
);
249 string value
= assignment
.substr(offset
+ 1);
251 eat_white(entryName
);
254 addEntry(entryName
, value
, append
);
260 IniFile::Section::findEntry(const std::string
&entryName
) const
264 EntryTable::const_iterator ei
= table
.find(entryName
);
266 return (ei
== table
.end()) ? NULL
: ei
->second
;
271 IniFile::addSection(const string
§ionName
)
273 SectionTable::iterator i
= table
.find(sectionName
);
275 if (i
!= table
.end()) {
280 Section
*sec
= new Section();
281 table
[sectionName
] = sec
;
288 IniFile::findSection(const string
§ionName
) const
290 SectionTable::const_iterator i
= table
.find(sectionName
);
292 return (i
== table
.end()) ? NULL
: i
->second
;
296 // Take string of the form "<section>:<parameter>=<value>" and add to
297 // database. Return true if successful, false if parse error.
299 IniFile::add(const string
&str
)
302 string::size_type offset
= str
.find(':');
303 if (offset
== string::npos
) // no ':' found
306 string sectionName
= str
.substr(0, offset
);
307 string rest
= str
.substr(offset
+ 1);
309 eat_white(sectionName
);
310 Section
*s
= addSection(sectionName
);
316 IniFile::load(istream
&f
)
318 Section
*section
= NULL
;
321 f
>> ws
; // Eat whitespace
328 if (line
.size() == 0)
332 int last
= line
.size() - 1;
334 if (line
[0] == '[' && line
[last
] == ']') {
335 string sectionName
= line
.substr(1, last
- 1);
336 eat_white(sectionName
);
337 section
= addSection(sectionName
);
344 if (!section
->add(line
))
352 IniFile::find(const string
§ionName
, const string
&entryName
,
355 Section
*section
= findSection(sectionName
);
359 Entry
*entry
= section
->findEntry(entryName
);
363 value
= entry
->getValue();
369 IniFile::findDefault(const string
&_section
, const string
&entry
,
372 string section
= _section
;
373 while (!findAppend(section
, entry
, value
)) {
374 if (!find(section
, "default", section
)) {
383 IniFile::findAppend(const string
&_section
, const string
&entry
,
386 string section
= _section
;
392 if (find(section
, entry
, val
)) {
403 } while (find(section
, "append", section
));
410 IniFile::sectionExists(const string
§ionName
) const
412 return findSection(sectionName
) != NULL
;
417 IniFile::Section::printUnreferenced(const string
§ionName
)
420 bool search_unref_entries
= false;
421 vector
<string
> unref_ok_entries
;
423 Entry
*entry
= findEntry("unref_entries_ok");
425 tokenize(unref_ok_entries
, entry
->getValue(), ' ');
426 if (unref_ok_entries
.size()) {
427 search_unref_entries
= true;
431 for (EntryTable::iterator ei
= table
.begin();
432 ei
!= table
.end(); ++ei
) {
433 const string
&entryName
= ei
->first
;
434 Entry
*entry
= ei
->second
;
436 if (entryName
== "unref_section_ok" ||
437 entryName
== "unref_entries_ok")
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()))
450 cerr
<< "Parameter " << sectionName
<< ":" << entryName
451 << " not referenced." << endl
;
461 IniFile::printUnreferenced()
465 for (SectionTable::iterator i
= table
.begin();
466 i
!= table
.end(); ++i
) {
467 const string
§ionName
= i
->first
;
468 Section
*section
= i
->second
;
470 if (!section
->isReferenced()) {
471 if (section
->findEntry("unref_section_ok") == NULL
) {
472 cerr
<< "Section " << sectionName
<< " not referenced."
478 if (section
->printUnreferenced(sectionName
)) {
489 IniFile::Section::dump(const string
§ionName
)
491 for (EntryTable::iterator ei
= table
.begin();
492 ei
!= table
.end(); ++ei
) {
493 cout
<< sectionName
<< ": " << (*ei
).first
<< " => "
494 << (*ei
).second
->getValue() << "\n";
501 for (SectionTable::iterator i
= table
.begin();
502 i
!= table
.end(); ++i
) {
503 i
->second
->dump(i
->first
);