]> sigrok.org Git - sigrok-cli.git/blame - parsers.c
Don't open an output file in the case of srzip.
[sigrok-cli.git] / parsers.c
CommitLineData
43e5747a 1/*
630293b4 2 * This file is part of the sigrok-cli project.
43e5747a
UH
3 *
4 * Copyright (C) 2011 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
20#include <stdio.h>
21#include <stdlib.h>
22#include <stdint.h>
23#include <string.h>
24#include <glib.h>
662a1e27 25#include "sigrok-cli.h"
43e5747a 26
029d73fe 27struct sr_channel *find_channel(GSList *channellist, const char *channelname)
43e5747a 28{
029d73fe 29 struct sr_channel *ch;
497f5362 30 GSList *l;
43e5747a 31
029d73fe
UH
32 ch = NULL;
33 for (l = channellist; l; l = l->next) {
34 ch = l->data;
35 if (!strcmp(ch->name, channelname))
497f5362 36 break;
c2c4a0de 37 }
029d73fe 38 ch = l ? l->data : NULL;
497f5362 39
029d73fe 40 return ch;
497f5362
BV
41}
42
029d73fe 43GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
497f5362 44{
029d73fe 45 struct sr_channel *ch;
c6fa2b2e 46 GSList *channellist, *channels;
497f5362
BV
47 int ret, n, b, e, i;
48 char **tokens, **range, **names, *eptr, str[8];
43e5747a 49
c6fa2b2e
UH
50 channels = sr_dev_inst_channels_get(sdi);
51
029d73fe
UH
52 if (!channelstring || !channelstring[0])
53 /* Use all channels by default. */
c6fa2b2e 54 return g_slist_copy(channels);
497f5362
BV
55
56 ret = SR_OK;
57 range = NULL;
66149c20 58 names = NULL;
029d73fe
UH
59 channellist = NULL;
60 tokens = g_strsplit(channelstring, ",", 0);
43e5747a 61 for (i = 0; tokens[i]; i++) {
497f5362 62 if (tokens[i][0] == '\0') {
029d73fe 63 g_critical("Invalid empty channel.");
497f5362
BV
64 ret = SR_ERR;
65 break;
66 }
43e5747a 67 if (strchr(tokens[i], '-')) {
f0f54487
UH
68 /*
69 * A range of channels in the form a-b. This will only work
029d73fe
UH
70 * if the channels are named as numbers -- so every channel
71 * in the range must exist as a channel name string in the
f0f54487
UH
72 * device.
73 */
43e5747a
UH
74 range = g_strsplit(tokens[i], "-", 2);
75 if (!range[0] || !range[1] || range[2]) {
76 /* Need exactly two arguments. */
029d73fe 77 g_critical("Invalid channel syntax '%s'.", tokens[i]);
497f5362 78 ret = SR_ERR;
8ec20386 79 goto range_fail;
43e5747a
UH
80 }
81
497f5362
BV
82 b = strtol(range[0], &eptr, 10);
83 if (eptr == range[0] || *eptr != '\0') {
029d73fe 84 g_critical("Invalid channel '%s'.", range[0]);
497f5362 85 ret = SR_ERR;
8ec20386 86 goto range_fail;
497f5362 87 }
43e5747a 88 e = strtol(range[1], NULL, 10);
497f5362 89 if (eptr == range[1] || *eptr != '\0') {
029d73fe 90 g_critical("Invalid channel '%s'.", range[1]);
497f5362 91 ret = SR_ERR;
8ec20386 92 goto range_fail;
497f5362
BV
93 }
94 if (b < 0 || b >= e) {
029d73fe 95 g_critical("Invalid channel range '%s'.", tokens[i]);
497f5362 96 ret = SR_ERR;
8ec20386 97 goto range_fail;
43e5747a
UH
98 }
99
100 while (b <= e) {
497f5362
BV
101 n = snprintf(str, 8, "%d", b);
102 if (n < 0 || n > 8) {
029d73fe 103 g_critical("Invalid channel '%d'.", b);
497f5362
BV
104 ret = SR_ERR;
105 break;
106 }
c6fa2b2e 107 ch = find_channel(channels, str);
029d73fe
UH
108 if (!ch) {
109 g_critical("unknown channel '%d'.", b);
497f5362
BV
110 ret = SR_ERR;
111 break;
112 }
029d73fe 113 channellist = g_slist_append(channellist, ch);
43e5747a
UH
114 b++;
115 }
8ec20386
DJ
116range_fail:
117 if (range)
118 g_strfreev(range);
119
497f5362
BV
120 if (ret != SR_OK)
121 break;
43e5747a 122 } else {
497f5362
BV
123 names = g_strsplit(tokens[i], "=", 2);
124 if (!names[0] || (names[1] && names[2])) {
125 /* Need one or two arguments. */
029d73fe 126 g_critical("Invalid channel '%s'.", tokens[i]);
6df458b7 127 g_strfreev(names);
497f5362 128 ret = SR_ERR;
43e5747a
UH
129 break;
130 }
131
c6fa2b2e 132 ch = find_channel(channels, names[0]);
029d73fe
UH
133 if (!ch) {
134 g_critical("unknown channel '%s'.", names[0]);
6df458b7 135 g_strfreev(names);
497f5362
BV
136 ret = SR_ERR;
137 break;
138 }
139 if (names[1]) {
029d73fe
UH
140 /* Rename channel. */
141 g_free(ch->name);
142 ch->name = g_strdup(names[1]);
43e5747a 143 }
029d73fe 144 channellist = g_slist_append(channellist, ch);
8ec20386 145
6df458b7 146 g_strfreev(names);
43e5747a
UH
147 }
148 }
66149c20 149
497f5362 150 if (ret != SR_OK) {
029d73fe
UH
151 g_slist_free(channellist);
152 channellist = NULL;
43e5747a
UH
153 }
154
155 g_strfreev(tokens);
43e5747a 156
029d73fe 157 return channellist;
43e5747a
UH
158}
159
6b27bde4
BV
160int parse_trigger_match(char c)
161{
162 int match;
163
164 if (c == '0')
165 match = SR_TRIGGER_ZERO;
166 else if (c == '1')
167 match = SR_TRIGGER_ONE;
168 else if (c == 'r')
169 match = SR_TRIGGER_RISING;
170 else if (c == 'f')
171 match = SR_TRIGGER_FALLING;
172 else if (c == 'e')
173 match = SR_TRIGGER_EDGE;
174 else if (c == 'o')
175 match = SR_TRIGGER_OVER;
176 else if (c == 'u')
177 match = SR_TRIGGER_UNDER;
178 else
179 match = 0;
180
181 return match;
182}
183
4bf77ec6
BV
184int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
185 struct sr_trigger **trigger)
6b27bde4
BV
186{
187 struct sr_channel *ch;
6b27bde4
BV
188 struct sr_trigger_stage *stage;
189 GVariant *gvar;
c6fa2b2e 190 GSList *l, *channels;
6b27bde4
BV
191 gsize num_matches;
192 gboolean found_match, error;
193 const int32_t *matches;
194 int32_t match;
195 unsigned int j;
196 int t, i;
197 char **tokens, *sep;
c6fa2b2e
UH
198 struct sr_dev_driver *driver;
199
200 driver = sr_dev_inst_driver_get(sdi);
201 channels = sr_dev_inst_channels_get(sdi);
6b27bde4 202
24bd9719 203 if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
6b27bde4
BV
204 &gvar) != SR_OK) {
205 g_critical("Device doesn't support any triggers.");
206 return FALSE;
207 }
208 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
209
4bf77ec6 210 *trigger = sr_trigger_new(NULL);
6b27bde4
BV
211 error = FALSE;
212 tokens = g_strsplit(s, ",", -1);
213 for (i = 0; tokens[i]; i++) {
214 if (!(sep = strchr(tokens[i], '='))) {
215 g_critical("Invalid trigger '%s'.", tokens[i]);
216 error = TRUE;
217 break;
218 }
219 *sep++ = 0;
220 ch = NULL;
c6fa2b2e 221 for (l = channels; l; l = l->next) {
6b27bde4
BV
222 ch = l->data;
223 if (ch->enabled && !strcmp(ch->name, tokens[i]))
224 break;
225 ch = NULL;
226 }
227 if (!ch) {
228 g_critical("Invalid channel '%s'.", tokens[i]);
229 error = TRUE;
230 break;
231 }
232 for (t = 0; sep[t]; t++) {
233 if (!(match = parse_trigger_match(sep[t]))) {
234 g_critical("Invalid trigger match '%c'.", sep[t]);
235 error = TRUE;
236 break;
237 }
238 found_match = FALSE;
239 for (j = 0; j < num_matches; j++) {
240 if (matches[j] == match) {
241 found_match = TRUE;
242 break;
243 }
244 }
245 if (!found_match) {
246 g_critical("Trigger match '%c' not supported by device.", sep[t]);
247 error = TRUE;
248 break;
249 }
250 /* Make sure this ends up in the right stage, creating
251 * them as needed. */
4bf77ec6
BV
252 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
253 sr_trigger_stage_add(*trigger);
6b27bde4
BV
254 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
255 error = TRUE;
256 break;
257 }
258 }
259 }
260 g_strfreev(tokens);
261 g_variant_unref(gvar);
262
263 if (error)
4bf77ec6 264 sr_trigger_free(*trigger);
6b27bde4
BV
265
266 return !error;
267}
268
63bb454c 269GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
43e5747a
UH
270{
271 GHashTable *hash;
272 int i;
273 char **elements, *e;
274
275 if (!arg || !arg[0])
276 return NULL;
277
63bb454c
BV
278 i = 0;
279 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
280 g_free, g_free);
43e5747a 281 elements = g_strsplit(arg, ":", 0);
63bb454c
BV
282 if (sep_first)
283 g_hash_table_insert(hash, g_strdup("sigrok_key"),
284 g_strdup(elements[i++]));
285 for (; elements[i]; i++) {
43e5747a
UH
286 e = strchr(elements[i], '=');
287 if (!e)
288 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
289 else {
290 *e++ = '\0';
291 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
292 }
293 }
294 g_strfreev(elements);
295
296 return hash;
297}
298
87e24fed
BV
299GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
300{
301 GHashTable *hash;
302 GVariant *gvar;
303 int i;
304 char *s;
41ef1ae4 305 gboolean b;
87e24fed
BV
306
307 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
308 (GDestroyNotify)g_variant_unref);
309 for (i = 0; opts[i]; i++) {
310 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
311 continue;
312 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
313 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
314 g_hash_table_insert(hash, g_strdup(opts[i]->id),
315 g_variant_ref_sink(gvar));
316 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
317 gvar = g_variant_new_int32(strtoul(s, NULL, 10));
318 g_hash_table_insert(hash, g_strdup(opts[i]->id),
319 g_variant_ref_sink(gvar));
902e368e
BV
320 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
321 gvar = g_variant_new_uint64(strtoul(s, NULL, 10));
322 g_hash_table_insert(hash, g_strdup(opts[i]->id),
323 g_variant_ref_sink(gvar));
87e24fed
BV
324 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
325 gvar = g_variant_new_double(strtod(s, NULL));
326 g_hash_table_insert(hash, g_strdup(opts[i]->id),
327 g_variant_ref_sink(gvar));
328 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
329 gvar = g_variant_new_string(s);
330 g_hash_table_insert(hash, g_strdup(opts[i]->id),
331 g_variant_ref_sink(gvar));
41ef1ae4
JS
332 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
333 b = TRUE;
334 if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
335 b = FALSE;
336 } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
337 g_critical("Unable to convert '%s' to boolean!", s);
338 }
339
340 gvar = g_variant_new_boolean(b);
341 g_hash_table_insert(hash, g_strdup(opts[i]->id),
342 g_variant_ref_sink(gvar));
87e24fed
BV
343 } else {
344 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
345 }
346 }
347
348 return hash;
349}
350
2be182e6 351static char *strcanon(const char *str)
432de709
BV
352{
353 int p0, p1;
354 char *s;
355
356 /* Returns newly allocated string. */
357 s = g_ascii_strdown(str, -1);
358 for (p0 = p1 = 0; str[p0]; p0++) {
359 if ((s[p0] >= 'a' && s[p0] <= 'z')
360 || (s[p0] >= '0' && s[p0] <= '9'))
361 s[p1++] = s[p0];
362 }
363 s[p1] = '\0';
364
365 return s;
366}
367
d2ee5eea 368int canon_cmp(const char *str1, const char *str2)
432de709
BV
369{
370 int ret;
371 char *s1, *s2;
372
373 s1 = strcanon(str1);
374 s2 = strcanon(str2);
375 ret = g_ascii_strcasecmp(s1, s2);
376 g_free(s2);
377 g_free(s1);
378
379 return ret;
380}
d75c8529
BV
381
382/* Convert driver options hash to GSList of struct sr_config. */
383static GSList *hash_to_hwopt(GHashTable *hash)
384{
385 struct sr_config *src;
386 GList *gl, *keys;
387 GSList *opts;
388 char *key;
389
390 keys = g_hash_table_get_keys(hash);
391 opts = NULL;
392 for (gl = keys; gl; gl = gl->next) {
393 key = gl->data;
394 src = g_malloc(sizeof(struct sr_config));
395 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
396 return NULL;
397 opts = g_slist_append(opts, src);
398 }
399 g_list_free(keys);
400
401 return opts;
402}
403
404int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
405{
406 struct sr_dev_driver **drivers;
407 GHashTable *drvargs;
408 int i;
409 char *drvname;
410
bc5b66d7
JS
411 if (!arg)
412 return FALSE;
413
d75c8529
BV
414 drvargs = parse_generic_arg(arg, TRUE);
415
416 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
417 g_hash_table_remove(drvargs, "sigrok_key");
418 *driver = NULL;
59e421bb 419 drivers = sr_driver_list(sr_ctx);
d75c8529
BV
420 for (i = 0; drivers[i]; i++) {
421 if (strcmp(drivers[i]->name, drvname))
422 continue;
423 *driver = drivers[i];
424 }
425 if (!*driver) {
426 g_critical("Driver %s not found.", drvname);
427 g_hash_table_destroy(drvargs);
428 g_free(drvname);
429 return FALSE;
430 }
431 g_free(drvname);
432 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
433 g_critical("Failed to initialize driver.");
434 g_hash_table_destroy(drvargs);
435 return FALSE;
436 }
437
438 if (drvopts) {
439 *drvopts = NULL;
440 if (g_hash_table_size(drvargs) > 0) {
441 if (!(*drvopts = hash_to_hwopt(drvargs))) {
442 /* Unknown options, already logged. */
443 g_hash_table_destroy(drvargs);
444 return FALSE;
445 }
446 }
447 }
448
449 g_hash_table_destroy(drvargs);
450
451 return TRUE;
452}