add netbsd nl(1)
[rofl0r-hardcore-utils.git] / nl.c
blob58e9ea07ad7b4dde54629532a12bec64bf6e9dd6
1 /* $NetBSD: nl.c,v 1.15 2020/12/31 04:07:37 ginsbach Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Klaus Klein.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 <errno.h>
33 #include <limits.h>
34 #include <locale.h>
35 #include <regex.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 #define errx(code, ...) { dprintf(2, __VA_ARGS__); exit(code); }
43 typedef enum {
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 */
48 } numbering_type;
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 */
61 #define FOOTER 0
62 #define BODY 1
63 #define HEADER 2
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.
88 static char *buffer;
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 */
107 static int incr = 1;
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[])
129 int c;
130 long val;
131 unsigned long uval;
132 char *ep;
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) {
144 switch (c) {
145 case 'p':
146 restart = 0;
147 break;
148 case 'b':
149 parse_numbering(optarg, BODY);
150 break;
151 case 'd':
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') {
158 errx(EXIT_FAILURE,
159 "invalid delim argument -- %s",
160 optarg);
161 /* NOTREACHED */
164 break;
165 case 'f':
166 parse_numbering(optarg, FOOTER);
167 break;
168 case 'h':
169 parse_numbering(optarg, HEADER);
170 break;
171 case 'i':
172 errno = 0;
173 val = strtol(optarg, &ep, 10);
174 if ((ep != NULL && *ep != '\0') ||
175 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
176 errx(EXIT_FAILURE,
177 "invalid incr argument -- %s", optarg);
178 incr = (int)val;
179 break;
180 case 'l':
181 errno = 0;
182 uval = strtoul(optarg, &ep, 10);
183 if ((ep != NULL && *ep != '\0') ||
184 (uval == ULONG_MAX && errno != 0))
185 errx(EXIT_FAILURE,
186 "invalid num argument -- %s", optarg);
187 nblank = (unsigned int)uval;
188 break;
189 case 'n':
190 if (strcmp(optarg, "ln") == 0) {
191 format = FORMAT_LN;
192 } else if (strcmp(optarg, "rn") == 0) {
193 format = FORMAT_RN;
194 } else if (strcmp(optarg, "rz") == 0) {
195 format = FORMAT_RZ;
196 } else
197 errx(EXIT_FAILURE,
198 "illegal format -- %s", optarg);
199 break;
200 case 's':
201 sep = optarg;
202 break;
203 case 'v':
204 errno = 0;
205 val = strtol(optarg, &ep, 10);
206 if ((ep != NULL && *ep != '\0') ||
207 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
208 errx(EXIT_FAILURE,
209 "invalid startnum value -- %s", optarg);
210 startnum = (int)val;
211 break;
212 case 'w':
213 errno = 0;
214 val = strtol(optarg, &ep, 10);
215 if ((ep != NULL && *ep != '\0') ||
216 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
217 errx(EXIT_FAILURE,
218 "invalid width value -- %s", optarg);
219 width = (int)val;
220 if (!(width > 0))
221 errx(EXIT_FAILURE,
222 "width argument must be > 0 -- %d",
223 width);
224 break;
225 case '?':
226 default:
227 usage(argv[0]);
228 /* NOTREACHED */
231 argc -= optind;
232 argv += optind;
234 switch (argc) {
235 case 0:
236 break;
237 case 1:
238 if (strcmp(argv[0], "-") != 0 &&
239 freopen(argv[0], "r", stdin) == NULL)
240 errx(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
241 break;
242 default:
243 usage(argv[0]);
244 /* NOTREACHED */
247 /* Determine the maximum input line length to operate on. */
248 if ((val = sysconf(_SC_LINE_MAX)) == -1) /* ignore errno */
249 val = LINE_MAX;
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");
260 /* Do the work. */
261 filter();
263 return EXIT_SUCCESS;
264 /* NOTREACHED */
267 static void
268 filter(void)
270 int line; /* logical line number */
271 int section; /* logical page section */
272 unsigned int adjblank; /* adjacent blank lines */
273 int consumed; /* intbuffer measurement */
274 int donumber, idx;
276 adjblank = 0;
277 line = startnum;
278 section = BODY;
279 #ifdef __GNUC__
280 donumber = 0; /* avoid bogus `uninitialized' warning */
281 #endif
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') {
290 section = idx;
291 adjblank = 0;
292 if (restart)
293 line = startnum;
294 goto nextline;
296 } else {
297 break;
301 switch (numbering_properties[section].type) {
302 case number_all:
304 * Doing this for number_all only is disputable, but
305 * the standard expresses an explicit dependency on
306 * `-b a' etc.
308 if (buffer[0] == '\n' && ++adjblank < nblank)
309 donumber = 0;
310 else
311 donumber = 1, adjblank = 0;
312 break;
313 case number_nonempty:
314 donumber = (buffer[0] != '\n');
315 break;
316 case number_none:
317 donumber = 0;
318 break;
319 case number_regex:
320 donumber =
321 (regexec(&numbering_properties[section].expr,
322 buffer, 0, NULL, 0) == 0);
323 break;
326 if (donumber) {
327 consumed = snprintf(intbuffer, intbuffersize, format,
328 width, line);
329 (void)printf("%s%s",
330 intbuffer + max(0, consumed - width), sep);
331 line += incr;
332 } else {
333 (void)printf("%*s%*s", width, "", (int)strlen(sep), "");
335 (void)printf("%s", buffer);
337 if (ferror(stdout))
338 errx(EXIT_FAILURE, "output error");
339 nextline:
343 if (ferror(stdin))
344 errx(EXIT_FAILURE, "input error");
348 * Various support functions.
351 static void
352 parse_numbering(const char *argstr, int section)
354 int error;
355 char errorbuf[NL_TEXTMAX];
357 switch (argstr[0]) {
358 case 'a':
359 numbering_properties[section].type = number_all;
360 break;
361 case 'n':
362 numbering_properties[section].type = number_none;
363 break;
364 case 't':
365 numbering_properties[section].type = number_nonempty;
366 break;
367 case 'p':
368 /* If there was a previous expression, throw it away. */
369 if (numbering_properties[section].type == number_regex)
370 regfree(&numbering_properties[section].expr);
371 else
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));
380 errx(EXIT_FAILURE,
381 "%s expr: %s -- %s",
382 numbering_properties[section].name, errorbuf,
383 &argstr[1]);
385 break;
386 default:
387 errx(EXIT_FAILURE,
388 "illegal %s line numbering type -- %s",
389 numbering_properties[section].name, argstr);
393 static void
394 usage(char* argv0)
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);
399 exit(EXIT_FAILURE);