]> sigrok.org Git - sigrok-cli.git/blame - input.c
Fix various clang warnings.
[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
f0f54487 31#define BUFSIZE (16 * 1024)
2be182e6 32
a8b4041a 33static void load_input_file_module(void)
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;
2be182e6 46
a8b4041a
BV
47 if (!sr_input_list())
48 g_critical("No supported input formats available.");
2be182e6 49
a8b4041a
BV
50 mod_id = NULL;
51 mod_args = NULL;
52 if (opt_input_format) {
53 mod_args = parse_generic_arg(opt_input_format, TRUE);
54 mod_id = g_hash_table_lookup(mod_args, "sigrok_key");
2be182e6
BV
55 }
56
64f9f5b2 57 fd = 0;
a8b4041a
BV
58 buf = g_string_sized_new(BUFSIZE);
59 if (mod_id) {
60 /* User specified an input module to use. */
61 if (!(imod = sr_input_find(mod_id)))
62 g_critical("Error: unknown input module '%s'.", mod_id);
63 g_hash_table_remove(mod_args, "sigrok_key");
64 if ((options = sr_input_options_get(imod))) {
65 mod_opts = generic_arg_to_opt(options, mod_args);
66 sr_output_options_free(options);
67 } else
68 mod_opts = NULL;
69 if (!(in = sr_input_new(imod, mod_opts)))
70 g_critical("Error: failed to initialize input module.");
71 if (mod_opts)
72 g_hash_table_destroy(mod_opts);
73 if (mod_args)
74 g_hash_table_destroy(mod_args);
75 if ((fd = open(opt_input_file, O_RDONLY)) == -1)
76 g_critical("Failed to load %s: %s.", opt_input_file,
19bdd3dd 77 g_strerror(errno));
a8b4041a 78 } else {
47f97410
BV
79 if (strcmp(opt_input_file, "-")) {
80 /*
81 * An actual filename: let the input modules try to
82 * identify the file.
83 */
b66260b9
BV
84 if (sr_input_scan_file(opt_input_file, &in) == SR_OK) {
85 /* That worked, reopen the file for reading. */
86 fd = open(opt_input_file, O_RDONLY);
87 }
47f97410
BV
88 } else {
89 /*
90 * Taking input from a pipe: let the input modules try
91 * to identify the stream content.
92 */
93 if (!strcmp(opt_input_file, "-")) {
94 /* stdin */
95 fd = 0;
96 } else {
97 if ((fd = open(opt_input_file, O_RDONLY)) == -1)
98 g_critical("Failed to load %s: %s.", opt_input_file,
19bdd3dd 99 g_strerror(errno));
47f97410
BV
100 }
101 if ((len = read(fd, buf->str, BUFSIZE)) < 1)
102 g_critical("Failed to read %s: %s.", opt_input_file,
19bdd3dd 103 g_strerror(errno));
47f97410 104 buf->len = len;
b66260b9 105 sr_input_scan_buffer(buf, &in);
47f97410
BV
106 }
107 if (!in)
a8b4041a 108 g_critical("Error: no input module found for this file.");
2be182e6 109 }
3d24ca2d 110 sr_session_new(sr_ctx, &session);
4bf77ec6 111 sr_session_datafeed_callback_add(session, &datafeed_in, NULL);
2be182e6 112
64f9f5b2 113 got_sdi = FALSE;
f0f54487 114 while (TRUE) {
a8b4041a
BV
115 g_string_truncate(buf, 0);
116 len = read(fd, buf->str, BUFSIZE);
117 if (len < 0)
19bdd3dd 118 g_critical("Read failed: %s", g_strerror(errno));
64f9f5b2
BV
119 if (len == 0)
120 /* End of file or stream. */
121 break;
a8b4041a
BV
122 buf->len = len;
123 if (sr_input_send(in, buf) != SR_OK)
124 break;
64f9f5b2
BV
125
126 sdi = sr_input_dev_inst_get(in);
f0f54487 127 if (!got_sdi && sdi) {
64f9f5b2
BV
128 /* First time we got a valid sdi. */
129 if (select_channels(sdi) != SR_OK)
130 return;
131 if (sr_session_dev_add(session, sdi) != SR_OK) {
132 g_critical("Failed to use device.");
133 sr_session_destroy(session);
134 return;
135 }
136 got_sdi = TRUE;
137 }
a8b4041a 138 }
67a00747 139 sr_input_end(in);
a8b4041a
BV
140 sr_input_free(in);
141 g_string_free(buf, TRUE);
2be182e6 142
4bf77ec6 143 sr_session_destroy(session);
2be182e6 144
2be182e6
BV
145}
146
147void load_input_file(void)
148{
4bf77ec6 149 struct sr_session *session;
e1ec80fa 150 struct sr_dev_inst *sdi;
a8b4041a 151 GSList *devices;
1ef118ab 152 GMainLoop *main_loop;
e1ec80fa 153 int ret;
2be182e6 154
f5531096
BV
155 if (!strcmp(opt_input_file, "-")) {
156 /* Input from stdin is never a session file. */
a8b4041a 157 load_input_file_module();
f5531096 158 } else {
3d24ca2d
ML
159 if ((ret = sr_session_load(sr_ctx, opt_input_file,
160 &session)) == SR_OK) {
f5531096
BV
161 /* sigrok session file */
162 ret = sr_session_dev_list(session, &devices);
163 if (ret != SR_OK || !devices || !devices->data) {
164 g_critical("Failed to access session device.");
1ef118ab 165 g_slist_free(devices);
f5531096
BV
166 sr_session_destroy(session);
167 return;
168 }
169 sdi = devices->data;
1ef118ab 170 g_slist_free(devices);
f5531096
BV
171 if (select_channels(sdi) != SR_OK) {
172 sr_session_destroy(session);
173 return;
174 }
1ef118ab
DE
175 main_loop = g_main_loop_new(NULL, FALSE);
176
f5531096 177 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
1ef118ab
DE
178 sr_session_stopped_callback_set(session,
179 (sr_session_stopped_callback)g_main_loop_quit,
180 main_loop);
181 if (sr_session_start(session) == SR_OK)
182 g_main_loop_run(main_loop);
183
184 g_main_loop_unref(main_loop);
185 sr_session_destroy(session);
f5531096
BV
186 } else if (ret != SR_ERR) {
187 /* It's a session file, but it didn't work out somehow. */
188 g_critical("Failed to load session file.");
189 } else {
190 /* Fall back on input modules. */
191 load_input_file_module();
192 }
2be182e6
BV
193 }
194}