aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 439f353059c8cf48436282d5c7b0409249fd3eb3 (plain) (blame)
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
#include <langinfo.h>
#include <locale.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <alloc.h>
#include <array.h>
#include <cli.h>
#include <errors.h>
#include <macros.h>
#include <mbstring.h>
#include <pcre2.h>
#include <unicode/prop.h>
#include <unicode/string.h>

#include "exitcodes.h"
#include "tpool.h"
#include "work.h"

#define MAIN_C 1
#include "flags.h"

static bool use_color_p(void);
static op_t *pattern_comp(u8view_t pat);
#if GIT_GRAB
static FILE *getfstream(int globc, char **globv);
#endif

atomic_int rv = EXIT_NOMATCH;
op_t *ops;
/* For use in diagnostic messages */
const char *lquot = "`", *rquot = "'";

/* We need to use different matching functions depending on if we’re using JIT
   matching or not */
typeof(pcre2_match) *pcre2_match_fn;

static char emsg[256]; /* Buffer for PCRE2 error messages */
/* TODO: Use the LUT in work.c */
static const bool opchars[] = {
	['g'] = true,
	['G'] = true,
	['h'] = true,
	['H'] = true,
	['x'] = true,
	['X'] = true,
};

int
main(int argc, char **argv)
{
	mlib_setprogname(argv[0]);

	/* TODO: Can we test this in an OpenBSD VM? */
#if 0 && defined(__OpenBSD__)
#if GIT_GRAB
	if (pledge("stdio rpath prot exec", NULL) == -1)
		cerr(EXIT_FATAL, "pledge:");
#else
	if (pledge("stdio rpath", NULL) == -1)
		cerr(EXIT_FATAL, "pledge:");
#endif
#endif

	setlocale(LC_ALL, "");
	if (streq(nl_langinfo(CODESET), "UTF-8")) {
		lquot = u8"‘";
		rquot = u8"’";
	}

	optparser_t parser = mkoptparser(argv);
	static const cli_opt_t opts[] = {
		{'c', U8C("color"),         CLI_NONE},
		{'h', U8C("help"),          CLI_NONE},
		{'i', U8C("ignore-case"),   CLI_NONE},
		{'l', U8C("line"),          CLI_NONE},
		{'p', U8C("predicate"),     CLI_NONE},
		{'s', U8C("strip-newline"), CLI_NONE},
		{'U', U8C("no-unicode"),    CLI_NONE},
		{'z', U8C("zero"),          CLI_NONE},
	};

	for (;;) {
		rune opt = optparse(&parser, opts, lengthof(opts));
		if (opt == 0)
			break;
		switch (opt) {
		case 'c':
			flags.c = true;
			break;
		case 'h':
			execlp("man", "man", "1", mlib_progname(), nullptr);
			err("execlp: man 1 %s:", mlib_progname());
		case 'i':
			flags.i = true;
			break;
		case 'l':
			flags.l = true;
			break;
		case 'p':
			flags.p = true;
			break;
		case 's':
			flags.s = true;
			break;
		case 'U':
			flags.U = true;
			break;
		case 'z':
			flags.z = true;
			break;
		case -1:
			warn(parser.errmsg);
			goto usage;
		}
	}

	if (flags.p && flags.s) {
		warn("-p and -s are mutually exclusive");
		goto usage;
	}
	if (flags.p && flags.z) {
		warn("-p and -z are mutually exclusive");
		goto usage;
	}
	if (flags.s && flags.z) {
		warn("-s and -z are mutually exclusive");
		goto usage;
	}

	argc -= parser.optind;
	argv += parser.optind;

	if (argc == 0) {
	usage:
		usage("[-p | -s | -z] [-cilU] pattern [file ...]", "-h");
		exit(EXIT_FATAL);
	}

	flags.c = flags.c || use_color_p();
	ops = pattern_comp((u8view_t){*argv, strlen(*argv)});

	allocator_t mem = init_heap_allocator(nullptr);

#if GIT_GRAB
	argc--;
	argv++;

	FILE *fstream = getfstream(argc, argv);
	if (fstream == nullptr)
		cerr(EXIT_FATAL, "getfstream:");

	const char **filenames = array_new(mem, typeof(*filenames), 1024);

	size_t len;
	ssize_t nr;
	char *file = nullptr;
	while ((nr = getdelim(&file, &len, 0, fstream)) != -1) {
		/* TODO: Would an arena improve performance? */
		const char *s = strdup(file);
		if (s == nullptr)
			cerr(EXIT_FATAL, "strdup:");
		array_push(&filenames, s);
	}
	if (ferror(fstream))
		cerr(EXIT_FATAL, "getdelim:");

	(void)fclose(fstream);
#else
	if (argc == 1) {
		argv = (static char *[]){"-"};
	/* TODO: Can we test this in an OpenBSD VM? */
#if 0 && defined(__OpenBSD__)
		if (pledge("stdio") == -1)
			cerr(EXIT_FATAL, "pledge:");
#endif
	} else {
		argc--;
		argv++;
		flags.do_header = true;
	}
#endif /* !GIT_GRAB */

	tpool_t tp;
	int thrds = tpinit(&tp,
#if GIT_GRAB
		filenames, array_len(filenames)
#else
		(const char **)argv, argc
#endif
	);

    /* Failed to spawn threads */
	if (thrds == 0) {
		unsigned char *buf = array_new(mem, typeof(*buf), 4096);
		for (int i = 0; i < argc; i++) {
			process_file(argv[i], &buf);
			fwrite(buf, 1, array_len(buf), stdout);
			array_hdr(buf)->len = 0;
		}
#if DEBUG
		array_free(buf);
#endif
	}

	if (thrds != 0)
		tpfree(&tp);
#if DEBUG
	pcre2_jit_free_unused_memory(nullptr);
	array_foreach (ops, op) {
		if (op->free_me)
			pcre2_code_free(op->re);
	}
	array_free(ops);
#if GIT_GRAB
	free(file);
	array_foreach (filenames, f)
		free((void *)*f);
	array_free(filenames);
#endif
#endif /* DEBUG */
	return rv;
}

