1 /* $NetBSD: nl.c,v 1.12 2013/09/17 20:00:50 wiz 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.12 2013/09/17 20:00:50 wiz Exp $");
50 number_all
, /* number all lines */
51 number_nonempty
, /* number non-empty lines */
52 number_none
, /* no line numbering */
53 number_regex
/* number lines matching regular expression */
56 struct numbering_property
{
57 const char * const name
; /* for diagnostics */
58 numbering_type type
; /* numbering type */
59 regex_t expr
; /* for type == number_regex */
62 /* line numbering formats */
63 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
64 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
65 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
70 #define NP_LAST HEADER
72 static struct numbering_property numbering_properties
[NP_LAST
+ 1] = {
73 { "footer", number_none
, { 0, 0, 0, 0 } },
74 { "body", number_nonempty
, { 0, 0, 0, 0 } },
75 { "header", number_none
, { 0, 0, 0, 0 } },
78 #define max(a, b) ((a) > (b) ? (a) : (b))
81 * Maximum number of characters required for a decimal representation of a
82 * (signed) int; courtesy of tzcode.
84 #define INT_STRLEN_MAXIMUM \
85 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
87 static void filter(void);
88 static void parse_numbering(const char *, int);
89 static void usage(void) __attribute__((__noreturn__
));
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
;
101 static size_t intbuffersize
;
104 * Configurable parameters.
106 /* delimiter characters that indicate the start of a logical page section */
107 static char delim
[2] = { '\\', ':' };
109 /* line numbering format */
110 static const char *format
= FORMAT_RN
;
112 /* increment value used to number logical page lines */
115 /* number of adjacent blank lines to be considered (and numbered) as one */
116 static unsigned int nblank
= 1;
118 /* whether to restart numbering at logical page delimiters */
119 static int restart
= 1;
121 /* characters used in separating the line number and the corrsp. text line */
122 static const char *sep
= "\t";
124 /* initial value used to number logical page lines */
125 static int startnum
= 1;
127 /* number of characters to be used for the line number */
128 /* should be unsigned but required signed by `*' precision conversion */
129 static int width
= 6;
133 main(int argc
, char *argv
[])
140 (void)setlocale(LC_ALL
, "");
143 * Note: this implementation strictly conforms to the XBD Utility
144 * Syntax Guidelines and does not permit the optional `file' operand
145 * to be intermingled with the options, which is defined in the
146 * XCU specification (Issue 5) but declared an obsolescent feature that
147 * will be removed from a future issue. It shouldn't matter, though.
149 while ((c
= getopt(argc
, argv
, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
155 parse_numbering(optarg
, BODY
);
158 if (optarg
[0] != '\0')
159 delim
[0] = optarg
[0];
160 if (optarg
[1] != '\0')
161 delim
[1] = optarg
[1];
162 /* at most two delimiter characters */
163 if (optarg
[2] != '\0') {
165 "invalid delim argument -- %s",
171 parse_numbering(optarg
, FOOTER
);
174 parse_numbering(optarg
, HEADER
);
178 val
= strtol(optarg
, &ep
, 10);
179 if ((ep
!= NULL
&& *ep
!= '\0') ||
180 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
182 "invalid incr argument -- %s", optarg
);
187 uval
= strtoul(optarg
, &ep
, 10);
188 if ((ep
!= NULL
&& *ep
!= '\0') ||
189 (uval
== ULONG_MAX
&& errno
!= 0))
191 "invalid num argument -- %s", optarg
);
192 nblank
= (unsigned int)uval
;
195 if (strcmp(optarg
, "ln") == 0) {
197 } else if (strcmp(optarg
, "rn") == 0) {
199 } else if (strcmp(optarg
, "rz") == 0) {
203 "illegal format -- %s", optarg
);
210 val
= strtol(optarg
, &ep
, 10);
211 if ((ep
!= NULL
&& *ep
!= '\0') ||
212 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
214 "invalid startnum value -- %s", optarg
);
219 val
= strtol(optarg
, &ep
, 10);
220 if ((ep
!= NULL
&& *ep
!= '\0') ||
221 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
223 "invalid width value -- %s", optarg
);
227 "width argument must be > 0 -- %d",
243 if (strcmp(argv
[0], "-") != 0 &&
244 freopen(argv
[0], "r", stdin
) == NULL
)
245 err(EXIT_FAILURE
, "Cannot open `%s'", argv
[0]);
252 /* Determine the maximum input line length to operate on. */
253 if ((val
= sysconf(_SC_LINE_MAX
)) == -1) /* ignore errno */
255 /* Allocate sufficient buffer space (including the terminating NUL). */
256 buffersize
= (size_t)val
+ 1;
257 if ((buffer
= malloc(buffersize
)) == NULL
)
258 err(EXIT_FAILURE
, "Cannot allocate input line buffer");
260 /* Allocate a buffer suitable for preformatting line number. */
261 intbuffersize
= max((int)INT_STRLEN_MAXIMUM
, width
) + 1; /* NUL */
262 if ((intbuffer
= malloc(intbuffersize
)) == NULL
)
263 err(EXIT_FAILURE
, "cannot allocate preformatting buffer");
275 int line
; /* logical line number */
276 int section
; /* logical page section */
277 unsigned int adjblank
; /* adjacent blank lines */
278 int consumed
; /* intbuffer measurement */
285 donumber
= 0; /* avoid bogus `uninitialized' warning */
288 while (fgets(buffer
, (int)buffersize
, stdin
) != NULL
) {
289 for (idx
= FOOTER
; idx
<= NP_LAST
; idx
++) {
290 /* Does it look like a delimiter? */
291 if (buffer
[2 * idx
+ 0] == delim
[0] &&
292 buffer
[2 * idx
+ 1] == delim
[1]) {
293 /* Was this the whole line? */
294 if (buffer
[2 * idx
+ 2] == '\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 consumed
= snprintf(intbuffer
, intbuffersize
, format
,
335 intbuffer
+ max(0, consumed
- width
));
338 (void)printf("%*s", width
, "");
340 (void)printf("%s%s", sep
, buffer
);
343 err(EXIT_FAILURE
, "output error");
349 err(EXIT_FAILURE
, "input error");
353 * Various support functions.
357 parse_numbering(const char *argstr
, int section
)
360 char errorbuf
[NL_TEXTMAX
];
364 numbering_properties
[section
].type
= number_all
;
367 numbering_properties
[section
].type
= number_none
;
370 numbering_properties
[section
].type
= number_nonempty
;
373 /* If there was a previous expression, throw it away. */
374 if (numbering_properties
[section
].type
== number_regex
)
375 regfree(&numbering_properties
[section
].expr
);
377 numbering_properties
[section
].type
= number_regex
;
379 /* Compile/validate the supplied regular expression. */
380 if ((error
= regcomp(&numbering_properties
[section
].expr
,
381 &argstr
[1], REG_NEWLINE
|REG_NOSUB
)) != 0) {
382 (void)regerror(error
,
383 &numbering_properties
[section
].expr
,
384 errorbuf
, sizeof (errorbuf
));
387 numbering_properties
[section
].name
, errorbuf
,
393 "illegal %s line numbering type -- %s",
394 numbering_properties
[section
].name
, argstr
);
402 (void)fprintf(stderr
, "Usage: %s [-p] [-b type] [-d delim] [-f type] "
403 "[-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] "
404 "[-v startnum] [-w width] [file]\n", getprogname());