sbase/sort.c

289 lines
5.3 KiB
C
Raw Normal View History

2011-06-02 08:03:34 -04:00
/* See LICENSE file for copyright and license details. */
#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"
struct keydef {
int start_column;
int end_column;
int start_char;
int end_char;
int flags;
};
enum {
MOD_N = 1 << 1,
MOD_STARTB = 1 << 2,
MOD_ENDB = 1 << 3,
MOD_R = 1 << 4
};
struct kdlist {
struct keydef keydef;
struct kdlist *next;
};
static struct kdlist *head = NULL;
static struct kdlist *tail = NULL;
static void addkeydef(char *, int);
static void freelist(void);
2011-06-02 08:03:34 -04:00
static int linecmp(const char **, const char **);
static char *next_nonblank(char *);
static char *next_blank(char *);
static int parse_flags(char **, int *, int);
static int parse_keydef(struct keydef *, char *, int);
static char *skip_columns(char *, size_t, bool);
static char *end_column(char *);
static char *columns(char *, const struct keydef *);
2011-06-02 08:03:34 -04:00
2011-06-02 08:09:30 -04:00
static bool uflag = false;
2013-06-14 14:20:47 -04:00
static void
usage(void)
{
2014-05-03 14:42:33 -04:00
enprintf(2, "usage: %s [-bnru] [-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;
int global_flags = 0;
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':
global_flags |= MOD_N;
2013-12-12 08:08:49 -05:00
break;
2013-06-14 14:20:47 -04:00
case 'r':
global_flags |= MOD_R;
2013-06-14 14:20:47 -04:00
break;
case 'u':
uflag = true;
break;
case 'b':
global_flags |= MOD_STARTB | MOD_ENDB;
break;
case 'k':
addkeydef(EARGF(usage()), global_flags);
break;
2013-06-14 14:20:47 -04:00
default:
usage();
} ARGEND;
if(!head && global_flags)
addkeydef("1", global_flags);
addkeydef("1", global_flags & MOD_R);
2013-06-14 14:20:47 -04:00
if(argc == 0) {
getlines(stdin, &linebuf);
2013-06-14 14:20:47 -04:00
} else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) {
enprintf(2, "fopen %s:", argv[0]);
continue;
}
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++) {
if(!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i],
(const char **)&linebuf.lines[i-1])) {
fputs(linebuf.lines[i], stdout);
2013-06-14 14:20:47 -04:00
}
}
freelist();
return EXIT_SUCCESS;
2011-06-02 08:03:34 -04:00
}
static void
addkeydef(char *def, int flags)
{
struct kdlist *node;
node = malloc(sizeof(*node));
if(!node)
enprintf(2, "malloc:");
if(!head)
head = node;
if(parse_keydef(&node->keydef, def, flags))
enprintf(2, "faulty key definition\n");
if(tail)
tail->next = node;
node->next = NULL;
tail = 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)
{
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);
/* if -u is given, don't use default key definition
* unless it is the only one */
if(uflag && node == tail && head != tail)
res = 0;
else if(node->keydef.flags & MOD_N)
res = strtol(s1, 0, 10) - strtol(s2, 0, 10);
2013-12-12 08:08:49 -05:00
else
res = strcmp(s1, s2);
if(node->keydef.flags & MOD_R)
res = -res;
free(s1);
free(s2);
}
return res;
}
static int
parse_flags(char **s, int *flags, int bflag)
{
while(isalpha(**s))
switch(*((*s)++)) {
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)
{
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->flags = flags;
kd->start_column = strtol(rest, &rest, 10);
if(kd->start_column < 1)
return -1;
if(*rest == '.')
kd->start_char = strtol(rest+1, &rest, 10);
if(kd->start_char < 1)
return -1;
if(parse_flags(&rest, &kd->flags, MOD_STARTB) == -1)
return -1;
if(*rest == ',') {
kd->end_column = strtol(rest+1, &rest, 10);
if(kd->end_column && kd->end_column < kd->start_column)
return -1;
if(*rest == '.') {
kd->end_char = strtol(rest+1, &rest, 10);
if(kd->end_char < 1)
return -1;
}
if(parse_flags(&rest, &kd->flags, MOD_ENDB) == -1)
return -1;
2013-12-12 08:08:49 -05:00
}
if(*rest != '\0')
return -1;
return 0;
2011-06-02 08:03:34 -04:00
}
2013-06-14 14:20:47 -04:00
static char *
next_nonblank(char *s)
{
while(*s && isblank(*s))
s++;
return s;
}
static char *
next_blank(char *s)
{
while(*s && !isblank(*s))
s++;
return s;
}
static char *
skip_columns(char *s, size_t n, bool bflag)
{
size_t i;
for(i = 0; i < n; i++) {
2014-05-03 14:39:45 -04:00
if(i > 0)
s = end_column(s);
if(bflag)
s = next_nonblank(s);
}
return s;
}
static char *
end_column(char *s)
{
if(isblank(*s))
s = next_nonblank(s);
return next_blank(s);
}
static char *
columns(char *line, const struct keydef *kd)
{
char *start, *end;
char *res;
2014-04-18 12:21:31 -04:00
start = skip_columns(line, kd->start_column, kd->flags & MOD_STARTB);
start += MIN(kd->start_char, end_column(start) - start) - 1;
if(kd->end_column) {
end = skip_columns(line, kd->end_column, kd->flags & MOD_ENDB);
2014-04-18 12:21:31 -04:00
if(kd->end_char)
end += MIN(kd->end_char, end_column(end) - end);
2014-04-18 12:21:31 -04:00
else
end = end_column(end);
} else {
if((end = strchr(line, '\n')) == NULL)
end = strchr(line, '\0');
}
if((res = strndup(start, end - start)) == NULL)
enprintf(2, "strndup:");
return res;
}