From d184d41670728bc6516e8089c100ebe9c4f9cdca Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Fri, 6 Jan 2017 15:13:07 +0100 Subject: [PATCH] tools-common: new source file Introduce a .c file containing code that is common across all GPIO tools but not really related to the libgpiod functionality. This file is statically linked with every executable. While we're at it - make gpiodetect use it right away. Signed-off-by: Bartosz Golaszewski --- Makefile.am | 12 +++++------ gpiodetect.c | 40 +++++++++++++++++++++++++++---------- tools-common.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools-common.h | 26 ++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 tools-common.c create mode 100644 tools-common.h diff --git a/Makefile.am b/Makefile.am index 6dc31d8..e4e1576 100644 --- a/Makefile.am +++ b/Makefile.am @@ -21,27 +21,27 @@ libgpiod_la_SOURCES = core.c bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon gpiofind -gpiodetect_SOURCES = gpiodetect.c +gpiodetect_SOURCES = gpiodetect.c tools-common.c gpiodetect_LDFLAGS = -lgpiod gpiodetect_DEPENDENCIES = libgpiod.la -gpioinfo_SOURCES = gpioinfo.c +gpioinfo_SOURCES = gpioinfo.c tools-common.c gpioinfo_LDFLAGS = -lgpiod gpioinfo_DEPENDENCIES = libgpiod.la -gpioget_SOURCES = gpioget.c +gpioget_SOURCES = gpioget.c tools-common.c gpioget_LDFLAGS = -lgpiod gpioget_DEPENDENCIES = libgpiod.la -gpioset_SOURCES = gpioset.c +gpioset_SOURCES = gpioset.c tools-common.c gpioset_LDFLAGS = -lgpiod gpioset_DEPENDENCIES = libgpiod.la -gpiomon_SOURCES = gpiomon.c +gpiomon_SOURCES = gpiomon.c tools-common.c gpiomon_LDFLAGS = -lgpiod gpiomon_DEPENDENCIES = libgpiod.la -gpiofind_SOURCES = gpiofind.c +gpiofind_SOURCES = gpiofind.c tools-common.c gpiofind_LDFLAGS = -lgpiod gpiofind_DEPENDENCIES = libgpiod.la diff --git a/gpiodetect.c b/gpiodetect.c index 9678c13..c5a7d6d 100644 --- a/gpiodetect.c +++ b/gpiodetect.c @@ -9,29 +9,49 @@ */ #include +#include "tools-common.h" #include #include +#include + +static const struct option longopts[] = { + { "help", no_argument, NULL, 'h' }, + { 0 }, +}; + +static const char *const shortopts = "+h"; int main(int argc, char **argv) { struct gpiod_chip_iter *iter; struct gpiod_chip *chip; + int optc, opti; - if (argc != 1) { - printf("Usage: %s\n", argv[0]); - printf("List all GPIO chips\n"); + set_progname(argv[0]); - return EXIT_FAILURE; + for (;;) { + optc = getopt_long(argc, argv, shortopts, longopts, &opti); + if (optc < 0) + break; + + switch (optc) { + case 'h': + printf("Usage: %s\n", get_progname()); + printf("List all GPIO chips\n"); + exit(EXIT_SUCCESS); + } } - iter = gpiod_chip_iter_new(); - if (!iter) { - fprintf(stderr, "%s: unable to access gpio chips: %s\n", - argv[0], gpiod_strerror(gpiod_errno())); + argc -= optind; + argv += optind; - return EXIT_FAILURE; - } + if (argc > 0) + die("unrecognized argument: %s", argv[0]); + + iter = gpiod_chip_iter_new(); + if (!iter) + die_perror("unable to access GPIO chips"); gpiod_foreach_chip(iter, chip) { printf("%s [%s] (%u lines)\n", diff --git a/tools-common.c b/tools-common.c new file mode 100644 index 0000000..43b9242 --- /dev/null +++ b/tools-common.c @@ -0,0 +1,54 @@ +/* + * Common code for GPIO tools. + * + * Copyright (C) 2017 Bartosz Golaszewski + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + */ + +#include +#include "tools-common.h" + +#include +#include +#include + +static char *progname = "unknown"; + +void set_progname(char *name) +{ + progname = name; +} + +const char * get_progname(void) +{ + return progname; +} + +void die(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + fprintf(stderr, "%s: ", progname); + vfprintf(stderr, fmt, va); + fprintf(stderr, "\n"); + va_end(va); + + exit(EXIT_FAILURE); +} + +void die_perror(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + fprintf(stderr, "%s: ", progname); + vfprintf(stderr, fmt, va); + fprintf(stderr, ": %s\n", gpiod_strerror(gpiod_errno())); + va_end(va); + + exit(EXIT_FAILURE); +} diff --git a/tools-common.h b/tools-common.h new file mode 100644 index 0000000..1cfef33 --- /dev/null +++ b/tools-common.h @@ -0,0 +1,26 @@ +/* + * Common code for GPIO tools. + * + * Copyright (C) 2017 Bartosz Golaszewski + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + */ + +#ifndef __GPIOD_TOOLS_COMMON_H__ +#define __GPIOD_TOOLS_COMMON_H__ + +/* + * Various helpers for the GPIO tools. + * + * NOTE: This is not a stable interface - it's only to avoid duplicating + * common code. + */ + +void set_progname(char *name); +const char * get_progname(void); +void die(const char *fmt, ...); +void die_perror(const char *fmt, ...); + +#endif /* __GPIOD_TOOLS_COMMON_H__ */ -- 2.30.2