diff --git a/ase/cmd/awk/awk.c b/ase/cmd/awk/awk.c index 5f1636ab..652a456a 100644 --- a/ase/cmd/awk/awk.c +++ b/ase/cmd/awk/awk.c @@ -1035,28 +1035,28 @@ static int handle_args (int argc, ase_char_t* argv[], struct awk_src_io* src_io) ase_cint_t c; static ase_opt_lng_t lng[] = { - { ASE_T("implicit"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("explicit"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("bxor"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("shift"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("idiv"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("extio"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("newline"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("baseone"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("stripspaces"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("nextofile"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("crlf"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("argstomain"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("reset"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("maptovar"), ASE_OPT_ARG_OPTIONAL, 0 }, - { ASE_T("pablock"), ASE_OPT_ARG_OPTIONAL, 0 }, + { ASE_T("implicit"), 0 }, + { ASE_T("explicit"), 0 }, + { ASE_T("bxor"), 0 }, + { ASE_T("shift"), 0 }, + { ASE_T("idiv"), 0 }, + { ASE_T("extio"), 0 }, + { ASE_T("newline"), 0 }, + { ASE_T("baseone"), 0 }, + { ASE_T("stripspaces"), 0 }, + { ASE_T("nextofile"), 0 }, + { ASE_T("crlf"), 0 }, + { ASE_T("argstomain"), 0 }, + { ASE_T("reset"), 0 }, + { ASE_T("maptovar"), 0 }, + { ASE_T("pablock"), 0 }, - { ASE_T("main"), ASE_OPT_ARG_REQUIRED, ASE_T('m') }, - { ASE_T("file"), ASE_OPT_ARG_REQUIRED, ASE_T('f') }, - { ASE_T("field-separator"), ASE_OPT_ARG_REQUIRED, ASE_T('F') }, - { ASE_T("assign"), ASE_OPT_ARG_REQUIRED, ASE_T('v') }, + { ASE_T(":main"), ASE_T('m') }, + { ASE_T(":file"), ASE_T('f') }, + { ASE_T(":field-separator"), ASE_T('F') }, + { ASE_T(":assign"), ASE_T('v') }, - { ASE_T("help"), ASE_OPT_ARG_NONE, ASE_T('h')} + { ASE_T("help"), ASE_T('h') } }; diff --git a/ase/include/ase/utl/getopt.h b/ase/include/ase/utl/getopt.h index 5c2ba13f..5bc6bc9e 100644 --- a/ase/include/ase/utl/getopt.h +++ b/ase/include/ase/utl/getopt.h @@ -1,5 +1,5 @@ /* - * $Id: getopt.h 289 2008-07-26 15:37:38Z baconevi $ + * $Id: getopt.h 290 2008-07-27 06:16:54Z baconevi $ * * {License} */ @@ -10,21 +10,13 @@ #include #include -enum -{ - ASE_OPT_ARG_NONE = 0, - ASE_OPT_ARG_REQUIRED = 1, - ASE_OPT_ARG_OPTIONAL = 2 -}; - typedef struct ase_opt_t ase_opt_t; typedef struct ase_opt_lng_t ase_opt_lng_t; struct ase_opt_lng_t { const ase_char_t* str; - int has_arg; - int val; + ase_cint_t val; }; struct ase_opt_t @@ -38,7 +30,7 @@ struct ase_opt_t ase_char_t* arg; /* argument associated with an option */ /* output */ - ase_char_t* lngopt; + const ase_char_t* lngopt; /* input + output */ int ind; /* index into parent argv vector */ @@ -51,7 +43,19 @@ struct ase_opt_t extern "C" { #endif -ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt); +/* + * NAME Parse command line options + * + * DESCRIPTION + * TODO: + * + * RETURNS ASE_CHAR_EOF, XXXX.XXXX + */ +ase_cint_t ase_getopt ( + int argc /* argument count */, + ase_char_t* const* argv /* argument array */, + ase_opt_t* opt /* option configuration */ +); #ifdef __cplusplus } diff --git a/ase/lib/utl/getopt.c b/ase/lib/utl/getopt.c index 9b69dbc0..08c8684e 100644 --- a/ase/lib/utl/getopt.c +++ b/ase/lib/utl/getopt.c @@ -1,5 +1,5 @@ /* - * $Id: getopt.c 289 2008-07-26 15:37:38Z baconevi $ + * $Id: getopt.c 290 2008-07-27 06:16:54Z baconevi $ * * {License} */ @@ -49,88 +49,12 @@ #define BADARG ASE_T(':') #define EMSG ASE_T("") -#if 0 -ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt) -{ - ase_char_t* oli; /* option letter list index */ - - if (opt->cur == ASE_NULL) - { - opt->cur = EMSG; - opt->ind = 1; - } - - if (*opt->cur == ASE_T('\0')) - { - /* update scanning pointer */ - if (opt->ind >= argc || *(opt->cur = argv[opt->ind]) != ASE_T('-')) - { - opt->cur = EMSG; - return ASE_CHAR_EOF; - } - - if (opt->cur[1] != ASE_T('\0') && *++opt->cur == ASE_T('-')) - { - /* found "--" */ - ++opt->ind; - opt->cur = EMSG; - return ASE_CHAR_EOF; - } - } /* option letter okay? */ - - if ((opt->opt = *opt->cur++) == ASE_T(':') || - (oli = ase_strchr(opt->str, opt->opt)) == ASE_NULL) - { - /* - * if the user didn't specify '-' as an option, - * assume it means EOF. - */ - if (opt->opt == (int)'-') return ASE_CHAR_EOF; - if (*opt->cur == ASE_T('\0')) ++opt->ind; - return BADCH; - } - - if (*++oli != ASE_T(':')) - { - /* don't need argument */ - opt->arg = ASE_NULL; - if (*opt->cur == ASE_T('\0')) ++opt->ind; - } - else - { - /* need an argument */ - - if (*opt->cur != ASE_T('\0')) - { - /* no white space */ - opt->arg = opt->cur; - } - else if (argc <= ++opt->ind) - { - /* no arg */ - opt->cur = EMSG; - /*if (*opt->str == ASE_T(':'))*/ return BADARG; - /*return BADCH;*/ - } - else - { - /* white space */ - opt->arg = argv[opt->ind]; - } - - opt->cur = EMSG; - ++opt->ind; - } - - return opt->opt; /* dump back option letter */ -} -#endif - ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt) { ase_char_t* oli; /* option letter list index */ int dbldash = 0; + opt->arg = ASE_NULL; opt->lngopt = ASE_NULL; if (opt->cur == ASE_NULL) @@ -187,29 +111,29 @@ ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt) for (o = opt->lng; o->str != ASE_NULL; o++) { - if (ase_strxcmp (opt->cur, end-opt->cur, o->str) != 0) continue; + const ase_char_t* str = o->str; + if (*str == ASE_T(':')) str++; + + if (ase_strxcmp (opt->cur, end-opt->cur, str) != 0) continue; /* match */ opt->cur = EMSG; opt->lngopt = o->str; - opt->arg = ((*end == ASE_T('='))? (end + 1): ASE_NULL); + if (*end == ASE_T('=')) opt->arg = end + 1; - if (o->has_arg == ASE_OPT_ARG_NONE) + if (*o->str != ASE_T(':')) { - if (opt->arg != ASE_NULL) - { - /* redundant argument */ - return BADARG; - } + /* should not have an option argument */ + if (opt->arg != ASE_NULL) return BADARG; } - else if (o->has_arg == ASE_OPT_ARG_REQUIRED && opt->arg == ASE_NULL) + else if (opt->arg == ASE_NULL) { - if (argc > ++opt->ind) opt->arg = argv[opt->ind]; - else - { - /* missing argument */ - return BADARG; - } + /* Check if it has a remaining argument + * available */ + if (argc <= ++opt->ind) return BADARG; + /* If so, the next available argument is + * taken to be an option argument */ + opt->arg = argv[opt->ind]; } opt->ind++; @@ -236,8 +160,7 @@ ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt) if (*++oli != ASE_T(':')) { /* don't need argument */ - opt->arg = ASE_NULL; - if (*opt->cur == ASE_T('\0')) ++opt->ind; + if (*opt->cur == ASE_T('\0')) opt->ind++; } else { @@ -252,7 +175,6 @@ ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt) { /* no arg */ opt->cur = EMSG; - opt->arg = ASE_NULL; /*if (*opt->str == ASE_T(':'))*/ return BADARG; /*return BADCH;*/ }