From 8121cd0fb48f6066b016a9e2d05f349f6ff3a669 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sun, 28 May 2023 22:05:17 -0400 Subject: [PATCH] cdecl99: Use packed option format from gen-options.awk. 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 | 54 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/cdecl99.c b/src/cdecl99.c index 1da195e..eac0023 100644 --- a/src/cdecl99.c +++ b/src/cdecl99.c @@ -36,18 +36,13 @@ #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) -- 2.43.2