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