commit 7d1fd2621ed35fc3eb066e5ff4b886710b5564f8
Author: Jakob Kramer <[email protected]>
Date:   Thu May 15 20:08:17 2014 +0200

    add -t flag to sort

diff --git a/sort.1 b/sort.1
index 955cebd..12fb95d 100644
--- a/sort.1
+++ b/sort.1
@@ -4,9 +4,10 @@ sort \- sort lines
 .SH SYNOPSIS
 .B sort
 .RB [ \-bnru ]
+.RB [ \-t
+.IR delim ]
 .RB [ \-k
-.I key
-.R ]...
+.IR key ]...
 .RI [ file ...]
 .SH DESCRIPTION
 .B sort
@@ -17,16 +18,7 @@ given, sort reads from stdin.
 .B \-b
 skip leading whitespace of columns when sorting.
 .TP
-.B \-n
-perform a numeric sort.
-.TP
-.B \-r
-reverses the sort.
-.TP
-.B \-u
-prints equal lines only once.
-.TP
-.B \-k key
+.BI \-k \ key
 specifies a key definition of the form
 .BR S [. s ][ f ][, E [. e ][ f ]]
 where
@@ -50,3 +42,15 @@ can be used to specify options
 that only apply to this key definition.
 .B b
 is special in that it only applies to the column that it was specified after.
+.TP
+.B \-n
+perform a numeric sort.
+.TP
+.B \-r
+reverses the sort.
+.TP
+.BI \-t \ delim
+specifies the field delimiter.
+.TP
+.B \-u
+prints equal lines only once.
diff --git a/sort.c b/sort.c
index a00e902..b0b9131 100644
--- a/sort.c
+++ b/sort.c
@@ -34,20 +34,19 @@ static struct kdlist *tail = NULL;
 static void addkeydef(char *, int);
 static void freelist(void);
 static int linecmp(const char **, const char **);
-static char *next_nonblank(char *);
-static char *next_blank(char *);
+static char *skipblank(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 *nextcol(char *);
 static char *columns(char *, const struct keydef *);
 
 static bool uflag = false;
+static char *fieldsep = NULL;
 
 static void
 usage(void)
 {
-       enprintf(2, "usage: %s [-bnru] [-k def]... [file...]
", argv0);
+       enprintf(2, "usage: %s [-bnru] [-t delim] [-k def]... [file...]
", argv0);
 }
 
 int
@@ -59,21 +58,26 @@ main(int argc, char *argv[])
        int global_flags = 0;
 
        ARGBEGIN {
+       case 'b':
+               global_flags |= MOD_STARTB | MOD_ENDB;
+               break;
+       case 'k':
+               addkeydef(EARGF(usage()), global_flags);
+               break;
        case 'n':
                global_flags |= MOD_N;
                break;
        case 'r':
                global_flags |= MOD_R;
                break;
+       case 't':
+               fieldsep = EARGF(usage());
+               if(strlen(fieldsep) != 1)
+                       usage();
+               break;
        case 'u':
                uflag = true;
                break;
-       case 'b':
-               global_flags |= MOD_STARTB | MOD_ENDB;
-               break;
-       case 'k':
-               addkeydef(EARGF(usage()), global_flags);
-               break;
        default:
                usage();
        } ARGEND;
@@ -224,7 +228,7 @@ parse_keydef(struct keydef *kd, char *s, int flags)
 }
 
 static char *
-next_nonblank(char *s)
+skipblank(char *s)
 {
        while(*s && isblank(*s))
                s++;
@@ -232,51 +236,43 @@ next_nonblank(char *s)
 }
 
 static char *
-next_blank(char *s)
+nextcol(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++) {
-               if(i > 0)
-                       s = end_column(s);
-               if(bflag)
-                       s = next_nonblank(s);
+       if(fieldsep == NULL) {
+               s = skipblank(s);
+               while(*s && !isblank(*s))
+                       s++;
+       } else {
+               if(strchr(s, *fieldsep) == NULL)
+                       s = strchr(s, '

Reply via email to