]> mj.ucw.cz Git - xsv.git/blob - xsv.c
Introduced die() and NONRET
[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 #else
22 #define NONRET
23 #endif
24
25 /*** General functions ***/
26
27 static void NONRET die(char *msg, ...)
28 {
29         va_list args;
30         va_start(args, msg);
31         fprintf(stderr, "xsv: ");
32         vfprintf(stderr, msg, args);
33         fputc('\n', stderr);
34         va_end(args);
35         exit(1);
36 }
37
38 /*** Memory allocation ***/
39
40 static void *xmalloc(size_t bytes)
41 {
42         void *p = malloc(bytes);
43         if (!p)
44                 die("Out of memory (cannot allocate %zu bytes)", bytes);
45         return p;
46 }
47
48 static void *xmalloc_zero(size_t bytes)
49 {
50         void *p = xmalloc(bytes);
51         memset(p, 0, bytes);
52         return p;
53 }
54
55 static void *xrealloc(void *old, size_t bytes)
56 {
57         void *p = realloc(old, bytes);
58         if (!p)
59                 die("Out of memory (cannot allocate %zu bytes)", bytes);
60         return p;
61 }
62
63 #define DECLARE_BUF(name, type) \
64         typedef struct { type *start; int count; int max; } name##_t;                           \
65         static inline void name##_init(name##_t *b) { b->start = NULL; b->count = b->max = 0; } \
66         static inline void name##_reset(name##_t *b) { b->count = 0; }                          \
67         static inline int name##_count(name##_t *b) { return b->count; }                        \
68         static void name##_extend(name##_t *b) {                                                \
69                 b->max = b->max ? 2*b->max : 16;                                                \
70                 b->start = xrealloc(b->start, b->max * sizeof(type));                           \
71         }                                                                                       \
72         static inline type *name##_push(name##_t *b) {                                          \
73                 if (b->count >= b->max) name##_extend(b);                                       \
74                 return &b->start[b->count++];                                                   \
75         }                                                                                       \
76         static inline type *name##_first(name##_t *b) { return b->start; }                      \
77         static inline type *name##_nth(name##_t *b, int n) { return &b->start[n]; }             \
78         // end
79
80 DECLARE_BUF(intarray, int);
81
82 /*** Formats and their parameters ***/
83
84 enum format_id {
85         FORM_UNSPEC,
86         FORM_TSV,
87         FORM_CSV,
88         FORM_WS,
89         FORM_REGEX,
90         FORM_TMP,
91         FORM_TABLE,
92 };
93
94 struct format {
95         enum format_id id;
96         int fs;
97         int quote;
98         int quiet;
99         int (*read_line)(void);
100         void (*write_line)(void);
101         int needs_two_passes;
102
103         // CSV backend:
104         int always_quote;
105
106         // WS backend:
107         int strict_ws;
108
109         // regex backend:
110         pcre *pcre;
111         pcre_extra *pcre_extra;
112
113         // Temporary file backend:
114         FILE *tmp_file;
115         intarray_t column_widths;
116
117         // Table backend:
118         int table_sep;
119 };
120
121 static struct format *in_format, *out_format;
122 static int want_trim;
123
124 struct field {
125         int start_pos;
126         int len;
127 };
128
129 DECLARE_BUF(fields, struct field);
130 DECLARE_BUF(line, unsigned char);
131
132 static fields_t in_fields, out_fields;
133 static struct field *in_field;
134 static line_t in_line;
135 static int line_number;
136
137 static void new_field(int pos)
138 {
139         in_field = fields_push(&in_fields);
140         in_field->start_pos = pos;
141         in_field->len = 0;
142 }
143
144 static void ensure_field(int pos)
145 {
146         if (!in_field)
147                 new_field(pos);
148 }
149
150 static void warn(struct format *fmt, char *msg, ...)
151 {
152         if (!fmt->quiet) {
153                 fprintf(stderr, "Warning at line %d: ", line_number);
154                 va_list args;
155                 va_start(args, msg);
156                 vfprintf(stderr, msg, args);
157                 va_end(args);
158                 fputc('\n', stderr);
159         }
160 }
161
162 static int next_line(void)
163 {
164         for (;;) {
165                 int c = getchar_unlocked();
166                 if (c == '\r')
167                         continue;
168                 if (c < 0)
169                         return !!line_count(&in_line);
170                 if (c == '\n')
171                         return 1;
172                 *line_push(&in_line) = c;
173         }
174 }
175
176 static int field_chars(struct field *f)
177 {
178         unsigned char *s = line_nth(&in_line, f->start_pos);
179         int i = 0;
180         mbstate_t mbs;
181         memset(&mbs, 0, sizeof(mbs));
182
183         int chars = 0;
184         while (i < f->len) {
185                 size_t k = mbrlen((char *) s + i, f->len - i, &mbs);
186                 if ((int) k <= 0)
187                         break;
188                 i += k;
189                 chars++;
190         }
191
192         return chars;
193 }
194
195 /*** CSV/TSV back-end */
196
197 static int csv_read(void)
198 {
199         int quoted = 0;
200         for (;;) {
201                 int c = getchar_unlocked();
202                 int i = line_count(&in_line);
203 restart:
204                 if (c == '\r')
205                         continue;
206                 if (c < 0 || c == '\n') {
207                         if (quoted)
208                                 warn(in_format, "Missing closing quote.");
209                         if (c < 0)
210                                 return !!fields_count(&in_fields);
211                         else
212                                 return 1;
213                 }
214                 if (quoted) {
215                         if (c == in_format->quote) {
216                                 c = getchar_unlocked();
217                                 if (c != in_format->quote) {
218                                         quoted = 0;
219                                         goto restart;
220                                 }
221                                 // Two quotes assimilate to one
222                         }
223                         // Fall through to pushing the character
224                 } else if (c == in_format->quote) {
225                         quoted = 1;
226                         continue;
227                 } else if (c == in_format->fs && !quoted) {
228                         ensure_field(i);
229                         new_field(i);
230                         continue;
231                 }
232                 ensure_field(i);
233                 *line_push(&in_line) = c;
234                 in_field->len++;
235         }
236 }
237
238 static int is_ws(int c)
239 {
240         return (c == ' ' || c == '\t' || c == '\f');
241 }
242
243 static void csv_write(void)
244 {
245         unsigned char *line = line_first(&in_line);
246         int n = fields_count(&out_fields);
247         for (int i=0; i<n; i++) {
248                 struct field *f = fields_nth(&out_fields, i);
249                 int need_quotes = 0;
250                 if (out_format->quote >= 0) {
251                         need_quotes = out_format->always_quote;
252                         for (int j=0; !need_quotes && j < f->len; j++) {
253                                 int c = line[f->start_pos + j];
254                                 if (c == out_format->fs || c == out_format->quote)
255                                         need_quotes = 1;
256                         }
257                 }
258                 if (i)
259                         putchar_unlocked(out_format->fs);
260                 if (need_quotes)
261                         putchar_unlocked(out_format->quote);
262                 for (int j=0; j < f->len; j++) {
263                         int c = line[f->start_pos + j];
264                         if (c == out_format->fs && !need_quotes)
265                                 warn(out_format, "Field separator found inside field and quoting is turned off.");
266                         if (c == out_format->quote)
267                                 putchar_unlocked(c);
268                         putchar_unlocked(c);
269                 }
270                 if (need_quotes)
271                         putchar_unlocked(out_format->quote);
272         }
273         putchar_unlocked('\n');
274 }
275
276 /*** White-space back-end ***/
277
278 static int ws_read(void)
279 {
280         if (!next_line())
281                 return 0;
282
283         unsigned char *line = line_first(&in_line);
284         int n = line_count(&in_line);
285         if (!n)
286                 return 1;
287
288         int ws = 0;
289         new_field(0);
290         for (int i=0; i<n; i++) {
291                 int c = line[i];
292                 if (is_ws(c)) {
293                         ws++;
294                 } else {
295                         if (ws) {
296                                 if (!in_field->start_pos &&
297                                     !in_field->len &&
298                                     !in_format->strict_ws)
299                                         in_field->start_pos = i;
300                                 else
301                                         new_field(i);
302                                 ws = 0;
303                         }
304                         in_field->len++;
305                 }
306         }
307
308         if (ws && in_format->strict_ws)
309                 new_field(n);
310         return 1;
311 }
312
313 /*** Regex back-end ***/
314
315 static const char *regex_set(struct format *f, char *rx)
316 {
317         const char *err;
318         int errpos;
319         f->pcre = pcre_compile(rx, PCRE_DOLLAR_ENDONLY, &err, &errpos, NULL);
320         if (!f->pcre)
321                 return err;
322
323         f->pcre_extra = pcre_study(f->pcre, 0, &err);
324         if (!f->pcre_extra)
325                 return err;
326
327         return NULL;
328 }
329
330 static int regex_read(void)
331 {
332         if (!next_line())
333                 return 0;
334
335         unsigned char *c = line_first(&in_line);
336         int n = line_count(&in_line);
337         if (!n)
338                 return 1;
339
340         int i = 0;
341         for (;;) {
342                 int ovec[3];
343                 int sep = pcre_exec(in_format->pcre, in_format->pcre_extra, (char *) c, n, i, 0, ovec, 3);
344                 if (sep < 0) {
345                         if (sep != PCRE_ERROR_NOMATCH)
346                                 warn(in_format, "PCRE matching error %d", sep);
347                         // No further occurrence of the separator: the rest is a single field
348                         new_field(i);
349                         in_field->len = n - i;
350                         return 1;
351                 }
352                 new_field(i);
353                 in_field->len = ovec[0] - i;
354                 i = ovec[1];
355         }
356 }
357
358 /*** Table back-end ***/
359
360 static void table_write(void)
361 {
362         for (int i = 0; i < fields_count(&in_fields); i++) {
363                 if (i)
364                         printf("%*s", out_format->table_sep, "");
365                 struct field *f = fields_nth(&in_fields, i);
366                 int fw = field_chars(f);
367                 int cw = *intarray_nth(&in_format->column_widths, i);
368                 if (fw > cw) {
369                         warn(out_format, "Internal error: Wrongly calculated column width (%d > %d)", fw, cw);
370                         cw = fw;
371                 }
372                 unsigned char *p = line_nth(&in_line, f->start_pos);
373                 for (int j = 0; j < f->len; j++)
374                         putchar_unlocked(p[j]);
375                 while (fw < cw) {
376                         putchar_unlocked(' ');
377                         fw++;
378                 }
379         }
380         putchar_unlocked('\n');
381 }
382
383 /*** Temporary file back-end ***/
384
385 static int tmp_read(void)
386 {
387         FILE *tf = in_format->tmp_file;
388
389         for (;;) {
390                 int c = getc_unlocked(tf);
391                 if (c < 0)
392                         return 0;
393                 if (c == 0xff)
394                         return 1;
395                 if (c == 0xfe) {
396                         c = getc_unlocked(tf);
397                         c = (c << 8) | getc_unlocked(tf);
398                         c = (c << 8) | getc_unlocked(tf);
399                         c = (c << 8) | getc_unlocked(tf);
400                 }
401                 new_field(line_count(&in_line));
402                 in_field->len = c;
403                 while (c--) {
404                         int x = getc_unlocked(tf);
405                         if (x < 0) {
406                                 warn(in_format, "Truncated temporary file");
407                                 return 0;
408                         }
409                         *line_push(&in_line) = x;
410                 }
411         }
412 }
413
414 static void tmp_write(void)
415 {
416         FILE *tf = out_format->tmp_file;
417
418         for (int i = 0; i < fields_count(&in_fields); i++) {
419                 struct field *f = fields_nth(&in_fields, i);
420                 if (f->len < 0xfe)
421                         putc_unlocked(f->len, tf);
422                 else {
423                         putc_unlocked(0xfe, tf);
424                         putc_unlocked((f->len >> 24) & 0xff, tf);
425                         putc_unlocked((f->len >> 16) & 0xff, tf);
426                         putc_unlocked((f->len >> 8) & 0xff, tf);
427                         putc_unlocked(f->len & 0xff, tf);
428                 }
429
430                 unsigned char *p = line_nth(&in_line, f->start_pos);
431                 for (int j = 0; j < f->len; j++)
432                         putc_unlocked(*p++, tf);
433
434                 intarray_t *w = &out_format->column_widths;
435                 while (i >= intarray_count(w))
436                         *intarray_push(w) = 0;
437                 int fw = field_chars(f);
438                 if (*intarray_nth(w, i) < fw)
439                         *intarray_nth(w, i) = fw;
440         }
441         putc_unlocked(0xff, tf);
442 }
443
444 /*** Transforms ***/
445
446 static void trim_fields(void)
447 {
448         unsigned char *line = line_first(&in_line);
449         for (int i = 0; i < fields_count(&in_fields); i++) {
450                 struct field *f = fields_nth(&in_fields, i);
451                 while (f->len && is_ws(line[f->start_pos]))
452                         f->start_pos++, f->len--;
453                 while (f->len && is_ws(line[f->start_pos + f->len - 1]))
454                         f->len--;
455         }
456 }
457
458 /*** Field selection ***/
459
460 struct selector {
461         int first_field, last_field;
462 };
463
464 DECLARE_BUF(selectors, struct selector);
465 static selectors_t selectors;
466
467 static char *parse_selector(char *str)
468 {
469         char buf[strlen(str) + 1];
470         strcpy(buf, str);
471
472         struct selector *s = selectors_push(&selectors);
473         char *sep = strchr(buf, '-');
474         if (sep) {
475                 *sep++ = 0;
476                 s->first_field = atoi(buf);
477                 s->last_field = atoi(sep);
478         } else
479                 s->first_field = s->last_field = atoi(buf);
480
481         return NULL;
482 }
483
484 static void finish_parse_selectors(void)
485 {
486         if (!selectors_count(&selectors))
487                 parse_selector("-");
488 }
489
490 static void select_fields(void)
491 {
492         for (int i = 0; i < selectors_count(&selectors); i++) {
493                 struct selector *s = selectors_nth(&selectors, i);
494                 int first = s->first_field;
495                 if (first <= 0)
496                         first = 1;
497                 int last = s->last_field;
498                 if (last <= 0)
499                         last = fields_count(&in_fields);
500                 for (int j = first; j <= last; j++) {
501                         struct field *f = fields_push(&out_fields);
502                         if (j >= 1 && j <= fields_count(&in_fields))
503                                 *f = *fields_nth(&in_fields, j-1);
504                         else
505                                 f->start_pos = f->len = 0;
506                 }
507         }
508 }
509
510 /*** Processing of files ***/
511
512 static void one_pass(void)
513 {
514         line_number = 0;
515         for (;;) {
516                 line_number++;
517                 fields_reset(&in_fields);
518                 line_reset(&in_line);
519                 in_field = NULL;
520                 if (!in_format->read_line())
521                         break;
522
523                 if (want_trim)
524                         trim_fields();
525
526                 fields_reset(&out_fields);
527                 select_fields();
528
529                 out_format->write_line();
530         }
531 }
532
533 static void two_pass(void)
534 {
535         struct format *final_format = out_format;
536
537         // We need to use character set info from the current locale
538         setlocale(LC_CTYPE, "");
539
540         // Pass 1: Set up writer of intermediate format
541         out_format = xmalloc_zero(sizeof(*out_format));
542         out_format->id = FORM_TMP;
543         out_format->read_line = tmp_read;
544         out_format->write_line = tmp_write;
545         out_format->tmp_file = tmpfile();
546         intarray_init(&out_format->column_widths);
547         one_pass();
548
549         // Pass 2: Set up reader of intermediate format
550         in_format = out_format;
551         rewind(in_format->tmp_file);
552         out_format = final_format;
553         one_pass();
554         fclose(in_format->tmp_file);
555 }
556
557 /*** Parsing of arguments ***/
558
559 static void NONRET usage(void)
560 {
561         printf("\
562 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
563 \n\
564 Formats:\n\
565 -t, --tsv               TAB-separated values (default)\n\
566 -c, --csv               Comma-separated values\n\
567 -w, --ws                Values separated by arbitrary whitespace\n\
568 -W, --strict-ws         Like --ws, but recognize empty columns at start/end\n\
569 -r, --regex=<rx>        Separator given by Perl regular expression (input only)\n\
570     --table             Format a table (output only)\n\
571 \n\
572 Format parameters:\n\
573 -d, --fs=<char>         Delimiter of fields\n\
574 -q, --quiet             Do not show warnings\n\
575     --always-quote      Put quotes around all fields (CSV output only)\n\
576     --table-sep=<n>     Separate table columns by <n> spaces (default: 2)\n\
577 \n\
578 Other options:\n\
579     --trim              Trim leading and trailing whitespaces in fields\n\
580 ");
581         exit(0);
582 }
583
584 static void NONRET bad_args(const char *msg, ...)
585 {
586         if (msg) {
587                 va_list args;
588                 va_start(args, msg);
589                 fprintf(stderr, "xsv: ");
590                 vfprintf(stderr, msg, args);
591                 fputc('\n', stderr);
592                 va_end(args);
593         }
594         fprintf(stderr, "Try `xsv --help' for more information.\n");
595         exit(1);
596 }
597
598 static const char short_options[] = "cd:qr:twW";
599
600 enum long_options {
601         OPT_HELP = 256,
602         OPT_TRIM,
603         OPT_ALWAYS_QUOTE,
604         OPT_TABLE,
605         OPT_TABLE_SEP,
606 };
607
608 static const struct option long_options[] = {
609         { "always-quote",       0,      NULL,   OPT_ALWAYS_QUOTE },
610         { "csv",                0,      NULL,   'c' },
611         { "fs",                 1,      NULL,   'd' },
612         { "quiet",              0,      NULL,   'q' },
613         { "regex",              1,      NULL,   'r' },
614         { "strict-ws",          0,      NULL,   'W' },
615         { "table",              0,      NULL,   OPT_TABLE },
616         { "table-sep",          1,      NULL,   OPT_TABLE_SEP },
617         { "trim",               0,      NULL,   OPT_TRIM },
618         { "tsv",                0,      NULL,   't' },
619         { "ws",                 0,      NULL,   'w' },
620         { "help",               0,      NULL,   OPT_HELP },
621         { NULL,                 0,      NULL,   0 },
622 };
623
624 static void set_format(int format_id)
625 {
626         struct format *f = xmalloc_zero(sizeof(*f));
627         f->id = format_id;
628
629         switch (format_id) {
630                 case FORM_TSV:
631                         f->fs = '\t';
632                         f->quote = -1;
633                         f->read_line = csv_read;
634                         f->write_line = csv_write;
635                         break;
636                 case FORM_CSV:
637                         f->fs = ',';
638                         f->quote = '"';
639                         f->read_line = csv_read;
640                         f->write_line = csv_write;
641                         break;
642                 case FORM_WS:
643                         f->fs = ' ';
644                         f->quote = -1;
645                         f->read_line = ws_read;
646                         f->write_line = csv_write;
647                         break;
648                 case FORM_REGEX:
649                         f->read_line = regex_read;
650                         break;
651                 case FORM_TABLE:
652                         f->write_line = table_write;
653                         f->needs_two_passes = 1;
654                         f->table_sep = 2;
655                         break;
656         }
657
658         if (!in_format)
659                 in_format = f;
660         else if (!out_format)
661                 out_format = f;
662         else
663                 bad_args("At most two formats may be given.");
664 }
665
666 static struct format *current_format(void)
667 {
668         if (out_format)
669                 return out_format;
670         if (in_format)
671                 return in_format;
672         set_format(FORM_TSV);
673         return in_format;
674 }
675
676 int main(int argc, char **argv)
677 {
678         int opt;
679         const char *err;
680
681         while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
682                 switch (opt) {
683                         case 'c':
684                                 set_format(FORM_CSV);
685                                 break;
686                         case 'd':
687                                 if (optarg[0])
688                                         current_format()->fs = optarg[0];
689                                 else
690                                         bad_args("No field delimiter given.");
691                                 break;
692                         case 'q':
693                                 current_format()->quiet = 1;
694                                 break;
695                         case 'r':
696                                 set_format(FORM_REGEX);
697                                 err = regex_set(current_format(), optarg);
698                                 if (err)
699                                         bad_args("Error compiling regex: %s", err);
700                                 break;
701                         case 't':
702                                 set_format(FORM_TSV);
703                                 break;
704                         case 'w':
705                                 set_format(FORM_WS);
706                                 break;
707                         case 'W':
708                                 set_format(FORM_WS);
709                                 current_format()->strict_ws = 1;
710                                 break;
711                         case OPT_ALWAYS_QUOTE:
712                                 if (current_format()->id != FORM_CSV)
713                                         bad_args("--always-quote makes sense only for CSV.");
714                                 current_format()->always_quote = 1;
715                                 break;
716                         case OPT_HELP:
717                                 usage();
718                         case OPT_TRIM:
719                                 want_trim = 1;
720                                 break;
721                         case OPT_TABLE:
722                                 set_format(FORM_TABLE);
723                                 break;
724                         case OPT_TABLE_SEP:
725                                 current_format()->table_sep = atoi(optarg);
726                                 break;
727                         default:
728                                 bad_args(NULL);
729                 }
730
731         current_format();
732         if (!out_format)
733                 out_format = in_format;
734         if (!in_format->read_line)
735                 bad_args("Write-only format selected for input.");
736         if (!out_format->write_line)
737                 bad_args("Read-only format selected for output.");
738
739         for (int i = optind; i < argc; i++) {
740                 err = parse_selector(argv[i]);
741                 if (err)
742                         bad_args(err);
743         }
744         finish_parse_selectors();
745
746         fields_init(&in_fields);
747         fields_init(&out_fields);
748         line_init(&in_line);
749
750         if (out_format->needs_two_passes)
751                 two_pass();
752         else
753                 one_pass();
754         return 0;
755 }