]> sigrok.org Git - sigrok-cli.git/blame - parsers.c
sigrok-cli: Fix memory leak if we save the default output format to a file.
[sigrok-cli.git] / parsers.c
CommitLineData
43e5747a 1/*
630293b4 2 * This file is part of the sigrok-cli project.
43e5747a
UH
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
2be182e6 28static struct sr_probe *find_probe(GSList *probelist, const char *probename)
43e5747a 29{
497f5362
BV
30 struct sr_probe *probe;
31 GSList *l;
43e5747a 32
497f5362
BV
33 probe = NULL;
34 for (l = probelist; l; l = l->next) {
35 probe = l->data;
36 if (!strcmp(probe->name, probename))
37 break;
c2c4a0de 38 }
497f5362
BV
39 probe = l ? l->data : NULL;
40
41 return probe;
42}
43
44GSList *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];
43e5747a 50
497f5362
BV
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;
66149c20 57 names = NULL;
497f5362
BV
58 probelist = NULL;
59 tokens = g_strsplit(probestring, ",", 0);
43e5747a 60 for (i = 0; tokens[i]; i++) {
497f5362
BV
61 if (tokens[i][0] == '\0') {
62 g_critical("Invalid empty probe.");
63 ret = SR_ERR;
64 break;
65 }
43e5747a 66 if (strchr(tokens[i], '-')) {
497f5362
BV
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. */
43e5747a
UH
71 range = g_strsplit(tokens[i], "-", 2);
72 if (!range[0] || !range[1] || range[2]) {
73 /* Need exactly two arguments. */
8170b8ea 74 g_critical("Invalid probe syntax '%s'.", tokens[i]);
497f5362 75 ret = SR_ERR;
8ec20386 76 goto range_fail;
43e5747a
UH
77 }
78
497f5362
BV
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;
8ec20386 83 goto range_fail;
497f5362 84 }
43e5747a 85 e = strtol(range[1], NULL, 10);
497f5362
BV
86 if (eptr == range[1] || *eptr != '\0') {
87 g_critical("Invalid probe '%s'.", range[1]);
88 ret = SR_ERR;
8ec20386 89 goto range_fail;
497f5362
BV
90 }
91 if (b < 0 || b >= e) {
8170b8ea 92 g_critical("Invalid probe range '%s'.", tokens[i]);
497f5362 93 ret = SR_ERR;
8ec20386 94 goto range_fail;
43e5747a
UH
95 }
96
97 while (b <= e) {
497f5362
BV
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);
43e5747a
UH
111 b++;
112 }
8ec20386
DJ
113range_fail:
114 if (range)
115 g_strfreev(range);
116
497f5362
BV
117 if (ret != SR_OK)
118 break;
43e5747a 119 } else {
497f5362
BV
120 names = g_strsplit(tokens[i], "=", 2);
121 if (!names[0] || (names[1] && names[2])) {
122 /* Need one or two arguments. */
123 g_critical("Invalid probe '%s'.", tokens[i]);
124 ret = SR_ERR;
43e5747a
UH
125 break;
126 }
127
497f5362
BV
128 probe = find_probe(sdi->probes, names[0]);
129 if (!probe) {
130 g_critical("unknown probe '%s'.", names[0]);
131 ret = SR_ERR;
132 break;
133 }
134 if (names[1]) {
135 /* Rename probe. */
136 g_free(probe->name);
137 probe->name = g_strdup(names[1]);
43e5747a 138 }
497f5362 139 probelist = g_slist_append(probelist, probe);
8ec20386
DJ
140
141 if (names)
142 g_strfreev(names);
43e5747a
UH
143 }
144 }
66149c20 145
497f5362
BV
146 if (ret != SR_OK) {
147 g_slist_free(probelist);
43e5747a
UH
148 probelist = NULL;
149 }
150
151 g_strfreev(tokens);
43e5747a
UH
152
153 return probelist;
154}
155
63bb454c 156GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
43e5747a
UH
157{
158 GHashTable *hash;
159 int i;
160 char **elements, *e;
161
162 if (!arg || !arg[0])
163 return NULL;
164
63bb454c
BV
165 i = 0;
166 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
167 g_free, g_free);
43e5747a 168 elements = g_strsplit(arg, ":", 0);
63bb454c
BV
169 if (sep_first)
170 g_hash_table_insert(hash, g_strdup("sigrok_key"),
171 g_strdup(elements[i++]));
172 for (; elements[i]; i++) {
43e5747a
UH
173 e = strchr(elements[i], '=');
174 if (!e)
175 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
176 else {
177 *e++ = '\0';
178 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
179 }
180 }
181 g_strfreev(elements);
182
183 return hash;
184}
185
2be182e6 186static char *strcanon(const char *str)
432de709
BV
187{
188 int p0, p1;
189 char *s;
190
191 /* Returns newly allocated string. */
192 s = g_ascii_strdown(str, -1);
193 for (p0 = p1 = 0; str[p0]; p0++) {
194 if ((s[p0] >= 'a' && s[p0] <= 'z')
195 || (s[p0] >= '0' && s[p0] <= '9'))
196 s[p1++] = s[p0];
197 }
198 s[p1] = '\0';
199
200 return s;
201}
202
d2ee5eea 203int canon_cmp(const char *str1, const char *str2)
432de709
BV
204{
205 int ret;
206 char *s1, *s2;
207
208 s1 = strcanon(str1);
209 s2 = strcanon(str2);
210 ret = g_ascii_strcasecmp(s1, s2);
211 g_free(s2);
212 g_free(s1);
213
214 return ret;
215}