diff --git a/ase/awk/extio.c b/ase/awk/extio.c index a66738a7..cc4b2afd 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.17 2006-07-01 16:07:06 bacon Exp $ + * $Id: extio.c,v 1.18 2006-07-13 03:10:34 bacon Exp $ */ #include @@ -249,7 +249,7 @@ int xp_awk_writeextio ( } /* convert the value to string representation first */ - if (xp_awk_valtostr(v, errnum, &buf) == XP_NULL) + if (xp_awk_valtostr(v, errnum, &buf, XP_NULL) == XP_NULL) { xp_str_close (&buf); return -1; diff --git a/ase/awk/func.c b/ase/awk/func.c index 4c217a6a..88b15d38 100644 --- a/ase/awk/func.c +++ b/ase/awk/func.c @@ -1,5 +1,5 @@ /* - * $Id: func.c,v 1.6 2006-06-30 04:25:53 bacon Exp $ + * $Id: func.c,v 1.7 2006-07-13 03:10:35 bacon Exp $ */ #include @@ -60,7 +60,7 @@ static int __bfn_close (void* run) return -1; } - if (xp_awk_valtostr (xp_awk_getarg(run, 0), &errnum, &buf) == XP_NULL) + if (xp_awk_valtostr (xp_awk_getarg(run, 0), &errnum, &buf, XP_NULL) == XP_NULL) { xp_str_close (&buf); xp_awk_seterrnum (run, errnum); diff --git a/ase/awk/run.c b/ase/awk/run.c index 15b343d3..c53e1bb9 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.131 2006-07-12 07:25:15 bacon Exp $ + * $Id: run.c,v 1.132 2006-07-13 03:10:35 bacon Exp $ */ #include @@ -1192,7 +1192,8 @@ static int __run_delete (xp_awk_run_t* run, xp_awk_nde_delete_t* nde) if (idx == XP_NULL) return -1; xp_awk_refupval (idx); - key = xp_awk_valtostr (idx, &errnum, XP_NULL); + key = xp_awk_valtostr ( + idx, &errnum, XP_NULL, XP_NULL); xp_awk_refdownval (run, idx); if (key == XP_NULL) PANIC_I (run, errnum); @@ -1269,7 +1270,8 @@ static int __run_delete (xp_awk_run_t* run, xp_awk_nde_delete_t* nde) if (idx == XP_NULL) return -1; xp_awk_refupval (idx); - key = xp_awk_valtostr (idx, &errnum, XP_NULL); + key = xp_awk_valtostr ( + idx, &errnum, XP_NULL, XP_NULL); xp_awk_refdownval (run, idx); if (key == XP_NULL) PANIC_I (run, errnum); @@ -1314,7 +1316,7 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) if (v == XP_NULL) return -1; xp_awk_refupval (v); - out = xp_awk_valtostr (v, &errnum, XP_NULL); + out = xp_awk_valtostr (v, &errnum, XP_NULL, XP_NULL); if (out == XP_NULL) { xp_awk_refdownval (run, v); @@ -1448,7 +1450,7 @@ static xp_awk_val_t* __eval_group (xp_awk_run_t* run, xp_awk_nde_t* nde) static xp_awk_val_t* __eval_assignment (xp_awk_run_t* run, xp_awk_nde_t* nde) { - xp_awk_val_t* val, * res; + xp_awk_val_t* val, * ret; xp_awk_nde_ass_t* ass = (xp_awk_nde_ass_t*)nde; xp_assert (ass->left != XP_NULL && ass->right != XP_NULL); @@ -1515,15 +1517,17 @@ static xp_awk_val_t* __eval_assignment (xp_awk_run_t* run, xp_awk_nde_t* nde) xp_awk_refupval (val); } - res = __do_assignment (run, ass->left, val); + ret = __do_assignment (run, ass->left, val); xp_awk_refdownval (run, val); - return res; + return ret; } static xp_awk_val_t* __do_assignment ( xp_awk_run_t* run, xp_awk_nde_t* var, xp_awk_val_t* val) { + xp_awk_val_t* ret; + if (val->type == XP_AWK_VAL_MAP) { /* a map cannot be assigned to a variable */ @@ -1535,30 +1539,18 @@ static xp_awk_val_t* __do_assignment ( var->type == XP_AWK_NDE_LOCAL || var->type == XP_AWK_NDE_ARG) { - if (__do_assignment_scalar ( - run, (xp_awk_nde_var_t*)var, val) == XP_NULL) - { - return XP_NULL; - } + ret = __do_assignment_scalar (run, (xp_awk_nde_var_t*)var, val); } else if (var->type == XP_AWK_NDE_NAMEDIDX || var->type == XP_AWK_NDE_GLOBALIDX || var->type == XP_AWK_NDE_LOCALIDX || var->type == XP_AWK_NDE_ARGIDX) { - if (__do_assignment_map ( - run, (xp_awk_nde_var_t*)var, val) == XP_NULL) - { - return XP_NULL; - } + ret = __do_assignment_map (run, (xp_awk_nde_var_t*)var, val); } else if (var->type == XP_AWK_NDE_POS) { - if (__do_assignment_pos ( - run, (xp_awk_nde_pos_t*)var, val) == XP_NULL) - { - return XP_NULL; - } + ret = __do_assignment_pos (run, (xp_awk_nde_pos_t*)var, val); } else { @@ -1566,7 +1558,7 @@ static xp_awk_val_t* __do_assignment ( PANIC (run, XP_AWK_EINTERNAL); } - return val; + return ret; } static xp_awk_val_t* __do_assignment_scalar ( @@ -1750,6 +1742,7 @@ static xp_awk_val_t* __do_assignment_pos ( xp_long_t lv; xp_real_t rv; xp_char_t* str; + xp_size_t len; int n, errnum; /* get the position number */ @@ -1765,14 +1758,14 @@ static xp_awk_val_t* __do_assignment_pos ( if (lv < 0) PANIC (run, XP_AWK_EPOSIDX); /* convert the value to the string */ - str = xp_awk_valtostr (val, &errnum, XP_NULL); + str = xp_awk_valtostr (val, &errnum, XP_NULL, &len); if (str == XP_NULL) PANIC (run, errnum); if (lv == 0) { __clear_record (run, xp_false); - if (xp_str_cpy (&run->inrec.line, str) == (xp_size_t)-1) + if (xp_str_ncpy (&run->inrec.line, str, len) == (xp_size_t)-1) { xp_free (str); PANIC (run, errnum); @@ -1791,12 +1784,8 @@ static xp_awk_val_t* __do_assignment_pos ( } else { - /* TODO: change xp_awk_valtostr to return the length - * so that xp_strlen doesn't have to be called here */ - xp_size_t xlen = xp_strlen(str); - if (__recomp_record_fields ( - run, (xp_size_t)lv, str, xlen, &errnum) == -1) + run, (xp_size_t)lv, str, len, &errnum) == -1) { xp_free (str); __clear_record (run, xp_false); @@ -1817,6 +1806,8 @@ static xp_awk_val_t* __do_assignment_pos ( xp_awk_refdownval (run, run->inrec.d0); run->inrec.d0 = v; xp_awk_refupval (v); + + val = v; } return val; @@ -2695,14 +2686,14 @@ static xp_awk_val_t* __eval_binop_concat ( PANIC (run, XP_AWK_ENOMEM); } - if (xp_awk_valtostr (left, &errnum, &lv) == XP_NULL) + if (xp_awk_valtostr (left, &errnum, &lv, XP_NULL) == XP_NULL) { xp_str_close (&lv); xp_str_close (&rv); PANIC (run, errnum); } - if (xp_awk_valtostr (right, &errnum, &rv) == XP_NULL) + if (xp_awk_valtostr (right, &errnum, &rv, XP_NULL) == XP_NULL) { xp_str_close (&lv); xp_str_close (&rv); @@ -3494,7 +3485,7 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde) if (v == XP_NULL) return XP_NULL; xp_awk_refupval (v); - in = xp_awk_valtostr (v, &errnum, XP_NULL); + in = xp_awk_valtostr (v, &errnum, XP_NULL, XP_NULL); if (in == XP_NULL) { xp_awk_refdownval (run, v); @@ -3938,7 +3929,7 @@ static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde) xp_awk_refupval (idx); - str = xp_awk_valtostr (idx, &errnum, XP_NULL); + str = xp_awk_valtostr (idx, &errnum, XP_NULL, XP_NULL); if (str == XP_NULL) { xp_awk_refdownval (run, idx); @@ -3977,7 +3968,8 @@ static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde) PANIC (run, XP_AWK_ENOMEM); } - if (xp_awk_valtostr (idx, &errnum, &idxstr) == XP_NULL) + if (xp_awk_valtostr (idx, + &errnum, &idxstr, XP_NULL) == XP_NULL) { xp_awk_refdownval (run, idx); xp_str_close (&idxstr); diff --git a/ase/awk/val.c b/ase/awk/val.c index f9b2e42b..7910a27c 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.40 2006-07-12 17:04:02 bacon Exp $ + * $Id: val.c,v 1.41 2006-07-13 03:10:35 bacon Exp $ */ #include @@ -314,7 +314,8 @@ xp_bool_t xp_awk_valtobool (xp_awk_val_t* val) return xp_false; } -xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) +xp_char_t* xp_awk_valtostr ( + xp_awk_val_t* v, int* errnum, xp_str_t* buf, xp_size_t* len) { if (v->type == XP_AWK_VAL_NIL) { @@ -322,12 +323,19 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) { xp_char_t* tmp; tmp = xp_strdup (XP_T("")); - if (tmp == XP_NULL) *errnum = XP_AWK_ENOMEM; + if (tmp == XP_NULL) + { + *errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + if (len != XP_NULL) *len = 0; return tmp; } else { xp_str_clear (buf); + if (len != XP_NULL) *len = XP_STR_LEN(buf); return XP_STR_BUF(buf); } } @@ -336,7 +344,7 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) { xp_char_t* tmp; xp_long_t t; - xp_size_t len = 0; + xp_size_t l = 0; t = ((xp_awk_val_int_t*)v)->val; if (t == 0) @@ -353,6 +361,7 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) tmp[0] = XP_T('0'); tmp[1] = XP_T('\0'); + if (len != XP_NULL) *len = 1; return tmp; } else @@ -364,53 +373,55 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) return XP_NULL; } + if (len != XP_NULL) *len = XP_STR_LEN(buf); return XP_STR_BUF(buf); } } /* non-zero values */ - if (t < 0) { t = -t; len++; } - while (t > 0) { len++; t /= 10; } + if (t < 0) { t = -t; l++; } + while (t > 0) { l++; t /= 10; } if (buf == XP_NULL) { - tmp = xp_malloc ((len + 1) * xp_sizeof(xp_char_t)); + tmp = xp_malloc ((l + 1) * xp_sizeof(xp_char_t)); if (tmp == XP_NULL) { *errnum = XP_AWK_ENOMEM; return XP_NULL; } - tmp[len] = XP_T('\0'); + tmp[l] = XP_T('\0'); } else { + /* clear the buffer */ xp_str_clear (buf); - /* get the current end of the buffer */ - tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf); + tmp = XP_STR_BUF(buf); /* extend the buffer */ if (xp_str_nccat ( - buf, XP_T(' '), len) == (xp_size_t)-1) + buf, XP_T(' '), l) == (xp_size_t)-1) { *errnum = XP_AWK_ENOMEM; return XP_NULL; } } + if (len != XP_NULL) *len = l; + t = ((xp_awk_val_int_t*)v)->val; if (t < 0) t = -t; while (t > 0) { - tmp[--len] = (xp_char_t)(t % 10) + XP_T('0'); + tmp[--l] = (xp_char_t)(t % 10) + XP_T('0'); t /= 10; } - if (((xp_awk_val_int_t*)v)->val < 0) tmp[--len] = XP_T('-'); + if (((xp_awk_val_int_t*)v)->val < 0) tmp[--l] = XP_T('-'); - /*return (buf == XP_NULL) tmp: XP_STR_BUF(buf);*/ return tmp; } @@ -429,7 +440,7 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) else { xp_str_clear (buf); - tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf); + tmp = XP_STR_BUF(buf); if (xp_str_ncat (buf, ((xp_awk_val_str_t*)v)->buf, @@ -440,6 +451,7 @@ xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) } } + if (len != XP_NULL) *len = ((xp_awk_val_str_t*)v)->len; return tmp; } diff --git a/ase/awk/val.h b/ase/awk/val.h index b801b442..048bf5eb 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.29 2006-07-12 07:25:15 bacon Exp $ + * $Id: val.h,v 1.30 2006-07-13 03:10:35 bacon Exp $ */ #ifndef _XP_AWK_VAL_H_ @@ -115,7 +115,8 @@ void xp_awk_refdownval (xp_awk_run_t* run, xp_awk_val_t* val); void xp_awk_refdownval_nofree (xp_awk_run_t* run, xp_awk_val_t* val); xp_bool_t xp_awk_valtobool (xp_awk_val_t* val); -xp_char_t* xp_awk_valtostr (xp_awk_val_t* val, int* errnum, xp_str_t* buf); +xp_char_t* xp_awk_valtostr ( + xp_awk_val_t* val, int* errnum, xp_str_t* buf, xp_size_t* len); void xp_awk_printval (xp_awk_val_t* val); diff --git a/ase/test/awk/t14.awk b/ase/test/awk/t14.awk index d9c22712..c9d338c6 100644 --- a/ase/test/awk/t14.awk +++ b/ase/test/awk/t14.awk @@ -33,7 +33,7 @@ global x, y; //$3 = $2; //$2 = $2; - $10 = 10; + $20 = 10; print $0; print "--------------------"; print "NF ===>>> " NF;