blob: 794e4b56c3e73d36dbb0b8ed9fa342fa2f2df982 [file] [log] [blame]
Uwe Hermann43e57472011-12-30 11:23:22 +01001/*
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 Vermeulen60ea8932012-07-05 01:06:28 +020025#include <libsigrok/libsigrok.h>
Uwe Hermann43e57472011-12-30 11:23:22 +010026#include "sigrok-cli.h"
27
28char **parse_probestring(int max_probes, const char *probestring)
29{
30 int tmp, b, e, i;
31 char **tokens, **range, **probelist, *name, str[8];
32 gboolean error;
33
34 error = FALSE;
35 range = NULL;
Uwe Hermannc2c4a0d2012-02-11 20:06:46 +010036 if (!(probelist = g_try_malloc0(max_probes * sizeof(char *)))) {
37 /* TODO: Handle errors. */
38 }
Uwe Hermann43e57472011-12-30 11:23:22 +010039 tokens = g_strsplit(probestring, ",", max_probes);
40
41 for (i = 0; tokens[i]; i++) {
42 if (strchr(tokens[i], '-')) {
43 /* A range of probes in the form 1-5. */
44 range = g_strsplit(tokens[i], "-", 2);
45 if (!range[0] || !range[1] || range[2]) {
46 /* Need exactly two arguments. */
Bert Vermeulen8170b8e2012-05-26 04:54:34 +020047 g_critical("Invalid probe syntax '%s'.", tokens[i]);
Uwe Hermann43e57472011-12-30 11:23:22 +010048 error = TRUE;
49 break;
50 }
51
52 b = strtol(range[0], NULL, 10);
53 e = strtol(range[1], NULL, 10);
Bert Vermeulen8c8056b2012-07-23 03:03:07 +020054 if (b < 0 || e >= max_probes || b >= e) {
Bert Vermeulen8170b8e2012-05-26 04:54:34 +020055 g_critical("Invalid probe range '%s'.", tokens[i]);
Uwe Hermann43e57472011-12-30 11:23:22 +010056 error = TRUE;
57 break;
58 }
59
60 while (b <= e) {
61 snprintf(str, 7, "%d", b);
Bert Vermeulen8c8056b2012-07-23 03:03:07 +020062 probelist[b] = g_strdup(str);
Uwe Hermann43e57472011-12-30 11:23:22 +010063 b++;
64 }
65 } else {
66 tmp = strtol(tokens[i], NULL, 10);
Bert Vermeulen8c8056b2012-07-23 03:03:07 +020067 if (tmp < 0 || tmp >= max_probes) {
Bert Vermeulen8170b8e2012-05-26 04:54:34 +020068 g_critical("Invalid probe %d.", tmp);
Uwe Hermann43e57472011-12-30 11:23:22 +010069 error = TRUE;
70 break;
71 }
72
73 if ((name = strchr(tokens[i], '='))) {
Bert Vermeulen8c8056b2012-07-23 03:03:07 +020074 probelist[tmp] = g_strdup(++name);
75 if (strlen(probelist[tmp]) > SR_MAX_PROBENAME_LEN)
76 probelist[tmp][SR_MAX_PROBENAME_LEN] = 0;
Uwe Hermann43e57472011-12-30 11:23:22 +010077 } else {
78 snprintf(str, 7, "%d", tmp);
Bert Vermeulen8c8056b2012-07-23 03:03:07 +020079 probelist[tmp] = g_strdup(str);
Uwe Hermann43e57472011-12-30 11:23:22 +010080 }
81 }
82 }
83
84 if (error) {
85 for (i = 0; i < max_probes; i++)
86 if (probelist[i])
87 g_free(probelist[i]);
88 g_free(probelist);
89 probelist = NULL;
90 }
91
92 g_strfreev(tokens);
93 if (range)
94 g_strfreev(range);
95
96 return probelist;
97}
98
Bert Vermeulen63bb4542012-07-16 01:03:30 +020099GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
Uwe Hermann43e57472011-12-30 11:23:22 +0100100{
101 GHashTable *hash;
102 int i;
103 char **elements, *e;
104
105 if (!arg || !arg[0])
106 return NULL;
107
Bert Vermeulen63bb4542012-07-16 01:03:30 +0200108 i = 0;
109 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
110 g_free, g_free);
Uwe Hermann43e57472011-12-30 11:23:22 +0100111 elements = g_strsplit(arg, ":", 0);
Bert Vermeulen63bb4542012-07-16 01:03:30 +0200112 if (sep_first)
113 g_hash_table_insert(hash, g_strdup("sigrok_key"),
114 g_strdup(elements[i++]));
115 for (; elements[i]; i++) {
Uwe Hermann43e57472011-12-30 11:23:22 +0100116 e = strchr(elements[i], '=');
117 if (!e)
118 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
119 else {
120 *e++ = '\0';
121 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
122 }
123 }
124 g_strfreev(elements);
125
126 return hash;
127}
128
Uwe Hermannd2ee5ee2012-05-30 22:15:29 +0200129char *strcanon(const char *str)
Bert Vermeulen432de702012-05-29 12:12:51 +0200130{
131 int p0, p1;
132 char *s;
133
134 /* Returns newly allocated string. */
135 s = g_ascii_strdown(str, -1);
136 for (p0 = p1 = 0; str[p0]; p0++) {
137 if ((s[p0] >= 'a' && s[p0] <= 'z')
138 || (s[p0] >= '0' && s[p0] <= '9'))
139 s[p1++] = s[p0];
140 }
141 s[p1] = '\0';
142
143 return s;
144}
145
Uwe Hermannd2ee5ee2012-05-30 22:15:29 +0200146int canon_cmp(const char *str1, const char *str2)
Bert Vermeulen432de702012-05-29 12:12:51 +0200147{
148 int ret;
149 char *s1, *s2;
150
151 s1 = strcanon(str1);
152 s2 = strcanon(str2);
153 ret = g_ascii_strcasecmp(s1, s2);
154 g_free(s2);
155 g_free(s1);
156
157 return ret;
158}