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