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

View File

@ -5,6 +5,7 @@
#include <ase/awk/awk.h> #include <ase/awk/awk.h>
#include <ase/utl/helper.h> #include <ase/utl/helper.h>
#include <ase/utl/getopt.h>
#include <ase/utl/stdio.h> #include <ase/utl/stdio.h>
#include <ase/utl/main.h> #include <ase/utl/main.h>
@ -1029,36 +1030,39 @@ static void handle_args (argc, argv)
} }
#endif #endif
#if 0 static int handle_args (int argc, ase_char_t* argv[], struct awk_src_io* src_io)
static int handle_args (int argc, ase_char_t* argv[])
{ {
ase_opt_t opt;
ase_cint_t c; ase_cint_t c;
static ase_opt_lng_t lng[] =
{
{ ASE_T("implicit"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("explicit"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("bxor"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("shift"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("idiv"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("extio"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("newline"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("baseone"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("stripspaces"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("nextofile"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("crfl"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("argstomain"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("reset"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("maptovar"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("pablock"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
ase_memset (&opt, 0, ASE_SIZEOF(opt)); { ASE_T("help"), ASE_OPT_NONE, ASE_NULL, ASE_T('h')},
opt.str = ASE_T("hm:i:"); { ASE_T("main"), ASE_OPT_REQUIRED, ASE_NULL, ASE_T('m')}
};
{ ASE_T("implicit"), ASE_OPT_OPTIONAL, ASE_NULL, 0 }, static ase_opt_t opt =
{ ASE_T("explicit"), ASE_OPT_OPTIONAL, ASE_NULL, 0 }, {
{ ASE_T("bxor"), ASE_OPT_OPTIONAL, ASE_NULL, 0 }, ASE_T("hf:m:i:"),
{ ASE_T("shift"), ASE_OPT_OPTIONAL, ASE_NULL, 0 }, lng
{ ASE_T("idiv"), ASE_OPT_OPTIONAL, ASE_NULL, 0 }, };
{ ASE_T("extio"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("newline"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("baseone"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("stripspaces"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("nextofile"), ASE_OPT_OPTIONAL, ASE_NULL, 0 },
{ ASE_T("crfl"), ASE_OPT_OPTOINAL, ASE_NULL, 0 },
{ ASE_T("argstomain"), ASE_OPT_OPTOINAL, ASE_NULL, 0 },
{ ASE_T("reset"), ASE_OPT_OPTOINAL, ASE_NULL, 0 },
{ ASE_T("maptovar"), ASE_OPT_OPTOINAL, ASE_NULL, 0 },
{ ASE_T("pablock"), ASE_OPT_OPTOINAL, ASE_NULL, 0 },
{ ASE_T("help"), ASE_OPT_NONE, ASE_NULL, ASE_T('h')},
{ ASE_T("main"), ASE_OPT_REQUIRED, ASE_NULL, ASE_T('m')},
opt.lng = lng;
ase_printf (ASE_T("111111111111111111\n"));
while ((c = ase_getopt (argc, argv, &opt)) != ASE_CHAR_EOF) while ((c = ase_getopt (argc, argv, &opt)) != ASE_CHAR_EOF)
{ {
switch (c) switch (c)
@ -1067,12 +1071,9 @@ static int handle_args (int argc, ase_char_t* argv[])
print_usage (argv[0]); print_usage (argv[0]);
return -1; return -1;
case ASE_T('m'):
opt_memsize = ase_strtoi(opt.arg);
break;
case ASE_T('i'): case ASE_T('f'):
opt_meminc = ase_strtoi(opt.arg); src_io->input_file = opt.arg;
break; break;
case ASE_T('?'): case ASE_T('?'):
@ -1097,9 +1098,9 @@ static int handle_args (int argc, ase_char_t* argv[])
return -1; return -1;
} }
ase_printf (ASE_T("1xx11111111111111111\n"));
return 0; return 0;
} }
#endif
typedef struct extension_t typedef struct extension_t
{ {
@ -1140,13 +1141,11 @@ static int awk_main (int argc, ase_char_t* argv[])
ASE_AWK_BASEONE | ASE_AWK_BASEONE |
ASE_AWK_PABLOCK; ASE_AWK_PABLOCK;
#if 0 if (handle_args (argc, argv, &src_io) == -1)
if (handle_args (argc, argv) == -1)
{ {
print_usage (argv[0]); print_usage (argv[0]);
return -1; return -1;
} }
#endif
infiles[file_count] = ASE_NULL; infiles[file_count] = ASE_NULL;
runarg[runarg_count].ptr = NULL; runarg[runarg_count].ptr = NULL;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: getopt.h 285 2008-07-23 03:59:57Z baconevi $ * $Id: getopt.h 287 2008-07-24 14:08:37Z baconevi $
* *
* {License} * {License}
*/ */
@ -10,11 +10,16 @@
#include <ase/types.h> #include <ase/types.h>
#include <ase/macros.h> #include <ase/macros.h>
typedef struct ase_opt_t ase_opt_t; #define ASE_OPT_NONE 0
#define ASE_OPT_REQUIRED 1
#define ASE_OPT_OPTIONAL 2
struct ase_opt_long_t 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* name; const ase_char_t* str;
int has_arg; int has_arg;
int* flag; int* flag;
int val; int val;
@ -23,8 +28,8 @@ struct ase_opt_long_t
struct ase_opt_t struct ase_opt_t
{ {
/* input */ /* input */
const ase_char_t* str; const ase_char_t* str; /* option string */
// ase_opt_long_t* opt_long; ase_opt_lng_t* lng; /* long options */
/* output */ /* output */
ase_cint_t opt; /* character checked for validity */ ase_cint_t opt; /* character checked for validity */
@ -34,6 +39,9 @@ struct ase_opt_t
/* input + output */ /* input + output */
int ind; /* index into parent argv vector */ int ind; /* index into parent argv vector */
/* output */
int lngind;
/* internal */ /* internal */
ase_char_t* cur; ase_char_t* cur;
}; };

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} * {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) ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt)
{ {
static int lastidx,lastofs; ase_char_t* oli; /* option letter list index */
ase_char_t* tmp;
if (opt->ind == 0) opt->ind = 1; if (opt->ind == 0) opt->ind = 1;
again: //again:
if (opt->ind > argc || !argv[opt->ind] || if (opt->ind > argc || !argv[opt->ind] ||
argv[opt->ind][0] != ASE_T('-') || 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('-') && if (argv[opt->ind][1] == ASE_T('-') &&
argv[opt->ind][2] == ASE_T('\0')) argv[opt->ind][2] == ASE_T('\0'))
@ -147,12 +146,13 @@ again:
return -1; 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 */ /* a long option */
ase_char_t* arg = argv[opt->ind] + 2; ase_char_t* arg = argv[opt->ind] + 2;
const struct option* o; const ase_opt_lng_t* o;
/* TODO: rewrite it.. */ /* TODO: rewrite it.. */
/*char* max=strchr(arg,'='); /*char* max=strchr(arg,'=');
@ -161,14 +161,13 @@ again:
while (*max != ASE_T('\0') && *max != ASE_T('=')) max++; while (*max != ASE_T('\0') && *max != ASE_T('=')) max++;
//for (o = longopts; o->name != ASE_NULL; o++) //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 (!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 */ /* match */
//if (longindex != ASE_NULL) *longindex =o - longopts; opt->lngind = o - opt->lng;
if (longindex != ASE_NULL) opt->lidx = o - opt->lopt;
if (o->has_arg > 0) if (o->has_arg > 0)
{ {
if (*max == ASE_T('=')) if (*max == ASE_T('='))
@ -197,10 +196,8 @@ again:
opt->ind++; opt->ind++;
if (o->flag) if (o->flag) *(o->flag) = o->val;
*(o->flag)=o->val; else return o->val;
else
return o->val;
return 0; return 0;
} }
@ -217,18 +214,23 @@ again:
return ASE_T('?'); return ASE_T('?');
} }
if (lastidx!=opt->ind) { #if 0
if (lastidx != opt->ind)
{
lastidx=opt->ind; lastofs=0; lastidx=opt->ind; lastofs=0;
} }
optopt=argv[opt->ind][lastofs+1]; opt->opt=argv[opt->ind][lastofs+1];
if ((tmp=strchr(optstring,optopt))) {
if (*tmp==0) 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 */ { /* apparently, we looked for \0, i.e. end of argument */
++opt->ind; ++opt->ind;
goto again; goto again;
} }
if (tmp[1]==':') if (tmp[1] == ASE_T(':'))
{ /* argument expected */ { /* argument expected */
if (tmp[2]==':' || argv[opt->ind][lastofs+2]) { /* "-foo", return "oo" as opt->arg */ 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; if (!*(opt->arg=argv[opt->ind]+lastofs+2)) opt->arg=0;
@ -237,7 +239,7 @@ again:
opt->arg=argv[opt->ind+1]; opt->arg=argv[opt->ind+1];
if (!opt->arg) { /* missing argument */ if (!opt->arg) { /* missing argument */
++opt->ind; ++opt->ind;
if (*optstring==':') return ':'; if (*opt->str==':') return ':';
getopterror(1); getopterror(1);
return ':'; return ':';
} }
@ -246,7 +248,7 @@ again:
else else
{ {
++lastofs; ++lastofs;
return optopt; return opt->opt;
} }
found: found:
++opt->ind; ++opt->ind;
@ -258,5 +260,76 @@ found:
++opt->ind; ++opt->ind;
return '?'; 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 */
} }