From 404b1297d3704cf01454976c6407a1a4ce57aa95 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 24 May 2025 11:19:27 +0900 Subject: [PATCH] refactored the main function --- Makefile.in | 4 ++-- bin/hawk.c | 18 ++--------------- bin/main.c | 58 ++++++++++++++++++++++++++++++++++++++++++++--------- bin/sed.c | 17 ++-------------- 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Makefile.in b/Makefile.in index 5f8f1843..c6de6bec 100644 --- a/Makefile.in +++ b/Makefile.in @@ -170,8 +170,8 @@ am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/ac/ar-lib \ $(top_srcdir)/ac/ltmain.sh $(top_srcdir)/ac/missing \ $(top_srcdir)/ac/tap-driver.sh $(top_srcdir)/ac/test-driver \ $(top_srcdir)/pkgs/hawk.spec.in README.md ac/ar-lib ac/compile \ - ac/config.guess ac/config.sub ac/install-sh ac/ltmain.sh \ - ac/missing + ac/config.guess ac/config.sub ac/depcomp ac/install-sh \ + ac/ltmain.sh ac/missing DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) distdir = $(PACKAGE)-$(VERSION) top_distdir = $(distdir) diff --git a/bin/hawk.c b/bin/hawk.c index 4bc79cb1..3aa24f1e 100644 --- a/bin/hawk.c +++ b/bin/hawk.c @@ -478,12 +478,6 @@ static void on_statement (hawk_rtx_t* rtx, hawk_nde_t* nde) } #endif -static void print_version (const hawk_bch_t* argv0) -{ - printf ("%s %s\n", hawk_get_base_name_bcstr(argv0), HAWK_PACKAGE_VERSION); - printf ("Copyright 2006-2022 Chung, Hyung-Hwan\n"); -} - static void print_error (const hawk_bch_t* fmt, ...) { va_list va; @@ -538,9 +532,8 @@ static void print_usage (FILE* out, const hawk_bch_t* argv0, const hawk_bch_t* r fprintf (out, "USAGE: %s%s%s [options] -f sourcefile [ -- ] [datafile]*\n", b1, b2, b3); fprintf (out, " %s%s%s [options] [ -- ] sourcestring [datafile]*\n", b1, b2, b3); - fprintf (out, "Where options are:\n"); + fprintf (out, "Options as follows:\n"); fprintf (out, " -h/--help print this message\n"); - fprintf (out, " --version print version\n"); fprintf (out, " -D show extra information\n"); fprintf (out, " -c/--call name call a function instead of entering\n"); fprintf (out, " the pattern-action loop. [datafile]* is\n"); @@ -691,7 +684,6 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar { "modern", '\0' }, { "classic", '\0' }, - { "version", '\0' }, { "help", 'h' }, { HAWK_NULL, '\0' } }; @@ -865,13 +857,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar /* a long option with no corresponding short option */ hawk_oow_t i; - if (hawk_comp_bcstr(opt.lngopt, "version", 0) == 0) - { - print_version (real_argv0? real_argv0: argv[0]); - oops_ret = 2; - goto oops; - } - else if (hawk_comp_bcstr(opt.lngopt, "script-encoding", 0) == 0) + if (hawk_comp_bcstr(opt.lngopt, "script-encoding", 0) == 0) { arg->script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); if (arg->script_cmgr == HAWK_NULL) diff --git a/bin/main.c b/bin/main.c index b2beffc4..66229f3f 100644 --- a/bin/main.c +++ b/bin/main.c @@ -25,19 +25,59 @@ */ #include "main.h" +#include +#include + +static int main_version(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) +{ + printf ("%s %s\n", hawk_get_base_name_bcstr(real_argv0), HAWK_PACKAGE_VERSION); + printf ("Copyright 2006-2022 Chung, Hyung-Hwan\n"); + return 0; +} + +static void print_usage(FILE* out, const hawk_bch_t* real_argv0) +{ + const hawk_bch_t* b1 = hawk_get_base_name_bcstr(real_argv0); + + fprintf (out, "USAGE: %s [options] [mode specific options and parameters]\n", b1); + fprintf (out, "Options as follows:\n"); + fprintf (out, " --usage print this message\n"); + fprintf (out, " --version print version\n"); + fprintf (out, " --awk/--hawk switch to the awk mode(default)\n"); + fprintf (out, " --sed switch to the sed mode\n"); +} + +static int main_usage(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) +{ + print_usage(stdout, real_argv0); + return 0; +} + +static struct { + const hawk_bch_t* name; + int (*main) (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0); +} entry_funcs[] = { + { "awk", main_hawk }, + { "hawk", main_hawk }, + { "sed", main_sed }, + { "usage", main_usage }, + { "version", main_version } +}; int main(int argc, hawk_bch_t* argv[]) { - if (argc >= 2 && hawk_comp_bcstr(argv[1], "--sed", 0) == 0) + if (argc >= 2) { - /* hawk --sed ... */ - return main_sed(argc - 1, &argv[1], argv[0]); - } - else if (argc >= 2 && hawk_comp_bcstr(argv[1], "--awk", 0) == 0) - { - /* hawk --awk ... */ - /* in this mode, the value ARGV[0] inside a hawk script is "--awk" */ - return main_hawk(argc - 1, &argv[1], argv[0]); + hawk_oow_t i; + const hawk_bch_t* first_opt = argv[1]; + for (i = 0; i < HAWK_COUNTOF(entry_funcs); i++) { + if (first_opt[0] == '-' && first_opt[1] == '-' && hawk_comp_bcstr(&first_opt[2], entry_funcs[i].name, 0) == 0) { + /* [NOTE] + * if hawk is invoked via 'hawk --awk' or 'hawk --hawk', + * the value ARGV[0] inside a hawk script is "--awk" or "--hawk" */ + return entry_funcs[i].main(argc -1, &argv[1], argv[0]); + } + } } return main_hawk(argc, argv, HAWK_NULL); diff --git a/bin/sed.c b/bin/sed.c index 230efe87..e2e07be4 100644 --- a/bin/sed.c +++ b/bin/sed.c @@ -172,12 +172,6 @@ static hawk_mmgr_t debug_mmgr = /* ------------------------------------------------------------------- */ -static void print_version (const hawk_bch_t* argv0) -{ - fprintf (stdout, "%s %s\n", hawk_get_base_name_bcstr(argv0), HAWK_PACKAGE_VERSION); - fprintf (stdout, "Copyright 2006-2022 Chung, Hyung-Hwan\n"); -} - static void print_error (const hawk_bch_t* fmt, ...) { va_list va; @@ -206,9 +200,8 @@ static void print_usage (FILE* out, const hawk_bch_t* argv0, const hawk_bch_t* r fprintf (out, " %s%s%s [options] -f script-file [file]\n", b1, b2, b3); fprintf (out, " %s%s%s [options] -e script [file]\n", b1, b2, b3); - fprintf (out, "options as follows:\n"); + fprintf (out, "Options as follows:\n"); fprintf (out, " -h/--help show this message\n"); - fprintf (out, " --version show version\n"); fprintf (out, " -n disable auto-print\n"); fprintf (out, " -e script specify a script\n"); fprintf (out, " -f file specify a script file\n"); @@ -296,7 +289,6 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg { ":outfile-encoding", '\0' }, #endif - { "version", '\0' }, { "help", 'h' }, { HAWK_NULL, '\0' } }; @@ -406,12 +398,7 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg case '\0': { - if (hawk_comp_bcstr(opt.lngopt, "version", 0) == 0) - { - print_version (real_argv0? real_argv0: argv[0]); - goto done; - } - else if (hawk_comp_bcstr(opt.lngopt, "script-encoding", 0) == 0) + if (hawk_comp_bcstr(opt.lngopt, "script-encoding", 0) == 0) { g_script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); if (g_script_cmgr == HAWK_NULL)