1 /* $NetBSD: nl.c,v 1.9 2008/07/21 14:19:24 lukem Exp $ */
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>
34 __COPYRIGHT("@(#) Copyright (c) 1999\
35 The NetBSD Foundation, Inc. All rights reserved.");
36 __RCSID("$NetBSD: nl.c,v 1.9 2008/07/21 14:19:24 lukem Exp $");
49 number_all
, /* number all lines */
50 number_nonempty
, /* number non-empty lines */
51 number_none
, /* no line numbering */
52 number_regex
/* number lines matching regular expression */
55 struct numbering_property
{
56 const char * const name
; /* for diagnostics */
57 numbering_type type
; /* numbering type */
58 regex_t expr
; /* for type == number_regex */
61 /* line numbering formats */
62 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
63 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
64 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
69 #define NP_LAST HEADER
71 static struct numbering_property numbering_properties
[NP_LAST
+ 1] = {
72 { "footer", number_none
, { 0, 0, 0, 0 } },
73 { "body", number_nonempty
, { 0, 0, 0, 0 } },
74 { "header", number_none
, { 0, 0, 0, 0 } },
77 #define max(a, b) ((a) > (b) ? (a) : (b))
80 * Maximum number of characters required for a decimal representation of a
81 * (signed) int; courtesy of tzcode.
83 #define INT_STRLEN_MAXIMUM \
84 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
86 static void filter
__P((void));
87 int main
__P((int, char *[]));
88 static void parse_numbering
__P((const char *, int));
89 static void usage
__P((void));
92 * Pointer to dynamically allocated input line buffer, and its size.
95 static size_t buffersize
;
98 * Dynamically allocated buffer suitable for string representation of ints.
100 static char *intbuffer
;
103 * Configurable parameters.
105 /* delimiter characters that indicate the start of a logical page section */
106 static char delim
[2] = { '\\', ':' };
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;
140 size_t intbuffersize
;
142 (void)setlocale(LC_ALL
, "");
145 * Note: this implementation strictly conforms to the XBD Utility
146 * Syntax Guidelines and does not permit the optional `file' operand
147 * to be intermingled with the options, which is defined in the
148 * XCU specification (Issue 5) but declared an obsolescent feature that
149 * will be removed from a future issue. It shouldn't matter, though.
151 while ((c
= getopt(argc
, argv
, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
157 parse_numbering(optarg
, BODY
);
160 if (optarg
[0] != '\0')
161 delim
[0] = optarg
[0];
162 if (optarg
[1] != '\0')
163 delim
[1] = optarg
[1];
164 /* at most two delimiter characters */
165 if (optarg
[2] != '\0') {
166 (void)fprintf(stderr
,
167 "nl: invalid delim argument -- %s\n",
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)) {
184 (void)fprintf(stderr
,
185 "invalid incr argument -- %s\n", optarg
);
192 uval
= strtoul(optarg
, &ep
, 10);
193 if ((ep
!= NULL
&& *ep
!= '\0') ||
194 (uval
== ULONG_MAX
&& errno
!= 0)) {
195 (void)fprintf(stderr
,
196 "invalid num argument -- %s\n", optarg
);
199 nblank
= (unsigned int)uval
;
202 if (strcmp(optarg
, "ln") == 0) {
204 } else if (strcmp(optarg
, "rn") == 0) {
206 } else if (strcmp(optarg
, "rz") == 0) {
209 (void)fprintf(stderr
,
210 "nl: illegal format -- %s\n", optarg
);
219 val
= strtol(optarg
, &ep
, 10);
220 if ((ep
!= NULL
&& *ep
!= '\0') ||
221 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0)) {
222 (void)fprintf(stderr
,
223 "invalid startnum value -- %s\n", optarg
);
230 val
= strtol(optarg
, &ep
, 10);
231 if ((ep
!= NULL
&& *ep
!= '\0') ||
232 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0)) {
233 (void)fprintf(stderr
,
234 "invalid width value -- %s\n", optarg
);
239 (void)fprintf(stderr
,
240 "nl: width argument must be > 0 -- %d\n",
258 if (freopen(argv
[0], "r", stdin
) == NULL
) {
268 /* Determine the maximum input line length to operate on. */
269 if ((val
= sysconf(_SC_LINE_MAX
)) == -1) /* ignore errno */
271 /* Allocate sufficient buffer space (including the terminating NUL). */
272 buffersize
= (size_t)val
+ 1;
273 if ((buffer
= malloc(buffersize
)) == NULL
) {
274 perror("cannot allocate input line buffer");
278 /* Allocate a buffer suitable for preformatting line number. */
279 intbuffersize
= max((int)INT_STRLEN_MAXIMUM
, width
) + 1; /* NUL */
280 if ((intbuffer
= malloc(intbuffersize
)) == NULL
) {
281 perror("cannot allocate preformatting buffer");
295 int line
; /* logical line number */
296 int section
; /* logical page section */
297 unsigned int adjblank
; /* adjacent blank lines */
298 int consumed
; /* intbuffer measurement */
305 donumber
= 0; /* avoid bogus `uninitialized' warning */
308 while (fgets(buffer
, (int)buffersize
, stdin
) != NULL
) {
309 for (idx
= FOOTER
; idx
<= NP_LAST
; idx
++) {
310 /* Does it look like a delimiter? */
311 if (buffer
[2 * idx
+ 0] == delim
[0] &&
312 buffer
[2 * idx
+ 1] == delim
[1]) {
313 /* Was this the whole line? */
314 if (buffer
[2 * idx
+ 2] == '\n') {
326 switch (numbering_properties
[section
].type
) {
329 * Doing this for number_all only is disputable, but
330 * the standard expresses an explicit dependency on
333 if (buffer
[0] == '\n' && ++adjblank
< nblank
)
336 donumber
= 1, adjblank
= 0;
338 case number_nonempty
:
339 donumber
= (buffer
[0] != '\n');
346 (regexec(&numbering_properties
[section
].expr
,
347 buffer
, 0, NULL
, 0) == 0);
352 /* Note: sprintf() is safe here. */
353 consumed
= sprintf(intbuffer
, format
, width
, line
);
355 intbuffer
+ max(0, consumed
- width
));
358 (void)printf("%*s", width
, "");
360 (void)printf("%s%s", sep
, buffer
);
362 if (ferror(stdout
)) {
363 perror("output error");
371 perror("input error");
377 * Various support functions.
381 parse_numbering(argstr
, section
)
386 char errorbuf
[NL_TEXTMAX
];
390 numbering_properties
[section
].type
= number_all
;
393 numbering_properties
[section
].type
= number_none
;
396 numbering_properties
[section
].type
= number_nonempty
;
399 /* If there was a previous expression, throw it away. */
400 if (numbering_properties
[section
].type
== number_regex
)
401 regfree(&numbering_properties
[section
].expr
);
403 numbering_properties
[section
].type
= number_regex
;
405 /* Compile/validate the supplied regular expression. */
406 if ((error
= regcomp(&numbering_properties
[section
].expr
,
407 &argstr
[1], REG_NEWLINE
|REG_NOSUB
)) != 0) {
408 (void)regerror(error
,
409 &numbering_properties
[section
].expr
,
410 errorbuf
, sizeof (errorbuf
));
411 (void)fprintf(stderr
,
412 "nl: %s expr: %s -- %s\n",
413 numbering_properties
[section
].name
, errorbuf
,
419 (void)fprintf(stderr
,
420 "nl: illegal %s line numbering type -- %s\n",
421 numbering_properties
[section
].name
, argstr
);
430 (void)fprintf(stderr
, "usage: nl [-p] [-b type] [-d delim] [-f type] \
431 [-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] [-v startnum] [-w width] \