Skip to content

Commit b6028fd

Browse files
committed
tests: Standardize programs error codes when scanning
The tools (as well as the iio_adi_xflow_check program) will now return 1 when scanning failed, or 0 when scanning detected at least one IIO context. Signed-off-by: Paul Cercueil <[email protected]>
1 parent d62bc62 commit b6028fd

File tree

9 files changed

+62
-58
lines changed

9 files changed

+62
-58
lines changed

examples/iio_adi_xflow_check.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ int main(int argc, char **argv)
153153
const char *device_name;
154154
struct iio_device *dev;
155155
char unit;
156-
int ret;
156+
int ret = EXIT_FAILURE;
157157
struct option *opts;
158-
bool do_scan = false;
159158

160159
argw = dup_argv(MY_NAME, argc, argv);
161160

162-
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS, options, options_descriptions);
161+
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS,
162+
options, options_descriptions, &ret);
163163
opts = add_common_options(options);
164164
if (!opts) {
165165
fprintf(stderr, "Failed to add common options\n");
@@ -177,8 +177,6 @@ int main(int argc, char **argv)
177177
case 'T':
178178
break;
179179
case 'S':
180-
do_scan = true;
181-
/* FALLTHROUGH */
182180
case 'a':
183181
if (!optarg && argc > optind && argv[optind] != NULL
184182
&& argv[optind][0] != '-')
@@ -206,17 +204,14 @@ int main(int argc, char **argv)
206204
}
207205
free(opts);
208206

209-
if (do_scan)
210-
return EXIT_SUCCESS;
211-
212207
if (optind + 1 != argc) {
213208
fprintf(stderr, "Incorrect number of arguments.\n\n");
214209
usage(MY_NAME, options, options_descriptions);
215210
return EXIT_FAILURE;
216211
}
217212

218213
if (!ctx)
219-
return EXIT_FAILURE;
214+
return ret;
220215

221216
#ifndef _WIN32
222217
set_handler(SIGHUP, &quit_all);

tests/iio_attr.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,10 @@ int main(int argc, char **argv)
346346
bool found_err = false, read_err = false, write_err = false,
347347
dev_found = false, attr_found = false, ctx_found = false,
348348
debug_found = false, channel_found = false ;
349-
bool context_scan = false;
350349
unsigned int i;
351350
char *wbuf = NULL;
352351
struct option *opts;
352+
int ret = EXIT_FAILURE;
353353

354354
argw = dup_argv(MY_NAME, argc, argv);
355355

@@ -364,7 +364,8 @@ int main(int argc, char **argv)
364364
argd--;
365365
}
366366

