X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/df005d9975b81f8e310761ebed0afbabd40acfe3..c807d652225fe170ee442591bb2a0bfc91c8618c:/src/cdecl99.c diff --git a/src/cdecl99.c b/src/cdecl99.c index 1da195e..5ddc889 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,8 +77,9 @@ static void print_usage(FILE *f) progname); } -static void print_help(void) +static void print_help(const struct option *lopts) { + struct lopt_help help = {0}; const struct option *opt; print_usage(stdout); @@ -94,8 +90,6 @@ static void print_help(void) puts(_("Options:")); for (opt = lopts; opt->name; opt++) { - struct lopt_help help; - if (!lopt_get_help(opt, &help)) continue; @@ -190,22 +184,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 +220,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 +238,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 +246,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 +254,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)