]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok-cli project. | |
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 "sigrok-cli.h" | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
25 | #include <glib.h> | |
26 | ||
27 | static struct sr_probe *find_probe(GSList *probelist, const char *probename) | |
28 | { | |
29 | struct sr_probe *probe; | |
30 | GSList *l; | |
31 | ||
32 | probe = NULL; | |
33 | for (l = probelist; l; l = l->next) { | |
34 | probe = l->data; | |
35 | if (!strcmp(probe->name, probename)) | |
36 | break; | |
37 | } | |
38 | probe = l ? l->data : NULL; | |
39 | ||
40 | return probe; | |
41 | } | |
42 | ||
43 | GSList *parse_probestring(struct sr_dev_inst *sdi, const char *probestring) | |
44 | { | |
45 | struct sr_probe *probe; | |
46 | GSList *probelist; | |
47 | int ret, n, b, e, i; | |
48 | char **tokens, **range, **names, *eptr, str[8]; | |
49 | ||
50 | if (!probestring || !probestring[0]) | |
51 | /* All probes are enabled by default by the driver. */ | |
52 | return NULL; | |
53 | ||
54 | ret = SR_OK; | |
55 | range = NULL; | |
56 | names = NULL; | |
57 | probelist = NULL; | |
58 | tokens = g_strsplit(probestring, ",", 0); | |
59 | for (i = 0; tokens[i]; i++) { | |
60 | if (tokens[i][0] == '\0') { | |
61 | g_critical("Invalid empty probe."); | |
62 | ret = SR_ERR; | |
63 | break; | |
64 | } | |
65 | if (strchr(tokens[i], '-')) { | |
66 | /* A range of probes in the form a-b. This will only work | |
67 | * if the probes are named as numbers -- so every probe | |
68 | * in the range must exist as a probe name string in the | |
69 | * device. */ | |
70 | range = g_strsplit(tokens[i], "-", 2); | |
71 | if (!range[0] || !range[1] || range[2]) { | |
72 | /* Need exactly two arguments. */ | |
73 | g_critical("Invalid probe syntax '%s'.", tokens[i]); | |
74 | ret = SR_ERR; | |
75 | goto range_fail; | |
76 | } | |
77 | ||
78 | b = strtol(range[0], &eptr, 10); | |
79 | if (eptr == range[0] || *eptr != '\0') { | |
80 | g_critical("Invalid probe '%s'.", range[0]); | |
81 | ret = SR_ERR; | |
82 | goto range_fail; | |
83 | } | |
84 | e = strtol(range[1], NULL, 10); | |
85 | if (eptr == range[1] || *eptr != '\0') { | |
86 | g_critical("Invalid probe '%s'.", range[1]); | |
87 | ret = SR_ERR; | |
88 | goto range_fail; | |
89 | } | |
90 | if (b < 0 || b >= e) { | |
91 | g_critical("Invalid probe range '%s'.", tokens[i]); | |
92 | ret = SR_ERR; | |
93 | goto range_fail; | |
94 | } | |
95 | ||
96 | while (b <= e) { | |
97 | n = snprintf(str, 8, "%d", b); | |
98 | if (n < 0 || n > 8) { | |
99 | g_critical("Invalid probe '%d'.", b); | |
100 | ret = SR_ERR; | |
101 | break; | |
102 | } | |
103 | probe = find_probe(sdi->probes, str); | |
104 | if (!probe) { | |
105 | g_critical("unknown probe '%d'.", b); | |
106 | ret = SR_ERR; | |
107 | break; | |
108 | } | |
109 | probelist = g_slist_append(probelist, probe); | |
110 | b++; | |
111 | } | |
112 | range_fail: | |
113 | if (range) | |
114 | g_strfreev(range); | |
115 | ||
116 | if (ret != SR_OK) | |
117 | break; | |
118 | } else { | |
119 | names = g_strsplit(tokens[i], "=", 2); | |
120 | if (!names[0] || (names[1] && names[2])) { | |
121 | /* Need one or two arguments. */ | |
122 | g_critical("Invalid probe '%s'.", tokens[i]); | |
123 | ret = SR_ERR; | |
124 | break; | |
125 | } | |
126 | ||
127 | probe = find_probe(sdi->probes, names[0]); | |
128 | if (!probe) { | |
129 | g_critical("unknown probe '%s'.", names[0]); | |
130 | ret = SR_ERR; | |
131 | break; | |
132 | } | |
133 | if (names[1]) { | |
134 | /* Rename probe. */ | |
135 | g_free(probe->name); | |
136 | probe->name = g_strdup(names[1]); | |
137 | } | |
138 | probelist = g_slist_append(probelist, probe); | |
139 | ||
140 | if (names) | |
141 | g_strfreev(names); | |
142 | } | |
143 | } | |
144 | ||
145 | if (ret != SR_OK) { | |
146 | g_slist_free(probelist); | |
147 | probelist = NULL; | |
148 | } | |
149 | ||
150 | g_strfreev(tokens); | |
151 | ||
152 | return probelist; | |
153 | } | |
154 | ||
155 | GHashTable *parse_generic_arg(const char *arg, gboolean sep_first) | |
156 | { | |
157 | GHashTable *hash; | |
158 | int i; | |
159 | char **elements, *e; | |
160 | ||
161 | if (!arg || !arg[0]) | |
162 | return NULL; | |
163 | ||
164 | i = 0; | |
165 | hash = g_hash_table_new_full(g_str_hash, g_str_equal, | |
166 | g_free, g_free); | |
167 | elements = g_strsplit(arg, ":", 0); | |
168 | if (sep_first) | |
169 | g_hash_table_insert(hash, g_strdup("sigrok_key"), | |
170 | g_strdup(elements[i++])); | |
171 | for (; elements[i]; i++) { | |
172 | e = strchr(elements[i], '='); | |
173 | if (!e) | |
174 | g_hash_table_insert(hash, g_strdup(elements[i]), NULL); | |
175 | else { | |
176 | *e++ = '\0'; | |
177 | g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e)); | |
178 | } | |
179 | } | |
180 | g_strfreev(elements); | |
181 | ||
182 | return hash; | |
183 | } | |
184 | ||
185 | static char *strcanon(const char *str) | |
186 | { | |
187 | int p0, p1; | |
188 | char *s; | |
189 | ||
190 | /* Returns newly allocated string. */ | |
191 | s = g_ascii_strdown(str, -1); | |
192 | for (p0 = p1 = 0; str[p0]; p0++) { | |
193 | if ((s[p0] >= 'a' && s[p0] <= 'z') | |
194 | || (s[p0] >= '0' && s[p0] <= '9')) | |
195 | s[p1++] = s[p0]; | |
196 | } | |
197 | s[p1] = '\0'; | |
198 | ||
199 | return s; | |
200 | } | |
201 | ||
202 | int canon_cmp(const char *str1, const char *str2) | |
203 | { | |
204 | int ret; | |
205 | char *s1, *s2; | |
206 | ||
207 | s1 = strcanon(str1); | |
208 | s2 = strcanon(str2); | |
209 | ret = g_ascii_strcasecmp(s1, s2); | |
210 | g_free(s2); | |
211 | g_free(s1); | |
212 | ||
213 | return ret; | |
214 | } |