2006-10-03 14:57:01 +00:00
|
|
|
/*
|
2006-10-22 11:34:53 +00:00
|
|
|
* $Id: rec.c,v 1.4 2006-10-22 11:34:53 bacon Exp $
|
2006-10-03 14:57:01 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
#include <sse/awk/awk_i.h>
|
2006-10-03 14:57:01 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
static int __split_record (sse_awk_run_t* run);
|
2006-10-03 14:57:01 +00:00
|
|
|
static int __recomp_record_fields (
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_run_t* run, sse_size_t lv,
|
|
|
|
const sse_char_t* str, sse_size_t len);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
int sse_awk_setrec (
|
|
|
|
sse_awk_run_t* run, sse_size_t idx, const sse_char_t* str, sse_size_t len)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_val_t* v;
|
2006-10-03 14:57:01 +00:00
|
|
|
int errnum;
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (str == SSE_AWK_STR_BUF(&run->inrec.line) &&
|
|
|
|
len == SSE_AWK_STR_LEN(&run->inrec.line))
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_clrrec (run, sse_true) == -1) return -1;
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_clrrec (run, sse_false) == -1) return -1;
|
2006-10-03 14:57:01 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_str_ncpy (&run->inrec.line, str, len) == (sse_size_t)-1)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_clrrec (run, sse_false);
|
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
v = sse_awk_makestrval (run, str, len);
|
|
|
|
if (v == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_clrrec (run, sse_false);
|
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk, run->inrec.d0->type == SSE_AWK_VAL_NIL);
|
2006-10-03 14:57:01 +00:00
|
|
|
/* d0 should be cleared before the next line is reached
|
2006-10-22 11:34:53 +00:00
|
|
|
* as it doesn't call sse_awk_refdownval on run->inrec.d0 */
|
2006-10-03 14:57:01 +00:00
|
|
|
run->inrec.d0 = v;
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_refupval (v);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
if (__split_record (run) == -1)
|
|
|
|
{
|
|
|
|
errnum = run->errnum;
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_clrrec (run, sse_false);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->errnum = errnum;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (__recomp_record_fields (run, idx, str, len) == -1)
|
|
|
|
{
|
|
|
|
errnum = run->errnum;
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_clrrec (run, sse_false);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->errnum = errnum;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recompose $0 */
|
2006-10-22 11:34:53 +00:00
|
|
|
v = sse_awk_makestrval (run,
|
|
|
|
SSE_AWK_STR_BUF(&run->inrec.line),
|
|
|
|
SSE_AWK_STR_LEN(&run->inrec.line));
|
|
|
|
if (v == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_clrrec (run, sse_false);
|
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_refdownval (run, run->inrec.d0);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->inrec.d0 = v;
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_refupval (v);
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
static int __split_record (sse_awk_run_t* run)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_char_t* p, * tok;
|
|
|
|
sse_size_t len, tok_len, nflds;
|
|
|
|
sse_awk_val_t* v, * fs;
|
|
|
|
sse_char_t* fs_ptr, * fs_free;
|
|
|
|
sse_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-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk, run->inrec.nflds == 0);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
/* get FS */
|
2006-10-22 11:34:53 +00:00
|
|
|
fs = sse_awk_getglobal (run, SSE_AWK_GLOBAL_FS);
|
|
|
|
if (fs->type == SSE_AWK_VAL_NIL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
fs_ptr = SSE_T(" ");
|
2006-10-03 14:57:01 +00:00
|
|
|
fs_len = 1;
|
2006-10-22 11:34:53 +00:00
|
|
|
fs_free = SSE_NULL;
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
2006-10-22 11:34:53 +00:00
|
|
|
else if (fs->type == SSE_AWK_VAL_STR)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
fs_ptr = ((sse_awk_val_str_t*)fs)->buf;
|
|
|
|
fs_len = ((sse_awk_val_str_t*)fs)->len;
|
|
|
|
fs_free = SSE_NULL;
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
fs_ptr = sse_awk_valtostr (
|
|
|
|
run, fs, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &fs_len);
|
|
|
|
if (fs_ptr == SSE_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-22 11:34:53 +00:00
|
|
|
p = SSE_AWK_STR_BUF(&run->inrec.line);
|
|
|
|
len = SSE_AWK_STR_LEN(&run->inrec.line);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
nflds = 0;
|
2006-10-22 11:34:53 +00:00
|
|
|
while (p != SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
|
|
|
if (fs_len <= 1)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
p = sse_awk_strxntok (run,
|
2006-10-03 14:57:01 +00:00
|
|
|
p, len, fs_ptr, fs_len, &tok, &tok_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
p = sse_awk_strxntokbyrex (run, p, len,
|
2006-10-03 14:57:01 +00:00
|
|
|
run->global.fs, &tok, &tok_len, &errnum);
|
2006-10-22 11:34:53 +00:00
|
|
|
if (p == SSE_NULL && errnum != SSE_AWK_ENOERR)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (fs_free != SSE_NULL)
|
|
|
|
SSE_AWK_FREE (run->awk, fs_free);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->errnum = errnum;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (nflds == 0 && p == SSE_NULL && tok_len == 0)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
|
|
|
/* there are no fields. it can just return here
|
2006-10-22 11:34:53 +00:00
|
|
|
* as sse_awk_clrrec has been called before this */
|
|
|
|
if (fs_free != SSE_NULL) SSE_AWK_FREE (run->awk, fs_free);
|
2006-10-03 14:57:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk,
|
|
|
|
(tok != SSE_NULL && tok_len > 0) || tok_len == 0);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
nflds++;
|
2006-10-22 11:34:53 +00:00
|
|
|
len = SSE_AWK_STR_LEN(&run->inrec.line) -
|
|
|
|
(p - SSE_AWK_STR_BUF(&run->inrec.line));
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate space */
|
|
|
|
if (nflds > run->inrec.maxflds)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
void* tmp = SSE_AWK_MALLOC (
|
|
|
|
run->awk, sse_sizeof(*run->inrec.flds) * nflds);
|
|
|
|
if (tmp == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (fs_free != SSE_NULL) SSE_AWK_FREE (run->awk, fs_free);
|
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (run->inrec.flds != SSE_NULL)
|
|
|
|
SSE_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-22 11:34:53 +00:00
|
|
|
p = SSE_AWK_STR_BUF(&run->inrec.line);
|
|
|
|
len = SSE_AWK_STR_LEN(&run->inrec.line);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
while (p != SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
|
|
|
if (fs_len <= 1)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
p = sse_awk_strxntok (
|
2006-10-03 14:57:01 +00:00
|
|
|
run, p, len, fs_ptr, fs_len, &tok, &tok_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
p = sse_awk_strxntokbyrex (run, p, len,
|
2006-10-03 14:57:01 +00:00
|
|
|
run->global.fs, &tok, &tok_len, &errnum);
|
2006-10-22 11:34:53 +00:00
|
|
|
if (p == SSE_NULL && errnum != SSE_AWK_ENOERR)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (fs_free != SSE_NULL)
|
|
|
|
SSE_AWK_FREE (run->awk, fs_free);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->errnum = errnum;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk,
|
|
|
|
(tok != SSE_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-22 11:34:53 +00:00
|
|
|
sse_awk_makestrval (run, tok, tok_len);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (run->inrec.flds[run->inrec.nflds].val == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (fs_free != SSE_NULL) SSE_AWK_FREE (run->awk, fs_free);
|
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_refupval (run->inrec.flds[run->inrec.nflds].val);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->inrec.nflds++;
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
len = SSE_AWK_STR_LEN(&run->inrec.line) -
|
|
|
|
(p - SSE_AWK_STR_BUF(&run->inrec.line));
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (fs_free != SSE_NULL) SSE_AWK_FREE (run->awk, fs_free);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
/* set the number of fields */
|
2006-10-22 11:34:53 +00:00
|
|
|
v = sse_awk_makeintval (run, (sse_long_t)nflds);
|
|
|
|
if (v == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_setglobal (run, SSE_AWK_GLOBAL_NF, v) == -1) return -1;
|
2006-10-03 14:57:01 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk, nflds == run->inrec.nflds);
|
2006-10-03 14:57:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
int sse_awk_clrrec (sse_awk_run_t* run, sse_bool_t skip_inrec_line)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_size_t i;
|
2006-10-03 14:57:01 +00:00
|
|
|
int n = 0;
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (run->inrec.d0 != sse_awk_val_nil)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_refdownval (run, run->inrec.d0);
|
|
|
|
run->inrec.d0 = sse_awk_val_nil;
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (run->inrec.nflds > 0)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk, run->inrec.flds != SSE_NULL);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
for (i = 0; i < run->inrec.nflds; i++)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk,
|
|
|
|
run->inrec.flds[i].val != SSE_NULL);
|
|
|
|
sse_awk_refdownval (run, run->inrec.flds[i].val);
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
run->inrec.nflds = 0;
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_setglobal (
|
|
|
|
run, SSE_AWK_GLOBAL_NF, sse_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-22 11:34:53 +00:00
|
|
|
sse_awk_assert (run->awk, run->inrec.nflds == 0);
|
|
|
|
if (!skip_inrec_line) sse_awk_str_clear (&run->inrec.line);
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __recomp_record_fields (
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_run_t* run, sse_size_t lv,
|
|
|
|
const sse_char_t* str, sse_size_t len)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_val_t* v;
|
|
|
|
sse_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-22 11:34:53 +00:00
|
|
|
sse_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-10-22 11:34:53 +00:00
|
|
|
if (run->awk->syscas.realloc != SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
tmp = SSE_AWK_REALLOC (
|
2006-10-03 14:57:01 +00:00
|
|
|
run->awk, run->inrec.flds,
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_sizeof(*run->inrec.flds) * max);
|
|
|
|
if (tmp == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
tmp = SSE_AWK_MALLOC (
|
|
|
|
run->awk, sse_sizeof(*run->inrec.flds) * max);
|
|
|
|
if (tmp == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-22 11:34:53 +00:00
|
|
|
if (run->inrec.flds != SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
SSE_AWK_MEMCPY (run->awk, tmp, run->inrec.flds,
|
|
|
|
sse_sizeof(*run->inrec.flds) * run->inrec.maxflds);
|
|
|
|
SSE_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-22 11:34:53 +00:00
|
|
|
sse_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-22 11:34:53 +00:00
|
|
|
if (sse_awk_str_ncat (
|
2006-10-03 14:57:01 +00:00
|
|
|
&run->inrec.line,
|
|
|
|
run->global.ofs.ptr,
|
2006-10-22 11:34:53 +00:00
|
|
|
run->global.ofs.len) == (sse_size_t)-1)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == lv)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_val_t* tmp;
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
run->inrec.flds[i].ptr =
|
2006-10-22 11:34:53 +00:00
|
|
|
SSE_AWK_STR_BUF(&run->inrec.line) +
|
|
|
|
SSE_AWK_STR_LEN(&run->inrec.line);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->inrec.flds[i].len = len;
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_str_ncat (
|
|
|
|
&run->inrec.line, str, len) == (sse_size_t)-1)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
tmp = sse_awk_makestrval (run, str,len);
|
|
|
|
if (tmp == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < nflds)
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_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-10-22 11:34:53 +00:00
|
|
|
sse_awk_refupval (tmp);
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
else if (i >= nflds)
|
|
|
|
{
|
|
|
|
run->inrec.flds[i].ptr =
|
2006-10-22 11:34:53 +00:00
|
|
|
SSE_AWK_STR_BUF(&run->inrec.line) +
|
|
|
|
SSE_AWK_STR_LEN(&run->inrec.line);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->inrec.flds[i].len = 0;
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_str_cat (
|
|
|
|
&run->inrec.line, SSE_T("")) == (sse_size_t)-1)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
/* sse_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-22 11:34:53 +00:00
|
|
|
/*sse_awk_refdownval (run, run->inrec.flds[i].val);*/
|
|
|
|
run->inrec.flds[i].val = sse_awk_val_zls;
|
|
|
|
sse_awk_refupval (sse_awk_val_zls);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->inrec.nflds++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_val_str_t* tmp;
|
2006-10-03 14:57:01 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
tmp = (sse_awk_val_str_t*)run->inrec.flds[i].val;
|
2006-10-03 14:57:01 +00:00
|
|
|
|
|
|
|
run->inrec.flds[i].ptr =
|
2006-10-22 11:34:53 +00:00
|
|
|
SSE_AWK_STR_BUF(&run->inrec.line) +
|
|
|
|
SSE_AWK_STR_LEN(&run->inrec.line);
|
2006-10-03 14:57:01 +00:00
|
|
|
run->inrec.flds[i].len = tmp->len;
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_str_ncat (&run->inrec.line,
|
|
|
|
tmp->buf, tmp->len) == (sse_size_t)-1)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
v = sse_awk_getglobal (run, SSE_AWK_GLOBAL_NF);
|
|
|
|
sse_awk_assert (run->awk, v->type == SSE_AWK_VAL_INT);
|
|
|
|
if (((sse_awk_val_int_t*)v)->val != max)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
v = sse_awk_makeintval (run, (sse_long_t)max);
|
|
|
|
if (v == SSE_NULL)
|
2006-10-03 14:57:01 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
run->errnum = SSE_AWK_ENOMEM;
|
2006-10-03 14:57:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_setglobal (
|
|
|
|
run, SSE_AWK_GLOBAL_NF, v) == -1) return -1;
|
2006-10-03 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|