]> sigrok.org Git - sigrok-cli.git/blame - input.c
parsers: avoid NULL dereference when option strings are empty
[sigrok-cli.git] / input.c
CommitLineData
2be182e6
BV
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
d486cbdd 20#include <config.h>
a8b4041a 21#include <sys/types.h>
8d52f788 22#include <sys/stat.h>
a8b4041a
BV
23#include <fcntl.h>
24#include <unistd.h>
8d52f788
DH
25#include <errno.h>
26#include <stdlib.h>
27#include <string.h>
2be182e6 28#include <glib.h>
662a1e27 29#include "sigrok-cli.h"
2be182e6 30
7ac79fb6 31#define CHUNK_SIZE (4 * 1024 * 1024)
2be182e6 32
1c950633 33static void load_input_file_module(struct df_arg_desc *df_arg)
2be182e6 34{
4bf77ec6 35 struct sr_session *session;
a8b4041a
BV
36 const struct sr_input *in;
37 const struct sr_input_module *imod;
38 const struct sr_option **options;
39 struct sr_dev_inst *sdi;
40 GHashTable *mod_args, *mod_opts;
41 GString *buf;
64f9f5b2
BV
42 gboolean got_sdi;
43 int fd;
a8b4041a
BV
44 ssize_t len;
45 char *mod_id;
a30c837a 46 gboolean is_stdin;
2b29fb39 47 gboolean push_scan_data;
2be182e6 48
a8b4041a
BV
49 if (!sr_input_list())
50 g_critical("No supported input formats available.");
2be182e6 51
a8b4041a
BV
52 mod_id = NULL;
53 mod_args = NULL;
54 if (opt_input_format) {
cad0cba6 55 mod_args = parse_generic_arg(opt_input_format, TRUE, NULL);
a8b4041a 56 mod_id = g_hash_table_lookup(mod_args, "sigrok_key");
2be182e6
BV
57 }
58
a30c837a 59 is_stdin = strcmp(opt_input_file, "-") == 0;
2b29fb39 60 push_scan_data = FALSE;
64f9f5b2 61 fd = 0;
7ac79fb6 62 buf = g_string_sized_new(CHUNK_SIZE);
a8b4041a
BV
63 if (mod_id) {
64 /* User specified an input module to use. */
65 if (!(imod = sr_input_find(mod_id)))
66 g_critical("Error: unknown input module '%s'.", mod_id);
67 g_hash_table_remove(mod_args, "sigrok_key");
68 if ((options = sr_input_options_get(imod))) {
69 mod_opts = generic_arg_to_opt(options, mod_args);
cfad6a30 70 (void)warn_unknown_keys(options, mod_args, NULL);
f3e44829 71 sr_input_options_free(options);
cfad6a30 72 } else {
a8b4041a 73 mod_opts = NULL;
cfad6a30 74 }
a8b4041a
BV
75 if (!(in = sr_input_new(imod, mod_opts)))
76 g_critical("Error: failed to initialize input module.");
77 if (mod_opts)
78 g_hash_table_destroy(mod_opts);
79 if (mod_args)
80 g_hash_table_destroy(mod_args);
a30c837a 81 if (!is_stdin && (fd = open(opt_input_file, O_RDONLY)) < 0)
a8b4041a 82 g_critical("Failed to load %s: %s.", opt_input_file,
19bdd3dd 83 g_strerror(errno));
a8b4041a 84 } else {
a30c837a 85 if (!is_stdin) {
47f97410
BV
86 /*
87 * An actual filename: let the input modules try to
88 * identify the file.
89 */
b66260b9
BV
90 if (sr_input_scan_file(opt_input_file, &in) == SR_OK) {
91 /* That worked, reopen the file for reading. */
92 fd = open(opt_input_file, O_RDONLY);
93 }
47f97410
BV
94 } else {
95 /*
96 * Taking input from a pipe: let the input modules try
97 * to identify the stream content.
98 */
a30c837a 99 if (is_stdin) {
47f97410
BV
100 /* stdin */
101 fd = 0;
102 } else {
a30c837a
GS
103 fd = open(opt_input_file, O_RDONLY);
104 if (fd == -1)
47f97410 105 g_critical("Failed to load %s: %s.", opt_input_file,
19bdd3dd 106 g_strerror(errno));
47f97410 107 }
f3e44829 108 if ((len = read(fd, buf->str, buf->allocated_len)) < 1)
47f97410 109 g_critical("Failed to read %s: %s.", opt_input_file,
19bdd3dd 110 g_strerror(errno));
47f97410 111 buf->len = len;
b66260b9 112 sr_input_scan_buffer(buf, &in);
2b29fb39 113 push_scan_data = TRUE;
47f97410
BV
114 }
115 if (!in)
a8b4041a 116 g_critical("Error: no input module found for this file.");
2be182e6 117 }
3d24ca2d 118 sr_session_new(sr_ctx, &session);
1c950633
GS
119 df_arg->session = session;
120 sr_session_datafeed_callback_add(session, datafeed_in, df_arg);
2be182e6 121
2b29fb39
GS
122 /*
123 * Implementation detail: The combination of reading from stdin
124 * and automatic file format detection may have pushed the first
125 * chunk of input data into the input module's data accumulator,
126 * _bypassing_ the .receive() callback. It is essential to call
127 * .receive() before calling .end() for files of size smaller than
128 * CHUNK_SIZE (which is a typical case). So that sdi becomes ready.
129 * Fortunately all input modules accept .receive() calls with
130 * a zero length, and inspect whatever was accumulated so far.
131 *
132 * After that optional initial push of data which was queued
133 * above during format detection, continue reading remaining
134 * chunks from the input file until EOF is seen.
135 */
64f9f5b2 136 got_sdi = FALSE;
f0f54487 137 while (TRUE) {
a8b4041a 138 g_string_truncate(buf, 0);
2b29fb39
GS
139 if (push_scan_data)
140 len = 0;
141 else
f3e44829 142 len = read(fd, buf->str, buf->allocated_len);
a8b4041a 143 if (len < 0)
19bdd3dd 144 g_critical("Read failed: %s", g_strerror(errno));
2b29fb39 145 if (len == 0 && !push_scan_data)
64f9f5b2
BV
146 /* End of file or stream. */
147 break;
2b29fb39 148 push_scan_data = FALSE;
a8b4041a 149 buf->len = len;
a9b64d89
GS
150 if (sr_input_send(in, buf) != SR_OK) {
151 g_critical("File import failed (read)");
a8b4041a 152 break;
a9b64d89 153 }
64f9f5b2
BV
154
155 sdi = sr_input_dev_inst_get(in);
f0f54487 156 if (!got_sdi && sdi) {
64f9f5b2 157 /* First time we got a valid sdi. */
a9b64d89
GS
158 if (select_channels(sdi) != SR_OK) {
159 g_critical("File import failed (channels)");
64f9f5b2 160 return;
a9b64d89 161 }
64f9f5b2
BV
162 if (sr_session_dev_add(session, sdi) != SR_OK) {
163 g_critical("Failed to use device.");
164 sr_session_destroy(session);
165 return;
166 }
167 got_sdi = TRUE;
168 }
a8b4041a 169 }
67a00747 170 sr_input_end(in);
a8b4041a
BV
171 sr_input_free(in);
172 g_string_free(buf, TRUE);
f3e44829 173 close(fd);
2be182e6 174
1c950633 175 df_arg->session = NULL;
4bf77ec6 176 sr_session_destroy(session);
2be182e6
BV
177}
178
1c950633 179void load_input_file(gboolean do_props)
2be182e6 180{
1c950633 181 struct df_arg_desc df_arg;
4bf77ec6 182 struct sr_session *session;
e1ec80fa 183 struct sr_dev_inst *sdi;
a8b4041a 184 GSList *devices;
1ef118ab 185 GMainLoop *main_loop;
e1ec80fa 186 int ret;
2be182e6 187
1c950633
GS
188 memset(&df_arg, 0, sizeof(df_arg));
189 df_arg.do_props = do_props;
190
f5531096
BV
191 if (!strcmp(opt_input_file, "-")) {
192 /* Input from stdin is never a session file. */
1c950633 193 load_input_file_module(&df_arg);
f5531096 194 } else {
3d24ca2d
ML
195 if ((ret = sr_session_load(sr_ctx, opt_input_file,
196 &session)) == SR_OK) {
f5531096
BV
197 /* sigrok session file */
198 ret = sr_session_dev_list(session, &devices);
199 if (ret != SR_OK || !devices || !devices->data) {
200 g_critical("Failed to access session device.");
1ef118ab 201 g_slist_free(devices);
f5531096
BV
202 sr_session_destroy(session);
203 return;
204 }
205 sdi = devices->data;
1ef118ab 206 g_slist_free(devices);
f5531096
BV
207 if (select_channels(sdi) != SR_OK) {
208 sr_session_destroy(session);
209 return;
210 }
1ef118ab
DE
211 main_loop = g_main_loop_new(NULL, FALSE);
212
1c950633
GS
213 df_arg.session = session;
214 sr_session_datafeed_callback_add(session,
215 datafeed_in, &df_arg);
1ef118ab
DE
216 sr_session_stopped_callback_set(session,
217 (sr_session_stopped_callback)g_main_loop_quit,
218 main_loop);
219 if (sr_session_start(session) == SR_OK)
220 g_main_loop_run(main_loop);
221
222 g_main_loop_unref(main_loop);
1c950633 223 df_arg.session = NULL;
1ef118ab 224 sr_session_destroy(session);
f5531096
BV
225 } else if (ret != SR_ERR) {
226 /* It's a session file, but it didn't work out somehow. */
227 g_critical("Failed to load session file.");
228 } else {
229 /* Fall back on input modules. */
1c950633 230 load_input_file_module(&df_arg);
f5531096 231 }
2be182e6
BV
232 }
233}