aboutsummaryrefslogtreecommitdiff
path: root/src/grab.c
blob: 40315402bcda84776c009cac64c52273733ec51c (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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#include <err.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if GRAB_DO_PCRE
#	include <pcre2posix.h>
#else
#	include <regex.h>
#	ifndef REG_DOTALL
#		define REG_DOTALL 0
#	endif
#	define REG_UCP 0
#	define REG_UTF 0
#	ifndef REG_STARTEND
#		error "REG_STARTEND not defined"
#	endif
#endif

#include <gbrk.h>
#include <rune.h>
#include <utf8.h>

#include "compat.h"
#include "da.h"

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define die(...)  err(EXIT_FAILURE, __VA_ARGS__)
#define diex(...) errx(EXIT_FAILURE, __VA_ARGS__)
#define warn(...) \
	do { \
		warn(__VA_ARGS__); \
		rv = EXIT_FAILURE; \
	} while (0)

#define streq(a, b)    (!strcmp(a, b))
#define memeq(a, b, n) (!memcmp(a, b, n))

#define EEARLY "Input string terminated prematurely"

#define DEFCOL_FN "35"
#define DEFCOL_HL "01;31"
#define DEFCOL_LN "32"
#define DEFCOL_SE "36"

struct matches {
	struct sv *buf;
	size_t len, cap;
};

struct op {
	char c;
	regex_t pat;
#ifdef GRAB_DEBUG
	bool alloced;
#endif
};

struct ops {
	struct op *buf;
	size_t len, cap;
};

struct sv {
	char *p;
	size_t len;
};

typedef unsigned char uchar;
typedef void cmd_func(struct sv, struct matches *, struct ops, size_t,
                      const char *);
typedef void put_func(struct sv, struct matches *, const char *);

static cmd_func cmdg, cmdh, cmdH, cmdx, cmdX;
static put_func putm, putm_nc;

#if GIT_GRAB
static FILE *getfstream(int n, char *v[n]);
#endif
static void grab(struct ops, FILE *, const char *);
static struct ops comppat(char8_t *);
static regex_t mkregex(char8_t *, size_t);
static bool islbrk(struct u8view);
static bool sgrvalid(const char *);
static bool xisspace(char);
static int svposcmp(const void *, const void *);
static char *env_or_default(const char *, const char *);

static int filecnt, rv;
static bool bflag, cflag, nflag, sflag, Uflag, zflag;
static bool fflag = GIT_GRAB;
static put_func *putf;

static struct {
	const char *p, *bp;
	size_t col, row;
} pos;

static cmd_func *op_table[UCHAR_MAX] = {
	['g'] = cmdg, ['G'] = cmdg, ['h'] = cmdh,
	['H'] = cmdH, ['x'] = cmdx, ['X'] = cmdX,
};

static void
usage(const char *s)
{
	fprintf(stderr,
#if GIT_GRAB
	        "Usage: %s [-s | -z] [-bcnU] pattern [glob ...]\n"
#else
	        "Usage: %s [-s | -z] [-bcfnU] pattern [file ...]\n"
#endif
	        "       %s -h\n",
	        s, s);
	exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
	int opt;
	struct ops ops;
	struct option longopts[] = {
		{"byte-offset",   no_argument, nullptr, 'b'},
		{"color",         no_argument, nullptr, 'c'},
#if GIT_GRAB
		{"filenames",     no_argument, nullptr, 'f'},
#endif
		{"help",          no_argument, nullptr, 'h'},
		{"newline",       no_argument, nullptr, 'n'},
		{"strip-newline", no_argument, nullptr, 's'},
		{"no-unicode",    no_argument, nullptr, 'U'},
		{"zero",          no_argument, nullptr, 'z'},
		{nullptr,         0,           nullptr, 0  },
	};

#if GIT_GRAB
	char *entry = nullptr;
	size_t len;
	ssize_t nr;
	FILE *flist;
	const char *opts = "bchnsUz";
#else
	const char *opts = "bcfhnsUz";
#endif

	argv[0] = basename(argv[0]);
	if (argc < 2)
		usage(argv[0]);

	setlocale(LC_ALL, "");

	while ((opt = getopt_long(argc, argv, opts, longopts, nullptr)) != -1) {
		switch (opt) {
		case 'b':
			bflag = true;
			break;
		case 'c':
			cflag = true;
			break;
#if !GIT_GRAB
		case 'f':
			fflag = true;
			break;
#endif
		case 'n':
			nflag = true;
			break;
		case 's':
			sflag = true;
			break;
		case 'U':
#if GRAB_DO_PCRE
			Uflag = true;
			break;
#else
			errx(2, "program not built with PCRE support");
#endif
		case 'z':
			zflag = true;
			break;
		case 'h':
			execlp("man", "man", "1", argv[0], nullptr);
			die("execlp: man 1 %s", argv[0]);
		default:
			usage(argv[0]);
		}
	}

	if (sflag && zflag)
		usage(argv[0]);

	argc -= optind;
	argv += optind;
	filecnt = argc - 1;

	if (!cflag && isatty(STDOUT_FILENO) == 1
	    && !env_or_default("NO_COLOR", nullptr))
	{
		cflag = !streq(env_or_default("TERM", ""), "dumb");
	}

	putf = cflag ? putm : putm_nc;
	ops = comppat(argv[0]);

#if GIT_GRAB
	if ((flist = getfstream(argc - 1, argv + 1)) == nullptr)
		die("getfstream");
	while ((nr = getdelim(&entry, &len, '\0', flist)) > 0) {
		FILE *fp;

		if ((fp = fopen(entry, "r")) == nullptr)
			warn("fopen: %s", entry);
		else {
			grab(ops, fp, entry);
			fclose(fp);
		}
	}
	if (ferror(flist))
		warn("getdelim");
	fclose(flist);
#else
	if (argc == 1)
		grab(ops, stdin, "-");
	else {
		for (int i = 1; i < argc; i++) {
			FILE *fp;

			if (streq(argv[i], "-")) {
				grab(ops, stdin, "-");
			} else if ((fp = fopen(argv[i], "r")) == nullptr) {
				warn("fopen: %s", argv[i]);
			} else {
				grab(ops, fp, argv[i]);
				fclose(fp);
			}
		}
	}
#endif

#ifdef GRAB_DEBUG
#	if GIT_GRAB
	free(entry);
#	endif
	for (size_t i = 0; i < ops.len; i++) {
		if (ops.buf[i].alloced)
			regfree(&ops.buf[i].pat);
	}
	free(ops.buf);
#endif

	return rv;
}

struct ops
comppat(char8_t *s)
{
	struct ops ops;

	dainit(&ops, 8);
	while (*s && xisspace(*s))
		s++;
	if (!*s)
		diex(EEARLY);

	do {
		int w;
		rune ch;
		size_t len;
		char8_t *p;
		struct op op;

		/* Grab the operator and delimiter.  All operators are ASCII, but
		   u8tor() is used to parse it so that we get properly formed error
		   messages when someone uses a non-ASCII operator. */
		w = u8tor(&ch, s);
		if (ch == RUNE_ERROR)
			diex("Invalid UTF-8 sequence near ‘%02hhX’", s[-1]);
		if (w > 1 || !op_table[ch])
			diex("Invalid operator ‘%.*s’", w, s);
		op.c = *s++;

		s += u8tor(&ch, s);
		if (ch == RUNE_ERROR)
			diex("Invalid UTF-8 sequence near ‘%02hhX’", s[-1]);
		if (ch == '\0')
			diex(EEARLY);

		/* Find the closing delimiter.  The user is allowed to omit the closing
		   delimiter if this is the last operation in the query pattern. */
		p = s;
		len = strlen(s);
		if (!(s = (char8_t *)u8chr(s, ch, len)))
			s = p + len;

		if (s - p == 0) {
			if (op.c != 'h')
				diex("Empty regex given to ‘%c’", op.c);
			op.pat = ops.buf[ops.len - 1].pat;
#ifdef GRAB_DEBUG
			op.alloced = false;
#endif
		} else {
			op.pat = mkregex(p, s - p);
#ifdef GRAB_DEBUG
			op.alloced = true;
#endif
		}
		dapush(&ops, op);

		if (*s) {
			if (s += u8tor(&ch, s), ch == RUNE_ERROR)
				diex("Invalid UTF-8 sequence near ‘%02hhX’", s[-1]);
		}
		while (*s && xisspace(*s))
			s++;
	} while (*s);

	return ops;
}

void
grab(struct ops ops, FILE *stream, const char *filename)
{
	size_t n;
	struct {
		char *buf;
		size_t len, cap;
	} chars = {0};

	do {
		static_assert(sizeof(char) == 1, "sizeof(char) != 1; wtf?");
		chars.cap += BUFSIZ;
		if ((chars.buf = realloc(chars.buf, chars.cap)) == nullptr)
			die("realloc");
		chars.len += n = fread(chars.buf + chars.len, 1, BUFSIZ, stream);
	} while (n == BUFSIZ);

	if (ferror(stream))
		warn("fread: %s", filename);
	else {
		struct sv sv = {
			.p = chars.buf,
			.len = chars.len,
		};
		struct matches ms;

		dainit(&ms, 4);
		pos.col = pos.row = 1;
		pos.bp = pos.p = chars.buf;
		op_table[(uchar)ops.buf[0].c](sv, &ms, ops, 0, filename);
		free(ms.buf);
	}

	free(chars.buf);
}

void
cmdg(struct sv sv, struct matches *ms, struct ops ops, size_t i,
     const char *filename)
{
	int r;
	regmatch_t rm = {
		.rm_so = 0,
		.rm_eo = sv.len,
	};
	struct op op = ops.buf[i];

	r = regexec(&op.pat, sv.p, 1, &rm, REG_STARTEND);
	if ((r == REG_NOMATCH && op.c == 'g') || (r != REG_NOMATCH && op.c == 'G'))
		return;

	if (i + 1 == ops.len)
		putf(sv, ms, filename);
	else
		op_table[(uchar)ops.buf[i + 1].c](sv, ms, ops, i + 1, filename);
}

void
cmdh(struct sv sv, struct matches *ms, struct ops ops, size_t i,
     const char *filename)
{
	regmatch_t rm = {
		.rm_so = 0,
		.rm_eo = sv.len,
	};
	struct op op = ops.buf[i];

	do {
		if (regexec(&op.pat, sv.p, 1, &rm, REG_STARTEND) == REG_NOMATCH)
			break;

		dapush(ms, ((struct sv){sv.p + rm.rm_so, rm.rm_eo - rm.rm_so}));

		if (rm.rm_so == rm.rm_eo)
			rm.rm_eo++;
		rm = (regmatch_t){
			.rm_so = rm.rm_eo,
			.rm_eo = sv.len,
		};
	} while (rm.rm_so < rm.rm_eo);

	if (i + 1 == ops.len)
		putf(sv, ms, filename);
	else {
		size_t save = ms->len;
		op_table[(uchar)ops.buf[i + 1].c](sv, ms, ops, i + 1, filename);
		ms->len = save;
	}
}

void
cmdH(struct sv sv, struct matches *ms, struct ops ops, size_t i,
     const char *filename)
{
	regmatch_t rm = {
		.rm_so = 0,
		.rm_eo = sv.len,
	};
	regmatch_t prev = {
		.rm_so = 0,
		.rm_eo = 0,
	};
	struct op op = ops.buf[i];

	do {
		struct sv nsv;

		if (regexec(&op.pat, sv.p, 1, &rm, REG_STARTEND) == REG_NOMATCH)
			break;

		if (prev.rm_so || prev.rm_eo || rm.rm_so) {
			nsv = (struct sv){
				.p = sv.p + prev.rm_eo,
				.len = rm.rm_so - prev.rm_eo,
			};
			if (nsv.len)
				dapush(ms, nsv);
		}

		prev = rm;
		if (rm.rm_so == rm.rm_eo)
			rm.rm_eo++;
		rm = (regmatch_t){
			.rm_so = rm.rm_eo,
			.rm_eo = sv.len,
		};
	} while (rm.rm_so < rm.rm_eo);

	if (prev.rm_eo < rm.rm_eo)
		dapush(ms, ((struct sv){sv.p + rm.rm_so, rm.rm_eo - rm.rm_so}));

	if (i + 1 == ops.len)
		putf(sv, ms, filename);
	else
		op_table[(uchar)ops.buf[i + 1].c](sv, ms, ops, i + 1, filename);
}

void
cmdx(struct sv sv, struct matches *ms, struct ops ops, size_t i,
     const char *filename)
{
	regmatch_t rm = {
		.rm_so = 0,
		.rm_eo = sv.len,
	};
	struct op op = ops.buf[i];

	do {
		struct sv nsv;

		if (regexec(&op.pat, sv.p, 1, &rm, REG_STARTEND) == REG_NOMATCH)
			break;
		nsv = (struct sv){
			.p = sv.p + rm.rm_so,
			.len = rm.rm_eo - rm.rm_so,
		};
		if (i + 1 == ops.len)
			putf(nsv, ms, filename);
		else {
			size_t save = ms->len;
			op_table[(uchar)ops.buf[i + 1].c](nsv, ms, ops, i + 1, filename);
			ms->len = save;
		}

		if (rm.rm_so == rm.rm_eo)
			rm.rm_eo++;
		rm = (regmatch_t){
			.rm_so = rm.rm_eo,
			.rm_eo = sv.len,
		};
	} while (rm.rm_so < rm.rm_eo);
}

void
cmdX(struct sv sv, struct matches *ms, struct ops ops, size_t i,
     const char *filename)
{
	regmatch_t rm = {
		.rm_so = 0,
		.rm_eo = sv.len,
	};
	regmatch_t prev = {
		.rm_so = 0,
		.rm_eo = 0,
	};
	struct op op = ops.buf[i];

	do {
		struct sv nsv;

		if (regexec(&op.pat, sv.p, 1, &rm, REG_STARTEND) == REG_NOMATCH)
			break;

		if (prev.rm_so || prev.rm_eo || rm.rm_so) {
			nsv = (struct sv){
				.p = sv.p + prev.rm_eo,
				.len = rm.rm_so - prev.rm_eo,
			};
			if (nsv.len) {
				if (i + 1 == ops.len)
					putf(nsv, ms, filename);
				else
					op_table[(uchar)ops.buf[i + 1].c](nsv, ms, ops, i + 1,
					                                  filename);
			}
		}

		prev = rm;
		if (rm.rm_so == rm.rm_eo)
			rm.rm_eo++;
		rm = (regmatch_t){
			.rm_so = rm.rm_eo,
			.rm_eo = sv.len,
		};
	} while (rm.rm_so < rm.rm_eo);

	if (prev.rm_eo < rm.rm_eo) {
		struct sv nsv = {
			.p = sv.p + rm.rm_so,
			.len = rm.rm_eo - rm.rm_so,
		};
		if (i + 1 == ops.len)
			putf(nsv, ms, filename);
		else
			op_table[(uchar)ops.buf[i + 1].c](nsv, ms, ops, i + 1, filename);
	}
}

int
svposcmp(const void *a, const void *b)
{
	struct sv *A, *B;
	A = (struct sv *)a;
	B = (struct sv *)b;
	return A->p != B->p ? A->p - B->p : A->len < B->len ? -1 : A->len != B->len;
}

void
putm(struct sv sv, struct matches *ms, const char *filename)
{
	const char *p;
	struct matches valid;
	static const char *fn, *hl, *ln, *se;

	if (cflag && !fn) {
		char *optstr;
		if ((optstr = env_or_default("GRAB_COLORS", nullptr))) {
			enum {
				OPT_FN,
				OPT_HL,
				OPT_LN,
				OPT_SE,
			};
			/* clang-format off */
			static char *const tokens[] = {
				[OPT_FN] = "fn",
				[OPT_HL] = "hl",
				[OPT_LN] = "ln",
				[OPT_SE] = "se",
				nullptr
			};
			/* clang-format on */

			while (*optstr) {
				char *val;
				switch (getsubopt(&optstr, tokens, &val)) {
				case OPT_FN:
					if (sgrvalid(val))
						fn = val;
					break;
				case OPT_HL:
					if (sgrvalid(val))
						hl = val;
					break;
				case OPT_LN:
					if (sgrvalid(val))
						fn = val;
					break;
				case OPT_SE:
					if (sgrvalid(val))
						se = val;
					break;
				default:
					warnx("invalid color value -- '%s'", val);
					rv = EXIT_FAILURE;
				}
			}
		}

		if (!fn)
			fn = DEFCOL_FN;
		if (!hl)
			hl = DEFCOL_HL;
		if (!ln)
			ln = DEFCOL_LN;
		if (!se)
			se = DEFCOL_SE;
	}

	if (fflag || filecnt > 1) {
		char sep = zflag ? '\0' : ':';
		printf("\33[%sm%s\33[0m"  /* filename */
		       "\33[%sm%c\33[0m", /* separator */
		       fn, filename, se, sep);

		if (bflag) {
			printf("\33[%sm%td\33[0m" /* byte offset */
			       "\33[%sm%c\33[0m", /* separator */
			       ln, sv.p - pos.bp, se, sep);
		} else {
			struct u8view v;
			size_t len = sv.p - pos.p;

			while (u8gnext(&v, (const char8_t **)&pos.p, &len)) {
				if (islbrk(v)) {
					pos.col = 1;
					pos.row++;
				} else
					pos.col++;
			}

			printf("\33[%sm%zu\33[0m" /* row */
			       "\33[%sm%c\33[0m"  /* separator */
			       "\33[%sm%zu\33[0m" /* column */
			       "\33[%sm%c\33[0m", /* separator */
			       ln, pos.row, se, sep, ln, pos.col, se, sep);
		}
	}

	/* Here we need to take all the views of regions to highlight, and try
	   to merge them into a simpler form.  This happens in two steps:

	   1. Sort the views by their starting position in the matched text.
	   2. Merge overlapping views.

	   After this process we should have the most reduced possible set of
	   views.  The next part is to actually print the highlighted regions
	   possible which requires bounds-checking as highlighted regions may
	   begin before or end after the matched text when using patterns such
	   as ‘h/.+/ x/.$/’. */

	dainit(&valid, ms->len);
	qsort(ms->buf, ms->len, sizeof(*ms->buf), svposcmp);
	memcpy(valid.buf, ms->buf, ms->len * sizeof(*ms->buf));
	valid.len = ms->len;

	for (size_t i = 0; i + 1 < valid.len;) {
		ptrdiff_t d;
		struct sv *a, *b;

		a = valid.buf + i;
		b = valid.buf + i + 1;
		d = a->p + a->len - b->p;

		if (d >= 0) {
			a->len += MAX(b->len - d, 0);
			daremove(&valid, i + 1);
		} else
			i++;
	}

	for (size_t i = 0; i < valid.len; i++) {
		struct sv *m = valid.buf + i;
		if (m->p + m->len < sv.p || m->p >= sv.p + sv.len) {
			daremove(&valid, i);
			i--;
			continue;
		}

		if (m->p < sv.p) {
			m->len -= sv.p - m->p;
			m->p = sv.p;
		}
		m->len = MIN(m->len, (size_t)(sv.p + sv.len - m->p));
	}

	p = sv.p;
	for (size_t i = 0; i < valid.len; i++) {
		struct sv m = valid.buf[i];
		printf("%.*s\33[%sm%.*s\33[0m", (int)(m.p - p), p, hl, (int)m.len, m.p);
		p = m.p + m.len;
	}
	fwrite(p, 1, sv.p + sv.len - p, stdout);

	if (!(sflag && sv.p[sv.len - 1] == '\n'))
		putchar(zflag ? '\0' : '\n');
	free(valid.buf);
}

void
putm_nc(struct sv sv, struct matches *ms, const char *filename)
{
	(void)ms;

	if (fflag || filecnt > 1) {
		char sep = zflag ? '\0' : ':';
		printf("%s%c", filename, sep);

		if (bflag)
			printf("%td%c", sv.p - pos.bp, sep);
		else {
			struct u8view v;
			size_t len = sv.p - pos.p;

			while (u8gnext(&v, (const char8_t **)&pos.p, &len)) {
				if (islbrk(v)) {
					pos.col = 1;
					pos.row++;
				} else
					pos.col++;
			}

			printf("%zu%c%zu%c", pos.row, sep, pos.col, sep);
		}
	}
	fwrite(sv.p, 1, sv.len, stdout);
	if (!(sflag && sv.p[sv.len - 1] == '\n'))
		putchar(zflag ? '\0' : '\n');
}

bool
islbrk(struct u8view v)
{
	return *v.p == '\n' || (v.len == 2 && memeq(v.p, "\r\n", 2));
}

bool
sgrvalid(const char *s)
{
	if (!s || !*s)
		return false;
	do {
		if ((*s < '0' || *s > '9') && *s != ';')
			return false;
	} while (*++s);

	return true;
}

regex_t
mkregex(char8_t *s, size_t n)
{
	int ret, cflags;
	regex_t r;
	char8_t c = s[n];

	s[n] = 0;
	cflags = REG_EXTENDED | REG_UTF | (nflag ? REG_NEWLINE : REG_DOTALL);
	if (!Uflag)
		cflags |= REG_UCP;
	if ((ret = regcomp(&r, s, cflags)) != 0) {
		char emsg[256];
		regerror(ret, &r, emsg, sizeof(emsg));
		diex("Failed to compile regex: %s", emsg);
	}
	s[n] = c;

	return r;
}

#if GIT_GRAB
FILE *
getfstream(int argc, char *argv[argc])
{
	pid_t pid;
	int fds[2];
	enum {
		FD_R,
		FD_W,
	};

	if (pipe(fds) == -1)
		die("pipe");

	switch (pid = fork()) {
	case -1:
		die("fork");
	case 0:;
		size_t len = argc + 5;
		char **args;

		close(fds[FD_R]);
		if (dup2(fds[FD_W], STDOUT_FILENO) == -1)
			die("dup2");
		close(fds[FD_W]);

		if (!(args = malloc(len * sizeof(char *))))
			die("malloc");
		args[0] = "git";
		args[1] = "ls-files";
		args[2] = "-z";
		args[3] = "--";
		memcpy(args + 4, argv, argc * sizeof(char *));
		args[len - 1] = nullptr;

		execvp("git", args);
		die("execvp: git ls-files -z");
	}

	close(fds[FD_W]);
	return fdopen(fds[FD_R], "r");
}
#endif

char *
env_or_default(const char *e, const char *d)
{
	const char *s = getenv(e);
	return (char *)(s && *s ? s : d);
}

bool
xisspace(char c)
{
	return c == ' ' || c == '\t' || c == '\n';
}