diff options
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | awk.h | 1 | ||||
-rw-r--r-- | eval.c | 30 | ||||
-rw-r--r-- | field.c | 21 | ||||
-rw-r--r-- | test/ChangeLog | 4 | ||||
-rw-r--r-- | test/ofs1.ok | 6 |
6 files changed, 57 insertions, 18 deletions
@@ -1,3 +1,16 @@ +2014-08-12 Arnold D. Robbins <arnold@skeeve.com> + + OFS being set should rebuild $0 using previous OFS if $0 + needs to be rebuilt. Thanks to Mike Brennan for pointing this out. + + * awk.h (rebuild_record): Declare. + * eval.c (set_OFS): If not being called from var_init(), check + if $0 needs rebuilding. If so, parse the record fully and rebuild it. + Make OFS point to a separate copy of the new OFS for next time, since + OFS_node->var_value->stptr was already updated at this point. + * field.c (rebuild_record): Is now extern instead of static. + Use OFS and OFSlen instead of the value of OFS_node. + 2014-08-05 Arnold D. Robbins <arnold@skeeve.com> Bug fix: For MPFR sqrt(), need to set precision of result to be @@ -1471,6 +1471,7 @@ extern NODE *get_actual_argument(int, bool, bool); extern void init_fields(void); extern void set_record(const char *buf, int cnt); extern void reset_record(void); +extern void rebuild_record(void); extern void set_NF(void); extern NODE **get_field(long num, Func_ptr *assign); extern NODE *do_split(int nargs); @@ -803,9 +803,35 @@ set_BINMODE() void set_OFS() { + static bool first = true; + size_t new_ofs_len; + + if (first) /* true when called from init_vars() in main() */ + first = false; + else { + /* rebuild $0 using OFS that was current when $0 changed */ + if (! field0_valid) { + get_field(UNLIMITED - 1, NULL); + rebuild_record(); + } + } + + /* + * Save OFS value for use in building record and in printing. + * Can't just have OFS point into the OFS_node since it's + * already updated when we come into this routine, and we need + * the old value to rebuild the record (see above). + */ OFS_node->var_value = force_string(OFS_node->var_value); - OFS = OFS_node->var_value->stptr; - OFSlen = OFS_node->var_value->stlen; + new_ofs_len = OFS_node->var_value->stlen; + + if (OFS == NULL) + emalloc(OFS, char *, new_ofs_len + 2, "set_OFS"); + else if (OFSlen < new_ofs_len) + erealloc(OFS, char *, new_ofs_len + 2, "set_OFS"); + + memcpy(OFS, OFS_node->var_value->stptr, OFS_node->var_value->stlen); + OFSlen = new_ofs_len; OFS[OFSlen] = '\0'; } @@ -40,7 +40,6 @@ typedef void (* Setfunc)(long, char *, long, NODE *); static long (*parse_field)(long, char **, int, NODE *, Regexp *, Setfunc, NODE *, NODE *, bool); -static void rebuild_record(void); static long re_parse_field(long, char **, int, NODE *, Regexp *, Setfunc, NODE *, NODE *, bool); static long def_parse_field(long, char **, int, NODE *, @@ -140,7 +139,7 @@ set_field(long num, /* rebuild_record --- Someone assigned a value to $(something). Fix up $0 to be right */ -static void +void rebuild_record() { /* @@ -148,9 +147,7 @@ rebuild_record() * a size_t isn't big enough. */ unsigned long tlen; - unsigned long ofslen; NODE *tmp; - NODE *ofs; char *ops; char *cops; long i; @@ -158,14 +155,12 @@ rebuild_record() assert(NF != -1); tlen = 0; - ofs = force_string(OFS_node->var_value); - ofslen = ofs->stlen; for (i = NF; i > 0; i--) { tmp = fields_arr[i]; tmp = force_string(tmp); tlen += tmp->stlen; } - tlen += (NF - 1) * ofslen; + tlen += (NF - 1) * OFSlen; if ((long) tlen < 0) tlen = 0; emalloc(ops, char *, tlen + 2, "rebuild_record"); @@ -183,11 +178,11 @@ rebuild_record() } /* copy OFS */ if (i != NF) { - if (ofslen == 1) - *cops++ = ofs->stptr[0]; - else if (ofslen != 0) { - memcpy(cops, ofs->stptr, ofslen); - cops += ofslen; + if (OFSlen == 1) + *cops++ = *OFS; + else if (OFSlen != 0) { + memcpy(cops, OFS, OFSlen); + cops += OFSlen; } } } @@ -231,7 +226,7 @@ rebuild_record() fields_arr[i] = n; assert((n->flags & WSTRCUR) == 0); } - cops += fields_arr[i]->stlen + ofslen; + cops += fields_arr[i]->stlen + OFSlen; } unref(fields_arr[0]); diff --git a/test/ChangeLog b/test/ChangeLog index afbccf5f..d396a049 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,7 @@ +2014-08-12 Arnold D. Robbins <arnold@skeeve.com> + + * ofs1.ok: Updated to match corrected behavior in gawk. + 2014-08-05 Arnold D. Robbins <arnold@skeeve.com> * Makefile.am (mpfrsqrt): New test. diff --git a/test/ofs1.ok b/test/ofs1.ok index a3a8ca7b..d01fa161 100644 --- a/test/ofs1.ok +++ b/test/ofs1.ok @@ -1,7 +1,7 @@ -a:x:c +a x c a x c a x c a -a:x:c a x c -a:x:c +a x c +a x c |