qse/ase/awk/rec.c

453 lines
10 KiB
C
Raw Normal View History

2006-10-03 14:57:01 +00:00
/*
2007-01-02 12:25:18 +00:00
* $Id: rec.c,v 1.12 2007-01-02 12:25:18 bacon Exp $
2006-10-03 14:57:01 +00:00
*/
2006-10-24 04:10:12 +00:00
#include <ase/awk/awk_i.h>
2006-10-03 14:57:01 +00:00
2006-10-24 04:10:12 +00:00
static int __split_record (ase_awk_run_t* run);
2006-10-03 14:57:01 +00:00
static int __recomp_record_fields (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_size_t lv,
const ase_char_t* str, ase_size_t len);
2006-10-03 14:57:01 +00:00
2006-10-24 04:10:12 +00:00
int ase_awk_setrec (
2006-12-04 07:17:13 +00:00
ase_awk_run_t* run, ase_size_t idx,
const ase_char_t* str, ase_size_t len)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v;
2006-10-03 14:57:01 +00:00
int errnum;
if (idx == 0)
{
2006-10-24 04:10:12 +00:00
if (str == ASE_AWK_STR_BUF(&run->inrec.line) &&
len == ASE_AWK_STR_LEN(&run->inrec.line))
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
if (ase_awk_clrrec (run, ase_true) == -1) return -1;
2006-10-03 14:57:01 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
if (ase_awk_clrrec (run, ase_false) == -1) return -1;
2006-10-03 14:57:01 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_str_ncpy (&run->inrec.line, str, len) == (ase_size_t)-1)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_clrrec (run, ase_false);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
v = ase_awk_makestrval (run, str, len);
if (v == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_clrrec (run, ase_false);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk, run->inrec.d0->type == ASE_AWK_VAL_NIL);
2006-10-03 14:57:01 +00:00
/* d0 should be cleared before the next line is reached
2006-10-24 04:10:12 +00:00
* as it doesn't call ase_awk_refdownval on run->inrec.d0 */
2006-10-03 14:57:01 +00:00
run->inrec.d0 = v;
2006-11-16 11:53:16 +00:00
ase_awk_refupval (run, v);
2006-10-03 14:57:01 +00:00
if (__split_record (run) == -1)
{
errnum = run->errnum;
2006-10-24 04:10:12 +00:00
ase_awk_clrrec (run, ase_false);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, errnum, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
}
else
{
if (__recomp_record_fields (run, idx, str, len) == -1)
{
errnum = run->errnum;
2006-10-24 04:10:12 +00:00
ase_awk_clrrec (run, ase_false);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, errnum, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
/* recompose $0 */
2006-10-24 04:10:12 +00:00
v = ase_awk_makestrval (run,
ASE_AWK_STR_BUF(&run->inrec.line),
ASE_AWK_STR_LEN(&run->inrec.line));
if (v == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_clrrec (run, ase_false);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, run->inrec.d0);
2006-10-03 14:57:01 +00:00
run->inrec.d0 = v;
2006-11-16 11:53:16 +00:00
ase_awk_refupval (run, v);
2006-10-03 14:57:01 +00:00
}
return 0;
}
2006-10-24 04:10:12 +00:00
static int __split_record (ase_awk_run_t* run)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* p, * tok;
ase_size_t len, tok_len, nflds;
ase_awk_val_t* v, * fs;
ase_char_t* fs_ptr, * fs_free;
ase_size_t fs_len;
2006-10-03 14:57:01 +00:00
int errnum;
/* inrec should be cleared before __split_record is called */
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk, run->inrec.nflds == 0);
2006-10-03 14:57:01 +00:00
/* get FS */
2006-10-24 04:10:12 +00:00
fs = ase_awk_getglobal (run, ASE_AWK_GLOBAL_FS);
if (fs->type == ASE_AWK_VAL_NIL)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
fs_ptr = ASE_T(" ");
2006-10-03 14:57:01 +00:00
fs_len = 1;
2006-10-24 04:10:12 +00:00
fs_free = ASE_NULL;
2006-10-03 14:57:01 +00:00
}
2006-10-24 04:10:12 +00:00
else if (fs->type == ASE_AWK_VAL_STR)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
fs_ptr = ((ase_awk_val_str_t*)fs)->buf;
fs_len = ((ase_awk_val_str_t*)fs)->len;
fs_free = ASE_NULL;
2006-10-03 14:57:01 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
fs_ptr = ase_awk_valtostr (
run, fs, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &fs_len);
if (fs_ptr == ASE_NULL) return -1;
2006-10-03 14:57:01 +00:00
fs_free = fs_ptr;
}
/* scan the input record to count the fields */
2006-10-24 04:10:12 +00:00
p = ASE_AWK_STR_BUF(&run->inrec.line);
len = ASE_AWK_STR_LEN(&run->inrec.line);
2006-10-03 14:57:01 +00:00
nflds = 0;
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-10-03 14:57:01 +00:00
{
if (fs_len <= 1)
{
2006-10-24 04:10:12 +00:00
p = ase_awk_strxntok (run,
2006-10-03 14:57:01 +00:00
p, len, fs_ptr, fs_len, &tok, &tok_len);
}
else
{
2006-10-24 04:10:12 +00:00
p = ase_awk_strxntokbyrex (run, p, len,
2006-10-03 14:57:01 +00:00
run->global.fs, &tok, &tok_len, &errnum);
2006-10-24 04:10:12 +00:00
if (p == ASE_NULL && errnum != ASE_AWK_ENOERR)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
if (fs_free != ASE_NULL)
ASE_AWK_FREE (run->awk, fs_free);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, errnum, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
if (nflds == 0 && p == ASE_NULL && tok_len == 0)
2006-10-03 14:57:01 +00:00
{
/* there are no fields. it can just return here
2006-10-24 04:10:12 +00:00
* as ase_awk_clrrec has been called before this */
if (fs_free != ASE_NULL) ASE_AWK_FREE (run->awk, fs_free);
2006-10-03 14:57:01 +00:00
return 0;
}
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk,
2006-10-24 04:10:12 +00:00
(tok != ASE_NULL && tok_len > 0) || tok_len == 0);
2006-10-03 14:57:01 +00:00
nflds++;
2006-10-24 04:10:12 +00:00
len = ASE_AWK_STR_LEN(&run->inrec.line) -
(p - ASE_AWK_STR_BUF(&run->inrec.line));
2006-10-03 14:57:01 +00:00
}
/* allocate space */
if (nflds > run->inrec.maxflds)
{
2006-10-24 04:10:12 +00:00
void* tmp = ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
run->awk, ASE_SIZEOF(*run->inrec.flds) * nflds);
2006-10-24 04:10:12 +00:00
if (tmp == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
if (fs_free != ASE_NULL) ASE_AWK_FREE (run->awk, fs_free);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (run->inrec.flds != ASE_NULL)
ASE_AWK_FREE (run->awk, run->inrec.flds);
2006-10-03 14:57:01 +00:00
run->inrec.flds = tmp;
run->inrec.maxflds = nflds;
}
/* scan again and split it */
2006-10-24 04:10:12 +00:00
p = ASE_AWK_STR_BUF(&run->inrec.line);
len = ASE_AWK_STR_LEN(&run->inrec.line);
2006-10-03 14:57:01 +00:00
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-10-03 14:57:01 +00:00
{
if (fs_len <= 1)
{
2006-10-24 04:10:12 +00:00
p = ase_awk_strxntok (
2006-10-03 14:57:01 +00:00
run, p, len, fs_ptr, fs_len, &tok, &tok_len);
}
else
{
2006-10-24 04:10:12 +00:00
p = ase_awk_strxntokbyrex (run, p, len,
2006-10-03 14:57:01 +00:00
run->global.fs, &tok, &tok_len, &errnum);
2006-10-24 04:10:12 +00:00
if (p == ASE_NULL && errnum != ASE_AWK_ENOERR)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
if (fs_free != ASE_NULL)
ASE_AWK_FREE (run->awk, fs_free);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, errnum, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
}
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk,
2006-10-24 04:10:12 +00:00
(tok != ASE_NULL && tok_len > 0) || tok_len == 0);
2006-10-03 14:57:01 +00:00
run->inrec.flds[run->inrec.nflds].ptr = tok;
run->inrec.flds[run->inrec.nflds].len = tok_len;
run->inrec.flds[run->inrec.nflds].val =
2006-10-24 04:10:12 +00:00
ase_awk_makestrval (run, tok, tok_len);
2006-10-03 14:57:01 +00:00
2006-10-24 04:10:12 +00:00
if (run->inrec.flds[run->inrec.nflds].val == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
if (fs_free != ASE_NULL) ASE_AWK_FREE (run->awk, fs_free);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-11-16 11:53:16 +00:00
ase_awk_refupval (run, run->inrec.flds[run->inrec.nflds].val);
2006-10-03 14:57:01 +00:00
run->inrec.nflds++;
2006-10-24 04:10:12 +00:00
len = ASE_AWK_STR_LEN(&run->inrec.line) -
(p - ASE_AWK_STR_BUF(&run->inrec.line));
2006-10-03 14:57:01 +00:00
}
2006-10-24 04:10:12 +00:00
if (fs_free != ASE_NULL) ASE_AWK_FREE (run->awk, fs_free);
2006-10-03 14:57:01 +00:00
/* set the number of fields */
2006-10-24 04:10:12 +00:00
v = ase_awk_makeintval (run, (ase_long_t)nflds);
if (v == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (run, ASE_AWK_GLOBAL_NF, v) == -1) return -1;
2006-10-03 14:57:01 +00:00
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk, nflds == run->inrec.nflds);
2006-10-03 14:57:01 +00:00
return 0;
}
2006-10-24 04:10:12 +00:00
int ase_awk_clrrec (ase_awk_run_t* run, ase_bool_t skip_inrec_line)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_size_t i;
2006-10-03 14:57:01 +00:00
int n = 0;
2006-10-24 04:10:12 +00:00
if (run->inrec.d0 != ase_awk_val_nil)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = ase_awk_val_nil;
2006-10-03 14:57:01 +00:00
}
if (run->inrec.nflds > 0)
{
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk, run->inrec.flds != ASE_NULL);
2006-10-03 14:57:01 +00:00
for (i = 0; i < run->inrec.nflds; i++)
{
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk,
2006-10-24 04:10:12 +00:00
run->inrec.flds[i].val != ASE_NULL);
ase_awk_refdownval (run, run->inrec.flds[i].val);
2006-10-03 14:57:01 +00:00
}
run->inrec.nflds = 0;
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (
run, ASE_AWK_GLOBAL_NF, ase_awk_val_zero) == -1)
2006-10-03 14:57:01 +00:00
{
/* first of all, this should never happen.
* if it happened, it would return an error
* after all the clearance tasks */
n = -1;
}
}
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk, run->inrec.nflds == 0);
2006-10-24 04:10:12 +00:00
if (!skip_inrec_line) ase_awk_str_clear (&run->inrec.line);
2006-10-03 14:57:01 +00:00
return n;
}
static int __recomp_record_fields (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_size_t lv,
const ase_char_t* str, ase_size_t len)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v;
ase_size_t max, i, nflds;
2006-10-03 14:57:01 +00:00
/* recomposes the record and the fields when $N has been assigned
* a new value and recomputes NF accordingly */
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk, lv > 0);
2006-10-03 14:57:01 +00:00
max = (lv > run->inrec.nflds)? lv: run->inrec.nflds;
nflds = run->inrec.nflds;
if (max > run->inrec.maxflds)
{
void* tmp;
/* if the given field number is greater than the maximum
* number of fields that the current record can hold,
* the field spaces are resized */
2006-12-13 14:13:07 +00:00
if (run->awk->sysfns.realloc != ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
tmp = ASE_AWK_REALLOC (
2006-10-03 14:57:01 +00:00
run->awk, run->inrec.flds,
2006-11-29 02:54:17 +00:00
ASE_SIZEOF(*run->inrec.flds) * max);
2006-10-24 04:10:12 +00:00
if (tmp == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
}
else
{
2006-10-24 04:10:12 +00:00
tmp = ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
run->awk, ASE_SIZEOF(*run->inrec.flds) * max);
2006-10-24 04:10:12 +00:00
if (tmp == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (run->inrec.flds != ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_MEMCPY (run->awk, tmp, run->inrec.flds,
2006-11-29 02:54:17 +00:00
ASE_SIZEOF(*run->inrec.flds) * run->inrec.maxflds);
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->inrec.flds);
2006-10-03 14:57:01 +00:00
}
}
run->inrec.flds = tmp;
run->inrec.maxflds = max;
}
lv = lv - 1; /* adjust the value to 0-based index */
2006-10-24 04:10:12 +00:00
ase_awk_str_clear (&run->inrec.line);
2006-10-03 14:57:01 +00:00
for (i = 0; i < max; i++)
{
if (i > 0)
{
2006-10-24 04:10:12 +00:00
if (ase_awk_str_ncat (
2006-10-03 14:57:01 +00:00
&run->inrec.line,
run->global.ofs.ptr,
2006-10-24 04:10:12 +00:00
run->global.ofs.len) == (ase_size_t)-1)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
}
if (i == lv)
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-10-03 14:57:01 +00:00
run->inrec.flds[i].ptr =
2006-10-24 04:10:12 +00:00
ASE_AWK_STR_BUF(&run->inrec.line) +
ASE_AWK_STR_LEN(&run->inrec.line);
2006-10-03 14:57:01 +00:00
run->inrec.flds[i].len = len;
2006-10-24 04:10:12 +00:00
if (ase_awk_str_ncat (
&run->inrec.line, str, len) == (ase_size_t)-1)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makestrval (run, str,len);
if (tmp == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
if (i < nflds)
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, run->inrec.flds[i].val);
2006-10-03 14:57:01 +00:00
else run->inrec.nflds++;
run->inrec.flds[i].val = tmp;
2006-11-16 11:53:16 +00:00
ase_awk_refupval (run, tmp);
2006-10-03 14:57:01 +00:00
}
else if (i >= nflds)
{
run->inrec.flds[i].ptr =
2006-10-24 04:10:12 +00:00
ASE_AWK_STR_BUF(&run->inrec.line) +
ASE_AWK_STR_LEN(&run->inrec.line);
2006-10-03 14:57:01 +00:00
run->inrec.flds[i].len = 0;
2006-10-24 04:10:12 +00:00
if (ase_awk_str_cat (
&run->inrec.line, ASE_T("")) == (ase_size_t)-1)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
/* ase_awk_refdownval should not be called over
2006-10-03 14:57:01 +00:00
* run->inrec.flds[i].val as it is not initialized
* to any valid values */
2006-10-24 04:10:12 +00:00
/*ase_awk_refdownval (run, run->inrec.flds[i].val);*/
run->inrec.flds[i].val = ase_awk_val_zls;
2006-11-16 11:53:16 +00:00
ase_awk_refupval (run, ase_awk_val_zls);
2006-10-03 14:57:01 +00:00
run->inrec.nflds++;
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_val_str_t* tmp;
2006-10-03 14:57:01 +00:00
2006-10-24 04:10:12 +00:00
tmp = (ase_awk_val_str_t*)run->inrec.flds[i].val;
2006-10-03 14:57:01 +00:00
run->inrec.flds[i].ptr =
2006-10-24 04:10:12 +00:00
ASE_AWK_STR_BUF(&run->inrec.line) +
ASE_AWK_STR_LEN(&run->inrec.line);
2006-10-03 14:57:01 +00:00
run->inrec.flds[i].len = tmp->len;
2006-10-24 04:10:12 +00:00
if (ase_awk_str_ncat (&run->inrec.line,
tmp->buf, tmp->len) == (ase_size_t)-1)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
}
}
2006-10-24 04:10:12 +00:00
v = ase_awk_getglobal (run, ASE_AWK_GLOBAL_NF);
2006-10-26 09:31:28 +00:00
ASE_AWK_ASSERT (run->awk, v->type == ASE_AWK_VAL_INT);
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_int_t*)v)->val != max)
2006-10-03 14:57:01 +00:00
{
2006-10-24 04:10:12 +00:00
v = ase_awk_makeintval (run, (ase_long_t)max);
if (v == ASE_NULL)
2006-10-03 14:57:01 +00:00
{
2007-01-02 12:25:18 +00:00
ase_awk_setrunerror (run, ASE_AWK_ENOMEM, 0, ASE_NULL);
2006-10-03 14:57:01 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (
run, ASE_AWK_GLOBAL_NF, v) == -1) return -1;
2006-10-03 14:57:01 +00:00
}
return 0;
}