sbase/nl.c

213 lines
4.3 KiB
C
Raw Normal View History

2011-05-29 20:30:44 +00:00
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
2011-05-29 20:30:44 +00:00
#include <stdlib.h>
#include <string.h>
2011-05-29 20:30:44 +00:00
#include "text.h"
#include "utf.h"
2011-05-29 20:30:44 +00:00
#include "util.h"
static size_t startnum = 1;
static size_t incr = 1;
static size_t blines = 1;
static size_t delimlen = 2;
static size_t seplen = 1;
static int width = 6;
static int pflag = 0;
static char type[] = { 'n', 't', 'n' }; /* footer, body, header */
static char *delim = "\\:";
static char format[6] = "%*ld";
static char *sep = "\t";
static regex_t preg[3];
2015-03-18 16:58:09 +00:00
static int
getsection(struct line *l, int *section)
2015-03-18 16:58:09 +00:00
{
size_t i;
int sectionchanged = 0, newsection = *section;
2015-03-18 16:58:09 +00:00
for (i = 0; (l->len - i) >= delimlen &&
!memcmp(l->data + i, delim, delimlen); i += delimlen) {
2015-03-18 16:58:09 +00:00
if (!sectionchanged) {
sectionchanged = 1;
newsection = 0;
} else {
newsection = (newsection + 1) % 3;
2015-03-18 16:58:09 +00:00
}
}
if (!(l->len - i) || l->data[i] == '\n')
2015-03-18 16:58:09 +00:00
*section = newsection;
else
sectionchanged = 0;
return sectionchanged;
}
2015-03-07 12:33:39 +00:00
static void
nl(const char *fname, FILE *fp)
{
static struct line line;
static size_t size;
size_t number = startnum, bl = 1;
ssize_t len;
int donumber, oldsection, section = 1;
while ((len = getline(&line.data, &size, fp)) > 0) {
line.len = len;
2015-03-10 11:49:56 +00:00
donumber = 0;
2015-03-18 16:58:09 +00:00
oldsection = section;
if (getsection(&line, &section)) {
2015-03-18 16:58:09 +00:00
if ((section >= oldsection) && !pflag)
number = startnum;
continue;
}
2015-03-10 11:49:56 +00:00
switch (type[section]) {
case 't':
if (line.data[0] != '\n')
donumber = 1;
break;
case 'p':
if (!regexec(preg + section, line.data, 0, NULL, 0))
donumber = 1;
break;
case 'a':
if (line.data[0] == '\n' && bl < blines) {
2015-03-10 11:49:56 +00:00
++bl;
} else {
donumber = 1;
bl = 1;
}
}
if (donumber) {
printf(format, width, number);
fwrite(sep, 1, seplen, stdout);
2015-03-18 16:58:09 +00:00
number += incr;
2015-02-20 12:05:54 +00:00
}
fwrite(line.data, 1, line.len, stdout);
}
free(line.data);
if (ferror(fp))
eprintf("getline %s:", fname);
}
2011-05-29 20:30:44 +00:00
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
eprintf("usage: %s [-p] [-b type] [-d delim] [-f type]\n"
" [-h type] [-i num] [-l num] [-n format]\n"
" [-s sep] [-v num] [-w num] [file]\n", argv0);
2015-03-18 16:58:09 +00:00
}
2015-03-18 16:58:09 +00:00
static char
getlinetype(char *type, regex_t *preg)
{
if (type[0] == 'p')
eregcomp(preg, type + 1, REG_NOSUB);
else if (!type[0] || !strchr("ant", type[0]))
2015-03-18 16:58:09 +00:00
usage();
return type[0];
2013-06-14 18:20:47 +00:00
}
2011-05-29 20:30:44 +00:00
int
main(int argc, char *argv[])
{
FILE *fp = NULL;
size_t s;
int ret = 0;
char *d, *formattype, *formatblit;
2011-05-29 20:30:44 +00:00
2013-06-14 18:20:47 +00:00
ARGBEGIN {
2015-03-18 16:58:09 +00:00
case 'd':
switch (utflen((d = EARGF(usage())))) {
case 0:
eprintf("empty logical page delimiter\n");
case 1:
s = strlen(d);
delim = emalloc(s + 1 + 1);
estrlcpy(delim, d, s + 1 + 1);
estrlcat(delim, ":", s + 1 + 1);
delimlen = s + 1;
break;
default:
delim = d;
delimlen = strlen(delim);
break;
2015-03-18 16:58:09 +00:00
}
break;
case 'f':
type[0] = getlinetype(EARGF(usage()), preg);
break;
case 'b':
type[1] = getlinetype(EARGF(usage()), preg + 1);
2015-03-18 16:58:09 +00:00
break;
case 'h':
type[2] = getlinetype(EARGF(usage()), preg + 2);
2013-06-14 18:20:47 +00:00
break;
case 'i':
incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
2013-06-14 18:20:47 +00:00
break;
2015-03-10 11:49:56 +00:00
case 'l':
blines = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
2015-03-10 11:49:56 +00:00
break;
2015-02-20 14:12:03 +00:00
case 'n':
formattype = EARGF(usage());
estrlcpy(format, "%", sizeof(format));
if (!strcmp(formattype, "ln")) {
formatblit = "-";
} else if (!strcmp(formattype, "rn")) {
formatblit = "";
} else if (!strcmp(formattype, "rz")) {
formatblit = "0";
} else {
eprintf("%s: bad format\n", formattype);
}
estrlcat(format, formatblit, sizeof(format));
estrlcat(format, "*ld", sizeof(format));
2015-02-20 14:12:03 +00:00
break;
2015-03-18 16:58:09 +00:00
case 'p':
pflag = 1;
break;
2013-06-14 18:20:47 +00:00
case 's':
sep = EARGF(usage());
seplen = unescape(sep);
2013-06-14 18:20:47 +00:00
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
2013-06-14 18:20:47 +00:00
2014-12-04 12:04:41 +00:00
if (argc > 1)
usage();
if (!argc) {
nl("<stdin>", stdin);
2014-12-04 12:04:41 +00:00
} else {
if (!strcmp(argv[0], "-")) {
argv[0] = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(argv[0], "r"))) {
2014-12-04 12:04:41 +00:00
eprintf("fopen %s:", argv[0]);
}
nl(argv[0], fp);
2011-05-29 20:30:44 +00:00
}
ret |= fp && fp != stdin && fshut(fp, argv[0]);
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
2011-05-29 20:30:44 +00:00
}