tools-common: new source file
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 6 Jan 2017 14:13:07 +0000 (15:13 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 6 Jan 2017 14:14:36 +0000 (15:14 +0100)
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 <bartekgola@gmail.com>
Makefile.am
gpiodetect.c
tools-common.c [new file with mode: 0644]
tools-common.h [new file with mode: 0644]

index 6dc31d8df348ec128167ef46580fccd192801b18..e4e157676aad963b4167442f55da9c5785890031 100644 (file)
@@ -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
 
index 9678c13e1e200ecfacf4f056de64e521b20b77eb..c5a7d6d9fa76b89a88c3fd91ef395baf4e5bb95c 100644 (file)
@@ -9,29 +9,49 @@
  */
 
 #include <gpiod.h>
+#include "tools-common.h"
 
 #include <stdio.h>
 #include <string.h>
+#include <getopt.h>
+
+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 (file)
index 0000000..43b9242
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Common code for GPIO tools.
+ *
+ * Copyright (C) 2017 Bartosz Golaszewski <bartekgola@gmail.com>
+ *
+ * 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 <gpiod.h>
+#include "tools-common.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+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 (file)
index 0000000..1cfef33
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Common code for GPIO tools.
+ *
+ * Copyright (C) 2017 Bartosz Golaszewski <bartekgola@gmail.com>
+ *
+ * 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__ */