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.");
42 __RCSID("$FreeBSD: src/usr.bin/nl/nl.c,v 1.10 2005/04/09 14:31:41 stefanf Exp $");
45 #include <sys/types.h>
59 number_all
, /* number all lines */
60 number_nonempty
, /* number non-empty lines */
61 number_none
, /* no line numbering */
62 number_regex
/* number lines matching regular expression */
65 struct numbering_property
{
66 const char * const name
; /* for diagnostics */
67 numbering_type type
; /* numbering type */
68 regex_t expr
; /* for type == number_regex */
71 /* line numbering formats */
72 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
73 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
74 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
79 #define NP_LAST HEADER
81 static struct numbering_property numbering_properties
[NP_LAST
+ 1] = {
82 { "footer", number_none
},
83 { "body", number_nonempty
},
84 { "header", number_none
}
87 #define max(a, b) ((a) > (b) ? (a) : (b))
90 * Maximum number of characters required for a decimal representation of a
91 * (signed) int; courtesy of tzcode.
93 #define INT_STRLEN_MAXIMUM \
94 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
96 static void filter(void);
97 static void parse_numbering(const char *, int);
98 static void usage(void);
101 * Pointer to dynamically allocated input line buffer, and its size.
104 static size_t buffersize
;
107 * Dynamically allocated buffer suitable for string representation of ints.
109 static char *intbuffer
;
111 /* delimiter characters that indicate the start of a logical page section */
112 static char delim
[2 * MB_LEN_MAX
];
116 * Configurable parameters.
119 /* line numbering format */
120 static const char *format
= FORMAT_RN
;
122 /* increment value used to number logical page lines */
125 /* number of adjacent blank lines to be considered (and numbered) as one */
126 static unsigned int nblank
= 1;
128 /* whether to restart numbering at logical page delimiters */
129 static int restart
= 1;
131 /* characters used in separating the line number and the corrsp. text line */
132 static const char *sep
= "\t";
134 /* initial value used to number logical page lines */
135 static int startnum
= 1;
137 /* number of characters to be used for the line number */
138 /* should be unsigned but required signed by `*' precision conversion */
139 static int width
= 6;
151 size_t intbuffersize
, clen
;
152 char delim1
[MB_LEN_MAX
] = { '\\' }, delim2
[MB_LEN_MAX
] = { ':' };
153 size_t delim1len
= 1, delim2len
= 1;
155 (void)setlocale(LC_ALL
, "");
157 while ((c
= getopt(argc
, argv
, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
163 parse_numbering(optarg
, BODY
);
166 clen
= mbrlen(optarg
, MB_CUR_MAX
, NULL
);
167 if (clen
== (size_t)-1 || clen
== (size_t)-2)
168 errc(EXIT_FAILURE
, EILSEQ
, NULL
);
170 memcpy(delim1
, optarg
, delim1len
= clen
);
171 clen
= mbrlen(optarg
+ delim1len
,
173 if (clen
== (size_t)-1 ||
175 errc(EXIT_FAILURE
, EILSEQ
, NULL
);
177 memcpy(delim2
, optarg
+ delim1len
,
179 if (optarg
[delim1len
+ clen
] != '\0')
181 "invalid delim argument -- %s",
187 parse_numbering(optarg
, FOOTER
);
190 parse_numbering(optarg
, HEADER
);
194 val
= strtol(optarg
, &ep
, 10);
195 if ((ep
!= NULL
&& *ep
!= '\0') ||
196 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
198 "invalid incr argument -- %s", optarg
);
203 uval
= strtoul(optarg
, &ep
, 10);
204 if ((ep
!= NULL
&& *ep
!= '\0') ||
205 (uval
== ULONG_MAX
&& errno
!= 0))
207 "invalid num argument -- %s", optarg
);
208 nblank
= (unsigned int)uval
;
211 if (strcmp(optarg
, "ln") == 0) {
213 } else if (strcmp(optarg
, "rn") == 0) {
215 } else if (strcmp(optarg
, "rz") == 0) {
219 "illegal format -- %s", optarg
);
226 val
= strtol(optarg
, &ep
, 10);
227 if ((ep
!= NULL
&& *ep
!= '\0') ||
228 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
230 "invalid startnum value -- %s", optarg
);
235 val
= strtol(optarg
, &ep
, 10);
236 if ((ep
!= NULL
&& *ep
!= '\0') ||
237 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
239 "invalid width value -- %s", optarg
);
243 "width argument must be > 0 -- %d",
260 if (freopen(argv
[0], "r", stdin
) == NULL
)
261 err(EXIT_FAILURE
, "%s", argv
[0]);
268 /* Generate the delimiter sequence */
269 memcpy(delim
, delim1
, delim1len
);
270 memcpy(delim
+ delim1len
, delim2
, delim2len
);
271 delimlen
= delim1len
+ delim2len
;
273 /* Determine the maximum input line length to operate on. */
274 if ((val
= sysconf(_SC_LINE_MAX
)) == -1) /* ignore errno */
276 /* Allocate sufficient buffer space (including the terminating NUL). */
277 buffersize
= (size_t)val
+ 1;
278 if ((buffer
= malloc(buffersize
)) == NULL
)
279 err(EXIT_FAILURE
, "cannot allocate input line buffer");
281 /* Allocate a buffer suitable for preformatting line number. */
282 intbuffersize
= max(INT_STRLEN_MAXIMUM
, width
) + 1; /* NUL */
283 if ((intbuffer
= malloc(intbuffersize
)) == NULL
)
284 err(EXIT_FAILURE
, "cannot allocate preformatting buffer");
296 int line
; /* logical line number */
297 int section
; /* logical page section */
298 unsigned int adjblank
; /* adjacent blank lines */
299 int consumed
; /* intbuffer measurement */
306 (void)&donumber
; /* avoid bogus `uninitialized' warning */
309 while (fgets(buffer
, (int)buffersize
, stdin
) != NULL
) {
310 for (idx
= FOOTER
; idx
<= NP_LAST
; idx
++) {
311 /* Does it look like a delimiter? */
312 if (memcmp(buffer
+ delimlen
* idx
, delim
,
314 /* Was this the whole line? */
315 if (buffer
[delimlen
* (idx
+ 1)] == '\n') {
317 /* if user wishes to restart line numbering on each logical page, AND
318 * the new section is logically "before", or the same as, the current
319 * section (thereby starting a new logical page), reset the line numbers.
321 if (restart
&& idx
>= section
)
323 #endif /* __APPLE __*/
329 #endif /* !__APPLE__ */
337 switch (numbering_properties
[section
].type
) {
340 * Doing this for number_all only is disputable, but
341 * the standard expresses an explicit dependency on
344 if (buffer
[0] == '\n' && ++adjblank
< nblank
)
347 donumber
= 1, adjblank
= 0;
349 case number_nonempty
:
350 donumber
= (buffer
[0] != '\n');
357 (regexec(&numbering_properties
[section
].expr
,
358 buffer
, 0, NULL
, 0) == 0);
363 /* Note: sprintf() is safe here. */
364 consumed
= sprintf(intbuffer
, format
, width
, line
);
366 intbuffer
+ max(0, consumed
- width
));
369 (void)printf("%*s", width
, "");
371 (void)printf("%s%s", sep
, buffer
);
374 err(EXIT_FAILURE
, "output error");
380 err(EXIT_FAILURE
, "input error");
384 * Various support functions.
388 parse_numbering(argstr
, section
)
393 char errorbuf
[NL_TEXTMAX
];
397 numbering_properties
[section
].type
= number_all
;
400 numbering_properties
[section
].type
= number_none
;
403 numbering_properties
[section
].type
= number_nonempty
;
406 /* If there was a previous expression, throw it away. */
407 if (numbering_properties
[section
].type
== number_regex
)
408 regfree(&numbering_properties
[section
].expr
);
410 numbering_properties
[section
].type
= number_regex
;
412 /* Compile/validate the supplied regular expression. */
413 if ((error
= regcomp(&numbering_properties
[section
].expr
,
414 &argstr
[1], REG_NEWLINE
|REG_NOSUB
)) != 0) {
415 (void)regerror(error
,
416 &numbering_properties
[section
].expr
,
417 errorbuf
, sizeof (errorbuf
));
420 numbering_properties
[section
].name
, errorbuf
,
426 "illegal %s line numbering type -- %s",
427 numbering_properties
[section
].name
, argstr
);
435 (void)fprintf(stderr
,
436 "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
437 " [-n format] [-s sep] [-v startnum] [-w width] [file]\n");