1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
// **********************************************************************
//
// Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Slice/Preprocessor.h>
#include <algorithm>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
# include <sys/wait.h>
#endif
using namespace std;
using namespace Slice;
Slice::Preprocessor::Preprocessor(const string& path, const string& fileName, const string& args) :
_path(path),
_fileName(fileName),
_args(args),
_cppHandle(0)
{
}
Slice::Preprocessor::~Preprocessor()
{
if(_cppHandle)
{
close();
}
}
string
Slice::Preprocessor::getBaseName()
{
string base(_fileName);
string suffix;
string::size_type pos = base.rfind('.');
if(pos != string::npos)
{
base.erase(pos);
}
return base;
}
FILE*
Slice::Preprocessor::preprocess(bool keepComments)
{
if(!checkInputFile())
{
return 0;
}
string cmd = searchIceCpp();
if(cmd.empty())
{
return 0;
}
if(keepComments)
{
cmd += " -C";
}
cmd += " " + _args + " " + _fileName;
//
// Open a pipe for reading to redirect icecpp output to _cppHandle.
//
#ifdef _WIN32
_cppHandle = _popen(cmd.c_str(), "r");
#else
_cppHandle = popen(cmd.c_str(), "r");
#endif
return _cppHandle;
}
void
Slice::Preprocessor::printMakefileDependencies(Language lang)
{
if(!checkInputFile())
{
return;
}
string cmd = searchIceCpp();
if(cmd.empty())
{
return;
}
cmd += " -M " + _args + " " + _fileName;
#ifdef _WIN32
FILE* cppHandle = _popen(cmd.c_str(), "r");
#else
FILE* cppHandle = popen(cmd.c_str(), "r");
#endif
/*
* icecpp emits dependencies in any of the following formats, depending on the
* length of the filenames:
*
* x.cpp: /path/x.ice /path/y.ice
*
* x.cpp: /path/x.ice \
* /path/y.ice
*
* x.cpp: /path/x.ice /path/y.ice \
* /path/z.ice
*
* x.cpp: \
* /path/x.ice
*
* x.cpp: \
* /path/x.ice \
* /path/y.ice
*
* Spaces embedded within filenames are escaped with a backslash. Note that
* Windows filenames may contain colons.
*
*/
switch(lang)
{
case CPlusPlus:
{
char buf[1024];
while(fgets(buf, sizeof(buf), cppHandle) != NULL)
{
fputs(buf, stdout);
}
break;
}
case Java:
{
//
// We want to shift the files left one position, so that
// "x.cpp: x.ice y.ice" becomes "x.ice: y.ice".
//
// Since the pipe input can be returned a line at a time, we collect
// all of the output into one string before manipulating it.
//
string deps;
char buf[1024];
while(fgets(buf, sizeof(buf), cppHandle) != NULL)
{
deps.append(buf, strlen(buf));
}
//
// Remove the first file.
//
string::size_type start = deps.find(".cpp:");
assert(start != string::npos);
start = deps.find_first_not_of(" \t\r\n\\", start + 5); // Skip to beginning of next file.
assert(start != string::npos);
deps.erase(0, start);
//
// Find end of next file.
//
string::size_type pos = 0;
while((pos = deps.find_first_of(" :\t\r\n\\", pos + 1)) != string::npos)
{
if(deps[pos] == ':')
{
deps.insert(pos, 1, '\\'); // Escape colons.
++pos;
}
else if(deps[pos] == '\\') // Ignore escaped characters.
{
++pos;
}
else
{
break;
}
}
if(pos == string::npos)
{
deps.append(":");
}
else
{
deps.insert(pos, 1, ':');
}
fputs(deps.c_str(), stdout);
break;
}
case CSharp:
{
//
// Change .cpp suffix to .cs suffix.
//
char buf[1024];
while(fgets(buf, sizeof(buf), cppHandle) != NULL)
{
char* dot;
char* colon = strchr(buf, ':');
if(colon != NULL)
{
*colon = '\0';
dot = strrchr(buf, '.');
*colon = ':';
if(dot != NULL)
{
if(strncmp(dot, ".cpp:", 5) == 0)
{
*dot = '\0';
fputs(buf, stdout);
fputs(".cs", stdout);
fputs(colon, stdout);
continue;
}
}
}
fputs(buf, stdout);
}
break;
}
case VisualBasic:
{
//
// Change .cpp suffix to .vb suffix.
//
char buf[1024];
while(fgets(buf, sizeof(buf), cppHandle) != NULL)
{
char* dot;
char* colon = strchr(buf, ':');
if(colon != NULL)
{
*colon = '\0';
dot = strrchr(buf, '.');
*colon = ':';
if(dot != NULL)
{
if(strncmp(dot, ".cpp:", 5) == 0)
{
*dot = '\0';
fputs(buf, stdout);
fputs(".vb", stdout);
fputs(colon, stdout);
continue;
}
}
}
fputs(buf, stdout);
}
break;
}
default:
{
abort();
break;
}
}
}
bool
Slice::Preprocessor::close()
{
assert(_cppHandle);
#ifndef WIN32
int status = pclose(_cppHandle);
_cppHandle = 0;
if(WIFEXITED(status) && WEXITSTATUS(status) != 0)
{
return false;
}
#else
int status = _pclose(_cppHandle);
_cppHandle = 0;
if(status != 0)
{
return false;
}
#endif
return true;
}
bool
Slice::Preprocessor::checkInputFile()
{
string base(_fileName);
string suffix;
string::size_type pos = base.rfind('.');
if(pos != string::npos)
{
suffix = base.substr(pos);
transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
}
if(suffix != ".ice")
{
cerr << _path << ": input files must end with `.ice'" << endl;
return false;
}
ifstream test(_fileName.c_str());
if(!test)
{
cerr << _path << ": can't open `" << _fileName << "' for reading" << endl;
return false;
}
test.close();
return true;
}
string
Slice::Preprocessor::searchIceCpp()
{
#ifndef WIN32
const char* icecpp = "icecpp";
#else
const char* icecpp = "icecpp.exe";
#endif
string::size_type pos = _path.find_last_of("/\\");
if(pos != string::npos)
{
string path = _path.substr(0, pos + 1);
path += icecpp;
struct stat st;
if(stat(path.c_str(), &st) == 0)
{
#ifndef WIN32
if(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
#else
if(st.st_mode & (S_IEXEC))
#endif
{
return path;
}
}
}
return icecpp;
}
|