2 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/cdefs.h>
40 "@(#) Copyright (c) 1999\
41 The NetBSD Foundation, Inc. All rights reserved.");
46 #include <sys/types.h>
60 number_all, /* number all lines */
61 number_nonempty, /* number non-empty lines */
62 number_none, /* no line numbering */
63 number_regex /* number lines matching regular expression */
66 struct numbering_property {
67 const char * const name; /* for diagnostics */
68 numbering_type type; /* numbering type */
69 regex_t expr; /* for type == number_regex */
72 /* line numbering formats */
73 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
74 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
75 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
80 #define NP_LAST HEADER
82 static struct numbering_property numbering_properties[NP_LAST + 1] = {
83 { "footer", number_none },
84 { "body", number_nonempty },
85 { "header", number_none }
88 #define max(a, b) ((a) > (b) ? (a) : (b))
91 * Maximum number of characters required for a decimal representation of a
92 * (signed) int; courtesy of tzcode.
94 #define INT_STRLEN_MAXIMUM \
95 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
97 static void filter(void);
98 static void parse_numbering(const char *, int);
99 static void usage(void);
102 * Dynamically allocated buffer suitable for string representation of ints.
104 static char *intbuffer;
106 /* delimiter characters that indicate the start of a logical page section */
107 static char delim[2 * MB_LEN_MAX];
111 * Configurable parameters.
114 /* line numbering format */
115 static const char *format = FORMAT_RN;
117 /* increment value used to number logical page lines */
120 /* number of adjacent blank lines to be considered (and numbered) as one */
121 static unsigned int nblank = 1;
123 /* whether to restart numbering at logical page delimiters */
124 static int restart = 1;
126 /* characters used in separating the line number and the corrsp. text line */
127 static const char *sep = "\t";
129 /* initial value used to number logical page lines */
130 static int startnum = 1;
132 /* number of characters to be used for the line number */
133 /* should be unsigned but required signed by `*' precision conversion */
134 static int width = 6;
146 size_t intbuffersize, clen;
147 char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
148 size_t delim1len = 1, delim2len = 1;
150 (void)setlocale(LC_ALL, "");
152 while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
158 parse_numbering(optarg, BODY);
161 clen = mbrlen(optarg, MB_CUR_MAX, NULL);
162 if (clen == (size_t)-1 || clen == (size_t)-2)
163 errc(EXIT_FAILURE, EILSEQ, NULL);
165 memcpy(delim1, optarg, delim1len = clen);
166 clen = mbrlen(optarg + delim1len,
168 if (clen == (size_t)-1 ||
170 errc(EXIT_FAILURE, EILSEQ, NULL);
172 memcpy(delim2, optarg + delim1len,
174 if (optarg[delim1len + clen] != '\0')
176 "invalid delim argument -- %s",
182 parse_numbering(optarg, FOOTER);
185 parse_numbering(optarg, HEADER);
189 val = strtol(optarg, &ep, 10);
190 if ((ep != NULL && *ep != '\0') ||
191 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
193 "invalid incr argument -- %s", optarg);
198 uval = strtoul(optarg, &ep, 10);
199 if ((ep != NULL && *ep != '\0') ||
200 (uval == ULONG_MAX && errno != 0))
202 "invalid num argument -- %s", optarg);
203 nblank = (unsigned int)uval;
206 if (strcmp(optarg, "ln") == 0) {
208 } else if (strcmp(optarg, "rn") == 0) {
210 } else if (strcmp(optarg, "rz") == 0) {
214 "illegal format -- %s", optarg);
221 val = strtol(optarg, &ep, 10);
222 if ((ep != NULL && *ep != '\0') ||
223 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
225 "invalid startnum value -- %s", optarg);
230 val = strtol(optarg, &ep, 10);
231 if ((ep != NULL && *ep != '\0') ||
232 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
234 "invalid width value -- %s", optarg);
238 "width argument must be > 0 -- %d",
254 if (freopen(argv[0], "r", stdin) == NULL)
255 err(EXIT_FAILURE, "%s", argv[0]);
262 /* Generate the delimiter sequence */
263 memcpy(delim, delim1, delim1len);
264 memcpy(delim + delim1len, delim2, delim2len);
265 delimlen = delim1len + delim2len;
267 /* Allocate a buffer suitable for preformatting line number. */
268 intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
269 if ((intbuffer = malloc(intbuffersize)) == NULL)
270 err(EXIT_FAILURE, "cannot allocate preformatting buffer");
285 int line; /* logical line number */
286 int section; /* logical page section */
287 unsigned int adjblank; /* adjacent blank lines */
288 int consumed; /* intbuffer measurement */
289 int donumber = 0, idx;
297 while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
298 for (idx = FOOTER; idx <= NP_LAST; idx++) {
299 /* Does it look like a delimiter? */
300 if (delimlen * (idx + 1) > linelen)
302 if (memcmp(buffer + delimlen * idx, delim,
305 /* Was this the whole line? */
306 if (buffer[delimlen * (idx + 1)] == '\n') {
315 switch (numbering_properties[section].type) {
318 * Doing this for number_all only is disputable, but
319 * the standard expresses an explicit dependency on
322 if (buffer[0] == '\n' && ++adjblank < nblank)
325 donumber = 1, adjblank = 0;
327 case number_nonempty:
328 donumber = (buffer[0] != '\n');
335 (regexec(&numbering_properties[section].expr,
336 buffer, 0, NULL, 0) == 0);
341 /* Note: sprintf() is safe here. */
342 consumed = sprintf(intbuffer, format, width, line);
344 intbuffer + max(0, consumed - width));
347 (void)printf("%*s", width, "");
349 (void)fputs(sep, stdout);
350 (void)fwrite(buffer, linelen, 1, stdout);
353 err(EXIT_FAILURE, "output error");
359 err(EXIT_FAILURE, "input error");
365 * Various support functions.
369 parse_numbering(argstr, section)
374 char errorbuf[NL_TEXTMAX];
378 numbering_properties[section].type = number_all;
381 numbering_properties[section].type = number_none;
384 numbering_properties[section].type = number_nonempty;
387 /* If there was a previous expression, throw it away. */
388 if (numbering_properties[section].type == number_regex)
389 regfree(&numbering_properties[section].expr);
391 numbering_properties[section].type = number_regex;
393 /* Compile/validate the supplied regular expression. */
394 if ((error = regcomp(&numbering_properties[section].expr,
395 &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
396 (void)regerror(error,
397 &numbering_properties[section].expr,
398 errorbuf, sizeof (errorbuf));
401 numbering_properties[section].name, errorbuf,
407 "illegal %s line numbering type -- %s",
408 numbering_properties[section].name, argstr);
416 (void)fprintf(stderr,
417 "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
418 " [-n format] [-s sep] [-v startnum] [-w width] [file]\n");