xref: /freebsd/usr.bin/nl/nl.c (revision f21559815d204e3a57da3222866b83a11f290ded)
1f2155981SBrian Somers /* $FreeBSD$ */
2f2155981SBrian Somers 
3f2155981SBrian Somers /*-
4f2155981SBrian Somers  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5f2155981SBrian Somers  * All rights reserved.
6f2155981SBrian Somers  *
7f2155981SBrian Somers  * This code is derived from software contributed to The NetBSD Foundation
8f2155981SBrian Somers  * by Klaus Klein.
9f2155981SBrian Somers  *
10f2155981SBrian Somers  * Redistribution and use in source and binary forms, with or without
11f2155981SBrian Somers  * modification, are permitted provided that the following conditions
12f2155981SBrian Somers  * are met:
13f2155981SBrian Somers  * 1. Redistributions of source code must retain the above copyright
14f2155981SBrian Somers  *    notice, this list of conditions and the following disclaimer.
15f2155981SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
16f2155981SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
17f2155981SBrian Somers  *    documentation and/or other materials provided with the distribution.
18f2155981SBrian Somers  * 3. All advertising materials mentioning features or use of this software
19f2155981SBrian Somers  *    must display the following acknowledgement:
20f2155981SBrian Somers  *        This product includes software developed by the NetBSD
21f2155981SBrian Somers  *        Foundation, Inc. and its contributors.
22f2155981SBrian Somers  * 4. Neither the name of The NetBSD Foundation nor the names of its
23f2155981SBrian Somers  *    contributors may be used to endorse or promote products derived
24f2155981SBrian Somers  *    from this software without specific prior written permission.
25f2155981SBrian Somers  *
26f2155981SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27f2155981SBrian Somers  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28f2155981SBrian Somers  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29f2155981SBrian Somers  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30f2155981SBrian Somers  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31f2155981SBrian Somers  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32f2155981SBrian Somers  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33f2155981SBrian Somers  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34f2155981SBrian Somers  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35f2155981SBrian Somers  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36f2155981SBrian Somers  * POSSIBILITY OF SUCH DAMAGE.
37f2155981SBrian Somers  */
38f2155981SBrian Somers 
39f2155981SBrian Somers #include <sys/cdefs.h>
40f2155981SBrian Somers #ifndef lint
41f2155981SBrian Somers __COPYRIGHT(
42f2155981SBrian Somers "@(#) Copyright (c) 1999\
43f2155981SBrian Somers  The NetBSD Foundation, Inc.  All rights reserved.");
44f2155981SBrian Somers __RCSID("$FreeBSD$");
45f2155981SBrian Somers #endif
46f2155981SBrian Somers 
47f2155981SBrian Somers #include <sys/types.h>
48f2155981SBrian Somers 
49f2155981SBrian Somers #include <errno.h>
50f2155981SBrian Somers #include <limits.h>
51f2155981SBrian Somers #include <locale.h>
52f2155981SBrian Somers #include <regex.h>
53f2155981SBrian Somers #include <stdio.h>
54f2155981SBrian Somers #include <stdlib.h>
55f2155981SBrian Somers #include <string.h>
56f2155981SBrian Somers #include <unistd.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 
96f2155981SBrian Somers static void	filter __P((void));
97f2155981SBrian Somers int		main __P((int, char *[]));
98f2155981SBrian Somers static void	parse_numbering __P((const char *, int));
99f2155981SBrian Somers static void	usage __P((void));
100f2155981SBrian Somers 
101f2155981SBrian Somers /*
102f2155981SBrian Somers  * Pointer to dynamically allocated input line buffer, and its size.
103f2155981SBrian Somers  */
104f2155981SBrian Somers static char *buffer;
105f2155981SBrian Somers static size_t buffersize;
106f2155981SBrian Somers 
107f2155981SBrian Somers /*
108f2155981SBrian Somers  * Dynamically allocated buffer suitable for string representation of ints.
109f2155981SBrian Somers  */
110f2155981SBrian Somers static char *intbuffer;
111f2155981SBrian Somers 
112f2155981SBrian Somers /*
113f2155981SBrian Somers  * Configurable parameters.
114f2155981SBrian Somers  */
115f2155981SBrian Somers /* delimiter characters that indicate the start of a logical page section */
116f2155981SBrian Somers static char delim[2] = { '\\', ':' };
117f2155981SBrian Somers 
118f2155981SBrian Somers /* line numbering format */
119f2155981SBrian Somers static const char *format = FORMAT_RN;
120f2155981SBrian Somers 
121f2155981SBrian Somers /* increment value used to number logical page lines */
122f2155981SBrian Somers static int incr = 1;
123f2155981SBrian Somers 
124f2155981SBrian Somers /* number of adjacent blank lines to be considered (and numbered) as one */
125f2155981SBrian Somers static unsigned int nblank = 1;
126f2155981SBrian Somers 
127f2155981SBrian Somers /* whether to restart numbering at logical page delimiters */
128f2155981SBrian Somers static int restart = 1;
129f2155981SBrian Somers 
130f2155981SBrian Somers /* characters used in separating the line number and the corrsp. text line */
131f2155981SBrian Somers static const char *sep = "\t";
132f2155981SBrian Somers 
133f2155981SBrian Somers /* initial value used to number logical page lines */
134f2155981SBrian Somers static int startnum = 1;
135f2155981SBrian Somers 
136f2155981SBrian Somers /* number of characters to be used for the line number */
137f2155981SBrian Somers /* should be unsigned but required signed by `*' precision conversion */
138f2155981SBrian Somers static int width = 6;
139f2155981SBrian Somers 
140f2155981SBrian Somers 
141f2155981SBrian Somers int
142f2155981SBrian Somers main(argc, argv)
143f2155981SBrian Somers 	int argc;
144f2155981SBrian Somers 	char *argv[];
145f2155981SBrian Somers {
146f2155981SBrian Somers 	int c;
147f2155981SBrian Somers 	long val;
148f2155981SBrian Somers 	unsigned long uval;
149f2155981SBrian Somers 	char *ep;
150f2155981SBrian Somers 	size_t intbuffersize;
151f2155981SBrian Somers 
152f2155981SBrian Somers 	(void)setlocale(LC_ALL, "");
153f2155981SBrian Somers 
154f2155981SBrian Somers 	/*
155f2155981SBrian Somers 	 * Note: this implementation strictly conforms to the XBD Utility
156f2155981SBrian Somers 	 * Syntax Guidelines and does not permit the optional `file' operand
157f2155981SBrian Somers 	 * to be intermingled with the options, which is defined in the
158f2155981SBrian Somers 	 * XCU specification (Issue 5) but declared an obsolescent feature that
159f2155981SBrian Somers 	 * will be removed from a future issue.  It shouldn't matter, though.
160f2155981SBrian Somers 	 */
161f2155981SBrian Somers 	while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
162f2155981SBrian Somers 		switch (c) {
163f2155981SBrian Somers 		case 'p':
164f2155981SBrian Somers 			restart = 0;
165f2155981SBrian Somers 			break;
166f2155981SBrian Somers 		case 'b':
167f2155981SBrian Somers 			parse_numbering(optarg, BODY);
168f2155981SBrian Somers 			break;
169f2155981SBrian Somers 		case 'd':
170f2155981SBrian Somers 			if (optarg[0] != '\0')
171f2155981SBrian Somers 				delim[0] = optarg[0];
172f2155981SBrian Somers 			if (optarg[1] != '\0')
173f2155981SBrian Somers 				delim[1] = optarg[1];
174f2155981SBrian Somers 			/* at most two delimiter characters */
175f2155981SBrian Somers 			if (optarg[2] != '\0') {
176f2155981SBrian Somers 				(void)fprintf(stderr,
177f2155981SBrian Somers 				    "nl: invalid delim argument -- %s\n",
178f2155981SBrian Somers 				    optarg);
179f2155981SBrian Somers 				exit(EXIT_FAILURE);
180f2155981SBrian Somers 				/* NOTREACHED */
181f2155981SBrian Somers 			}
182f2155981SBrian Somers 			break;
183f2155981SBrian Somers 		case 'f':
184f2155981SBrian Somers 			parse_numbering(optarg, FOOTER);
185f2155981SBrian Somers 			break;
186f2155981SBrian Somers 		case 'h':
187f2155981SBrian Somers 			parse_numbering(optarg, HEADER);
188f2155981SBrian Somers 			break;
189f2155981SBrian Somers 		case 'i':
190f2155981SBrian Somers 			errno = 0;
191f2155981SBrian Somers 			val = strtol(optarg, &ep, 10);
192f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
193f2155981SBrian Somers 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
194f2155981SBrian Somers 				(void)fprintf(stderr,
195f2155981SBrian Somers 				    "invalid incr argument -- %s\n", optarg);
196f2155981SBrian Somers 				exit(EXIT_FAILURE);
197f2155981SBrian Somers 			}
198f2155981SBrian Somers 			incr = (int)val;
199f2155981SBrian Somers 			break;
200f2155981SBrian Somers 		case 'l':
201f2155981SBrian Somers 			errno = 0;
202f2155981SBrian Somers 			uval = strtoul(optarg, &ep, 10);
203f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
204f2155981SBrian Somers 			    (uval == ULONG_MAX && errno != 0)) {
205f2155981SBrian Somers 				(void)fprintf(stderr,
206f2155981SBrian Somers 				    "invalid num argument -- %s\n", optarg);
207f2155981SBrian Somers 				exit(EXIT_FAILURE);
208f2155981SBrian Somers 			}
209f2155981SBrian Somers 			nblank = (unsigned int)uval;
210f2155981SBrian Somers 			break;
211f2155981SBrian Somers 		case 'n':
212f2155981SBrian Somers 			if (strcmp(optarg, "ln") == 0) {
213f2155981SBrian Somers 				format = FORMAT_LN;
214f2155981SBrian Somers 			} else if (strcmp(optarg, "rn") == 0) {
215f2155981SBrian Somers 				format = FORMAT_RN;
216f2155981SBrian Somers 			} else if (strcmp(optarg, "rz") == 0) {
217f2155981SBrian Somers 				format = FORMAT_RZ;
218f2155981SBrian Somers 			} else {
219f2155981SBrian Somers 				(void)fprintf(stderr,
220f2155981SBrian Somers 				    "nl: illegal format -- %s\n", optarg);
221f2155981SBrian Somers 				exit(EXIT_FAILURE);
222f2155981SBrian Somers 			}
223f2155981SBrian Somers 			break;
224f2155981SBrian Somers 		case 's':
225f2155981SBrian Somers 			sep = optarg;
226f2155981SBrian Somers 			break;
227f2155981SBrian Somers 		case 'v':
228f2155981SBrian Somers 			errno = 0;
229f2155981SBrian Somers 			val = strtol(optarg, &ep, 10);
230f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
231f2155981SBrian Somers 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
232f2155981SBrian Somers 				(void)fprintf(stderr,
233f2155981SBrian Somers 				    "invalid startnum value -- %s\n", optarg);
234f2155981SBrian Somers 				exit(EXIT_FAILURE);
235f2155981SBrian Somers 			}
236f2155981SBrian Somers 			startnum = (int)val;
237f2155981SBrian Somers 			break;
238f2155981SBrian Somers 		case 'w':
239f2155981SBrian Somers 			errno = 0;
240f2155981SBrian Somers 			val = strtol(optarg, &ep, 10);
241f2155981SBrian Somers 			if ((ep != NULL && *ep != '\0') ||
242f2155981SBrian Somers 			 ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
243f2155981SBrian Somers 				(void)fprintf(stderr,
244f2155981SBrian Somers 				    "invalid width value -- %s\n", optarg);
245f2155981SBrian Somers 				exit(EXIT_FAILURE);
246f2155981SBrian Somers 			}
247f2155981SBrian Somers 			width = (int)val;
248f2155981SBrian Somers 			if (!(width > 0)) {
249f2155981SBrian Somers 				(void)fprintf(stderr,
250f2155981SBrian Somers 				    "nl: width argument must be > 0 -- %d\n",
251f2155981SBrian Somers 				    width);
252f2155981SBrian Somers 				 exit(EXIT_FAILURE);
253f2155981SBrian Somers 			}
254f2155981SBrian Somers 			break;
255f2155981SBrian Somers 		case '?':
256f2155981SBrian Somers 		default:
257f2155981SBrian Somers 			usage();
258f2155981SBrian Somers 			/* NOTREACHED */
259f2155981SBrian Somers 		}
260f2155981SBrian Somers 	}
261f2155981SBrian Somers 	argc -= optind;
262f2155981SBrian Somers 	argv += optind;
263f2155981SBrian Somers 
264f2155981SBrian Somers 	switch (argc) {
265f2155981SBrian Somers 	case 0:
266f2155981SBrian Somers 		break;
267f2155981SBrian Somers 	case 1:
268f2155981SBrian Somers 		if (freopen(argv[0], "r", stdin) == NULL) {
269f2155981SBrian Somers 			perror(argv[0]);
270f2155981SBrian Somers 			exit(EXIT_FAILURE);
271f2155981SBrian Somers 		}
272f2155981SBrian Somers 		break;
273f2155981SBrian Somers 	default:
274f2155981SBrian Somers 		usage();
275f2155981SBrian Somers 		/* NOTREACHED */
276f2155981SBrian Somers 	}
277f2155981SBrian Somers 
278f2155981SBrian Somers 	/* Determine the maximum input line length to operate on. */
279f2155981SBrian Somers 	if ((val = sysconf(_SC_LINE_MAX)) == -1) /* ignore errno */
280f2155981SBrian Somers 		val = LINE_MAX;
281f2155981SBrian Somers 	/* Allocate sufficient buffer space (including the terminating NUL). */
282f2155981SBrian Somers 	buffersize = (size_t)val + 1;
283f2155981SBrian Somers 	if ((buffer = malloc(buffersize)) == NULL) {
284f2155981SBrian Somers 		perror("cannot allocate input line buffer");
285f2155981SBrian Somers 		exit(EXIT_FAILURE);
286f2155981SBrian Somers 	}
287f2155981SBrian Somers 
288f2155981SBrian Somers 	/* Allocate a buffer suitable for preformatting line number. */
289f2155981SBrian Somers 	intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1;	/* NUL */
290f2155981SBrian Somers 	if ((intbuffer = malloc(intbuffersize)) == NULL) {
291f2155981SBrian Somers 		perror("cannot allocate preformatting buffer");
292f2155981SBrian Somers 		exit(EXIT_FAILURE);
293f2155981SBrian Somers 	}
294f2155981SBrian Somers 
295f2155981SBrian Somers 	/* Do the work. */
296f2155981SBrian Somers 	filter();
297f2155981SBrian Somers 
298f2155981SBrian Somers 	exit(EXIT_SUCCESS);
299f2155981SBrian Somers 	/* NOTREACHED */
300f2155981SBrian Somers }
301f2155981SBrian Somers 
302f2155981SBrian Somers static void
303f2155981SBrian Somers filter()
304f2155981SBrian Somers {
305f2155981SBrian Somers 	int line;		/* logical line number */
306f2155981SBrian Somers 	int section;		/* logical page section */
307f2155981SBrian Somers 	unsigned int adjblank;	/* adjacent blank lines */
308f2155981SBrian Somers 	int consumed;		/* intbuffer measurement */
309f2155981SBrian Somers 	int donumber, idx;
310f2155981SBrian Somers 
311f2155981SBrian Somers 	adjblank = 0;
312f2155981SBrian Somers 	line = startnum;
313f2155981SBrian Somers 	section = BODY;
314f2155981SBrian Somers #ifdef __GNUC__
315f2155981SBrian Somers 	(void)&donumber;	/* avoid bogus `uninitialized' warning */
316f2155981SBrian Somers #endif
317f2155981SBrian Somers 
318f2155981SBrian Somers 	while (fgets(buffer, (int)buffersize, stdin) != NULL) {
319f2155981SBrian Somers 		for (idx = FOOTER; idx <= NP_LAST; idx++) {
320f2155981SBrian Somers 			/* Does it look like a delimiter? */
321f2155981SBrian Somers 			if (buffer[2 * idx + 0] == delim[0] &&
322f2155981SBrian Somers 			    buffer[2 * idx + 1] == delim[1]) {
323f2155981SBrian Somers 				/* Was this the whole line? */
324f2155981SBrian Somers 				if (buffer[2 * idx + 2] == '\n') {
325f2155981SBrian Somers 					section = idx;
326f2155981SBrian Somers 					adjblank = 0;
327f2155981SBrian Somers 					if (restart)
328f2155981SBrian Somers 						line = startnum;
329f2155981SBrian Somers 					goto nextline;
330f2155981SBrian Somers 				}
331f2155981SBrian Somers 			} else {
332f2155981SBrian Somers 				break;
333f2155981SBrian Somers 			}
334f2155981SBrian Somers 		}
335f2155981SBrian Somers 
336f2155981SBrian Somers 		switch (numbering_properties[section].type) {
337f2155981SBrian Somers 		case number_all:
338f2155981SBrian Somers 			/*
339f2155981SBrian Somers 			 * Doing this for number_all only is disputable, but
340f2155981SBrian Somers 			 * the standard expresses an explicit dependency on
341f2155981SBrian Somers 			 * `-b a' etc.
342f2155981SBrian Somers 			 */
343f2155981SBrian Somers 			if (buffer[0] == '\n' && ++adjblank < nblank)
344f2155981SBrian Somers 				donumber = 0;
345f2155981SBrian Somers 			else
346f2155981SBrian Somers 				donumber = 1, adjblank = 0;
347f2155981SBrian Somers 			break;
348f2155981SBrian Somers 		case number_nonempty:
349f2155981SBrian Somers 			donumber = (buffer[0] != '\n');
350f2155981SBrian Somers 			break;
351f2155981SBrian Somers 		case number_none:
352f2155981SBrian Somers 			donumber = 0;
353f2155981SBrian Somers 			break;
354f2155981SBrian Somers 		case number_regex:
355f2155981SBrian Somers 			donumber =
356f2155981SBrian Somers 			    (regexec(&numbering_properties[section].expr,
357f2155981SBrian Somers 			    buffer, 0, NULL, 0) == 0);
358f2155981SBrian Somers 			break;
359f2155981SBrian Somers 		}
360f2155981SBrian Somers 
361f2155981SBrian Somers 		if (donumber) {
362f2155981SBrian Somers 			/* Note: sprintf() is safe here. */
363f2155981SBrian Somers 			consumed = sprintf(intbuffer, format, width, line);
364f2155981SBrian Somers 			(void)printf("%s",
365f2155981SBrian Somers 			    intbuffer + max(0, consumed - width));
366f2155981SBrian Somers 			line += incr;
367f2155981SBrian Somers 		} else {
368f2155981SBrian Somers 			(void)printf("%*s", width, "");
369f2155981SBrian Somers 		}
370f2155981SBrian Somers 		(void)printf("%s%s", sep, buffer);
371f2155981SBrian Somers 
372f2155981SBrian Somers 		if (ferror(stdout)) {
373f2155981SBrian Somers 			perror("output error");
374f2155981SBrian Somers 			exit(EXIT_FAILURE);
375f2155981SBrian Somers 		}
376f2155981SBrian Somers nextline:
377f2155981SBrian Somers 		;
378f2155981SBrian Somers 	}
379f2155981SBrian Somers 
380f2155981SBrian Somers 	if (ferror(stdin)) {
381f2155981SBrian Somers 		perror("input error");
382f2155981SBrian Somers 		exit(EXIT_FAILURE);
383f2155981SBrian Somers 	}
384f2155981SBrian Somers }
385f2155981SBrian Somers 
386f2155981SBrian Somers /*
387f2155981SBrian Somers  * Various support functions.
388f2155981SBrian Somers  */
389f2155981SBrian Somers 
390f2155981SBrian Somers static void
391f2155981SBrian Somers parse_numbering(argstr, section)
392f2155981SBrian Somers 	const char *argstr;
393f2155981SBrian Somers 	int section;
394f2155981SBrian Somers {
395f2155981SBrian Somers 	int error;
396f2155981SBrian Somers 	char errorbuf[NL_TEXTMAX];
397f2155981SBrian Somers 
398f2155981SBrian Somers 	switch (argstr[0]) {
399f2155981SBrian Somers 	case 'a':
400f2155981SBrian Somers 		numbering_properties[section].type = number_all;
401f2155981SBrian Somers 		break;
402f2155981SBrian Somers 	case 'n':
403f2155981SBrian Somers 		numbering_properties[section].type = number_none;
404f2155981SBrian Somers 		break;
405f2155981SBrian Somers 	case 't':
406f2155981SBrian Somers 		numbering_properties[section].type = number_nonempty;
407f2155981SBrian Somers 		break;
408f2155981SBrian Somers 	case 'p':
409f2155981SBrian Somers 		/* If there was a previous expression, throw it away. */
410f2155981SBrian Somers 		if (numbering_properties[section].type == number_regex)
411f2155981SBrian Somers 			regfree(&numbering_properties[section].expr);
412f2155981SBrian Somers 		else
413f2155981SBrian Somers 			numbering_properties[section].type = number_regex;
414f2155981SBrian Somers 
415f2155981SBrian Somers 		/* Compile/validate the supplied regular expression. */
416f2155981SBrian Somers 		if ((error = regcomp(&numbering_properties[section].expr,
417f2155981SBrian Somers 		    &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
418f2155981SBrian Somers 			(void)regerror(error,
419f2155981SBrian Somers 			    &numbering_properties[section].expr,
420f2155981SBrian Somers 			    errorbuf, sizeof (errorbuf));
421f2155981SBrian Somers 			(void)fprintf(stderr,
422f2155981SBrian Somers 			    "nl: %s expr: %s -- %s\n",
423f2155981SBrian Somers 			    numbering_properties[section].name, errorbuf,
424f2155981SBrian Somers 			    &argstr[1]);
425f2155981SBrian Somers 			exit(EXIT_FAILURE);
426f2155981SBrian Somers 		}
427f2155981SBrian Somers 		break;
428f2155981SBrian Somers 	default:
429f2155981SBrian Somers 		(void)fprintf(stderr,
430f2155981SBrian Somers 		    "nl: illegal %s line numbering type -- %s\n",
431f2155981SBrian Somers 		    numbering_properties[section].name, argstr);
432f2155981SBrian Somers 		exit(EXIT_FAILURE);
433f2155981SBrian Somers 	}
434f2155981SBrian Somers }
435f2155981SBrian Somers 
436f2155981SBrian Somers static void
437f2155981SBrian Somers usage()
438f2155981SBrian Somers {
439f2155981SBrian Somers 
440f2155981SBrian Somers 	(void)fprintf(stderr, "usage: nl [-p] [-b type] [-d delim] [-f type] \
441f2155981SBrian Somers [-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] [-v startnum] [-w width] \
442f2155981SBrian Somers [file]\n");
443f2155981SBrian Somers 	exit(EXIT_FAILURE);
444f2155981SBrian Somers }
445