2 * This file is part of the sigrok 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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "sigrok-cli.h"
28 struct sr_probe
*find_probe(GSList
*probelist
, const char *probename
)
30 struct sr_probe
*probe
;
34 for (l
= probelist
; l
; l
= l
->next
) {
36 if (!strcmp(probe
->name
, probename
))
39 probe
= l
? l
->data
: NULL
;
44 GSList
*parse_probestring(struct sr_dev_inst
*sdi
, const char *probestring
)
46 struct sr_probe
*probe
;
49 char **tokens
, **range
, **names
, *eptr
, str
[8];
51 if (!probestring
|| !probestring
[0])
52 /* All probes are enabled by default by the driver. */
59 tokens
= g_strsplit(probestring
, ",", 0);
60 for (i
= 0; tokens
[i
]; i
++) {
61 if (tokens
[i
][0] == '\0') {
62 g_critical("Invalid empty probe.");
66 if (strchr(tokens
[i
], '-')) {
67 /* A range of probes in the form a-b. This will only work
68 * if the probes are named as numbers -- so every probe
69 * in the range must exist as a probe name string in the
71 range
= g_strsplit(tokens
[i
], "-", 2);
72 if (!range
[0] || !range
[1] || range
[2]) {
73 /* Need exactly two arguments. */
74 g_critical("Invalid probe syntax '%s'.", tokens
[i
]);
79 b
= strtol(range
[0], &eptr
, 10);
80 if (eptr
== range
[0] || *eptr
!= '\0') {
81 g_critical("Invalid probe '%s'.", range
[0]);
85 e
= strtol(range
[1], NULL
, 10);
86 if (eptr
== range
[1] || *eptr
!= '\0') {
87 g_critical("Invalid probe '%s'.", range
[1]);
91 if (b
< 0 || b
>= e
) {
92 g_critical("Invalid probe range '%s'.", tokens
[i
]);
98 n
= snprintf(str
, 8, "%d", b
);
100 g_critical("Invalid probe '%d'.", b
);
104 probe
= find_probe(sdi
->probes
, str
);
106 g_critical("unknown probe '%d'.", b
);
110 probelist
= g_slist_append(probelist
, probe
);
116 names
= g_strsplit(tokens
[i
], "=", 2);
117 if (!names
[0] || (names
[1] && names
[2])) {
118 /* Need one or two arguments. */
119 g_critical("Invalid probe '%s'.", tokens
[i
]);
124 probe
= find_probe(sdi
->probes
, names
[0]);
126 g_critical("unknown probe '%s'.", names
[0]);
133 probe
->name
= g_strdup(names
[1]);
135 probelist
= g_slist_append(probelist
, probe
);
145 g_slist_free(probelist
);
154 GHashTable
*parse_generic_arg(const char *arg
, gboolean sep_first
)
164 hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
166 elements
= g_strsplit(arg
, ":", 0);
168 g_hash_table_insert(hash
, g_strdup("sigrok_key"),
169 g_strdup(elements
[i
++]));
170 for (; elements
[i
]; i
++) {
171 e
= strchr(elements
[i
], '=');
173 g_hash_table_insert(hash
, g_strdup(elements
[i
]), NULL
);
176 g_hash_table_insert(hash
, g_strdup(elements
[i
]), g_strdup(e
));
179 g_strfreev(elements
);
184 char *strcanon(const char *str
)
189 /* Returns newly allocated string. */
190 s
= g_ascii_strdown(str
, -1);
191 for (p0
= p1
= 0; str
[p0
]; p0
++) {
192 if ((s
[p0
] >= 'a' && s
[p0
] <= 'z')
193 || (s
[p0
] >= '0' && s
[p0
] <= '9'))
201 int canon_cmp(const char *str1
, const char *str2
)
208 ret
= g_ascii_strcasecmp(s1
, s2
);