Skip to content

Commit

Permalink
tests: Standardize programs error codes when scanning
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pcercuei committed Aug 9, 2023
1 parent d62bc62 commit b6028fd
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 58 deletions.
13 changes: 4 additions & 9 deletions examples/iio_adi_xflow_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ int main(int argc, char **argv)
const char *device_name;
struct iio_device *dev;
char unit;
int ret;
int ret = EXIT_FAILURE;
struct option *opts;
bool do_scan = false;

argw = dup_argv(MY_NAME, argc, argv);

ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS, options, options_descriptions);
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS,
options, options_descriptions, &ret);
opts = add_common_options(options);
if (!opts) {
fprintf(stderr, "Failed to add common options\n");
Expand All @@ -177,8 +177,6 @@ int main(int argc, char **argv)
case 'T':
break;
case 'S':
do_scan = true;
/* FALLTHROUGH */
case 'a':
if (!optarg && argc > optind && argv[optind] != NULL
&& argv[optind][0] != '-')
Expand Down Expand Up @@ -206,17 +204,14 @@ int main(int argc, char **argv)
}
free(opts);

if (do_scan)
return EXIT_SUCCESS;

if (optind + 1 != argc) {
fprintf(stderr, "Incorrect number of arguments.\n\n");
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}

if (!ctx)
return EXIT_FAILURE;
return ret;

#ifndef _WIN32
set_handler(SIGHUP, &quit_all);
Expand Down
17 changes: 4 additions & 13 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ int main(int argc, char **argv)
bool found_err = false, read_err = false, write_err = false,
dev_found = false, attr_found = false, ctx_found = false,
debug_found = false, channel_found = false ;
bool context_scan = false;
unsigned int i;
char *wbuf = NULL;
struct option *opts;
int ret = EXIT_FAILURE;

argw = dup_argv(MY_NAME, argc, argv);

Expand All @@ -364,7 +364,8 @@ int main(int argc, char **argv)
argd--;
}

