2011-06-02 12:03:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-04-12 15:53:10 +00:00
|
|
|
#include <ctype.h>
|
2011-06-02 12:03:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-06-02 12:03:34 +00:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
struct keydef {
|
2014-05-06 11:35:06 +00:00
|
|
|
int start_column;
|
|
|
|
int end_column;
|
|
|
|
int start_char;
|
|
|
|
int end_char;
|
2014-05-06 14:07:05 +00:00
|
|
|
int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MOD_N = 1 << 1,
|
|
|
|
MOD_STARTB = 1 << 2,
|
|
|
|
MOD_ENDB = 1 << 3,
|
2014-11-13 17:29:30 +00:00
|
|
|
MOD_R = 1 << 4,
|
2014-04-12 15:53:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct kdlist {
|
|
|
|
struct keydef keydef;
|
|
|
|
struct kdlist *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kdlist *head = NULL;
|
2014-05-06 16:47:02 +00:00
|
|
|
static struct kdlist *tail = NULL;
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-05-06 14:07:05 +00:00
|
|
|
static void addkeydef(char *, int);
|
2014-11-23 19:35:56 +00:00
|
|
|
static void check(FILE *);
|
2011-06-02 12:03:34 +00:00
|
|
|
static int linecmp(const char **, const char **);
|
2014-05-15 18:08:17 +00:00
|
|
|
static char *skipblank(char *);
|
2014-05-06 14:07:05 +00:00
|
|
|
static int parse_flags(char **, int *, int);
|
|
|
|
static int parse_keydef(struct keydef *, char *, int);
|
2014-05-15 18:08:17 +00:00
|
|
|
static char *nextcol(char *);
|
2014-04-12 15:53:10 +00:00
|
|
|
static char *columns(char *, const struct keydef *);
|
2011-06-02 12:03:34 +00:00
|
|
|
|
2014-11-23 19:35:56 +00:00
|
|
|
static int Cflag = 0, cflag = 0, uflag = 0;
|
2014-05-15 18:08:17 +00:00
|
|
|
static char *fieldsep = NULL;
|
2012-05-21 20:09:44 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-11-23 19:35:56 +00:00
|
|
|
enprintf(2, "usage: %s [-Cbcnru] [-t delim] [-k def]... [file...]\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-06-02 12:03:34 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
FILE *fp;
|
2014-05-03 16:28:20 +00:00
|
|
|
struct linebuf linebuf = EMPTY_LINEBUF;
|
2014-05-06 14:07:05 +00:00
|
|
|
int global_flags = 0;
|
2011-06-02 12:03:34 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
2014-11-23 19:35:56 +00:00
|
|
|
case 'C':
|
|
|
|
Cflag = 1;
|
|
|
|
break;
|
2014-05-15 18:08:17 +00:00
|
|
|
case 'b':
|
|
|
|
global_flags |= MOD_STARTB | MOD_ENDB;
|
|
|
|
break;
|
2014-11-23 19:35:56 +00:00
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
2014-05-15 18:08:17 +00:00
|
|
|
case 'k':
|
|
|
|
addkeydef(EARGF(usage()), global_flags);
|
|
|
|
break;
|
2013-12-12 13:08:49 +00:00
|
|
|
case 'n':
|
2014-05-06 14:07:05 +00:00
|
|
|
global_flags |= MOD_N;
|
2013-12-12 13:08:49 +00:00
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
case 'r':
|
2014-05-06 14:07:05 +00:00
|
|
|
global_flags |= MOD_R;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2014-05-15 18:08:17 +00:00
|
|
|
case 't':
|
|
|
|
fieldsep = EARGF(usage());
|
2014-11-13 17:29:30 +00:00
|
|
|
if (strlen(fieldsep) != 1)
|
2014-05-15 18:08:17 +00:00
|
|
|
usage();
|
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
case 'u':
|
2014-11-13 20:24:47 +00:00
|
|
|
uflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!head && global_flags)
|
2014-05-06 14:07:05 +00:00
|
|
|
addkeydef("1", global_flags);
|
|
|
|
addkeydef("1", global_flags & MOD_R);
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0) {
|
2014-11-23 19:35:56 +00:00
|
|
|
if (Cflag || cflag) {
|
|
|
|
check(stdin);
|
|
|
|
} else {
|
|
|
|
getlines(stdin, &linebuf);
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
} else for (; argc > 0; argc--, argv++) {
|
|
|
|
if (!(fp = fopen(argv[0], "r"))) {
|
2014-04-12 15:53:10 +00:00
|
|
|
enprintf(2, "fopen %s:", argv[0]);
|
2013-11-13 11:39:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-23 19:35:56 +00:00
|
|
|
if (Cflag || cflag) {
|
|
|
|
check(fp);
|
|
|
|
} else {
|
|
|
|
getlines(fp, &linebuf);
|
|
|
|
}
|
2011-06-02 12:03:34 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2014-11-23 19:35:56 +00:00
|
|
|
if (!Cflag && !cflag) {
|
|
|
|
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines,
|
|
|
|
(int (*)(const void *, const void *))linecmp);
|
|
|
|
|
|
|
|
for (i = 0; i < linebuf.nlines; i++) {
|
|
|
|
if (!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i],
|
|
|
|
(const char **)&linebuf.lines[i-1])) {
|
|
|
|
fputs(linebuf.lines[i], stdout);
|
|
|
|
}
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2011-06-02 12:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
static void
|
2014-05-06 14:07:05 +00:00
|
|
|
addkeydef(char *def, int flags)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
|
|
|
struct kdlist *node;
|
|
|
|
|
|
|
|
node = malloc(sizeof(*node));
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!node)
|
2014-04-12 15:53:10 +00:00
|
|
|
enprintf(2, "malloc:");
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!head)
|
2014-04-12 15:53:10 +00:00
|
|
|
head = node;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (parse_keydef(&node->keydef, def, flags))
|
2014-05-03 17:06:20 +00:00
|
|
|
enprintf(2, "faulty key definition\n");
|
2014-11-13 17:29:30 +00:00
|
|
|
if (tail)
|
2014-05-06 16:47:02 +00:00
|
|
|
tail->next = node;
|
2014-04-12 15:53:10 +00:00
|
|
|
node->next = NULL;
|
2014-05-06 16:47:02 +00:00
|
|
|
tail = node;
|
2014-04-12 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:35:56 +00:00
|
|
|
static void
|
|
|
|
check(FILE *fp)
|
|
|
|
{
|
|
|
|
static struct { char *buf; size_t size; } prev, cur, tmp;
|
|
|
|
|
|
|
|
if (!prev.buf)
|
|
|
|
getline(&prev.buf, &prev.size, fp);
|
|
|
|
while (getline(&cur.buf, &cur.size, fp) != -1) {
|
|
|
|
if (uflag > linecmp((const char **) &cur.buf, (const char **) &prev.buf)) {
|
|
|
|
if (!Cflag)
|
|
|
|
weprintf("disorder: %s", cur.buf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
tmp = cur;
|
|
|
|
cur = prev;
|
|
|
|
prev = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
static int
|
2011-06-02 12:03:34 +00:00
|
|
|
linecmp(const char **a, const char **b)
|
|
|
|
{
|
2014-04-12 15:53:10 +00:00
|
|
|
char *s1, *s2;
|
|
|
|
int res = 0;
|
|
|
|
struct kdlist *node;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (node = head; node && res == 0; node = node->next) {
|
2014-04-12 15:53:10 +00:00
|
|
|
s1 = columns((char *)*a, &node->keydef);
|
|
|
|
s2 = columns((char *)*b, &node->keydef);
|
|
|
|
|
2014-05-06 16:47:02 +00:00
|
|
|
/* if -u is given, don't use default key definition
|
|
|
|
* unless it is the only one */
|
2014-11-13 17:29:30 +00:00
|
|
|
if (uflag && node == tail && head != tail)
|
2014-04-12 15:53:10 +00:00
|
|
|
res = 0;
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (node->keydef.flags & MOD_N)
|
2014-05-06 11:35:06 +00:00
|
|
|
res = strtol(s1, 0, 10) - strtol(s2, 0, 10);
|
2013-12-12 13:08:49 +00:00
|
|
|
else
|
2014-04-12 15:53:10 +00:00
|
|
|
res = strcmp(s1, s2);
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (node->keydef.flags & MOD_R)
|
2014-05-06 14:07:05 +00:00
|
|
|
res = -res;
|
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
free(s1);
|
|
|
|
free(s2);
|
|
|
|
}
|
2014-05-06 14:07:05 +00:00
|
|
|
return res;
|
2014-04-12 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-05-06 14:07:05 +00:00
|
|
|
parse_flags(char **s, int *flags, int bflag)
|
|
|
|
{
|
2014-11-13 17:29:30 +00:00
|
|
|
while (isalpha((int)**s))
|
|
|
|
switch (*((*s)++)) {
|
2014-05-06 14:07:05 +00:00
|
|
|
case 'b':
|
|
|
|
*flags |= bflag;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*flags |= MOD_N;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*flags |= MOD_R;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_keydef(struct keydef *kd, char *s, int flags)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
|
|
|
char *rest = s;
|
2014-05-03 17:06:20 +00:00
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
kd->start_column = 1;
|
|
|
|
kd->start_char = 1;
|
|
|
|
/* 0 means end of line */
|
|
|
|
kd->end_column = 0;
|
|
|
|
kd->end_char = 0;
|
2014-05-06 14:07:05 +00:00
|
|
|
kd->flags = flags;
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->start_column = strtol(rest, &rest, 10);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->start_column < 1)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*rest == '.')
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->start_char = strtol(rest+1, &rest, 10);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->start_char < 1)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
2014-11-19 19:59:37 +00:00
|
|
|
if (parse_flags(&rest, &kd->flags, MOD_STARTB) < 0)
|
2014-05-06 14:07:05 +00:00
|
|
|
return -1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*rest == ',') {
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->end_column = strtol(rest+1, &rest, 10);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->end_column && kd->end_column < kd->start_column)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*rest == '.') {
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->end_char = strtol(rest+1, &rest, 10);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->end_char < 1)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-19 19:59:37 +00:00
|
|
|
if (parse_flags(&rest, &kd->flags, MOD_ENDB) < 0)
|
2014-05-06 14:07:05 +00:00
|
|
|
return -1;
|
2013-12-12 13:08:49 +00:00
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*rest != '\0')
|
2014-04-12 15:53:10 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2011-06-02 12:03:34 +00:00
|
|
|
}
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
static char *
|
2014-05-15 18:08:17 +00:00
|
|
|
skipblank(char *s)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
2014-04-30 14:08:11 +00:00
|
|
|
while(*s && isblank(*s))
|
|
|
|
s++;
|
2014-04-12 15:53:10 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2014-05-15 18:08:17 +00:00
|
|
|
nextcol(char *s)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!fieldsep) {
|
2014-05-15 18:08:17 +00:00
|
|
|
s = skipblank(s);
|
|
|
|
while(*s && !isblank(*s))
|
|
|
|
s++;
|
|
|
|
} else {
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!strchr(s, *fieldsep))
|
2014-05-15 18:08:17 +00:00
|
|
|
s = strchr(s, '\0');
|
|
|
|
else
|
|
|
|
s = strchr(s, *fieldsep) + 1;
|
2014-05-03 16:34:51 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
static char *
|
|
|
|
columns(char *line, const struct keydef *kd)
|
|
|
|
{
|
|
|
|
char *start, *end;
|
2014-04-30 14:08:11 +00:00
|
|
|
char *res;
|
2014-05-15 18:08:17 +00:00
|
|
|
int i;
|
2014-04-18 16:21:31 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 1, start = line; i < kd->start_column; i++)
|
2014-05-15 18:08:17 +00:00
|
|
|
start = nextcol(start);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->flags & MOD_STARTB)
|
2014-05-15 18:08:17 +00:00
|
|
|
start = skipblank(start);
|
|
|
|
start += MIN(kd->start_char, nextcol(start) - start) - 1;
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->end_column) {
|
|
|
|
for (i = 1, end = line; i < kd->end_column; i++)
|
2014-05-15 18:08:17 +00:00
|
|
|
end = nextcol(end);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->flags & MOD_ENDB)
|
2014-05-15 18:08:17 +00:00
|
|
|
end = skipblank(end);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->end_char)
|
2014-05-15 18:08:17 +00:00
|
|
|
end += MIN(kd->end_char, nextcol(end) - end);
|
2014-04-18 16:21:31 +00:00
|
|
|
else
|
2014-05-15 18:08:17 +00:00
|
|
|
end = nextcol(end);
|
2014-05-03 16:34:51 +00:00
|
|
|
} else {
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!(end = strchr(line, '\n')))
|
2014-05-06 11:37:05 +00:00
|
|
|
end = strchr(line, '\0');
|
2014-05-03 16:34:51 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!(res = strndup(start, end - start)))
|
2014-04-30 14:08:11 +00:00
|
|
|
enprintf(2, "strndup:");
|
|
|
|
return res;
|
2014-04-12 15:53:10 +00:00
|
|
|
}
|