decode: Prepare to rework text output for protocol decoder annotations
[sigrok-cli.git] / parsers.c
blob5ec655f78ad3dce8a05f01186a2a7ec61febb437
1 /*
2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <glib.h>
26 #include "sigrok-cli.h"
28 struct sr_channel *find_channel(GSList *channellist, const char *channelname)
30 struct sr_channel *ch;
31 GSList *l;
33 ch = NULL;
34 for (l = channellist; l; l = l->next) {
35 ch = l->data;
36 if (!strcmp(ch->name, channelname))
37 break;
39 ch = l ? l->data : NULL;
41 return ch;
44 GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
46 struct sr_channel *ch;
47 GSList *channellist, *channels;
48 int ret, n, b, e, i;
49 char **tokens, **range, **names, *eptr, str[8];
51 channels = sr_dev_inst_channels_get(sdi);
53 if (!channelstring || !channelstring[0])
54 /* Use all channels by default. */
55 return g_slist_copy(channels);
57 ret = SR_OK;
58 range = NULL;
59 names = NULL;
60 channellist = NULL;
61 tokens = g_strsplit(channelstring, ",", 0);
62 for (i = 0; tokens[i]; i++) {
63 if (tokens[i][0] == '\0') {
64 g_critical("Invalid empty channel.");
65 ret = SR_ERR;
66 break;
68 if (strchr(tokens[i], '-')) {
70 * A range of channels in the form a-b. This will only work
71 * if the channels are named as numbers -- so every channel
72 * in the range must exist as a channel name string in the
73 * device.
75 range = g_strsplit(tokens[i], "-", 2);
76 if (!range[0] || !range[1] || range[2]) {
77 /* Need exactly two arguments. */
78 g_critical("Invalid channel syntax '%s'.", tokens[i]);
79 ret = SR_ERR;
80 goto range_fail;
83 b = strtol(range[0], &eptr, 10);
84 if (eptr == range[0] || *eptr != '\0') {
85 g_critical("Invalid channel '%s'.", range[0]);
86 ret = SR_ERR;
87 goto range_fail;
89 e = strtol(range[1], NULL, 10);
90 if (eptr == range[1] || *eptr != '\0') {
91 g_critical("Invalid channel '%s'.", range[1]);
92 ret = SR_ERR;
93 goto range_fail;
95 if (b < 0 || b >= e) {
96 g_critical("Invalid channel range '%s'.", tokens[i]);
97 ret = SR_ERR;
98 goto range_fail;
101 while (b <= e) {
102 n = snprintf(str, 8, "%d", b);
103 if (n < 0 || n > 8) {
104 g_critical("Invalid channel '%d'.", b);
105 ret = SR_ERR;
106 break;
108 ch = find_channel(channels, str);
109 if (!ch) {
110 g_critical("unknown channel '%d'.", b);
111 ret = SR_ERR;
112 break;
114 channellist = g_slist_append(channellist, ch);
115 b++;
117 range_fail:
118 if (range)
119 g_strfreev(range);
121 if (ret != SR_OK)
122 break;
123 } else {
124 names = g_strsplit(tokens[i], "=", 2);
125 if (!names[0] || (names[1] && names[2])) {
126 /* Need one or two arguments. */
127 g_critical("Invalid channel '%s'.", tokens[i]);
128 g_strfreev(names);
129 ret = SR_ERR;
130 break;
133 ch = find_channel(channels, names[0]);
134 if (!ch) {
135 g_critical("unknown channel '%s'.", names[0]);
136 g_strfreev(names);
137 ret = SR_ERR;
138 break;
140 if (names[1]) {
141 /* Rename channel. */
142 g_free(ch->name);
143 ch->name = g_strdup(names[1]);
145 channellist = g_slist_append(channellist, ch);
147 g_strfreev(names);
151 if (ret != SR_OK) {
152 g_slist_free(channellist);
153 channellist = NULL;
156 g_strfreev(tokens);
158 return channellist;
161 int parse_trigger_match(char c)
163 int match;
165 if (c == '0')
166 match = SR_TRIGGER_ZERO;
167 else if (c == '1')
168 match = SR_TRIGGER_ONE;
169 else if (c == 'r')
170 match = SR_TRIGGER_RISING;
171 else if (c == 'f')
172 match = SR_TRIGGER_FALLING;
173 else if (c == 'e')
174 match = SR_TRIGGER_EDGE;
175 else if (c == 'o')
176 match = SR_TRIGGER_OVER;
177 else if (c == 'u')
178 match = SR_TRIGGER_UNDER;
179 else
180 match = 0;
182 return match;
185 int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
186 struct sr_trigger **trigger)
188 struct sr_channel *ch;
189 struct sr_trigger_stage *stage;
190 GVariant *gvar;
191 GSList *l, *channels;
192 gsize num_matches;
193 gboolean found_match, error;
194 const int32_t *matches;
195 int32_t match;
196 unsigned int j;
197 int t, i;
198 char **tokens, *sep;
199 struct sr_dev_driver *driver;
201 driver = sr_dev_inst_driver_get(sdi);
202 channels = sr_dev_inst_channels_get(sdi);
204 if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
205 &gvar) != SR_OK) {
206 g_critical("Device doesn't support any triggers.");
207 return FALSE;
209 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
211 *trigger = sr_trigger_new(NULL);
212 error = FALSE;
213 tokens = g_strsplit(s, ",", -1);
214 for (i = 0; tokens[i]; i++) {
215 if (!(sep = strchr(tokens[i], '='))) {
216 g_critical("Invalid trigger '%s'.", tokens[i]);
217 error = TRUE;
218 break;
220 *sep++ = 0;
221 ch = NULL;
222 for (l = channels; l; l = l->next) {
223 ch = l->data;
224 if (ch->enabled && !strcmp(ch->name, tokens[i]))
225 break;
226 ch = NULL;
228 if (!ch) {
229 g_critical("Invalid channel '%s'.", tokens[i]);
230 error = TRUE;
231 break;
233 for (t = 0; sep[t]; t++) {
234 if (!(match = parse_trigger_match(sep[t]))) {
235 g_critical("Invalid trigger match '%c'.", sep[t]);
236 error = TRUE;
237 break;
239 found_match = FALSE;
240 for (j = 0; j < num_matches; j++) {
241 if (matches[j] == match) {
242 found_match = TRUE;
243 break;
246 if (!found_match) {
247 g_critical("Trigger match '%c' not supported by device.", sep[t]);
248 error = TRUE;
249 break;
251 /* Make sure this ends up in the right stage, creating
252 * them as needed. */
253 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
254 sr_trigger_stage_add(*trigger);
255 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
256 error = TRUE;
257 break;
261 g_strfreev(tokens);
262 g_variant_unref(gvar);
264 if (error)
265 sr_trigger_free(*trigger);
267 return !error;
270 GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
272 GHashTable *hash;
273 int i;
274 char **elements, *e;
276 if (!arg || !arg[0])
277 return NULL;
279 i = 0;
280 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
281 g_free, g_free);
282 elements = g_strsplit(arg, ":", 0);
283 if (sep_first)
284 g_hash_table_insert(hash, g_strdup("sigrok_key"),
285 g_strdup(elements[i++]));
286 for (; elements[i]; i++) {
287 e = strchr(elements[i], '=');
288 if (!e)
289 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
290 else {
291 *e++ = '\0';
292 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
295 g_strfreev(elements);
297 return hash;
300 GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
302 GHashTable *hash;
303 GVariant *gvar;
304 int i;
305 char *s;
306 gboolean b;
308 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
309 (GDestroyNotify)g_variant_unref);
310 for (i = 0; opts[i]; i++) {
311 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
312 continue;
313 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
314 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
315 g_hash_table_insert(hash, g_strdup(opts[i]->id),
316 g_variant_ref_sink(gvar));
317 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
318 gvar = g_variant_new_int32(strtoul(s, NULL, 10));
319 g_hash_table_insert(hash, g_strdup(opts[i]->id),
320 g_variant_ref_sink(gvar));
321 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
322 gvar = g_variant_new_uint64(strtoul(s, NULL, 10));
323 g_hash_table_insert(hash, g_strdup(opts[i]->id),
324 g_variant_ref_sink(gvar));
325 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
326 gvar = g_variant_new_double(strtod(s, NULL));
327 g_hash_table_insert(hash, g_strdup(opts[i]->id),
328 g_variant_ref_sink(gvar));
329 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
330 gvar = g_variant_new_string(s);
331 g_hash_table_insert(hash, g_strdup(opts[i]->id),
332 g_variant_ref_sink(gvar));
333 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
334 b = TRUE;
335 if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
336 b = FALSE;
337 } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
338 g_critical("Unable to convert '%s' to boolean!", s);
341 gvar = g_variant_new_boolean(b);
342 g_hash_table_insert(hash, g_strdup(opts[i]->id),
343 g_variant_ref_sink(gvar));
344 } else {
345 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
349 return hash;
352 static char *strcanon(const char *str)
354 int p0, p1;
355 char *s;
357 /* Returns newly allocated string. */
358 s = g_ascii_strdown(str, -1);
359 for (p0 = p1 = 0; str[p0]; p0++) {
360 if ((s[p0] >= 'a' && s[p0] <= 'z')
361 || (s[p0] >= '0' && s[p0] <= '9'))
362 s[p1++] = s[p0];
364 s[p1] = '\0';
366 return s;
369 int canon_cmp(const char *str1, const char *str2)
371 int ret;
372 char *s1, *s2;
374 s1 = strcanon(str1);
375 s2 = strcanon(str2);
376 ret = g_ascii_strcasecmp(s1, s2);
377 g_free(s2);
378 g_free(s1);
380 return ret;
383 /* Convert driver options hash to GSList of struct sr_config. */
384 static GSList *hash_to_hwopt(GHashTable *hash)
386 struct sr_config *src;
387 GList *gl, *keys;
388 GSList *opts;
389 char *key;
391 keys = g_hash_table_get_keys(hash);
392 opts = NULL;
393 for (gl = keys; gl; gl = gl->next) {
394 key = gl->data;
395 src = g_malloc(sizeof(struct sr_config));
396 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
397 return NULL;
398 opts = g_slist_append(opts, src);
400 g_list_free(keys);
402 return opts;
405 int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
407 struct sr_dev_driver **drivers;
408 GHashTable *drvargs;
409 int i;
410 char *drvname;
412 if (!arg)
413 return FALSE;
415 drvargs = parse_generic_arg(arg, TRUE);
417 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
418 g_hash_table_remove(drvargs, "sigrok_key");
419 *driver = NULL;
420 drivers = sr_driver_list(sr_ctx);
421 for (i = 0; drivers[i]; i++) {
422 if (strcmp(drivers[i]->name, drvname))
423 continue;
424 *driver = drivers[i];
426 if (!*driver) {
427 g_critical("Driver %s not found.", drvname);
428 g_hash_table_destroy(drvargs);
429 g_free(drvname);
430 return FALSE;
432 g_free(drvname);
433 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
434 g_critical("Failed to initialize driver.");
435 g_hash_table_destroy(drvargs);
436 return FALSE;
439 if (drvopts) {
440 *drvopts = NULL;
441 if (g_hash_table_size(drvargs) > 0) {
442 if (!(*drvopts = hash_to_hwopt(drvargs))) {
443 /* Unknown options, already logged. */
444 g_hash_table_destroy(drvargs);
445 return FALSE;
450 g_hash_table_destroy(drvargs);
452 return TRUE;