1f2155981SBrian Somers /*-
2*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
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 *
19f2155981SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f2155981SBrian Somers * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f2155981SBrian Somers * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f2155981SBrian Somers * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f2155981SBrian Somers * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f2155981SBrian Somers * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f2155981SBrian Somers * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f2155981SBrian Somers * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f2155981SBrian Somers * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f2155981SBrian Somers * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f2155981SBrian Somers * POSSIBILITY OF SUCH DAMAGE.
30f2155981SBrian Somers */
31f2155981SBrian Somers
32f2155981SBrian Somers #include <sys/types.h>
33f2155981SBrian Somers
34f1e20ff7STim J. Robbins #include <err.h>
35f2155981SBrian Somers #include <errno.h>
36f2155981SBrian Somers #include <limits.h>
37f2155981SBrian Somers #include <locale.h>
38f2155981SBrian Somers #include <regex.h>
39f2155981SBrian Somers #include <stdio.h>
40f2155981SBrian Somers #include <stdlib.h>
41f2155981SBrian Somers #include <string.h>
42f2155981SBrian Somers #include <unistd.h>
4333ec7f26STim J. Robbins #include <wchar.h>
44f2155981SBrian Somers
45f2155981SBrian Somers typedef enum {
46f2155981SBrian Somers number_all, /* number all lines */
47f2155981SBrian Somers number_nonempty, /* number non-empty lines */
48f2155981SBrian Somers number_none, /* no line numbering */
49f2155981SBrian Somers number_regex /* number lines matching regular expression */
50f2155981SBrian Somers } numbering_type;
51f2155981SBrian Somers
52f2155981SBrian Somers struct numbering_property {
53f2155981SBrian Somers const char * const name; /* for diagnostics */
54f2155981SBrian Somers numbering_type type; /* numbering type */
55f2155981SBrian Somers regex_t expr; /* for type == number_regex */
56f2155981SBrian Somers };
57f2155981SBrian Somers
58f2155981SBrian Somers /* line numbering formats */
59f2155981SBrian Somers #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
60f2155981SBrian Somers #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
61f2155981SBrian Somers #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
62f2155981SBrian Somers
63f2155981SBrian Somers #define FOOTER 0
64f2155981SBrian Somers #define BODY 1
65f2155981SBrian Somers #define HEADER 2
66f2155981SBrian Somers #define NP_LAST HEADER
67f2155981SBrian Somers
68f2155981SBrian Somers static struct numbering_property numbering_properties[NP_LAST + 1] = {
69b7cf00e8SEd Schouten { .name = "footer", .type = number_none },
70b7cf00e8SEd Schouten { .name = "body", .type = number_nonempty },
71b7cf00e8SEd Schouten { .name = "header", .type = number_none }
72f2155981SBrian Somers };
73f2155981SBrian Somers
74f2155981SBrian Somers #define max(a, b) ((a) > (b) ? (a) : (b))
75f2155981SBrian Somers
76f2155981SBrian Somers /*
77f2155981SBrian Somers * Maximum number of characters required for a decimal representation of a
78f2155981SBrian Somers * (signed) int; courtesy of tzcode.
79f2155981SBrian Somers */
80f2155981SBrian Somers #define INT_STRLEN_MAXIMUM \
81f2155981SBrian Somers ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
82f2155981SBrian Somers
83d3cb5dedSWarner Losh static void filter(void);
84d3cb5dedSWarner Losh static void parse_numbering(const char *, int);
85d3cb5dedSWarner Losh static void usage(void);
86f2155981SBrian Somers
87f2155981SBrian Somers /*
88f2155981SBrian Somers * Dynamically allocated buffer suitable for string representation of ints.
89f2155981SBrian Somers */
90f2155981SBrian Somers static char *intbuffer;
91f2155981SBrian Somers
9233ec7f26STim J. Robbins /* delimiter characters that indicate the start of a logical page section */
9333ec7f26STim J. Robbins static char delim[2 * MB_LEN_MAX];
9433ec7f26STim J. Robbins static int delimlen;
9533ec7f26STim J. Robbins
96f2155981SBrian Somers /*
97f2155981SBrian Somers * Configurable parameters.
98f2155981SBrian Somers */
99f2155981SBrian Somers
100f2155981SBrian Somers /* line numbering format */
101f2155981SBrian Somers static const char *format = FORMAT_RN;
102f2155981SBrian Somers
103f2155981SBrian Somers /* increment value used to number logical page lines */
104f2155981SBrian Somers static int incr = 1;
105f2155981SBrian Somers
106f2155981SBrian Somers /* number of adjacent blank lines to be considered (and numbered) as one */
107f2155981SBrian Somers static unsigned int nblank = 1;
108f2155981SBrian Somers
109f2155981SBrian Somers /* whether to restart numbering at logical page delimiters */
110f2155981SBrian Somers static int restart = 1;
111f2155981SBrian Somers
112f2155981SBrian Somers /* characters used in separating the line number and the corrsp. text line */
113f2155981SBrian Somers static const char *sep = "\t";
114f2155981SBrian Somers
115f2155981SBrian Somers /* initial value used to number logical page lines */
116f2155981SBrian Somers static int startnum = 1;
117f2155981SBrian Somers
118f2155981SBrian Somers /* number of characters to be used for the line number */
119f2155981SBrian Somers /* should be unsigned but required signed by `*' precision conversion */
120f2155981SBrian Somers static int width = 6;
121f2155981SBrian Somers
122f2155981SBrian Somers
123f2155981SBrian Somers int
main(int argc,char * argv[])124b7cf00e8SEd Schouten main(int argc, char *argv[])
125f2155981SBrian Somers {
1266c97c3d1SStefan Farfeleder int c;
127f2155981SBrian Somers long val;
128f2155981SBrian Somers unsigned long uval;
129f2155981SBrian Somers char *ep;
13033ec7f26STim J. Robbins size_t intbuffersize, clen;
13133ec7f26STim J. Robbins char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
13233ec7f26STim J. Robbins size_t delim1len = 1, delim2len = 1;
133f2155981SBrian Somers
134f2155981SBrian Somers (void)setlocale(LC_ALL, "");
135f2155981SBrian Somers
136f2155981SBrian Somers while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
137f2155981SBrian Somers switch (c) {
138f2155981SBrian Somers case 'p':
139f2155981SBrian Somers restart = 0;
140f2155981SBrian Somers break;
141f2155981SBrian Somers case 'b':
142f2155981SBrian Somers parse_numbering(optarg, BODY);
143f2155981SBrian Somers break;
144f2155981SBrian Somers case 'd':
14533ec7f26STim J. Robbins clen = mbrlen(optarg, MB_CUR_MAX, NULL);
14633ec7f26STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2)
14733ec7f26STim J. Robbins errc(EXIT_FAILURE, EILSEQ, NULL);
14833ec7f26STim J. Robbins if (clen != 0) {
14933ec7f26STim J. Robbins memcpy(delim1, optarg, delim1len = clen);
15033ec7f26STim J. Robbins clen = mbrlen(optarg + delim1len,
15133ec7f26STim J. Robbins MB_CUR_MAX, NULL);
15233ec7f26STim J. Robbins if (clen == (size_t)-1 ||
15333ec7f26STim J. Robbins clen == (size_t)-2)
15433ec7f26STim J. Robbins errc(EXIT_FAILURE, EILSEQ, NULL);
15533ec7f26STim J. Robbins if (clen != 0) {
15633ec7f26STim J. Robbins memcpy(delim2, optarg + delim1len,
15733ec7f26STim J. Robbins delim2len = clen);
15833ec7f26STim J. Robbins if (optarg[delim1len + clen] != '\0')
15953e29ec5STim J. Robbins errx(EXIT_FAILURE,
16053e29ec5STim J. Robbins "invalid delim argument -- %s",
161f2155981SBrian Somers optarg);
16233ec7f26STim J. Robbins }
163f2155981SBrian Somers }
164f2155981SBrian Somers break;
165f2155981SBrian Somers case 'f':
166f2155981SBrian Somers parse_numbering(optarg, FOOTER);
167f2155981SBrian Somers break;
168f2155981SBrian Somers case 'h':
169f2155981SBrian Somers parse_numbering(optarg, HEADER);
170f2155981SBrian Somers break;
171f2155981SBrian Somers case 'i':
172f2155981SBrian Somers errno = 0;
173f2155981SBrian Somers val = strtol(optarg, &ep, 10);
174f2155981SBrian Somers if ((ep != NULL && *ep != '\0') ||
17553e29ec5STim J. Robbins ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
17653e29ec5STim J. Robbins errx(EXIT_FAILURE,
17753e29ec5STim J. Robbins "invalid incr argument -- %s", optarg);
178f2155981SBrian Somers incr = (int)val;
179f2155981SBrian Somers break;
180f2155981SBrian Somers case 'l':
181f2155981SBrian Somers errno = 0;
182f2155981SBrian Somers uval = strtoul(optarg, &ep, 10);
183f2155981SBrian Somers if ((ep != NULL && *ep != '\0') ||
18453e29ec5STim J. Robbins (uval == ULONG_MAX && errno != 0))
18553e29ec5STim J. Robbins errx(EXIT_FAILURE,
18653e29ec5STim J. Robbins "invalid num argument -- %s", optarg);
187f2155981SBrian Somers nblank = (unsigned int)uval;
188f2155981SBrian Somers break;
189f2155981SBrian Somers case 'n':
190f2155981SBrian Somers if (strcmp(optarg, "ln") == 0) {
191f2155981SBrian Somers format = FORMAT_LN;
192f2155981SBrian Somers } else if (strcmp(optarg, "rn") == 0) {
193f2155981SBrian Somers format = FORMAT_RN;
194f2155981SBrian Somers } else if (strcmp(optarg, "rz") == 0) {
195f2155981SBrian Somers format = FORMAT_RZ;
19653e29ec5STim J. Robbins } else
19753e29ec5STim J. Robbins errx(EXIT_FAILURE,
19853e29ec5STim J. Robbins "illegal format -- %s", optarg);
199f2155981SBrian Somers break;
200f2155981SBrian Somers case 's':
201f2155981SBrian Somers sep = optarg;
202f2155981SBrian Somers break;
203f2155981SBrian Somers case 'v':
204f2155981SBrian Somers errno = 0;
205f2155981SBrian Somers val = strtol(optarg, &ep, 10);
206f2155981SBrian Somers if ((ep != NULL && *ep != '\0') ||
20753e29ec5STim J. Robbins ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
20853e29ec5STim J. Robbins errx(EXIT_FAILURE,
20953e29ec5STim J. Robbins "invalid startnum value -- %s", optarg);
210f2155981SBrian Somers startnum = (int)val;
211f2155981SBrian Somers break;
212f2155981SBrian Somers case 'w':
213f2155981SBrian Somers errno = 0;
214f2155981SBrian Somers val = strtol(optarg, &ep, 10);
215f2155981SBrian Somers if ((ep != NULL && *ep != '\0') ||
21653e29ec5STim J. Robbins ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
21753e29ec5STim J. Robbins errx(EXIT_FAILURE,
21853e29ec5STim J. Robbins "invalid width value -- %s", optarg);
219f2155981SBrian Somers width = (int)val;
22053e29ec5STim J. Robbins if (!(width > 0))
22153e29ec5STim J. Robbins errx(EXIT_FAILURE,
22253e29ec5STim J. Robbins "width argument must be > 0 -- %d",
223f2155981SBrian Somers width);
224f2155981SBrian Somers break;
225f2155981SBrian Somers case '?':
226f2155981SBrian Somers default:
227f2155981SBrian Somers usage();
228f2155981SBrian Somers /* NOTREACHED */
229f2155981SBrian Somers }
230f2155981SBrian Somers }
231f2155981SBrian Somers argc -= optind;
232f2155981SBrian Somers argv += optind;
233f2155981SBrian Somers
234f2155981SBrian Somers switch (argc) {
235f2155981SBrian Somers case 0:
236f2155981SBrian Somers break;
237f2155981SBrian Somers case 1:
238c401df01SSergey Kandaurov if (strcmp(argv[0], "-") != 0 &&
239c401df01SSergey Kandaurov freopen(argv[0], "r", stdin) == NULL)
240f1e20ff7STim J. Robbins err(EXIT_FAILURE, "%s", argv[0]);
241f2155981SBrian Somers break;
242f2155981SBrian Somers default:
243f2155981SBrian Somers usage();
244f2155981SBrian Somers /* NOTREACHED */
245f2155981SBrian Somers }
246f2155981SBrian Somers
24733ec7f26STim J. Robbins /* Generate the delimiter sequence */
24833ec7f26STim J. Robbins memcpy(delim, delim1, delim1len);
24933ec7f26STim J. Robbins memcpy(delim + delim1len, delim2, delim2len);
25033ec7f26STim J. Robbins delimlen = delim1len + delim2len;
25133ec7f26STim J. Robbins
252f2155981SBrian Somers /* Allocate a buffer suitable for preformatting line number. */
253b7cf00e8SEd Schouten intbuffersize = max((int)INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
254f1e20ff7STim J. Robbins if ((intbuffer = malloc(intbuffersize)) == NULL)
255f1e20ff7STim J. Robbins err(EXIT_FAILURE, "cannot allocate preformatting buffer");
256f2155981SBrian Somers
257f2155981SBrian Somers /* Do the work. */
258f2155981SBrian Somers filter();
259f2155981SBrian Somers
260f2155981SBrian Somers exit(EXIT_SUCCESS);
261f2155981SBrian Somers /* NOTREACHED */
262f2155981SBrian Somers }
263f2155981SBrian Somers
264f2155981SBrian Somers static void
filter(void)265b7cf00e8SEd Schouten filter(void)
266f2155981SBrian Somers {
2678e31b96cSDavid Schultz char *buffer;
2688e31b96cSDavid Schultz size_t buffersize;
2698e31b96cSDavid Schultz ssize_t linelen;
270f2155981SBrian Somers int line; /* logical line number */
271f2155981SBrian Somers int section; /* logical page section */
272f2155981SBrian Somers unsigned int adjblank; /* adjacent blank lines */
273f2155981SBrian Somers int consumed; /* intbuffer measurement */
274353bb0a3SWarner Losh int donumber = 0, idx;
275f2155981SBrian Somers
276f2155981SBrian Somers adjblank = 0;
277f2155981SBrian Somers line = startnum;
278f2155981SBrian Somers section = BODY;
279f2155981SBrian Somers
2808e31b96cSDavid Schultz buffer = NULL;
2818e31b96cSDavid Schultz buffersize = 0;
2828e31b96cSDavid Schultz while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
283f2155981SBrian Somers for (idx = FOOTER; idx <= NP_LAST; idx++) {
284f2155981SBrian Somers /* Does it look like a delimiter? */
2858e31b96cSDavid Schultz if (delimlen * (idx + 1) > linelen)
2868e31b96cSDavid Schultz break;
28733ec7f26STim J. Robbins if (memcmp(buffer + delimlen * idx, delim,
2888e31b96cSDavid Schultz delimlen) != 0)
2898e31b96cSDavid Schultz break;
290f2155981SBrian Somers /* Was this the whole line? */
29133ec7f26STim J. Robbins if (buffer[delimlen * (idx + 1)] == '\n') {
292f2155981SBrian Somers section = idx;
293f2155981SBrian Somers adjblank = 0;
294f2155981SBrian Somers if (restart)
295f2155981SBrian Somers line = startnum;
296f2155981SBrian Somers goto nextline;
297f2155981SBrian Somers }
298f2155981SBrian Somers }
299f2155981SBrian Somers
300f2155981SBrian Somers switch (numbering_properties[section].type) {
301f2155981SBrian Somers case number_all:
302f2155981SBrian Somers /*
303f2155981SBrian Somers * Doing this for number_all only is disputable, but
304f2155981SBrian Somers * the standard expresses an explicit dependency on
305f2155981SBrian Somers * `-b a' etc.
306f2155981SBrian Somers */
307f2155981SBrian Somers if (buffer[0] == '\n' && ++adjblank < nblank)
308f2155981SBrian Somers donumber = 0;
309f2155981SBrian Somers else
310f2155981SBrian Somers donumber = 1, adjblank = 0;
311f2155981SBrian Somers break;
312f2155981SBrian Somers case number_nonempty:
313f2155981SBrian Somers donumber = (buffer[0] != '\n');
314f2155981SBrian Somers break;
315f2155981SBrian Somers case number_none:
316f2155981SBrian Somers donumber = 0;
317f2155981SBrian Somers break;
318f2155981SBrian Somers case number_regex:
319f2155981SBrian Somers donumber =
320f2155981SBrian Somers (regexec(&numbering_properties[section].expr,
321f2155981SBrian Somers buffer, 0, NULL, 0) == 0);
322f2155981SBrian Somers break;
323f2155981SBrian Somers }
324f2155981SBrian Somers
325f2155981SBrian Somers if (donumber) {
326f2155981SBrian Somers /* Note: sprintf() is safe here. */
327f2155981SBrian Somers consumed = sprintf(intbuffer, format, width, line);
328f2155981SBrian Somers (void)printf("%s",
329f2155981SBrian Somers intbuffer + max(0, consumed - width));
330f2155981SBrian Somers line += incr;
331f2155981SBrian Somers } else {
332f2155981SBrian Somers (void)printf("%*s", width, "");
333f2155981SBrian Somers }
3348e31b96cSDavid Schultz (void)fputs(sep, stdout);
3358e31b96cSDavid Schultz (void)fwrite(buffer, linelen, 1, stdout);
336f2155981SBrian Somers
337f1e20ff7STim J. Robbins if (ferror(stdout))
338f1e20ff7STim J. Robbins err(EXIT_FAILURE, "output error");
339f2155981SBrian Somers nextline:
340f2155981SBrian Somers ;
341f2155981SBrian Somers }
342f2155981SBrian Somers
343f1e20ff7STim J. Robbins if (ferror(stdin))
344f1e20ff7STim J. Robbins err(EXIT_FAILURE, "input error");
3458e31b96cSDavid Schultz
3468e31b96cSDavid Schultz free(buffer);
347f2155981SBrian Somers }
348f2155981SBrian Somers
349f2155981SBrian Somers /*
350f2155981SBrian Somers * Various support functions.
351f2155981SBrian Somers */
352f2155981SBrian Somers
353f2155981SBrian Somers static void
parse_numbering(const char * argstr,int section)354b7cf00e8SEd Schouten parse_numbering(const char *argstr, int section)
355f2155981SBrian Somers {
356f2155981SBrian Somers int error;
357f2155981SBrian Somers char errorbuf[NL_TEXTMAX];
358f2155981SBrian Somers
359f2155981SBrian Somers switch (argstr[0]) {
360f2155981SBrian Somers case 'a':
361f2155981SBrian Somers numbering_properties[section].type = number_all;
362f2155981SBrian Somers break;
363f2155981SBrian Somers case 'n':
364f2155981SBrian Somers numbering_properties[section].type = number_none;
365f2155981SBrian Somers break;
366f2155981SBrian Somers case 't':
367f2155981SBrian Somers numbering_properties[section].type = number_nonempty;
368f2155981SBrian Somers break;
369f2155981SBrian Somers case 'p':
370f2155981SBrian Somers /* If there was a previous expression, throw it away. */
371f2155981SBrian Somers if (numbering_properties[section].type == number_regex)
372f2155981SBrian Somers regfree(&numbering_properties[section].expr);
373f2155981SBrian Somers else
374f2155981SBrian Somers numbering_properties[section].type = number_regex;
375f2155981SBrian Somers
376f2155981SBrian Somers /* Compile/validate the supplied regular expression. */
377f2155981SBrian Somers if ((error = regcomp(&numbering_properties[section].expr,
378f2155981SBrian Somers &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
379f2155981SBrian Somers (void)regerror(error,
380f2155981SBrian Somers &numbering_properties[section].expr,
381f2155981SBrian Somers errorbuf, sizeof (errorbuf));
38253e29ec5STim J. Robbins errx(EXIT_FAILURE,
38353e29ec5STim J. Robbins "%s expr: %s -- %s",
384f2155981SBrian Somers numbering_properties[section].name, errorbuf,
385f2155981SBrian Somers &argstr[1]);
386f2155981SBrian Somers }
387f2155981SBrian Somers break;
388f2155981SBrian Somers default:
38953e29ec5STim J. Robbins errx(EXIT_FAILURE,
39053e29ec5STim J. Robbins "illegal %s line numbering type -- %s",
391f2155981SBrian Somers numbering_properties[section].name, argstr);
392f2155981SBrian Somers }
393f2155981SBrian Somers }
394f2155981SBrian Somers
395f2155981SBrian Somers static void
usage(void)396b7cf00e8SEd Schouten usage(void)
397f2155981SBrian Somers {
398f2155981SBrian Somers
3997007f3d6STim J. Robbins (void)fprintf(stderr,
4007007f3d6STim J. Robbins "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
4017007f3d6STim J. Robbins " [-n format] [-s sep] [-v startnum] [-w width] [file]\n");
402f2155981SBrian Somers exit(EXIT_FAILURE);
403f2155981SBrian Somers }
404