]> sigrok.org Git - libsigrok.git/blob - src/input/csv.c
5957ed491533fcba4eac5363ad5e402850352bed
[libsigrok.git] / src / input / csv.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include <glib.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28
29 #define LOG_PREFIX "input/csv"
30
31 #define CHUNK_SIZE      (4 * 1024 * 1024)
32
33 /*
34  * The CSV input module has the following options:
35  *
36  * single-column: Specifies the column number which stores the sample data for
37  *                single column mode and enables single column mode. Multi
38  *                column mode is used if this parameter is omitted.
39  *
40  * numchannels:   Specifies the number of channels to use. In multi column mode
41  *                the number of channels are the number of columns and in single
42  *                column mode the number of bits (LSB first) beginning at
43  *                'first-channel'.
44  *
45  * delimiter:     Specifies the delimiter for columns. Must be at least one
46  *                character. Comma is used as default delimiter.
47  *
48  * format:        Specifies the format of the sample data in single column mode.
49  *                Available formats are: 'bin', 'hex' and 'oct'. The binary
50  *                format is used by default. This option has no effect in multi
51  *                column mode.
52  *
53  * comment:       Specifies the prefix character(s) for comments. No prefix
54  *                characters are used by default which disables removing of
55  *                comments.
56  *
57  * samplerate:    Samplerate which the sample data was captured with. Default
58  *                value is 0.
59  *
60  * first-channel: Column number of the first channel in multi column mode and
61  *                position of the bit for the first channel in single column mode.
62  *                Default value is 0.
63  *
64  * header:        Determines if the first line should be treated as header
65  *                and used for channel names in multi column mode. Empty header
66  *                names will be replaced by the channel number. If enabled in
67  *                single column mode the first line will be skipped. Usage of
68  *                header is disabled by default.
69  *
70  * startline:     Line number to start processing sample data. Must be greater
71  *                than 0. The default line number to start processing is 1.
72  */
73
74 /*
75  * TODO
76  *
77  * - Determine how the text line handling can get improved, regarding
78  *   all of robustness and flexibility and correctness.
79  *   - The current implementation splits on "any run of CR and LF". Which
80  *     translates to: Line numbers are wrong in the presence of empty
81  *     lines in the input stream. See below for an (expensive) fix.
82  *   - Dropping support for CR style end-of-line markers could improve
83  *     the situation a lot. Code could search for and split on LF, and
84  *     trim optional trailing CR. This would result in proper support
85  *     for CRLF (Windows) as well as LF (Unix), and allow for correct
86  *     line number counts.
87  *   - When support for CR-only line termination cannot get dropped,
88  *     then the current implementation is inappropriate. Currently the
89  *     input stream is scanned for the first occurance of either of the
90  *     supported termination styles (which is good). For the remaining
91  *     session a consistent encoding of the text lines is assumed (which
92  *     is acceptable).
93  *   - When line numbers need to be correct and reliable, _and_ the full
94  *     set of previously supported line termination sequences are required,
95  *     and potentially more are to get added for improved compatibility
96  *     with more platforms or generators, then the current approach of
97  *     splitting on runs of termination characters needs to get replaced,
98  *     by the more expensive approach to scan for and count the initially
99  *     determined termination sequence.
100  *
101  * - Add support for analog input data? (optional)
102  *   - Needs a syntax first for user specs which channels (columns) are
103  *     logic and which are analog. May need heuristics(?) to guess from
104  *     input data in the absence of user provided specs.
105  */
106
107 /* Single column formats. */
108 enum single_col_format {
109         FORMAT_BIN,
110         FORMAT_HEX,
111         FORMAT_OCT,
112 };
113
114 struct context {
115         gboolean started;
116
117         /* Current selected samplerate. */
118         uint64_t samplerate;
119
120         /* Number of channels. */
121         size_t num_channels;
122
123         /* Column delimiter character(s). */
124         GString *delimiter;
125
126         /* Comment prefix character(s). */
127         GString *comment;
128
129         /* Termination character(s) used in current stream. */
130         char *termination;
131
132         /* Determines if sample data is stored in multiple columns. */
133         gboolean multi_column_mode;
134
135         /* Column number of the sample data in single column mode. */
136         size_t single_column;
137
138         /*
139          * Number of the first column to parse. Equivalent to the number of the
140          * first channel in multi column mode and the single column number in
141          * single column mode.
142          */
143         size_t first_column;
144
145         /*
146          * Column number of the first channel in multi column mode and position of
147          * the bit for the first channel in single column mode.
148          */
149         size_t first_channel;
150
151         /* Line number to start processing. */
152         size_t start_line;
153
154         /*
155          * Determines if the first line should be treated as header and used for
156          * channel names in multi column mode.
157          */
158         gboolean header;
159
160         /* Format sample data is stored in single column mode. */
161         enum single_col_format format;
162
163         size_t sample_unit_size;        /**!< Byte count for a single sample. */
164         uint8_t *sample_buffer;         /**!< Buffer for a single sample. */
165
166         uint8_t *datafeed_buffer;       /**!< Queue for datafeed submission. */
167         size_t datafeed_buf_size;
168         size_t datafeed_buf_fill;
169
170         /* Current line number. */
171         size_t line_number;
172
173         /* List of previously created sigrok channels. */
174         GSList *prev_sr_channels;
175 };
176
177 static void strip_comment(char *buf, const GString *prefix)
178 {
179         char *ptr;
180
181         if (!prefix->len)
182                 return;
183
184         if ((ptr = strstr(buf, prefix->str)))
185                 *ptr = '\0';
186 }
187
188 static int parse_binstr(const char *str, struct context *inc)
189 {
190         gsize i, j, length;
191
192         length = strlen(str);
193
194         if (!length) {
195                 sr_err("Column %zu in line %zu is empty.", inc->single_column,
196                         inc->line_number);
197                 return SR_ERR;
198         }
199
200         /* Clear buffer in order to set bits only. */
201         memset(inc->sample_buffer, 0, inc->sample_unit_size);
202
203         i = inc->first_channel;
204
205         for (j = 0; i < length && j < inc->num_channels; i++, j++) {
206                 if (str[length - i - 1] == '1') {
207                         inc->sample_buffer[j / 8] |= (1 << (j % 8));
208                 } else if (str[length - i - 1] != '0') {
209                         sr_err("Invalid value '%s' in column %zu in line %zu.",
210                                 str, inc->single_column, inc->line_number);
211                         return SR_ERR;
212                 }
213         }
214
215         return SR_OK;
216 }
217
218 static int parse_hexstr(const char *str, struct context *inc)
219 {
220         gsize i, j, k, length;
221         uint8_t value;
222         char c;
223
224         length = strlen(str);
225
226         if (!length) {
227                 sr_err("Column %zu in line %zu is empty.", inc->single_column,
228                         inc->line_number);
229                 return SR_ERR;
230         }
231
232         /* Clear buffer in order to set bits only. */
233         memset(inc->sample_buffer, 0, inc->sample_unit_size);
234
235         /* Calculate the position of the first hexadecimal digit. */
236         i = inc->first_channel / 4;
237
238         for (j = 0; i < length && j < inc->num_channels; i++) {
239                 c = str[length - i - 1];
240
241                 if (!g_ascii_isxdigit(c)) {
242                         sr_err("Invalid value '%s' in column %zu in line %zu.",
243                                 str, inc->single_column, inc->line_number);
244                         return SR_ERR;
245                 }
246
247                 value = g_ascii_xdigit_value(c);
248
249                 k = (inc->first_channel + j) % 4;
250
251                 for (; j < inc->num_channels && k < 4; k++) {
252                         if (value & (1 << k))
253                                 inc->sample_buffer[j / 8] |= (1 << (j % 8));
254
255                         j++;
256                 }
257         }
258
259         return SR_OK;
260 }
261
262 static int parse_octstr(const char *str, struct context *inc)
263 {
264         gsize i, j, k, length;
265         uint8_t value;
266         char c;
267
268         length = strlen(str);
269
270         if (!length) {
271                 sr_err("Column %zu in line %zu is empty.", inc->single_column,
272                         inc->line_number);
273                 return SR_ERR;
274         }
275
276         /* Clear buffer in order to set bits only. */
277         memset(inc->sample_buffer, 0, inc->sample_unit_size);
278
279         /* Calculate the position of the first octal digit. */
280         i = inc->first_channel / 3;
281
282         for (j = 0; i < length && j < inc->num_channels; i++) {
283                 c = str[length - i - 1];
284
285                 if (c < '0' || c > '7') {
286                         sr_err("Invalid value '%s' in column %zu in line %zu.",
287                                 str, inc->single_column, inc->line_number);
288                         return SR_ERR;
289                 }
290
291                 value = g_ascii_xdigit_value(c);
292
293                 k = (inc->first_channel + j) % 3;
294
295                 for (; j < inc->num_channels && k < 3; k++) {
296                         if (value & (1 << k))
297                                 inc->sample_buffer[j / 8] |= (1 << (j % 8));
298
299                         j++;
300                 }
301         }
302
303         return SR_OK;
304 }
305
306 /**
307  * @brief Splits a text line into a set of columns.
308  *
309  * @param[in] buf       The input text line to split.
310  * @param[in] inc       The input module's context.
311  * @param[in] max_cols  The maximum column count, negative to get all of them.
312  *
313  * @returns An array of strings, representing the columns' text.
314  */
315 static char **parse_line(char *buf, struct context *inc, ssize_t max_cols)
316 {
317         const char *str, *remainder;
318         GSList *list, *l;
319         char **columns;
320         char *column;
321         gsize n, k;
322
323         n = 0;
324         k = 0;
325         list = NULL;
326
327         remainder = buf;
328         str = strstr(remainder, inc->delimiter->str);
329
330         while (str && max_cols) {
331                 if (n >= inc->first_column) {
332                         column = g_strndup(remainder, str - remainder);
333                         list = g_slist_prepend(list, g_strstrip(column));
334
335                         max_cols--;
336                         k++;
337                 }
338
339                 remainder = str + inc->delimiter->len;
340                 str = strstr(remainder, inc->delimiter->str);
341                 n++;
342         }
343
344         if (buf[0] && max_cols && n >= inc->first_column) {
345                 column = g_strdup(remainder);
346                 list = g_slist_prepend(list, g_strstrip(column));
347                 k++;
348         }
349
350         if (!(columns = g_try_new(char *, k + 1)))
351                 return NULL;
352
353         columns[k--] = NULL;
354
355         for (l = list; l; l = l->next)
356                 columns[k--] = l->data;
357
358         g_slist_free(list);
359
360         return columns;
361 }
362
363 static int parse_multi_columns(char **columns, struct context *inc)
364 {
365         gsize i;
366         char *column;
367
368         /* Clear buffer in order to set bits only. */
369         memset(inc->sample_buffer, 0, inc->sample_unit_size);
370
371         for (i = 0; i < inc->num_channels; i++) {
372                 column = columns[i];
373                 if (column[0] == '1') {
374                         inc->sample_buffer[i / 8] |= (1 << (i % 8));
375                 } else if (!strlen(column)) {
376                         sr_err("Column %zu in line %zu is empty.",
377                                 inc->first_channel + i, inc->line_number);
378                         return SR_ERR;
379                 } else if (column[0] != '0') {
380                         sr_err("Invalid value '%s' in column %zu in line %zu.",
381                                 column, inc->first_channel + i,
382                                 inc->line_number);
383                         return SR_ERR;
384                 }
385         }
386
387         return SR_OK;
388 }
389
390 static int parse_single_column(const char *column, struct context *inc)
391 {
392         int res;
393
394         res = SR_ERR;
395
396         switch (inc->format) {
397         case FORMAT_BIN:
398                 res = parse_binstr(column, inc);
399                 break;
400         case FORMAT_HEX:
401                 res = parse_hexstr(column, inc);
402                 break;
403         case FORMAT_OCT:
404                 res = parse_octstr(column, inc);
405                 break;
406         }
407
408         return res;
409 }
410
411 static int flush_samples(const struct sr_input *in)
412 {
413         struct context *inc;
414         struct sr_datafeed_packet packet;
415         struct sr_datafeed_logic logic;
416         int rc;
417
418         inc = in->priv;
419         if (!inc->datafeed_buf_fill)
420                 return SR_OK;
421
422         memset(&packet, 0, sizeof(packet));
423         memset(&logic, 0, sizeof(logic));
424         packet.type = SR_DF_LOGIC;
425         packet.payload = &logic;
426         logic.unitsize = inc->sample_unit_size;
427         logic.length = inc->datafeed_buf_fill;
428         logic.data = inc->datafeed_buffer;
429
430         rc = sr_session_send(in->sdi, &packet);
431         if (rc != SR_OK)
432                 return rc;
433
434         inc->datafeed_buf_fill = 0;
435         return SR_OK;
436 }
437
438 static int queue_samples(const struct sr_input *in)
439 {
440         struct context *inc;
441         int rc;
442
443         inc = in->priv;
444
445         inc->datafeed_buf_fill += inc->sample_unit_size;
446         if (inc->datafeed_buf_fill == inc->datafeed_buf_size) {
447                 rc = flush_samples(in);
448                 if (rc != SR_OK)
449                         return rc;
450         }
451         inc->sample_buffer = &inc->datafeed_buffer[inc->datafeed_buf_fill];
452         return SR_OK;
453 }
454
455 static int init(struct sr_input *in, GHashTable *options)
456 {
457         struct context *inc;
458         const char *s;
459
460         in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
461         in->priv = inc = g_malloc0(sizeof(struct context));
462
463         inc->single_column = g_variant_get_uint32(g_hash_table_lookup(options, "single-column"));
464         inc->multi_column_mode = inc->single_column == 0;
465
466         inc->num_channels = g_variant_get_uint32(g_hash_table_lookup(options, "numchannels"));
467
468         inc->delimiter = g_string_new(g_variant_get_string(
469                         g_hash_table_lookup(options, "delimiter"), NULL));
470         if (inc->delimiter->len == 0) {
471                 sr_err("Delimiter must be at least one character.");
472                 return SR_ERR_ARG;
473         }
474
475         s = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL);
476         if (!g_ascii_strncasecmp(s, "bin", 3)) {
477                 inc->format = FORMAT_BIN;
478         } else if (!g_ascii_strncasecmp(s, "hex", 3)) {
479                 inc->format = FORMAT_HEX;
480         } else if (!g_ascii_strncasecmp(s, "oct", 3)) {
481                 inc->format = FORMAT_OCT;
482         } else {
483                 sr_err("Invalid format: '%s'", s);
484                 return SR_ERR_ARG;
485         }
486
487         inc->comment = g_string_new(g_variant_get_string(
488                         g_hash_table_lookup(options, "comment"), NULL));
489         if (g_string_equal(inc->comment, inc->delimiter)) {
490                 /* That's never going to work. Likely the result of the user
491                  * setting the delimiter to ; -- the default comment. Clearing
492                  * the comment setting will work in that case. */
493                 g_string_truncate(inc->comment, 0);
494         }
495
496         inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
497
498         inc->first_channel = g_variant_get_uint32(g_hash_table_lookup(options, "first-channel"));
499
500         inc->header = g_variant_get_boolean(g_hash_table_lookup(options, "header"));
501
502         inc->start_line = g_variant_get_uint32(g_hash_table_lookup(options, "startline"));
503         if (inc->start_line < 1) {
504                 sr_err("Invalid start line %zu.", inc->start_line);
505                 return SR_ERR_ARG;
506         }
507
508         if (inc->multi_column_mode)
509                 inc->first_column = inc->first_channel;
510         else
511                 inc->first_column = inc->single_column;
512
513         if (!inc->multi_column_mode && !inc->num_channels) {
514                 sr_err("Number of channels needs to be specified in single column mode.");
515                 return SR_ERR_ARG;
516         }
517
518         return SR_OK;
519 }
520
521 /*
522  * Check the channel list for consistency across file re-import. See
523  * the VCD input module for more details and motivation.
524  */
525
526 static void keep_header_for_reread(const struct sr_input *in)
527 {
528         struct context *inc;
529
530         inc = in->priv;
531         g_slist_free_full(inc->prev_sr_channels, sr_channel_free_cb);
532         inc->prev_sr_channels = in->sdi->channels;
533         in->sdi->channels = NULL;
534 }
535
536 static int check_header_in_reread(const struct sr_input *in)
537 {
538         struct context *inc;
539
540         if (!in)
541                 return FALSE;
542         inc = in->priv;
543         if (!inc)
544                 return FALSE;
545         if (!inc->prev_sr_channels)
546                 return TRUE;
547
548         if (sr_channel_lists_differ(inc->prev_sr_channels, in->sdi->channels)) {
549                 sr_err("Channel list change not supported for file re-read.");
550                 return FALSE;
551         }
552         g_slist_free_full(in->sdi->channels, sr_channel_free_cb);
553         in->sdi->channels = inc->prev_sr_channels;
554         inc->prev_sr_channels = NULL;
555
556         return TRUE;
557 }
558
559 static const char *delim_set = "\r\n";
560
561 static const char *get_line_termination(GString *buf)
562 {
563         const char *term;
564
565         term = NULL;
566         if (g_strstr_len(buf->str, buf->len, "\r\n"))
567                 term = "\r\n";
568         else if (memchr(buf->str, '\n', buf->len))
569                 term = "\n";
570         else if (memchr(buf->str, '\r', buf->len))
571                 term = "\r";
572
573         return term;
574 }
575
576 static int initial_parse(const struct sr_input *in, GString *buf)
577 {
578         struct context *inc;
579         GString *channel_name;
580         size_t num_columns, i;
581         size_t line_number, l;
582         int ret;
583         char **lines, *line, **columns, *column;
584
585         ret = SR_OK;
586         inc = in->priv;
587         columns = NULL;
588
589         line_number = 0;
590         lines = g_strsplit_set(buf->str, delim_set, 0);
591         for (l = 0; lines[l]; l++) {
592                 line_number++;
593                 line = lines[l];
594                 if (inc->start_line > line_number) {
595                         sr_spew("Line %zu skipped.", line_number);
596                         continue;
597                 }
598                 if (line[0] == '\0') {
599                         sr_spew("Blank line %zu skipped.", line_number);
600                         continue;
601                 }
602                 strip_comment(line, inc->comment);
603                 if (line[0] == '\0') {
604                         sr_spew("Comment-only line %zu skipped.", line_number);
605                         continue;
606                 }
607
608                 /* Reached first proper line. */
609                 break;
610         }
611         if (!lines[l]) {
612                 /* Not enough data for a proper line yet. */
613                 ret = SR_ERR_NA;
614                 goto out;
615         }
616
617         /*
618          * In order to determine the number of columns parse the current line
619          * without limiting the number of columns.
620          */
621         columns = parse_line(line, inc, -1);
622         if (!columns) {
623                 sr_err("Error while parsing line %zu.", line_number);
624                 ret = SR_ERR;
625                 goto out;
626         }
627         num_columns = g_strv_length(columns);
628
629         /* Ensure that the first column is not out of bounds. */
630         if (!num_columns) {
631                 sr_err("Column %zu in line %zu is out of bounds.",
632                         inc->first_column, line_number);
633                 ret = SR_ERR;
634                 goto out;
635         }
636
637         if (inc->multi_column_mode) {
638                 /*
639                  * Detect the number of channels in multi column mode
640                  * automatically if not specified.
641                  */
642                 if (!inc->num_channels) {
643                         inc->num_channels = num_columns;
644                         sr_dbg("Number of auto-detected channels: %zu.",
645                                 inc->num_channels);
646                 }
647
648                 /*
649                  * Ensure that the number of channels does not exceed the number
650                  * of columns in multi column mode.
651                  */
652                 if (num_columns < inc->num_channels) {
653                         sr_err("Not enough columns for desired number of channels in line %zu.",
654                                 line_number);
655                         ret = SR_ERR;
656                         goto out;
657                 }
658         }
659
660         channel_name = g_string_sized_new(64);
661         for (i = 0; i < inc->num_channels; i++) {
662                 column = columns[i];
663                 if (inc->header && inc->multi_column_mode && column[0] != '\0')
664                         g_string_assign(channel_name, column);
665                 else
666                         g_string_printf(channel_name, "%zu", i);
667                 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name->str);
668         }
669         g_string_free(channel_name, TRUE);
670         if (!check_header_in_reread(in)) {
671                 ret = SR_ERR_DATA;
672                 goto out;
673         }
674
675         /*
676          * Calculate the minimum buffer size to store the set of samples
677          * of all channels (unit size). Determine a larger buffer size
678          * for datafeed submission that is a multiple of the unit size.
679          * Allocate the larger buffer, and have the "sample buffer" point
680          * to a location within that large buffer.
681          */
682         inc->sample_unit_size = (inc->num_channels + 7) / 8;
683         inc->datafeed_buf_size = CHUNK_SIZE;
684         inc->datafeed_buf_size *= inc->sample_unit_size;
685         inc->datafeed_buffer = g_malloc(inc->datafeed_buf_size);
686         inc->datafeed_buf_fill = 0;
687         inc->sample_buffer = &inc->datafeed_buffer[inc->datafeed_buf_fill];
688
689 out:
690         if (columns)
691                 g_strfreev(columns);
692         g_strfreev(lines);
693
694         return ret;
695 }
696
697 /*
698  * Gets called from initial_receive(), which runs until the end-of-line
699  * encoding of the input stream could get determined. Assumes that this
700  * routine receives enough buffered initial input data to either see the
701  * BOM when there is one, or that no BOM will follow when a text line
702  * termination sequence was seen. Silently drops the UTF-8 BOM sequence
703  * from the input buffer if one was seen. Does not care to protect
704  * against multiple execution or dropping the BOM multiple times --
705  * there should be at most one in the input stream.
706  */
707 static void initial_bom_check(const struct sr_input *in)
708 {
709         static const char *utf8_bom = "\xef\xbb\xbf";
710
711         if (in->buf->len < strlen(utf8_bom))
712                 return;
713         if (strncmp(in->buf->str, utf8_bom, strlen(utf8_bom)) != 0)
714                 return;
715         g_string_erase(in->buf, 0, strlen(utf8_bom));
716 }
717
718 static int initial_receive(const struct sr_input *in)
719 {
720         struct context *inc;
721         GString *new_buf;
722         int len, ret;
723         char *p;
724         const char *termination;
725
726         initial_bom_check(in);
727
728         inc = in->priv;
729
730         termination = get_line_termination(in->buf);
731         if (!termination)
732                 /* Don't have a full line yet. */
733                 return SR_ERR_NA;
734
735         p = g_strrstr_len(in->buf->str, in->buf->len, termination);
736         if (!p)
737                 /* Don't have a full line yet. */
738                 return SR_ERR_NA;
739         len = p - in->buf->str - 1;
740         new_buf = g_string_new_len(in->buf->str, len);
741         g_string_append_c(new_buf, '\0');
742
743         inc->termination = g_strdup(termination);
744
745         if (in->buf->str[0] != '\0')
746                 ret = initial_parse(in, new_buf);
747         else
748                 ret = SR_OK;
749
750         g_string_free(new_buf, TRUE);
751
752         return ret;
753 }
754
755 static int process_buffer(struct sr_input *in, gboolean is_eof)
756 {
757         struct sr_datafeed_packet packet;
758         struct sr_datafeed_meta meta;
759         struct sr_config *src;
760         struct context *inc;
761         gsize num_columns;
762         uint64_t samplerate;
763         size_t max_columns, l;
764         int ret;
765         char *p, **lines, *line, **columns;
766
767         inc = in->priv;
768         if (!inc->started) {
769                 std_session_send_df_header(in->sdi);
770
771                 if (inc->samplerate) {
772                         packet.type = SR_DF_META;
773                         packet.payload = &meta;
774                         samplerate = inc->samplerate;
775                         src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate));
776                         meta.config = g_slist_append(NULL, src);
777                         sr_session_send(in->sdi, &packet);
778                         g_slist_free(meta.config);
779                         sr_config_free(src);
780                 }
781
782                 inc->started = TRUE;
783         }
784
785         /* Limit the number of columns to parse. */
786         if (inc->multi_column_mode)
787                 max_columns = inc->num_channels;
788         else
789                 max_columns = 1;
790
791         /*
792          * Consider empty input non-fatal. Keep accumulating input until
793          * at least one full text line has become available. Grab the
794          * maximum amount of accumulated data that consists of full text
795          * lines, and process what has been received so far, leaving not
796          * yet complete lines for the next invocation.
797          *
798          * Enforce that all previously buffered data gets processed in
799          * the "EOF" condition. Do not insist in the presence of the
800          * termination sequence for the last line (may often be missing
801          * on Windows). A present termination sequence will just result
802          * in the "execution of an empty line", and does not harm.
803          */
804         if (!in->buf->len)
805                 return SR_OK;
806         if (is_eof) {
807                 p = in->buf->str + in->buf->len;
808         } else {
809                 p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination);
810                 if (!p)
811                         return SR_ERR;
812                 *p = '\0';
813                 p += strlen(inc->termination);
814         }
815         g_strstrip(in->buf->str);
816
817         ret = SR_OK;
818         lines = g_strsplit_set(in->buf->str, delim_set, 0);
819         for (l = 0; lines[l]; l++) {
820                 inc->line_number++;
821                 line = lines[l];
822                 if (line[0] == '\0') {
823                         sr_spew("Blank line %zu skipped.", inc->line_number);
824                         continue;
825                 }
826
827                 /* Remove trailing comment. */
828                 strip_comment(line, inc->comment);
829                 if (line[0] == '\0') {
830                         sr_spew("Comment-only line %zu skipped.", inc->line_number);
831                         continue;
832                 }
833
834                 /* Skip the header line, its content was used as the channel names. */
835                 if (inc->header) {
836                         sr_spew("Header line %zu skipped.", inc->line_number);
837                         inc->header = FALSE;
838                         continue;
839                 }
840
841                 columns = parse_line(line, inc, max_columns);
842                 if (!columns) {
843                         sr_err("Error while parsing line %zu.", inc->line_number);
844                         g_strfreev(lines);
845                         return SR_ERR;
846                 }
847                 num_columns = g_strv_length(columns);
848                 if (!num_columns) {
849                         sr_err("Column %zu in line %zu is out of bounds.",
850                                 inc->first_column, inc->line_number);
851                         g_strfreev(columns);
852                         g_strfreev(lines);
853                         return SR_ERR;
854                 }
855                 /*
856                  * Ensure that the number of channels does not exceed the number
857                  * of columns in multi column mode.
858                  */
859                 if (inc->multi_column_mode && num_columns < inc->num_channels) {
860                         sr_err("Not enough columns for desired number of channels in line %zu.",
861                                 inc->line_number);
862                         g_strfreev(columns);
863                         g_strfreev(lines);
864                         return SR_ERR;
865                 }
866
867                 if (inc->multi_column_mode)
868                         ret = parse_multi_columns(columns, inc);
869                 else
870                         ret = parse_single_column(columns[0], inc);
871                 if (ret != SR_OK) {
872                         g_strfreev(columns);
873                         g_strfreev(lines);
874                         return SR_ERR;
875                 }
876
877                 /* Send sample data to the session bus. */
878                 ret = queue_samples(in);
879                 if (ret != SR_OK) {
880                         sr_err("Sending samples failed.");
881                         g_strfreev(columns);
882                         g_strfreev(lines);
883                         return SR_ERR;
884                 }
885
886                 g_strfreev(columns);
887         }
888         g_strfreev(lines);
889         g_string_erase(in->buf, 0, p - in->buf->str);
890
891         return ret;
892 }
893
894 static int receive(struct sr_input *in, GString *buf)
895 {
896         struct context *inc;
897         int ret;
898
899         g_string_append_len(in->buf, buf->str, buf->len);
900
901         inc = in->priv;
902         if (!inc->termination) {
903                 ret = initial_receive(in);
904                 if (ret == SR_ERR_NA)
905                         /* Not enough data yet. */
906                         return SR_OK;
907                 else if (ret != SR_OK)
908                         return SR_ERR;
909
910                 /* sdi is ready, notify frontend. */
911                 in->sdi_ready = TRUE;
912                 return SR_OK;
913         }
914
915         ret = process_buffer(in, FALSE);
916
917         return ret;
918 }
919
920 static int end(struct sr_input *in)
921 {
922         struct context *inc;
923         int ret;
924
925         if (in->sdi_ready)
926                 ret = process_buffer(in, TRUE);
927         else
928                 ret = SR_OK;
929         if (ret != SR_OK)
930                 return ret;
931
932         ret = flush_samples(in);
933         if (ret != SR_OK)
934                 return ret;
935
936         inc = in->priv;
937         if (inc->started)
938                 std_session_send_df_end(in->sdi);
939
940         return ret;
941 }
942
943 static void cleanup(struct sr_input *in)
944 {
945         struct context *inc;
946
947         keep_header_for_reread(in);
948
949         inc = in->priv;
950
951         g_free(inc->termination);
952         inc->termination = NULL;
953         g_free(inc->datafeed_buffer);
954         inc->datafeed_buffer = NULL;
955 }
956
957 static int reset(struct sr_input *in)
958 {
959         struct context *inc = in->priv;
960
961         cleanup(in);
962         inc->started = FALSE;
963         g_string_truncate(in->buf, 0);
964
965         return SR_OK;
966 }
967
968 static struct sr_option options[] = {
969         { "single-column", "Single column", "Enable single-column mode, using the specified column (>= 1); 0: multi-col. mode", NULL, NULL },
970         { "numchannels", "Number of logic channels", "The number of (logic) channels (single-col. mode: number of bits beginning at 'first channel', LSB-first)", NULL, NULL },
971         { "delimiter", "Column delimiter", "The column delimiter (>= 1 characters)", NULL, NULL },
972         { "format", "Data format (single-col. mode)", "The numeric format of the data (single-col. mode): bin, hex, oct", NULL, NULL },
973         { "comment", "Comment character(s)", "The comment prefix character(s)", NULL, NULL },
974         { "samplerate", "Samplerate (Hz)", "The sample rate (used during capture) in Hz", NULL, NULL },
975         { "first-channel", "First channel", "The column number of the first channel (multi-col. mode); bit position for the first channel (single-col. mode)", NULL, NULL },
976         { "header", "Interpret first line as header (multi-col. mode)", "Treat the first line as header with channel names (multi-col. mode)", NULL, NULL },
977         { "startline", "Start line", "The line number at which to start processing samples (>= 1)", NULL, NULL },
978         ALL_ZERO
979 };
980
981 static const struct sr_option *get_options(void)
982 {
983         GSList *l;
984
985         if (!options[0].def) {
986                 options[0].def = g_variant_ref_sink(g_variant_new_int32(0));
987                 options[1].def = g_variant_ref_sink(g_variant_new_int32(0));
988                 options[2].def = g_variant_ref_sink(g_variant_new_string(","));
989                 options[3].def = g_variant_ref_sink(g_variant_new_string("bin"));
990                 l = NULL;
991                 l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("bin")));
992                 l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("hex")));
993                 l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("oct")));
994                 options[3].values = l;
995                 options[4].def = g_variant_ref_sink(g_variant_new_string(";"));
996                 options[5].def = g_variant_ref_sink(g_variant_new_uint64(0));
997                 options[6].def = g_variant_ref_sink(g_variant_new_int32(0));
998                 options[7].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
999                 options[8].def = g_variant_ref_sink(g_variant_new_int32(1));
1000         }
1001
1002         return options;
1003 }
1004
1005 SR_PRIV struct sr_input_module input_csv = {
1006         .id = "csv",
1007         .name = "CSV",
1008         .desc = "Comma-separated values",
1009         .exts = (const char*[]){"csv", NULL},
1010         .options = get_options,
1011         .init = init,
1012         .receive = receive,
1013         .end = end,
1014         .cleanup = cleanup,
1015         .reset = reset,
1016 };