/
options.c
490 lines (453 loc) · 17 KB
/
options.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "config.h"
#include "ignore.h"
#include "options.h"
#include "log.h"
#include "util.h"
const char *color_line_number = "\e[1;33m"; /* yellow with black background */
const char *color_match = "\e[30;43m"; /* black with yellow background */
const char *color_path = "\e[1;32m"; /* bold green */
/* TODO: try to obey out_fd? */
void usage() {
printf("Usage: ag [OPTIONS] PATTERN [PATH]\n\
\n\
Recursively search for PATTERN in PATH.\n\
Like grep or ack, but faster.\n\
\n\
Example: ag -i foo /bar/\n\
\n\
Search options:\n\
\n\
--ackmate Print results in AckMate-parseable format\n\
-a --all-types Search all files (doesn't include hidden files\n\
or patterns from ignore files)\n\
-A --after [LINES] Print lines before match (Default: 2)\n\
-B --before [LINES] Print lines after match (Default: 2)\n\
--[no]break Print newlines between matches in different files\n\
(Enabled by default)\n\
--[no]color Print color codes in results (Enabled by default)\n\
--color-line-number Color codes for line numbers (Default: 1;33)\n\
--color-match Color codes for result match numbers (Default: 30;43)\n\
--color-path Color codes for path names (Default: 1;32)\n\
--column Print column numbers in results\n\
--line-numbers Print line numbers even for streams\n\
-C --context [LINES] Print lines before and after matches (Default: 2)\n\
-D --debug Ridiculous debugging (probably not useful)\n\
--depth NUM Search up to NUM directories deep (Default: 25)\n\
-f --follow Follow symlinks\n\
--[no]group Same as --[no]break --[no]heading\n\
-g PATTERN Print filenames matching PATTERN\n\
-G, --file-search-regex PATTERN Limit search to filenames matching PATTERN\n\
--[no]heading\n\
--hidden Search hidden files (obeys .*ignore files)\n\
-i, --ignore-case Match case insensitively\n\
--ignore PATTERN Ignore files/directories matching PATTERN\n\
(literal file/directory names also allowed)\n\
--ignore-dir NAME Alias for --ignore for compatibility with ack.\n\
-l --files-with-matches Only print filenames that contain matches\n\
(don't print the matching lines)\n\
-L --files-without-matches\n\
Only print filenames that don't contain matches\n\
-m --max-count NUM Skip the rest of a file after NUM matches (Default: 10,000)\n\
-p --path-to-agignore STRING\n\
Use .agignore file at STRING\n\
--print-long-lines Print matches on very long lines (Default: >2k characters)\n\
-Q --literal Don't parse PATTERN as a regular expression\n\
-s --case-sensitive Match case sensitively (Enabled by default)\n\
-S --smart-case Match case insensitively unless PATTERN contains\n\
uppercase characters\n\
--search-binary Search binary files for matches\n\
--stats Print stats (files scanned, time taken, etc.)\n\
-t --all-text Search all text files (doesn't include hidden files)\n\
-u --unrestricted Search all files (ignore .agignore, .gitignore, etc.;\n\
searches binary and hidden files as well)\n\
-U --skip-vcs-ignores Ignore VCS ignore files\n\
(.gitigore, .hgignore, .svnignore; still obey .agignore)\n\
-v --invert-match\n\
-w --word-regexp Only match whole words\n\
-z --search-zip Search contents of compressed (e.g., gzip) files\n\
\n");
}
void print_version() {
printf("ag version %s\n", PACKAGE_VERSION);
}
void init_options() {
memset(&opts, 0, sizeof(opts));
opts.casing = CASE_SENSITIVE;
opts.color = TRUE;
opts.max_matches_per_file = 10000;
opts.max_search_depth = 25;
opts.print_break = TRUE;
opts.print_heading = TRUE;
opts.print_line_numbers = TRUE;
opts.recurse_dirs = TRUE;
opts.color_path = ag_strdup(color_path);
opts.color_match = ag_strdup(color_match);
opts.color_line_number = ag_strdup(color_line_number);
}
void cleanup_options() {
free(opts.color_path);
free(opts.color_match);
free(opts.color_line_number);
if (opts.query) {
free(opts.query);
}
pcre_free(opts.re);
if (opts.re_extra) {
/* Using pcre_free_study on pcre_extra* can segfault on some versions of PCRE */
pcre_free(opts.re_extra);
}
if (opts.ackmate_dir_filter) {
pcre_free(opts.ackmate_dir_filter);
}
if (opts.ackmate_dir_filter_extra) {
pcre_free(opts.ackmate_dir_filter_extra);
}
if (opts.file_search_regex) {
pcre_free(opts.file_search_regex);
}
if (opts.file_search_regex_extra) {
pcre_free(opts.file_search_regex_extra);
}
}
void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
int ch;
int i;
int path_len = 0;
int useless = 0;
int group = 1;
int help = 0;
int version = 0;
int opt_index = 0;
const char *home_dir = getenv("HOME");
char *ignore_file_path = NULL;
int needs_query = 1;
init_options();
struct option longopts[] = {
{ "ackmate", no_argument, &opts.ackmate, 1 },
{ "ackmate-dir-filter", required_argument, NULL, 0 },
{ "after", required_argument, NULL, 'A' },
{ "all-text", no_argument, NULL, 't' },
{ "all-types", no_argument, NULL, 'a' },
{ "before", required_argument, NULL, 'B' },
{ "break", no_argument, &opts.print_break, 1 },
{ "case-sensitive", no_argument, NULL, 's' },
{ "color", no_argument, &opts.color, 1 },
{ "color-path", required_argument, NULL, 0 },
{ "color-match", required_argument, NULL, 0 },
{ "color-line-number", required_argument, NULL, 0 },
{ "column", no_argument, &opts.column, 1 },
{ "context", optional_argument, NULL, 'C' },
{ "debug", no_argument, NULL, 'D' },
{ "depth", required_argument, NULL, 0 },
{ "file-search-regex", required_argument, NULL, 'G' },
{ "files-with-matches", no_argument, NULL, 'l' },
{ "files-without-matches", no_argument, NULL, 'L' },
{ "follow", no_argument, &opts.follow_symlinks, 1 },
{ "group", no_argument, &group, 1 },
{ "heading", no_argument, &opts.print_heading, 1 },
{ "help", no_argument, NULL, 'h' },
{ "hidden", no_argument, &opts.search_hidden_files, 1 },
{ "ignore", required_argument, NULL, 0 },
{ "ignore-dir", required_argument, NULL, 0 },
{ "ignore-case", no_argument, NULL, 'i' },
{ "invert-match", no_argument, &opts.invert_match, 1 },
{ "line-numbers", no_argument, &opts.print_line_numbers, 2 },
{ "literal", no_argument, NULL, 'Q' },
{ "match", no_argument, &useless, 0 },
{ "max-count", required_argument, NULL, 'm' },
{ "no-recurse", no_argument, NULL, 'n' },
{ "nobreak", no_argument, &opts.print_break, 0 },
{ "nocolor", no_argument, &opts.color, 0 },
{ "nofollow", no_argument, &opts.follow_symlinks, 0 },
{ "nogroup", no_argument, &group, 0 },
{ "noheading", no_argument, &opts.print_heading, 0 },
{ "nopager", no_argument, NULL, 0 },
{ "pager", required_argument, NULL, 0 },
{ "parallel", no_argument, &opts.parallel, 1},
{ "path-to-agignore", required_argument, NULL, 'p'},
{ "print-long-lines", no_argument, &opts.print_long_lines, 1 },
{ "recurse", no_argument, NULL, 'r' },
{ "search-binary", no_argument, &opts.search_binary_files, 1 },
{ "search-zip", no_argument, &opts.search_zip_files, 1 },
{ "search-files", no_argument, &opts.search_stream, 0 },
{ "skip-vcs-ignores", no_argument, NULL, 'U' },
{ "smart-case", no_argument, NULL, 'S' },
{ "stats", no_argument, &opts.stats, 1 },
{ "unrestricted", no_argument, NULL, 'u' },
{ "version", no_argument, &version, 1 },
{ "word-regexp", no_argument, NULL, 'w' },
{ "workers", required_argument, NULL, 0 },
{ NULL, 0, NULL, 0 }
};
if (argc < 2) {
usage();
exit(1);
}
/* stdin isn't a tty. something's probably being piped to ag */
if (!isatty(fileno(stdin))) {
opts.search_stream = 1;
}
/* If we're not outputting to a terminal. change output to:
* turn off colors
* print filenames on every line
*/
if (!isatty(fileno(stdout))) {
opts.color = 0;
group = 0;
/* Don't search the file that stdout is redirected to */
struct stat statbuf;
int rv;
rv = fstat(fileno(stdout), &statbuf);
if (rv != 0) {
die("Error fstat()ing stdout");
}
opts.stdout_inode = statbuf.st_ino;
}
while ((ch = getopt_long(argc, argv, "A:aB:C:DG:g:fhiLlm:np:QRrSsvVtuUwz", longopts, &opt_index)) != -1) {
switch (ch) {
case 'A':
opts.after = atoi(optarg);
break;
case 'a':
opts.search_all_files = 1;
opts.search_binary_files = 1;
break;
case 'B':
opts.before = atoi(optarg);
break;
case 'C':
if (optarg) {
opts.context = atoi(optarg);
if (opts.context == 0 && errno == EINVAL) {
/* This arg must be the search string instead of the context length */
optind--;
opts.context = DEFAULT_CONTEXT_LEN;
}
} else {
opts.context = DEFAULT_CONTEXT_LEN;
}
break;
case 'D':
set_log_level(LOG_LEVEL_DEBUG);
break;
case 'f':
opts.follow_symlinks = 1;
break;
case 'g':
needs_query = 0;
opts.match_files = 1;
/* Fall through and build regex */
case 'G':
compile_study(&opts.file_search_regex, &opts.file_search_regex_extra, optarg, 0, 0);
break;
case 'h':
help = 1;
break;
case 'i':
opts.casing = CASE_INSENSITIVE;
break;
case 'L':
opts.invert_match = 1;
/* fall through */
case 'l':
opts.print_filename_only = 1;
break;
case 'm':
opts.max_matches_per_file = atoi(optarg);
break;
case 'n':
opts.recurse_dirs = 0;
break;
case 'p':
opts.path_to_agignore = optarg;
break;
case 'Q':
opts.literal = 1;
break;
case 'R':
case 'r':
opts.recurse_dirs = 1;
break;
case 'S':
opts.casing = CASE_SMART;
break;
case 's':
opts.casing = CASE_SENSITIVE;
break;
case 't':
opts.search_all_files = 1;
break;
case 'u':
opts.search_binary_files = 1;
opts.search_all_files = 1;
opts.search_hidden_files = 1;
break;
case 'U':
opts.skip_vcs_ignores = 1;
break;
case 'v':
opts.invert_match = 1;
break;
case 'V':
version = 1;
break;
case 'w':
opts.word_regexp = 1;
break;
case 'z':
opts.search_zip_files = 1;
break;
case 0: /* Long option */
if (strcmp(longopts[opt_index].name, "ackmate-dir-filter") == 0) {
compile_study(&opts.ackmate_dir_filter, &opts.ackmate_dir_filter_extra, optarg, 0, 0);
break;
} else if (strcmp(longopts[opt_index].name, "depth") == 0) {
opts.max_search_depth = atoi(optarg);
break;
} else if (strcmp(longopts[opt_index].name, "ignore-dir") == 0) {
add_ignore_pattern(root_ignores, optarg);
break;
} else if (strcmp(longopts[opt_index].name, "ignore") == 0) {
add_ignore_pattern(root_ignores, optarg);
break;
} else if (strcmp(longopts[opt_index].name, "nopager") == 0) {
out_fd = stdout;
opts.pager = NULL;
break;
} else if (strcmp(longopts[opt_index].name, "pager") == 0) {
opts.pager = optarg;
break;
} else if (strcmp(longopts[opt_index].name, "workers") == 0) {
opts.workers = atoi(optarg);
break;
} else if (strcmp(longopts[opt_index].name, "color-line-number") == 0) {
free(opts.color_line_number);
ag_asprintf(&opts.color_line_number, "\e[%sm", optarg);
break;
} else if (strcmp(longopts[opt_index].name, "color-match") == 0) {
free(opts.color_match);
ag_asprintf(&opts.color_match, "\e[%sm", optarg);
break;
} else if (strcmp(longopts[opt_index].name, "color-path") == 0) {
free(opts.color_path);
ag_asprintf(&opts.color_path, "\e[%sm", optarg);
break;
}
/* Continue to usage if we don't recognize the option */
if (longopts[opt_index].flag != 0) {
break;
}
log_err("option %s does not take a value", longopts[opt_index].name);
default:
usage();
exit(1);
}
}
argc -= optind;
argv += optind;
if (opts.pager) {
out_fd = popen(opts.pager, "w");
if (!out_fd) {
perror("Failed to run pager");
exit(1);
}
}
if (help) {
usage();
exit(0);
}
if (version) {
print_version();
exit(0);
}
if (needs_query && argc == 0) {
log_err("What do you want to search for?");
exit(1);
}
if (home_dir && !opts.search_all_files) {
log_debug("Found user's home dir: %s", home_dir);
ag_asprintf(&ignore_file_path, "%s/%s", home_dir, ignore_pattern_files[0]);
load_ignore_patterns(root_ignores, ignore_file_path);
free(ignore_file_path);
}
if (opts.context > 0) {
opts.before = opts.context;
opts.after = opts.context;
}
if (opts.ackmate) {
opts.color = 0;
opts.print_break = 1;
group = 1;
opts.search_stream = 0;
}
if (opts.parallel) {
opts.search_stream = 0;
}
if (opts.print_heading == 0 || opts.print_break == 0) {
goto skip_group;
}
if (group) {
opts.print_heading = 1;
opts.print_break = 1;
} else {
opts.print_heading = 0;
opts.print_break = 0;
}
skip_group:;
if (opts.search_stream) {
opts.print_break = 0;
opts.print_heading = 0;
if (opts.print_line_numbers != 2)
opts.print_line_numbers = 0;
}
if (needs_query) {
opts.query = ag_strdup(argv[0]);
argc--;
argv++;
} else {
opts.query = ag_strdup(".");
}
opts.query_len = strlen(opts.query);
log_debug("Query is %s", opts.query);
if (opts.query_len == 0) {
log_err("Error: No query. What do you want to search for?");
exit(1);
}
if (!is_regex(opts.query)) {
opts.literal = 1;
}
char *path = NULL;
opts.paths_len = argc;
if (argc > 0) {
*paths = ag_calloc(sizeof(char*), argc + 1);
*base_paths = ag_calloc(sizeof(char*), argc + 1);
for (i = 0; i < argc; i++) {
path = ag_strdup(argv[i]);
path_len = strlen(path);
/* kill trailing slash */
if (path_len > 1 && path[path_len - 1] == '/') {
path[path_len - 1] = '\0';
}
(*paths)[i] = path;
(*base_paths)[i] = realpath(path, NULL);
}
/* Make sure we search these paths instead of stdin. */
opts.search_stream = 0;
} else {
path = ag_strdup(".");
*paths = ag_malloc(sizeof(char*) * 2);
*base_paths = ag_malloc(sizeof(char*) * 2);
(*paths)[0] = path;
(*base_paths)[0] = realpath(path, NULL);
i = 1;
}
(*paths)[i] = NULL;
(*base_paths)[i] = NULL;
}