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
*/
#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",
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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__ */