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