op_t *
pattern_comp(u8view_t pat)
{
	allocator_t mem = init_heap_allocator(nullptr);
	op_t *ops = array_new(mem, op_t, 16);

	for (;;) {
		int w;
		rune ch;

		while ((w = ucsnext(&ch, &pat)) != 0) {
			if (!uprop_is_pat_ws(ch)) {
				VSHFT(&pat, -w);
				break;
			}
		}
		if (pat.len == 0)
			break;

		/* Grab the operator.  We grab the entire next grapheme for
		   better error messages in the case that someone tries to use a
		   non-ASCII grapheme as an operator for whatever reason. */

		op_t op;
		u8view_t g;

		(void)ucsgnext(&g, &pat);
		if (g.len != 1 || *g.p >= lengthof(opchars) || !opchars[*g.p]) {
			cerr(EXIT_FATAL, "Invalid operator %s%.*s%s",
				lquot, SV_PRI_ARGS(g), rquot);
		}
		op.c = (char)*g.p;

		/* Unlike with the operator, we parse the delimeter as a rune
		   instead of a grapheme.  This makes it easier for users to
		   write patterns that match combining characters.  This _may_ be
		   subject to change in the future but for now this is the
		   rationale.  Alongside standard delimeters, if the opening
		   delimeter is a bracket or some other form of paired-bracket
		   (as determined by Unicode) then the closing delimeter is set
		   to the right-hand form of the bracket.  This means that the
		   following are both valid delimeted patterns:

		       /regex/
		       「regex」 */

		rune ldelim, rdelim;
		if ((w = ucsnext(&ldelim, &pat)) == 0)
			cerr(EXIT_FATAL, "Premature end of pattern");
		if (ldelim == '\\')
			cerr(EXIT_FATAL, "Cannot use %s\\%s as a delimeter", lquot, rquot);
		rdelim = uprop_get_bpb(ldelim);

		/* Find the right delimeter, which is optional for the last
		   operator */
		u8view_t re = {pat.p, -1};
		while ((w = ucsnext(&ch, &pat)) != 0) {
			if (ch == '\\') {
				if (ucsnext(nullptr, &pat) == 0)
					cerr(EXIT_FATAL, "Premature end of pattern");
			} else if (ch == rdelim) {
				re.len = pat.p - re.p - w;
				break;
			}
		}
		if (re.len == -1)
			re.len = pat.p - re.p;
		if (re.len == 0) {
			if (op.c != 'h') {
				cerr(EXIT_FATAL, "%s%c%s operator given empty regex",
				     lquot, op.c, rquot);
			}
			if (array_len(ops) == 0) {
				cerr(EXIT_FATAL,
				     "%sh%s operator given empty regex as the first operator",
				     lquot, rquot);
			}
			op.re = ops[array_len(ops) - 1].re;
#if DEBUG
			op.free_me = false;
#endif
			if (ucsnext(&ch, &pat) != 0 && !uprop_is_pat_ws(ch))
				cerr(EXIT_FATAL, "Cannot pass flags to empty regex");
		} else {
			u8view_t g;
			uint32_t reopts = PCRE2_DOTALL | PCRE2_MATCH_INVALID_UTF
							  | PCRE2_UTF;
			if (flags.i)
				reopts |= PCRE2_CASELESS;
			if (!flags.U)
				reopts |= PCRE2_UCP;

			for (;;) {
				if (ucsgnext(&g, &pat) == 0)
					break;
				if (g.len != 1)
					goto bad_flag;
				if (uprop_is_pat_ws(*g.p))
					break;

				switch (*g.p) {
				case 'i': reopts |=  PCRE2_CASELESS; break;
				case 'I': reopts &= ~PCRE2_CASELESS; break;
				case 'l': reopts |=  PCRE2_LITERAL;  break;
				case 'L': reopts &= ~PCRE2_LITERAL;  break;
				case 'u': reopts |=  PCRE2_UCP;      break;
				case 'U': reopts &= ~PCRE2_UCP;      break;
				default:
				bad_flag:
					cerr(EXIT_FATAL, "Unknown regex flag %s%.*s%s",
					     lquot, SV_PRI_ARGS(g), rquot);
				}
			}

			/* When doing literal matches we need to ensure the following
               options are disabled, otherwise PCRE2 complains loudly instead of
               just dealing with it™. */
			if (reopts & PCRE2_LITERAL)
				reopts &= ~(PCRE2_DOTALL | PCRE2_UCP);

			int ec;
			size_t eoff;
			op.re = pcre2_compile(re.p, re.len, reopts, &ec, &eoff, nullptr);
			if (op.re == nullptr) {
				/* TODO: Ensure the buffer is large enough for the error message */
				(void)pcre2_get_error_message(ec, emsg, sizeof(emsg));
				cerr(EXIT_FATAL, "Failed to compile regex: %s", emsg);
			}
			if ((ec = pcre2_jit_compile(op.re, PCRE2_JIT_COMPLETE)) != 0) {
				/* TODO: Ensure the buffer is large enough for the error message */
				(void)pcre2_get_error_message(ec, emsg, sizeof(emsg));
				warn("Failed to JIT compile regex: %s", emsg);
				rv = EXIT_WARNING;
				pcre2_match_fn = pcre2_match;
			} else
				pcre2_match_fn = pcre2_jit_match;
#if DEBUG
			op.free_me = true;
#endif
		}
		array_push(&ops, op);
	}

	if (array_len(ops) == 0)
		err("Empty pattern");

	return ops;
}

