From ff7c3f6901655416389aaa995bbc40c4042465d6 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 28 Dec 2015 19:06:34 +0100 Subject: [PATCH] Add the list command. --- examples/Makefile.am | 1 + examples/dctool.c | 1 + examples/dctool.h | 1 + examples/dctool_list.c | 103 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 examples/dctool_list.c diff --git a/examples/Makefile.am b/examples/Makefile.am index 186ec97..0356d90 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -11,5 +11,6 @@ dctool_SOURCES = \ dctool.c \ dctool_help.c \ dctool_version.c \ + dctool_list.c \ utils.h \ utils.c diff --git a/examples/dctool.c b/examples/dctool.c index da36120..7cf6728 100644 --- a/examples/dctool.c +++ b/examples/dctool.c @@ -50,6 +50,7 @@ static const dctool_command_t *g_commands[] = { &dctool_help, &dctool_version, + &dctool_list, NULL }; diff --git a/examples/dctool.h b/examples/dctool.h index 3b422bd..c1239a0 100644 --- a/examples/dctool.h +++ b/examples/dctool.h @@ -44,6 +44,7 @@ typedef struct dctool_command_t { extern const dctool_command_t dctool_help; extern const dctool_command_t dctool_version; +extern const dctool_command_t dctool_list; const dctool_command_t * dctool_command_find (const char *name); diff --git a/examples/dctool_list.c b/examples/dctool_list.c new file mode 100644 index 0000000..89e5386 --- /dev/null +++ b/examples/dctool_list.c @@ -0,0 +1,103 @@ +/* + * libdivecomputer + * + * Copyright (C) 2015 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#ifdef HAVE_GETOPT_H +#include +#endif + +#include +#include +#include + +#include "dctool.h" + +static int +dctool_list_run (int argc, char *argv[], dc_context_t *context, dc_descriptor_t *dummy) +{ + // Default option values. + unsigned int help = 0; + + // Parse the command-line options. + int opt = 0; + const char *optstring = "h"; +#ifdef HAVE_GETOPT_LONG + struct option options[] = { + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0 } + }; + while ((opt = getopt_long (argc, argv, optstring, options, NULL)) != -1) { +#else + while ((opt = getopt (argc, argv, optstring)) != -1) { +#endif + switch (opt) { + case 'h': + help = 1; + break; + default: + return EXIT_FAILURE; + } + } + + argc -= optind; + argv += optind; + + // Show help message. + if (help) { + dctool_command_showhelp (&dctool_list); + return EXIT_SUCCESS; + } + + dc_iterator_t *iterator = NULL; + dc_descriptor_t *descriptor = NULL; + dc_descriptor_iterator (&iterator); + while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) { + printf ("%s %s\n", + dc_descriptor_get_vendor (descriptor), + dc_descriptor_get_product (descriptor)); + dc_descriptor_free (descriptor); + } + dc_iterator_free (iterator); + + return EXIT_SUCCESS; +} + +const dctool_command_t dctool_list = { + dctool_list_run, + DCTOOL_CONFIG_NONE, + "list", + "List supported devices", + "Usage:\n" + " dctool list [options]\n" + "\n" + "Options:\n" +#ifdef HAVE_GETOPT_LONG + " -h, --help Show help message\n" +#else + " -h Show help message\n" +#endif +};