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.");
45 #include <sys/types.h>
58 number_all, /* number all lines */
59 number_nonempty, /* number non-empty lines */
60 number_none, /* no line numbering */
61 number_regex /* number lines matching regular expression */
64 struct numbering_property {
65 const char * const name; /* for diagnostics */
66 numbering_type type; /* numbering type */
67 regex_t expr; /* for type == number_regex */
70 /* line numbering formats */
71 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
72 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
73 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
78 #define NP_LAST HEADER
80 static struct numbering_property numbering_properties[NP_LAST + 1] = {
81 { "footer", number_none },
82 { "body", number_nonempty },
83 { "header", number_none }
86 #define max(a, b) ((a) > (b) ? (a) : (b))
89 * Maximum number of characters required for a decimal representation of a
90 * (signed) int; courtesy of tzcode.
92 #define INT_STRLEN_MAXIMUM \
93 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
95 static void filter(void);
96 static void parse_numbering(const char *, int);
97 static void usage(void);
100 * Pointer to dynamically allocated input line buffer, and its size.
103 static size_t buffersize;
106 * Dynamically allocated buffer suitable for string representation of ints.
108 static char *intbuffer;
111 * Configurable parameters.
113 /* delimiter characters that indicate the start of a logical page section */
114 static char delim[2] = { '\\', ':' };
116 /* line numbering format */
117 static const char *format = FORMAT_RN;
119 /* increment value used to number logical page lines */
122 /* number of adjacent blank lines to be considered (and numbered) as one */
123 static unsigned int nblank = 1;
125 /* whether to restart numbering at logical page delimiters */
126 static int restart = 1;
128 /* characters used in separating the line number and the corrsp. text line */
129 static const char *sep = "\t";
131 /* initial value used to number logical page lines */
132 static int startnum = 1;
134 /* number of characters to be used for the line number */
135 /* should be unsigned but required signed by `*' precision conversion */
136 static int width = 6;
148 size_t intbuffersize;
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 if (optarg[0] != '\0')
162 delim[0] = optarg[0];
163 if (optarg[1] != '\0')
164 delim[1] = optarg[1];
165 /* at most two delimiter characters */
166 if (optarg[2] != '\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 (freopen(argv[0], "r", stdin) == NULL)
247 err(EXIT_FAILURE, "%s", argv[0]);
254 /* Determine the maximum input line length to operate on. */
255 if ((val = sysconf(_SC_LINE_MAX)) == -1) /* ignore errno */
257 /* Allocate sufficient buffer space (including the terminating NUL). */
258 buffersize = (size_t)val + 1;
259 if ((buffer = malloc(buffersize)) == NULL)
260 err(EXIT_FAILURE, "cannot allocate input line buffer");
262 /* Allocate a buffer suitable for preformatting line number. */
263 intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
264 if ((intbuffer = malloc(intbuffersize)) == NULL)
265 err(EXIT_FAILURE, "cannot allocate preformatting buffer");
277 int line; /* logical line number */
278 int section; /* logical page section */
279 unsigned int adjblank; /* adjacent blank lines */
280 int consumed; /* intbuffer measurement */
287 (void)&donumber; /* avoid bogus `uninitialized' warning */
290 while (fgets(buffer, (int)buffersize, stdin) != NULL) {
291 for (idx = FOOTER; idx <= NP_LAST; idx++) {
292 /* Does it look like a delimiter? */
293 if (buffer[2 * idx + 0] == delim[0] &&
294 buffer[2 * idx + 1] == delim[1]) {
295 /* Was this the whole line? */
296 if (buffer[2 * idx + 2] == '\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)printf("%s%s", sep, buffer);
345 err(EXIT_FAILURE, "output error");
351 err(EXIT_FAILURE, "input error");
355 * Various support functions.
359 parse_numbering(argstr, section)
364 char errorbuf[NL_TEXTMAX];
368 numbering_properties[section].type = number_all;
371 numbering_properties[section].type = number_none;
374 numbering_properties[section].type = number_nonempty;
377 /* If there was a previous expression, throw it away. */
378 if (numbering_properties[section].type == number_regex)
379 regfree(&numbering_properties[section].expr);
381 numbering_properties[section].type = number_regex;
383 /* Compile/validate the supplied regular expression. */
384 if ((error = regcomp(&numbering_properties[section].expr,
385 &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
386 (void)regerror(error,
387 &numbering_properties[section].expr,
388 errorbuf, sizeof (errorbuf));
391 numbering_properties[section].name, errorbuf,
397 "illegal %s line numbering type -- %s",
398 numbering_properties[section].name, argstr);
406 (void)fprintf(stderr, "usage: nl [-p] [-b type] [-d delim] [-f type] \
407 [-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] [-v startnum] [-w width] \