nl: add -d -p -f -h options
This commit is contained in:
parent
3111908b03
commit
787d99d896
1
LICENSE
1
LICENSE
@ -56,3 +56,4 @@ Authors/contributors include:
|
|||||||
© 2014 Adria Garriga <rhaps0dy@installgentoo.com>
|
© 2014 Adria Garriga <rhaps0dy@installgentoo.com>
|
||||||
© 2014 Greg Reagle <greg.reagle@umbc.edu>
|
© 2014 Greg Reagle <greg.reagle@umbc.edu>
|
||||||
© 2015 Tai Chi Minh Ralph Eastwood <tcmreastwood@gmail.com>
|
© 2015 Tai Chi Minh Ralph Eastwood <tcmreastwood@gmail.com>
|
||||||
|
© 2015 Quentin Rameau <quinq@quinq.eu.org>
|
||||||
|
2
README
2
README
@ -49,7 +49,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
|
|||||||
=*| mktemp non-posix none
|
=*| mktemp non-posix none
|
||||||
=*| mv yes none (-i)
|
=*| mv yes none (-i)
|
||||||
=*| nice yes none
|
=*| nice yes none
|
||||||
= nl no -d, -f, -h, -p
|
=* nl yes none
|
||||||
=*| nohup yes none
|
=*| nohup yes none
|
||||||
#*| paste yes none
|
#*| paste yes none
|
||||||
=*| printenv non-posix none
|
=*| printenv non-posix none
|
||||||
|
26
nl.1
26
nl.1
@ -1,4 +1,4 @@
|
|||||||
.Dd February 20, 2015
|
.Dd March 18, 2015
|
||||||
.Dt NL 1
|
.Dt NL 1
|
||||||
.Os sbase
|
.Os sbase
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -6,7 +6,11 @@
|
|||||||
.Nd line numbering filter
|
.Nd line numbering filter
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
|
.Op Fl p
|
||||||
.Op Fl b Ar type
|
.Op Fl b Ar type
|
||||||
|
.Op Fl d Ar delim
|
||||||
|
.Op Fl f Ar type
|
||||||
|
.Op Fl h Ar type
|
||||||
.Op Fl i Ar incr
|
.Op Fl i Ar incr
|
||||||
.Op Fl l Ar num
|
.Op Fl l Ar num
|
||||||
.Op Fl n Ar format
|
.Op Fl n Ar format
|
||||||
@ -25,8 +29,10 @@ is given
|
|||||||
reads from stdin.
|
reads from stdin.
|
||||||
.Sh OPTIONS
|
.Sh OPTIONS
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
|
.It Fl p
|
||||||
|
Do not reset number for logical pages
|
||||||
.It Fl b Ar type
|
.It Fl b Ar type
|
||||||
Defines which lines will be numbered:
|
Defines which lines will be numbered for body sections:
|
||||||
.Bl -tag -width pstringXX
|
.Bl -tag -width pstringXX
|
||||||
.It a
|
.It a
|
||||||
All lines.
|
All lines.
|
||||||
@ -40,6 +46,16 @@ Only lines which match
|
|||||||
a regular expression as defined in
|
a regular expression as defined in
|
||||||
.Xr regex 7 .
|
.Xr regex 7 .
|
||||||
.El
|
.El
|
||||||
|
.It Fl d Ar delim
|
||||||
|
Specify the two characters delimiter (default is "\\:"). If only one character is specified, the second remains ':'.
|
||||||
|
.It Fl f Ar type
|
||||||
|
Same as
|
||||||
|
.Fl b
|
||||||
|
except for footer sections.
|
||||||
|
.It Fl h Ar type
|
||||||
|
Same as
|
||||||
|
.Fl b
|
||||||
|
except for header sections.
|
||||||
.It Fl i Ar incr
|
.It Fl i Ar incr
|
||||||
Defines the increment between numbered lines.
|
Defines the increment between numbered lines.
|
||||||
.It Fl l Ar num
|
.It Fl l Ar num
|
||||||
@ -76,3 +92,9 @@ The default is 6.
|
|||||||
.El
|
.El
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr pr 1
|
.Xr pr 1
|
||||||
|
.Sh STANDARDS
|
||||||
|
The
|
||||||
|
.Nm
|
||||||
|
utility is compliant with the
|
||||||
|
.St -p1003.1-2008
|
||||||
|
specification.
|
||||||
|
95
nl.c
95
nl.c
@ -13,29 +13,64 @@
|
|||||||
#define FORMAT_RN "%*ld%s"
|
#define FORMAT_RN "%*ld%s"
|
||||||
#define FORMAT_RZ "%0*ld%s"
|
#define FORMAT_RZ "%0*ld%s"
|
||||||
|
|
||||||
static char mode = 't';
|
static char type[] = { 'n', 't', 'n' }; /* footer, body, header */
|
||||||
static int blines = 1;
|
static char delim[] = { '\\', ':' };
|
||||||
static const char *format = FORMAT_RN;
|
static const char *format = FORMAT_RN;
|
||||||
static const char *sep = "\t";
|
static const char *sep = "\t";
|
||||||
static int width = 6;
|
static int width = 6;
|
||||||
|
static int pflag = 0;
|
||||||
static size_t startnum = 1;
|
static size_t startnum = 1;
|
||||||
static size_t incr = 1;
|
static size_t incr = 1;
|
||||||
static regex_t preg;
|
static size_t blines = 1;
|
||||||
|
static regex_t preg[3];
|
||||||
|
|
||||||
|
static int
|
||||||
|
getsection(char *buf, int *section)
|
||||||
|
{
|
||||||
|
int sectionchanged = 0;
|
||||||
|
int newsection = *section;
|
||||||
|
|
||||||
|
while (!strncmp(buf, delim, 2)) {
|
||||||
|
if (!sectionchanged) {
|
||||||
|
sectionchanged = 1;
|
||||||
|
newsection = 0;
|
||||||
|
} else {
|
||||||
|
++newsection;
|
||||||
|
newsection %= 3;
|
||||||
|
}
|
||||||
|
buf += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buf && buf[0] == '\n')
|
||||||
|
*section = newsection;
|
||||||
|
else
|
||||||
|
sectionchanged = 0;
|
||||||
|
|
||||||
|
return sectionchanged;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nl(const char *name, FILE *fp)
|
nl(const char *name, FILE *fp)
|
||||||
{
|
{
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
int donumber, bl = 1;
|
int donumber, oldsection, section = 1, bl = 1;
|
||||||
size_t size = 0;
|
size_t number = startnum, size = 0;
|
||||||
|
|
||||||
while (getline(&buf, &size, fp) != -1) {
|
while (getline(&buf, &size, fp) != -1) {
|
||||||
donumber = 0;
|
donumber = 0;
|
||||||
|
oldsection = section;
|
||||||
|
|
||||||
if ((mode == 't' && buf[0] != '\n')
|
if (getsection(buf, §ion)) {
|
||||||
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))) {
|
if ((section >= oldsection) && !pflag)
|
||||||
|
number = startnum;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((type[section] == 't' && buf[0] != '\n')
|
||||||
|
|| (type[section] == 'p' &&
|
||||||
|
!regexec(&preg[section], buf, 0, NULL, 0))) {
|
||||||
donumber = 1;
|
donumber = 1;
|
||||||
} else if (mode == 'a') {
|
} else if (type[section] == 'a') {
|
||||||
if (buf[0] == '\n' && bl < blines) {
|
if (buf[0] == '\n' && bl < blines) {
|
||||||
++bl;
|
++bl;
|
||||||
} else {
|
} else {
|
||||||
@ -45,8 +80,8 @@ nl(const char *name, FILE *fp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (donumber) {
|
if (donumber) {
|
||||||
printf(format, width, startnum, sep);
|
printf(format, width, number, sep);
|
||||||
startnum += incr;
|
number += incr;
|
||||||
} else {
|
} else {
|
||||||
printf("%*s", width, "");
|
printf("%*s", width, "");
|
||||||
}
|
}
|
||||||
@ -60,23 +95,46 @@ nl(const char *name, FILE *fp)
|
|||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-b type] [-i incr] [-l num] [-n format] [-s sep] [-v startnum] [-w width] [file]\n", argv0);
|
eprintf("usage: %s [-p] [-b type] [-d delim] [-f type] "
|
||||||
|
"[-h type] [-i incr] [-l num]\n[-n format] [-s sep] "
|
||||||
|
"[-v startnum] [-w width] [file]\n", argv0);
|
||||||
|
}
|
||||||
|
static char
|
||||||
|
getlinetype(char *type, regex_t *preg)
|
||||||
|
{
|
||||||
|
if (type[0] == 'p')
|
||||||
|
eregcomp(preg, &type[1], REG_NOSUB);
|
||||||
|
else if (!strchr("ant", type[0]))
|
||||||
|
usage();
|
||||||
|
|
||||||
|
return type[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
char *r;
|
char *d;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'b':
|
case 'b':
|
||||||
r = EARGF(usage());
|
type[1] = getlinetype(EARGF(usage()), &preg[1]);
|
||||||
mode = r[0];
|
break;
|
||||||
if (r[0] == 'p')
|
case 'd':
|
||||||
eregcomp(&preg, &r[1], REG_NOSUB);
|
d = EARGF(usage());
|
||||||
else if (!strchr("ant", mode))
|
if (strlen(d) > 2) {
|
||||||
usage();
|
usage();
|
||||||
|
} else if (d[0] != '\0') {
|
||||||
|
delim[0] = d[0];
|
||||||
|
if (d[1])
|
||||||
|
delim[1] = d[1];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
type[0] = getlinetype(EARGF(usage()), &preg[0]);
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
type[2] = getlinetype(EARGF(usage()), &preg[2]);
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
||||||
@ -95,6 +153,9 @@ main(int argc, char *argv[])
|
|||||||
else
|
else
|
||||||
eprintf("%s: bad format\n", format);
|
eprintf("%s: bad format\n", format);
|
||||||
break;
|
break;
|
||||||
|
case 'p':
|
||||||
|
pflag = 1;
|
||||||
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
sep = EARGF(usage());
|
sep = EARGF(usage());
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user