xref: /freebsd/usr.bin/nl/nl.c (revision 1de7b4b8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3  *
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Klaus Klein.
9  *
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.
18  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT(
35 "@(#) Copyright (c) 1999\
36  The NetBSD Foundation, Inc.  All rights reserved.");
37 __RCSID("$FreeBSD$");
38 #endif
39 
40 #include <sys/types.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <limits.h>
45 #include <locale.h>
46 #include <regex.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <wchar.h>
52 
53 typedef enum {
54 	number_all,		/* number all lines */
55 	number_nonempty,	/* number non-empty lines */
56 	number_none,		/* no line numbering */
57 	number_regex		/* number lines matching regular expression */
58 } numbering_type;
59 
60 struct numbering_property {
61 	const char * const	name;		/* for diagnostics */
62 	numbering_type		type;		/* numbering type */
63 	regex_t			expr;		/* for type == number_regex */
64 };
65 
66 /* line numbering formats */
67 #define FORMAT_LN	"%-*d"	/* left justified, leading zeros suppressed */
68 #define FORMAT_RN	"%*d"	/* right justified, leading zeros suppressed */
69 #define FORMAT_RZ	"%0*d"	/* right justified, leading zeros kept */
70 
71 #define FOOTER		0
72 #define BODY		1
73 #define HEADER		2
74 #define NP_LAST		HEADER
75 
76 static struct numbering_property numbering_properties[NP_LAST + 1] = {
77 	{ .name = "footer", .type = number_none },
78 	{ .name = "body", .type = number_nonempty },
79 	{ .name = "header", .type = number_none }
80 };
81 
82 #define max(a, b)	((a) > (b) ? (a) : (b))
83 
84 /*
85  * Maximum number of characters required for a decimal representation of a
86  * (signed) int; courtesy of tzcode.
87  */
88 #define INT_STRLEN_MAXIMUM \
89 	((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
90 
91 static void	filter(void);
92 static void	parse_numbering(const char *, int);
93 static void	usage(void);
94 
95 /*
96  * Dynamically allocated buffer suitable for string representation of ints.
97  */
98 static char *intbuffer;
99 
100 /* delimiter characters that indicate the start of a logical page section */
101 static char delim[2 * MB_LEN_MAX];
102 static int delimlen;
103 
104 /*
105  * Configurable parameters.
106  */
107 
108 /* line numbering format */
109 static const char *format = FORMAT_RN;
110 
111 /* increment value used to number logical page lines */
112 static int incr = 1;
113 
114 /* number of adjacent blank lines to be considered (and numbered) as one */
115 static unsigned int nblank = 1;
116 
117 /* whether to restart numbering at logical page delimiters */
118 static int restart = 1;
119 
120 /* characters used in separating the line number and the corrsp. text line */
121 static const char *sep = "\t";
122 
123 /* initial value used to number logical page lines */
124 static int startnum = 1;
125 
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;
129 
130 
131 int
132 main(int argc, char *argv[])
133 {
134 	int c;
135 	long val;
136 	unsigned long uval;
137 	char *ep;
138 	size_t intbuffersize, clen;
139 	char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
140 	size_t delim1len = 1, delim2len = 1;
141 
142 	(void)setlocale(LC_ALL, "");
143 
144 	while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
145 		switch (c) {
146 		case 'p':
147 			restart = 0;
148 			break;
149 		case 'b':
150 			parse_numbering(optarg, BODY);
151 			break;
152 		case 'd':
153 			clen = mbrlen(optarg, MB_CUR_MAX, NULL);
154 			if (clen == (size_t)-1 || clen == (size_t)-2)
155 				errc(EXIT_FAILURE, EILSEQ, NULL);
156 			if (clen != 0) {
157 				memcpy(delim1, optarg, delim1len = clen);
158 				clen = mbrlen(optarg + delim1len,
159 				    MB_CUR_MAX, NULL);
160 				if (clen == (size_t)-1 ||
161 				    clen == (size_t)-2)
162 					errc(EXIT_FAILURE, EILSEQ, NULL);
163 				if (clen != 0) {
164 					memcpy(delim2, optarg + delim1len,
165 					    delim2len = clen);
166 				if (optarg[delim1len + clen] != '\0')
167 					errx(EXIT_FAILURE,
168 					    "invalid delim argument -- %s",
169 					    optarg);
170 				}
171 			}
172 			break;
173 		case 'f':
174 			parse_numbering(optarg, FOOTER);
175 			break;
176 		case 'h':
177 			parse_numbering(optarg, HEADER);
178 			break;
179 		case 'i':
180 			errno = 0;
181 			val = strtol(optarg, &ep, 10);
182 			if ((ep != NULL && *ep != '\0') ||
183 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
184 				errx(EXIT_FAILURE,
185 				    "invalid incr argument -- %s", optarg);
186 			incr = (int)val;
187 			break;
188 		case 'l':
189 			errno = 0;
190 			uval = strtoul(optarg, &ep, 10);
191 			if ((ep != NULL && *ep != '\0') ||
192 			    (uval == ULONG_MAX && errno != 0))
193 				errx(EXIT_FAILURE,
194 				    "invalid num argument -- %s", optarg);
195 			nblank = (unsigned int)uval;
196 			break;
197 		case 'n':
198 			if (strcmp(optarg, "ln") == 0) {
199 				format = FORMAT_LN;
200 			} else if (strcmp(optarg, "rn") == 0) {
201 				format = FORMAT_RN;
202 			} else if (strcmp(optarg, "rz") == 0) {
203 				format = FORMAT_RZ;
204 			} else
205 				errx(EXIT_FAILURE,
206 				    "illegal format -- %s", optarg);
207 			break;
208 		case 's':
209 			sep = optarg;
210 			break;
211 		case 'v':
212 			errno = 0;
213 			val = strtol(optarg, &ep, 10);
214 			if ((ep != NULL && *ep != '\0') ||
215 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
216 				errx(EXIT_FAILURE,
217 				    "invalid startnum value -- %s", optarg);
218 			startnum = (int)val;
219 			break;
220 		case 'w':
221 			errno = 0;
222 			val = strtol(optarg, &ep, 10);
223 			if ((ep != NULL && *ep != '\0') ||
224 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
225 				errx(EXIT_FAILURE,
226 				    "invalid width value -- %s", optarg);
227 			width = (int)val;
228 			if (!(width > 0))
229 				errx(EXIT_FAILURE,
230 				    "width argument must be > 0 -- %d",
231 				    width);
232 			break;
233 		case '?':
234 		default:
235 			usage();
236 			/* NOTREACHED */
237 		}
238 	}
239 	argc -= optind;
240 	argv += optind;
241 
242 	switch (argc) {
243 	case 0:
244 		break;
245 	case 1:
246 		if (strcmp(argv[0], "-") != 0 &&
247 		    freopen(argv[0], "r", stdin) == NULL)
248 			err(EXIT_FAILURE, "%s", argv[0]);
249 		break;
250 	default:
251 		usage();
252 		/* NOTREACHED */
253 	}
254 
255 	/* Generate the delimiter sequence */
256 	memcpy(delim, delim1, delim1len);
257 	memcpy(delim + delim1len, delim2, delim2len);
258 	delimlen = delim1len + delim2len;
259 
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");
264 
265 	/* Do the work. */
266 	filter();
267 
268 	exit(EXIT_SUCCESS);
269 	/* NOTREACHED */
270 }
271 
272 static void
273 filter(void)
274 {
275 	char *buffer;
276 	size_t buffersize;
277 	ssize_t linelen;
278 	int line;		/* logical line number */
279 	int section;		/* logical page section */
280 	unsigned int adjblank;	/* adjacent blank lines */
281 	int consumed;		/* intbuffer measurement */
282 	int donumber = 0, idx;
283 
284 	adjblank = 0;
285 	line = startnum;
286 	section = BODY;
287 
288 	buffer = NULL;
289 	buffersize = 0;
290 	while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
291 		for (idx = FOOTER; idx <= NP_LAST; idx++) {
292 			/* Does it look like a delimiter? */
293 			if (delimlen * (idx + 1) > linelen)
294 				break;
295 			if (memcmp(buffer + delimlen * idx, delim,
296 			    delimlen) != 0)
297 				break;
298 			/* Was this the whole line? */
299 			if (buffer[delimlen * (idx + 1)] == '\n') {
300 				section = idx;
301 				adjblank = 0;
302 				if (restart)
303 					line = startnum;
304 				goto nextline;
305 			}
306 		}
307 
308 		switch (numbering_properties[section].type) {
309 		case number_all:
310 			/*
311 			 * Doing this for number_all only is disputable, but
312 			 * the standard expresses an explicit dependency on
313 			 * `-b a' etc.
314 			 */
315 			if (buffer[0] == '\n' && ++adjblank < nblank)
316 				donumber = 0;
317 			else
318 				donumber = 1, adjblank = 0;
319 			break;
320 		case number_nonempty:
321 			donumber = (buffer[0] != '\n');
322 			break;
323 		case number_none:
324 			donumber = 0;
325 			break;
326 		case number_regex:
327 			donumber =
328 			    (regexec(&numbering_properties[section].expr,
329 			    buffer, 0, NULL, 0) == 0);
330 			break;
331 		}
332 
333 		if (donumber) {
334 			/* Note: sprintf() is safe here. */
335 			consumed = sprintf(intbuffer, format, width, line);
336 			(void)printf("%s",
337 			    intbuffer + max(0, consumed - width));
338 			line += incr;
339 		} else {
340 			(void)printf("%*s", width, "");
341 		}
342 		(void)fputs(sep, stdout);
343 		(void)fwrite(buffer, linelen, 1, stdout);
344 
345 		if (ferror(stdout))
346 			err(EXIT_FAILURE, "output error");
347 nextline:
348 		;
349 	}
350 
351 	if (ferror(stdin))
352 		err(EXIT_FAILURE, "input error");
353 
354 	free(buffer);
355 }
356 
357 /*
358  * Various support functions.
359  */
360 
361 static void
362 parse_numbering(const char *argstr, int section)
363 {
364 	int error;
365 	char errorbuf[NL_TEXTMAX];
366 
367 	switch (argstr[0]) {
368 	case 'a':
369 		numbering_properties[section].type = number_all;
370 		break;
371 	case 'n':
372 		numbering_properties[section].type = number_none;
373 		break;
374 	case 't':
375 		numbering_properties[section].type = number_nonempty;
376 		break;
377 	case 'p':
378 		/* If there was a previous expression, throw it away. */
379 		if (numbering_properties[section].type == number_regex)
380 			regfree(&numbering_properties[section].expr);
381 		else
382 			numbering_properties[section].type = number_regex;
383 
384 		/* Compile/validate the supplied regular expression. */
385 		if ((error = regcomp(&numbering_properties[section].expr,
386 		    &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
387 			(void)regerror(error,
388 			    &numbering_properties[section].expr,
389 			    errorbuf, sizeof (errorbuf));
390 			errx(EXIT_FAILURE,
391 			    "%s expr: %s -- %s",
392 			    numbering_properties[section].name, errorbuf,
393 			    &argstr[1]);
394 		}
395 		break;
396 	default:
397 		errx(EXIT_FAILURE,
398 		    "illegal %s line numbering type -- %s",
399 		    numbering_properties[section].name, argstr);
400 	}
401 }
402 
403 static void
404 usage(void)
405 {
406 
407 	(void)fprintf(stderr,
408 "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
409 "          [-n format] [-s sep] [-v startnum] [-w width] [file]\n");
410 	exit(EXIT_FAILURE);
411 }
412