2 * The Swiss-Army Knife for CSV-like Files
4 * (c) 2012 Martin Mares <mj@ucw.cz>
20 #define NONRET __attribute__((noreturn))
21 #define UNUSED __attribute__((unused))
27 static void select_fields(void);
28 static void select_all_fields(void);
30 /*** General functions ***/
32 static void NONRET die(char *msg, ...)
36 fprintf(stderr, "xsv: ");
37 vfprintf(stderr, msg, args);
43 /*** Memory allocation ***/
45 static void *xmalloc(size_t bytes)
47 void *p = malloc(bytes);
49 die("Out of memory (cannot allocate %zu bytes)", bytes);
53 static void *xmalloc_zero(size_t bytes)
55 void *p = xmalloc(bytes);
60 static void *xrealloc(void *old, size_t bytes)
62 void *p = realloc(old, bytes);
64 die("Out of memory (cannot allocate %zu bytes)", bytes);
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)); \
77 static inline type *name##_push(name##_t *b) { \
78 if (b->count >= b->max) name##_extend(b); \
79 return &b->start[b->count++]; \
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]; } \
85 DECLARE_BUF(intarray, int);
86 DECLARE_BUF(stringarray, char *);
88 /*** Formats and their parameters ***/
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
113 char *set_field_names;
114 struct field_names *field_names;
121 pcre_extra *pcre_extra;
123 // Temporary file backend:
131 static struct format *in_format, *out_format;
132 static int want_trim, want_equalize, want_stats;
139 DECLARE_BUF(fields, struct field);
140 DECLARE_BUF(line, unsigned char);
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;
147 static int read_line(void)
149 fields_reset(&in_fields);
150 line_reset(&in_line);
152 if (!in_format->read_line(in_format))
154 if (ferror_unlocked(stdin))
155 die("I/O error when reading standard input");
159 static void write_line(void)
161 out_format->write_line(out_format);
162 if (ferror_unlocked(stdout))
163 die("I/O error when writing standard input");
166 static void write_grid(int pos)
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");
175 static void new_field(int pos)
177 in_field = fields_push(&in_fields);
178 in_field->start_pos = pos;
182 static void ensure_field(int pos)
188 static unsigned char *get_field(fields_t *fields, int i, int *len)
190 struct field *f = fields_nth(fields, i);
192 return line_nth(&in_line, f->start_pos);
195 static void warn(struct format *fmt, char *msg, ...)
198 fprintf(stderr, "Warning at line %d: ", line_number);
201 vfprintf(stderr, msg, args);
207 static int next_line(void)
210 int c = getchar_unlocked();
214 return !!line_count(&in_line);
217 *line_push(&in_line) = c;
221 static int field_chars(struct field *f)
223 unsigned char *s = line_nth(&in_line, f->start_pos);
226 memset(&mbs, 0, sizeof(mbs));
230 size_t k = mbrlen((char *) s + i, f->len - i, &mbs);
240 /*** Field statistics ***/
242 static intarray_t column_widths;
244 static void update_stats(void)
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;
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;
261 /*** CSV/TSV back-end */
263 static int csv_read(struct format *fmt)
267 int c = getchar_unlocked();
268 int i = line_count(&in_line);
272 if (c < 0 || c == '\n') {
274 warn(fmt, "Missing closing quote.");
276 return !!fields_count(&in_fields);
281 if (c == fmt->quote) {
282 c = getchar_unlocked();
283 if (c != fmt->quote) {
287 // Two quotes assimilate to one
289 // Fall through to pushing the character
290 } else if (c == fmt->quote) {
293 } else if (c == fmt->fs && !quoted) {
299 *line_push(&in_line) = c;
304 static int is_ws(int c)
306 return (c == ' ' || c == '\t' || c == '\f');
309 static void csv_write(struct format *fmt)
311 for (int i=0; i < fields_count(&out_fields); i++) {
313 unsigned char *p = get_field(&out_fields, i, &len);
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)
324 putchar_unlocked(fmt->fs);
326 putchar_unlocked(fmt->quote);
327 for (int j=0; j < len; j++) {
329 if (c == fmt->fs && !need_quotes)
330 warn(fmt, "Field separator found inside field and quoting is turned off.");
336 putchar_unlocked(fmt->quote);
338 putchar_unlocked('\n');
341 /*** White-space back-end ***/
343 static int ws_read(struct format *fmt)
348 unsigned char *line = line_first(&in_line);
349 int n = line_count(&in_line);
355 for (int i=0; i<n; i++) {
361 if (!in_field->start_pos &&
364 in_field->start_pos = i;
373 if (ws && !fmt->sloppy)
378 /*** Regex back-end ***/
380 static const char *regex_set(struct format *f, char *rx)
384 f->pcre = pcre_compile(rx, PCRE_DOLLAR_ENDONLY, &err, &errpos, NULL);
388 f->pcre_extra = pcre_study(f->pcre, 0, &err);
395 static int regex_read(struct format *fmt)
400 unsigned char *c = line_first(&in_line);
401 int n = line_count(&in_line);
408 int err = pcre_exec(fmt->pcre, fmt->pcre_extra, (char *) c, n, i, 0, ovec, 3);
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) {
415 in_field->len = n - i;
419 if (!fmt->sloppy || ovec[0]) {
421 in_field->len = ovec[0] - i;
427 /*** Table back-end ***/
429 static void table_write(struct format *fmt)
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, "");
436 printf("%*s", fmt->table_sep, "");
438 int cw = *intarray_nth(&column_widths, i);
440 if (i < fields_count(&out_fields)) {
442 unsigned char *p = get_field(&out_fields, i, &len);
443 fw = field_chars(fields_nth(&out_fields, i));
445 warn(fmt, "Internal error: Wrongly calculated width of column %d (%d > %d)", i, fw, cw);
452 putchar_unlocked(' ');
457 printf("%*s", fmt->table_sep - fmt->table_sep / 2, "");
461 putchar_unlocked('|');
462 putchar_unlocked('\n');
465 static void table_write_grid(struct format *fmt, int pos UNUSED)
467 if (!fmt->table_grid)
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);
476 putchar_unlocked('+');
477 putchar_unlocked('\n');
480 /*** Temporary file back-end ***/
482 static int tmp_read(struct format *fmt)
484 FILE *tf = fmt->tmp_file;
487 int c = getc_unlocked(tf);
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);
498 new_field(line_count(&in_line));
501 int x = getc_unlocked(tf);
503 die("Truncated temporary file");
504 *line_push(&in_line) = x;
508 if (ferror_unlocked(tf))
509 die("I/O error when reading temporary file");
512 static void tmp_write(struct format *fmt)
514 FILE *tf = fmt->tmp_file;
516 for (int i = 0; i < fields_count(&out_fields); i++) {
518 unsigned char *p = get_field(&out_fields, i, &len);
521 putc_unlocked(len, tf);
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);
531 putc_unlocked(*p++, tf);
533 putc_unlocked(0xff, tf);
535 if (ferror_unlocked(tf))
536 die("I/O error when writing temporary file");
541 static void trim_fields(void)
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]))
553 static void equalize_fields(void)
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;
561 /*** Field names and headers ***/
567 static void add_field(struct field_names *fn, char *name, int namelen)
569 char *n = xmalloc(namelen + 1);
570 memcpy(n, name, namelen);
572 *stringarray_push(&fn->names) = n;
575 static void add_field_names(struct field_names *fn, char *names)
579 char *q = strchr(p, ',');
580 int len = q ? q-p : (int) strlen(p);
581 add_field(fn, p, len);
586 static void read_header(void)
588 if (!(in_format->has_header || in_format->set_field_names))
591 struct field_names *fn = xmalloc_zero(sizeof(*fn));
592 in_format->field_names = fn;
594 if (in_format->has_header) {
596 die("Missing input header");
599 if (in_format->set_field_names) {
600 add_field_names(fn, in_format->set_field_names);
602 for (int i = 0; i < fields_count(&in_fields); i++) {
604 char *s = (char *) get_field(&in_fields, i, &len);
605 add_field(fn, s, len);
610 static void write_header(void)
612 if (!out_format->has_header) {
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;
626 die("Output header requested, but no field names specified");
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);
635 char *s = *stringarray_nth(&fn->names, i);
637 *line_push(&in_line) = *s++;
642 fields_reset(&out_fields);
643 if (want_select_fields)
648 // This is tricky: when we are formatting a table, field names are normally
649 // calculated in pass 1, but the header is written in pass 2, so we have to
650 // update column statistics, because field name can be too wide to fit.
661 static void write_footer(void)
666 static int find_field_by_name(struct field_names *fn, char *name)
668 for (int i = 0; i < stringarray_count(&fn->names); i++)
669 if (!strcmp(*stringarray_nth(&fn->names, i), name))
674 /*** Field selection ***/
677 int first_field, last_field; // 0 means "boundary"
680 DECLARE_BUF(selectors, struct selector);
681 static selectors_t selectors;
683 static int parse_field_num(char *str)
688 if (*str < '0' || *str > '9')
692 f = 10*f + *str - '0';
698 static int parse_field(char *str)
703 int f = parse_field_num(str);
707 if (in_format->field_names && (f = find_field_by_name(in_format->field_names, str)) > 0)
710 die("Unknown field `%s'", str);
713 static char *parse_selector(char *str)
715 char buf[strlen(str) + 1];
718 struct selector *s = selectors_push(&selectors);
719 char *sep = strchr(buf, '-');
722 s->first_field = parse_field(buf);
723 s->last_field = parse_field(sep);
725 s->first_field = s->last_field = parse_field(buf);
730 static void finish_parse_selectors(void)
732 if (!selectors_count(&selectors))
736 static void select_fields(void)
738 for (int i = 0; i < selectors_count(&selectors); i++) {
739 struct selector *s = selectors_nth(&selectors, i);
740 int first = s->first_field;
743 int last = s->last_field;
745 last = fields_count(&in_fields);
746 for (int j = first; j <= last; j++) {
747 struct field *f = fields_push(&out_fields);
748 if (j >= 1 && j <= fields_count(&in_fields))
749 *f = *fields_nth(&in_fields, j-1);
751 f->start_pos = f->len = 0;
756 static void select_all_fields(void)
758 for (int i = 0; i < fields_count(&in_fields); i++)
759 *fields_push(&out_fields) = *fields_nth(&in_fields, i);
762 /*** Processing of files ***/
764 static void one_pass(int pass)
774 if (want_trim && (pass & 1))
777 fields_reset(&out_fields);
783 if (want_equalize && (pass & 2))
793 static void two_pass(void)
795 struct format *final_format = out_format;
797 // We need to use character set info from the current locale
798 setlocale(LC_CTYPE, "");
800 // Pass 1: Set up writer of intermediate format
801 out_format = xmalloc_zero(sizeof(*out_format));
802 out_format->id = FORM_TMP;
803 out_format->read_line = tmp_read;
804 out_format->write_line = tmp_write;
805 out_format->tmp_file = tmpfile();
806 out_format->field_names = in_format->field_names;
809 // Pass 2: Set up reader of intermediate format
810 in_format = out_format;
811 rewind(in_format->tmp_file);
813 out_format = final_format;
816 fclose(in_format->tmp_file);
819 /*** Parsing of arguments ***/
821 static void NONRET usage(void)
824 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
827 -t, --tsv Tab-separated values (default)\n\
828 -c, --csv Comma-separated values\n\
829 -w, --ws Values separated by arbitrary whitespace\n\
830 -r, --regex=<rx> Separator given by Perl regular expression (input only)\n\
831 --table Format a table (output only)\n\
833 Format parameters:\n\
834 -d, --fs=<char> Delimiter of fields\n\
835 -f, --fields=<f>,... Set field names\n\
836 -h, --header The first line contains field names\n\
837 -q, --quiet Do not show warnings\n\
838 --always-quote Put quotes around all fields (CSV output only)\n\
839 --table-sep=<n> Separate table columns by <n> spaces (default: 2)\n\
840 --grid Separate table columns by grid lines\n\
841 -s, --sloppy Ignore separators at the start/end of line (ws/regex only)\n\
844 --trim Trim leading and trailing whitespaces in fields\n\
845 --equalize Pad all lines to the maximum number of fields\n\
850 static void NONRET bad_args(const char *msg, ...)
855 fprintf(stderr, "xsv: ");
856 vfprintf(stderr, msg, args);
860 fprintf(stderr, "Try `xsv --help' for more information.\n");
864 static const char short_options[] = "cd:f:hqr:twW";
876 static const struct option long_options[] = {
877 { "always-quote", 0, NULL, OPT_ALWAYS_QUOTE },
878 { "csv", 0, NULL, 'c' },
879 { "equalize", 0, NULL, OPT_EQUALIZE },
880 { "fields", 1, NULL, 'f' },
881 { "fs", 1, NULL, 'd' },
882 { "grid", 0, NULL, OPT_GRID },
883 { "header", 0, NULL, 'h' },
884 { "quiet", 0, NULL, 'q' },
885 { "regex", 1, NULL, 'r' },
886 { "sloppy", 0, NULL, 's' },
887 { "table", 0, NULL, OPT_TABLE },
888 { "table-sep", 1, NULL, OPT_TABLE_SEP },
889 { "trim", 0, NULL, OPT_TRIM },
890 { "tsv", 0, NULL, 't' },
891 { "ws", 0, NULL, 'w' },
892 { "help", 0, NULL, OPT_HELP },
893 { NULL, 0, NULL, 0 },
896 static void set_format(int format_id)
898 struct format *f = xmalloc_zero(sizeof(*f));
905 f->read_line = csv_read;
906 f->write_line = csv_write;
911 f->read_line = csv_read;
912 f->write_line = csv_write;
917 f->read_line = ws_read;
918 f->write_line = csv_write;
921 f->read_line = regex_read;
924 f->write_line = table_write;
925 f->write_grid = table_write_grid;
933 else if (!out_format)
936 bad_args("At most two formats may be given.");
939 static struct format *current_format(void)
945 set_format(FORM_TSV);
949 int main(int argc, char **argv)
954 while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
957 set_format(FORM_CSV);
961 current_format()->fs = optarg[0];
963 bad_args("No field delimiter given.");
966 current_format()->set_field_names = optarg;
969 current_format()->has_header = 1;
972 current_format()->quiet = 1;
975 set_format(FORM_REGEX);
976 err = regex_set(current_format(), optarg);
978 bad_args("Error compiling regex: %s", err);
981 if (current_format()->id != FORM_WS && current_format()->id != FORM_REGEX)
982 bad_args("--sloppy makes sense only for --ws or --regex.");
983 current_format()->sloppy = 1;
986 set_format(FORM_TSV);
991 case OPT_ALWAYS_QUOTE:
992 if (current_format()->id != FORM_CSV)
993 bad_args("--always-quote makes sense only for --csv.");
994 current_format()->always_quote = 1;
1002 set_format(FORM_TABLE);
1005 current_format()->table_sep = atoi(optarg);
1008 current_format()->table_grid = 1;
1019 out_format = in_format;
1020 if (!in_format->read_line)
1021 bad_args("Write-only format selected for input.");
1022 if (!out_format->write_line)
1023 bad_args("Read-only format selected for output.");
1026 for (int i = optind; i < argc; i++) {
1027 err = parse_selector(argv[i]);
1031 finish_parse_selectors();
1033 want_stats = out_format->needs_stats | want_equalize;