From aa148554fced7bba734e8c1ab9ac616a69955ca3 Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Thu, 9 Feb 2017 18:05:12 +0100 Subject: [PATCH] Added 'list-devices' menu. --- src/p7/args.c | 10 ++++++++++ src/p7/list_devices.c | 44 +++++++++++++++++++++++++++++++++++++++++++ src/p7/main.h | 3 +++ 3 files changed, 57 insertions(+) create mode 100644 src/p7/list_devices.c diff --git a/src/p7/args.c b/src/p7/args.c index f9fc181..5905852 100644 --- a/src/p7/args.c +++ b/src/p7/args.c @@ -136,6 +136,12 @@ static const char help_info[] = "Dump information about the calculator.\n" FOOT; +/* List serial devices */ +static const char help_listcom[] = +"Usage: " QUOTE(BIN) " list-devices\n" +"List serial devices.\n" +FOOT; + /* ************************************************************************** */ /* Main function */ /* ************************************************************************** */ @@ -255,6 +261,10 @@ int parse_args(int ac, char **av, args_t *args) } else if (!strcmp(aav[0], "version")) { puts(version_message); return (0); + } else if (!strcmp(aav[0], "list-devices")) { + if (help || aac > 1) puts(help_listcom); + else list_devices(); + return (0); } else if (!strcmp(aav[0], "info")) { sub_init(info, 0) } else if (!strcmp(aav[0], "list") || !strcmp(aav[0], "ls")) { diff --git a/src/p7/list_devices.c b/src/p7/list_devices.c new file mode 100644 index 0000000..57f977f --- /dev/null +++ b/src/p7/list_devices.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* _____ _ */ +/* p7/list_devices.c |_ _|__ _ _| |__ ___ _ _ */ +/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */ +/* | | (_) | |_| | | | | __/ |_| | */ +/* By: thomas |_|\___/ \__,_|_| |_|\___|\__, |.fr */ +/* Last updated: 2017/02/09 17:55:40 |___/ */ +/* */ +/* ************************************************************************** */ +#include "main.h" + +/** + * list_a_device: + * List one serial device. + * + * @arg cookie the cookie (unused) + * @arg path the path. + */ + +static int initialized = 0; +static void list_a_device(void *cookie, const char *path) +{ + (void)cookie; + if (!initialized) { + printf("Available devices:\n"); + initialized = 1; + } + printf("- %s\n", path); +} + +/** + * list_devices: + * List serial devices. + * + * @return the program return code. + */ + +int list_devices(void) +{ + p7_comlist(list_a_device, NULL); + if (!initialized) + fprintf(stderr, "Could not find any devices.\n"); + return (0); +} diff --git a/src/p7/main.h b/src/p7/main.h index 6ad30c5..f4470d9 100644 --- a/src/p7/main.h +++ b/src/p7/main.h @@ -42,6 +42,9 @@ typedef struct { /* Parsing function */ int parse_args(int ac, char **av, args_t *args); +/* List devices function */ +int list_devices(void); + /* Dumping function */ int dump(p7_handle_t *handle);