/
core.c
301 lines (274 loc) · 6.94 KB
/
core.c
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
/*
* Lint compiler interceptor
* Copyright (C) 2013 Bo Rydberg
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "core.h"
#include "util.h"
#define CANONICAL_TOOL_NAME "Lint Compiler Interceptor"
#define COPYRIGHT_STRING "Copyright (c) 2013 Bo Rydberg"
static char lint[] = "fake-lint-nt.exe";
static char const *copyright[] = {
COPYRIGHT_STRING,
"License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/>",
"This is free software: you are free to change and redistribute it.",
"There is NO WARRANTY, to the extent permitted by law.",
NULL
};
static char const *version[] = {
CANONICAL_TOOL_NAME " 0.0",
NULL
};
static char const *banner[] = {
CANONICAL_TOOL_NAME " " COPYRIGHT_STRING,
NULL
};
static char const *usage[] = {
"usage:",
" " TOOL_NAME " [options]",
" " TOOL_NAME " [options] compiler [compiler options]",
" compiler [compiler options] (via symbolic link)",
"",
"options:",
" -b, --no-banner suppress banner",
" -c, --no-compiler do not run compiler",
" -f, --force-lint run lint even after failed compile",
" -l, --no-lint do not run lint",
" -v, --verbose verbose output",
"",
" --help print this text and exit",
" --version print version and exit",
"",
"Report bugs to: mailing-address",
CANONICAL_TOOL_NAME " home page: <https://github.com/bolry/lci/>",
NULL
};
int force_lint = 0;
int run_compiler = 1;
int run_lint = 1;
int show_banner = 1;
static void fputa(char const *arr[], FILE * stream)
{
int i;
for (i = 0; arr[i] != NULL; ++i)
if (stream_format_output(stream, "%s\n", arr[i]) < 0) {
log_puts(LCI_SEV_ALERT, "cannot print\n");
exit(EXIT_FAILURE);
}
}
int lci_called_by_real_name(char const *path)
{
char *tmp_path;
char const *bname;
int same;
tmp_path = xstrdup(path);
bname = basename(tmp_path);
same = (strcmp(bname, TOOL_NAME) == 0);
free(tmp_path);
/*
* Trailing slash check because basename can remove it
*/
if (same)
same = (path[strlen(path) - 1u] != '/');
return same;
}
static void print_usage_on(FILE * stream)
{
fputa(usage, stream);
}
static void print_version_on(FILE * stream)
{
fputa(version, stream);
fputa(copyright, stream);
}
int parse_bool_flag(char const unknown_arg[], char const option[],
int unique_from)
{
int match;
if (unique_from < 0) {
log_puts(LCI_SEV_DEBUG, "exact match requested\n");
match = (strcmp(unknown_arg, option) == 0);
} else {
char const *const res = strstr(option, unknown_arg);
log_printf(LCI_SEV_DEBUG, "first %d must match\n", unique_from);
if (res != option)
match = 0;
else
match = (strlen(unknown_arg) >= (size_t) unique_from);
}
return match;
}
void remove_index(int *offset, int *cnt, char *vec[])
{
/*
* memmove does this
* for (int i = *offset; i < *cnt; ++i)
* vec[i] = vec[i + 1];
*/
memmove(&vec[*offset], &vec[*offset + 1],
sizeof(char *) * (*cnt - *offset));
--(*cnt);
--(*offset);
}
void lci_options(int *cnt, char *vec[])
{
int i;
if (*cnt < 2) {
print_usage_on(stderr);
exit(EXIT_FAILURE);
}
for (i = 1; i != *cnt; ++i) {
if (parse_bool_flag(vec[i], "-b", -1) ||
parse_bool_flag(vec[i], "--no-banner", 6)) {
log_puts(LCI_SEV_DEBUG, "no banner\n");
show_banner = 0;
remove_index(&i, cnt, &vec[0]);
continue;
}
if (parse_bool_flag(vec[i], "-c", -1) ||
parse_bool_flag(vec[i], "--no-compiler", 6)) {
log_puts(LCI_SEV_DEBUG, "no compiler\n");
run_compiler = 0;
remove_index(&i, cnt, &vec[0]);
continue;
}
if (parse_bool_flag(vec[i], "-f", -1) ||
parse_bool_flag(vec[i], "--force-lint", 3)) {
log_puts(LCI_SEV_DEBUG, "force lint\n");
force_lint = 1;
remove_index(&i, cnt, &vec[0]);
continue;
}
if (parse_bool_flag(vec[i], "-l", -1) ||
parse_bool_flag(vec[i], "--no-lint", 6)) {
log_puts(LCI_SEV_DEBUG, "no lint\n");
run_lint = 0;
remove_index(&i, cnt, &vec[0]);
continue;
}
if (parse_bool_flag(vec[i], "-v", -1) ||
parse_bool_flag(vec[i], "--verbose", 6)) {
log_puts(LCI_SEV_DEBUG, "verbose\n");
inc_severity_ceiling();
remove_index(&i, cnt, &vec[0]);
continue;
}
if (parse_bool_flag(vec[i], "--help", 3)) {
log_puts(LCI_SEV_DEBUG, "help\n");
print_usage_on(stdout);
exit(EXIT_SUCCESS);
}
if (parse_bool_flag(vec[i], "--version", 6)) {
log_puts(LCI_SEV_DEBUG, "version\n");
print_version_on(stdout);
exit(EXIT_SUCCESS);
}
break;
}
}
int will_compile_and_or_link(int argc, char *argv[])
{
return 1;
}
static void print_banner(void)
{
if (show_banner)
fputa(banner, stderr);
}
static void flush_all(void)
{
if (fflush(NULL) == EOF) {
perror(TOOL_NAME ": fflush");
exit(EXIT_FAILURE);
}
}
static void handle_possible_lci_options(int *argc, char *argv[])
{
if (lci_called_by_real_name(argv[0]))
lci_options(argc, &argv[0]);
}
static void only_run_lint_if_compile_and_or_link(int argc, char *argv[])
{
if (run_compiler)
if (!will_compile_and_or_link(argc, &argv[0]))
run_lint = 0;
}
int lci_main(int argc, char *argv[])
{
handle_possible_lci_options(&argc, &argv[0]);
print_banner();
only_run_lint_if_compile_and_or_link(argc, &argv[0]);
flush_all();
if (run_compiler && run_lint) {
/*
* run compiler first and if OK then run lint
*/
int status;
pid_t cpid;
pid_t w;
cpid = fork();
if (-1 == cpid) {
perror(TOOL_NAME ": fork");
exit(EXIT_FAILURE);
}
if (0 == cpid) {
(void)execvp(argv[1], &argv[1]);
perror(TOOL_NAME ": execvp");
_exit(EXIT_FAILURE);
}
errno = 0;
w = waitpid(cpid, &status, 0);
if (w != cpid) {
perror(TOOL_NAME ": waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status) && (WEXITSTATUS(status) != EXIT_SUCCESS)) {
if (force_lint) {
/*
* run lint anyway
*/
} else {
exit(WEXITSTATUS(status));
}
}
if (WIFSIGNALED(status))
exit(WTERMSIG(status));
argv[0] = lint;
(void)execvp(argv[0], &argv[0]);
perror(TOOL_NAME ": execvp");
} else if (run_compiler) {
(void)execvp(argv[1], &argv[1]);
perror(TOOL_NAME ": execvp");
} else if (run_lint) {
argv[0] = lint;
(void)execvp(argv[0], &argv[0]);
perror(TOOL_NAME ": execvp");
} else {
/*
* a do nothing option
*/
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}