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 char **parse_probestring(int max_probes, const char *probestring)
31 char **tokens, **range, **probelist, *name, str[8];
36 if (!(probelist = g_try_malloc0(max_probes * sizeof(char *)))) {
37 /* TODO: Handle errors. */
39 tokens = g_strsplit(probestring, ",", max_probes);
41 for (i = 0; tokens[i]; i++) {
42 if (strchr(tokens[i], '-')) {
43 /* A range of probes in the form 1-5. */
44 range = g_strsplit(tokens[i], "-", 2);
45 if (!range[0] || !range[1] || range[2]) {
46 /* Need exactly two arguments. */
47 g_critical("Invalid probe syntax '%s'.", tokens[i]);
52 b = strtol(range[0], NULL, 10);
53 e = strtol(range[1], NULL, 10);
54 if (b < 0 || e >= max_probes || b >= e) {
55 g_critical("Invalid probe range '%s'.", tokens[i]);
61 snprintf(str, 7, "%d", b);
62 probelist[b] = g_strdup(str);
66 tmp = strtol(tokens[i], NULL, 10);
67 if (tmp < 0 || tmp >= max_probes) {
68 g_critical("Invalid probe %d.", tmp);
73 if ((name = strchr(tokens[i], '='))) {
74 probelist[tmp] = g_strdup(++name);
75 if (strlen(probelist[tmp]) > SR_MAX_PROBENAME_LEN)
76 probelist[tmp][SR_MAX_PROBENAME_LEN] = 0;
78 snprintf(str, 7, "%d", tmp);
79 probelist[tmp] = g_strdup(str);
85 for (i = 0; i < max_probes; i++)
99 GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
109 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
111 elements = g_strsplit(arg, ":", 0);
113 g_hash_table_insert(hash, g_strdup("sigrok_key"),
114 g_strdup(elements[i++]));
115 for (; elements[i]; i++) {
116 e = strchr(elements[i], '=');
118 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
121 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
124 g_strfreev(elements);
129 char *strcanon(const char *str)
134 /* Returns newly allocated string. */
135 s = g_ascii_strdown(str, -1);
136 for (p0 = p1 = 0; str[p0]; p0++) {
137 if ((s[p0] >= 'a' && s[p0] <= 'z')
138 || (s[p0] >= '0' && s[p0] <= '9'))
146 int canon_cmp(const char *str1, const char *str2)
153 ret = g_ascii_strcasecmp(s1, s2);