1 /* nl.c - print line numbers
3 * Copyright 2013 CE Strake <strake888@gmail.com>
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/nl.html
7 * This implements a subset: only one logical page (-ip), no sections (-dfh).
10 USE_NL(NEWTOY(nl, "v#<1=1l#b:n:s:w#<0=6E", TOYFLAG_BIN))
16 usage: nl [-E] [-l #] [-b MODE] [-n STYLE] [-s SEPARATOR] [-w WIDTH] [FILE...]
18 Number lines of input.
20 -E Use extended regex syntax (when doing -b pREGEX)
21 -b which lines to number: a (all) t (non-empty, default) pREGEX (pattern)
22 -l Only count last of this many consecutive blank lines
23 -n number STYLE: ln (left justified) rn (right justified) rz (zero pad)
24 -s Separator to use between number and line (instead of TAB)
25 -w Width of line numbers (default 6)
39 // Count of consecutive blank lines for -l has to persist between files
43 static void do_nl(int fd, char *name)
45 FILE *f = xfdopen(fd, "r");
46 int w = TT.w, slen = strlen(TT.s);
51 int match = *TT.b != 'n';
53 if (getline(&line, &temp, f) < 1) {
54 if (ferror(f)) perror_msg("%s", name);
58 if (*TT.b == 'p') match = !regexec((void *)(toybuf+16), line, 0, 0, 0);
59 if (TT.l || *TT.b == 't')
60 if (*line == '\n') match = TT.l && ++TT.lcount >= TT.l;
63 printf(toybuf, w, TT.v++, TT.s);
64 } else printf("%*c", (int)w+slen, ' ');
77 if (!TT.s) TT.s = "\t";
79 if (!TT.n || !strcmp(TT.n, "rn")); // default
80 else if (!strcmp(TT.n, "ln")) clip = "-";
81 else if (!strcmp(TT.n, "rz")) clip = "0";
82 else error_exit("bad -n '%s'", TT.n);
84 sprintf(toybuf, "%%%s%s", clip, "*ld%s");
86 if (!TT.b) TT.b = "t";
87 if (*TT.b == 'p' && TT.b[1])
88 xregcomp((void *)(toybuf+16), TT.b+1,
89 REG_NOSUB | (toys.optflags&FLAG_E)*REG_EXTENDED);
90 else if (!strchr("atn", *TT.b)) error_exit("bad -b '%s'", TT.b);
92 loopfiles (toys.optargs, do_nl);