1 /* $NetBSD: nl.c,v 1.15 2020/12/31 04:07:37 ginsbach 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.
41 #define errx(code, ...) { dprintf(2, __VA_ARGS__); exit(code); }
44 number_all
, /* number all lines */
45 number_nonempty
, /* number non-empty lines */
46 number_none
, /* no line numbering */
47 number_regex
/* number lines matching regular expression */
50 struct numbering_property
{
51 const char * const name
; /* for diagnostics */
52 numbering_type type
; /* numbering type */
53 regex_t expr
; /* for type == number_regex */
56 /* line numbering formats */
57 #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
58 #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
59 #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
64 #define NP_LAST HEADER
66 static struct numbering_property numbering_properties
[NP_LAST
+ 1] = {
67 { "footer", number_none
, { 0, 0, 0, 0 } },
68 { "body", number_nonempty
, { 0, 0, 0, 0 } },
69 { "header", number_none
, { 0, 0, 0, 0 } },
72 #define max(a, b) ((a) > (b) ? (a) : (b))
75 * Maximum number of characters required for a decimal representation of a
76 * (signed) int; courtesy of tzcode.
78 #define INT_STRLEN_MAXIMUM \
79 ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
81 static void filter(void);
82 static void parse_numbering(const char *, int);
83 static void usage(char *) __attribute__((__noreturn__
));
86 * Pointer to dynamically allocated input line buffer, and its size.
89 static size_t buffersize
;
92 * Dynamically allocated buffer suitable for string representation of ints.
94 static char *intbuffer
;
95 static size_t intbuffersize
;
98 * Configurable parameters.
100 /* delimiter characters that indicate the start of a logical page section */
101 static char delim
[2] = { '\\', ':' };
103 /* line numbering format */
104 static const char *format
= FORMAT_RN
;
106 /* increment value used to number logical page lines */
109 /* number of adjacent blank lines to be considered (and numbered) as one */
110 static unsigned int nblank
= 1;
112 /* whether to restart numbering at logical page delimiters */
113 static int restart
= 1;
115 /* characters used in separating the line number and the corrsp. text line */
116 static const char *sep
= "\t";
118 /* initial value used to number logical page lines */
119 static int startnum
= 1;
121 /* number of characters to be used for the line number */
122 /* should be unsigned but required signed by `*' precision conversion */
123 static int width
= 6;
127 main(int argc
, char *argv
[])
134 (void)setlocale(LC_ALL
, "");
137 * Note: this implementation strictly conforms to the XBD Utility
138 * Syntax Guidelines and does not permit the optional `file' operand
139 * to be intermingled with the options, which is defined in the
140 * XCU specification (Issue 5) but declared an obsolescent feature that
141 * will be removed from a future issue. It shouldn't matter, though.
143 while ((c
= getopt(argc
, argv
, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
149 parse_numbering(optarg
, BODY
);
152 if (optarg
[0] != '\0')
153 delim
[0] = optarg
[0];
154 if (optarg
[1] != '\0') {
155 delim
[1] = optarg
[1];
156 /* at most two delimiter characters */
157 if (optarg
[2] != '\0') {
159 "invalid delim argument -- %s",
166 parse_numbering(optarg
, FOOTER
);
169 parse_numbering(optarg
, HEADER
);
173 val
= strtol(optarg
, &ep
, 10);
174 if ((ep
!= NULL
&& *ep
!= '\0') ||
175 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
177 "invalid incr argument -- %s", optarg
);
182 uval
= strtoul(optarg
, &ep
, 10);
183 if ((ep
!= NULL
&& *ep
!= '\0') ||
184 (uval
== ULONG_MAX
&& errno
!= 0))
186 "invalid num argument -- %s", optarg
);
187 nblank
= (unsigned int)uval
;
190 if (strcmp(optarg
, "ln") == 0) {
192 } else if (strcmp(optarg
, "rn") == 0) {
194 } else if (strcmp(optarg
, "rz") == 0) {
198 "illegal format -- %s", optarg
);
205 val
= strtol(optarg
, &ep
, 10);
206 if ((ep
!= NULL
&& *ep
!= '\0') ||
207 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
209 "invalid startnum value -- %s", optarg
);
214 val
= strtol(optarg
, &ep
, 10);
215 if ((ep
!= NULL
&& *ep
!= '\0') ||
216 ((val
== LONG_MIN
|| val
== LONG_MAX
) && errno
!= 0))
218 "invalid width value -- %s", optarg
);
222 "width argument must be > 0 -- %d",
238 if (strcmp(argv
[0], "-") != 0 &&
239 freopen(argv
[0], "r", stdin
) == NULL
)
240 errx(EXIT_FAILURE
, "Cannot open `%s'", argv
[0]);
247 /* Determine the maximum input line length to operate on. */
248 if ((val
= sysconf(_SC_LINE_MAX
)) == -1) /* ignore errno */
250 /* Allocate sufficient buffer space (including the terminating NUL). */
251 buffersize
= (size_t)val
+ 1;
252 if ((buffer
= malloc(buffersize
)) == NULL
)
253 errx(EXIT_FAILURE
, "Cannot allocate input line buffer");
255 /* Allocate a buffer suitable for preformatting line number. */
256 intbuffersize
= max((int)INT_STRLEN_MAXIMUM
, width
) + 1; /* NUL */
257 if ((intbuffer
= malloc(intbuffersize
)) == NULL
)
258 errx(EXIT_FAILURE
, "cannot allocate preformatting buffer");
270 int line
; /* logical line number */
271 int section
; /* logical page section */
272 unsigned int adjblank
; /* adjacent blank lines */
273 int consumed
; /* intbuffer measurement */
280 donumber
= 0; /* avoid bogus `uninitialized' warning */
283 while (fgets(buffer
, (int)buffersize
, stdin
) != NULL
) {
284 for (idx
= FOOTER
; idx
<= NP_LAST
; idx
++) {
285 /* Does it look like a delimiter? */
286 if (buffer
[2 * idx
+ 0] == delim
[0] &&
287 buffer
[2 * idx
+ 1] == delim
[1]) {
288 /* Was this the whole line? */
289 if (buffer
[2 * idx
+ 2] == '\n') {
301 switch (numbering_properties
[section
].type
) {
304 * Doing this for number_all only is disputable, but
305 * the standard expresses an explicit dependency on
308 if (buffer
[0] == '\n' && ++adjblank
< nblank
)
311 donumber
= 1, adjblank
= 0;
313 case number_nonempty
:
314 donumber
= (buffer
[0] != '\n');
321 (regexec(&numbering_properties
[section
].expr
,
322 buffer
, 0, NULL
, 0) == 0);
327 consumed
= snprintf(intbuffer
, intbuffersize
, format
,
330 intbuffer
+ max(0, consumed
- width
), sep
);
333 (void)printf("%*s%*s", width
, "", (int)strlen(sep
), "");
335 (void)printf("%s", buffer
);
338 errx(EXIT_FAILURE
, "output error");
344 errx(EXIT_FAILURE
, "input error");
348 * Various support functions.
352 parse_numbering(const char *argstr
, int section
)
355 char errorbuf
[NL_TEXTMAX
];
359 numbering_properties
[section
].type
= number_all
;
362 numbering_properties
[section
].type
= number_none
;
365 numbering_properties
[section
].type
= number_nonempty
;
368 /* If there was a previous expression, throw it away. */
369 if (numbering_properties
[section
].type
== number_regex
)
370 regfree(&numbering_properties
[section
].expr
);
372 numbering_properties
[section
].type
= number_regex
;
374 /* Compile/validate the supplied regular expression. */
375 if ((error
= regcomp(&numbering_properties
[section
].expr
,
376 &argstr
[1], REG_NEWLINE
|REG_NOSUB
)) != 0) {
377 (void)regerror(error
,
378 &numbering_properties
[section
].expr
,
379 errorbuf
, sizeof (errorbuf
));
382 numbering_properties
[section
].name
, errorbuf
,
388 "illegal %s line numbering type -- %s",
389 numbering_properties
[section
].name
, argstr
);
396 (void)fprintf(stderr
, "Usage: %s [-p] [-b type] [-d delim] [-f type] "
397 "[-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] "
398 "[-v startnum] [-w width] [file]\n", argv0
);