/
xrl_parser_input.cc
458 lines (411 loc) · 10.8 KB
/
xrl_parser_input.cc
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
// vim:set sts=4 ts=8:
// Copyright (c) 2001-2009 XORP, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License, Version
// 2.1, June 1999 as published by the Free Software Foundation.
// Redistribution and/or modification of this program under the terms of
// any other version of the GNU Lesser General Public License is not
// permitted.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
// see the GNU Lesser General Public License, Version 2.1, a copy of
// which can be found in the XORP LICENSE.lgpl file.
//
// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net
#include "xrl_module.h"
#include "libxorp/xorp.h"
#include "libxorp/c_format.hh"
#include "libxorp/utils.hh"
#include "xrl_parser_input.hh"
// CPP # directives that are supported here
static const int OPENING_FILE = 1;
static const int RETURNING_TO_FILE = 2;
bool
XrlParserFileInput::eof() const
{
return _stack[0].input()->eof() && _inserted_lines.empty();
}
bool
XrlParserFileInput::slurp_line(string& line)
throw (XrlParserInputException)
{
// Check if we need to step down a stack level
if (stack_top().input()->eof()) {
if (stack_depth() > 1) {
close_input(stack_top().input());
pop_stack();
line = c_format("# %d \"%s\" %d", stack_top().line(),
stack_top().filename(), RETURNING_TO_FILE);
_inserted_lines.push_back("");
return true;
} else {
line = "";
return false;
}
}
stack_top().incr_line();
#ifdef XORP_USE_USTL
{
// Lame as can be, but uSTL doesn't have working getline(), so
// fake one out here..
line.clear();
char c[2];
while (stack_top().input()->remaining() > 0) {
stack_top().input()->read(&c[0], 1);
c[1] = 0;
if (c[0] == '\n')
break;
line.append(c);
}
}
#else
std::getline(*stack_top().input(), line);
#endif
// Okay, got line see if it's a pre-processor directive
for (string::const_iterator c = line.begin();
c != line.end(); c++) {
if (xorp_isspace(*c)) continue;
if ('#' == *c) {
// The following may throw an exception
line = try_include(c, line.end());
}
break;
}
return true;
}
static string
chomp(const string& input, const string& exclude = string(" \t"))
{
string r;
for (string::const_iterator i = input.begin(); i != input.end(); ++i) {
if (exclude.find(*i) == string::npos) {
r += *i;
}
}
return r;
}
string
XrlParserFileInput::try_include(string::const_iterator& begin,
const string::const_iterator& end)
throw (XrlParserInputException)
{
static const string h("#include");
for (string::const_iterator hi = h.begin(); hi != h.end(); hi++, begin++) {
if (begin == end || *begin != *hi)
xorp_throw(XrlParserInputException, "Unsupported # directive");
}
// Okay found include directive skip space
while (begin != end && xorp_isspace(*begin))
begin++;
// Find quote
char qc = 0;
string::const_iterator fn_start;
for (fn_start = begin; fn_start <= end; fn_start++) {
if (*fn_start == '"') {
fn_start++;
qc = '"';
break;
} else if (*fn_start == '<') {
fn_start++;
qc = '>';
break;
}
}
// Find ending quote
string::const_iterator fn_end;
for (fn_end = fn_start; fn_end <= end; fn_end++) {
if (*fn_end == qc) {
break;
}
}
if (fn_end >= end) {
xorp_throw(XrlParserInputException, "Malformed #include directive");
}
// Check for junk following end of filename
for (string::const_iterator junk = fn_end + 1; junk < end; junk++) {
if (!xorp_isspace(*junk)) {
xorp_throw (XrlParserInputException,
"Junk following filename in #include directive");
}
}
string filename(fn_start, fn_end);
push_stack(FileState(path_open_input(filename.c_str()), filename.c_str()));
return c_format("# %d \"%s\" %d", 1, filename.c_str(), OPENING_FILE);
}
string
XrlParserFileInput::stack_trace() const
{
string s;
for (size_t i = 0; i < _stack.size(); i++) {
s += string(" ", i);
s += c_format("From file \"%s\" line %d\n",
_stack[i].filename(), _stack[i].line());
}
return s;
}
ifstream*
XrlParserFileInput::path_open_input(const char* filename)
throw (XrlParserInputException)
{
// XXX We could check for recursive includes here
if (filename == 0)
return 0;
if (is_absolute_path(filename)) {
ifstream* pif = new ifstream(filename);
if (pif->good()) {
return pif;
}
delete pif;
} else {
// A relative path has been specified, therefore try to locate
// the file under one of our search paths.
string filename_s = unix_path_to_native(filename);
for (list<string>::const_iterator pi = _path.begin();
pi != _path.end(); pi++) {
const string& path = *pi;
if (path.size() == 0)
continue;
string path_file;
// Eliminate trailing slash from appended path, if any.
if (path[path.size() - 1] == PATH_DELIMITER_CHAR) {
path_file = path + filename_s;
} else {
path_file = path + PATH_DELIMITER_STRING + filename_s;
}
ifstream* pif = new ifstream(path_file.c_str());
if (pif->good()) {
return pif;
}
delete pif;
}
}
xorp_throw(XrlParserInputException, c_format("Could not open \"%s\": %s",
filename, strerror(errno)));
return 0;
}
void
XrlParserFileInput::close_input(istream* pif)
{
delete pif;
}
XrlParserFileInput::XrlParserFileInput(istream* input, const char* fname)
throw (XrlParserInputException)
: _own_bottom(false), _current_mode(NORMAL)
{
initialize_path();
push_stack(FileState(input, fname));
_inserted_lines.push_back(c_format("# 1 \"%s\"", fname));
}
XrlParserFileInput::XrlParserFileInput(const char* fname)
throw (XrlParserInputException)
: _own_bottom(true), _current_mode(NORMAL)
{
initialize_path();
push_stack(FileState(path_open_input(fname), fname));
_inserted_lines.push_back(c_format("# 1 \"%s\"", fname));
}
XrlParserFileInput::~XrlParserFileInput()
{
while (stack_depth() > 1) {
close_input(stack_top().input());
pop_stack();
}
if (_own_bottom) {
close_input(stack_top().input());
}
}
void
XrlParserFileInput::initialize_path()
{
_path.push_back(".");
}
void
XrlParserFileInput::push_stack(const FileState& fs)
throw (XrlParserInputException)
{
if (fs.input()->good() == false) {
xorp_throw (XrlParserInputException, "Bad ifstream, rejected by stack");
}
_stack.push_back(fs);
}
void
XrlParserFileInput::pop_stack()
{
if (_stack.size())
_stack.pop_back();
}
XrlParserFileInput::FileState&
XrlParserFileInput::stack_top()
{
assert(_stack.size() != 0);
return _stack.back();
}
size_t
XrlParserFileInput::stack_depth() const
{
return _stack.size();
}
/* Return true if caller should try getline() again to get useful
* line data, false otherwise.
*/
bool
XrlParserFileInput::getline(string& line) throw (XrlParserInputException)
{
line.clear();
if (_inserted_lines.empty() == false) {
line = _inserted_lines.front();
_inserted_lines.erase(_inserted_lines.begin());
return true;
} else if (eof()) {
return false;
}
string r;
while (slurp_line(r)) {
if (filter_line(line, r) == false) {
break;
}
}
for (size_t i = 0; i < line.size(); i++) {
if (xorp_isspace(line[i]) == false)
return false; // Non blank line
}
line.clear();
return true;
}
/** @return true if terminating squote found, false otherwise.
* @sideeffect advance i to one past terminating squote.
*/
bool
advance_to_terminating_squote(string::const_iterator& i,
const string::const_iterator& end)
{
while (i != end) {
if (*i == '\'') {
i++;
return true;
}
i++;
}
return false;
}
/** @return true if terminating dquote found, false otherwise.
* @sideeffect advance i to one past terminating squote.
*/
bool
advance_to_terminating_dquote(string::const_iterator& i,
const string::const_iterator& end)
{
if (*i == '"') {
i++;
return true;
}
const string::const_iterator last_start = end - 1;
while (i != last_start) {
if (*i != '\\' && *(i + 1) == '"') {
i += 2;
return true;
}
i++;
}
i = end; // didn't find dquote goto end of line
return false;
}
/** @return true if terminating comment found, false otherwise.
* @sideeffect advance i to one past terminating squote.
*/
bool
advance_to_terminating_c_comment(string::const_iterator& i,
const string::const_iterator& end)
{
const string::const_iterator last_start = end - 1;
while (i != last_start) {
if (*i == '*' && *(i + 1) == '/') {
i += 2;
return true;
}
i++;
}
i = end; // didn't find comment goto end of line
return false;
}
/** @return true if another line is required because of a continuation,
* false otherwise.
*/
bool
XrlParserFileInput::filter_line(string& output, const string& input)
{
string::const_iterator ci = input.begin();
while (ci != input.end()) {
switch (_current_mode) {
case NORMAL: {
string::const_iterator began = ci;
while (ci != input.end()) {
if (*ci == '\"') {
_current_mode = IN_DQUOTE;
output += chomp(string(began, ++ci));
began = ci;
break;
} else if (*ci == '\'') {
_current_mode = IN_SQUOTE;
output += chomp(string(began, ++ci));
began = ci;
break;
} else if (input.end() - ci >= 2) {
if (*ci == '/' && *(ci + 1) == '*') {
// Found a C comment beginning
_current_mode = IN_C_COMMENT;
output += chomp(string(began, ci)) + string(" ");
ci += 2;
began = ci;
break;
} else if (*ci == '/' && *(ci + 1) == '/' &&
(ci == input.begin() ||
xorp_isspace(*(ci - 1)))) {
// Found a c++ comment
began = ci;
break;
}
} else if (input.end() - ci == 1 && *ci == '\\') {
output += chomp(string(began, ci));
return true;
}
ci++;
}
// do a copy from began to ci
output += chomp(string(began, ci));
break;
}
case IN_SQUOTE: {
string::const_iterator began = ci;
if (advance_to_terminating_squote(ci, input.end())) {
_current_mode = NORMAL;
}
output += string(began, ci);
break;
}
case IN_DQUOTE: {
string::const_iterator began = ci;
if (advance_to_terminating_dquote(ci, input.end())) {
_current_mode = NORMAL;
output += string(began, ci);
} else if (ci == input.end() && *(ci - 1) == '\\') {
output += string(began, ci - 1);
return true;
}
break;
}
case IN_C_COMMENT:
if (advance_to_terminating_c_comment(ci, input.end())) {
_current_mode = NORMAL;
}
/* do not copy output */
break;
}
}
return false;
}