This commit is contained in:
2008-07-25 08:08:37 +00:00
parent bcb66176b0
commit 3a8e597e03
3 changed files with 141 additions and 61 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: getopt.c 286 2008-07-23 14:11:17Z baconevi $
* $Id: getopt.c 287 2008-07-24 14:08:37Z baconevi $
*
* {License}
*/
@ -130,15 +130,14 @@ 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)
{
static int lastidx,lastofs;
ase_char_t* tmp;
ase_char_t* oli; /* option letter list index */
if (opt->ind == 0) opt->ind = 1;
again:
//again:
if (opt->ind > argc || !argv[opt->ind] ||
argv[opt->ind][0] != ASE_T('-') ||
argv[opt->ind][1] == AES_T('\0')) return -1;
argv[opt->ind][1] == ASE_T('\0')) return -1;
if (argv[opt->ind][1] == ASE_T('-') &&
argv[opt->ind][2] == ASE_T('\0'))
@ -147,12 +146,13 @@ again:
return -1;
}
if (argv[opt->ind][1] == ASE_T('-'))
// TODO: how to handle when lng is off? is this correct?
if (opt->lng != ASE_NULL && argv[opt->ind][1] == ASE_T('-'))
{
/* a long option */
ase_char_t* arg = argv[opt->ind] + 2;
const struct option* o;
const ase_opt_lng_t* o;
/* TODO: rewrite it.. */
/*char* max=strchr(arg,'=');
@ -161,14 +161,13 @@ again:
while (*max != ASE_T('\0') && *max != ASE_T('=')) max++;
//for (o = longopts; o->name != ASE_NULL; o++)
for (o = opt->lopt; o->name != ASE_NULL; o++)
for (o = opt->lng; o->str != ASE_NULL; o++)
{
//if (!strncmp (o->name, arg, max - arg))
if (ase_strxcmp (arg, max-arg, o->name) != 0) continue;
if (ase_strxcmp (arg, max-arg, o->str) != 0) continue;
/* match */
//if (longindex != ASE_NULL) *longindex =o - longopts;
if (longindex != ASE_NULL) opt->lidx = o - opt->lopt;
opt->lngind = o - opt->lng;
if (o->has_arg > 0)
{
if (*max == ASE_T('='))
@ -197,10 +196,8 @@ again:
opt->ind++;
if (o->flag)
*(o->flag)=o->val;
else
return o->val;
if (o->flag) *(o->flag) = o->val;
else return o->val;
return 0;
}
@ -217,18 +214,23 @@ again:
return ASE_T('?');
}
if (lastidx!=opt->ind) {
#if 0
if (lastidx != opt->ind)
{
lastidx=opt->ind; lastofs=0;
}
optopt=argv[opt->ind][lastofs+1];
if ((tmp=strchr(optstring,optopt))) {
if (*tmp==0)
opt->opt=argv[opt->ind][lastofs+1];
tmp=ase_strchr(opt->str,opt->opt);
if (tmp != ASE_NULL)
{
if (tmp[0] == ASE_T('\0'))
{ /* apparently, we looked for \0, i.e. end of argument */
++opt->ind;
goto again;
}
if (tmp[1]==':')
if (tmp[1] == ASE_T(':'))
{ /* argument expected */
if (tmp[2]==':' || argv[opt->ind][lastofs+2]) { /* "-foo", return "oo" as opt->arg */
if (!*(opt->arg=argv[opt->ind]+lastofs+2)) opt->arg=0;
@ -237,7 +239,7 @@ again:
opt->arg=argv[opt->ind+1];
if (!opt->arg) { /* missing argument */
++opt->ind;
if (*optstring==':') return ':';
if (*opt->str==':') return ':';
getopterror(1);
return ':';
}
@ -246,7 +248,7 @@ again:
else
{
++lastofs;
return optopt;
return opt->opt;
}
found:
++opt->ind;
@ -258,5 +260,76 @@ found:
++opt->ind;
return '?';
}
#endif
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 */
}