bool
use_color_p(void)
{
	const char *ev = getenv("TERM");
	if (ev != nullptr && streq(ev, "dumb"))
		return false;
	if ((ev = getenv("NO_COLOR")) != nullptr && *ev != 0)
		return false;
	if ((ev = getenv("CLICOLOR_FORCE")) != nullptr && *ev != 0)
		return true;
	return isatty(STDOUT_FILENO);
}

#if GIT_GRAB
FILE *
getfstream(int globc, char **globv)
{
	pid_t pid;
	int fds[2];
	enum { R, W };

	if (pipe(fds) == 1)
		cerr(EXIT_FATAL, "pipe:");

	switch (pid = fork()) {
	case -1:
		cerr(EXIT_FATAL, "fork:");
	case 0:
		static const char *git_grep_argv[] = {
			"git", "grep", "-Ilz", "",
		};

		close(fds[R]);
		if (dup2(fds[W], STDOUT_FILENO) == -1)
			cerr(EXIT_FATAL, "dup2:");
		close(fds[W]);

		size_t argc = globc + lengthof(git_grep_argv) + 1;
		char **argv = malloc(argc * sizeof(char *));
		if (argv == nullptr)
			cerr(EXIT_FATAL, "malloc:");
		memcpy(argv, git_grep_argv, sizeof(git_grep_argv));
		memcpy(argv + lengthof(git_grep_argv), globv, globc * sizeof(char *));
		argv[argc - 1] = nullptr;

		execvp("git", argv);
		cerr(EXIT_FATAL, "execvp: git grep -Ilz '':");
	}

	/* TODO: Can we test this in an OpenBSD VM? */
#if 0 && defined(__OpenBSD__)
	if (pledge("stdio rpath") == -1)
		cerr(EXIT_FATAL, "pledge:");
#endif
	close(fds[W]);
	return fdopen(fds[R], "r");
}
#endif /* GIT_GRAB */