1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
* This file is part of the sigrok project.
*
* Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <glib.h>
#include <sigrok.h>
#include "sigrok-cli.h"
char **parse_probestring(int max_probes, const char *probestring)
{
int tmp, b, e, i;
char **tokens, **range, **probelist, *name, str[8];
gboolean error;
error = FALSE;
range = NULL;
if (!(probelist = g_try_malloc0(max_probes * sizeof(char *)))) {
/* TODO: Handle errors. */
}
tokens = g_strsplit(probestring, ",", max_probes);
for (i = 0; tokens[i]; i++) {
if (strchr(tokens[i], '-')) {
/* A range of probes in the form 1-5. */
range = g_strsplit(tokens[i], "-", 2);
if (!range[0] || !range[1] || range[2]) {
/* Need exactly two arguments. */
printf("Invalid probe syntax '%s'.\n",
tokens[i]);
error = TRUE;
break;
}
b = strtol(range[0], NULL, 10);
e = strtol(range[1], NULL, 10);
if (b < 1 || e > max_probes || b >= e) {
printf("Invalid probe range '%s'.\n",
tokens[i]);
error = TRUE;
break;
}
while (b <= e) {
snprintf(str, 7, "%d", b);
probelist[b - 1] = g_strdup(str);
b++;
}
} else {
tmp = strtol(tokens[i], NULL, 10);
if (tmp < 1 || tmp > max_probes) {
printf("Invalid probe %d.\n", tmp);
error = TRUE;
break;
}
if ((name = strchr(tokens[i], '='))) {
probelist[tmp - 1] = g_strdup(++name);
if (strlen(probelist[tmp - 1]) > SR_MAX_PROBENAME_LEN)
probelist[tmp - 1][SR_MAX_PROBENAME_LEN] = 0;
} else {
snprintf(str, 7, "%d", tmp);
probelist[tmp - 1] = g_strdup(str);
}
}
}
if (error) {
for (i = 0; i < max_probes; i++)
if (probelist[i])
g_free(probelist[i]);
g_free(probelist);
probelist = NULL;
}
g_strfreev(tokens);
if (range)
g_strfreev(range);
return probelist;
}
GHashTable *parse_generic_arg(const char *arg)
{
GHashTable *hash;
int i;
char **elements, *e;
if (!arg || !arg[0])
return NULL;
hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
elements = g_strsplit(arg, ":", 0);
g_hash_table_insert(hash, g_strdup("sigrok_key"), g_strdup(elements[0]));
for (i = 1; elements[i]; i++) {
e = strchr(elements[i], '=');
if (!e)
g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
else {
*e++ = '\0';
g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
}
}
g_strfreev(elements);
return hash;
}
struct sr_dev *parse_devstring(const char *devstring)
{
struct sr_dev *dev, *d;
struct sr_dev_driver **drivers;
GSList *devs, *l;
int i, num_devs, dev_num, dev_cnt;
char *tmp;
if (!devstring)
return NULL;
dev = NULL;
dev_num = strtol(devstring, &tmp, 10);
if (tmp != devstring) {
/* argument is numeric, meaning a device ID. Make all drivers
* scan for devices.
*/
num_devs = num_real_devs();
if (dev_num < 0 || dev_num >= num_devs)
return NULL;
dev_cnt = 0;
devs = sr_dev_list();
for (l = devs; l; l = l->next) {
d = l->data;
if (sr_dev_has_hwcap(d, SR_HWCAP_DEMO_DEV))
continue;
if (dev_cnt == dev_num) {
if (dev_num == dev_cnt) {
dev = d;
break;
}
}
dev_cnt++;
}
} else {
/* select device by driver -- only initialize that driver,
* no need to let them all scan
*/
dev = NULL;
drivers = sr_driver_list();
for (i = 0; drivers[i]; i++) {
if (strcmp(drivers[i]->name, devstring))
continue;
num_devs = sr_driver_init(drivers[i]);
if (num_devs == 1) {
devs = sr_dev_list();
dev = devs->data;
} else if (num_devs > 1) {
printf("driver '%s' found %d devices, select by ID instead.\n",
devstring, num_devs);
}
/* fall through: selected driver found no devices */
break;
}
}
return dev;
}
|