2011-05-29 20:30:44 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-20 12:21:46 +00:00
|
|
|
#include <limits.h>
|
2011-05-29 20:30:44 +00:00
|
|
|
#include <stdio.h>
|
2015-02-01 00:24:03 +00:00
|
|
|
#include <stdint.h>
|
2011-05-29 20:30:44 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-05-29 20:30:44 +00:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-20 14:12:03 +00:00
|
|
|
#define FORMAT_LN "%-*ld%s%s"
|
|
|
|
#define FORMAT_RN "%*ld%s%s"
|
|
|
|
#define FORMAT_RZ "%0*ld%s%s"
|
|
|
|
|
2015-02-01 00:24:03 +00:00
|
|
|
static char mode = 't';
|
2015-02-20 14:12:03 +00:00
|
|
|
static const char *format = FORMAT_RN;
|
2011-05-29 20:30:44 +00:00
|
|
|
static const char *sep = "\t";
|
2015-02-20 12:15:43 +00:00
|
|
|
static int width = 6;
|
2015-02-20 12:05:54 +00:00
|
|
|
static size_t startnum = 1;
|
2015-02-01 00:24:03 +00:00
|
|
|
static size_t incr = 1;
|
|
|
|
static regex_t preg;
|
|
|
|
|
2015-03-07 12:33:39 +00:00
|
|
|
static void
|
2015-02-01 00:24:03 +00:00
|
|
|
nl(const char *name, FILE *fp)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
2015-02-20 12:05:54 +00:00
|
|
|
size_t size = 0;
|
2015-02-01 00:24:03 +00:00
|
|
|
|
|
|
|
while (getline(&buf, &size, fp) != -1) {
|
|
|
|
if ((mode == 'a')
|
|
|
|
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
|
2015-02-20 12:05:54 +00:00
|
|
|
|| (mode == 't' && buf[0] != '\n')) {
|
2015-02-20 14:12:03 +00:00
|
|
|
printf(format, width, startnum, sep, buf);
|
2015-02-20 12:05:54 +00:00
|
|
|
startnum += incr;
|
|
|
|
} else {
|
2015-02-01 00:24:03 +00:00
|
|
|
printf(" %s", buf);
|
2015-02-20 12:05:54 +00:00
|
|
|
}
|
2015-02-01 00:24:03 +00:00
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("%s: read error:", name);
|
|
|
|
}
|
2011-05-29 20:30:44 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-20 14:12:03 +00:00
|
|
|
eprintf("usage: %s [-b type] [-i incr] [-n format] [-s sep] [-v startnum] [-w width] [file]\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-05-29 20:30:44 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2013-06-14 18:20:47 +00:00
|
|
|
char *r;
|
2011-05-29 20:30:44 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'b':
|
|
|
|
r = EARGF(usage());
|
|
|
|
mode = r[0];
|
2014-12-04 12:00:19 +00:00
|
|
|
if (r[0] == 'p')
|
2014-11-16 14:17:24 +00:00
|
|
|
eregcomp(&preg, &r[1], REG_NOSUB);
|
2014-12-04 12:00:19 +00:00
|
|
|
else if (!strchr("ant", mode))
|
2013-06-14 18:20:47 +00:00
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'i':
|
2015-02-01 00:24:03 +00:00
|
|
|
incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2015-02-20 14:12:03 +00: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;
|
2013-06-14 18:20:47 +00:00
|
|
|
case 's':
|
|
|
|
sep = EARGF(usage());
|
|
|
|
break;
|
2015-02-20 12:05:54 +00:00
|
|
|
case 'v':
|
|
|
|
startnum = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
|
|
|
break;
|
2015-02-20 12:15:43 +00:00
|
|
|
case 'w':
|
|
|
|
width = estrtonum(EARGF(usage()), 1, INT_MAX);
|
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-12-04 12:04:41 +00:00
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0) {
|
2014-12-04 12:00:19 +00:00
|
|
|
nl("<stdin>", stdin);
|
2014-12-04 12:04:41 +00:00
|
|
|
} else {
|
|
|
|
if (!(fp = fopen(argv[0], "r")))
|
|
|
|
eprintf("fopen %s:", argv[0]);
|
2014-12-04 12:00:19 +00:00
|
|
|
nl(argv[0], fp);
|
2011-05-29 20:30:44 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2014-12-04 12:04:41 +00:00
|
|
|
return 0;
|
2011-05-29 20:30:44 +00:00
|
|
|
}
|