improved the standard console handler to handle when ARGV has been overridden in a script

This commit is contained in:
2009-06-25 02:29:33 +00:00
parent 4b139e0472
commit 393dd9be8d
82 changed files with 593 additions and 101 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: err.c 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: err.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -146,7 +146,7 @@ const qse_char_t* qse_awk_dflerrstr (qse_awk_t* awk, qse_awk_errnum_t errnum)
QSE_T("no such io name found"),
QSE_T("i/o handler returned an error"),
QSE_T("i/o name empty"),
QSE_T("i/o name containing a null character"),
QSE_T("i/o name '${0}' containing a null character"),
QSE_T("not sufficient arguments to formatting sequence"),
QSE_T("recursion detected in format conversion"),
QSE_T("invalid character in CONVFMT"),

View File

@ -1,5 +1,5 @@
/*
* $Id: fnc.c 203 2009-06-17 12:43:50Z hyunghwan.chung $
* $Id: fnc.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -787,7 +787,7 @@ static int fnc_split (
/* put it into the map */
key_len = qse_awk_longtostr (
++nflds, 10, QSE_NULL, key, QSE_COUNTOF(key));
run->awk, ++nflds, 10, QSE_NULL, key, QSE_COUNTOF(key));
QSE_ASSERT (key_len != (qse_size_t)-1);
/* don't forget to update the reference count when you

View File

@ -1,5 +1,5 @@
/*
* $Id: misc.c 195 2009-06-10 13:18:25Z hyunghwan.chung $
* $Id: misc.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -533,8 +533,8 @@ done:
}
qse_size_t qse_awk_longtostr (
qse_long_t value, int radix, const qse_char_t* prefix,
qse_char_t* buf, qse_size_t size)
qse_awk_t* awk, qse_long_t value,
int radix, const qse_char_t* prefix, qse_char_t* buf, qse_size_t size)
{
qse_long_t t, rem;
qse_size_t len, ret, i;

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: parse.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -5697,7 +5697,8 @@ static int deparse (qse_awk_t* awk)
}
else
{
len = qse_awk_longtostr ((qse_long_t)i,
len = qse_awk_longtostr (
awk, (qse_long_t)i,
10, QSE_T("__g"), tmp, QSE_COUNTOF(tmp));
QSE_ASSERT (len != (qse_size_t)-1);
if (qse_awk_putsrcstrx (awk, tmp, len) == -1)
@ -5722,7 +5723,8 @@ static int deparse (qse_awk_t* awk)
}
else
{
len = qse_awk_longtostr ((qse_long_t)i,
len = qse_awk_longtostr (
awk, (qse_long_t)i,
10, QSE_T("__g"), tmp, QSE_COUNTOF(tmp));
QSE_ASSERT (len != (qse_size_t)-1);
if (qse_awk_putsrcstrx (awk, tmp, len) == -1)
@ -5858,7 +5860,8 @@ exit_deparse:
static qse_map_walk_t deparse_func (qse_map_t* map, qse_map_pair_t* pair, void* arg)
static qse_map_walk_t deparse_func (
qse_map_t* map, qse_map_pair_t* pair, void* arg)
{
struct deparse_func_t* df = (struct deparse_func_t*)arg;
/* CHECK: */
@ -5892,7 +5895,8 @@ static qse_map_walk_t deparse_func (qse_map_t* map, qse_map_pair_t* pair, void*
for (i = 0; i < fun->nargs; )
{
n = qse_awk_longtostr (i++, 10,
n = qse_awk_longtostr (
df->awk, i++, 10,
QSE_T("__p"), df->tmp, df->tmp_len);
QSE_ASSERT (n != (qse_size_t)-1);
PUT_SX (df, df->tmp, n);

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: run.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -1004,7 +1004,8 @@ static int build_runarg (
}
key_len = qse_awk_longtostr (
argc, 10, QSE_NULL, key, QSE_COUNTOF(key));
run->awk, argc, 10,
QSE_NULL, key, QSE_COUNTOF(key));
QSE_ASSERT (key_len != (qse_size_t)-1);
/* increment reference count of v_tmp in advance as if
@ -2820,10 +2821,21 @@ static int run_print (qse_awk_rtx_t* run, qse_awk_nde_print_t* nde)
{
if (out[--len] == QSE_T('\0'))
{
qse_cstr_t errarg;
errarg.ptr = out;
/* provide length up to one character before
* the first null not to contains a null
* in an error message */
errarg.len = qse_strlen(out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL,
nde->line, &errarg
);
/* if so, it skips writing */
QSE_AWK_FREE (run->awk, out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL, nde->line, QSE_NULL);
return -1;
}
}
@ -2978,11 +2990,22 @@ static int run_printf (qse_awk_rtx_t* run, qse_awk_nde_print_t* nde)
{
if (out[--len] == QSE_T('\0'))
{
qse_cstr_t errarg;
errarg.ptr = out;
/* provide length up to one character before
* the first null not to contains a null
* in an error message */
errarg.len = qse_strlen(out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL,
nde->line, &errarg
);
/* the output destination name contains a null
* character. */
QSE_AWK_FREE (run->awk, out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL, nde->line, QSE_NULL);
return -1;
}
}
@ -6380,7 +6403,9 @@ static qse_awk_val_t* eval_getline (qse_awk_rtx_t* run, qse_awk_nde_t* nde)
if (in[--len] == QSE_T('\0'))
{
/* the input source name contains a null
* character. make getline return -1 */
* character. make getline return -1.
* unlike print & printf, it is not a hard
* error */
QSE_AWK_FREE (run->awk, in);
n = -1;
goto skip_read;

View File

@ -1,5 +1,5 @@
/*
* $Id: std.c 209 2009-06-23 13:29:18Z hyunghwan.chung $
* $Id: std.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -78,6 +78,7 @@ typedef struct rxtn_t
struct {
const qse_char_t*const* files;
qse_size_t index;
qse_size_t count;
} in;
struct
@ -616,15 +617,92 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
* any fields of riod when the open operation fails */
qse_sio_t* sio;
const qse_char_t* file;
qse_awk_val_t* argv;
qse_map_t* map;
qse_map_pair_t* pair;
qse_char_t ibuf[128];
qse_size_t ibuflen;
qse_awk_val_t* v;
qse_awk_rtx_valtostr_out_t out;
nextfile:
file = rxtn->c.in.files[rxtn->c.in.index];
if (file == QSE_NULL)
{
/* no more input file */
if (rxtn->c.in.count == 0)
{
/* all ARGVs are empty strings.
* so no console files were opened.
* open the standard input here.
*
* 'BEGIN { ARGV[1]=""; ARGV[2]=""; }
* { print $0; }' file1 file2
*/
riod->handle = qse_sio_in;
rxtn->c.in.count++;
return 1;
}
return 0;
}
/* handle special case when ARGV[x] has been altered.
* so from here down, the file name gotten from
* rxtn->c.in.files is not important and is overridden
* from ARGV.
* 'BEGIN { ARGV[1]="file3"; }
* { print $0; }' file1 file2
*/
argv = qse_awk_rtx_getgbl (rtx, QSE_AWK_GBL_ARGV);
QSE_ASSERT (argv != QSE_NULL);
QSE_ASSERT (argv->type == QSE_AWK_VAL_MAP);
map = ((qse_awk_val_map_t*)argv)->map;
QSE_ASSERT (map != QSE_NULL);
ibuflen = qse_awk_longtostr (
rtx->awk, rxtn->c.in.index + 1, 10, QSE_NULL,
ibuf, QSE_COUNTOF(ibuf));
pair = qse_map_search (map, ibuf, ibuflen);
QSE_ASSERT (pair != QSE_NULL);
v = QSE_MAP_VPTR(pair);
QSE_ASSERT (v != QSE_NULL);
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) return -1;
if (out.u.cpldup.len == 0)
{
/* the name is empty */
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
rxtn->c.in.index++;
goto nextfile;
}
if (qse_strlen(out.u.cpldup.ptr) < out.u.cpldup.len)
{
/* the name contains one or more '\0' */
qse_cstr_t errarg;
errarg.ptr = out.u.cpldup.ptr;
/* use this length not to contains '\0'
* in an error message */
errarg.len = qse_strlen(out.u.cpldup.ptr);
qse_awk_rtx_seterror (
rtx, QSE_AWK_EIONMNL, 0, &errarg);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
file = out.u.cpldup.ptr;
if (file[0] == QSE_T('-') && file[1] == QSE_T('\0'))
{
/* special file name '-' */
@ -643,6 +721,8 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
qse_awk_rtx_seterror (
rtx, QSE_AWK_EOPEN, 0, &errarg);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
}
@ -651,10 +731,15 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
rtx, file, qse_strlen(file)) == -1)
{
if (sio != qse_sio_in) qse_sio_close (sio);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
riod->handle = sio;
/* increment the counter of files successfully opened */
rxtn->c.in.count++;
}
rxtn->c.in.index++;
@ -704,7 +789,6 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
}
}
if (qse_awk_rtx_setofilename (
rtx, file, qse_strlen(file)) == -1)
{
@ -932,6 +1016,7 @@ qse_awk_rtx_t* qse_awk_rtx_openstd (
rxtn->c.in.files = icf;
rxtn->c.in.index = 0;
rxtn->c.in.count = 0;
rxtn->c.out.files = ocf;
rxtn->c.out.index = 0;

View File

@ -1,5 +1,5 @@
/*
* $Id: tree.c 75 2009-02-22 14:10:34Z hyunghwan.chung $
* $Id: tree.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -320,7 +320,10 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
QSE_ASSERT (px->id.idxa != (qse_size_t)-1);
n = qse_awk_longtostr (
px->id.idxa, 10, QSE_NULL, tmp, QSE_COUNTOF(tmp));
awk,
px->id.idxa,
10, QSE_NULL, tmp, QSE_COUNTOF(tmp)
);
PUT_SRCSTR (awk, QSE_T("__p"));
PUT_SRCSTRX (awk, tmp, n);
@ -338,8 +341,10 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
PUT_SRCSTR (awk, QSE_T("__p"));
n = qse_awk_longtostr (
awk,
px->id.idxa, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T("["));
PRINT_EXPRESSION_LIST (awk, px->idx);
@ -396,8 +401,12 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
PUT_SRCSTR (awk, QSE_T("__g"));
n = qse_awk_longtostr (
px->id.idxa, 10,
QSE_NULL, tmp, QSE_COUNTOF(tmp));
awk,
px->id.idxa,
10,
QSE_NULL,
tmp, QSE_COUNTOF(tmp)
);
PUT_SRCSTRX (awk, tmp, n);
}
}
@ -434,8 +443,12 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
PUT_SRCSTR (awk, QSE_T("__g"));
n = qse_awk_longtostr (
px->id.idxa, 10,
QSE_NULL, tmp, QSE_COUNTOF(tmp));
awk,
px->id.idxa,
10,
QSE_NULL,
tmp, QSE_COUNTOF(tmp)
);
PUT_SRCSTRX (awk, tmp, n);
}
PUT_SRCSTR (awk, QSE_T("["));
@ -460,8 +473,13 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
{
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
px->id.idxa, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
px->id.idxa,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
}
else
@ -481,8 +499,13 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
{
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
px->id.idxa, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
px->id.idxa,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T("["));
}
@ -616,16 +639,26 @@ static int print_statement (qse_awk_t* awk, qse_awk_nde_t* p, int depth)
{
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
i, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
i,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T(", "));
}
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
i, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
i,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T(";"));
PUT_NEWLINE (awk);