From 5c66cc28d922c02b876ba052d68716a41d7c86eb Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Sun, 19 Apr 2020 14:30:33 -0400 Subject: [PATCH] test: use a common function for printing usage. Shuffle the common usage() function in the common library, and remove it from all the individual applications. Signed-off-by: Robin Getz --- tests/CMakeLists.txt | 2 +- tests/iio_adi_xflow_check.c | 16 +++------------- tests/iio_attr.c | 37 +++++++------------------------------ tests/iio_common.c | 17 +++++++++++++++++ tests/iio_common.h | 3 +++ tests/iio_genxml.c | 20 +++++--------------- tests/iio_info.c | 20 +++++--------------- tests/iio_readdev.c | 19 ++++--------------- tests/iio_stresstest.c | 23 ++++++----------------- tests/iio_writedev.c | 20 +++++--------------- 10 files changed, 56 insertions(+), 121 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6ebc64a9f..27bcf7795 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -36,7 +36,7 @@ add_executable(iio_readdev iio_readdev.c ${GETOPT_C_FILE} ${LIBIIO_RC}) add_executable(iio_reg iio_reg.c ${GETOPT_C_FILE} ${LIBIIO_RC}) add_executable(iio_writedev iio_writedev.c ${GETOPT_C_FILE} ${LIBIIO_RC}) -target_link_libraries(iio_genxml iio) +target_link_libraries(iio_genxml iio iio_tests_helper) target_link_libraries(iio_info iio iio_tests_helper) target_link_libraries(iio_attr iio iio_tests_helper) target_link_libraries(iio_readdev iio iio_tests_helper) diff --git a/tests/iio_adi_xflow_check.c b/tests/iio_adi_xflow_check.c index 6c3fa1a5f..b01205011 100644 --- a/tests/iio_adi_xflow_check.c +++ b/tests/iio_adi_xflow_check.c @@ -48,6 +48,7 @@ static const struct option options[] = { }; static const char *options_descriptions[] = { + "[-n ] [-u ] [-a ] [-s ] ", "Show this help and quit.", "Use the network backend with the provided hostname.", "Use the context with the provided URI.", @@ -55,17 +56,6 @@ static const char *options_descriptions[] = { "Scan for available contexts and if only one is available use it.", }; -static void usage(char *argv[]) -{ - unsigned int i; - - printf("Usage:\n\t%s [-n ] [-u ] [ -a ][-s ] \n\nOptions:\n", argv[0]); - for (i = 0; options[i].name; i++) - printf("\t-%c, --%s\n\t\t\t%s\n", - options[i].val, options[i].name, - options_descriptions[i]); -} - static bool app_running = true; static bool device_is_tx; @@ -188,7 +178,7 @@ int main(int argc, char **argv) options, &option_index)) != -1) { switch (c) { case 'h': - usage(argv); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; case 's': ret = sscanf(optarg, "%u%c", &buffer_size, &unit); @@ -217,7 +207,7 @@ int main(int argc, char **argv) if (optind + 1 != argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); - usage(argv); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; } diff --git a/tests/iio_attr.c b/tests/iio_attr.c index 79c7babf7..090dcc667 100644 --- a/tests/iio_attr.c +++ b/tests/iio_attr.c @@ -292,6 +292,11 @@ static const struct option options[] = { }; static const char *options_descriptions[] = { + "-d [device] [attr] [value]\n" + "\t\t\t\t-c [device] [channel] [attr] [value]\n" + "\t\t\t\t-B [device] [attr] [value]\n" + "\t\t\t\t-D [device] [attr] [value]\n" + "\t\t\t\t-C [attr]", /* help */ "Show this help and quit.", "Ignore case distinctions.", @@ -312,34 +317,6 @@ static const char *options_descriptions[] = { "Read/Write debug attributes.", }; -static void usage(void) -{ - unsigned int i, j = 0, k; - - printf("Usage:\n\t" MY_NAME " [OPTION]...\t-d [device] [attr] [value]\n" - "\t\t\t\t-c [device] [channel] [attr] [value]\n" - "\t\t\t\t-B [device] [attr] [value]\n" - "\t\t\t\t-D [device] [attr] [value]\n" - "\t\t\t\t-C [attr]\nOptions:\n"); - for (i = 0; options[i].name; i++) { - k = strlen(options[i].name); - if (k > j) - j = k; - } - j++; - for (i = 0; options[i].name; i++) { - printf("\t-%c, --%s%*c: %s\n", - options[i].val, options[i].name, - j - (int)strlen(options[i].name), ' ', - options_descriptions[i]); - /* when printing out the help, add some subtitles, to help visually */ - if (i == 3) - printf("Optional qualifiers:\n"); - if (i == 8) - printf("Attribute types:\n"); - } -} - int main(int argc, char **argv) { struct iio_context *ctx; @@ -359,7 +336,7 @@ int main(int argc, char **argv) switch (c) { /* help */ case 'h': - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; /* context connection */ case 'a': @@ -429,7 +406,7 @@ int main(int argc, char **argv) if (!(search_device + search_channel + search_context + search_debug + search_buffer)) { if (argc == 1) { - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; } fprintf(stderr, "must specify one of -d, -c, -C, -B or -D.\n"); diff --git a/tests/iio_common.c b/tests/iio_common.c index 1db1956d6..c75b2b406 100644 --- a/tests/iio_common.c +++ b/tests/iio_common.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "iio_common.h" #include "gen_code.h" @@ -128,3 +129,19 @@ unsigned long int sanitize_clamp(const char *name, const char *argv, return val; } + +void usage(char *name, const struct option *options, + const char *options_descriptions[]) +{ + unsigned int i; + + printf("Usage:\n"); + printf("\t%s [OPTION]...\t%s\n", name, options_descriptions[0]); + printf("Options:\n"); + for (i = 0; options[i].name; i++) { + printf("\t-%c, --%s\n\t\t\t%s\n", + options[i].val, options[i].name, + options_descriptions[i + 1]); + } +} + diff --git a/tests/iio_common.h b/tests/iio_common.h index 8f03bdaab..404ecae25 100644 --- a/tests/iio_common.h +++ b/tests/iio_common.h @@ -22,6 +22,8 @@ #ifndef IIO_TESTS_COMMON_H #define IIO_TESTS_COMMON_H +#include + /* * internal buffers need to be big enough for attributes * coming back from the kernel. Because of virtual memory, @@ -42,5 +44,6 @@ void * xmalloc(size_t n, const char *name); struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name); unsigned long int sanitize_clamp(const char *name, const char *argv, uint64_t min, uint64_t max); +void usage(char *name, const struct option *options, const char *options_descriptions[]); #endif /* IIO_TESTS_COMMON_H */ diff --git a/tests/iio_genxml.c b/tests/iio_genxml.c index ed6d4ae6b..a35420495 100644 --- a/tests/iio_genxml.c +++ b/tests/iio_genxml.c @@ -41,25 +41,15 @@ static const struct option options[] = { }; static const char *options_descriptions[] = { + "\t[-x ]\n" + "\t\t\t\t[-u ]\n" + "\t\t\t\t[-n ]", "Show this help and quit.", "Use the XML backend with the provided XML file.", "Use the network backend with the provided hostname.", "Use the context with the provided URI.", }; -static void usage(void) -{ - unsigned int i; - - printf("Usage:\n\t" MY_NAME " [-x ]\n\t" - MY_NAME " [-u ]\n\t" - MY_NAME " [-n ]\n\nOptions:\n"); - for (i = 0; options[i].name; i++) - printf("\t-%c, --%s\n\t\t\t%s\n", - options[i].val, options[i].name, - options_descriptions[i]); -} - int main(int argc, char **argv) { char *xml; @@ -74,7 +64,7 @@ int main(int argc, char **argv) options, &option_index)) != -1) { switch (c) { case 'h': - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; case 'n': if (backend != IIO_LOCAL) { @@ -103,7 +93,7 @@ int main(int argc, char **argv) if (optind != argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; } diff --git a/tests/iio_info.c b/tests/iio_info.c index 8bdc489d4..f42f5f297 100644 --- a/tests/iio_info.c +++ b/tests/iio_info.c @@ -45,6 +45,9 @@ static const struct option options[] = { }; static const char *options_descriptions[] = { + "\t[-x ]\n" + "\t\t\t\t[-n ]\n" + "\t\t\t\t[-u ]", "Show this help and quit.", "Use the XML backend with the provided XML file.", "Use the network backend with the provided hostname.", @@ -53,19 +56,6 @@ static const char *options_descriptions[] = { "Scan for available contexts and if only one is available use it.", }; -static void usage(void) -{ - unsigned int i; - - printf("Usage:\n\t" MY_NAME " [-x ]\n\t" - MY_NAME " [-n ]\n\t" - MY_NAME " [-u ]\n\nOptions:\n"); - for (i = 0; options[i].name; i++) - printf("\t-%c, --%s\n\t\t\t%s\n", - options[i].val, options[i].name, - options_descriptions[i]); -} - static int dev_is_buffer_capable(const struct iio_device *dev) { unsigned int i; @@ -97,7 +87,7 @@ int main(int argc, char **argv) options, &option_index)) != -1) { switch (c) { case 'h': - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; case 'n': if (backend != IIO_LOCAL) { @@ -136,7 +126,7 @@ int main(int argc, char **argv) if (optind != argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; } diff --git a/tests/iio_readdev.c b/tests/iio_readdev.c index c8fdb52f4..f549de319 100644 --- a/tests/iio_readdev.c +++ b/tests/iio_readdev.c @@ -47,6 +47,8 @@ static const struct option options[] = { }; static const char *options_descriptions[] = { + "[-n ] [-t ] [-T ] [-b ]" + "[-s ] [ ...]", "Show this help and quit.", "Use the network backend with the provided hostname.", "Use the context with the provided URI.", @@ -57,19 +59,6 @@ static const char *options_descriptions[] = { "Scan for available contexts and if only one is available use it.", }; -static void usage(void) -{ - unsigned int i; - - printf("Usage:\n\t" MY_NAME " [-n ] [-t ] " - "[-T ] [-b ] [-s ] " - " [ ...]\n\nOptions:\n"); - for (i = 0; options[i].name; i++) - printf("\t-%c, --%s\n\t\t\t%s\n", - options[i].val, options[i].name, - options_descriptions[i]); -} - static struct iio_context *ctx; static struct iio_buffer *buffer; static const char *trigger_name = NULL; @@ -223,7 +212,7 @@ int main(int argc, char **argv) options, &option_index)) != -1) { switch (c) { case 'h': - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; case 'n': arg_ip = optarg; @@ -253,7 +242,7 @@ int main(int argc, char **argv) if (argc == optind) { fprintf(stderr, "Incorrect number of arguments.\n\n"); - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; } diff --git a/tests/iio_stresstest.c b/tests/iio_stresstest.c index 190c8b726..042f98795 100644 --- a/tests/iio_stresstest.c +++ b/tests/iio_stresstest.c @@ -109,6 +109,8 @@ static const struct option options[] = { }; static const char *options_descriptions[] = { + "[-n ] [-u :] [-t ] [-b ] [-s ]" + " [ ...]", "Show this help and quit.", "Use the context at the provided URI.", "Size of the capture buffer. Default is 256.", @@ -118,19 +120,6 @@ static const char *options_descriptions[] = { "Increase verbosity (-vv and -vvv for more)", }; -static void usage(void) -{ - unsigned int i; - - printf("Usage:\n\t" MY_NAME " [-n ] [-u :] " - "[-t ] [-b ] [-s ] " - " [ ...]\n\nOptions:\n"); - for (i = 0; options[i].name; i++) - printf("\t-%c, --%s\n\t\t\t%s\n", - options[i].val, options[i].name, - options_descriptions[i]); -} - static bool app_running = true; static bool threads_running = true; static int exit_code = EXIT_SUCCESS; @@ -422,7 +411,7 @@ int main(int argc, char **argv) options, &option_index)) != -1) { switch (c) { case 'h': - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; case 'u': info.arg_index += 2; @@ -490,7 +479,7 @@ int main(int argc, char **argv) } } fprintf(stderr, "\n"); - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; } @@ -498,7 +487,7 @@ int main(int argc, char **argv) struct iio_context *ctx = iio_create_context_from_uri(info.argv[info.uri_index]); if (!ctx) { fprintf(stderr, "need valid uri\n"); - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; } iio_context_destroy(ctx); @@ -511,7 +500,7 @@ int main(int argc, char **argv) } else { fprintf(stderr, "need valid uri\n"); - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; } diff --git a/tests/iio_writedev.c b/tests/iio_writedev.c index c3e85f122..5c8248ad9 100644 --- a/tests/iio_writedev.c +++ b/tests/iio_writedev.c @@ -57,6 +57,9 @@ static const struct option options[] = { }; static const char *options_descriptions[] = { + "[-n ] [-t ] " + "[-T ] [-b ] [-s ] " + " [ ...]", "Show this help and quit.", "Use the network backend with the provided hostname.", "Use the context with the provided URI.", @@ -68,19 +71,6 @@ static const char *options_descriptions[] = { "Use cyclic buffer mode.", }; -static void usage(void) -{ - unsigned int i; - - printf("Usage:\n\t" MY_NAME " [-n ] [-t ] " - "[-T ] [-b ] [-s ] " - " [ ...]\n\nOptions:\n"); - for (i = 0; options[i].name; i++) - printf("\t-%c, --%s\n\t\t\t%s\n", - options[i].val, options[i].name, - options_descriptions[i]); -} - static struct iio_context *ctx; static struct iio_buffer *buffer; static const char *trigger_name = NULL; @@ -233,7 +223,7 @@ int main(int argc, char **argv) options, &option_index)) != -1) { switch (c) { case 'h': - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_SUCCESS; case 'n': arg_ip = optarg; @@ -266,7 +256,7 @@ int main(int argc, char **argv) if (argc == optind) { fprintf(stderr, "Incorrect number of arguments.\n\n"); - usage(); + usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; }