2011-06-02 08:03:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-04-12 11:53:10 -04:00
|
|
|
#include <ctype.h>
|
2011-06-02 08:03:34 -04:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-12 11:53:10 -04:00
|
|
|
struct keydef {
|
|
|
|
unsigned start_column;
|
|
|
|
unsigned end_column;
|
|
|
|
unsigned start_char;
|
|
|
|
unsigned end_char;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct kdlist {
|
|
|
|
struct keydef keydef;
|
|
|
|
struct kdlist *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kdlist *head = NULL;
|
|
|
|
static struct kdlist *curr = NULL;
|
|
|
|
|
|
|
|
static void addkeydef(char *);
|
|
|
|
static void freelist(void);
|
2011-06-02 08:03:34 -04:00
|
|
|
static int linecmp(const char **, const char **);
|
2014-04-12 11:53:10 -04:00
|
|
|
static char *next_nonblank(char *);
|
|
|
|
static char *next_blank(char *);
|
|
|
|
static int parse_keydef(struct keydef *, char *);
|
|
|
|
static char *columns(char *, const struct keydef *);
|
2011-06-02 08:03:34 -04:00
|
|
|
|
|
|
|
static bool rflag = false;
|
2011-06-02 08:09:30 -04:00
|
|
|
static bool uflag = false;
|
2013-12-12 08:08:49 -05:00
|
|
|
static bool nflag = false;
|
2012-05-21 16:09:44 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-04-12 11:53:10 -04:00
|
|
|
enprintf(2, "usage: %s [-nru] [-k def]... [file...]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-06-02 08:03:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
FILE *fp;
|
2014-05-03 12:28:20 -04:00
|
|
|
struct linebuf linebuf = EMPTY_LINEBUF;
|
2011-06-02 08:03:34 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
2013-12-12 08:08:49 -05:00
|
|
|
case 'n':
|
|
|
|
nflag = true;
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
case 'r':
|
|
|
|
rflag = true;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = true;
|
|
|
|
break;
|
2014-04-12 11:53:10 -04:00
|
|
|
case 'k':
|
|
|
|
addkeydef(EARGF(usage()));
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-04-12 11:53:10 -04:00
|
|
|
addkeydef("1");
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
if(argc == 0) {
|
2012-05-21 16:09:44 -04:00
|
|
|
getlines(stdin, &linebuf);
|
2013-06-14 14:20:47 -04:00
|
|
|
} else for(; argc > 0; argc--, argv++) {
|
2013-11-13 06:39:24 -05:00
|
|
|
if(!(fp = fopen(argv[0], "r"))) {
|
2014-04-12 11:53:10 -04:00
|
|
|
enprintf(2, "fopen %s:", argv[0]);
|
2013-11-13 06:39:24 -05:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-21 16:09:44 -04:00
|
|
|
getlines(fp, &linebuf);
|
2011-06-02 08:03:34 -04:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2013-06-14 14:20:47 -04:00
|
|
|
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines,
|
|
|
|
(int (*)(const void *, const void *))linecmp);
|
2011-06-02 08:03:34 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
for(i = 0; i < linebuf.nlines; i++) {
|
2014-04-12 11:53:10 -04:00
|
|
|
if(!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i],
|
|
|
|
(const char **)&linebuf.lines[i-1])) {
|
2012-05-21 16:09:44 -04:00
|
|
|
fputs(linebuf.lines[i], stdout);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-12 11:53:10 -04:00
|
|
|
freelist();
|
2013-10-07 11:41:55 -04:00
|
|
|
return EXIT_SUCCESS;
|
2011-06-02 08:03:34 -04:00
|
|
|
}
|
|
|
|
|
2014-04-12 11:53:10 -04:00
|
|
|
static void
|
|
|
|
addkeydef(char *def)
|
|
|
|
{
|
|
|
|
struct kdlist *node;
|
|
|
|
|
|
|
|
node = malloc(sizeof(*node));
|
|
|
|
if(!node)
|
|
|
|
enprintf(2, "malloc:");
|
|
|
|
if(!head)
|
|
|
|
head = node;
|
|
|
|
if(parse_keydef(&node->keydef, def))
|
|
|
|
enprintf(2, "parse_keydef:");
|
|
|
|
if(curr)
|
|
|
|
curr->next = node;
|
|
|
|
node->next = NULL;
|
|
|
|
curr = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
freelist(void)
|
|
|
|
{
|
|
|
|
struct kdlist *node;
|
|
|
|
struct kdlist *tmp;
|
|
|
|
|
|
|
|
for(node = head; node; node = tmp) {
|
|
|
|
tmp = node->next;
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-06-02 08:03:34 -04:00
|
|
|
linecmp(const char **a, const char **b)
|
|
|
|
{
|
2014-04-12 11:53:10 -04:00
|
|
|
char *s1, *s2;
|
|
|
|
int res = 0;
|
|
|
|
struct kdlist *node;
|
|
|
|
|
|
|
|
for(node = head; node && res == 0; node = node->next) {
|
|
|
|
s1 = columns((char *)*a, &node->keydef);
|
|
|
|
s2 = columns((char *)*b, &node->keydef);
|
|
|
|
|
|
|
|
/* don't consider modifiers if it's the default key
|
|
|
|
* definition that was implicitly added */
|
|
|
|
/* if -u is given, don't use default */
|
|
|
|
if(uflag && !(node == head) && !node->next)
|
|
|
|
res = 0;
|
|
|
|
else if(!(node == head) && !node->next)
|
|
|
|
res = strcmp(s1, s2);
|
|
|
|
else if(nflag)
|
|
|
|
res = strtoul(s1, 0, 10) - strtoul(s2, 0, 10);
|
2013-12-12 08:08:49 -05:00
|
|
|
else
|
2014-04-12 11:53:10 -04:00
|
|
|
res = strcmp(s1, s2);
|
|
|
|
|
|
|
|
free(s1);
|
|
|
|
free(s2);
|
|
|
|
}
|
|
|
|
return rflag ? -res : res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_keydef(struct keydef *kd, char *s)
|
|
|
|
{
|
|
|
|
char *rest = s;
|
|
|
|
kd->start_column = 1;
|
|
|
|
kd->start_char = 1;
|
|
|
|
/* 0 means end of line */
|
|
|
|
kd->end_column = 0;
|
|
|
|
kd->end_char = 0;
|
|
|
|
|
|
|
|
kd->start_column = strtoul(rest, &rest, 10);
|
|
|
|
if(!kd->start_column)
|
|
|
|
enprintf(2, "starting column cannot be 0\n");
|
|
|
|
if(*rest == '.')
|
|
|
|
kd->start_char = strtoul(rest+1, &rest, 10);
|
|
|
|
if(*rest == ',') {
|
|
|
|
kd->end_column = strtoul(rest+1, &rest, 10);
|
|
|
|
if(kd->end_column < kd->start_column)
|
|
|
|
enprintf(2, ",%u is too small\n", kd->end_column);
|
2013-12-12 08:08:49 -05:00
|
|
|
}
|
2014-04-12 11:53:10 -04:00
|
|
|
if(*rest == '.')
|
|
|
|
kd->end_char = strtoul(rest+1, &rest, 10);
|
|
|
|
if(*rest != '\0')
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2011-06-02 08:03:34 -04:00
|
|
|
}
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2014-04-12 11:53:10 -04:00
|
|
|
static char *
|
|
|
|
next_nonblank(char *s)
|
|
|
|
{
|
2014-04-30 10:08:11 -04:00
|
|
|
while(*s && isblank(*s))
|
|
|
|
s++;
|
2014-04-12 11:53:10 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
next_blank(char *s)
|
|
|
|
{
|
2014-04-30 10:08:11 -04:00
|
|
|
while(*s && !isblank(*s))
|
|
|
|
s++;
|
2014-04-12 11:53:10 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
columns(char *line, const struct keydef *kd)
|
|
|
|
{
|
|
|
|
char *rest;
|
|
|
|
char *start, *end;
|
2014-04-30 10:08:11 -04:00
|
|
|
char *res;
|
2014-04-23 10:00:34 -04:00
|
|
|
unsigned int i;
|
2014-04-18 12:21:31 -04:00
|
|
|
|
2014-04-30 10:08:11 -04:00
|
|
|
rest = line;
|
|
|
|
for(i = 0; i < kd->start_column; i++) {
|
2014-04-12 11:53:10 -04:00
|
|
|
if(i != 0)
|
|
|
|
rest = next_blank(rest);
|
|
|
|
rest = next_nonblank(rest);
|
|
|
|
}
|
2014-04-30 10:08:11 -04:00
|
|
|
for(i = 1; i < kd->start_char && *rest && !isblank(*rest); i++)
|
|
|
|
rest++;
|
2014-04-12 11:53:10 -04:00
|
|
|
start = rest;
|
|
|
|
|
|
|
|
if(kd->end_column) {
|
2014-04-30 10:08:11 -04:00
|
|
|
rest = line;
|
|
|
|
for(i = 0; i < kd->end_column; i++) {
|
2014-04-12 11:53:10 -04:00
|
|
|
if(i != 0)
|
|
|
|
rest = next_blank(rest);
|
|
|
|
rest = next_nonblank(rest);
|
|
|
|
}
|
2014-04-18 12:21:31 -04:00
|
|
|
if(kd->end_char)
|
2014-04-30 10:08:11 -04:00
|
|
|
for(i = 1; i < kd->end_char && *rest && !isblank(*rest); i++)
|
|
|
|
rest++;
|
2014-04-18 12:21:31 -04:00
|
|
|
else
|
2014-04-12 11:53:10 -04:00
|
|
|
rest = next_blank(rest);
|
2014-04-30 10:08:11 -04:00
|
|
|
end = rest - 1;
|
|
|
|
} else
|
2014-04-12 11:53:10 -04:00
|
|
|
end = rest + strlen(rest);
|
2014-04-30 10:08:11 -04:00
|
|
|
if((res = strndup(start, end - start)) == NULL)
|
|
|
|
enprintf(2, "strndup:");
|
|
|
|
return res;
|
2014-04-12 11:53:10 -04:00
|
|
|
}
|