From: Ben Harris Date: Sun, 31 Jan 2021 11:34:16 +0000 (+0000) Subject: Add a usage message and trap various usage errors X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=6d3d8c42022bede059e0e031f97cdee02070a141;p=clunk.git Add a usage message and trap various usage errors --- diff --git a/clunk.c b/clunk.c index 1616dbd..1bd3ecd 100644 --- a/clunk.c +++ b/clunk.c @@ -208,7 +208,8 @@ init_statefile(char const *statefilename) #ifdef WITH_LIBGPIOD static void -init_libgpiod(char const *chip_name, char const *line_name, int line_num) +init_libgpiod(char const *chip_name, char const *line_name, + unsigned int line_num) { struct gpiod_chip * chip; @@ -247,6 +248,14 @@ init_sysfs(char const *path) outfn = &sysfs_out; } +static void +usage() +{ + + fprintf(stderr, "usage!\n"); + exit(2); +} + static void init(int argc, char **argv) { @@ -258,7 +267,9 @@ init(int argc, char **argv) char *statefile = NULL, *statestr = NULL; #ifdef WITH_LIBGPIOD char *gpiod_chip = NULL, *gpiod_line_name = NULL; - int gpiod_line_num = -1; + unsigned int gpiod_line_num; + char *endptr; + bool have_gpiod_line_num = false; #endif int opt; @@ -290,18 +301,35 @@ init(int argc, char **argv) break; #ifdef WITH_LIBGPIOD case 'G': + if (gpiod_line_name != NULL) + usage(); gpiod_line_name = optarg; break; case 'C': + if (gpiod_chip != NULL) + usage(); gpiod_chip = optarg; break; case 'L': - gpiod_line_num = atoi(optarg); + if (have_gpiod_line_num) + usage(); + errno = 0; + gpiod_line_num = strtoul(optarg, &endptr, 0); + if (errno != 0) { + warn(NULL); + usage(); + } + if (*endptr != '\0' || *optarg == '\0') + usage(); + have_gpiod_line_num = true; break; #endif /* WITH_LIBGPIOD */ case 'Y': init_sysfs(optarg); break; + case '?': + usage(); + break; } } /* Default startup state is to assume the dial is correct. */