sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 1622089a21a19e7d6fd20f213370a22a077c81cd
parent 61ee561728f53a264c384d7c67dcb4671a3903ce
Author: FRIGN <dev@frign.de>
Date:   Mon,  3 Aug 2015 01:32:21 +0200

Reorder functions in sort(1)

I kind of missed that the sorting was still not properly done.
parse_flags() and addkeydef() are independent of everything else,
so they can be put at the bottom.
Sorting the other functions reveals the true hierarchy much better.

Diffstat:
Msort.c | 226++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 113 insertions(+), 113 deletions(-)

diff --git a/sort.c b/sort.c @@ -26,83 +26,84 @@ enum { static TAILQ_HEAD(kdhead, keydef) kdhead = TAILQ_HEAD_INITIALIZER(kdhead); -static int parse_flags(char **, int *, int); -static void addkeydef(char *, int); -static int linecmp(const char **, const char **); -static void check(FILE *); static char *skipblank(char *); static char *skipnonblank(char *); static char *skipcolumn(char *, char *, int); static size_t columns(char *, const struct keydef *, char **, size_t *); +static int linecmp(const char **, const char **); +static void check(FILE *); +static int parse_flags(char **, int *, int); +static void addkeydef(char *, int); static int Cflag = 0, cflag = 0, uflag = 0; static char *fieldsep = NULL; static char *col1, *col2; static size_t col1siz, col2siz; -static int -parse_flags(char **s, int *flags, int bflag) +static char * +skipblank(char *s) { - while (isalpha((int)**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; + while (isblank(*s)) + s++; + return s; } -static void -addkeydef(char *kdstr, int flags) +static char * +skipnonblank(char *s) { - struct keydef *kd; - - kd = enmalloc(2, sizeof(*kd)); - - /* parse key definition kdstr with format - * start_column[.start_char][flags][,end_column[.end_char][flags]] - */ - kd->start_column = 1; - kd->start_char = 1; - kd->end_column = 0; /* 0 means end of line */ - kd->end_char = 0; /* 0 means end of column */ - kd->flags = flags; - - if ((kd->start_column = strtol(kdstr, &kdstr, 10)) < 1) - enprintf(2, "invalid start column in key definition\n"); + while (*s && *s != '\n' && !isblank(*s)) + s++; + return s; +} - if (*kdstr == '.') { - if ((kd->start_char = strtol(kdstr + 1, &kdstr, 10)) < 1) - enprintf(2, "invalid start character in key definition\n"); +static char * +skipcolumn(char *s, char *eol, int next_col) +{ + if (fieldsep) { + if ((s = strstr(s, fieldsep))) + s += next_col ? strlen(fieldsep) : 0; + else + s = eol; + } else { + s = skipblank(s); + s = skipnonblank(s); } - if (parse_flags(&kdstr, &kd->flags, MOD_STARTB) < 0) - enprintf(2, "invalid start flags in key definition\n"); + return s; +} - if (*kdstr == ',') { - if ((kd->end_column = strtol(kdstr + 1, &kdstr, 10)) < 0) - enprintf(2, "invalid end column in key definition\n"); - if (*kdstr == '.') { - if ((kd->end_char = strtol(kdstr + 1, &kdstr, 10)) < 0) - enprintf(2, "invalid end character in key definition\n"); - } - if (parse_flags(&kdstr, &kd->flags, MOD_ENDB) < 0) - enprintf(2, "invalid end flags in key definition\n"); - } +static size_t +columns(char *line, const struct keydef *kd, char **col, size_t *colsiz) +{ + char *start, *end, *eol = strchr(line, '\n'); + size_t len; + int i; - if (*kdstr != '\0') - enprintf(2, "invalid key definition\n"); + for (i = 1, start = line; i < kd->start_column; i++) + start = skipcolumn(start, eol, 1); + if (kd->flags & MOD_STARTB) + start = skipblank(start); + start = MIN(eol, start + kd->start_char - 1); - TAILQ_INSERT_TAIL(&kdhead, kd, entry); + if (kd->end_column) { + for (i = 1, end = line; i < kd->end_column; i++) + end = skipcolumn(end, eol, 1); + if (kd->flags & MOD_ENDB) + end = skipblank(end); + if (kd->end_char) + end = MIN(eol, end + kd->end_char); + else + end = skipcolumn(end, eol, 0); + } else { + end = eol; + } + len = start > end ? 0 : end - start; + if (!*col || *colsiz < len) + *col = erealloc(*col, len + 1); + memcpy(*col, start, len); + (*col)[len] = '\0'; + if (*colsiz < len) + *colsiz = len; + return len; } static int @@ -157,70 +158,69 @@ check(FILE *fp) } } -static char * -skipblank(char *s) +static int +parse_flags(char **s, int *flags, int bflag) { - while (isblank(*s)) - s++; - return s; -} + while (isalpha((int)**s)) { + switch (*((*s)++)) { + case 'b': + *flags |= bflag; + break; + case 'n': + *flags |= MOD_N; + break; + case 'r': + *flags |= MOD_R; + break; + default: + return -1; + } + } -static char * -skipnonblank(char *s) -{ - while (*s && *s != '\n' && !isblank(*s)) - s++; - return s; + return 0; } -static char * -skipcolumn(char *s, char *eol, int next_col) +static void +addkeydef(char *kdstr, int flags) { - if (fieldsep) { - if ((s = strstr(s, fieldsep))) - s += next_col ? strlen(fieldsep) : 0; - else - s = eol; - } else { - s = skipblank(s); - s = skipnonblank(s); - } - return s; -} + struct keydef *kd; -static size_t -columns(char *line, const struct keydef *kd, char **col, size_t *colsiz) -{ - char *start, *end, *eol = strchr(line, '\n'); - size_t len; - int i; + kd = enmalloc(2, sizeof(*kd)); - for (i = 1, start = line; i < kd->start_column; i++) - start = skipcolumn(start, eol, 1); - if (kd->flags & MOD_STARTB) - start = skipblank(start); - start = MIN(eol, start + kd->start_char - 1); + /* parse key definition kdstr with format + * start_column[.start_char][flags][,end_column[.end_char][flags]] + */ + kd->start_column = 1; + kd->start_char = 1; + kd->end_column = 0; /* 0 means end of line */ + kd->end_char = 0; /* 0 means end of column */ + kd->flags = flags; - if (kd->end_column) { - for (i = 1, end = line; i < kd->end_column; i++) - end = skipcolumn(end, eol, 1); - if (kd->flags & MOD_ENDB) - end = skipblank(end); - if (kd->end_char) - end = MIN(eol, end + kd->end_char); - else - end = skipcolumn(end, eol, 0); - } else { - end = eol; + if ((kd->start_column = strtol(kdstr, &kdstr, 10)) < 1) + enprintf(2, "invalid start column in key definition\n"); + + if (*kdstr == '.') { + if ((kd->start_char = strtol(kdstr + 1, &kdstr, 10)) < 1) + enprintf(2, "invalid start character in key definition\n"); } - len = start > end ? 0 : end - start; - if (!*col || *colsiz < len) - *col = erealloc(*col, len + 1); - memcpy(*col, start, len); - (*col)[len] = '\0'; - if (*colsiz < len) - *colsiz = len; - return len; + if (parse_flags(&kdstr, &kd->flags, MOD_STARTB) < 0) + enprintf(2, "invalid start flags in key definition\n"); + + if (*kdstr == ',') { + if ((kd->end_column = strtol(kdstr + 1, &kdstr, 10)) < 0) + enprintf(2, "invalid end column in key definition\n"); + if (*kdstr == '.') { + if ((kd->end_char = strtol(kdstr + 1, &kdstr, 10)) < 0) + enprintf(2, "invalid end character in key definition\n"); + } + if (parse_flags(&kdstr, &kd->flags, MOD_ENDB) < 0) + enprintf(2, "invalid end flags in key definition\n"); + } + + if (*kdstr != '\0') + enprintf(2, "invalid key definition\n"); + + TAILQ_INSERT_TAIL(&kdhead, kd, entry); } static void