]> sigrok.org Git - sigrok-cli.git/blame - parsers.c
cli: add --show option, and switch to new info_get driver API
[sigrok-cli.git] / parsers.c
CommitLineData
43e5747a
UH
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>
60ea8937 25#include <libsigrok/libsigrok.h>
43e5747a
UH
26#include "sigrok-cli.h"
27
28char **parse_probestring(int max_probes, const char *probestring)
29{
30 int tmp, b, e, i;
31 char **tokens, **range, **probelist, *name, str[8];
32 gboolean error;
33
34 error = FALSE;
35 range = NULL;
c2c4a0de
UH
36 if (!(probelist = g_try_malloc0(max_probes * sizeof(char *)))) {
37 /* TODO: Handle errors. */
38 }
43e5747a
UH
39 tokens = g_strsplit(probestring, ",", max_probes);
40
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. */
8170b8ea 47 g_critical("Invalid probe syntax '%s'.", tokens[i]);
43e5747a
UH
48 error = TRUE;
49 break;
50 }
51
52 b = strtol(range[0], NULL, 10);
53 e = strtol(range[1], NULL, 10);
54 if (b < 1 || e > max_probes || b >= e) {
8170b8ea 55 g_critical("Invalid probe range '%s'.", tokens[i]);
43e5747a
UH
56 error = TRUE;
57 break;
58 }
59
60 while (b <= e) {
61 snprintf(str, 7, "%d", b);
62 probelist[b - 1] = g_strdup(str);
63 b++;
64 }
65 } else {
66 tmp = strtol(tokens[i], NULL, 10);
67 if (tmp < 1 || tmp > max_probes) {
8170b8ea 68 g_critical("Invalid probe %d.", tmp);
43e5747a
UH
69 error = TRUE;
70 break;
71 }
72
73 if ((name = strchr(tokens[i], '='))) {
74 probelist[tmp - 1] = g_strdup(++name);
75 if (strlen(probelist[tmp - 1]) > SR_MAX_PROBENAME_LEN)
76 probelist[tmp - 1][SR_MAX_PROBENAME_LEN] = 0;
77 } else {
78 snprintf(str, 7, "%d", tmp);
79 probelist[tmp - 1] = g_strdup(str);
80 }
81 }
82 }
83
84 if (error) {
85 for (i = 0; i < max_probes; i++)
86 if (probelist[i])
87 g_free(probelist[i]);
88 g_free(probelist);
89 probelist = NULL;
90 }
91
92 g_strfreev(tokens);
93 if (range)
94 g_strfreev(range);
95
96 return probelist;
97}
98
63bb454c 99GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
43e5747a
UH
100{
101 GHashTable *hash;
102 int i;
103 char **elements, *e;
104
105 if (!arg || !arg[0])
106 return NULL;
107
63bb454c
BV
108 i = 0;
109 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
110 g_free, g_free);
43e5747a 111 elements = g_strsplit(arg, ":", 0);
63bb454c
BV
112 if (sep_first)
113 g_hash_table_insert(hash, g_strdup("sigrok_key"),
114 g_strdup(elements[i++]));
115 for (; elements[i]; i++) {
43e5747a
UH
116 e = strchr(elements[i], '=');
117 if (!e)
118 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
119 else {
120 *e++ = '\0';
121 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
122 }
123 }
124 g_strfreev(elements);
125
126 return hash;
127}
128
1e0f9ed9 129struct sr_dev *parse_devstring(const char *devstring)
43e5747a 130{
1e0f9ed9 131 struct sr_dev *dev, *d;
a210c4cc 132 struct sr_dev_driver **drivers;
bc8e2400
UH
133 GSList *devs, *l;
134 int i, num_devs, dev_num, dev_cnt;
43e5747a
UH
135 char *tmp;
136
1e0f9ed9 137 if (!devstring)
43e5747a
UH
138 return NULL;
139
1e0f9ed9
UH
140 dev = NULL;
141 dev_num = strtol(devstring, &tmp, 10);
142 if (tmp != devstring) {
a210c4cc
UH
143 /* argument is numeric, meaning a device ID. Make all drivers
144 * scan for devices.
43e5747a 145 */
1e0f9ed9
UH
146 num_devs = num_real_devs();
147 if (dev_num < 0 || dev_num >= num_devs)
43e5747a
UH
148 return NULL;
149
1e0f9ed9
UH
150 dev_cnt = 0;
151 devs = sr_dev_list();
152 for (l = devs; l; l = l->next) {
43e5747a 153 d = l->data;
1e0f9ed9 154 if (sr_dev_has_hwcap(d, SR_HWCAP_DEMO_DEV))
43e5747a 155 continue;
1e0f9ed9
UH
156 if (dev_cnt == dev_num) {
157 if (dev_num == dev_cnt) {
158 dev = d;
43e5747a
UH
159 break;
160 }
161 }
1e0f9ed9 162 dev_cnt++;
43e5747a
UH
163 }
164 } else {
165 /* select device by driver -- only initialize that driver,
166 * no need to let them all scan
167 */
1e0f9ed9 168 dev = NULL;
a214a2eb 169 drivers = sr_driver_list();
a210c4cc
UH
170 for (i = 0; drivers[i]; i++) {
171 if (strcmp(drivers[i]->name, devstring))
43e5747a 172 continue;
a214a2eb 173 num_devs = sr_driver_init(drivers[i]);
1e0f9ed9
UH
174 if (num_devs == 1) {
175 devs = sr_dev_list();
176 dev = devs->data;
177 } else if (num_devs > 1) {
43e5747a 178 printf("driver '%s' found %d devices, select by ID instead.\n",
1e0f9ed9 179 devstring, num_devs);
43e5747a
UH
180 }
181 /* fall through: selected driver found no devices */
182 break;
183 }
184 }
185
1e0f9ed9 186 return dev;
43e5747a 187}
432de709 188
d2ee5eea 189char *strcanon(const char *str)
432de709
BV
190{
191 int p0, p1;
192 char *s;
193
194 /* Returns newly allocated string. */
195 s = g_ascii_strdown(str, -1);
196 for (p0 = p1 = 0; str[p0]; p0++) {
197 if ((s[p0] >= 'a' && s[p0] <= 'z')
198 || (s[p0] >= '0' && s[p0] <= '9'))
199 s[p1++] = s[p0];
200 }
201 s[p1] = '\0';
202
203 return s;
204}
205
d2ee5eea 206int canon_cmp(const char *str1, const char *str2)
432de709
BV
207{
208 int ret;
209 char *s1, *s2;
210
211 s1 = strcanon(str1);
212 s2 = strcanon(str2);
213 ret = g_ascii_strcasecmp(s1, s2);
214 g_free(s2);
215 g_free(s1);
216
217 return ret;
218}