ctx = handle_common_opts(MY_NAME, argd, argw, MY_OPTS, options, options_descriptions);
ctx = handle_common_opts(MY_NAME, argd, argw, MY_OPTS,
options, options_descriptions, &ret);
opts = add_common_options(options);
if (!opts) {
fprintf(stderr, "Failed to add common options\n");
Expand All @@ -382,8 +383,6 @@ int main(int argc, char **argv)
case 'T':
break;
case 'S':
context_scan = true;
/* FALLTHRU */
case 'a':
if (!optarg && argc > optind && argv[optind] != NULL
&& argv[optind][0] != '-')
Expand Down Expand Up @@ -443,11 +442,8 @@ int main(int argc, char **argv)

free(opts);

if (context_scan)
return EXIT_SUCCESS;

if (!ctx)
return EXIT_FAILURE;
return ret;

if (gen_code) {
if (!gen_test_path(gen_file)) {
Expand Down Expand Up @@ -600,7 +596,6 @@ int main(int argc, char **argv)
ctx_found = true;
for (i = 0; i < nb_ctx_attrs; i++) {
const char *key, *value;
ssize_t ret;

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

for (k = 0; k < nb_attrs; k++) {
int ret;
const char *attr =
iio_channel_get_attr(ch, k);

Expand Down Expand Up @@ -808,7 +802,6 @@ int main(int argc, char **argv)
}

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

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

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

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

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

if ((attr_index && str_match(attr, argw[attr_index],
Expand Down
40 changes: 26 additions & 14 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ char *cmn_strndup(const char *str, size_t n)
#endif
}

struct iio_context * autodetect_context(bool rtn, const char * name, const char * scan)
struct iio_context * autodetect_context(bool rtn, const char * name,
const char * scan, int *err_code)
{
struct iio_scan_context *scan_ctx;
struct iio_context_info **info;
struct iio_context *ctx = NULL;
unsigned int i;
ssize_t ret;
FILE *out;
int err = EXIT_FAILURE;

scan_ctx = iio_create_scan_context(scan, 0);
if (!scan_ctx) {
Expand Down Expand Up @@ -97,6 +99,7 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char
} else {
out = stdout;
fprintf(out, "Available contexts:\n");
err = EXIT_SUCCESS;
}
for (i = 0; i < (size_t) ret; i++) {
fprintf(out, "\t%u: %s [%s]\n",
Expand All @@ -110,6 +113,8 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char
err_free_ctx:
iio_scan_context_destroy(scan_ctx);

if (err_code)
*err_code = err;
return ctx;
}

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

struct iio_context * handle_common_opts(char * name, int argc,
char * const argv[], const char *optstring,
const struct option *options, const char *options_descriptions[])
const struct option *options, const char *options_descriptions[],
int *err_code)
{
struct iio_context *ctx = NULL;
int c;
Expand All @@ -280,7 +286,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
opts = add_common_options(options);
if (!opts) {
fprintf(stderr, "Failed to add common options\n");
return NULL;
goto err_fail;
}

while ((c = getopt_long(argc, argv, buf, /* Flawfinder: ignore */
Expand All @@ -296,43 +302,43 @@ struct iio_context * handle_common_opts(char * name, int argc,
case 'n':
if (backend != IIO_LOCAL) {
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
return NULL;
goto err_fail;
}
if (!optarg) {
fprintf(stderr, "network options requires a uri\n");
return NULL;
goto err_fail;
}
backend = IIO_NETWORK;
arg = optarg;
break;
case 'x':
if (backend != IIO_LOCAL) {
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
return NULL;
goto err_fail;
}
if (!optarg) {
fprintf(stderr, "xml options requires a uri\n");
return NULL;
goto err_fail;
}
backend = IIO_XML;
arg = optarg;
break;
case 'u':
if (backend != IIO_LOCAL) {
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
return NULL;
goto err_fail;
}
if (!optarg) {
fprintf(stderr, "uri options requires a uri\n");
return NULL;
goto err_fail;
}
backend = IIO_URI;
arg = optarg;
break;
case 'a':
if (backend != IIO_LOCAL) {
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
return NULL;
goto err_fail;
}
backend = IIO_AUTO;
detect_context = true;
Expand All @@ -355,7 +361,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
case 'T':
if (!optarg) {
fprintf(stderr, "Timeout requires an argument\n");
return NULL;
goto err_fail;
}
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
break;
Expand All @@ -368,10 +374,10 @@ struct iio_context * handle_common_opts(char * name, int argc,
opterr = 1;

if (do_scan) {
autodetect_context(false, name, arg);
autodetect_context(false, name, arg, err_code);
return NULL;
} else if (detect_context || backend == IIO_AUTO)
ctx = autodetect_context(true, name, arg);
ctx = autodetect_context(true, name, arg, err_code);
else if (!arg && backend != IIO_LOCAL)
fprintf(stderr, "argument parsing error\n");
else if (backend == IIO_XML)
Expand All @@ -390,6 +396,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
fprintf(stderr, "Unable to create IIO context %s: %s\n", arg, err_str);
else
fprintf(stderr, "Unable to create Local IIO context : %s\n", err_str);
goto err_fail;
}

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

return ctx;

err_fail:
if (err_code)
*err_code = EXIT_FAILURE;
return NULL;
}

void usage(char *name, const struct option *options,
Expand Down
6 changes: 4 additions & 2 deletions tests/iio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ enum backend {
void * xmalloc(size_t n, const char *name);
char *cmn_strndup(const char *str, size_t n);

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

struct iio_context * handle_common_opts(char * name, int argc,
char * const argv[], const char *optstring,
const struct option *options, const char *options_descriptions[]);
const struct option *options, const char *options_descriptions[],
int *ret);
struct option * add_common_options(const struct option * longopts);
void usage(char *name, const struct option *options, const char *options_descriptions[]);
void version(char *name);
Expand Down
7 changes: 4 additions & 3 deletions tests/iio_genxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ int main(int argc, char **argv)
char *xml;
const char *tmp;
struct iio_context *ctx;
int c;
size_t xml_len;
struct option *opts;
int c, ret = EXIT_FAILURE;

argw = dup_argv(MY_NAME, argc, argv);
ctx = handle_common_opts(MY_NAME, argc, argw, "", options, options_descriptions);
ctx = handle_common_opts(MY_NAME, argc, argw, "",
options, options_descriptions, &ret);
opts = add_common_options(options);
if (!opts) {
fprintf(stderr, "Failed to add common options\n");
Expand Down Expand Up @@ -73,7 +74,7 @@ int main(int argc, char **argv)
}

if (!ctx)
return EXIT_FAILURE;
return ret;

tmp = iio_context_get_xml(ctx);
if (!tmp) {
Expand Down
11 changes: 6 additions & 5 deletions tests/iio_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ int main(int argc, char **argv)
int c;
unsigned int i, major, minor;
char git_tag[8];
int ret;
struct option *opts;
int ret = EXIT_FAILURE;

argw = dup_argv(MY_NAME, argc, argv);

ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS, options, options_descriptions);
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS,
options, options_descriptions, &ret);
opts = add_common_options(options);
if (!opts) {
fprintf(stderr, "Failed to add common options\n");
Expand All @@ -82,8 +83,8 @@ int main(int argc, char **argv)
optind++;
break;
case 's':
autodetect_context(false, MY_NAME, NULL);
return EXIT_SUCCESS;
autodetect_context(false, MY_NAME, NULL, &ret);
return ret;
case '?':
printf("Unknown argument '%c'\n", c);
return EXIT_FAILURE;
Expand All @@ -98,7 +99,7 @@ int main(int argc, char **argv)
}

if (!ctx)
return EXIT_FAILURE;
return ret;

version(MY_NAME);
printf("IIO context created with %s backend.\n",
Expand Down
6 changes: 4 additions & 2 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,14 @@ int main(int argc, char **argv)
struct option *opts;
bool mib, benchmark = false;
uint64_t before = 0, after, rate, total;
int err_code = EXIT_FAILURE;

argw = dup_argv(MY_NAME, argc, argv);

setup_sig_handler();

ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS, options, options_descriptions);
ctx = handle_common_opts(MY_NAME, argc, argw, MY_OPTS,
options, options_descriptions, &err_code);
opts = add_common_options(options);
if (!opts) {
fprintf(stderr, "Failed to add common options\n");
Expand Down Expand Up @@ -260,7 +262,7 @@ int main(int argc, char **argv)
}

if (!ctx)
return EXIT_FAILURE;
return err_code;

if (!argw[optind]) {
unsigned int nb_devices = iio_context_get_devices_count(ctx);
Expand Down
Loading

0 comments on commit b6028fd

Please sign in to comment.