This commit is contained in:
hyung-hwan 2008-07-28 00:16:54 +00:00
parent 925701797f
commit a754d8e7c1
3 changed files with 54 additions and 128 deletions

View File

@ -1035,28 +1035,28 @@ static int handle_args (int argc, ase_char_t* argv[], struct awk_src_io* src_io)
ase_cint_t c; ase_cint_t c;
static ase_opt_lng_t lng[] = static ase_opt_lng_t lng[] =
{ {
{ ASE_T("implicit"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("implicit"), 0 },
{ ASE_T("explicit"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("explicit"), 0 },
{ ASE_T("bxor"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("bxor"), 0 },
{ ASE_T("shift"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("shift"), 0 },
{ ASE_T("idiv"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("idiv"), 0 },
{ ASE_T("extio"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("extio"), 0 },
{ ASE_T("newline"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("newline"), 0 },
{ ASE_T("baseone"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("baseone"), 0 },
{ ASE_T("stripspaces"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("stripspaces"), 0 },
{ ASE_T("nextofile"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("nextofile"), 0 },
{ ASE_T("crlf"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("crlf"), 0 },
{ ASE_T("argstomain"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("argstomain"), 0 },
{ ASE_T("reset"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("reset"), 0 },
{ ASE_T("maptovar"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("maptovar"), 0 },
{ ASE_T("pablock"), ASE_OPT_ARG_OPTIONAL, 0 }, { ASE_T("pablock"), 0 },
{ ASE_T("main"), ASE_OPT_ARG_REQUIRED, ASE_T('m') }, { ASE_T(":main"), ASE_T('m') },
{ ASE_T("file"), ASE_OPT_ARG_REQUIRED, ASE_T('f') }, { ASE_T(":file"), ASE_T('f') },
{ ASE_T("field-separator"), ASE_OPT_ARG_REQUIRED, ASE_T('F') }, { ASE_T(":field-separator"), ASE_T('F') },
{ ASE_T("assign"), ASE_OPT_ARG_REQUIRED, ASE_T('v') }, { ASE_T(":assign"), ASE_T('v') },
{ ASE_T("help"), ASE_OPT_ARG_NONE, ASE_T('h')} { ASE_T("help"), ASE_T('h') }
}; };

View File

@ -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} * {License}
*/ */
@ -10,21 +10,13 @@
#include <ase/types.h> #include <ase/types.h>
#include <ase/macros.h> #include <ase/macros.h>
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_t ase_opt_t;
typedef struct ase_opt_lng_t ase_opt_lng_t; typedef struct ase_opt_lng_t ase_opt_lng_t;
struct ase_opt_lng_t struct ase_opt_lng_t
{ {
const ase_char_t* str; const ase_char_t* str;
int has_arg; ase_cint_t val;
int val;
}; };
struct ase_opt_t struct ase_opt_t
@ -38,7 +30,7 @@ struct ase_opt_t
ase_char_t* arg; /* argument associated with an option */ ase_char_t* arg; /* argument associated with an option */
/* output */ /* output */
ase_char_t* lngopt; const ase_char_t* lngopt;
/* input + output */ /* input + output */
int ind; /* index into parent argv vector */ int ind; /* index into parent argv vector */
@ -51,7 +43,19 @@ struct ase_opt_t
extern "C" { extern "C" {
#endif #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 #ifdef __cplusplus
} }

View File

@ -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} * {License}
*/ */
@ -49,88 +49,12 @@
#define BADARG ASE_T(':') #define BADARG ASE_T(':')
#define EMSG 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_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt)
{ {
ase_char_t* oli; /* option letter list index */ ase_char_t* oli; /* option letter list index */
int dbldash = 0; int dbldash = 0;
opt->arg = ASE_NULL;
opt->lngopt = ASE_NULL; opt->lngopt = ASE_NULL;
if (opt->cur == 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++) 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 */ /* match */
opt->cur = EMSG; opt->cur = EMSG;
opt->lngopt = o->str; 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) /* should not have an option argument */
{ if (opt->arg != ASE_NULL) return BADARG;
/* redundant argument */
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]; /* Check if it has a remaining argument
else * available */
{ if (argc <= ++opt->ind) return BADARG;
/* missing argument */ /* If so, the next available argument is
return BADARG; * taken to be an option argument */
} opt->arg = argv[opt->ind];
} }
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(':')) if (*++oli != ASE_T(':'))
{ {
/* don't need argument */ /* 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 else
{ {
@ -252,7 +175,6 @@ ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt)
{ {
/* no arg */ /* no arg */
opt->cur = EMSG; opt->cur = EMSG;
opt->arg = ASE_NULL;
/*if (*opt->str == ASE_T(':'))*/ return BADARG; /*if (*opt->str == ASE_T(':'))*/ return BADARG;
/*return BADCH;*/ /*return BADCH;*/
} }