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#define FOR_nl
30#include "toys.h"
31
32GLOBALS(
33 char *s, *n, *b;
34 long w, l, v;
35
36
37 long lcount;
38 long slen;
39)
40
41static void do_nl(char **pline, long len)
42{
43 char *line;
44 int match = *TT.b != 'n';
45
46 if (!pline) return;
47 line = *pline;
48
49 if (*TT.b == 'p') match = !regexec((void *)(toybuf+16), line, 0, 0, 0);
50 if (TT.l || *TT.b == 't')
51 if (*line == '\n') match = TT.l && ++TT.lcount >= TT.l;
52 if (match) {
53 TT.lcount = 0;
54 printf(toybuf, TT.w, TT.v++, TT.s);
55 } else printf("%*c", (int)(TT.w+TT.slen), ' ');
56 xprintf("%s", line);
57}
58
59void nl_main(void)
60{
61 char *clip = "";
62
63 if (!TT.s) TT.s = "\t";
64 TT.slen = strlen(TT.s);
65
66 if (!TT.n || !strcmp(TT.n, "rn"));
67 else if (!strcmp(TT.n, "ln")) clip = "-";
68 else if (!strcmp(TT.n, "rz")) clip = "0";
69 else error_exit("bad -n '%s'", TT.n);
70
71 sprintf(toybuf, "%%%s%s", clip, "*ld%s");
72
73 if (!TT.b) TT.b = "t";
74 if (*TT.b == 'p' && TT.b[1])
75 xregcomp((void *)(toybuf+16), TT.b+1,
76 REG_NOSUB | (toys.optflags&FLAG_E)*REG_EXTENDED);
77 else if (!strchr("atn", *TT.b)) error_exit("bad -b '%s'", TT.b);
78
79 loopfiles_lines(toys.optargs, do_nl);
80}
81