]> sigrok.org Git - sigrok-cli.git/blame - parsers.c
options: Make sure there are no extra arguments.
[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
20fb52e0 20#include "sigrok-cli.h"
43e5747a
UH
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdint.h>
24#include <string.h>
25#include <glib.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
UH
45 struct sr_channel *ch;
46 GSList *channellist;
497f5362
BV
47 int ret, n, b, e, i;
48 char **tokens, **range, **names, *eptr, str[8];
43e5747a 49
029d73fe
UH
50 if (!channelstring || !channelstring[0])
51 /* Use all channels by default. */
52 return g_slist_copy(sdi->channels);
497f5362
BV
53
54 ret = SR_OK;
55 range = NULL;
66149c20 56 names = NULL;
029d73fe
UH
57 channellist = NULL;
58 tokens = g_strsplit(channelstring, ",", 0);
43e5747a 59 for (i = 0; tokens[i]; i++) {
497f5362 60 if (tokens[i][0] == '\0') {
029d73fe 61 g_critical("Invalid empty channel.");
497f5362
BV
62 ret = SR_ERR;
63 break;
64 }
43e5747a 65 if (strchr(tokens[i], '-')) {
029d73fe
UH
66 /* A range of channels in the form a-b. This will only work
67 * if the channels are named as numbers -- so every channel
68 * in the range must exist as a channel name string in the
497f5362 69 * device. */
43e5747a
UH
70 range = g_strsplit(tokens[i], "-", 2);
71 if (!range[0] || !range[1] || range[2]) {
72 /* Need exactly two arguments. */
029d73fe 73 g_critical("Invalid channel syntax '%s'.", tokens[i]);
497f5362 74 ret = SR_ERR;
8ec20386 75 goto range_fail;
43e5747a
UH
76 }
77
497f5362
BV
78 b = strtol(range[0], &eptr, 10);
79 if (eptr == range[0] || *eptr != '\0') {
029d73fe 80 g_critical("Invalid channel '%s'.", range[0]);
497f5362 81 ret = SR_ERR;
8ec20386 82 goto range_fail;
497f5362 83 }
43e5747a 84 e = strtol(range[1], NULL, 10);
497f5362 85 if (eptr == range[1] || *eptr != '\0') {
029d73fe 86 g_critical("Invalid channel '%s'.", range[1]);
497f5362 87 ret = SR_ERR;
8ec20386 88 goto range_fail;
497f5362
BV
89 }
90 if (b < 0 || b >= e) {
029d73fe 91 g_critical("Invalid channel range '%s'.", tokens[i]);
497f5362 92 ret = SR_ERR;
8ec20386 93 goto range_fail;
43e5747a
UH
94 }
95
96 while (b <= e) {
497f5362
BV
97 n = snprintf(str, 8, "%d", b);
98 if (n < 0 || n > 8) {
029d73fe 99 g_critical("Invalid channel '%d'.", b);
497f5362
BV
100 ret = SR_ERR;
101 break;
102 }
029d73fe
UH
103 ch = find_channel(sdi->channels, str);
104 if (!ch) {
105 g_critical("unknown channel '%d'.", b);
497f5362
BV
106 ret = SR_ERR;
107 break;
108 }
029d73fe 109 channellist = g_slist_append(channellist, ch);
43e5747a
UH
110 b++;
111 }
8ec20386
DJ
112range_fail:
113 if (range)
114 g_strfreev(range);
115
497f5362
BV
116 if (ret != SR_OK)
117 break;
43e5747a 118 } else {
497f5362
BV
119 names = g_strsplit(tokens[i], "=", 2);
120 if (!names[0] || (names[1] && names[2])) {
121 /* Need one or two arguments. */
029d73fe 122 g_critical("Invalid channel '%s'.", tokens[i]);
6df458b7 123 g_strfreev(names);
497f5362 124 ret = SR_ERR;
43e5747a
UH
125 break;
126 }
127
029d73fe
UH
128 ch = find_channel(sdi->channels, names[0]);
129 if (!ch) {
130 g_critical("unknown channel '%s'.", names[0]);
6df458b7 131 g_strfreev(names);
497f5362
BV
132 ret = SR_ERR;
133 break;
134 }
135 if (names[1]) {
029d73fe
UH
136 /* Rename channel. */
137 g_free(ch->name);
138 ch->name = g_strdup(names[1]);
43e5747a 139 }
029d73fe 140 channellist = g_slist_append(channellist, ch);
8ec20386 141
6df458b7 142 g_strfreev(names);
43e5747a
UH
143 }
144 }
66149c20 145
497f5362 146 if (ret != SR_OK) {
029d73fe
UH
147 g_slist_free(channellist);
148 channellist = NULL;
43e5747a
UH
149 }
150
151 g_strfreev(tokens);
43e5747a 152
029d73fe 153 return channellist;
43e5747a
UH
154}
155
6b27bde4
BV
156int parse_trigger_match(char c)
157{
158 int match;
159
160 if (c == '0')
161 match = SR_TRIGGER_ZERO;
162 else if (c == '1')
163 match = SR_TRIGGER_ONE;
164 else if (c == 'r')
165 match = SR_TRIGGER_RISING;
166 else if (c == 'f')
167 match = SR_TRIGGER_FALLING;
168 else if (c == 'e')
169 match = SR_TRIGGER_EDGE;
170 else if (c == 'o')
171 match = SR_TRIGGER_OVER;
172 else if (c == 'u')
173 match = SR_TRIGGER_UNDER;
174 else
175 match = 0;
176
177 return match;
178}
179
180int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s)
181{
182 struct sr_channel *ch;
183 struct sr_trigger *trigger;
184 struct sr_trigger_stage *stage;
185 GVariant *gvar;
186 GSList *l;
187 gsize num_matches;
188 gboolean found_match, error;
189 const int32_t *matches;
190 int32_t match;
191 unsigned int j;
192 int t, i;
193 char **tokens, *sep;
194
195 if (sr_config_list(sdi->driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
196 &gvar) != SR_OK) {
197 g_critical("Device doesn't support any triggers.");
198 return FALSE;
199 }
200 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
201
202 trigger = sr_trigger_new(NULL);
203 error = FALSE;
204 tokens = g_strsplit(s, ",", -1);
205 for (i = 0; tokens[i]; i++) {
206 if (!(sep = strchr(tokens[i], '='))) {
207 g_critical("Invalid trigger '%s'.", tokens[i]);
208 error = TRUE;
209 break;
210 }
211 *sep++ = 0;
212 ch = NULL;
213 for (l = sdi->channels; l; l = l->next) {
214 ch = l->data;
215 if (ch->enabled && !strcmp(ch->name, tokens[i]))
216 break;
217 ch = NULL;
218 }
219 if (!ch) {
220 g_critical("Invalid channel '%s'.", tokens[i]);
221 error = TRUE;
222 break;
223 }
224 for (t = 0; sep[t]; t++) {
225 if (!(match = parse_trigger_match(sep[t]))) {
226 g_critical("Invalid trigger match '%c'.", sep[t]);
227 error = TRUE;
228 break;
229 }
230 found_match = FALSE;
231 for (j = 0; j < num_matches; j++) {
232 if (matches[j] == match) {
233 found_match = TRUE;
234 break;
235 }
236 }
237 if (!found_match) {
238 g_critical("Trigger match '%c' not supported by device.", sep[t]);
239 error = TRUE;
240 break;
241 }
242 /* Make sure this ends up in the right stage, creating
243 * them as needed. */
244 while (!(stage = g_slist_nth_data(trigger->stages, t)))
f9170968 245 sr_trigger_stage_add(trigger);
6b27bde4
BV
246 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
247 error = TRUE;
248 break;
249 }
250 }
251 }
252 g_strfreev(tokens);
253 g_variant_unref(gvar);
254
255 if (error)
256 sr_trigger_free(trigger);
257 else
258 error = sr_session_trigger_set(trigger) != SR_OK;
259
260 return !error;
261}
262
63bb454c 263GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
43e5747a
UH
264{
265 GHashTable *hash;
266 int i;
267 char **elements, *e;
268
269 if (!arg || !arg[0])
270 return NULL;
271
63bb454c
BV
272 i = 0;
273 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
274 g_free, g_free);
43e5747a 275 elements = g_strsplit(arg, ":", 0);
63bb454c
BV
276 if (sep_first)
277 g_hash_table_insert(hash, g_strdup("sigrok_key"),
278 g_strdup(elements[i++]));
279 for (; elements[i]; i++) {
43e5747a
UH
280 e = strchr(elements[i], '=');
281 if (!e)
282 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
283 else {
284 *e++ = '\0';
285 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
286 }
287 }
288 g_strfreev(elements);
289
290 return hash;
291}
292
2be182e6 293static char *strcanon(const char *str)
432de709
BV
294{
295 int p0, p1;
296 char *s;
297
298 /* Returns newly allocated string. */
299 s = g_ascii_strdown(str, -1);
300 for (p0 = p1 = 0; str[p0]; p0++) {
301 if ((s[p0] >= 'a' && s[p0] <= 'z')
302 || (s[p0] >= '0' && s[p0] <= '9'))
303 s[p1++] = s[p0];
304 }
305 s[p1] = '\0';
306
307 return s;
308}
309
d2ee5eea 310int canon_cmp(const char *str1, const char *str2)
432de709
BV
311{
312 int ret;
313 char *s1, *s2;
314
315 s1 = strcanon(str1);
316 s2 = strcanon(str2);
317 ret = g_ascii_strcasecmp(s1, s2);
318 g_free(s2);
319 g_free(s1);
320
321 return ret;
322}