]> git.draconx.ca Git - rarpd-dx.git/commitdiff
Adjust usage message and add explicit help option.
authorNick Bowler <nbowler@draconx.ca>
Thu, 27 Jul 2023 04:00:41 +0000 (00:00 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 27 Jul 2023 04:00:41 +0000 (00:00 -0400)
Instead of dumping a huge splat on usage errors, output a brief message
instead with an option to request the full details.  Since we currently
only do short options, this option is -H (same as my own programs).

When requested by the user, program help text is not an error message
so it goes on standard output (that way it can be grepped and all that
good stuff).

src/rarpd.c

index 76b92e5585ad326e65309caf1ad29c5933d2857d..b7aa2e1007e5236318cbd10fb69b753fc7bbc838 100644 (file)
@@ -48,8 +48,9 @@ int listen_arp;
 char *ifname;
 char *tftp_dir = "/etc/tftpboot";
 
+static const char *progname = "rarpd";
+
 extern int ether_ntohost(char *name, unsigned char *ea);
-void usage(void) __attribute__((noreturn));
 
 struct iflink
 {
@@ -80,22 +81,30 @@ struct rarp_map
        uint32_t                ipaddr;
 } *rarp_db;
 
-void usage(void)
+static void print_usage(FILE *f)
 {
-       fprintf(stderr,
-               "\nUsage:\n"
-               "  rarpd [options] [interface]\n"
-               "\nOptions:\n"
-               "  -A        listen also arp messages\n"
-               "  -a        listen on all the interfaces\n"
-               "  -b <dir>  tftpd boot directory\n"
-               "  -d        debug mode\n"
-               "  -e        /etc/ethers markup alone is fine\n"
-               "  -v        verbose mode\n"
-               "  -V        print version and exit\n"
-               "\nFor more details see rarpd(8).\n"
-       );
-       exit(1);
+       fprintf(f, "Usage: %s [options] [interface]\n", progname);
+       if (f != stdout)
+               fprintf(f, "Try %s -H for more information.\n", progname);
+}
+
+static void print_help(void)
+{
+       print_usage(stdout);
+       putchar('\n');
+
+       puts("Options:");
+       puts("  -A        listen also arp messages");
+       puts("  -a        listen on all the interfaces");
+       puts("  -b <dir>  tftpd boot directory");
+       puts("  -d        debug mode");
+       puts("  -e        /etc/ethers markup alone is fine");
+       puts("  -v        verbose mode");
+       puts("  -V        print version and then exit");
+       puts("  -H        print this message and then exit");
+       putchar('\n');
+
+       puts("For more information, see the rarpd(8) man page.");
 }
 
 static void print_version(void)
@@ -588,8 +597,11 @@ int main(int argc, char **argv)
        int psize;
        int opt;
 
+       if (argc > 0)
+               progname = argv[0];
+
        opterr = 0;
-       while ((opt = getopt(argc, argv, "aAb:dvoeV")) != EOF) {
+       while ((opt = getopt(argc, argv, "aAb:dvoeVH")) != EOF) {
                switch (opt) {
                case 'a':
                        ++all_ifaces;
@@ -621,13 +633,19 @@ int main(int argc, char **argv)
                case 'V':
                        print_version();
                        return 0;
+               case 'H':
+                       print_help();
+                       return 0;
                default:
-                       usage();
+                       print_usage(stderr);
+                       return EXIT_FAILURE;
                }
        }
        if (argc > optind) {
-               if (argc > optind+1)
-                       usage();
+               if (argc > optind+1) {
+                       print_usage(stderr);
+                       return EXIT_FAILURE;
+               }
                ifname = argv[optind];
        }