blob: 3df04e2a5da487803faf2459a0a3b8fd2711ab07 [file] [log] [blame]
Uwe Hermann43e57472011-12-30 11:23:22 +01001/*
Uwe Hermann630293b2013-04-23 22:10:41 +02002 * This file is part of the sigrok-cli project.
Uwe Hermann43e57472011-12-30 11:23:22 +01003 *
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
Uwe Hermann20fb52e2013-11-19 11:48:06 +010020#include "sigrok-cli.h"
Uwe Hermann43e57472011-12-30 11:23:22 +010021#include <stdio.h>
22#include <stdlib.h>
23#include <stdint.h>
24#include <string.h>
25#include <glib.h>
Uwe Hermann43e57472011-12-30 11:23:22 +010026
Uwe Hermann029d73f2014-03-24 22:32:47 +010027struct sr_channel *find_channel(GSList *channellist, const char *channelname)
Uwe Hermann43e57472011-12-30 11:23:22 +010028{
Uwe Hermann029d73f2014-03-24 22:32:47 +010029 struct sr_channel *ch;
Bert Vermeulen497f5362012-10-14 14:53:30 +020030 GSList *l;
Uwe Hermann43e57472011-12-30 11:23:22 +010031
Uwe Hermann029d73f2014-03-24 22:32:47 +010032 ch = NULL;
33 for (l = channellist; l; l = l->next) {
34 ch = l->data;
35 if (!strcmp(ch->name, channelname))
Bert Vermeulen497f5362012-10-14 14:53:30 +020036 break;
Uwe Hermannc2c4a0d2012-02-11 20:06:46 +010037 }
Uwe Hermann029d73f2014-03-24 22:32:47 +010038 ch = l ? l->data : NULL;
Uwe Hermann43e57472011-12-30 11:23:22 +010039
Uwe Hermann029d73f2014-03-24 22:32:47 +010040 return ch;
Bert Vermeulen497f5362012-10-14 14:53:30 +020041}
42
Uwe Hermann029d73f2014-03-24 22:32:47 +010043GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
Bert Vermeulen497f5362012-10-14 14:53:30 +020044{
Uwe Hermann029d73f2014-03-24 22:32:47 +010045 struct sr_channel *ch;
46 GSList *channellist;
Bert Vermeulen497f5362012-10-14 14:53:30 +020047 int ret, n, b, e, i;
48 char **tokens, **range, **names, *eptr, str[8];
49
Uwe Hermann029d73f2014-03-24 22:32:47 +010050 if (!channelstring || !channelstring[0])
51 /* Use all channels by default. */
52 return g_slist_copy(sdi->channels);
Bert Vermeulen497f5362012-10-14 14:53:30 +020053
54 ret = SR_OK;
55 range = NULL;
Alexandru Gagniuc66149c22012-12-19 01:31:58 -060056 names = NULL;
Uwe Hermann029d73f2014-03-24 22:32:47 +010057 channellist = NULL;
58 tokens = g_strsplit(channelstring, ",", 0);
Uwe Hermann43e57472011-12-30 11:23:22 +010059 for (i = 0; tokens[i]; i++) {
Bert Vermeulen497f5362012-10-14 14:53:30 +020060 if (tokens[i][0] == '\0') {
Uwe Hermann029d73f2014-03-24 22:32:47 +010061 g_critical("Invalid empty channel.");
Bert Vermeulen497f5362012-10-14 14:53:30 +020062 ret = SR_ERR;
63 break;
64 }
Uwe Hermann43e57472011-12-30 11:23:22 +010065 if (strchr(tokens[i], '-')) {
Uwe Hermann029d73f2014-03-24 22:32:47 +010066 /* A range of channels in the form a-b. This will only work
67 * if the channels are named as numbers -- so every channel
68 * in the range must exist as a channel name string in the
Bert Vermeulen497f5362012-10-14 14:53:30 +020069 * device. */
Uwe Hermann43e57472011-12-30 11:23:22 +010070 range = g_strsplit(tokens[i], "-", 2);
71 if (!range[0] || !range[1] || range[2]) {
72 /* Need exactly two arguments. */
Uwe Hermann029d73f2014-03-24 22:32:47 +010073 g_critical("Invalid channel syntax '%s'.", tokens[i]);
Bert Vermeulen497f5362012-10-14 14:53:30 +020074 ret = SR_ERR;
poljar (Damir Jelić)8ec20382013-11-14 20:01:18 +010075 goto range_fail;
Uwe Hermann43e57472011-12-30 11:23:22 +010076 }
77
Bert Vermeulen497f5362012-10-14 14:53:30 +020078 b = strtol(range[0], &eptr, 10);
79 if (eptr == range[0] || *eptr != '\0') {
Uwe Hermann029d73f2014-03-24 22:32:47 +010080 g_critical("Invalid channel '%s'.", range[0]);
Bert Vermeulen497f5362012-10-14 14:53:30 +020081 ret = SR_ERR;
poljar (Damir Jelić)8ec20382013-11-14 20:01:18 +010082 goto range_fail;
Bert Vermeulen497f5362012-10-14 14:53:30 +020083 }
Uwe Hermann43e57472011-12-30 11:23:22 +010084 e = strtol(range[1], NULL, 10);
Bert Vermeulen497f5362012-10-14 14:53:30 +020085 if (eptr == range[1] || *eptr != '\0') {
Uwe Hermann029d73f2014-03-24 22:32:47 +010086 g_critical("Invalid channel '%s'.", range[1]);
Bert Vermeulen497f5362012-10-14 14:53:30 +020087 ret = SR_ERR;
poljar (Damir Jelić)8ec20382013-11-14 20:01:18 +010088 goto range_fail;
Bert Vermeulen497f5362012-10-14 14:53:30 +020089 }
90 if (b < 0 || b >= e) {
Uwe Hermann029d73f2014-03-24 22:32:47 +010091 g_critical("Invalid channel range '%s'.", tokens[i]);
Bert Vermeulen497f5362012-10-14 14:53:30 +020092 ret = SR_ERR;
poljar (Damir Jelić)8ec20382013-11-14 20:01:18 +010093 goto range_fail;
Uwe Hermann43e57472011-12-30 11:23:22 +010094 }
95
96 while (b <= e) {
Bert Vermeulen497f5362012-10-14 14:53:30 +020097 n = snprintf(str, 8, "%d", b);
98 if (n < 0 || n > 8) {
Uwe Hermann029d73f2014-03-24 22:32:47 +010099 g_critical("Invalid channel '%d'.", b);
Bert Vermeulen497f5362012-10-14 14:53:30 +0200100 ret = SR_ERR;
101 break;
102 }
Uwe Hermann029d73f2014-03-24 22:32:47 +0100103 ch = find_channel(sdi->channels, str);
104 if (!ch) {
105 g_critical("unknown channel '%d'.", b);
Bert Vermeulen497f5362012-10-14 14:53:30 +0200106 ret = SR_ERR;
107 break;
108 }
Uwe Hermann029d73f2014-03-24 22:32:47 +0100109 channellist = g_slist_append(channellist, ch);
Uwe Hermann43e57472011-12-30 11:23:22 +0100110 b++;
111 }
poljar (Damir Jelić)8ec20382013-11-14 20:01:18 +0100112range_fail:
113 if (range)
114 g_strfreev(range);
115
Bert Vermeulen497f5362012-10-14 14:53:30 +0200116 if (ret != SR_OK)
117 break;
Uwe Hermann43e57472011-12-30 11:23:22 +0100118 } else {
Bert Vermeulen497f5362012-10-14 14:53:30 +0200119 names = g_strsplit(tokens[i], "=", 2);
120 if (!names[0] || (names[1] && names[2])) {
121 /* Need one or two arguments. */
Uwe Hermann029d73f2014-03-24 22:32:47 +0100122 g_critical("Invalid channel '%s'.", tokens[i]);
Daniel Elstner6df458b2014-02-21 00:50:22 +0100123 g_strfreev(names);
Bert Vermeulen497f5362012-10-14 14:53:30 +0200124 ret = SR_ERR;
Uwe Hermann43e57472011-12-30 11:23:22 +0100125 break;
126 }
127
Uwe Hermann029d73f2014-03-24 22:32:47 +0100128 ch = find_channel(sdi->channels, names[0]);
129 if (!ch) {
130 g_critical("unknown channel '%s'.", names[0]);
Daniel Elstner6df458b2014-02-21 00:50:22 +0100131 g_strfreev(names);
Bert Vermeulen497f5362012-10-14 14:53:30 +0200132 ret = SR_ERR;
133 break;
Uwe Hermann43e57472011-12-30 11:23:22 +0100134 }
Bert Vermeulen497f5362012-10-14 14:53:30 +0200135 if (names[1]) {
Uwe Hermann029d73f2014-03-24 22:32:47 +0100136 /* Rename channel. */
137 g_free(ch->name);
138 ch->name = g_strdup(names[1]);
Bert Vermeulen497f5362012-10-14 14:53:30 +0200139 }
Uwe Hermann029d73f2014-03-24 22:32:47 +0100140 channellist = g_slist_append(channellist, ch);
poljar (Damir Jelić)8ec20382013-11-14 20:01:18 +0100141
Daniel Elstner6df458b2014-02-21 00:50:22 +0100142 g_strfreev(names);
Uwe Hermann43e57472011-12-30 11:23:22 +0100143 }
144 }
Alexandru Gagniuc66149c22012-12-19 01:31:58 -0600145
Bert Vermeulen497f5362012-10-14 14:53:30 +0200146 if (ret != SR_OK) {
Uwe Hermann029d73f2014-03-24 22:32:47 +0100147 g_slist_free(channellist);
148 channellist = NULL;
Uwe Hermann43e57472011-12-30 11:23:22 +0100149 }
150
151 g_strfreev(tokens);
Uwe Hermann43e57472011-12-30 11:23:22 +0100152
Uwe Hermann029d73f2014-03-24 22:32:47 +0100153 return channellist;
Uwe Hermann43e57472011-12-30 11:23:22 +0100154}
155
Bert Vermeulen63bb4542012-07-16 01:03:30 +0200156GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
Uwe Hermann43e57472011-12-30 11:23:22 +0100157{
158 GHashTable *hash;
159 int i;
160 char **elements, *e;
161
162 if (!arg || !arg[0])
163 return NULL;
164
Bert Vermeulen63bb4542012-07-16 01:03:30 +0200165 i = 0;
166 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
167 g_free, g_free);
Uwe Hermann43e57472011-12-30 11:23:22 +0100168 elements = g_strsplit(arg, ":", 0);
Bert Vermeulen63bb4542012-07-16 01:03:30 +0200169 if (sep_first)
170 g_hash_table_insert(hash, g_strdup("sigrok_key"),
171 g_strdup(elements[i++]));
172 for (; elements[i]; i++) {
Uwe Hermann43e57472011-12-30 11:23:22 +0100173 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
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100186static char *strcanon(const char *str)
Bert Vermeulen432de702012-05-29 12:12:51 +0200187{
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
Uwe Hermannd2ee5ee2012-05-30 22:15:29 +0200203int canon_cmp(const char *str1, const char *str2)
Bert Vermeulen432de702012-05-29 12:12:51 +0200204{
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}