*** empty log message ***

This commit is contained in:
hyung-hwan 2006-09-12 15:20:18 +00:00
parent 39650fce92
commit 241470730d
3 changed files with 168 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h,v 1.114 2006-09-10 15:50:34 bacon Exp $
* $Id: awk.h,v 1.115 2006-09-12 15:20:18 bacon Exp $
*/
#ifndef _XP_AWK_AWK_H_
@ -344,6 +344,7 @@ xp_awk_val_t* xp_awk_getglobal (void* run, xp_size_t idx);
int xp_awk_setglobal (void* run, xp_size_t idx, xp_awk_val_t* val);
void xp_awk_seterrnum (void* run, int errnum);
void xp_awk_setretval (void* run, xp_awk_val_t* val);
int xp_awk_setrecord (void* run, const xp_char_t* str, xp_size_t len);
/* utility functions exported by awk.h */
xp_long_t xp_awk_strtolong (

View File

@ -1,5 +1,5 @@
/*
* $Id: func.c,v 1.48 2006-09-11 14:29:22 bacon Exp $
* $Id: func.c,v 1.49 2006-09-12 15:20:18 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -801,10 +801,16 @@ static int __bfn_gsub (xp_awk_t* awk, void* run)
{
xp_size_t nargs;
xp_awk_val_t* a0, * a1, * a2;
xp_char_t* a0_ptr, * a1_ptr;
xp_size_t a0_len, a1_len;
xp_char_t* a0_ptr, * a1_ptr, * a2_ptr;
xp_size_t a0_len, a1_len, a2_len;
xp_char_t* a0_ptr_free = XP_NULL;
xp_char_t* a1_ptr_free = XP_NULL;
xp_char_t* a2_ptr_free = XP_NULL;
void* rex;
int opt, n;
const xp_char_t* cur_ptr, * match_ptr;
xp_size_t cur_len, match_len;
xp_awk_str_t new;
nargs = xp_awk_getnargs (run);
xp_assert (nargs >= 2 && nargs <= 3);
@ -844,17 +850,142 @@ static int __bfn_gsub (xp_awk_t* awk, void* run)
a1_ptr_free = a1_ptr;
}
/* TODO: */
if (a2 == XP_NULL)
{
/* operation is on $0 */
/* TODO; is this correct??? need to use inrec.d0?? */
a2_ptr = XP_AWK_STR_BUF(&((xp_awk_run_t*)run)->inrec.line);
a2_len = XP_AWK_STR_LEN(&((xp_awk_run_t*)run)->inrec.line);
}
else
{
/* operation is on a2 */
/*
a2ref = (xp_awk_val_t**)((xp_awk_val_ref_t*)a2)->adr;
if ((*a2ref)->type != XP_AWK_VAL_NIL &&
(*a2ref)->type != XP_AWK_VAL_MAP)
{
}
*/
}
rex = xp_awk_buildrex (awk,
a0_ptr, a0_len, &((xp_awk_run_t*)run)->errnum);
if (rex == XP_NULL)
{
if (a2_ptr_free != XP_NULL) XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL) XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL) XP_AWK_FREE (awk, a0_ptr_free);
return -1;
}
opt = (((xp_awk_run_t*)run)->rex.ignorecase)? XP_AWK_REX_IGNORECASE: 0;
cur_ptr = a2_ptr;
cur_len = a2_len;
if (xp_awk_str_open (&new, a2_len, awk) == XP_NULL)
{
xp_awk_freerex (awk, rex);
if (a2_ptr_free != XP_NULL) XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL) XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL) XP_AWK_FREE (awk, a0_ptr_free);
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
while (1)
{
n = xp_awk_matchrex (
awk, rex, opt, cur_ptr, cur_len,
&match_ptr, &match_len, &((xp_awk_run_t*)run)->errnum);
if (n == -1)
{
xp_awk_str_close (&new);
xp_awk_freerex (awk, rex);
if (a2_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a0_ptr_free);
return -1;
}
if (n == 0)
{
/* no more match found */
if (xp_awk_str_ncat (
&new, cur_ptr, cur_len) == (xp_size_t)-1)
{
xp_awk_str_close (&new);
xp_awk_freerex (awk, rex);
if (a2_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a0_ptr_free);
return -1;
}
break;
}
if (xp_awk_str_ncat (&new,
cur_ptr, match_ptr - cur_ptr) == (xp_size_t)-1)
{
xp_awk_str_close (&new);
xp_awk_freerex (awk, rex);
if (a2_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a0_ptr_free);
return -1;
}
/*TODO: handle & */
if (xp_awk_str_ncat (&new, a1_ptr, a1_len) == (xp_size_t)-1)
{
xp_awk_str_close (&new);
xp_awk_freerex (awk, rex);
if (a2_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a0_ptr_free);
return -1;
}
cur_ptr = match_ptr + match_len;
cur_len = cur_len - ((match_ptr - cur_ptr) + match_len);
}
xp_awk_freerex (awk, rex);
xp_printf (XP_T("NEW STRING [%s]\n"), XP_AWK_STR_BUF(&new));
if (a2 == XP_NULL)
{
if (xp_awk_setrecord (run,
XP_AWK_STR_BUF(&new), XP_AWK_STR_LEN(&new)) == -1)
{
xp_awk_str_close (&new);
if (a2_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL)
XP_AWK_FREE (awk, a0_ptr_free);
return -1;
}
}
else
{
/* TODO: */
}
xp_awk_str_close (&new);
if (a2_ptr_free != XP_NULL) XP_AWK_FREE (awk, a2_ptr_free);
if (a1_ptr_free != XP_NULL) XP_AWK_FREE (awk, a1_ptr_free);
if (a0_ptr_free != XP_NULL) XP_AWK_FREE (awk, a0_ptr_free);

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c,v 1.200 2006-09-11 03:20:42 bacon Exp $
* $Id: run.c,v 1.201 2006-09-12 15:20:18 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -4690,14 +4690,9 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde)
if (p->var == XP_NULL)
{
/* set $0 with the input value */
if (__clear_record (run, xp_false) == -1)
{
xp_awk_str_close (&buf);
return XP_NULL;
}
if (__set_record (run,
XP_AWK_STR_BUF(&buf), XP_AWK_STR_LEN(&buf)) == -1)
if (xp_awk_setrecord (run,
XP_AWK_STR_BUF(&buf),
XP_AWK_STR_LEN(&buf)) == -1)
{
xp_awk_str_close (&buf);
return XP_NULL;
@ -4791,9 +4786,12 @@ static int __read_record (xp_awk_run_t* run)
run, XP_AWK_IN_CONSOLE, XP_T(""), &run->inrec.line);
if (n < 0)
{
if (run->errnum == XP_AWK_EIOHANDLER)
PANIC_I (run, XP_AWK_ECONINDATA);
else return -1;
int errnum = run->errnum;
__clear_record (run, xp_false);
run->errnum =
(errnum == XP_AWK_EIOHANDLER)?
XP_AWK_ECONINDATA: errnum;
return -1;
}
if (n == 0)
{
@ -4808,7 +4806,23 @@ static int __read_record (xp_awk_run_t* run)
return 1;
}
static int __set_record (xp_awk_run_t* run, const xp_char_t* str, xp_size_t len)
int xp_awk_setrecord (void* run, const xp_char_t* str, xp_size_t len)
{
if (__clear_record (run, xp_false) == -1) return -1;
if (xp_awk_str_ncpy (
&((xp_awk_run_t*)run)->inrec.line, str, len) == (xp_size_t)-1)
{
__clear_record (run, xp_false);
((xp_awk_run_t*)run)->errnum = XP_AWK_ENOMEM;
return -1;
}
return __set_record (run, str, len);
}
static int __set_record (
xp_awk_run_t* run, const xp_char_t* str, xp_size_t len)
{
xp_awk_val_t* v;
int errnum;
@ -4820,7 +4834,7 @@ static int __set_record (xp_awk_run_t* run, const xp_char_t* str, xp_size_t len)
PANIC_I (run, XP_AWK_ENOMEM);
}
xp_assert (run->inrec.d0 == xp_awk_val_nil);
xp_assert (run->inrec.d0->type == XP_AWK_VAL_NIL);
/* the record should be clear cleared before this function is called
* as it doesn't call xp_awk_refdownval on run->inrec.d0 */
run->inrec.d0 = v;
@ -5082,7 +5096,7 @@ static int __recomp_record_fields (
v = STACK_GLOBAL(run, XP_AWK_GLOBAL_OFS);
xp_awk_refupval (v);
if (v == xp_awk_val_nil)
if (v->type == XP_AWK_VAL_NIL)
{
/* OFS not set */
ofs = XP_T(" ");
@ -5238,7 +5252,7 @@ static int __shorten_record (xp_awk_run_t* run, xp_size_t nflds)
v = STACK_GLOBAL(run, XP_AWK_GLOBAL_OFS);
xp_awk_refupval (v);
if (v == xp_awk_val_nil)
if (v->type == XP_AWK_VAL_NIL)
{
/* OFS not set */
ofs = XP_T(" ");