]> git.draconx.ca Git - cdecl99.git/commitdiff
cdecl99: Use packed option format from gen-options.awk.
authorNick Bowler <nbowler@draconx.ca>
Mon, 29 May 2023 02:05:17 +0000 (22:05 -0400)
committerNick Bowler <nbowler@draconx.ca>
Mon, 29 May 2023 02:18:34 +0000 (22:18 -0400)
This feature uses a compact, fully constant array to generate the
real struct option array at runtime.  This allows the full-sized array
to be dropped after command-line processing is finished, and with
position-independent executables, reduces the amount of relocation
processing needed.

src/cdecl99.c

index 1da195ea0838d0d53ffbbf9fdc3d88a37cb103ca..eac0023731ce9838342dd286032ad876c36463b4 100644 (file)
 #include "cdecl99.h"
 #include "cdecl.h"
 #include "help.h"
+#include "xtra.h"
 #include "copysym.h"
+#include "options.h"
 
 static const char *progname = "cdecl99";
 static bool interactive = true;
 
-#include "options.h"
-static const char sopts[] = SOPT_STRING;
-static const struct option lopts[] = {
-       LOPTS_INITIALIZER,
-       {0}
-};
-
 void print_error(const char *fmt, ...)
 {
        va_list(ap);
@@ -82,7 +77,7 @@ static void print_usage(FILE *f)
                           progname);
 }
 
-static void print_help(void)
+static void print_help(const struct option *lopts)
 {
        const struct option *opt;
 
@@ -190,22 +185,27 @@ static void init_i18n(void)
        textdomain(PACKAGE);
 }
 
-int main(int argc, char **argv)
+enum {
+       INIT_EXIT_SUCCESS = -1,
+       INIT_EXIT_FAILURE = -2
+};
+
+static int initialize(int argc, char **argv)
 {
-       bool show_intro = true;
+       int i, opt, quiet = 0, execute = 0;
        const char *filename = NULL;
-       unsigned execute = 0;
-       int i, opt, rc;
+
+       XTRA_PACKED_LOPTS(lopts);
 
        if (argc > 0)
                progname = argv[0];
 
        init_i18n();
 
-       while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
+       while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
                switch (opt) {
                case 'q':
-                       show_intro = false;
+                       quiet = 1;
                        break;
                case 'b':
                        interactive = false;
@@ -221,13 +221,13 @@ int main(int argc, char **argv)
                        break;
                case 'V':
                        print_version();
-                       return EXIT_SUCCESS;
+                       return INIT_EXIT_SUCCESS;
                case 'H':
-                       print_help();
-                       return EXIT_SUCCESS;
+                       print_help(lopts);
+                       return INIT_EXIT_SUCCESS;
                default:
                        print_usage(stderr);
-                       return EXIT_FAILURE;
+                       return INIT_EXIT_FAILURE;
                }
        }
 
@@ -239,7 +239,7 @@ int main(int argc, char **argv)
                }
                fprintf(stderr, "\n");
                print_usage(stderr);
-               return EXIT_FAILURE;
+               return INIT_EXIT_FAILURE;
        }
 
        /* --filename and --execute imply --batch. */
@@ -247,7 +247,7 @@ int main(int argc, char **argv)
                interactive = false;
 
        /* --batch implies --quiet */
-       if (interactive && show_intro)
+       if (interactive && !quiet)
                print_version();
 
        /* --execute supersedes --filename */
@@ -255,10 +255,22 @@ int main(int argc, char **argv)
                if (!freopen(filename, "r", stdin)) {
                        print_error("failed to open %s: %s", filename,
                                                        strerror(errno));
-                       return EXIT_FAILURE;
+                       return INIT_EXIT_FAILURE;
                }
        }
 
+       return execute;
+}
+
+int main(int argc, char **argv)
+{
+       int rc, execute;
+
+       switch ((execute = initialize(argc, argv))) {
+       case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
+       case INIT_EXIT_FAILURE: return EXIT_FAILURE;
+       }
+
        if (interactive)
                rc = repl();
        else if (execute)