2013-10-08 19:39:08 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-10-08 19:39:08 +00:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: cut -b list [-n] [file...]\n"
|
|
|
|
" cut -c list [file...]\n"
|
|
|
|
" cut -f list [-d delim] [-s] [file...]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct Range {
|
|
|
|
size_t min, max;
|
|
|
|
struct Range *next;
|
|
|
|
} Range;
|
|
|
|
|
|
|
|
static Range *list = NULL;
|
|
|
|
static char mode = 0;
|
|
|
|
static char delim = '\t';
|
|
|
|
static bool nflag = false;
|
|
|
|
static bool sflag = false;
|
|
|
|
|
|
|
|
static void
|
|
|
|
insert(Range *r)
|
|
|
|
{
|
|
|
|
Range *l, *p, *t;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (p = NULL, l = list; l; p = l, l = l->next) {
|
|
|
|
if (r->max && r->max + 1 < l->min) {
|
2013-10-08 19:39:08 +00:00
|
|
|
r->next = l;
|
|
|
|
break;
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (!l->max || r->min < l->max + 2) {
|
2013-10-08 19:39:08 +00:00
|
|
|
l->min = MIN(r->min, l->min);
|
2014-11-13 17:29:30 +00:00
|
|
|
for (p = l, t = l->next; t; p = t, t = t->next)
|
|
|
|
if (r->max && r->max + 1 < t->min)
|
2014-06-01 12:39:34 +00:00
|
|
|
break;
|
2013-10-08 19:39:08 +00:00
|
|
|
l->max = (p->max && r->max) ? MAX(p->max, r->max) : 0;
|
|
|
|
l->next = t;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (p)
|
2014-06-01 12:39:34 +00:00
|
|
|
p->next = r;
|
|
|
|
else
|
|
|
|
list = r;
|
2013-10-08 19:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parselist(char *str)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
size_t n = 1;
|
|
|
|
Range *r;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (s = str; *s; s++) {
|
|
|
|
if (*s == ' ')
|
2014-06-01 12:39:34 +00:00
|
|
|
*s = ',';
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*s == ',')
|
2014-06-01 12:39:34 +00:00
|
|
|
n++;
|
2013-10-08 19:39:08 +00:00
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!(r = malloc(n * sizeof(Range))))
|
2013-10-08 19:39:08 +00:00
|
|
|
eprintf("malloc:");
|
2014-11-13 17:29:30 +00:00
|
|
|
for (s = str; n; n--, s++) {
|
2013-10-10 22:03:15 +00:00
|
|
|
r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
|
|
|
|
r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min;
|
2013-10-08 19:39:08 +00:00
|
|
|
r->next = NULL;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!r->min || (r->max && r->max < r->min) || (*s && *s != ','))
|
2013-10-08 19:39:08 +00:00
|
|
|
eprintf("cut: bad list value\n");
|
|
|
|
insert(r++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 12:39:34 +00:00
|
|
|
static void
|
|
|
|
freelist(void) {
|
|
|
|
Range *l = list, *next;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while (l) {
|
2014-06-01 12:39:34 +00:00
|
|
|
next = l->next;
|
|
|
|
free(l);
|
|
|
|
l->next = NULL;
|
|
|
|
l = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-08 19:39:08 +00:00
|
|
|
static size_t
|
|
|
|
seek(const char *s, size_t pos, size_t *prev, size_t count)
|
|
|
|
{
|
|
|
|
const char *t;
|
|
|
|
size_t n = pos - *prev;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (mode == 'b') {
|
|
|
|
if ((t = memchr(s, 0, n)))
|
2013-10-08 19:39:08 +00:00
|
|
|
return t - s;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (nflag)
|
|
|
|
while (n && !UTF8_POINT(s[n]))
|
2014-06-01 12:39:34 +00:00
|
|
|
n--;
|
2013-10-08 19:39:08 +00:00
|
|
|
*prev += n;
|
|
|
|
return n;
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (mode == 'c') {
|
|
|
|
for (n++, t = s; *t; t++)
|
|
|
|
if (UTF8_POINT(*t) && !--n)
|
2014-06-01 12:39:34 +00:00
|
|
|
break;
|
2013-10-08 19:39:08 +00:00
|
|
|
} else {
|
2014-11-13 17:29:30 +00:00
|
|
|
for (t = (count < 2) ? s : s + 1; n && *t; t++)
|
|
|
|
if (*t == delim && !--n && count)
|
2014-06-01 12:39:34 +00:00
|
|
|
break;
|
2013-10-08 19:39:08 +00:00
|
|
|
}
|
|
|
|
*prev = pos;
|
|
|
|
return t - s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cut(FILE *fp)
|
|
|
|
{
|
2014-06-01 12:39:34 +00:00
|
|
|
char *buf = NULL, *s;
|
|
|
|
size_t size = 0, i, n, p;
|
|
|
|
ssize_t len;
|
2013-10-08 19:39:08 +00:00
|
|
|
Range *r;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((len = agetline(&buf, &size, fp)) != -1) {
|
|
|
|
if (len && buf[len - 1] == '\n')
|
2014-06-01 12:39:34 +00:00
|
|
|
buf[len - 1] = '\0';
|
2014-11-13 17:29:30 +00:00
|
|
|
if (mode == 'f' && !strchr(buf, delim)) {
|
|
|
|
if (!sflag)
|
2013-10-08 19:39:08 +00:00
|
|
|
puts(buf);
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0, p = 1, s = buf, r = list; r; r = r->next, s += n) {
|
2013-10-08 19:39:08 +00:00
|
|
|
s += seek(s, r->min, &p, i++);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!*s)
|
2014-06-01 12:39:34 +00:00
|
|
|
break;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!r->max) {
|
2013-10-08 19:39:08 +00:00
|
|
|
fputs(s, stdout);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n = seek(s, r->max + 1, &p, i++);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (fwrite(s, 1, n, stdout) != n)
|
2013-10-08 19:39:08 +00:00
|
|
|
eprintf("write error:");
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2014-06-01 11:49:07 +00:00
|
|
|
free(buf);
|
2013-10-08 19:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'b':
|
|
|
|
case 'c':
|
|
|
|
case 'f':
|
|
|
|
mode = ARGC();
|
|
|
|
parselist(ARGF());
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
delim = *ARGF();
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!mode)
|
2013-10-08 19:39:08 +00:00
|
|
|
usage();
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!argc)
|
2013-10-08 19:39:08 +00:00
|
|
|
cut(stdin);
|
2014-06-01 12:39:34 +00:00
|
|
|
else {
|
2014-11-13 17:29:30 +00:00
|
|
|
for (; argc--; argv++) {
|
|
|
|
if (strcmp(*argv, "-"))
|
2014-06-01 12:39:34 +00:00
|
|
|
fp = fopen(*argv, "r");
|
|
|
|
else
|
|
|
|
fp = stdin;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!fp) {
|
2014-06-01 12:39:34 +00:00
|
|
|
weprintf("fopen %s:", *argv);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cut(fp);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (fp != stdin)
|
2014-06-01 12:39:34 +00:00
|
|
|
fclose(fp);
|
2013-11-13 11:39:24 +00:00
|
|
|
}
|
2013-10-08 19:39:08 +00:00
|
|
|
}
|
2014-06-01 12:39:34 +00:00
|
|
|
freelist();
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-10-08 19:39:08 +00:00
|
|
|
}
|