]> mj.ucw.cz Git - xsv.git/blobdiff - xsv.c
Convert a couple of remaining putchar's to unlocked
[xsv.git] / xsv.c
diff --git a/xsv.c b/xsv.c
index f980634d214036ef5e20199ea4e178bd4c235680..0d2d972b055544ddab45ad06132ed09ea4e2b8c6 100644 (file)
--- a/xsv.c
+++ b/xsv.c
@@ -1,11 +1,9 @@
 /*
- *     A Swiss-Army Knife for CSV-like Files
+ *     The Swiss-Army Knife for CSV-like Files
  *
  *     (c) 2012 Martin Mares <mj@ucw.cz>
  */
 
-#define _GNU_SOURCE
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -24,6 +22,9 @@
 #define UNUSED
 #endif
 
+static void select_fields(void);
+static void select_all_fields(void);
+
 /*** General functions ***/
 
 static void NONRET die(char *msg, ...)
@@ -413,6 +414,12 @@ static int regex_read(struct format *fmt)
                        }
                        return 1;
                }
+               if (ovec[0] == ovec[1]) {
+                       warn(fmt, "Regular expression matched an empty separator.");
+                       new_field(i);
+                       in_field->len = n - i;
+                       return 1;
+               }
                if (!fmt->sloppy || ovec[0]) {
                        new_field(i);
                        in_field->len = ovec[0] - i;
@@ -443,7 +450,7 @@ static void table_write(struct format *fmt)
                                cw = fw;
                        }
                        while (len--)
-                               putchar(*p++);
+                               putchar_unlocked(*p++);
                }
                while (fw < cw) {
                        putchar_unlocked(' ');
@@ -468,7 +475,7 @@ static void table_write_grid(struct format *fmt, int pos UNUSED)
                putchar_unlocked('+');
                int w = fmt->table_sep + *intarray_nth(&column_widths, i);
                while (w--)
-                       putchar('-');
+                       putchar_unlocked('-');
        }
        putchar_unlocked('+');
        putchar_unlocked('\n');
@@ -611,20 +618,22 @@ static void write_header(void)
                return;
        }
 
+       int want_select_fields = 0;
        if (out_format->set_field_names) {
                struct field_names *fn = xmalloc_zero(sizeof(*fn));
                out_format->field_names = fn;
                add_field_names(fn, out_format->set_field_names);
-       } else if (in_format->field_names)
+       } else if (in_format->field_names) {
                out_format->field_names = in_format->field_names;
-       else
+               want_select_fields = 1;
+       } else
                die("Output header requested, but no field names specified");
 
        line_reset(&in_line);
-       fields_reset(&out_fields);
+       fields_reset(&in_fields);
        struct field_names *fn = out_format->field_names;
        for (int i = 0; i < stringarray_count(&fn->names); i++) {
-               struct field *f = fields_push(&out_fields);
+               struct field *f = fields_push(&in_fields);
                f->start_pos = line_count(&in_line);
                f->len = 0;
                char *s = *stringarray_nth(&fn->names, i);
@@ -634,6 +643,12 @@ static void write_header(void)
                }
        }
 
+       fields_reset(&out_fields);
+       if (want_select_fields)
+               select_fields();
+       else
+               select_all_fields();
+
        // This is tricky: when we are formatting a table, field names are normally
        // calculated in pass 1, but the header is written in pass 2, so we have to
        // update column statistics, because field name can be too wide to fit.
@@ -696,7 +711,7 @@ static int parse_field(char *str)
        if (in_format->field_names && (f = find_field_by_name(in_format->field_names, str)) > 0)
                return f;
 
-       die("Unknown field %s", str);
+       die("Unknown field `%s'", str);
 }
 
 static char *parse_selector(char *str)
@@ -813,7 +828,7 @@ static void NONRET usage(void)
 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
 \n\
 Formats:\n\
--t, --tsv              TAB-separated values (default)\n\
+-t, --tsv              Tab-separated values (default)\n\
 -c, --csv              Comma-separated values\n\
 -w, --ws               Values separated by arbitrary whitespace\n\
 -r, --regex=<rx>       Separator given by Perl regular expression (input only)\n\
@@ -854,6 +869,7 @@ static const char short_options[] = "cd:f:hqr:twW";
 
 enum long_options {
        OPT_HELP = 256,
+       OPT_VERSION,
        OPT_TRIM,
        OPT_ALWAYS_QUOTE,
        OPT_TABLE,
@@ -870,6 +886,7 @@ static const struct option long_options[] = {
        { "fs",                 1,      NULL,   'd' },
        { "grid",               0,      NULL,   OPT_GRID },
        { "header",             0,      NULL,   'h' },
+       { "help",               0,      NULL,   OPT_HELP },
        { "quiet",              0,      NULL,   'q' },
        { "regex",              1,      NULL,   'r' },
        { "sloppy",             0,      NULL,   's' },
@@ -877,8 +894,8 @@ static const struct option long_options[] = {
        { "table-sep",          1,      NULL,   OPT_TABLE_SEP },
        { "trim",               0,      NULL,   OPT_TRIM },
        { "tsv",                0,      NULL,   't' },
+       { "version",            0,      NULL,   OPT_VERSION },
        { "ws",                 0,      NULL,   'w' },
-       { "help",               0,      NULL,   OPT_HELP },
        { NULL,                 0,      NULL,   0 },
 };
 
@@ -984,6 +1001,9 @@ int main(int argc, char **argv)
                                break;
                        case OPT_HELP:
                                usage();
+                       case OPT_VERSION:
+                               puts("This is xsv version " VERSION ".");
+                               exit(0);
                        case OPT_TRIM:
                                want_trim = 1;
                                break;