2001-05-04 17:25:23 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Klaus Klein.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
|
|
|
__COPYRIGHT(
|
|
|
|
"@(#) Copyright (c) 1999\
|
|
|
|
The NetBSD Foundation, Inc. All rights reserved.");
|
|
|
|
__RCSID("$FreeBSD$");
|
|
|
|
#endif
|
|
|
|
|
2009-02-28 15:53:36 +00:00
|
|
|
#define _WITH_GETLINE
|
2001-05-04 17:25:23 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2002-05-27 06:37:34 +00:00
|
|
|
#include <err.h>
|
2001-05-04 17:25:23 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2004-07-13 02:18:21 +00:00
|
|
|
#include <wchar.h>
|
2001-05-04 17:25:23 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
number_all, /* number all lines */
|
|
|
|
number_nonempty, /* number non-empty lines */
|
|
|
|
number_none, /* no line numbering */
|
|
|
|
number_regex /* number lines matching regular expression */
|
|
|
|
} numbering_type;
|
|
|
|
|
|
|
|
struct numbering_property {
|
|
|
|
const char * const name; /* for diagnostics */
|
|
|
|
numbering_type type; /* numbering type */
|
|
|
|
regex_t expr; /* for type == number_regex */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* line numbering formats */
|
|
|
|
#define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
|
|
|
|
#define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
|
|
|
|
#define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
|
|
|
|
|
|
|
|
#define FOOTER 0
|
|
|
|
#define BODY 1
|
|
|
|
#define HEADER 2
|
|
|
|
#define NP_LAST HEADER
|
|
|
|
|
|
|
|
static struct numbering_property numbering_properties[NP_LAST + 1] = {
|
|
|
|
{ "footer", number_none },
|
|
|
|
{ "body", number_nonempty },
|
|
|
|
{ "header", number_none }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum number of characters required for a decimal representation of a
|
|
|
|
* (signed) int; courtesy of tzcode.
|
|
|
|
*/
|
|
|
|
#define INT_STRLEN_MAXIMUM \
|
|
|
|
((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
|
|
|
|
|
2002-03-22 01:33:25 +00:00
|
|
|
static void filter(void);
|
|
|
|
static void parse_numbering(const char *, int);
|
|
|
|
static void usage(void);
|
2001-05-04 17:25:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dynamically allocated buffer suitable for string representation of ints.
|
|
|
|
*/
|
|
|
|
static char *intbuffer;
|
|
|
|
|
2004-07-13 02:18:21 +00:00
|
|
|
/* delimiter characters that indicate the start of a logical page section */
|
|
|
|
static char delim[2 * MB_LEN_MAX];
|
|
|
|
static int delimlen;
|
|
|
|
|
2001-05-04 17:25:23 +00:00
|
|
|
/*
|
|
|
|
* Configurable parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* line numbering format */
|
|
|
|
static const char *format = FORMAT_RN;
|
|
|
|
|
|
|
|
/* increment value used to number logical page lines */
|
|
|
|
static int incr = 1;
|
|
|
|
|
|
|
|
/* number of adjacent blank lines to be considered (and numbered) as one */
|
|
|
|
static unsigned int nblank = 1;
|
|
|
|
|
|
|
|
/* whether to restart numbering at logical page delimiters */
|
|
|
|
static int restart = 1;
|
|
|
|
|
|
|
|
/* characters used in separating the line number and the corrsp. text line */
|
|
|
|
static const char *sep = "\t";
|
|
|
|
|
|
|
|
/* initial value used to number logical page lines */
|
|
|
|
static int startnum = 1;
|
|
|
|
|
|
|
|
/* number of characters to be used for the line number */
|
|
|
|
/* should be unsigned but required signed by `*' precision conversion */
|
|
|
|
static int width = 6;
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
2005-04-09 14:31:41 +00:00
|
|
|
int c;
|
2001-05-04 17:25:23 +00:00
|
|
|
long val;
|
|
|
|
unsigned long uval;
|
|
|
|
char *ep;
|
2004-07-13 02:18:21 +00:00
|
|
|
size_t intbuffersize, clen;
|
|
|
|
char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
|
|
|
|
size_t delim1len = 1, delim2len = 1;
|
2001-05-04 17:25:23 +00:00
|
|
|
|
|
|
|
(void)setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
restart = 0;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
parse_numbering(optarg, BODY);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2004-07-13 02:18:21 +00:00
|
|
|
clen = mbrlen(optarg, MB_CUR_MAX, NULL);
|
|
|
|
if (clen == (size_t)-1 || clen == (size_t)-2)
|
|
|
|
errc(EXIT_FAILURE, EILSEQ, NULL);
|
|
|
|
if (clen != 0) {
|
|
|
|
memcpy(delim1, optarg, delim1len = clen);
|
|
|
|
clen = mbrlen(optarg + delim1len,
|
|
|
|
MB_CUR_MAX, NULL);
|
|
|
|
if (clen == (size_t)-1 ||
|
|
|
|
clen == (size_t)-2)
|
|
|
|
errc(EXIT_FAILURE, EILSEQ, NULL);
|
|
|
|
if (clen != 0) {
|
|
|
|
memcpy(delim2, optarg + delim1len,
|
|
|
|
delim2len = clen);
|
|
|
|
if (optarg[delim1len + clen] != '\0')
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"invalid delim argument -- %s",
|
|
|
|
optarg);
|
|
|
|
}
|
2001-05-04 17:25:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
parse_numbering(optarg, FOOTER);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
parse_numbering(optarg, HEADER);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(optarg, &ep, 10);
|
|
|
|
if ((ep != NULL && *ep != '\0') ||
|
2002-05-27 06:46:29 +00:00
|
|
|
((val == LONG_MIN || val == LONG_MAX) && errno != 0))
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"invalid incr argument -- %s", optarg);
|
2001-05-04 17:25:23 +00:00
|
|
|
incr = (int)val;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
errno = 0;
|
|
|
|
uval = strtoul(optarg, &ep, 10);
|
|
|
|
if ((ep != NULL && *ep != '\0') ||
|
2002-05-27 06:46:29 +00:00
|
|
|
(uval == ULONG_MAX && errno != 0))
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"invalid num argument -- %s", optarg);
|
2001-05-04 17:25:23 +00:00
|
|
|
nblank = (unsigned int)uval;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
if (strcmp(optarg, "ln") == 0) {
|
|
|
|
format = FORMAT_LN;
|
|
|
|
} else if (strcmp(optarg, "rn") == 0) {
|
|
|
|
format = FORMAT_RN;
|
|
|
|
} else if (strcmp(optarg, "rz") == 0) {
|
|
|
|
format = FORMAT_RZ;
|
2002-05-27 06:46:29 +00:00
|
|
|
} else
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"illegal format -- %s", optarg);
|
2001-05-04 17:25:23 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sep = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(optarg, &ep, 10);
|
|
|
|
if ((ep != NULL && *ep != '\0') ||
|
2002-05-27 06:46:29 +00:00
|
|
|
((val == LONG_MIN || val == LONG_MAX) && errno != 0))
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"invalid startnum value -- %s", optarg);
|
2001-05-04 17:25:23 +00:00
|
|
|
startnum = (int)val;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(optarg, &ep, 10);
|
|
|
|
if ((ep != NULL && *ep != '\0') ||
|
2002-05-27 06:46:29 +00:00
|
|
|
((val == LONG_MIN || val == LONG_MAX) && errno != 0))
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"invalid width value -- %s", optarg);
|
2001-05-04 17:25:23 +00:00
|
|
|
width = (int)val;
|
2002-05-27 06:46:29 +00:00
|
|
|
if (!(width > 0))
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"width argument must be > 0 -- %d",
|
2001-05-04 17:25:23 +00:00
|
|
|
width);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
2002-05-27 06:37:34 +00:00
|
|
|
if (freopen(argv[0], "r", stdin) == NULL)
|
|
|
|
err(EXIT_FAILURE, "%s", argv[0]);
|
2001-05-04 17:25:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2004-07-13 02:18:21 +00:00
|
|
|
/* Generate the delimiter sequence */
|
|
|
|
memcpy(delim, delim1, delim1len);
|
|
|
|
memcpy(delim + delim1len, delim2, delim2len);
|
|
|
|
delimlen = delim1len + delim2len;
|
|
|
|
|
2001-05-04 17:25:23 +00:00
|
|
|
/* Allocate a buffer suitable for preformatting line number. */
|
|
|
|
intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
|
2002-05-27 06:37:34 +00:00
|
|
|
if ((intbuffer = malloc(intbuffersize)) == NULL)
|
|
|
|
err(EXIT_FAILURE, "cannot allocate preformatting buffer");
|
2001-05-04 17:25:23 +00:00
|
|
|
|
|
|
|
/* Do the work. */
|
|
|
|
filter();
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filter()
|
|
|
|
{
|
2009-02-28 15:53:36 +00:00
|
|
|
char *buffer;
|
|
|
|
size_t buffersize;
|
|
|
|
ssize_t linelen;
|
2001-05-04 17:25:23 +00:00
|
|
|
int line; /* logical line number */
|
|
|
|
int section; /* logical page section */
|
|
|
|
unsigned int adjblank; /* adjacent blank lines */
|
|
|
|
int consumed; /* intbuffer measurement */
|
2006-12-22 05:54:19 +00:00
|
|
|
int donumber = 0, idx;
|
2001-05-04 17:25:23 +00:00
|
|
|
|
|
|
|
adjblank = 0;
|
|
|
|
line = startnum;
|
|
|
|
section = BODY;
|
|
|
|
|
2009-02-28 15:53:36 +00:00
|
|
|
buffer = NULL;
|
|
|
|
buffersize = 0;
|
|
|
|
while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
|
2001-05-04 17:25:23 +00:00
|
|
|
for (idx = FOOTER; idx <= NP_LAST; idx++) {
|
|
|
|
/* Does it look like a delimiter? */
|
2009-02-28 15:53:36 +00:00
|
|
|
if (delimlen * (idx + 1) > linelen)
|
|
|
|
break;
|
2004-07-13 02:18:21 +00:00
|
|
|
if (memcmp(buffer + delimlen * idx, delim,
|
2009-02-28 15:53:36 +00:00
|
|
|
delimlen) != 0)
|
2001-05-04 17:25:23 +00:00
|
|
|
break;
|
2009-02-28 15:53:36 +00:00
|
|
|
/* Was this the whole line? */
|
|
|
|
if (buffer[delimlen * (idx + 1)] == '\n') {
|
|
|
|
section = idx;
|
|
|
|
adjblank = 0;
|
|
|
|
if (restart)
|
|
|
|
line = startnum;
|
|
|
|
goto nextline;
|
2001-05-04 17:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (numbering_properties[section].type) {
|
|
|
|
case number_all:
|
|
|
|
/*
|
|
|
|
* Doing this for number_all only is disputable, but
|
|
|
|
* the standard expresses an explicit dependency on
|
|
|
|
* `-b a' etc.
|
|
|
|
*/
|
|
|
|
if (buffer[0] == '\n' && ++adjblank < nblank)
|
|
|
|
donumber = 0;
|
|
|
|
else
|
|
|
|
donumber = 1, adjblank = 0;
|
|
|
|
break;
|
|
|
|
case number_nonempty:
|
|
|
|
donumber = (buffer[0] != '\n');
|
|
|
|
break;
|
|
|
|
case number_none:
|
|
|
|
donumber = 0;
|
|
|
|
break;
|
|
|
|
case number_regex:
|
|
|
|
donumber =
|
|
|
|
(regexec(&numbering_properties[section].expr,
|
|
|
|
buffer, 0, NULL, 0) == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (donumber) {
|
|
|
|
/* Note: sprintf() is safe here. */
|
|
|
|
consumed = sprintf(intbuffer, format, width, line);
|
|
|
|
(void)printf("%s",
|
|
|
|
intbuffer + max(0, consumed - width));
|
|
|
|
line += incr;
|
|
|
|
} else {
|
|
|
|
(void)printf("%*s", width, "");
|
|
|
|
}
|
2009-02-28 15:53:36 +00:00
|
|
|
(void)fputs(sep, stdout);
|
|
|
|
(void)fwrite(buffer, linelen, 1, stdout);
|
2001-05-04 17:25:23 +00:00
|
|
|
|
2002-05-27 06:37:34 +00:00
|
|
|
if (ferror(stdout))
|
|
|
|
err(EXIT_FAILURE, "output error");
|
2001-05-04 17:25:23 +00:00
|
|
|
nextline:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2002-05-27 06:37:34 +00:00
|
|
|
if (ferror(stdin))
|
|
|
|
err(EXIT_FAILURE, "input error");
|
2009-02-28 15:53:36 +00:00
|
|
|
|
|
|
|
free(buffer);
|
2001-05-04 17:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Various support functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_numbering(argstr, section)
|
|
|
|
const char *argstr;
|
|
|
|
int section;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
char errorbuf[NL_TEXTMAX];
|
|
|
|
|
|
|
|
switch (argstr[0]) {
|
|
|
|
case 'a':
|
|
|
|
numbering_properties[section].type = number_all;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
numbering_properties[section].type = number_none;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
numbering_properties[section].type = number_nonempty;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
/* If there was a previous expression, throw it away. */
|
|
|
|
if (numbering_properties[section].type == number_regex)
|
|
|
|
regfree(&numbering_properties[section].expr);
|
|
|
|
else
|
|
|
|
numbering_properties[section].type = number_regex;
|
|
|
|
|
|
|
|
/* Compile/validate the supplied regular expression. */
|
|
|
|
if ((error = regcomp(&numbering_properties[section].expr,
|
|
|
|
&argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
|
|
|
|
(void)regerror(error,
|
|
|
|
&numbering_properties[section].expr,
|
|
|
|
errorbuf, sizeof (errorbuf));
|
2002-05-27 06:46:29 +00:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"%s expr: %s -- %s",
|
2001-05-04 17:25:23 +00:00
|
|
|
numbering_properties[section].name, errorbuf,
|
|
|
|
&argstr[1]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-05-27 06:46:29 +00:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"illegal %s line numbering type -- %s",
|
2001-05-04 17:25:23 +00:00
|
|
|
numbering_properties[section].name, argstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
|
2003-08-17 10:33:54 +00:00
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n"
|
|
|
|
" [-n format] [-s sep] [-v startnum] [-w width] [file]\n");
|
2001-05-04 17:25:23 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|