sort: add -b flag; don't use it as default

This commit is contained in:
Jakob Kramer 2014-05-03 20:24:08 +02:00 committed by sin
parent a62a2197a8
commit d965985a52

29
sort.c
View File

@ -30,11 +30,13 @@ static char *next_nonblank(char *);
static char *next_blank(char *); static char *next_blank(char *);
static int parse_keydef(struct keydef *, char *); static int parse_keydef(struct keydef *, char *);
static char *skip_columns(char *, size_t); static char *skip_columns(char *, size_t);
static char *end_column(char *);
static char *columns(char *, const struct keydef *); static char *columns(char *, const struct keydef *);
static bool rflag = false; static bool rflag = false;
static bool uflag = false; static bool uflag = false;
static bool nflag = false; static bool nflag = false;
static bool bflag = false;
static void static void
usage(void) usage(void)
@ -59,6 +61,9 @@ main(int argc, char *argv[])
case 'u': case 'u':
uflag = true; uflag = true;
break; break;
case 'b':
bflag = true;
break;
case 'k': case 'k':
addkeydef(EARGF(usage())); addkeydef(EARGF(usage()));
break; break;
@ -169,7 +174,7 @@ parse_keydef(struct keydef *kd, char *s)
kd->start_char = strtoul(rest+1, &rest, 10); kd->start_char = strtoul(rest+1, &rest, 10);
if(*rest == ',') { if(*rest == ',') {
kd->end_column = strtoul(rest+1, &rest, 10); kd->end_column = strtoul(rest+1, &rest, 10);
if(kd->end_column < kd->start_column) if(kd->end_column && kd->end_column < kd->start_column)
enprintf(2, ",%u is too small\n", kd->end_column); enprintf(2, ",%u is too small\n", kd->end_column);
if(*rest == '.') if(*rest == '.')
kd->end_char = strtoul(rest+1, &rest, 10); kd->end_char = strtoul(rest+1, &rest, 10);
@ -201,14 +206,30 @@ skip_columns(char *s, size_t n)
size_t i; size_t i;
for(i = 0; i < n; i++) { for(i = 0; i < n; i++) {
if(bflag) {
if(i != 0) if(i != 0)
s = next_blank(s); s = next_blank(s);
s = next_nonblank(s); s = next_nonblank(s);
} else {
if(i == 0)
continue;
s = next_nonblank(s);
s = next_blank(s);
}
} }
return s; return s;
} }
static char *
end_column(char *s)
{
if(bflag)
return next_blank(s);
else
return next_blank(next_nonblank(s));
}
static char * static char *
columns(char *line, const struct keydef *kd) columns(char *line, const struct keydef *kd)
{ {
@ -216,14 +237,14 @@ columns(char *line, const struct keydef *kd)
char *res; char *res;
start = skip_columns(line, kd->start_column); start = skip_columns(line, kd->start_column);
start += MIN(kd->start_char, next_blank(start) - start) - 1; start += MIN(kd->start_char, end_column(start) - start) - 1;
if(kd->end_column) { if(kd->end_column) {
end = skip_columns(line, kd->end_column); end = skip_columns(line, kd->end_column);
if(kd->end_char) if(kd->end_char)
end += MIN(kd->end_char, next_blank(end) - end); end += MIN(kd->end_char, end_column(end) - end);
else else
end = next_blank(end); end = end_column(end);
} else { } else {
end = line + strlen(line); end = line + strlen(line);
} }