367-
ctx = handle_common_opts(MY_NAME, argd, argw, MY_OPTS, options, options_descriptions);
367+
ctx = handle_common_opts(MY_NAME, argd, argw, MY_OPTS,
368+
options, options_descriptions, &ret);
368369
opts = add_common_options(options);
369370
if (!opts) {
370371
fprintf(stderr, "Failed to add common options\n");
@@ -382,8 +383,6 @@ int main(int argc, char **argv)
382383
case 'T':
383384
break;
384385
case 'S':
385-
context_scan = true;
386-
/* FALLTHRU */
387386
case 'a':
388387
if (!optarg && argc > optind && argv[optind] != NULL
389388
&& argv[optind][0] != '-')
@@ -443,11 +442,8 @@ int main(int argc, char **argv)
443442

444443
free(opts);
445444

446-
if (context_scan)
447-
return EXIT_SUCCESS;
448-
449445
if (!ctx)
450-
return EXIT_FAILURE;
446+
return ret;
451447

452448
if (gen_code) {
453449
if (!gen_test_path(gen_file)) {
@@ -600,7 +596,6 @@ int main(int argc, char **argv)
600596
ctx_found = true;
601597
for (i = 0; i < nb_ctx_attrs; i++) {
602598
const char *key, *value;
603-
ssize_t ret;
604599

605600
ret = iio_context_get_attr(ctx, i, &key, &value);
606601
if (!ret) {
@@ -776,7 +771,6 @@ int main(int argc, char **argv)
776771
continue;
777772

778773
for (k = 0; k < nb_attrs; k++) {
779-
int ret;
780774
const char *attr =
781775
iio_channel_get_attr(ch, k);
782776

@@ -808,7 +802,6 @@ int main(int argc, char **argv)
808802
}
809803

810804
if (search_device && device_index && nb_attrs) {
811-
int ret;
812805
for (j = 0; j < nb_attrs; j++) {
813806
const char *attr = iio_device_get_attr(dev, j);
814807

@@ -841,7 +834,6 @@ int main(int argc, char **argv)
841834

842835
if (search_buffer && device_index && nb_attrs) {
843836
for (j = 0; j < nb_attrs; j++) {
844-
int ret;
845837
const char *attr = iio_device_get_buffer_attr(dev, j);
846838

847839
if ((attr_index && str_match(attr, argw[attr_index],
@@ -867,7 +859,6 @@ int main(int argc, char **argv)
867859

868860
if (search_debug && device_index && nb_attrs) {
869861
for (j = 0; j < nb_attrs; j++) {
870-
int ret;
871862
const char *attr = iio_device_get_debug_attr(dev, j);
872863

873864
if ((attr_index && str_match(attr, argw[attr_index],

tests/iio_common.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ char *cmn_strndup(const char *str, size_t n)
5858
#endif
5959
}
6060

61-
struct iio_context * autodetect_context(bool rtn, const char * name, const char * scan)
61+
struct iio_context * autodetect_context(bool rtn, const char * name,
62+
const char * scan, int *err_code)
6263
{
6364
struct iio_scan_context *scan_ctx;
6465
struct iio_context_info **info;
6566
struct iio_context *ctx = NULL;
6667
unsigned int i;
6768
ssize_t ret;
6869
FILE *out;
70+
int err = EXIT_FAILURE;
6971

7072
scan_ctx = iio_create_scan_context(scan, 0);
7173
if (!scan_ctx) {
@@ -97,6 +99,7 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char
9799
} else {
98100
out = stdout;
99101
fprintf(out, "Available contexts:\n");
102+
err = EXIT_SUCCESS;
100103
}
101104
for (i = 0; i < (size_t) ret; i++) {
102105
fprintf(out, "\t%u: %s [%s]\n",
@@ -110,6 +113,8 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char
110113
err_free_ctx:
111114
iio_scan_context_destroy(scan_ctx);
112115

116+
if (err_code)
117+
*err_code = err;
113118
return ctx;
114119
}
115120

@@ -260,7 +265,8 @@ static const char *common_options_descriptions[] = {
260265

261266
struct iio_context * handle_common_opts(char * name, int argc,
262267
char * const argv[], const char *optstring,
263-
const struct option *options, const char *options_descriptions[])
268+
const struct option *options, const char *options_descriptions[],
269+
int *err_code)
264270
{
265271
struct iio_context *ctx = NULL;
266272
int c;
@@ -280,7 +286,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
280286
opts = add_common_options(options);
281287
if (!opts) {
282288
fprintf(stderr, "Failed to add common options\n");
283-
return NULL;
289+
goto err_fail;
284290
}
285291

286292
while ((c = getopt_long(argc, argv, buf, /* Flawfinder: ignore */
@@ -296,43 +302,43 @@ struct iio_context * handle_common_opts(char * name, int argc,
296302
case 'n':
297303
if (backend != IIO_LOCAL) {
298304
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
299-
return NULL;
305+
goto err_fail;
300306
}
301307
if (!optarg) {
302308
fprintf(stderr, "network options requires a uri\n");
303-
return NULL;
309+
goto err_fail;
304310
}
305311
backend = IIO_NETWORK;
306312
arg = optarg;
307313
break;
308314
case 'x':
309315
if (backend != IIO_LOCAL) {
310316
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
311-
return NULL;
317+
goto err_fail;
312318
}
313319
if (!optarg) {
314320
fprintf(stderr, "xml options requires a uri\n");
315-
return NULL;
321+
goto err_fail;
316322
}
317323
backend = IIO_XML;
318324
arg = optarg;
319325
break;
320326
case 'u':
321327
if (backend != IIO_LOCAL) {
322328
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
323-
return NULL;
329+
goto err_fail;
324330
}
325331
if (!optarg) {
326332
fprintf(stderr, "uri options requires a uri\n");
327-
return NULL;
333+
goto err_fail;
328334
}
329335
backend = IIO_URI;
330336
arg = optarg;
331337
break;
332338
case 'a':
333339
if (backend != IIO_LOCAL) {
334340
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
335-
return NULL;
341+
goto err_fail;
336342
}
337343
backend = IIO_AUTO;
338344
detect_context = true;
@@ -355,7 +361,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
355361
case 'T':
356362
if (!optarg) {
357363
fprintf(stderr, "Timeout requires an argument\n");
358-
return NULL;
364+
goto err_fail;
359365
}
360366
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
361367
break;
@@ -368,10 +374,10 @@ struct iio_context * handle_common_opts(char * name, int argc,
368374
opterr = 1;
369375

370376
if (do_scan) {
371-
autodetect_context(false, name, arg);
377+
autodetect_context(false, name, arg, err_code);
372378
return NULL;
373379
} else if (detect_context || backend == IIO_AUTO)
374-
ctx = autodetect_context(true, name, arg);
380+
ctx = autodetect_context(true, name, arg, err_code);
375381
else if (!arg && backend != IIO_LOCAL)
376382
fprintf(stderr, "argument parsing error\n");
377383
else if (backend == IIO_XML)
@@ -390,6 +396,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
390396
fprintf(stderr, "Unable to create IIO context %s: %s\n", arg, err_str);
391397
else
392398
fprintf(stderr, "Unable to create Local IIO context : %s\n", err_str);
399+
goto err_fail;
393400
}
394401

395402
if (ctx && timeout >= 0) {
@@ -400,11 +407,16 @@ struct iio_context * handle_common_opts(char * name, int argc,
400407
fprintf(stderr, "IIO contexts set timeout failed : %s\n",
401408
err_str);
402409
iio_context_destroy(ctx);
403-
return NULL;
410+
goto err_fail;
404411
}
405412
}
406413

407414
return ctx;
415+
416+
err_fail:
417+
if (err_code)
418+
*err_code = EXIT_FAILURE;
419+
return NULL;
408420
}
409421

410422
void usage(char *name, const struct option *options,

tests/iio_common.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ enum backend {
3131
void * xmalloc(size_t n, const char *name);
3232
char *cmn_strndup(const char *str, size_t n);
3333

34-
struct iio_context * autodetect_context(bool rtn, const char *name, const char *scan);
34+
struct iio_context * autodetect_context(bool rtn, const char *name,
35+
const char *scan, int *err_code);
3536
unsigned long int sanitize_clamp(const char *name, const char *argv,
3637
uint64_t min, uint64_t max);
3738
int iio_device_enable_channel(const struct iio_device *dev, const char * channel, bool type);
@@ -44,7 +45,8 @@ int iio_device_enable_channel(const struct iio_device *dev, const char * channel
4445

4546
struct iio_context * handle_common_opts(char * name, int argc,
4647
char * const argv[], const char *optstring,
47-
const struct option *options, const char *options_descriptions[]);
48+
const struct option *options, const char *options_descriptions[],
49+
int *ret);
4850
struct option * add_common_options(const struct option * longopts);
4951
void usage(char *name, const struct option *options, const char *options_descriptions[]);
5052
void version(char *name);

tests/iio_genxml.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ int main(int argc, char **argv)
3131
char *xml;
3232
const char *tmp;
3333
struct iio_context *ctx;
34-
int c;
3534
size_t xml_len;
3635
struct option *opts;
36+
int c, ret = EXIT_FAILURE;
3737

3838
argw = dup_argv(MY_NAME, argc, argv);
39-
ctx = handle_common_opts(MY_NAME, argc, argw, "", options, options_descriptions);
39+
ctx = handle_common_opts(MY_NAME, argc, argw, "",
40+
options, options_descriptions, &ret);
4041
opts = add_common_options(options);
4142
if (!opts) {
4243
fprintf(stderr, "Failed to add common options\n");
@@ -73,7 +74,7 @@ int main(int argc, char **argv)
7374
}
7475

7576
if (!ctx)
76-
return EXIT_FAILURE;
77+
return ret;
7778

7879
tmp = iio_context_get_xml(ctx);
7980
if (!tmp) {

tests/iio_info.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ int main(int argc, char **argv)
5353
int c;
5454
unsigned int i, major, minor;
5555
char git_tag[8];
56-
int ret;
5756
struct option *opts;
57+
int ret = EXIT_FAILURE;
5858

5959
argw = dup_argv(MY_NAME, argc, argv);
6060

61-
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS, options, options_descriptions);
61+
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS,
62+
options, options_descriptions, &ret);
6263
opts = add_common_options(options);
6364
if (!opts) {
6465
fprintf(stderr, "Failed to add common options\n");
@@ -82,8 +83,8 @@ int main(int argc, char **argv)
8283
optind++;
8384
break;
8485
case 's':
85-
autodetect_context(false, MY_NAME, NULL);
86-
return EXIT_SUCCESS;
86+
autodetect_context(false, MY_NAME, NULL, &ret);
87+
return ret;
8788
case '?':
8889
printf("Unknown argument '%c'\n", c);
8990
return EXIT_FAILURE;
@@ -98,7 +99,7 @@ int main(int argc, char **argv)
9899
}
99100

100101
if (!ctx)
101-
return EXIT_FAILURE;
102+
return ret;
102103

103104
version(MY_NAME);
104105
printf("IIO context created with %s backend.\n",

tests/iio_readdev.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,14 @@ int main(int argc, char **argv)
193193
struct option *opts;
194194
bool mib, benchmark = false;
195195
uint64_t before = 0, after, rate, total;
196+
int err_code = EXIT_FAILURE;
196197

197198
argw = dup_argv(MY_NAME, argc, argv);
198199

199200
setup_sig_handler();
200201

201-
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS, options, options_descriptions);
202+
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS,
203+
options, options_descriptions, &err_code);
202204
opts = add_common_options(options);
203205
if (!opts) {
204206
fprintf(stderr, "Failed to add common options\n");
@@ -260,7 +262,7 @@ int main(int argc, char **argv)
260262
}
261263

262264
if (!ctx)
263-
return EXIT_FAILURE;
265+
return err_code;
264266

265267
if (!argw[optind]) {
266268
unsigned int nb_devices = iio_context_get_devices_count(ctx);

0 commit comments

Comments
 (0)