Skip to content

Commit 53c4add

Browse files
committed
iio_utils: now that we support backend filtering, do so.
On the -S (scan) and -a (connect automatically). This enables things like any iio_utility to do something like: (only scan USB context) iio_info -S usb (only attach to USB context) iio_info -a usb: (only scan USB or IP context) iio_info -S usb:ip: Signed-off-by: Robin Getz <[email protected]>
1 parent d532845 commit 53c4add

File tree

9 files changed

+54
-18
lines changed

9 files changed

+54
-18
lines changed

tests/iio_adi_xflow_check.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,12 @@ int main(int argc, char **argv)
177177
case 'h':
178178
case 'n':
179179
case 'x':
180-
case 'S':
181180
case 'u':
181+
break;
182+
case 'S':
182183
case 'a':
184+
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
185+
optind++;
183186
break;
184187

185188
case 's':

tests/iio_attr.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,12 @@ int main(int argc, char **argv)
368368
case 'h':
369369
case 'n':
370370
case 'x':
371-
case 'S':
372371
case 'u':
372+
break;
373+
case 'S':
373374
case 'a':
375+
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
376+
optind++;
374377
break;
375378
/* Attribute type
376379
* 'd'evice, 'c'hannel, 'C'ontext, 'B'uffer or 'D'ebug

tests/iio_common.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ char *cmn_strndup(const char *str, size_t n)
7272

7373

7474

75-
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * name)
75+
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * name, const char * scan)
7676
{
7777
struct iio_scan_context *scan_ctx;
7878
struct iio_context_info **info;
@@ -81,7 +81,7 @@ struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * na
8181
ssize_t ret;
8282
FILE *out;
8383

84-
scan_ctx = iio_create_scan_context(NULL, 0);
84+
scan_ctx = iio_create_scan_context(scan, 0);
8585
if (!scan_ctx) {
8686
fprintf(stderr, "Unable to create scan context\n");
8787
return NULL;
@@ -195,17 +195,19 @@ static const struct option common_options[] = {
195195
{"help", no_argument, 0, 'h'},
196196
{"xml", required_argument, 0, 'x'},
197197
{"uri", required_argument, 0, 'u'},
198-
{"scan", no_argument, 0, 'S'},
199-
{"auto", no_argument, 0, 'a'},
198+
{"scan", optional_argument, 0, 'S'},
199+
{"auto", optional_argument, 0, 'a'},
200200
{0, 0, 0, 0},
201201
};
202202

203203
static const char *common_options_descriptions[] = {
204204
"Show this help and quit.",
205205
"Use the XML backend with the provided XML file.",
206206
"Use the context at the provided URI.",
207-
"Scan for available backends.",
208-
"Scan for available contexts and if only one is available use it.",
207+
"Scan for available backends."
208+
"\n\t\t\toptional arg of specific backend(s)",
209+
"Scan for available contexts and if only one is available use it."
210+
"\n\t\t\toptional arg of specific backend(s)",
209211
};
210212

211213

@@ -222,6 +224,7 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
222224
opterr = 0;
223225
/* start over at first index */
224226
optind = 1;
227+
225228
while ((c = getopt_long(argc, argv, COMMON_OPTIONS, /* Flawfinder: ignore */
226229
common_options, &option_index)) != -1) {
227230
switch (c) {
@@ -258,9 +261,21 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
258261
return NULL;
259262
}
260263
detect_context = true;
264+
if (optarg) {
265+
arg = optarg;
266+
} else {
267+
if (argv[optind] && argv[optind][0] != '-')
268+
arg = argv[optind++];
269+
}
261270
break;
262271
case 'S':
263272
do_scan = true;
273+
if (optarg) {
274+
arg = optarg;
275+
} else {
276+
if (argv[optind] && argv[optind][0] != '-')
277+
arg = argv[optind++];
278+
}
264279
break;
265280
case '?':
266281
break;
@@ -270,10 +285,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
270285
opterr = 1;
271286

272287
if (do_scan) {
273-
autodetect_context(false, false, name);
288+
autodetect_context(false, false, name, arg);
274289
exit(0);
275290
} else if (detect_context)
276-
ctx = autodetect_context(true, false, name);
291+
ctx = autodetect_context(true, false, name, arg);
277292
else if (!arg && backend != IIO_LOCAL)
278293
fprintf(stderr, "argument parsing error\n");
279294
else if (backend == IIO_XML)

tests/iio_common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ enum backend {
4242
void * xmalloc(size_t n, const char *name);
4343
char *cmn_strndup(const char *str, size_t n);
4444

45-
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name);
45+
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name, const char *scan);
4646
unsigned long int sanitize_clamp(const char *name, const char *argv,
4747
uint64_t min, uint64_t max);
4848

49-
#define COMMON_OPTIONS "hn:x:u:aS"
49+
#define COMMON_OPTIONS "hn:x:u:a::S::"
5050
struct iio_context * handle_common_opts(char * name, int argc, char * const argv[],
5151
const struct option *options, const char *options_descriptions[]);
5252
void usage(char *name, const struct option *options, const char *options_descriptions[]);

tests/iio_genxml.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ int main(int argc, char **argv)
5757
case 'h':
5858
case 'n':
5959
case 'x':
60-
case 'S':
6160
case 'u':
61+
break;
62+
case 'S':
6263
case 'a':
64+
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
65+
optind++;
6366
break;
6467
case '?':
6568
printf("Unknown argument '%c'\n", c);

tests/iio_info.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ int main(int argc, char **argv)
8585
case 'h':
8686
case 'n':
8787
case 'x':
88-
case 'S':
8988
case 'u':
89+
break;
90+
case 'S':
9091
case 'a':
92+
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
93+
optind++;
9194
break;
9295
case 's':
93-
autodetect_context(false, false, MY_NAME);
96+
autodetect_context(false, false, MY_NAME, NULL);
9497
return EXIT_SUCCESS;
9598
case '?':
9699
printf("Unknown argument '%c'\n", c);

tests/iio_readdev.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,12 @@ int main(int argc, char **argv)
211211
case 'h':
212212
case 'n':
213213
case 'x':
214-
case 'S':
215214
case 'u':
215+
break;
216+
case 'S':
216217
case 'a':
218+
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
219+
optind++;
217220
break;
218221
case 't':
219222
trigger_name = optarg;

tests/iio_reg.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ int main(int argc, char **argv)
9393
case 'h':
9494
case 'n':
9595
case 'x':
96-
case 'S':
9796
case 'u':
97+
break;
98+
case 'S':
9899
case 'a':
100+
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
101+
optind++;
99102
break;
100103
case '?':
101104
printf("Unknown argument '%c'\n", c);

tests/iio_writedev.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,12 @@ int main(int argc, char **argv)
222222
case 'h':
223223
case 'n':
224224
case 'x':
225-
case 'S':
226225
case 'u':
226+
break;
227+
case 'S':
227228
case 'a':
229+
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
230+
optind++;
228231
break;
229232
case 't':
230233
trigger_name = optarg;

0 commit comments

Comments
 (0)