inifile.cc (5202:ff56fa8c2091) inifile.cc (5544:65b27e939646)
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;

--- 15 unchanged lines hidden (view full) ---

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
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;

--- 15 unchanged lines hidden (view full) ---

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>
32#include <fstream>
33#include <iostream>
47
48#include <vector>
49#include <string>
50
51#include "base/inifile.hh"
52#include "base/str.hh"
53
54using namespace std;
55

--- 6 unchanged lines hidden (view full) ---

62 SectionTable::iterator end = table.end();
63
64 while (i != end) {
65 delete (*i).second;
66 ++i;
67 }
68}
69
34#include <vector>
35#include <string>
36
37#include "base/inifile.hh"
38#include "base/str.hh"
39
40using namespace std;
41

--- 6 unchanged lines hidden (view full) ---

48 SectionTable::iterator end = table.end();
49
50 while (i != end) {
51 delete (*i).second;
52 ++i;
53 }
54}
55
70
71#ifdef USE_CPP
72bool
56bool
73IniFile::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
167bool
168IniFile::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);

--- 263 unchanged lines hidden ---
57IniFile::load(const string &file)
58{
59 ifstream f(file.c_str());
60
61 if (!f.is_open())
62 return false;
63
64 return load(f);

--- 263 unchanged lines hidden ---