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 * 17f2155981SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18f2155981SBrian Somers * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19f2155981SBrian Somers * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20f2155981SBrian Somers * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21f2155981SBrian Somers * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22f2155981SBrian Somers * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23f2155981SBrian Somers * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24f2155981SBrian Somers * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25f2155981SBrian Somers * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26f2155981SBrian Somers * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27f2155981SBrian Somers * POSSIBILITY OF SUCH DAMAGE. 28f2155981SBrian Somers */ 29f2155981SBrian Somers 30f2155981SBrian Somers #include <sys/cdefs.h> 31f2155981SBrian Somers #ifndef lint 32f2155981SBrian Somers __COPYRIGHT( 33f2155981SBrian Somers "@(#) Copyright (c) 1999\ 34f2155981SBrian Somers The NetBSD Foundation, Inc. All rights reserved."); 35f2155981SBrian Somers __RCSID("$FreeBSD$"); 36f2155981SBrian Somers #endif 37f2155981SBrian Somers 388e31b96cSDavid Schultz #define _WITH_GETLINE 39f2155981SBrian Somers #include <sys/types.h> 40f2155981SBrian Somers 41f1e20ff7STim J. Robbins #include <err.h> 42f2155981SBrian Somers #include <errno.h> 43f2155981SBrian Somers #include <limits.h> 44f2155981SBrian Somers #include <locale.h> 45f2155981SBrian Somers #include <regex.h> 46f2155981SBrian Somers #include <stdio.h> 47f2155981SBrian Somers #include <stdlib.h> 48f2155981SBrian Somers #include <string.h> 49f2155981SBrian Somers #include <unistd.h> 5033ec7f26STim J. Robbins #include <wchar.h> 51f2155981SBrian Somers 52f2155981SBrian Somers typedef enum { 53f2155981SBrian Somers number_all, /* number all lines */ 54f2155981SBrian Somers number_nonempty, /* number non-empty lines */ 55f2155981SBrian Somers number_none, /* no line numbering */ 56f2155981SBrian Somers number_regex /* number lines matching regular expression */ 57f2155981SBrian Somers } numbering_type; 58f2155981SBrian Somers 59f2155981SBrian Somers struct numbering_property { 60f2155981SBrian Somers const char * const name; /* for diagnostics */ 61f2155981SBrian Somers numbering_type type; /* numbering type */ 62f2155981SBrian Somers regex_t expr; /* for type == number_regex */ 63f2155981SBrian Somers }; 64f2155981SBrian Somers 65f2155981SBrian Somers /* line numbering formats */ 66f2155981SBrian Somers #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */ 67f2155981SBrian Somers #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */ 68f2155981SBrian Somers #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */ 69f2155981SBrian Somers 70f2155981SBrian Somers #define FOOTER 0 71f2155981SBrian Somers #define BODY 1 72f2155981SBrian Somers #define HEADER 2 73f2155981SBrian Somers #define NP_LAST HEADER 74f2155981SBrian Somers 75f2155981SBrian Somers static struct numbering_property numbering_properties[NP_LAST + 1] = { 76b7cf00e8SEd Schouten { .name = "footer", .type = number_none }, 77b7cf00e8SEd Schouten { .name = "body", .type = number_nonempty }, 78b7cf00e8SEd Schouten { .name = "header", .type = number_none } 79f2155981SBrian Somers }; 80f2155981SBrian Somers 81f2155981SBrian Somers #define max(a, b) ((a) > (b) ? (a) : (b)) 82f2155981SBrian Somers 83f2155981SBrian Somers /* 84f2155981SBrian Somers * Maximum number of characters required for a decimal representation of a 85f2155981SBrian Somers * (signed) int; courtesy of tzcode. 86f2155981SBrian Somers */ 87f2155981SBrian Somers #define INT_STRLEN_MAXIMUM \ 88f2155981SBrian Somers ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2) 89f2155981SBrian Somers 90d3cb5dedSWarner Losh static void filter(void); 91d3cb5dedSWarner Losh static void parse_numbering(const char *, int); 92d3cb5dedSWarner Losh static void usage(void); 93f2155981SBrian Somers 94f2155981SBrian Somers /* 95f2155981SBrian Somers * Dynamically allocated buffer suitable for string representation of ints. 96f2155981SBrian Somers */ 97f2155981SBrian Somers static char *intbuffer; 98f2155981SBrian Somers 9933ec7f26STim J. Robbins /* delimiter characters that indicate the start of a logical page section */ 10033ec7f26STim J. Robbins static char delim[2 * MB_LEN_MAX]; 10133ec7f26STim J. Robbins static int delimlen; 10233ec7f26STim J. Robbins 103f2155981SBrian Somers /* 104f2155981SBrian Somers * Configurable parameters. 105f2155981SBrian Somers */ 106f2155981SBrian Somers 107f2155981SBrian Somers /* line numbering format */ 108f2155981SBrian Somers static const char *format = FORMAT_RN; 109f2155981SBrian Somers 110f2155981SBrian Somers /* increment value used to number logical page lines */ 111f2155981SBrian Somers static int incr = 1; 112f2155981SBrian Somers 113f2155981SBrian Somers /* number of adjacent blank lines to be considered (and numbered) as one */ 114f2155981SBrian Somers static unsigned int nblank = 1; 115f2155981SBrian Somers 116f2155981SBrian Somers /* whether to restart numbering at logical page delimiters */ 117f2155981SBrian Somers static int restart = 1; 118f2155981SBrian Somers 119f2155981SBrian Somers /* characters used in separating the line number and the corrsp. text line */ 120f2155981SBrian Somers static const char *sep = "\t"; 121f2155981SBrian Somers 122f2155981SBrian Somers /* initial value used to number logical page lines */ 123f2155981SBrian Somers static int startnum = 1; 124f2155981SBrian Somers 125f2155981SBrian Somers /* number of characters to be used for the line number */ 126f2155981SBrian Somers /* should be unsigned but required signed by `*' precision conversion */ 127f2155981SBrian Somers static int width = 6; 128f2155981SBrian Somers 129f2155981SBrian Somers 130f2155981SBrian Somers int 131b7cf00e8SEd Schouten main(int argc, char *argv[]) 132f2155981SBrian Somers { 1336c97c3d1SStefan Farfeleder int c; 134f2155981SBrian Somers long val; 135f2155981SBrian Somers unsigned long uval; 136f2155981SBrian Somers char *ep; 13733ec7f26STim J. Robbins size_t intbuffersize, clen; 13833ec7f26STim J. Robbins char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' }; 13933ec7f26STim J. Robbins size_t delim1len = 1, delim2len = 1; 140f2155981SBrian Somers 141f2155981SBrian Somers (void)setlocale(LC_ALL, ""); 142f2155981SBrian Somers 143f2155981SBrian Somers while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) { 144f2155981SBrian Somers switch (c) { 145f2155981SBrian Somers case 'p': 146f2155981SBrian Somers restart = 0; 147f2155981SBrian Somers break; 148f2155981SBrian Somers case 'b': 149f2155981SBrian Somers parse_numbering(optarg, BODY); 150f2155981SBrian Somers break; 151f2155981SBrian Somers case 'd': 15233ec7f26STim J. Robbins clen = mbrlen(optarg, MB_CUR_MAX, NULL); 15333ec7f26STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) 15433ec7f26STim J. Robbins errc(EXIT_FAILURE, EILSEQ, NULL); 15533ec7f26STim J. Robbins if (clen != 0) { 15633ec7f26STim J. Robbins memcpy(delim1, optarg, delim1len = clen); 15733ec7f26STim J. Robbins clen = mbrlen(optarg + delim1len, 15833ec7f26STim J. Robbins MB_CUR_MAX, NULL); 15933ec7f26STim J. Robbins if (clen == (size_t)-1 || 16033ec7f26STim J. Robbins clen == (size_t)-2) 16133ec7f26STim J. Robbins errc(EXIT_FAILURE, EILSEQ, NULL); 16233ec7f26STim J. Robbins if (clen != 0) { 16333ec7f26STim J. Robbins memcpy(delim2, optarg + delim1len, 16433ec7f26STim J. Robbins delim2len = clen); 16533ec7f26STim J. Robbins if (optarg[delim1len + clen] != '\0') 16653e29ec5STim J. Robbins errx(EXIT_FAILURE, 16753e29ec5STim J. Robbins "invalid delim argument -- %s", 168f2155981SBrian Somers optarg); 16933ec7f26STim J. Robbins } 170f2155981SBrian Somers } 171f2155981SBrian Somers break; 172f2155981SBrian Somers case 'f': 173f2155981SBrian Somers parse_numbering(optarg, FOOTER); 174f2155981SBrian Somers break; 175f2155981SBrian Somers case 'h': 176f2155981SBrian Somers parse_numbering(optarg, HEADER); 177f2155981SBrian Somers break; 178f2155981SBrian Somers case 'i': 179f2155981SBrian Somers errno = 0; 180f2155981SBrian Somers val = strtol(optarg, &ep, 10); 181f2155981SBrian Somers if ((ep != NULL && *ep != '\0') || 18253e29ec5STim J. Robbins ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) 18353e29ec5STim J. Robbins errx(EXIT_FAILURE, 18453e29ec5STim J. Robbins "invalid incr argument -- %s", optarg); 185f2155981SBrian Somers incr = (int)val; 186f2155981SBrian Somers break; 187f2155981SBrian Somers case 'l': 188f2155981SBrian Somers errno = 0; 189f2155981SBrian Somers uval = strtoul(optarg, &ep, 10); 190f2155981SBrian Somers if ((ep != NULL && *ep != '\0') || 19153e29ec5STim J. Robbins (uval == ULONG_MAX && errno != 0)) 19253e29ec5STim J. Robbins errx(EXIT_FAILURE, 19353e29ec5STim J. Robbins "invalid num argument -- %s", optarg); 194f2155981SBrian Somers nblank = (unsigned int)uval; 195f2155981SBrian Somers break; 196f2155981SBrian Somers case 'n': 197f2155981SBrian Somers if (strcmp(optarg, "ln") == 0) { 198f2155981SBrian Somers format = FORMAT_LN; 199f2155981SBrian Somers } else if (strcmp(optarg, "rn") == 0) { 200f2155981SBrian Somers format = FORMAT_RN; 201f2155981SBrian Somers } else if (strcmp(optarg, "rz") == 0) { 202f2155981SBrian Somers format = FORMAT_RZ; 20353e29ec5STim J. Robbins } else 20453e29ec5STim J. Robbins errx(EXIT_FAILURE, 20553e29ec5STim J. Robbins "illegal format -- %s", optarg); 206f2155981SBrian Somers break; 207f2155981SBrian Somers case 's': 208f2155981SBrian Somers sep = optarg; 209f2155981SBrian Somers break; 210f2155981SBrian Somers case 'v': 211f2155981SBrian Somers errno = 0; 212f2155981SBrian Somers val = strtol(optarg, &ep, 10); 213f2155981SBrian Somers if ((ep != NULL && *ep != '\0') || 21453e29ec5STim J. Robbins ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) 21553e29ec5STim J. Robbins errx(EXIT_FAILURE, 21653e29ec5STim J. Robbins "invalid startnum value -- %s", optarg); 217f2155981SBrian Somers startnum = (int)val; 218f2155981SBrian Somers break; 219f2155981SBrian Somers case 'w': 220f2155981SBrian Somers errno = 0; 221f2155981SBrian Somers val = strtol(optarg, &ep, 10); 222f2155981SBrian Somers if ((ep != NULL && *ep != '\0') || 22353e29ec5STim J. Robbins ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) 22453e29ec5STim J. Robbins errx(EXIT_FAILURE, 22553e29ec5STim J. Robbins "invalid width value -- %s", optarg); 226f2155981SBrian Somers width = (int)val; 22753e29ec5STim J. Robbins if (!(width > 0)) 22853e29ec5STim J. Robbins errx(EXIT_FAILURE, 22953e29ec5STim J. Robbins "width argument must be > 0 -- %d", 230f2155981SBrian Somers width); 231f2155981SBrian Somers break; 232f2155981SBrian Somers case '?': 233f2155981SBrian Somers default: 234f2155981SBrian Somers usage(); 235f2155981SBrian Somers /* NOTREACHED */ 236f2155981SBrian Somers } 237f2155981SBrian Somers } 238f2155981SBrian Somers argc -= optind; 239f2155981SBrian Somers argv += optind; 240f2155981SBrian Somers 241f2155981SBrian Somers switch (argc) { 242f2155981SBrian Somers case 0: 243f2155981SBrian Somers break; 244f2155981SBrian Somers case 1: 245*c401df01SSergey Kandaurov if (strcmp(argv[0], "-") != 0 && 246*c401df01SSergey Kandaurov freopen(argv[0], "r", stdin) == NULL) 247f1e20ff7STim J. Robbins err(EXIT_FAILURE, "%s", argv[0]); 248f2155981SBrian Somers break; 249f2155981SBrian Somers default: 250f2155981SBrian Somers usage(); 251f2155981SBrian Somers /* NOTREACHED */ 252f2155981SBrian Somers } 253f2155981SBrian Somers 25433ec7f26STim J. Robbins /* Generate the delimiter sequence */ 25533ec7f26STim J. Robbins memcpy(delim, delim1, delim1len); 25633ec7f26STim J. Robbins memcpy(delim + delim1len, delim2, delim2len); 25733ec7f26STim J. Robbins delimlen = delim1len + delim2len; 25833ec7f26STim J. Robbins 259f2155981SBrian Somers /* Allocate a buffer suitable for preformatting line number. */ 260b7cf00e8SEd Schouten intbuffersize = max((int)INT_STRLEN_MAXIMUM, width) + 1; /* NUL */ 261f1e20ff7STim J. Robbins if ((intbuffer = malloc(intbuffersize)) == NULL) 262f1e20ff7STim J. Robbins err(EXIT_FAILURE, "cannot allocate preformatting buffer"); 263f2155981SBrian Somers 264f2155981SBrian Somers /* Do the work. */ 265f2155981SBrian Somers filter(); 266f2155981SBrian Somers 267f2155981SBrian Somers exit(EXIT_SUCCESS); 268f2155981SBrian Somers /* NOTREACHED */ 269f2155981SBrian Somers } 270f2155981SBrian Somers 271f2155981SBrian Somers static void 272b7cf00e8SEd Schouten filter(void) 273f2155981SBrian Somers { 2748e31b96cSDavid Schultz char *buffer; 2758e31b96cSDavid Schultz size_t buffersize; 2768e31b96cSDavid Schultz ssize_t linelen; 277f2155981SBrian Somers int line; /* logical line number */ 278f2155981SBrian Somers int section; /* logical page section */ 279f2155981SBrian Somers unsigned int adjblank; /* adjacent blank lines */ 280f2155981SBrian Somers int consumed; /* intbuffer measurement */ 281353bb0a3SWarner Losh int donumber = 0, idx; 282f2155981SBrian Somers 283f2155981SBrian Somers adjblank = 0; 284f2155981SBrian Somers line = startnum; 285f2155981SBrian Somers section = BODY; 286f2155981SBrian Somers 2878e31b96cSDavid Schultz buffer = NULL; 2888e31b96cSDavid Schultz buffersize = 0; 2898e31b96cSDavid Schultz while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) { 290f2155981SBrian Somers for (idx = FOOTER; idx <= NP_LAST; idx++) { 291f2155981SBrian Somers /* Does it look like a delimiter? */ 2928e31b96cSDavid Schultz if (delimlen * (idx + 1) > linelen) 2938e31b96cSDavid Schultz break; 29433ec7f26STim J. Robbins if (memcmp(buffer + delimlen * idx, delim, 2958e31b96cSDavid Schultz delimlen) != 0) 2968e31b96cSDavid Schultz break; 297f2155981SBrian Somers /* Was this the whole line? */ 29833ec7f26STim J. Robbins if (buffer[delimlen * (idx + 1)] == '\n') { 299f2155981SBrian Somers section = idx; 300f2155981SBrian Somers adjblank = 0; 301f2155981SBrian Somers if (restart) 302f2155981SBrian Somers line = startnum; 303f2155981SBrian Somers goto nextline; 304f2155981SBrian Somers } 305f2155981SBrian Somers } 306f2155981SBrian Somers 307f2155981SBrian Somers switch (numbering_properties[section].type) { 308f2155981SBrian Somers case number_all: 309f2155981SBrian Somers /* 310f2155981SBrian Somers * Doing this for number_all only is disputable, but 311f2155981SBrian Somers * the standard expresses an explicit dependency on 312f2155981SBrian Somers * `-b a' etc. 313f2155981SBrian Somers */ 314f2155981SBrian Somers if (buffer[0] == '\n' && ++adjblank < nblank) 315f2155981SBrian Somers donumber = 0; 316f2155981SBrian Somers else 317f2155981SBrian Somers donumber = 1, adjblank = 0; 318f2155981SBrian Somers break; 319f2155981SBrian Somers case number_nonempty: 320f2155981SBrian Somers donumber = (buffer[0] != '\n'); 321f2155981SBrian Somers break; 322f2155981SBrian Somers case number_none: 323f2155981SBrian Somers donumber = 0; 324f2155981SBrian Somers break; 325f2155981SBrian Somers case number_regex: 326f2155981SBrian Somers donumber = 327f2155981SBrian Somers (regexec(&numbering_properties[section].expr, 328f2155981SBrian Somers buffer, 0, NULL, 0) == 0); 329f2155981SBrian Somers break; 330f2155981SBrian Somers } 331f2155981SBrian Somers 332f2155981SBrian Somers if (donumber) { 333f2155981SBrian Somers /* Note: sprintf() is safe here. */ 334f2155981SBrian Somers consumed = sprintf(intbuffer, format, width, line); 335f2155981SBrian Somers (void)printf("%s", 336f2155981SBrian Somers intbuffer + max(0, consumed - width)); 337f2155981SBrian Somers line += incr; 338f2155981SBrian Somers } else { 339f2155981SBrian Somers (void)printf("%*s", width, ""); 340f2155981SBrian Somers } 3418e31b96cSDavid Schultz (void)fputs(sep, stdout); 3428e31b96cSDavid Schultz (void)fwrite(buffer, linelen, 1, stdout); 343f2155981SBrian Somers 344f1e20ff7STim J. Robbins if (ferror(stdout)) 345f1e20ff7STim J. Robbins err(EXIT_FAILURE, "output error"); 346f2155981SBrian Somers nextline: 347f2155981SBrian Somers ; 348f2155981SBrian Somers } 349f2155981SBrian Somers 350f1e20ff7STim J. Robbins if (ferror(stdin)) 351f1e20ff7STim J. Robbins err(EXIT_FAILURE, "input error"); 3528e31b96cSDavid Schultz 3538e31b96cSDavid Schultz free(buffer); 354f2155981SBrian Somers } 355f2155981SBrian Somers 356f2155981SBrian Somers /* 357f2155981SBrian Somers * Various support functions. 358f2155981SBrian Somers */ 359f2155981SBrian Somers 360f2155981SBrian Somers static void 361b7cf00e8SEd Schouten parse_numbering(const char *argstr, int section) 362f2155981SBrian Somers { 363f2155981SBrian Somers int error; 364f2155981SBrian Somers char errorbuf[NL_TEXTMAX]; 365f2155981SBrian Somers 366f2155981SBrian Somers switch (argstr[0]) { 367f2155981SBrian Somers case 'a': 368f2155981SBrian Somers numbering_properties[section].type = number_all; 369f2155981SBrian Somers break; 370f2155981SBrian Somers case 'n': 371f2155981SBrian Somers numbering_properties[section].type = number_none; 372f2155981SBrian Somers break; 373f2155981SBrian Somers case 't': 374f2155981SBrian Somers numbering_properties[section].type = number_nonempty; 375f2155981SBrian Somers break; 376f2155981SBrian Somers case 'p': 377f2155981SBrian Somers /* If there was a previous expression, throw it away. */ 378f2155981SBrian Somers if (numbering_properties[section].type == number_regex) 379f2155981SBrian Somers regfree(&numbering_properties[section].expr); 380f2155981SBrian Somers else 381f2155981SBrian Somers numbering_properties[section].type = number_regex; 382f2155981SBrian Somers 383f2155981SBrian Somers /* Compile/validate the supplied regular expression. */ 384f2155981SBrian Somers if ((error = regcomp(&numbering_properties[section].expr, 385f2155981SBrian Somers &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) { 386f2155981SBrian Somers (void)regerror(error, 387f2155981SBrian Somers &numbering_properties[section].expr, 388f2155981SBrian Somers errorbuf, sizeof (errorbuf)); 38953e29ec5STim J. Robbins errx(EXIT_FAILURE, 39053e29ec5STim J. Robbins "%s expr: %s -- %s", 391f2155981SBrian Somers numbering_properties[section].name, errorbuf, 392f2155981SBrian Somers &argstr[1]); 393f2155981SBrian Somers } 394f2155981SBrian Somers break; 395f2155981SBrian Somers default: 39653e29ec5STim J. Robbins errx(EXIT_FAILURE, 39753e29ec5STim J. Robbins "illegal %s line numbering type -- %s", 398f2155981SBrian Somers numbering_properties[section].name, argstr); 399f2155981SBrian Somers } 400f2155981SBrian Somers } 401f2155981SBrian Somers 402f2155981SBrian Somers static void 403b7cf00e8SEd Schouten usage(void) 404f2155981SBrian Somers { 405f2155981SBrian Somers 4067007f3d6STim J. Robbins (void)fprintf(stderr, 4077007f3d6STim J. Robbins "usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n" 4087007f3d6STim J. Robbins " [-n format] [-s sep] [-v startnum] [-w width] [file]\n"); 409f2155981SBrian Somers exit(EXIT_FAILURE); 410f2155981SBrian Somers } 411