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.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
33 "@(#) Copyright (c) 1999\
34 The NetBSD Foundation, Inc. All rights reserved.");
38 #include <sys/types.h>
52 number_all, /* number all lines */
53 number_nonempty, /* number non-empty lines */
54 number_none, /* no line numbering */
55 number_regex /* number lines matching regular expression */
58 struct numbering_property {
59 const char * const name; /* for diagnostics */
60 numbering_type type; /* numbering type */
61 regex_t expr; /* for type == number_regex */
64 /* line numbering formats */
65 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
66 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
67 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
72 #define NP_LAST HEADER
74 static struct numbering_property numbering_properties[NP_LAST + 1] = {
75 { .name = "footer", .type = number_none },
76 { .name = "body", .type = number_nonempty },
77 { .name = "header", .type = number_none }
80 #define max(a, b) ((a) > (b) ? (a) : (b))
83 * Maximum number of characters required for a decimal representation of a
84 * (signed) int; courtesy of tzcode.
86 #define INT_STRLEN_MAXIMUM \
87 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
89 static void filter(void);
90 static void parse_numbering(const char *, int);
91 static void usage(void);
94 * Dynamically allocated buffer suitable for string representation of ints.
96 static char *intbuffer;
98 /* delimiter characters that indicate the start of a logical page section */
99 static char delim[2 * MB_LEN_MAX];
103 * Configurable parameters.
106 /* line numbering format */
107 static const char *format = FORMAT_RN;
109 /* increment value used to number logical page lines */
112 /* number of adjacent blank lines to be considered (and numbered) as one */
113 static unsigned int nblank = 1;
115 /* whether to restart numbering at logical page delimiters */
116 static int restart = 1;
118 /* characters used in separating the line number and the corrsp. text line */
119 static const char *sep = "\t";
121 /* initial value used to number logical page lines */
122 static int startnum = 1;
124 /* number of characters to be used for the line number */
125 /* should be unsigned but required signed by `*' precision conversion */
126 static int width = 6;
130 main(int argc, char *argv[])
136 size_t intbuffersize, clen;
137 char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
138 size_t delim1len = 1, delim2len = 1;
140 (void)setlocale(LC_ALL, "");
142 while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
148 parse_numbering(optarg, BODY);
151 clen = mbrlen(optarg, MB_CUR_MAX, NULL);
152 if (clen == (size_t)-1 || clen == (size_t)-2)
153 errc(EXIT_FAILURE, EILSEQ, NULL);
155 memcpy(delim1, optarg, delim1len = clen);
156 clen = mbrlen(optarg + delim1len,
158 if (clen == (size_t)-1 ||
160 errc(EXIT_FAILURE, EILSEQ, NULL);
162 memcpy(delim2, optarg + delim1len,
164 if (optarg[delim1len + clen] != '\0')
166 "invalid delim argument -- %s",
172 parse_numbering(optarg, FOOTER);
175 parse_numbering(optarg, HEADER);
179 val = strtol(optarg, &ep, 10);
180 if ((ep != NULL && *ep != '\0') ||
181 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
183 "invalid incr argument -- %s", optarg);
188 uval = strtoul(optarg, &ep, 10);
189 if ((ep != NULL && *ep != '\0') ||
190 (uval == ULONG_MAX && errno != 0))
192 "invalid num argument -- %s", optarg);
193 nblank = (unsigned int)uval;
196 if (strcmp(optarg, "ln") == 0) {
198 } else if (strcmp(optarg, "rn") == 0) {
200 } else if (strcmp(optarg, "rz") == 0) {
204 "illegal format -- %s", optarg);
211 val = strtol(optarg, &ep, 10);
212 if ((ep != NULL && *ep != '\0') ||
213 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
215 "invalid startnum value -- %s", optarg);
220 val = strtol(optarg, &ep, 10);
221 if ((ep != NULL && *ep != '\0') ||
222 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
224 "invalid width value -- %s", optarg);
228 "width argument must be > 0 -- %d",
244 if (strcmp(argv[0], "-") != 0 &&
245 freopen(argv[0], "r", stdin) == NULL)
246 err(EXIT_FAILURE, "%s", argv[0]);
253 /* Generate the delimiter sequence */
254 memcpy(delim, delim1, delim1len);
255 memcpy(delim + delim1len, delim2, delim2len);
256 delimlen = delim1len + delim2len;
258 /* Allocate a buffer suitable for preformatting line number. */
259 intbuffersize = max((int)INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
260 if ((intbuffer = malloc(intbuffersize)) == NULL)
261 err(EXIT_FAILURE, "cannot allocate preformatting buffer");
276 int line; /* logical line number */
277 int section; /* logical page section */
278 unsigned int adjblank; /* adjacent blank lines */
279 int consumed; /* intbuffer measurement */
280 int donumber = 0, idx;
288 while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
289 for (idx = FOOTER; idx <= NP_LAST; idx++) {
290 /* Does it look like a delimiter? */
291 if (delimlen * (idx + 1) > linelen)
293 if (memcmp(buffer + delimlen * idx, delim,
296 /* Was this the whole line? */
297 if (buffer[delimlen * (idx + 1)] == '\n') {
306 switch (numbering_properties[section].type) {
309 * Doing this for number_all only is disputable, but
310 * the standard expresses an explicit dependency on
313 if (buffer[0] == '\n' && ++adjblank < nblank)
316 donumber = 1, adjblank = 0;
318 case number_nonempty:
319 donumber = (buffer[0] != '\n');
326 (regexec(&numbering_properties[section].expr,
327 buffer, 0, NULL, 0) == 0);
332 /* Note: sprintf() is safe here. */
333 consumed = sprintf(intbuffer, format, width, line);
335 intbuffer + max(0, consumed - width));
338 (void)printf("%*s", width, "");
340 (void)fputs(sep, stdout);
341 (void)fwrite(buffer, linelen, 1, stdout);
344 err(EXIT_FAILURE, "output error");
350 err(EXIT_FAILURE, "input error");
356 * Various support functions.
360 parse_numbering(const char *argstr, int section)
363 char errorbuf[NL_TEXTMAX];
367 numbering_properties[section].type = number_all;
370 numbering_properties[section].type = number_none;
373 numbering_properties[section].type = number_nonempty;
376 /* If there was a previous expression, throw it away. */
377 if (numbering_properties[section].type == number_regex)
378 regfree(&numbering_properties[section].expr);
380 numbering_properties[section].type = number_regex;
382 /* Compile/validate the supplied regular expression. */
383 if ((error = regcomp(&numbering_properties[section].expr,
384 &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
385 (void)regerror(error,
386 &numbering_properties[section].expr,
387 errorbuf, sizeof (errorbuf));
390 numbering_properties[section].name, errorbuf,
396 "illegal %s line numbering type -- %s",
397 numbering_properties[section].name, argstr);
405 (void)fprintf(stderr,
406 "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
407 " [-n format] [-s sep] [-v startnum] [-w width] [file]\n");