]> mj.ucw.cz Git - xsv.git/blob - xsv.c
fa7964da7727328dfc087dd1c080bdcb4944d4b3
[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 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <getopt.h>
11
12 /*** Memory allocation ***/
13
14 static void *xmalloc(size_t bytes)
15 {
16         void *p = malloc(bytes);
17         if (!p) {
18                 fprintf(stderr, "xsv: Out of memory (cannot allocate %zu bytes)\n", bytes);
19                 exit(1);
20         }
21         return p;
22 }
23
24 static void *xrealloc(void *old, size_t bytes)
25 {
26         void *p = realloc(old, bytes);
27         if (!p) {
28                 fprintf(stderr, "xsv: Out of memory (cannot allocate %zu bytes)\n", bytes);
29                 exit(1);
30         }
31         return p;
32 }
33
34 #define DECLARE_BUF(name, type) \
35         typedef struct { type *start; int count; int max; } name##_t;                           \
36         static inline void name##_init(name##_t *b) { b->start = NULL; b->count = b->max = 0; } \
37         static inline void name##_reset(name##_t *b) { b->count = 0; }                          \
38         static inline int name##_count(name##_t *b) { return b->count; }                        \
39         static void name##_extend(name##_t *b) {                                                \
40                 b->max = b->max ? 2*b->max : 16;                                                \
41                 b->start = xrealloc(b->start, b->max * sizeof(type));                           \
42         }                                                                                       \
43         static inline type *name##_push(name##_t *b) {                                          \
44                 if (b->count >= b->max) name##_extend(b);                                       \
45                 return &b->start[b->count++];                                                   \
46         }                                                                                       \
47         static inline type *name##_nth(name##_t *b, int n) { return &b->start[n]; }             \
48         // end
49
50 /*** Formats and their parameters ***/
51
52 enum format_id {
53         FORM_UNSPEC,
54         FORM_TSV,
55         FORM_CSV,
56         FORM_WS,
57 };
58
59 struct format {
60         enum format_id id;
61         int fs;
62         int quote;
63         int (*read_line)(void);
64         void (*write_line)(void);
65 };
66
67 static struct format *in_format, *out_format;
68
69 struct field {
70         int start_pos;
71         int len;
72 };
73
74 DECLARE_BUF(fields, struct field);
75 DECLARE_BUF(line, unsigned char);
76
77 static fields_t in_fields, out_fields;
78 static struct field *in_field;
79 static line_t in_line;
80
81 static void new_field(void)
82 {
83         in_field = fields_push(&in_fields);
84         in_field->start_pos = line_count(&in_line);
85         in_field->len = 0;
86 }
87
88 static void ensure_field(void)
89 {
90         if (!in_field)
91                 new_field();
92 }
93
94 static int csv_read(void)
95 {
96         int quoted = 0;
97         // FIXME: Complain if closing quote is missing?
98         for (;;) {
99                 int c = getchar();
100 restart:
101                 if (c < 0)
102                         return !!fields_count(&in_fields);
103                 if (c == '\r')
104                         continue;
105                 if (c == '\n')
106                         return 1;
107                 if (quoted) {
108                         if (c == in_format->quote) {
109                                 c = getchar();
110                                 if (c != in_format->quote) {
111                                         quoted = 0;
112                                         goto restart;
113                                 }
114                                 // Two quotes assimilate to one
115                         }
116                         // Fall through to pushing the character
117                 } else if (c == in_format->quote) {
118                         quoted = 1;
119                         continue;
120                 } else if (c == in_format->fs && !quoted) {
121                         ensure_field();
122                         new_field();
123                         continue;
124                 }
125                 ensure_field();
126                 *line_push(&in_line) = c;
127                 in_field->len++;
128         }
129 }
130
131 static void csv_write(void)
132 {
133         unsigned char *line = line_nth(&in_line, 0);
134         int n = fields_count(&out_fields);
135         for (int i=0; i<n; i++) {
136                 struct field *f = fields_nth(&out_fields, i);
137                 int need_quotes = 0;
138                 if (out_format->quote >= 0) {
139                         for (int j=0; j < f->len; j++) {
140                                 int c = line[f->start_pos + j];
141                                 if (c == out_format->fs || c == out_format->quote) {
142                                         need_quotes = 1;
143                                         break;
144                                 }
145                         }
146                 }
147                 if (i)
148                         putchar(out_format->fs);
149                 if (need_quotes)
150                         putchar(out_format->quote);
151                 for (int j=0; j < f->len; j++) {
152                         int c = line[f->start_pos + j];
153                         if (c == out_format->quote)
154                                 putchar(c);
155                         putchar(c);
156                 }
157                 if (need_quotes)
158                         putchar(out_format->quote);
159         }
160         putchar('\n');
161 }
162
163 /*** Field selection ***/
164
165 struct selector {
166         int first_field, last_field;
167 };
168
169 DECLARE_BUF(selectors, struct selector);
170 static selectors_t selectors;
171
172 static char *parse_selector(char *str)
173 {
174         char buf[strlen(str) + 1];
175         strcpy(buf, str);
176
177         struct selector *s = selectors_push(&selectors);
178         char *sep = strchr(buf, '-');
179         if (sep) {
180                 *sep++ = 0;
181                 s->first_field = atoi(buf);
182                 s->last_field = atoi(sep);
183         } else
184                 s->first_field = s->last_field = atoi(buf);
185
186         return NULL;
187 }
188
189 static void finish_parse_selectors(void)
190 {
191         if (!selectors_count(&selectors))
192                 parse_selector("-");
193 }
194
195 static void select_fields(void)
196 {
197         for (int i = 0; i < selectors_count(&selectors); i++) {
198                 struct selector *s = selectors_nth(&selectors, i);
199                 int first = s->first_field;
200                 if (first <= 0)
201                         first = 1;
202                 int last = s->last_field;
203                 if (last <= 0)
204                         last = fields_count(&in_fields);
205                 for (int j = first; j <= last; j++) {
206                         struct field *f = fields_push(&out_fields);
207                         if (j >= 1 && j <= fields_count(&in_fields))
208                                 *f = *fields_nth(&in_fields, j-1);
209                         else
210                                 f->start_pos = f->len = 0;
211                 }
212         }
213 }
214
215 /*** Parsing of arguments ***/
216
217 static void usage(void)
218 {
219         printf("\
220 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
221 \n\
222 Formats:\n\
223 -t, --tsv               TAB-separated values (default)\n\
224 -c, --csv               Comma-separated values\n\
225 -w, --ws                Values separated by arbitrary whitespace\n\
226 \n\
227 Format parameters:\n\
228 -d, --fs=<char> Delimiter of fields\n\
229 \n\
230 Other options:\n\
231 (so far none)\n\
232 ");
233         exit(0);
234 }
235
236 static void bad_args(char *msg)
237 {
238         if (msg)
239                 fprintf(stderr, "xsv: %s\n", msg);
240         fprintf(stderr, "Try `xsv --help' for more information.\n");
241         exit(1);
242 }
243
244 static const char short_options[] = "cd:tw";
245
246 enum long_options {
247         OPT_HELP = 256,
248 };
249
250 static const struct option long_options[] = {
251         { "csv",                0,      NULL,   'c' },
252         { "fs",                 1,      NULL,   'd' },
253         { "tsv",                0,      NULL,   't' },
254         { "ws",                 0,      NULL,   'w' },
255         { "help",               0,      NULL,   OPT_HELP },
256         { NULL,                 0,      NULL,   0 },
257 };
258
259 static void set_format(int format_id)
260 {
261         struct format *f = xmalloc(sizeof(*f));
262         memset(f, 0, sizeof(*f));
263         f->id = format_id;
264
265         switch (format_id) {
266                 case FORM_TSV:
267                         f->fs = '\t';
268                         f->quote = -1;
269                         f->read_line = csv_read;
270                         f->write_line = csv_write;
271                         break;
272                 case FORM_CSV:
273                         f->fs = ',';
274                         f->quote = '"';
275                         f->read_line = csv_read;
276                         f->write_line = csv_write;
277                         break;
278                 case FORM_WS:
279                         break;
280         }
281
282         if (!in_format)
283                 in_format = f;
284         else if (!out_format)
285                 out_format = f;
286         else
287                 bad_args("At most two format may be given.");
288 }
289
290 static struct format *current_format(void)
291 {
292         if (out_format)
293                 return out_format;
294         if (in_format)
295                 return in_format;
296         set_format(FORM_TSV);
297         return in_format;
298 }
299
300 int main(int argc, char **argv)
301 {
302         int opt;
303
304         while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
305                 switch (opt) {
306                         case 'c':
307                                 set_format(FORM_CSV);
308                                 break;
309                         case 'd':
310                                 if (optarg[0])
311                                         current_format()->fs = optarg[0];
312                                 else
313                                         bad_args("No field delimiter given.");
314                                 break;
315                         case 't':
316                                 set_format(FORM_TSV);
317                                 break;
318                         case 'w':
319                                 set_format(FORM_WS);
320                                 break;
321                         case OPT_HELP:
322                                 usage();
323                         default:
324                                 bad_args(NULL);
325                 }
326
327         current_format();
328         if (!out_format)
329                 out_format = in_format;
330
331         for (int i = optind; i < argc; i++) {
332                 char *err = parse_selector(argv[i]);
333                 if (err)
334                         bad_args(err);
335         }
336         finish_parse_selectors();
337
338         fields_init(&in_fields);
339         fields_init(&out_fields);
340         line_init(&in_line);
341
342         for (;;) {
343                 fields_reset(&in_fields);
344                 line_reset(&in_line);
345                 in_field = NULL;
346                 if (!in_format->read_line())
347                         break;
348
349                 fields_reset(&out_fields);
350                 select_fields();
351
352                 out_format->write_line();
353         }
354
355         return 0;
356 }