forked from csutils/csdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcshtml.cc
192 lines (162 loc) · 5.95 KB
/
cshtml.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
/*
* Copyright (C) 2012 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/
#include "parser.hh"
#include "cwe-name-lookup.hh"
#include "deflookup.hh"
#include "instream.hh"
#include "regex.hh"
#include "version.hh"
#include "writer-html.hh"
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#ifndef DEFAULT_CWE_NAMES_FILE
# define DEFAULT_CWE_NAMES_FILE "/usr/share/csdiff/cwe-names.csv"
#endif
std::string titleFromFileName(const std::string &fileName)
{
if (!fileName.compare("-"))
return "";
const RE reNoVer("^(.*/)?[^/0-9]*$");
if (boost::regex_match(fileName, reNoVer))
// no version information -> bailing out
return "";
const RE reTitle("^(?:.*/)?([^/]*)\\.(?:err|js)$");
boost::smatch sm;
if (!boost::regex_match(fileName, sm, reTitle))
return "";
return sm[/* title */ 1];
}
int main(int argc, char *argv[])
{
using std::string;
const char *name = argv[0];
std::string fnCweNamesDefault = DEFAULT_CWE_NAMES_FILE;
if (!boost::filesystem::exists(fnCweNamesDefault))
// if the default file is not installed, do not use it as default
fnCweNamesDefault.clear();
namespace po = boost::program_options;
po::variables_map vm;
po::options_description desc(string("Usage: ") + name
+ " [options] proj.js, where options are", /* line_length */ 0x80);
typedef std::vector<string> TStringList;
string defUrlTemplate, fnBase, checkerIgnRegex, plainTextUrl, spPosition;
string fnCweNames;
try {
desc.add_options()
("cwe-names",
po::value(&fnCweNames)->default_value(fnCweNamesDefault),
"CSV file mapping CWE numbers to names")
("defect-url-template", po::value(&defUrlTemplate),
"e.g. http://localhost/index.php?proj=%d&defect=%d")
("diff-base", po::value(&fnBase),
"use the given list of defects as diff base")
("diff-base-ignore-checkers", po::value(&checkerIgnRegex),
"do not diff base for checkers matching the given regex")
("plain-text-url", po::value(&plainTextUrl),
"generate a link to plain-text version")
("scan-props-placement",
po::value<string>(&spPosition)->default_value("bottom"),
"placement of the table with scan properties: top, bottom, none")
("quiet,q", "do not report any parsing errors")
("help", "produce help message")
("version", "print version");
po::options_description hidden("");
hidden.add_options()
("input-file", po::value<TStringList>(), "input file");
po::positional_options_description p;
p.add("input-file", -1);
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
po::options_description opts;
opts.add(desc).add(hidden);
po::store(po::command_line_parser(argc, argv).
options(opts).positional(p).run(), vm);
po::notify(vm);
}
catch (po::error &e) {
std::cerr << name << ": error: " << e.what() << "\n\n";
desc.print(std::cerr);
return 1;
}
if (vm.count("help")) {
desc.print(std::cout);
return 0;
}
if (vm.count("version")) {
std::cout << CS_VERSION << "\n";
return 0;
}
if (!vm.count("input-file")) {
desc.print(std::cerr);
return 1;
}
const TStringList inputFiles(vm["input-file"].as<TStringList>());
if (1 != inputFiles.size()) {
desc.print(std::cerr);
return 1;
}
const string &fnInput = inputFiles.front();
const bool silent = vm.count("quiet");
try {
// initialize parser for .err
InStream strInput(fnInput, silent);
Parser pInput(strInput);
DefLookup baseLookup;
TScanProps baseProps;
// read old defects if given
if (!fnBase.empty()) {
InStream strBase(fnBase, silent);
Parser pBase(strBase);
Defect def;
while (pBase.getNext(&def))
baseLookup.hashDefect(def);
baseProps = pBase.getScanProps();
}
// initialize HTML writer
const std::string titleFallback = titleFromFileName(fnInput);
HtmlWriter writer(std::cout, titleFallback, defUrlTemplate, spPosition);
writer.setScanProps(pInput.getScanProps());
if (!plainTextUrl.empty())
writer.setPlainTextUrl(plainTextUrl);
if (!fnBase.empty()) {
const std::string diffTitleFallback = titleFromFileName(fnBase);
writer.setDiffBase(&baseLookup, checkerIgnRegex, baseProps,
diffTitleFallback);
}
// initialize CWE names lookup
CweNameLookup cweNames;
if (!fnCweNames.empty()) {
InStream strCweNames(std::move(fnCweNames));
cweNames.parse(strCweNames);
writer.setCweNameLookup(&cweNames);
}
// write HTML
writer.handleFile(pInput);
writer.flush();
return pInput.hasError();
}
catch (const InFileException &e) {
std::cerr << e.fileName << ": failed to open input file\n";
return EXIT_FAILURE;
}
catch (const std::exception &e) {
std::cerr << name << ": error: " << e.what() << "\n";
return EXIT_FAILURE;
}
}