2011-05-29 16:30:44 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-20 07:21:46 -05:00
|
|
|
#include <limits.h>
|
2015-01-31 19:24:03 -05:00
|
|
|
#include <stdint.h>
|
2015-03-20 13:09:23 -04:00
|
|
|
#include <stdio.h>
|
2011-05-29 16:30:44 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-05-29 16:30:44 -04:00
|
|
|
#include "text.h"
|
2015-03-20 13:09:23 -04:00
|
|
|
#include "utf.h"
|
2011-05-29 16:30:44 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-03-10 07:49:56 -04:00
|
|
|
/* formats here specify line number and separator (not line content) */
|
|
|
|
#define FORMAT_LN "%-*ld%s"
|
|
|
|
#define FORMAT_RN "%*ld%s"
|
|
|
|
#define FORMAT_RZ "%0*ld%s"
|
2015-02-20 09:12:03 -05:00
|
|
|
|
2015-03-18 12:58:09 -04:00
|
|
|
static char type[] = { 'n', 't', 'n' }; /* footer, body, header */
|
2015-03-20 13:09:23 -04:00
|
|
|
static char *delim = "\\:";
|
2015-02-20 09:12:03 -05:00
|
|
|
static const char *format = FORMAT_RN;
|
2011-05-29 16:30:44 -04:00
|
|
|
static const char *sep = "\t";
|
2015-02-20 07:15:43 -05:00
|
|
|
static int width = 6;
|
2015-03-18 12:58:09 -04:00
|
|
|
static int pflag = 0;
|
2015-02-20 07:05:54 -05:00
|
|
|
static size_t startnum = 1;
|
2015-01-31 19:24:03 -05:00
|
|
|
static size_t incr = 1;
|
2015-03-18 12:58:09 -04:00
|
|
|
static size_t blines = 1;
|
2015-03-20 13:09:23 -04:00
|
|
|
static size_t delimlen = 2;
|
2015-03-18 12:58:09 -04:00
|
|
|
static regex_t preg[3];
|
|
|
|
|
|
|
|
static int
|
|
|
|
getsection(char *buf, int *section)
|
|
|
|
{
|
|
|
|
int sectionchanged = 0;
|
|
|
|
int newsection = *section;
|
|
|
|
|
2015-03-20 13:09:23 -04:00
|
|
|
for (; !strncmp(buf, delim, delimlen); buf += delimlen) {
|
2015-03-18 12:58:09 -04:00
|
|
|
if (!sectionchanged) {
|
|
|
|
sectionchanged = 1;
|
|
|
|
newsection = 0;
|
|
|
|
} else {
|
|
|
|
++newsection;
|
|
|
|
newsection %= 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf && buf[0] == '\n')
|
|
|
|
*section = newsection;
|
|
|
|
else
|
|
|
|
sectionchanged = 0;
|
|
|
|
|
|
|
|
return sectionchanged;
|
|
|
|
}
|
2015-01-31 19:24:03 -05:00
|
|
|
|
2015-03-07 07:33:39 -05:00
|
|
|
static void
|
2015-01-31 19:24:03 -05:00
|
|
|
nl(const char *name, FILE *fp)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
2015-03-18 12:58:09 -04:00
|
|
|
int donumber, oldsection, section = 1, bl = 1;
|
|
|
|
size_t number = startnum, size = 0;
|
2015-01-31 19:24:03 -05:00
|
|
|
|
|
|
|
while (getline(&buf, &size, fp) != -1) {
|
2015-03-10 07:49:56 -04:00
|
|
|
donumber = 0;
|
2015-03-18 12:58:09 -04:00
|
|
|
oldsection = section;
|
|
|
|
|
|
|
|
if (getsection(buf, §ion)) {
|
|
|
|
if ((section >= oldsection) && !pflag)
|
|
|
|
number = startnum;
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-10 07:49:56 -04:00
|
|
|
|
2015-03-18 12:58:09 -04:00
|
|
|
if ((type[section] == 't' && buf[0] != '\n')
|
|
|
|
|| (type[section] == 'p' &&
|
|
|
|
!regexec(&preg[section], buf, 0, NULL, 0))) {
|
2015-03-10 07:49:56 -04:00
|
|
|
donumber = 1;
|
2015-03-18 12:58:09 -04:00
|
|
|
} else if (type[section] == 'a') {
|
2015-03-10 07:49:56 -04:00
|
|
|
if (buf[0] == '\n' && bl < blines) {
|
|
|
|
++bl;
|
|
|
|
} else {
|
|
|
|
donumber = 1;
|
|
|
|
bl = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (donumber) {
|
2015-03-18 12:58:09 -04:00
|
|
|
printf(format, width, number, sep);
|
|
|
|
number += incr;
|
2015-02-20 07:05:54 -05:00
|
|
|
} else {
|
2015-03-10 07:49:56 -04:00
|
|
|
printf("%*s", width, "");
|
2015-02-20 07:05:54 -05:00
|
|
|
}
|
2015-03-10 07:49:56 -04:00
|
|
|
printf("%s", buf);
|
2015-01-31 19:24:03 -05:00
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("%s: read error:", name);
|
|
|
|
}
|
2011-05-29 16:30:44 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-18 12:58:09 -04:00
|
|
|
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];
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-05-29 16:30:44 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-03-18 12:58:09 -04:00
|
|
|
char *d;
|
2015-03-20 13:09:23 -04:00
|
|
|
size_t l, s;
|
2011-05-29 16:30:44 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'b':
|
2015-03-18 12:58:09 -04:00
|
|
|
type[1] = getlinetype(EARGF(usage()), &preg[1]);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
d = EARGF(usage());
|
2015-03-20 13:09:23 -04:00
|
|
|
l = utflen(d);
|
|
|
|
|
|
|
|
switch (l) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s = strlen(d);
|
|
|
|
delim = emalloc(s + 2);
|
|
|
|
estrlcpy(delim, d, s + 2);
|
|
|
|
estrlcat(delim, ":", s + 2);
|
|
|
|
delimlen = s + 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
delim = d;
|
|
|
|
delimlen = strlen(delim);
|
|
|
|
break;
|
2015-03-18 12:58:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
type[0] = getlinetype(EARGF(usage()), &preg[0]);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
type[2] = getlinetype(EARGF(usage()), &preg[2]);
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
|
|
|
case 'i':
|
2015-01-31 19:24:03 -05:00
|
|
|
incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
2015-03-10 07:49:56 -04:00
|
|
|
case 'l':
|
|
|
|
blines = estrtonum(EARGF(usage()), 0, UINT_MAX);
|
|
|
|
break;
|
2015-02-20 09:12:03 -05:00
|
|
|
case 'n':
|
|
|
|
format = EARGF(usage());
|
|
|
|
if (!strcmp(format, "ln"))
|
|
|
|
format = FORMAT_LN;
|
|
|
|
else if (!strcmp(format, "rn"))
|
|
|
|
format = FORMAT_RN;
|
|
|
|
else if (!strcmp(format, "rz"))
|
|
|
|
format = FORMAT_RZ;
|
|
|
|
else
|
|
|
|
eprintf("%s: bad format\n", format);
|
|
|
|
break;
|
2015-03-18 12:58:09 -04:00
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
case 's':
|
|
|
|
sep = EARGF(usage());
|
|
|
|
break;
|
2015-02-20 07:05:54 -05:00
|
|
|
case 'v':
|
|
|
|
startnum = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
|
|
|
break;
|
2015-02-20 07:15:43 -05:00
|
|
|
case 'w':
|
|
|
|
width = estrtonum(EARGF(usage()), 1, INT_MAX);
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-12-04 07:04:41 -05:00
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (argc == 0) {
|
2014-12-04 07:00:19 -05:00
|
|
|
nl("<stdin>", stdin);
|
2014-12-04 07:04:41 -05:00
|
|
|
} else {
|
|
|
|
if (!(fp = fopen(argv[0], "r")))
|
|
|
|
eprintf("fopen %s:", argv[0]);
|
2014-12-04 07:00:19 -05:00
|
|
|
nl(argv[0], fp);
|
2011-05-29 16:30:44 -04:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2014-12-04 07:04:41 -05:00
|
|
|
return 0;
|
2011-05-29 16:30:44 -04:00
|
|
|
}
|