]> mj.ucw.cz Git - xsv.git/blob - xsv.c
FIXME considered unworthy
[xsv.git] / xsv.c
1 /*
2  *      A Swiss-Army Knife for CSV-like Files
3  *
4  *      (c) 2012 Martin Mares <mj@ucw.cz>
5  */
6
7 #define _GNU_SOURCE
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <getopt.h>
14 #include <wchar.h>
15 #include <locale.h>
16
17 #include <pcre.h>
18
19 #ifdef __GNUC__
20 #define NONRET __attribute__((noreturn))
21 #define UNUSED __attribute__((unused))
22 #else
23 #define NONRET
24 #define UNUSED
25 #endif
26
27 /*** General functions ***/
28
29 static void NONRET die(char *msg, ...)
30 {
31         va_list args;
32         va_start(args, msg);
33         fprintf(stderr, "xsv: ");
34         vfprintf(stderr, msg, args);
35         fputc('\n', stderr);
36         va_end(args);
37         exit(1);
38 }
39
40 /*** Memory allocation ***/
41
42 static void *xmalloc(size_t bytes)
43 {
44         void *p = malloc(bytes);
45         if (!p)
46                 die("Out of memory (cannot allocate %zu bytes)", bytes);
47         return p;
48 }
49
50 static void *xmalloc_zero(size_t bytes)
51 {
52         void *p = xmalloc(bytes);
53         memset(p, 0, bytes);
54         return p;
55 }
56
57 static void *xrealloc(void *old, size_t bytes)
58 {
59         void *p = realloc(old, bytes);
60         if (!p)
61                 die("Out of memory (cannot allocate %zu bytes)", bytes);
62         return p;
63 }
64
65 #define DECLARE_BUF(name, type) \
66         typedef struct { type *start; int count; int max; } name##_t;                           \
67         static inline void name##_init(name##_t *b) { b->start = NULL; b->count = b->max = 0; } \
68         static inline void name##_reset(name##_t *b) { b->count = 0; }                          \
69         static inline int name##_count(name##_t *b) { return b->count; }                        \
70         static void name##_extend(name##_t *b) {                                                \
71                 b->max = b->max ? 2*b->max : 16;                                                \
72                 b->start = xrealloc(b->start, b->max * sizeof(type));                           \
73         }                                                                                       \
74         static inline type *name##_push(name##_t *b) {                                          \
75                 if (b->count >= b->max) name##_extend(b);                                       \
76                 return &b->start[b->count++];                                                   \
77         }                                                                                       \
78         static inline type *name##_first(name##_t *b) { return b->start; }                      \
79         static inline type *name##_nth(name##_t *b, int n) { return &b->start[n]; }             \
80         // end
81
82 DECLARE_BUF(intarray, int);
83 DECLARE_BUF(stringarray, char *);
84
85 /*** Formats and their parameters ***/
86
87 enum format_id {
88         FORM_UNSPEC,
89         FORM_TSV,
90         FORM_CSV,
91         FORM_WS,
92         FORM_REGEX,
93         FORM_TMP,
94         FORM_TABLE,
95 };
96
97 struct format {
98         enum format_id id;
99         int fs;
100         int quote;
101         int quiet;
102         int (*read_line)(struct format *fmt);
103         void (*write_line)(struct format *fmt);
104         void (*write_grid)(struct format *fmt, int pos);        // -1=above, 1=below, 0=after header
105         int needs_stats;
106
107         // Field names
108         int has_header;
109         char *set_field_names;
110         struct field_names *field_names;
111
112         // CSV backend:
113         int always_quote;
114
115         // WS backend:
116         int strict_ws;
117
118         // regex backend:
119         pcre *pcre;
120         pcre_extra *pcre_extra;
121
122         // Temporary file backend:
123         FILE *tmp_file;
124
125         // Table backend:
126         int table_sep;
127         int table_grid;
128 };
129
130 static struct format *in_format, *out_format;
131 static int want_trim;
132
133 struct field {
134         int start_pos;
135         int len;
136 };
137
138 DECLARE_BUF(fields, struct field);
139 DECLARE_BUF(line, unsigned char);
140
141 static fields_t in_fields, out_fields;
142 static struct field *in_field;
143 static line_t in_line;
144 static int line_number;
145
146 static int read_line(void)
147 {
148         fields_reset(&in_fields);
149         line_reset(&in_line);
150         in_field = NULL;
151         if (!in_format->read_line(in_format))
152                 return 0;
153         if (ferror_unlocked(stdin))
154                 die("I/O error when reading standard input");
155         return 1;
156 }
157
158 static void write_line(void)
159 {
160         out_format->write_line(out_format);
161         if (ferror_unlocked(stdout))
162                 die("I/O error when writing standard input");
163 }
164
165 static void write_grid(int pos)
166 {
167         if (out_format->write_grid) {
168                 out_format->write_grid(out_format, pos);
169                 if (ferror_unlocked(stdout))
170                         die("I/O error when writing standard input");
171         }
172 }
173
174 static void new_field(int pos)
175 {
176         in_field = fields_push(&in_fields);
177         in_field->start_pos = pos;
178         in_field->len = 0;
179 }
180
181 static void ensure_field(int pos)
182 {
183         if (!in_field)
184                 new_field(pos);
185 }
186
187 static unsigned char *get_field(fields_t *fields, int i, int *len)
188 {
189         struct field *f = fields_nth(fields, i);
190         *len = f->len;
191         return line_nth(&in_line, f->start_pos);
192 }
193
194 static void warn(struct format *fmt, char *msg, ...)
195 {
196         if (!fmt->quiet) {
197                 fprintf(stderr, "Warning at line %d: ", line_number);
198                 va_list args;
199                 va_start(args, msg);
200                 vfprintf(stderr, msg, args);
201                 va_end(args);
202                 fputc('\n', stderr);
203         }
204 }
205
206 static int next_line(void)
207 {
208         for (;;) {
209                 int c = getchar_unlocked();
210                 if (c == '\r')
211                         continue;
212                 if (c < 0)
213                         return !!line_count(&in_line);
214                 if (c == '\n')
215                         return 1;
216                 *line_push(&in_line) = c;
217         }
218 }
219
220 static int field_chars(struct field *f)
221 {
222         unsigned char *s = line_nth(&in_line, f->start_pos);
223         int i = 0;
224         mbstate_t mbs;
225         memset(&mbs, 0, sizeof(mbs));
226
227         int chars = 0;
228         while (i < f->len) {
229                 size_t k = mbrlen((char *) s + i, f->len - i, &mbs);
230                 if ((int) k <= 0)
231                         break;
232                 i += k;
233                 chars++;
234         }
235
236         return chars;
237 }
238
239 /*** Field statistics ***/
240
241 static intarray_t column_widths;
242
243 static void update_stats(void)
244 {
245         for (int i = 0; i < fields_count(&out_fields); i++) {
246                 struct field *f = fields_nth(&out_fields, i);
247                 intarray_t *w = &column_widths;
248
249                 while (i >= intarray_count(w))
250                         *intarray_push(w) = 0;
251                 int fw = field_chars(f);
252                 if (*intarray_nth(w, i) < fw)
253                         *intarray_nth(w, i) = fw;
254         }
255 }
256
257 /*** CSV/TSV back-end */
258
259 static int csv_read(struct format *fmt)
260 {
261         int quoted = 0;
262         for (;;) {
263                 int c = getchar_unlocked();
264                 int i = line_count(&in_line);
265 restart:
266                 if (c == '\r')
267                         continue;
268                 if (c < 0 || c == '\n') {
269                         if (quoted)
270                                 warn(fmt, "Missing closing quote.");
271                         if (c < 0)
272                                 return !!fields_count(&in_fields);
273                         else
274                                 return 1;
275                 }
276                 if (quoted) {
277                         if (c == fmt->quote) {
278                                 c = getchar_unlocked();
279                                 if (c != fmt->quote) {
280                                         quoted = 0;
281                                         goto restart;
282                                 }
283                                 // Two quotes assimilate to one
284                         }
285                         // Fall through to pushing the character
286                 } else if (c == fmt->quote) {
287                         quoted = 1;
288                         continue;
289                 } else if (c == fmt->fs && !quoted) {
290                         ensure_field(i);
291                         new_field(i);
292                         continue;
293                 }
294                 ensure_field(i);
295                 *line_push(&in_line) = c;
296                 in_field->len++;
297         }
298 }
299
300 static int is_ws(int c)
301 {
302         return (c == ' ' || c == '\t' || c == '\f');
303 }
304
305 static void csv_write(struct format *fmt)
306 {
307         for (int i=0; i < fields_count(&out_fields); i++) {
308                 int len;
309                 unsigned char *p = get_field(&out_fields, i, &len);
310
311                 int need_quotes = 0;
312                 if (fmt->quote >= 0) {
313                         need_quotes = fmt->always_quote;
314                         for (int j=0; !need_quotes && j < len; j++) {
315                                 if (p[j] == fmt->fs || p[j] == fmt->quote)
316                                         need_quotes = 1;
317                         }
318                 }
319                 if (i)
320                         putchar_unlocked(fmt->fs);
321                 if (need_quotes)
322                         putchar_unlocked(fmt->quote);
323                 for (int j=0; j < len; j++) {
324                         int c = p[j];
325                         if (c == fmt->fs && !need_quotes)
326                                 warn(fmt, "Field separator found inside field and quoting is turned off.");
327                         if (c == fmt->quote)
328                                 putchar_unlocked(c);
329                         putchar_unlocked(c);
330                 }
331                 if (need_quotes)
332                         putchar_unlocked(fmt->quote);
333         }
334         putchar_unlocked('\n');
335 }
336
337 /*** White-space back-end ***/
338
339 static int ws_read(struct format *fmt)
340 {
341         if (!next_line())
342                 return 0;
343
344         unsigned char *line = line_first(&in_line);
345         int n = line_count(&in_line);
346         if (!n)
347                 return 1;
348
349         int ws = 0;
350         new_field(0);
351         for (int i=0; i<n; i++) {
352                 int c = line[i];
353                 if (is_ws(c)) {
354                         ws++;
355                 } else {
356                         if (ws) {
357                                 if (!in_field->start_pos &&
358                                     !in_field->len &&
359                                     !fmt->strict_ws)
360                                         in_field->start_pos = i;
361                                 else
362                                         new_field(i);
363                                 ws = 0;
364                         }
365                         in_field->len++;
366                 }
367         }
368
369         if (ws && fmt->strict_ws)
370                 new_field(n);
371         return 1;
372 }
373
374 /*** Regex back-end ***/
375
376 static const char *regex_set(struct format *f, char *rx)
377 {
378         const char *err;
379         int errpos;
380         f->pcre = pcre_compile(rx, PCRE_DOLLAR_ENDONLY, &err, &errpos, NULL);
381         if (!f->pcre)
382                 return err;
383
384         f->pcre_extra = pcre_study(f->pcre, 0, &err);
385         if (!f->pcre_extra)
386                 return err;
387
388         return NULL;
389 }
390
391 static int regex_read(struct format *fmt)
392 {
393         if (!next_line())
394                 return 0;
395
396         unsigned char *c = line_first(&in_line);
397         int n = line_count(&in_line);
398         if (!n)
399                 return 1;
400
401         int i = 0;
402         for (;;) {
403                 int ovec[3];
404                 int sep = pcre_exec(fmt->pcre, fmt->pcre_extra, (char *) c, n, i, 0, ovec, 3);
405                 if (sep < 0) {
406                         if (sep != PCRE_ERROR_NOMATCH)
407                                 warn(fmt, "PCRE matching error %d", sep);
408                         // No further occurrence of the separator: the rest is a single field
409                         new_field(i);
410                         in_field->len = n - i;
411                         return 1;
412                 }
413                 new_field(i);
414                 in_field->len = ovec[0] - i;
415                 i = ovec[1];
416         }
417 }
418
419 /*** Table back-end ***/
420
421 static void table_write(struct format *fmt)
422 {
423         for (int i = 0; i < intarray_count(&column_widths); i++) {
424                 if (fmt->table_grid) {
425                         putchar_unlocked('|');
426                         printf("%*s", fmt->table_sep / 2, "");
427                 } else if (i)
428                         printf("%*s", fmt->table_sep, "");
429
430                 int cw = *intarray_nth(&column_widths, i);
431                 int fw = 0;
432                 if (i < fields_count(&out_fields)) {
433                         int len;
434                         unsigned char *p = get_field(&out_fields, i, &len);
435                         fw = field_chars(fields_nth(&out_fields, i));
436                         if (fw > cw) {
437                                 warn(fmt, "Internal error: Wrongly calculated column width (%d > %d)", fw, cw);
438                                 cw = fw;
439                         }
440                         while (len--)
441                                 putchar(*p++);
442                 }
443                 while (fw < cw) {
444                         putchar_unlocked(' ');
445                         fw++;
446                 }
447
448                 if (fmt->table_grid)
449                         printf("%*s", fmt->table_sep - fmt->table_sep / 2, "");
450         }
451
452         if (fmt->table_grid)
453                 putchar_unlocked('|');
454         putchar_unlocked('\n');
455 }
456
457 static void table_write_grid(struct format *fmt, int pos UNUSED)
458 {
459         if (!fmt->table_grid)
460                 return;
461
462         for (int i = 0; i < intarray_count(&column_widths); i++) {
463                 putchar_unlocked('+');
464                 int w = fmt->table_sep + *intarray_nth(&column_widths, i);
465                 while (w--)
466                         putchar('-');
467         }
468         putchar_unlocked('+');
469         putchar_unlocked('\n');
470 }
471
472 /*** Temporary file back-end ***/
473
474 static int tmp_read(struct format *fmt)
475 {
476         FILE *tf = fmt->tmp_file;
477
478         for (;;) {
479                 int c = getc_unlocked(tf);
480                 if (c < 0)
481                         return 0;
482                 if (c == 0xff)
483                         return 1;
484                 if (c == 0xfe) {
485                         c = getc_unlocked(tf);
486                         c = (c << 8) | getc_unlocked(tf);
487                         c = (c << 8) | getc_unlocked(tf);
488                         c = (c << 8) | getc_unlocked(tf);
489                 }
490                 new_field(line_count(&in_line));
491                 in_field->len = c;
492                 while (c--) {
493                         int x = getc_unlocked(tf);
494                         if (x < 0) {
495                                 warn(fmt, "Truncated temporary file");
496                                 return 0;
497                         }
498                         *line_push(&in_line) = x;
499                 }
500         }
501
502         if (ferror_unlocked(tf))
503                 die("I/O error when reading temporary file");
504 }
505
506 static void tmp_write(struct format *fmt)
507 {
508         FILE *tf = fmt->tmp_file;
509
510         for (int i = 0; i < fields_count(&out_fields); i++) {
511                 int len;
512                 unsigned char *p = get_field(&out_fields, i, &len);
513
514                 if (len < 0xfe)
515                         putc_unlocked(len, tf);
516                 else {
517                         putc_unlocked(0xfe, tf);
518                         putc_unlocked((len >> 24) & 0xff, tf);
519                         putc_unlocked((len >> 16) & 0xff, tf);
520                         putc_unlocked((len >> 8) & 0xff, tf);
521                         putc_unlocked(len & 0xff, tf);
522                 }
523
524                 while (len--)
525                         putc_unlocked(*p++, tf);
526         }
527         putc_unlocked(0xff, tf);
528
529         if (ferror_unlocked(tf))
530                 die("I/O error when writing temporary file");
531 }
532
533 /*** Transforms ***/
534
535 static void trim_fields(void)
536 {
537         unsigned char *line = line_first(&in_line);
538         for (int i = 0; i < fields_count(&in_fields); i++) {
539                 struct field *f = fields_nth(&in_fields, i);
540                 while (f->len && is_ws(line[f->start_pos]))
541                         f->start_pos++, f->len--;
542                 while (f->len && is_ws(line[f->start_pos + f->len - 1]))
543                         f->len--;
544         }
545 }
546
547 /*** Field names and headers ***/
548
549 struct field_names {
550         stringarray_t names;
551 };
552
553 static void add_field(struct field_names *fn, char *name, int namelen)
554 {
555         char *n = xmalloc(namelen + 1);
556         memcpy(n, name, namelen);
557         n[namelen] = 0;
558         *stringarray_push(&fn->names) = n;
559 }
560
561 static void add_field_names(struct field_names *fn, char *names)
562 {
563         char *p = names;
564         while (p) {
565                 char *q = strchr(p, ',');
566                 int len = q ? q-p : (int) strlen(p);
567                 add_field(fn, p, len);
568                 p = q ? q+1 : NULL;
569         }
570 }
571
572 static void read_header(void)
573 {
574         if (!(in_format->has_header || in_format->set_field_names))
575                 return;
576
577         struct field_names *fn = xmalloc_zero(sizeof(*fn));
578         in_format->field_names = fn;
579
580         if (in_format->has_header) {
581                 if (!read_line())
582                         die("Missing input header");
583         }
584
585         if (in_format->set_field_names) {
586                 add_field_names(fn, in_format->set_field_names);
587         } else {
588                 for (int i = 0; i < fields_count(&in_fields); i++) {
589                         int len;
590                         char *s = (char *) get_field(&in_fields, i, &len);
591                         add_field(fn, s, len);
592                 }
593         }
594 }
595
596 static void write_header(void)
597 {
598         if (!out_format->has_header) {
599                 write_grid(-1);
600                 return;
601         }
602
603         if (out_format->set_field_names) {
604                 struct field_names *fn = xmalloc_zero(sizeof(*fn));
605                 out_format->field_names = fn;
606                 add_field_names(fn, out_format->set_field_names);
607         } else if (in_format->field_names)
608                 out_format->field_names = in_format->field_names;
609         else
610                 die("Output header requested, but no field names specified");
611
612         line_reset(&in_line);
613         fields_reset(&out_fields);
614         struct field_names *fn = out_format->field_names;
615         for (int i = 0; i < stringarray_count(&fn->names); i++) {
616                 struct field *f = fields_push(&out_fields);
617                 f->start_pos = line_count(&in_line);
618                 f->len = 0;
619                 char *s = *stringarray_nth(&fn->names, i);
620                 while (*s) {
621                         *line_push(&in_line) = *s++;
622                         f->len++;
623                 }
624         }
625
626         // This is tricky: when we are formatting a table, field names are normally
627         // calculated in pass 1, but the header is written in pass 2, so we have to
628         // update column statistics, because field name can be too wide to fit.
629         update_stats();
630         write_grid(-1);
631         write_line();
632         write_grid(0);
633 }
634
635 static void write_footer(void)
636 {
637         write_grid(1);
638 }
639
640 static int find_field_by_name(struct field_names *fn, char *name)
641 {
642         for (int i = 0; i < stringarray_count(&fn->names); i++)
643                 if (!strcmp(*stringarray_nth(&fn->names, i), name))
644                         return i + 1;
645         return -1;
646 }
647
648 /*** Field selection ***/
649
650 struct selector {
651         int first_field, last_field;            // 0 means "boundary"
652 };
653
654 DECLARE_BUF(selectors, struct selector);
655 static selectors_t selectors;
656
657 static int parse_field_num(char *str)
658 {
659         int f = 0;
660
661         while (*str) {
662                 if (*str < '0' || *str > '9')
663                         return -1;
664                 if (f >= 100000000)
665                         return -1;
666                 f = 10*f + *str - '0';
667                 str++;
668         }
669         return f;
670 }
671
672 static int parse_field(char *str)
673 {
674         if (!*str)
675                 return 0;
676
677         int f = parse_field_num(str);
678         if (f > 0)
679                 return f;
680
681         if (in_format->field_names && (f = find_field_by_name(in_format->field_names, str)) > 0)
682                 return f;
683
684         die("Unknown field %s", str);
685 }
686
687 static char *parse_selector(char *str)
688 {
689         char buf[strlen(str) + 1];
690         strcpy(buf, str);
691
692         struct selector *s = selectors_push(&selectors);
693         char *sep = strchr(buf, '-');
694         if (sep) {
695                 *sep++ = 0;
696                 s->first_field = parse_field(buf);
697                 s->last_field = parse_field(sep);
698         } else
699                 s->first_field = s->last_field = parse_field(buf);
700
701         return NULL;
702 }
703
704 static void finish_parse_selectors(void)
705 {
706         if (!selectors_count(&selectors))
707                 parse_selector("-");
708 }
709
710 static void select_fields(void)
711 {
712         for (int i = 0; i < selectors_count(&selectors); i++) {
713                 struct selector *s = selectors_nth(&selectors, i);
714                 int first = s->first_field;
715                 if (first <= 0)
716                         first = 1;
717                 int last = s->last_field;
718                 if (last <= 0)
719                         last = fields_count(&in_fields);
720                 for (int j = first; j <= last; j++) {
721                         struct field *f = fields_push(&out_fields);
722                         if (j >= 1 && j <= fields_count(&in_fields))
723                                 *f = *fields_nth(&in_fields, j-1);
724                         else
725                                 f->start_pos = f->len = 0;
726                 }
727         }
728 }
729
730 static void select_all_fields(void)
731 {
732         for (int i = 0; i < fields_count(&in_fields); i++)
733                 *fields_push(&out_fields) = *fields_nth(&in_fields, i);
734 }
735
736 /*** Processing of files ***/
737
738 static void one_pass(int pass)
739 {
740         if (pass & 2)
741                 write_header();
742
743         for (;;) {
744                 line_number++;
745                 if (!read_line())
746                         break;
747
748                 if (want_trim && (pass & 1))
749                         trim_fields();
750
751                 fields_reset(&out_fields);
752                 if (pass & 1)
753                         select_fields();
754                 else
755                         select_all_fields();
756
757                 if (out_format->needs_stats)
758                         update_stats();
759
760                 write_line();
761         }
762
763         if (pass & 2)
764                 write_footer();
765 }
766
767 static void two_pass(void)
768 {
769         struct format *final_format = out_format;
770
771         // We need to use character set info from the current locale
772         setlocale(LC_CTYPE, "");
773
774         // Pass 1: Set up writer of intermediate format
775         out_format = xmalloc_zero(sizeof(*out_format));
776         out_format->id = FORM_TMP;
777         out_format->read_line = tmp_read;
778         out_format->write_line = tmp_write;
779         out_format->tmp_file = tmpfile();
780         out_format->needs_stats = final_format->needs_stats;
781         out_format->field_names = in_format->field_names;
782         one_pass(1);
783
784         // Pass 2: Set up reader of intermediate format
785         in_format = out_format;
786         rewind(in_format->tmp_file);
787         line_number = 0;
788         out_format = final_format;
789         out_format->needs_stats = 0;
790         one_pass(2);
791         fclose(in_format->tmp_file);
792 }
793
794 /*** Parsing of arguments ***/
795
796 static void NONRET usage(void)
797 {
798         printf("\
799 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
800 \n\
801 Formats:\n\
802 -t, --tsv               TAB-separated values (default)\n\
803 -c, --csv               Comma-separated values\n\
804 -w, --ws                Values separated by arbitrary whitespace\n\
805 -W, --strict-ws         Like --ws, but recognize empty columns at start/end\n\
806 -r, --regex=<rx>        Separator given by Perl regular expression (input only)\n\
807     --table             Format a table (output only)\n\
808 \n\
809 Format parameters:\n\
810 -d, --fs=<char>         Delimiter of fields\n\
811 -f, --fields=<f>,...    Set field names\n\
812 -h, --header            The first line contains field names\n\
813 -q, --quiet             Do not show warnings\n\
814     --always-quote      Put quotes around all fields (CSV output only)\n\
815     --table-sep=<n>     Separate table columns by <n> spaces (default: 2)\n\
816     --grid              Separate table columns by grid lines\n\
817 \n\
818 Other options:\n\
819     --trim              Trim leading and trailing whitespaces in fields\n\
820 ");
821         exit(0);
822 }
823
824 static void NONRET bad_args(const char *msg, ...)
825 {
826         if (msg) {
827                 va_list args;
828                 va_start(args, msg);
829                 fprintf(stderr, "xsv: ");
830                 vfprintf(stderr, msg, args);
831                 fputc('\n', stderr);
832                 va_end(args);
833         }
834         fprintf(stderr, "Try `xsv --help' for more information.\n");
835         exit(1);
836 }
837
838 static const char short_options[] = "cd:f:hqr:twW";
839
840 enum long_options {
841         OPT_HELP = 256,
842         OPT_TRIM,
843         OPT_ALWAYS_QUOTE,
844         OPT_TABLE,
845         OPT_TABLE_SEP,
846         OPT_GRID,
847 };
848
849 static const struct option long_options[] = {
850         { "always-quote",       0,      NULL,   OPT_ALWAYS_QUOTE },
851         { "csv",                0,      NULL,   'c' },
852         { "fields",             1,      NULL,   'f' },
853         { "fs",                 1,      NULL,   'd' },
854         { "grid",               0,      NULL,   OPT_GRID },
855         { "header",             0,      NULL,   'h' },
856         { "quiet",              0,      NULL,   'q' },
857         { "regex",              1,      NULL,   'r' },
858         { "strict-ws",          0,      NULL,   'W' },
859         { "table",              0,      NULL,   OPT_TABLE },
860         { "table-sep",          1,      NULL,   OPT_TABLE_SEP },
861         { "trim",               0,      NULL,   OPT_TRIM },
862         { "tsv",                0,      NULL,   't' },
863         { "ws",                 0,      NULL,   'w' },
864         { "help",               0,      NULL,   OPT_HELP },
865         { NULL,                 0,      NULL,   0 },
866 };
867
868 static void set_format(int format_id)
869 {
870         struct format *f = xmalloc_zero(sizeof(*f));
871         f->id = format_id;
872
873         switch (format_id) {
874                 case FORM_TSV:
875                         f->fs = '\t';
876                         f->quote = -1;
877                         f->read_line = csv_read;
878                         f->write_line = csv_write;
879                         break;
880                 case FORM_CSV:
881                         f->fs = ',';
882                         f->quote = '"';
883                         f->read_line = csv_read;
884                         f->write_line = csv_write;
885                         break;
886                 case FORM_WS:
887                         f->fs = ' ';
888                         f->quote = -1;
889                         f->read_line = ws_read;
890                         f->write_line = csv_write;
891                         break;
892                 case FORM_REGEX:
893                         f->read_line = regex_read;
894                         break;
895                 case FORM_TABLE:
896                         f->write_line = table_write;
897                         f->write_grid = table_write_grid;
898                         f->needs_stats = 1;
899                         f->table_sep = 2;
900                         break;
901         }
902
903         if (!in_format)
904                 in_format = f;
905         else if (!out_format)
906                 out_format = f;
907         else
908                 bad_args("At most two formats may be given.");
909 }
910
911 static struct format *current_format(void)
912 {
913         if (out_format)
914                 return out_format;
915         if (in_format)
916                 return in_format;
917         set_format(FORM_TSV);
918         return in_format;
919 }
920
921 int main(int argc, char **argv)
922 {
923         int opt;
924         const char *err;
925
926         while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
927                 switch (opt) {
928                         case 'c':
929                                 set_format(FORM_CSV);
930                                 break;
931                         case 'd':
932                                 if (optarg[0])
933                                         current_format()->fs = optarg[0];
934                                 else
935                                         bad_args("No field delimiter given.");
936                                 break;
937                         case 'f':
938                                 current_format()->set_field_names = optarg;
939                                 break;
940                         case 'h':
941                                 current_format()->has_header = 1;
942                                 break;
943                         case 'q':
944                                 current_format()->quiet = 1;
945                                 break;
946                         case 'r':
947                                 set_format(FORM_REGEX);
948                                 err = regex_set(current_format(), optarg);
949                                 if (err)
950                                         bad_args("Error compiling regex: %s", err);
951                                 break;
952                         case 't':
953                                 set_format(FORM_TSV);
954                                 break;
955                         case 'w':
956                                 set_format(FORM_WS);
957                                 break;
958                         case 'W':
959                                 set_format(FORM_WS);
960                                 current_format()->strict_ws = 1;
961                                 break;
962                         case OPT_ALWAYS_QUOTE:
963                                 if (current_format()->id != FORM_CSV)
964                                         bad_args("--always-quote makes sense only for CSV.");
965                                 current_format()->always_quote = 1;
966                                 break;
967                         case OPT_HELP:
968                                 usage();
969                         case OPT_TRIM:
970                                 want_trim = 1;
971                                 break;
972                         case OPT_TABLE:
973                                 set_format(FORM_TABLE);
974                                 break;
975                         case OPT_TABLE_SEP:
976                                 current_format()->table_sep = atoi(optarg);
977                                 break;
978                         case OPT_GRID:
979                                 current_format()->table_grid = 1;
980                                 break;
981                         default:
982                                 bad_args(NULL);
983                 }
984
985         current_format();
986         if (!out_format)
987                 out_format = in_format;
988         if (!in_format->read_line)
989                 bad_args("Write-only format selected for input.");
990         if (!out_format->write_line)
991                 bad_args("Read-only format selected for output.");
992         read_header();
993
994         for (int i = optind; i < argc; i++) {
995                 err = parse_selector(argv[i]);
996                 if (err)
997                         bad_args(err);
998         }
999         finish_parse_selectors();
1000
1001         if (out_format->needs_stats)
1002                 two_pass();
1003         else
1004                 one_pass(3);
1005         return 0;
1006 }