Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 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> |
Bert Vermeulen | 60ea893 | 2012-07-05 01:06:28 +0200 | [diff] [blame] | 25 | #include <libsigrok/libsigrok.h> |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 26 | #include "sigrok-cli.h" |
| 27 | |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 28 | struct sr_probe *find_probe(GSList *probelist, const char *probename) |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 29 | { |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 30 | struct sr_probe *probe; |
| 31 | GSList *l; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 32 | |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 33 | probe = NULL; |
| 34 | for (l = probelist; l; l = l->next) { |
| 35 | probe = l->data; |
| 36 | if (!strcmp(probe->name, probename)) |
| 37 | break; |
Uwe Hermann | c2c4a0d | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 38 | } |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 39 | probe = l ? l->data : NULL; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 40 | |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 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 | probelist = NULL; |
| 58 | tokens = g_strsplit(probestring, ",", 0); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 59 | for (i = 0; tokens[i]; i++) { |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 60 | if (tokens[i][0] == '\0') { |
| 61 | g_critical("Invalid empty probe."); |
| 62 | ret = SR_ERR; |
| 63 | break; |
| 64 | } |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 65 | if (strchr(tokens[i], '-')) { |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 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. */ |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 70 | range = g_strsplit(tokens[i], "-", 2); |
| 71 | if (!range[0] || !range[1] || range[2]) { |
| 72 | /* Need exactly two arguments. */ |
Bert Vermeulen | 8170b8e | 2012-05-26 04:54:34 +0200 | [diff] [blame] | 73 | g_critical("Invalid probe syntax '%s'.", tokens[i]); |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 74 | ret = SR_ERR; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 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 | break; |
| 83 | } |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 84 | e = strtol(range[1], NULL, 10); |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 85 | if (eptr == range[1] || *eptr != '\0') { |
| 86 | g_critical("Invalid probe '%s'.", range[1]); |
| 87 | ret = SR_ERR; |
| 88 | break; |
| 89 | } |
| 90 | if (b < 0 || b >= e) { |
Bert Vermeulen | 8170b8e | 2012-05-26 04:54:34 +0200 | [diff] [blame] | 91 | g_critical("Invalid probe range '%s'.", tokens[i]); |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 92 | ret = SR_ERR; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | |
| 96 | while (b <= e) { |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 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); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 110 | b++; |
| 111 | } |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 112 | if (ret != SR_OK) |
| 113 | break; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 114 | } else { |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 115 | names = g_strsplit(tokens[i], "=", 2); |
| 116 | if (!names[0] || (names[1] && names[2])) { |
| 117 | /* Need one or two arguments. */ |
| 118 | g_critical("Invalid probe '%s'.", tokens[i]); |
| 119 | ret = SR_ERR; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 123 | probe = find_probe(sdi->probes, names[0]); |
| 124 | if (!probe) { |
| 125 | g_critical("unknown probe '%s'.", names[0]); |
| 126 | ret = SR_ERR; |
| 127 | break; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 128 | } |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 129 | if (names[1]) { |
| 130 | /* Rename probe. */ |
| 131 | g_free(probe->name); |
| 132 | probe->name = g_strdup(names[1]); |
| 133 | } |
| 134 | probelist = g_slist_append(probelist, probe); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 137 | if (range) |
| 138 | g_strfreev(range); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 139 | |
Bert Vermeulen | 497f536 | 2012-10-14 14:53:30 +0200 | [diff] [blame] | 140 | if (ret != SR_OK) { |
| 141 | g_slist_free(probelist); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 142 | probelist = NULL; |
| 143 | } |
| 144 | |
| 145 | g_strfreev(tokens); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 146 | |
| 147 | return probelist; |
| 148 | } |
| 149 | |
Bert Vermeulen | 63bb454 | 2012-07-16 01:03:30 +0200 | [diff] [blame] | 150 | GHashTable *parse_generic_arg(const char *arg, gboolean sep_first) |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 151 | { |
| 152 | GHashTable *hash; |
| 153 | int i; |
| 154 | char **elements, *e; |
| 155 | |
| 156 | if (!arg || !arg[0]) |
| 157 | return NULL; |
| 158 | |
Bert Vermeulen | 63bb454 | 2012-07-16 01:03:30 +0200 | [diff] [blame] | 159 | i = 0; |
| 160 | hash = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 161 | g_free, g_free); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 162 | elements = g_strsplit(arg, ":", 0); |
Bert Vermeulen | 63bb454 | 2012-07-16 01:03:30 +0200 | [diff] [blame] | 163 | if (sep_first) |
| 164 | g_hash_table_insert(hash, g_strdup("sigrok_key"), |
| 165 | g_strdup(elements[i++])); |
| 166 | for (; elements[i]; i++) { |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 167 | e = strchr(elements[i], '='); |
| 168 | if (!e) |
| 169 | g_hash_table_insert(hash, g_strdup(elements[i]), NULL); |
| 170 | else { |
| 171 | *e++ = '\0'; |
| 172 | g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e)); |
| 173 | } |
| 174 | } |
| 175 | g_strfreev(elements); |
| 176 | |
| 177 | return hash; |
| 178 | } |
| 179 | |
Uwe Hermann | d2ee5ee | 2012-05-30 22:15:29 +0200 | [diff] [blame] | 180 | char *strcanon(const char *str) |
Bert Vermeulen | 432de70 | 2012-05-29 12:12:51 +0200 | [diff] [blame] | 181 | { |
| 182 | int p0, p1; |
| 183 | char *s; |
| 184 | |
| 185 | /* Returns newly allocated string. */ |
| 186 | s = g_ascii_strdown(str, -1); |
| 187 | for (p0 = p1 = 0; str[p0]; p0++) { |
| 188 | if ((s[p0] >= 'a' && s[p0] <= 'z') |
| 189 | || (s[p0] >= '0' && s[p0] <= '9')) |
| 190 | s[p1++] = s[p0]; |
| 191 | } |
| 192 | s[p1] = '\0'; |
| 193 | |
| 194 | return s; |
| 195 | } |
| 196 | |
Uwe Hermann | d2ee5ee | 2012-05-30 22:15:29 +0200 | [diff] [blame] | 197 | int canon_cmp(const char *str1, const char *str2) |
Bert Vermeulen | 432de70 | 2012-05-29 12:12:51 +0200 | [diff] [blame] | 198 | { |
| 199 | int ret; |
| 200 | char *s1, *s2; |
| 201 | |
| 202 | s1 = strcanon(str1); |
| 203 | s2 = strcanon(str2); |
| 204 | ret = g_ascii_strcasecmp(s1, s2); |
| 205 | g_free(s2); |
| 206 | g_free(s1); |
| 207 | |
| 208 | return ret; |
| 209 | } |