blob: 5c64923bbd5659b77739d50c4f7d3107613e37b4 [file] [log] [blame]
Denys Vlasenkod88f94a2017-04-05 18:17:17 +02001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenkoc1b5b2a2017-04-09 21:19:29 +02003 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
4 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenkod88f94a2017-04-05 18:17:17 +02006 */
7//config:config NL
8//config: bool "nl"
9//config: default y
10//config: help
11//config: nl is used to number lines of files.
12
Denys Vlasenkoe7ac7ab2017-04-05 18:31:19 +020013//applet:IF_NL(APPLET(nl, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020014
Denys Vlasenkoe7ac7ab2017-04-05 18:31:19 +020015//kbuild:lib-$(CONFIG_NL) += nl.o
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020016
17//usage:#define nl_trivial_usage
18//usage: "[OPTIONS] [FILE]..."
19//usage:#define nl_full_usage "\n\n"
20//usage: "Write FILEs to standard output with line numbers added\n"
21//usage: "\n -b STYLE Which lines to number - a: all, t: nonempty, n: none"
22//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TODO: support "pBRE": number only lines thatmatch regexp BRE"
23////usage: "\n -f STYLE footer lines"
24////usage: "\n -h STYLE header lines"
25////usage: "\n -d CC use CC for separating logical pages"
26//usage: "\n -i N Line number increment"
27////usage: "\n -l NUMBER group of NUMBER empty lines counted as one"
28////usage: "\n -n FORMAT lneft justified, no leading zeros; rn or rz"
29////usage: "\n -p do not reset line numbers at logical pages (huh?)"
30//usage: "\n -s STRING Use STRING as line number separator"
31//usage: "\n -v N Start from N"
32//usage: "\n -w N Width of line numbers"
33
34/* By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn */
35
36#include "libbb.h"
37
38void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename)
39{
40 FILE *fp = fopen_or_warn_stdin(filename);
41 unsigned N = ns->start;
42 char *line;
43
44 while ((line = xmalloc_fgetline(fp)) != NULL) {
45 if (ns->all
46 || (ns->nonempty && line[0])
47 ) {
48 printf("%*u%s%s\n", ns->width, N, ns->sep, line);
49 N += ns->inc;
50 } else if (ns->empty_str)
51 fputs(ns->empty_str, stdout);
52 free(line);
53 }
54
55 fclose(fp);
56}
57
58int nl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59int nl_main(int argc UNUSED_PARAM, char **argv)
60{
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020061 struct number_state ns;
62 const char *opt_b = "t";
63 enum {
64 OPT_p = (1 << 0),
65 };
66#if ENABLE_LONG_OPTS
67 static const char nl_longopts[] ALIGN1 =
68 "body-numbering\0" Required_argument "b"
69 // "footer-numbering\0" Required_argument "f" - not implemented yet
70 // "header-numbering\0" Required_argument "h" - not implemented yet
71 // "section-delimiter\0" Required_argument "d" - not implemented yet
72 "line-increment\0" Required_argument "i"
73 // "join-blank-lines\0" Required_argument "l" - not implemented yet
74 // "number-format\0" Required_argument "n" - not implemented yet
75 "no-renumber\0" No_argument "p" // no-op so far
76 "number-separator\0" Required_argument "s"
77 "starting-line-number\0"Required_argument "v"
78 "number-width\0" Required_argument "w"
79 ;
80
81 applet_long_options = nl_longopts;
82#endif
83 ns.width = 6;
84 ns.start = 1;
85 ns.inc = 1;
86 ns.sep = "\t";
Denys Vlasenkoe7ac7ab2017-04-05 18:31:19 +020087 getopt32(argv, "pw:+s:v:+i:+b:", &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b);
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020088 ns.all = (opt_b[0] == 'a');
89 ns.nonempty = (opt_b[0] == 't');
Denys Vlasenkoe7ac7ab2017-04-05 18:31:19 +020090 ns.empty_str = xasprintf("%*s\n", ns.width + (int)strlen(ns.sep), "");
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020091
92 argv += optind;
93 if (!*argv)
94 *--argv = (char*)"-";
95
96 do {
97 print_numbered_lines(&ns, *argv);
98 } while (*++argv);
99
100 fflush_stdout_and_exit(EXIT_SUCCESS);
101}