xref: /freebsd/usr.bin/nl/nl.c (revision 33ec7f265b22d9d4fe4402a1efbd734352b72954)
1f2155981SBrian Somers /*-
2f2155981SBrian Somers  * Copyright (c) 1999 The NetBSD Foundation, Inc.
3f2155981SBrian Somers  * All rights reserved.
4f2155981SBrian Somers  *
5f2155981SBrian Somers  * This code is derived from software contributed to The NetBSD Foundation
6f2155981SBrian Somers  * by Klaus Klein.
7f2155981SBrian Somers  *
8f2155981SBrian Somers  * Redistribution and use in source and binary forms, with or without
9f2155981SBrian Somers  * modification, are permitted provided that the following conditions
10f2155981SBrian Somers  * are met:
11f2155981SBrian Somers  * 1. Redistributions of source code must retain the above copyright
12f2155981SBrian Somers  *    notice, this list of conditions and the following disclaimer.
13f2155981SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
14f2155981SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
15f2155981SBrian Somers  *    documentation and/or other materials provided with the distribution.
16f2155981SBrian Somers  * 3. All advertising materials mentioning features or use of this software
17f2155981SBrian Somers  *    must display the following acknowledgement:
18f2155981SBrian Somers  *        This product includes software developed by the NetBSD
19f2155981SBrian Somers  *        Foundation, Inc. and its contributors.
20f2155981SBrian Somers  * 4. Neither the name of The NetBSD Foundation nor the names of its
21f2155981SBrian Somers  *    contributors may be used to endorse or promote products derived
22f2155981SBrian Somers  *    from this software without specific prior written permission.
23f2155981SBrian Somers  *
24f2155981SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25f2155981SBrian Somers  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26f2155981SBrian Somers  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27f2155981SBrian Somers  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28f2155981SBrian Somers  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29f2155981SBrian Somers  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30f2155981SBrian Somers  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31f2155981SBrian Somers  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32f2155981SBrian Somers  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33f2155981SBrian Somers  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34f2155981SBrian Somers  * POSSIBILITY OF SUCH DAMAGE.
35f2155981SBrian Somers  */
36f2155981SBrian Somers 
37f2155981SBrian Somers #include <sys/cdefs.h>
38f2155981SBrian Somers #ifndef lint
39f2155981SBrian Somers __COPYRIGHT(
40f2155981SBrian Somers "@(#) Copyright (c) 1999\
41f2155981SBrian Somers  The NetBSD Foundation, Inc.  All rights reserved.");
42f2155981SBrian Somers __RCSID("$FreeBSD$");
43f2155981SBrian Somers #endif
44f2155981SBrian Somers 
45f2155981SBrian Somers #include <sys/types.h>
46f2155981SBrian Somers 
47f1e20ff7STim J. Robbins #include <err.h>
48f2155981SBrian Somers #include <errno.h>
49f2155981SBrian Somers #include <limits.h>
50f2155981SBrian Somers #include <locale.h>
51f2155981SBrian Somers #include <regex.h>
52f2155981SBrian Somers #include <stdio.h>
53f2155981SBrian Somers #include <stdlib.h>
54f2155981SBrian Somers #include <string.h>
55f2155981SBrian Somers #include <unistd.h>
5633ec7f26STim J. Robbins #include <wchar.h>
57f2155981SBrian Somers 
58f2155981SBrian Somers typedef enum {
59f2155981SBrian Somers 	number_all,		/* number all lines */
60f2155981SBrian Somers 	number_nonempty,	/* number non-empty lines */
61f2155981SBrian Somers 	number_none,		/* no line numbering */
62f2155981SBrian Somers 	number_regex		/* number lines matching regular expression */
63f2155981SBrian Somers } numbering_type;
64f2155981SBrian Somers 
65f2155981SBrian Somers struct numbering_property {
66f2155981SBrian Somers 	const char * const	name;		/* for diagnostics */
67f2155981SBrian Somers 	numbering_type		type;		/* numbering type */
68f2155981SBrian Somers 	regex_t			expr;		/* for type == number_regex */
69f2155981SBrian Somers };
70f2155981SBrian Somers 
71f2155981SBrian Somers /* line numbering formats */
72f2155981SBrian Somers #define FORMAT_LN	"%-*d"	/* left justified, leading zeros suppressed */
73f2155981SBrian Somers #define FORMAT_RN	"%*d"	/* right justified, leading zeros suppressed */
74f2155981SBrian Somers #define FORMAT_RZ	"%0*d"	/* right justified, leading zeros kept */
75f2155981SBrian Somers 
76f2155981SBrian Somers #define FOOTER		0
77f2155981SBrian Somers #define BODY		1
78f2155981SBrian Somers #define HEADER		2
79f2155981SBrian Somers #define NP_LAST		HEADER
80f2155981SBrian Somers 
81f2155981SBrian Somers static struct numbering_property numbering_properties[NP_LAST + 1] = {
82f2155981SBrian Somers 	{ "footer",	number_none	},
83f2155981SBrian Somers 	{ "body",	number_nonempty	},
84f2155981SBrian Somers 	{ "header",	number_none	}
85f2155981SBrian Somers };
86f2155981SBrian Somers 
87f2155981SBrian Somers #define max(a, b)	((a) > (b) ? (a) : (b))
88f2155981SBrian Somers 
89f2155981SBrian Somers /*
90f2155981SBrian Somers  * Maximum number of characters required for a decimal representation of a
91f2155981SBrian Somers  * (signed) int; courtesy of tzcode.
92f2155981SBrian Somers  */
93f2155981SBrian Somers #define INT_STRLEN_MAXIMUM \
94f2155981SBrian Somers 	((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
95f2155981SBrian Somers 
96d3cb5dedSWarner Losh static void	filter(void);
97d3cb5dedSWarner Losh static void	parse_numbering(const char *, int);
98d3cb5dedSWarner Losh static void	usage(void);
99f2155981SBrian Somers 
100f2155981SBrian Somers /*
101f2155981SBrian Somers  * Pointer to dynamically allocated input line buffer, and its size.
102f2155981SBrian Somers  */
103f2155981SBrian Somers static char *buffer;
104f2155981SBrian Somers static size_t buffersize;
105f2155981SBrian Somers 
106f2155981SBrian Somers /*
107f2155981SBrian Somers  * Dynamically allocated buffer suitable for string representation of ints.
108f2155981SBrian Somers  */
109f2155981SBrian Somers static char *intbuffer;
110f2155981SBrian Somers 
11133ec7f26STim J. Robbins /* delimiter characters that indicate the start of a logical page section */
11233ec7f26STim J. Robbins static char delim[2 * MB_LEN_MAX];
11333ec7f26STim J. Robbins static int delimlen;
11433ec7f26STim J. Robbins 
115f2155981SBrian Somers /*
116f2155981SBrian Somers  * Configurable parameters.
117f2155981SBrian Somers  */
118f2155981SBrian Somers 
119f2155981SBrian Somers /* line numbering format */
120f2155981SBrian Somers static const char *format = FORMAT_RN;
121f2155981SBrian Somers 
122f2155981SBrian Somers /* increment value used to number logical page lines */
123f2155981SBrian Somers static int incr = 1;
124f2155981SBrian Somers 
125f2155981SBrian Somers /* number of adjacent blank lines to be considered (and numbered) as one */
126f2155981SBrian Somers static unsigned int nblank = 1;
127f2155981SBrian Somers 
128f2155981SBrian Somers /* whether to restart numbering at logical page delimiters */
129f2155981SBrian Somers static int restart = 1;
130f2155981SBrian Somers 
131f2155981SBrian Somers /* characters used in separating the line number and the corrsp. text line */
132f2155981SBrian Somers static const char *sep = "\t";
133f2155981SBrian Somers 
134f2155981SBrian Somers /* initial value used to number logical page lines */
135f2155981SBrian Somers static int startnum = 1;
136f2155981SBrian Somers 
137f2155981SBrian Somers /* number of characters to be used for the line number */
138f2155981SBrian Somers /* should be unsigned but required signed by `*' precision conversion */
139f2155981SBrian Somers static int width = 6;
140f2155981SBrian Somers 
141f2155981SBrian Somers 
142f2155981SBrian Somers int
143f2155981SBrian Somers main(argc, argv)
144f2155981SBrian Somers 	int argc;
145f2155981SBrian Somers 	char *argv[];
146f2155981SBrian Somers {
14733ec7f26STim J. Robbins 	int c, n;
148f2155981SBrian Somers 	long val;
149f2155981SBrian Somers 	unsigned long uval;
150f2155981SBrian Somers 	char *ep;
15133ec7f26STim J. Robbins 	size_t intbuffersize, clen;
15233ec7f26STim J. Robbins 	char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
15333ec7f26STim J. Robbins 	size_t delim1len = 1, delim2len = 1;
154f2155981SBrian Somers 
155f2155981SBrian Somers 	(void)setlocale(LC_ALL, "");
156f2155981SBrian Somers 
157f2155981SBrian Somers 	while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
158f2155981SBrian Somers 		switch (c) {
159f2155981SBrian Somers 		case 'p':
160f2155981SBrian Somers 			restart = 0;
161f2155981SBrian Somers 			break;
162f2155981SBrian Somers 		case 'b':
163f2155981SBrian Somers 			parse_numbering(optarg, BODY);
164f2155981SBrian Somers 			break;
165f2155981SBrian Somers 		case 'd':
16633ec7f26STim J. Robbins 			clen = mbrlen(optarg, MB_CUR_MAX, NULL);
16733ec7f26STim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2)
16833ec7f26STim J. Robbins 				errc(EXIT_FAILURE, EILSEQ, NULL);
16933ec7f26STim J. Robbins 			if (clen != 0) {
17033ec7f26STim J. Robbins 				memcpy(delim1, optarg, delim1len = clen);
17133ec7f26STim J. Robbins 				clen = mbrlen(optarg + delim1len,
17233ec7f26STim J. Robbins 				    MB_CUR_MAX, NULL);
17333ec7f26STim J. Robbins 				if (clen == (size_t)-1 ||
17433ec7f26STim J. Robbins 				    clen == (size_t)-2)
17533ec7f26STim J. Robbins 					errc(EXIT_FAILURE, EILSEQ, NULL);
17633ec7f26STim J. Robbins 				if (clen != 0) {
17733ec7f26STim J. Robbins 					memcpy(delim2, optarg + delim1len,
17833ec7f26STim J. Robbins 					    delim2len = clen);
17933ec7f26STim J. Robbins 				if (optarg[delim1len + clen] != '\0')
18053e29ec5STim J. Robbins 					errx(EXIT_FAILURE,
18153e29ec5STim J. Robbins 					    "invalid delim argument -- %s",
182f2155981SBrian Somers 					    optarg);
18333ec7f26STim J. Robbins 				}
184f2155981SBrian Somers 			}
185f2155981SBrian Somers 			break;
186f2155981SBrian Somers 		case 'f':
187f2155981SBrian Somers 			parse_numbering(optarg, FOOTER);
188f2155981SBrian Somers 			break;
189f2155981SBrian Somers 		case 'h':
190f2155981SBrian Somers 			parse_numbering(optarg, HEADER);
191f2155981SBrian Somers 			break;
192f2155981SBrian Somers 		case 'i':
193f2155981SBrian Somers 			errno = 0;
194f2155981SBrian Somers 			val = strtol(optarg, &ep, 10);
195f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
19653e29ec5STim J. Robbins 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
19753e29ec5STim J. Robbins 				errx(EXIT_FAILURE,
19853e29ec5STim J. Robbins 				    "invalid incr argument -- %s", optarg);
199f2155981SBrian Somers 			incr = (int)val;
200f2155981SBrian Somers 			break;
201f2155981SBrian Somers 		case 'l':
202f2155981SBrian Somers 			errno = 0;
203f2155981SBrian Somers 			uval = strtoul(optarg, &ep, 10);
204f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
20553e29ec5STim J. Robbins 			    (uval == ULONG_MAX && errno != 0))
20653e29ec5STim J. Robbins 				errx(EXIT_FAILURE,
20753e29ec5STim J. Robbins 				    "invalid num argument -- %s", optarg);
208f2155981SBrian Somers 			nblank = (unsigned int)uval;
209f2155981SBrian Somers 			break;
210f2155981SBrian Somers 		case 'n':
211f2155981SBrian Somers 			if (strcmp(optarg, "ln") == 0) {
212f2155981SBrian Somers 				format = FORMAT_LN;
213f2155981SBrian Somers 			} else if (strcmp(optarg, "rn") == 0) {
214f2155981SBrian Somers 				format = FORMAT_RN;
215f2155981SBrian Somers 			} else if (strcmp(optarg, "rz") == 0) {
216f2155981SBrian Somers 				format = FORMAT_RZ;
21753e29ec5STim J. Robbins 			} else
21853e29ec5STim J. Robbins 				errx(EXIT_FAILURE,
21953e29ec5STim J. Robbins 				    "illegal format -- %s", optarg);
220f2155981SBrian Somers 			break;
221f2155981SBrian Somers 		case 's':
222f2155981SBrian Somers 			sep = optarg;
223f2155981SBrian Somers 			break;
224f2155981SBrian Somers 		case 'v':
225f2155981SBrian Somers 			errno = 0;
226f2155981SBrian Somers 			val = strtol(optarg, &ep, 10);
227f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
22853e29ec5STim J. Robbins 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
22953e29ec5STim J. Robbins 				errx(EXIT_FAILURE,
23053e29ec5STim J. Robbins 				    "invalid startnum value -- %s", optarg);
231f2155981SBrian Somers 			startnum = (int)val;
232f2155981SBrian Somers 			break;
233f2155981SBrian Somers 		case 'w':
234f2155981SBrian Somers 			errno = 0;
235f2155981SBrian Somers 			val = strtol(optarg, &ep, 10);
236f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
23753e29ec5STim J. Robbins 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
23853e29ec5STim J. Robbins 				errx(EXIT_FAILURE,
23953e29ec5STim J. Robbins 				    "invalid width value -- %s", optarg);
240f2155981SBrian Somers 			width = (int)val;
24153e29ec5STim J. Robbins 			if (!(width > 0))
24253e29ec5STim J. Robbins 				errx(EXIT_FAILURE,
24353e29ec5STim J. Robbins 				    "width argument must be > 0 -- %d",
244f2155981SBrian Somers 				    width);
245f2155981SBrian Somers 			break;
246f2155981SBrian Somers 		case '?':
247f2155981SBrian Somers 		default:
248f2155981SBrian Somers 			usage();
249f2155981SBrian Somers 			/* NOTREACHED */
250f2155981SBrian Somers 		}
251f2155981SBrian Somers 	}
252f2155981SBrian Somers 	argc -= optind;
253f2155981SBrian Somers 	argv += optind;
254f2155981SBrian Somers 
255f2155981SBrian Somers 	switch (argc) {
256f2155981SBrian Somers 	case 0:
257f2155981SBrian Somers 		break;
258f2155981SBrian Somers 	case 1:
259f1e20ff7STim J. Robbins 		if (freopen(argv[0], "r", stdin) == NULL)
260f1e20ff7STim J. Robbins 			err(EXIT_FAILURE, "%s", argv[0]);
261f2155981SBrian Somers 		break;
262f2155981SBrian Somers 	default:
263f2155981SBrian Somers 		usage();
264f2155981SBrian Somers 		/* NOTREACHED */
265f2155981SBrian Somers 	}
266f2155981SBrian Somers 
26733ec7f26STim J. Robbins 	/* Generate the delimiter sequence */
26833ec7f26STim J. Robbins 	memcpy(delim, delim1, delim1len);
26933ec7f26STim J. Robbins 	memcpy(delim + delim1len, delim2, delim2len);
27033ec7f26STim J. Robbins 	delimlen = delim1len + delim2len;
27133ec7f26STim J. Robbins 
272f2155981SBrian Somers 	/* Determine the maximum input line length to operate on. */
273f2155981SBrian Somers 	if ((val = sysconf(_SC_LINE_MAX)) == -1) /* ignore errno */
274f2155981SBrian Somers 		val = LINE_MAX;
275f2155981SBrian Somers 	/* Allocate sufficient buffer space (including the terminating NUL). */
276f2155981SBrian Somers 	buffersize = (size_t)val + 1;
277f1e20ff7STim J. Robbins 	if ((buffer = malloc(buffersize)) == NULL)
278f1e20ff7STim J. Robbins 		err(EXIT_FAILURE, "cannot allocate input line buffer");
279f2155981SBrian Somers 
280f2155981SBrian Somers 	/* Allocate a buffer suitable for preformatting line number. */
281f2155981SBrian Somers 	intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1;	/* NUL */
282f1e20ff7STim J. Robbins 	if ((intbuffer = malloc(intbuffersize)) == NULL)
283f1e20ff7STim J. Robbins 		err(EXIT_FAILURE, "cannot allocate preformatting buffer");
284f2155981SBrian Somers 
285f2155981SBrian Somers 	/* Do the work. */
286f2155981SBrian Somers 	filter();
287f2155981SBrian Somers 
288f2155981SBrian Somers 	exit(EXIT_SUCCESS);
289f2155981SBrian Somers 	/* NOTREACHED */
290f2155981SBrian Somers }
291f2155981SBrian Somers 
292f2155981SBrian Somers static void
293f2155981SBrian Somers filter()
294f2155981SBrian Somers {
295f2155981SBrian Somers 	int line;		/* logical line number */
296f2155981SBrian Somers 	int section;		/* logical page section */
297f2155981SBrian Somers 	unsigned int adjblank;	/* adjacent blank lines */
298f2155981SBrian Somers 	int consumed;		/* intbuffer measurement */
299f2155981SBrian Somers 	int donumber, idx;
300f2155981SBrian Somers 
301f2155981SBrian Somers 	adjblank = 0;
302f2155981SBrian Somers 	line = startnum;
303f2155981SBrian Somers 	section = BODY;
304f2155981SBrian Somers #ifdef __GNUC__
305f2155981SBrian Somers 	(void)&donumber;	/* avoid bogus `uninitialized' warning */
306f2155981SBrian Somers #endif
307f2155981SBrian Somers 
308f2155981SBrian Somers 	while (fgets(buffer, (int)buffersize, stdin) != NULL) {
309f2155981SBrian Somers 		for (idx = FOOTER; idx <= NP_LAST; idx++) {
310f2155981SBrian Somers 			/* Does it look like a delimiter? */
31133ec7f26STim J. Robbins 			if (memcmp(buffer + delimlen * idx, delim,
31233ec7f26STim J. Robbins 			    delimlen) == 0) {
313f2155981SBrian Somers 				/* Was this the whole line? */
31433ec7f26STim J. Robbins 				if (buffer[delimlen * (idx + 1)] == '\n') {
315f2155981SBrian Somers 					section = idx;
316f2155981SBrian Somers 					adjblank = 0;
317f2155981SBrian Somers 					if (restart)
318f2155981SBrian Somers 						line = startnum;
319f2155981SBrian Somers 					goto nextline;
320f2155981SBrian Somers 				}
321f2155981SBrian Somers 			} else {
322f2155981SBrian Somers 				break;
323f2155981SBrian Somers 			}
324f2155981SBrian Somers 		}
325f2155981SBrian Somers 
326f2155981SBrian Somers 		switch (numbering_properties[section].type) {
327f2155981SBrian Somers 		case number_all:
328f2155981SBrian Somers 			/*
329f2155981SBrian Somers 			 * Doing this for number_all only is disputable, but
330f2155981SBrian Somers 			 * the standard expresses an explicit dependency on
331f2155981SBrian Somers 			 * `-b a' etc.
332f2155981SBrian Somers 			 */
333f2155981SBrian Somers 			if (buffer[0] == '\n' && ++adjblank < nblank)
334f2155981SBrian Somers 				donumber = 0;
335f2155981SBrian Somers 			else
336f2155981SBrian Somers 				donumber = 1, adjblank = 0;
337f2155981SBrian Somers 			break;
338f2155981SBrian Somers 		case number_nonempty:
339f2155981SBrian Somers 			donumber = (buffer[0] != '\n');
340f2155981SBrian Somers 			break;
341f2155981SBrian Somers 		case number_none:
342f2155981SBrian Somers 			donumber = 0;
343f2155981SBrian Somers 			break;
344f2155981SBrian Somers 		case number_regex:
345f2155981SBrian Somers 			donumber =
346f2155981SBrian Somers 			    (regexec(&numbering_properties[section].expr,
347f2155981SBrian Somers 			    buffer, 0, NULL, 0) == 0);
348f2155981SBrian Somers 			break;
349f2155981SBrian Somers 		}
350f2155981SBrian Somers 
351f2155981SBrian Somers 		if (donumber) {
352f2155981SBrian Somers 			/* Note: sprintf() is safe here. */
353f2155981SBrian Somers 			consumed = sprintf(intbuffer, format, width, line);
354f2155981SBrian Somers 			(void)printf("%s",
355f2155981SBrian Somers 			    intbuffer + max(0, consumed - width));
356f2155981SBrian Somers 			line += incr;
357f2155981SBrian Somers 		} else {
358f2155981SBrian Somers 			(void)printf("%*s", width, "");
359f2155981SBrian Somers 		}
360f2155981SBrian Somers 		(void)printf("%s%s", sep, buffer);
361f2155981SBrian Somers 
362f1e20ff7STim J. Robbins 		if (ferror(stdout))
363f1e20ff7STim J. Robbins 			err(EXIT_FAILURE, "output error");
364f2155981SBrian Somers nextline:
365f2155981SBrian Somers 		;
366f2155981SBrian Somers 	}
367f2155981SBrian Somers 
368f1e20ff7STim J. Robbins 	if (ferror(stdin))
369f1e20ff7STim J. Robbins 		err(EXIT_FAILURE, "input error");
370f2155981SBrian Somers }
371f2155981SBrian Somers 
372f2155981SBrian Somers /*
373f2155981SBrian Somers  * Various support functions.
374f2155981SBrian Somers  */
375f2155981SBrian Somers 
376f2155981SBrian Somers static void
377f2155981SBrian Somers parse_numbering(argstr, section)
378f2155981SBrian Somers 	const char *argstr;
379f2155981SBrian Somers 	int section;
380f2155981SBrian Somers {
381f2155981SBrian Somers 	int error;
382f2155981SBrian Somers 	char errorbuf[NL_TEXTMAX];
383f2155981SBrian Somers 
384f2155981SBrian Somers 	switch (argstr[0]) {
385f2155981SBrian Somers 	case 'a':
386f2155981SBrian Somers 		numbering_properties[section].type = number_all;
387f2155981SBrian Somers 		break;
388f2155981SBrian Somers 	case 'n':
389f2155981SBrian Somers 		numbering_properties[section].type = number_none;
390f2155981SBrian Somers 		break;
391f2155981SBrian Somers 	case 't':
392f2155981SBrian Somers 		numbering_properties[section].type = number_nonempty;
393f2155981SBrian Somers 		break;
394f2155981SBrian Somers 	case 'p':
395f2155981SBrian Somers 		/* If there was a previous expression, throw it away. */
396f2155981SBrian Somers 		if (numbering_properties[section].type == number_regex)
397f2155981SBrian Somers 			regfree(&numbering_properties[section].expr);
398f2155981SBrian Somers 		else
399f2155981SBrian Somers 			numbering_properties[section].type = number_regex;
400f2155981SBrian Somers 
401f2155981SBrian Somers 		/* Compile/validate the supplied regular expression. */
402f2155981SBrian Somers 		if ((error = regcomp(&numbering_properties[section].expr,
403f2155981SBrian Somers 		    &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
404f2155981SBrian Somers 			(void)regerror(error,
405f2155981SBrian Somers 			    &numbering_properties[section].expr,
406f2155981SBrian Somers 			    errorbuf, sizeof (errorbuf));
40753e29ec5STim J. Robbins 			errx(EXIT_FAILURE,
40853e29ec5STim J. Robbins 			    "%s expr: %s -- %s",
409f2155981SBrian Somers 			    numbering_properties[section].name, errorbuf,
410f2155981SBrian Somers 			    &argstr[1]);
411f2155981SBrian Somers 		}
412f2155981SBrian Somers 		break;
413f2155981SBrian Somers 	default:
41453e29ec5STim J. Robbins 		errx(EXIT_FAILURE,
41553e29ec5STim J. Robbins 		    "illegal %s line numbering type -- %s",
416f2155981SBrian Somers 		    numbering_properties[section].name, argstr);
417f2155981SBrian Somers 	}
418f2155981SBrian Somers }
419f2155981SBrian Somers 
420f2155981SBrian Somers static void
421f2155981SBrian Somers usage()
422f2155981SBrian Somers {
423f2155981SBrian Somers 
4247007f3d6STim J. Robbins 	(void)fprintf(stderr,
4257007f3d6STim J. Robbins "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
4267007f3d6STim J. Robbins "          [-n format] [-s sep] [-v startnum] [-w width] [file]\n");
427f2155981SBrian Somers 	exit(EXIT_FAILURE);
428f2155981SBrian Somers }
429