X-Git-Url: https://sigrok.org/gitweb/?p=sigrok-cli.git;a=blobdiff_plain;f=parsers.c;h=707bcdd5a7e67e5b93c20aec127704bd8a76eef1;hp=b895825030b9052433eebaf1f6c443dc02b19eb2;hb=1a0be0e3eb0145c4eca5854132ce144b3197273d;hpb=43e5747a59ed92243c217f7e36da2ac35bbcd80d diff --git a/parsers.c b/parsers.c index b895825..707bcdd 100644 --- a/parsers.c +++ b/parsers.c @@ -33,7 +33,9 @@ char **parse_probestring(int max_probes, const char *probestring) error = FALSE; range = NULL; - probelist = g_malloc0(max_probes * sizeof(char *)); + if (!(probelist = g_try_malloc0(max_probes * sizeof(char *)))) { + /* TODO: Handle errors. */ + } tokens = g_strsplit(probestring, ",", max_probes); for (i = 0; tokens[i]; i++) { @@ -42,8 +44,7 @@ char **parse_probestring(int max_probes, const char *probestring) 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]); + g_critical("Invalid probe syntax '%s'.", tokens[i]); error = TRUE; break; } @@ -51,8 +52,7 @@ char **parse_probestring(int max_probes, const char *probestring) 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]); + g_critical("Invalid probe range '%s'.", tokens[i]); error = TRUE; break; } @@ -65,7 +65,7 @@ char **parse_probestring(int max_probes, const char *probestring) } else { tmp = strtol(tokens[i], NULL, 10); if (tmp < 1 || tmp > max_probes) { - printf("Invalid probe %d.\n", tmp); + g_critical("Invalid probe %d.", tmp); error = TRUE; break; } @@ -122,63 +122,95 @@ GHashTable *parse_generic_arg(const char *arg) return hash; } -struct sr_device *parse_devicestring(const char *devicestring) +struct sr_dev *parse_devstring(const char *devstring) { - struct sr_device *device, *d; - struct sr_device_plugin *plugin; - GSList *devices, *plugins, *l, *p; - int num_devices, device_num, device_cnt; + struct sr_dev *dev, *d; + struct sr_dev_driver **drivers; + GSList *devs, *l; + int i, num_devs, dev_num, dev_cnt; char *tmp; - if (!devicestring) + if (!devstring) return NULL; - device = NULL; - device_num = strtol(devicestring, &tmp, 10); - if (tmp != devicestring) { - /* argument is numeric, meaning a device ID. Make all driver - * plugins scan for devices. + 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_devices = num_real_devices(); - if (device_num < 0 || device_num >= num_devices) + num_devs = num_real_devs(); + if (dev_num < 0 || dev_num >= num_devs) return NULL; - device_cnt = 0; - devices = sr_device_list(); - for (l = devices; l; l = l->next) { + dev_cnt = 0; + devs = sr_dev_list(); + for (l = devs; l; l = l->next) { d = l->data; - if (strstr(d->plugin->name, "demo")) + if (sr_dev_has_hwcap(d, SR_HWCAP_DEMO_DEV)) continue; - if (device_cnt == device_num) { - if (device_num == device_cnt) { - device = d; + if (dev_cnt == dev_num) { + if (dev_num == dev_cnt) { + dev = d; break; } } - device_cnt++; + dev_cnt++; } } else { /* select device by driver -- only initialize that driver, * no need to let them all scan */ - device = NULL; - plugins = sr_list_hwplugins(); - for (p = plugins; p; p = p->next) { - plugin = p->data; - if (strcmp(plugin->name, devicestring)) + dev = NULL; + drivers = sr_driver_list(); + for (i = 0; drivers[i]; i++) { + if (strcmp(drivers[i]->name, devstring)) continue; - num_devices = sr_init_hwplugins(plugin); - if (num_devices == 1) { - devices = sr_device_list(); - device = devices->data; - } else if (num_devices > 1) { + 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", - devicestring, num_devices); + devstring, num_devs); } /* fall through: selected driver found no devices */ break; } } - return device; + return dev; } + +char *strcanon(char *str) +{ + int p0, p1; + char *s; + + /* Returns newly allocated string. */ + s = g_ascii_strdown(str, -1); + for (p0 = p1 = 0; str[p0]; p0++) { + if ((s[p0] >= 'a' && s[p0] <= 'z') + || (s[p0] >= '0' && s[p0] <= '9')) + s[p1++] = s[p0]; + } + s[p1] = '\0'; + + return s; +} + + +int canon_cmp(char *str1, char *str2) +{ + int ret; + char *s1, *s2; + + s1 = strcanon(str1); + s2 = strcanon(str2); + ret = g_ascii_strcasecmp(s1, s2); + g_free(s2); + g_free(s1); + + return ret; +} +