-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
395 lines (324 loc) · 12.8 KB
/
main.cpp
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
/*
* MIT License
*
* Copyright (c) 2017 EDDR Software, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Changes:
* 2017-01-01: First & Last Name: What you did.
* 2017-05-31: Kevin Nesmith: Initial contribution.
* 2017-06-05: Kevin Nesmith: Fixed the reading of the last parameter on a line.
* There was an issue with multiple spaces.
* 2017-06-14: Dan Houlihan: Added check for INCLUDE recursion > 1 level
* Added check for comment at end of otherwise valid line
*
*/
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <memory.h>
#include <wordexp.h>
#include <linux/limits.h>
#include <boost/program_options.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
using namespace std;
using namespace boost::program_options;
using namespace boost::filesystem;
namespace fs = boost::filesystem;
bool iCompareChar(char a, char b)
{
return tolower(a)==tolower(b);
}
bool iCompareString(const string &a, const string &b)
{
if(a.length()==b.length()) {
return equal(b.begin(), b.end(), a.begin(), iCompareChar);
}
return false;
}
int fullPath(const char *pathIn, const path &base, path *outPath)
{
int retVal = 0;
try {
*outPath = canonical(pathIn, base);
} catch(const filesystem_error &error) {
if(is_symlink(error.path1())) {
cerr << "ERROR: Path " << error.path1() << " contains a broken symlink." <<
endl;
} else {
cerr << "ERROR: " << error.code().message() << " " << error.path1() << endl;
}
retVal = error.code().value();
}
return retVal;
}
bool parse(const string &varIn, const path &pathIn, path *pathOut,
const string &line = string(), int lineNum = 0)
{
bool retVal = true;
wordexp_t par;
memset(&par, 0, sizeof(par));
wordexp(varIn.c_str(), &par, WRDE_SHOWERR | WRDE_UNDEF);
char **wp = par.we_wordv;
if(par.we_wordc==0){
if(lineNum==0) {
cerr << "INVALID FILE: " << varIn << endl;
} else {
cerr << "ERROR LINE: " << pathIn.string() << ":" << lineNum << " => " << line << endl;
}
retVal = false;
}
for(size_t i = 0; i<par.we_wordc; ++i) {
path current = pathIn;
// check for comment at end of a line
if (*(wp[i]) == '#') {
break;
}
if(!is_directory(current)) {
current = pathIn.parent_path();
}
if(fullPath(wp[i], current, pathOut)) {
if(lineNum==0) {
cerr << "INVALID FILE: " << varIn << endl;
} else {
cerr << "INVALID LINE: " << pathIn.string() << ":" << lineNum << " => " << line << endl;
}
retVal = false;
break;
}
}
wordfree(&par);
return retVal;
}
void readUntilEOL(stringstream *stream, string *outVar)
{
string buffer;
getline(*stream, buffer);
// getline does not trim the beginning of line. There is usually whitespace.
buffer.erase(buffer.begin(), find_if(buffer.begin(), buffer.end(),
not1(ptr_fun<int, int>(isspace))));
*outVar = buffer;
}
void evaluateDefine(const variables_map &vm, const string &var1,
const string &var2, const path &fp, const string &line, int lineNum)
{
bool showLibs = vm.count("libs");
bool showCells = vm.count("cells");
bool showViews = vm.count("views");
bool showCellViews = vm.count("cellviews");
bool showDefines = vm.count("defines");
path libName(var1);
path libPath;
if(parse(var2, fp, &libPath, line, lineNum)) {
if(showDefines) {
cout << "DEFINE: " << fp.string() << ":" << lineNum << " \"" << line << "\"" <<
endl;
}
cout << "libPath: " << libPath.string() << endl;
if(showLibs) {
cout << "\tlibName: " << libName.string() << endl;
}
if(showCells || showViews || showCellViews) {
recursive_directory_iterator it(libPath), eod;
for(; it!=eod; ++it) {
if(*it!=libPath) {
if(is_directory(it->path()) &&
it->path().parent_path()==libPath) {
// Ignore hidden directories.
if(it->path().filename().string()[0]=='.'){
continue;
}
if(showCells) {
cout << "\t\tcellName: " << it->path().filename().string() << endl;
}
if(showViews || showCellViews) {
recursive_directory_iterator it2(it->path()), eod2;
for(; it2!=eod2; ++it2) {
if(!is_directory(it2->path()) &&
iCompareString(it2->path().extension().string(), string(".oa"))) {
string viewName = it2->path().parent_path().filename().string();
if(showCellViews) {
cout << "\t\tcellViewName: " << it->path().filename().string() << "/" <<
viewName << endl;
} else if(showViews) {
cout << "\t\t\tviewName: " << viewName << endl;
}
}
}
}
}
}
}
}
}
}
bool
isRecursiveInclude(const path &fp, std::vector<string> *pLibDefFiles)
{
for (int i = 0; i < pLibDefFiles->size(); ++i)
{
if (fp.string().c_str() == (*pLibDefFiles)[i])
{
return true;
}
}
return false;
}
void evaluate(const variables_map &vm, const string &libdefs, std::vector<string> *pLibDefFiles);
void evaluateInclude(const variables_map &vm, const string &var1,
const path &fp, const string &line, int lineNum,
std::vector<string> *pLibDefFiles)
{
bool showIncludes = vm.count("includes");
path includePath;
if(parse(var1, fp, &includePath, line, lineNum)) {
// check for indirect recursion (grandparents)
if (! isRecursiveInclude(fp, pLibDefFiles)) {
if(showIncludes) {
cout << "INCLUDE: " << fp.string() << ":" << lineNum << " " << includePath <<
endl;
}
pLibDefFiles->push_back(fp.string());
evaluate(vm, includePath.string(), pLibDefFiles);
} else {
cerr << "ERROR: Recursion in file => " << fp <<
" includes file => " << includePath <<
" which directly or indirectly references itself." << endl;
}
}
}
void evaluate(const variables_map &vm, const string &libdefs, std::vector<string> *pLibDefFiles)
{
bool showAssigns = vm.count("assigns");
bool showComments = vm.count("comments");
bool showEmptyLines = vm.count("emptylines");
path fp;
try {
if(parse(libdefs, current_path(), &fp)) {
ifstream libdefsFile(fp.string().c_str());
if(libdefsFile.is_open()) {
int lineNum = 0;
while(libdefsFile.good()) {
lineNum++;
string line;
getline(libdefsFile, line);
if(line.length()==0) {
if(showEmptyLines) {
cout << "EMPTY LINE: " << fp.string() << ":" <<
lineNum << endl;
}
continue;
}
string action;
stringstream stream(line);
stream >> action;
if(action[0]=='#') {
if(showComments) {
cout << "COMMENT: " << line << endl;
}
continue;
}
if(action=="DEFINE") {
string var1, var2;
stream >> var1;
// var2 requires reading until EOL;
readUntilEOL(&stream, &var2);
if(var1[0]=='#' || var2[0]=='#') {
cerr << "INVALID LINE: " << fp.string() << ":" <<
lineNum << " => " << line << endl;
continue;
}
if(var1.length()>0 && var2.length()>0) {
evaluateDefine(vm, var1, var2, fp, line, lineNum);
}
} else if(action=="INCLUDE") {
string var1;
// var1 requires reading until EOL
readUntilEOL(&stream, &var1);
if(var1[0]=='#') {
cerr << "INVALID LINE: " << fp.string() << ":" <<
lineNum << " => " << line << endl;
continue;
}
if(var1.length()>0) {
evaluateInclude(vm, var1, fp, line, lineNum, pLibDefFiles);
}
} else if(action=="ASSIGN") {
if(showAssigns) {
cout << "ASSIGN: " << fp.string() << ":" << lineNum << " \"" << line << "\"" <<
endl;
}
} else {
cerr << "INVALID LINE: " << fp.string() << ":" <<
lineNum << " => " << line << endl;
}
}
} else {
cerr << "ERROR: Library path " << libdefs <<
" does not exist." << endl;
}
}
} catch(...) {
cerr << "ERROR: Library path " << fp << " does not exist." << endl;
}
}
int main(int argc, char *argv[])
{
options_description desc("Supported options.");
desc.add_options()
("help,h", "Show this help message.")
("def,d", value<string>(), "Open this lib.defs file to parse.")
("libs", "Show lib names in the output.")
("cells", "Show cell names in the output.")
("views", "Show view names in the output.")
("cellviews",
"Show cell/views as single line items in the output.")
("defines", "Show DEFINE statements in the output.")
("includes", "Show INCLUDE statements in the output.")
("assigns", "Show ASSIGN statements in the output.")
("comments", "Show COMMENTS in the output.")
("emptylines", "Show EMPTY LINES in the output.")
("version,v", "Show version information.");
variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
if(vm.count("help")) {
cout << desc << endl;
return 0;
}
if(vm.count("version")) {
cout << "libdefsEval version 0.1.0" << endl;
return 0;
}
string libdefs;
if(vm.count("def")) {
cout << "def: " << vm["def"].as<string>() << endl;
libdefs = vm["def"].as<string>();
} else {
cerr << "ERROR: defs not defined." << endl;
cerr << " Try ./libdefsEval --defs lib.defs" << endl;
return 1;
}
std::vector<string> libDefFiles;
evaluate(vm, libdefs, &libDefFiles);
return 0;
}