2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
35 "@(#) Copyright (c) 1999\
36 The NetBSD Foundation, Inc. All rights reserved.");
40 #include <sys/types.h>
54 number_all, /* number all lines */
55 number_nonempty, /* number non-empty lines */
56 number_none, /* no line numbering */
57 number_regex /* number lines matching regular expression */
60 struct numbering_property {
61 const char * const name; /* for diagnostics */
62 numbering_type type; /* numbering type */
63 regex_t expr; /* for type == number_regex */
66 /* line numbering formats */
67 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
68 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
69 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
74 #define NP_LAST HEADER
76 static struct numbering_property numbering_properties[NP_LAST + 1] = {
77 { .name = "footer", .type = number_none },
78 { .name = "body", .type = number_nonempty },
79 { .name = "header", .type = number_none }
82 #define max(a, b) ((a) > (b) ? (a) : (b))
85 * Maximum number of characters required for a decimal representation of a
86 * (signed) int; courtesy of tzcode.
88 #define INT_STRLEN_MAXIMUM \
89 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
91 static void filter(void);
92 static void parse_numbering(const char *, int);
93 static void usage(void);
96 * Dynamically allocated buffer suitable for string representation of ints.
98 static char *intbuffer;
100 /* delimiter characters that indicate the start of a logical page section */
101 static char delim[2 * MB_LEN_MAX];
105 * Configurable parameters.
108 /* line numbering format */
109 static const char *format = FORMAT_RN;
111 /* increment value used to number logical page lines */
114 /* number of adjacent blank lines to be considered (and numbered) as one */
115 static unsigned int nblank = 1;
117 /* whether to restart numbering at logical page delimiters */
118 static int restart = 1;
120 /* characters used in separating the line number and the corrsp. text line */
121 static const char *sep = "\t";
123 /* initial value used to number logical page lines */
124 static int startnum = 1;
126 /* number of characters to be used for the line number */
127 /* should be unsigned but required signed by `*' precision conversion */
128 static int width = 6;
132 main(int argc, char *argv[])
138 size_t intbuffersize, clen;
139 char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
140 size_t delim1len = 1, delim2len = 1;
142 (void)setlocale(LC_ALL, "");
144 while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
150 parse_numbering(optarg, BODY);
153 clen = mbrlen(optarg, MB_CUR_MAX, NULL);
154 if (clen == (size_t)-1 || clen == (size_t)-2)
155 errc(EXIT_FAILURE, EILSEQ, NULL);
157 memcpy(delim1, optarg, delim1len = clen);
158 clen = mbrlen(optarg + delim1len,
160 if (clen == (size_t)-1 ||
162 errc(EXIT_FAILURE, EILSEQ, NULL);
164 memcpy(delim2, optarg + delim1len,
166 if (optarg[delim1len + clen] != '\0')
168 "invalid delim argument -- %s",
174 parse_numbering(optarg, FOOTER);
177 parse_numbering(optarg, HEADER);
181 val = strtol(optarg, &ep, 10);
182 if ((ep != NULL && *ep != '\0') ||
183 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
185 "invalid incr argument -- %s", optarg);
190 uval = strtoul(optarg, &ep, 10);
191 if ((ep != NULL && *ep != '\0') ||
192 (uval == ULONG_MAX && errno != 0))
194 "invalid num argument -- %s", optarg);
195 nblank = (unsigned int)uval;
198 if (strcmp(optarg, "ln") == 0) {
200 } else if (strcmp(optarg, "rn") == 0) {
202 } else if (strcmp(optarg, "rz") == 0) {
206 "illegal format -- %s", optarg);
213 val = strtol(optarg, &ep, 10);
214 if ((ep != NULL && *ep != '\0') ||
215 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
217 "invalid startnum value -- %s", optarg);
222 val = strtol(optarg, &ep, 10);
223 if ((ep != NULL && *ep != '\0') ||
224 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
226 "invalid width value -- %s", optarg);
230 "width argument must be > 0 -- %d",
246 if (strcmp(argv[0], "-") != 0 &&
247 freopen(argv[0], "r", stdin) == NULL)
248 err(EXIT_FAILURE, "%s", argv[0]);
255 /* Generate the delimiter sequence */
256 memcpy(delim, delim1, delim1len);
257 memcpy(delim + delim1len, delim2, delim2len);
258 delimlen = delim1len + delim2len;
260 /* Allocate a buffer suitable for preformatting line number. */
261 intbuffersize = max((int)INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
262 if ((intbuffer = malloc(intbuffersize)) == NULL)
263 err(EXIT_FAILURE, "cannot allocate preformatting buffer");
278 int line; /* logical line number */
279 int section; /* logical page section */
280 unsigned int adjblank; /* adjacent blank lines */
281 int consumed; /* intbuffer measurement */
282 int donumber = 0, idx;
290 while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
291 for (idx = FOOTER; idx <= NP_LAST; idx++) {
292 /* Does it look like a delimiter? */
293 if (delimlen * (idx + 1) > linelen)
295 if (memcmp(buffer + delimlen * idx, delim,
298 /* Was this the whole line? */
299 if (buffer[delimlen * (idx + 1)] == '\n') {
308 switch (numbering_properties[section].type) {
311 * Doing this for number_all only is disputable, but
312 * the standard expresses an explicit dependency on
315 if (buffer[0] == '\n' && ++adjblank < nblank)
318 donumber = 1, adjblank = 0;
320 case number_nonempty:
321 donumber = (buffer[0] != '\n');
328 (regexec(&numbering_properties[section].expr,
329 buffer, 0, NULL, 0) == 0);
334 /* Note: sprintf() is safe here. */
335 consumed = sprintf(intbuffer, format, width, line);
337 intbuffer + max(0, consumed - width));
340 (void)printf("%*s", width, "");
342 (void)fputs(sep, stdout);
343 (void)fwrite(buffer, linelen, 1, stdout);
346 err(EXIT_FAILURE, "output error");
352 err(EXIT_FAILURE, "input error");
358 * Various support functions.
362 parse_numbering(const char *argstr, int section)
365 char errorbuf[NL_TEXTMAX];
369 numbering_properties[section].type = number_all;
372 numbering_properties[section].type = number_none;
375 numbering_properties[section].type = number_nonempty;
378 /* If there was a previous expression, throw it away. */
379 if (numbering_properties[section].type == number_regex)
380 regfree(&numbering_properties[section].expr);
382 numbering_properties[section].type = number_regex;
384 /* Compile/validate the supplied regular expression. */
385 if ((error = regcomp(&numbering_properties[section].expr,
386 &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
387 (void)regerror(error,
388 &numbering_properties[section].expr,
389 errorbuf, sizeof (errorbuf));
392 numbering_properties[section].name, errorbuf,
398 "illegal %s line numbering type -- %s",
399 numbering_properties[section].name, argstr);
407 (void)fprintf(stderr,
408 "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
409 " [-n format] [-s sep] [-v startnum] [-w width] [file]\n");