From b6028fdeef888ab45f7c1dd6e4ed9480ae4b55e3 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Wed, 9 Aug 2023 16:19:32 +0200 Subject: [PATCH] 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 --- examples/iio_adi_xflow_check.c | 13 ++++------- tests/iio_attr.c | 17 ++++----------- tests/iio_common.c | 40 ++++++++++++++++++++++------------ tests/iio_common.h | 6 +++-- tests/iio_genxml.c | 7 +++--- tests/iio_info.c | 11 +++++----- tests/iio_readdev.c | 6 +++-- tests/iio_reg.c | 14 +++++------- tests/iio_writedev.c | 6 +++-- 9 files changed, 62 insertions(+), 58 deletions(-) diff --git a/examples/iio_adi_xflow_check.c b/examples/iio_adi_xflow_check.c index 628261b5e..30eb45446 100644 --- a/examples/iio_adi_xflow_check.c +++ b/examples/iio_adi_xflow_check.c @@ -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"); @@ -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] != '-') @@ -206,9 +204,6 @@ 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); @@ -216,7 +211,7 @@ int main(int argc, char **argv) } if (!ctx) - return EXIT_FAILURE; + return ret; #ifndef _WIN32 set_handler(SIGHUP, &quit_all); diff --git a/tests/iio_attr.c b/tests/iio_attr.c index cd7a40742..23e3b9d12 100644 --- a/tests/iio_attr.c +++ b/tests/iio_attr.c @@ -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); @@ -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"); @@ -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] != '-') @@ -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)) { @@ -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) { @@ -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); @@ -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); @@ -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], @@ -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], diff --git a/tests/iio_common.c b/tests/iio_common.c index 879f6ec3b..e2eabd5a5 100644 --- a/tests/iio_common.c +++ b/tests/iio_common.c @@ -58,7 +58,8 @@ 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; @@ -66,6 +67,7 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char unsigned int i; ssize_t ret; FILE *out; + int err = EXIT_FAILURE; scan_ctx = iio_create_scan_context(scan, 0); if (!scan_ctx) { @@ -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", @@ -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; } @@ -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; @@ -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 */ @@ -296,11 +302,11 @@ 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; @@ -308,11 +314,11 @@ struct iio_context * handle_common_opts(char * name, int argc, 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; @@ -320,11 +326,11 @@ struct iio_context * handle_common_opts(char * name, int argc, 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; @@ -332,7 +338,7 @@ struct iio_context * handle_common_opts(char * name, int argc, 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; @@ -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; @@ -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) @@ -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) { @@ -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, diff --git a/tests/iio_common.h b/tests/iio_common.h index 25e15d8d7..aa91ee99f 100644 --- a/tests/iio_common.h +++ b/tests/iio_common.h @@ -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); @@ -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); diff --git a/tests/iio_genxml.c b/tests/iio_genxml.c index 7368c8ce7..29a77c813 100644 --- a/tests/iio_genxml.c +++ b/tests/iio_genxml.c @@ -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"); @@ -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) { diff --git a/tests/iio_info.c b/tests/iio_info.c index 0e9c80471..8a94786aa 100644 --- a/tests/iio_info.c +++ b/tests/iio_info.c @@ -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"); @@ -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; @@ -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", diff --git a/tests/iio_readdev.c b/tests/iio_readdev.c index fc87974e1..ff4002186 100644 --- a/tests/iio_readdev.c +++ b/tests/iio_readdev.c @@ -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"); @@ -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); diff --git a/tests/iio_reg.c b/tests/iio_reg.c index 55c2cfc97..bcfb28719 100644 --- a/tests/iio_reg.c +++ b/tests/iio_reg.c @@ -66,14 +66,14 @@ int main(int argc, char **argv) unsigned long addr; struct iio_context *ctx; struct iio_device *dev; - int c; + int c, ret = EXIT_FAILURE; char * name; struct option *opts; - bool do_scan = false; 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"); @@ -91,8 +91,6 @@ int main(int argc, char **argv) case 'T': break; case 'S': - do_scan = true; - /* FALLTHRU */ case 'a': if (!optarg && argc > optind && argv[optind] != NULL && argv[optind][0] != '-') @@ -105,14 +103,14 @@ int main(int argc, char **argv) } free(opts); - if (do_scan) - return EXIT_SUCCESS; - if ((argc - optind) < 2 || (argc - optind) > 3) { usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; } + if (!ctx) + return ret; + name = cmn_strndup(argw[optind], NAME_MAX); dev = iio_context_find_device(ctx, name); if (!dev) { diff --git a/tests/iio_writedev.c b/tests/iio_writedev.c index a00e36c4a..ed694bd2d 100644 --- a/tests/iio_writedev.c +++ b/tests/iio_writedev.c @@ -204,12 +204,14 @@ int main(int argc, char **argv) ssize_t ret; struct option *opts; 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"); @@ -272,7 +274,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);