chiark / gitweb /
Add a usage message and trap various usage errors
authorBen Harris <bjh21@bjh21.me.uk>
Sun, 31 Jan 2021 11:34:16 +0000 (11:34 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sun, 31 Jan 2021 11:34:16 +0000 (11:34 +0000)
clunk.c

diff --git a/clunk.c b/clunk.c
index 1616dbd8e2a099ec41f150c15d2f36c020d9afe3..1bd3ecdfaabafd37085ff9738e0b82e2e6cfe69b 100644 (file)
--- 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. */