touched up code for less lines

This commit is contained in:
hyung-hwan 2011-05-19 20:50:51 +00:00
parent c7a49615a2
commit 6eebf4baa2
4 changed files with 136 additions and 233 deletions

View File

@ -266,7 +266,9 @@ pio1 (QSE_T("ls -laF"), QSE_PIO_READOUT|QSE_PIO_WRITEIN|/*QSE_PIO_SHELL|*/QSE_PI
{ {
qse_printf (QSE_T("sizeof(int) = %d\n"), (int)sizeof(int)); qse_printf (QSE_T("sizeof(int) = %d\n"), (int)sizeof(int));
qse_printf (QSE_T("sizeof(long) = %d\n"), (int)sizeof(long)); qse_printf (QSE_T("sizeof(long) = %d\n"), (int)sizeof(long));
#if QSE_SIZEOF_LONG_LONG>0
qse_printf (QSE_T("sizeof(long long) = %d\n"), (int)sizeof(long long)); qse_printf (QSE_T("sizeof(long long) = %d\n"), (int)sizeof(long long));
#endif
qse_printf (QSE_T("sizeof(float) = %d\n"), (int)sizeof(float)); qse_printf (QSE_T("sizeof(float) = %d\n"), (int)sizeof(float));
qse_printf (QSE_T("sizeof(double) = %d\n"), (int)sizeof(double)); qse_printf (QSE_T("sizeof(double) = %d\n"), (int)sizeof(double));
qse_printf (QSE_T("sizeof(long double) = %d\n"), (int)sizeof(long double)); qse_printf (QSE_T("sizeof(long double) = %d\n"), (int)sizeof(long double));

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h 462 2011-05-18 14:36:40Z hyunghwan.chung $ * $Id: awk.h 463 2011-05-19 02:50:51Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -720,7 +720,7 @@ enum qse_awk_option_t
QSE_AWK_NEWLINE = (1 << 5), QSE_AWK_NEWLINE = (1 << 5),
/** /**
* remove empty fields when splitting a record if FS is a regular * removes empty fields when splitting a record if FS is a regular
* expression and the match is all spaces. * expression and the match is all spaces.
* *
* @code * @code
@ -735,13 +735,13 @@ enum qse_awk_option_t
* *
* @code * @code
* BEGIN { * BEGIN {
* n=split(" o my god ", x, /[ o]+/); * n=split(" oh my noodle ", x, /[ o]+/);
* for (i=1;i<=n;i++) print "[" x[i] "]"; * for (i=1;i<=n;i++) print "[" x[i] "]";
* } * }
* @endcode * @endcode
* The above example splits the string to [], [my], [g], [d] * This example splits the string to [], [h], [my], [n], [dle]
* if #QSE_AWK_STRIPRECSPC is on. Otherwise, it results in * if #QSE_AWK_STRIPRECSPC is on. Otherwise, it results in
* [], [my], [g], [d], []. Note that the first empty field is not * [], [h], [my], [n], [dle], []. Note that the first empty field is not
* removed as the field separator is not all spaces. (space + 'o'). * removed as the field separator is not all spaces. (space + 'o').
*/ */
QSE_AWK_STRIPRECSPC = (1 << 6), QSE_AWK_STRIPRECSPC = (1 << 6),

View File

@ -1,5 +1,5 @@
/* /*
* $Id: fnc.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * $Id: fnc.c 463 2011-05-19 02:50:51Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -162,7 +162,7 @@ void* qse_awk_addfnc (
int qse_awk_delfnc ( int qse_awk_delfnc (
qse_awk_t* awk, const qse_char_t* name, qse_size_t name_len) qse_awk_t* awk, const qse_char_t* name, qse_size_t name_len)
{ {
if (qse_htb_delete (awk->fnc.user, name, name_len) == -1) if (qse_htb_delete (awk->fnc.user, name, name_len) <= -1)
{ {
qse_cstr_t errarg; qse_cstr_t errarg;
@ -348,7 +348,7 @@ static int fnc_close (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
n = qse_awk_rtx_closeio (rtx, name, opt); n = qse_awk_rtx_closeio (rtx, name, opt);
/* failure to close is not a critical error. instead, that is /* failure to close is not a critical error. instead, that is
* flagged by the return value of close(). * flagged by the return value of close().
if (n == -1 && rtx->errinf.num != QSE_AWK_EIONMNF) if (n <= -1 && rtx->errinf.num != QSE_AWK_EIONMNF)
{ {
if (a0->type != QSE_AWK_VAL_STR) if (a0->type != QSE_AWK_VAL_STR)
QSE_AWK_FREE (rtx->awk, name); QSE_AWK_FREE (rtx->awk, name);
@ -377,7 +377,7 @@ static int flush_io (
if (run->rio.handler[rio] != QSE_NULL) if (run->rio.handler[rio] != QSE_NULL)
{ {
n2 = qse_awk_rtx_flushio (run, rio, name); n2 = qse_awk_rtx_flushio (run, rio, name);
if (n2 == -1) if (n2 <= -1)
{ {
/* /*
if (run->errinf.num == QSE_AWK_EIOIMPL) n = -1; if (run->errinf.num == QSE_AWK_EIOIMPL) n = -1;
@ -623,7 +623,7 @@ static int fnc_substr (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
else else
{ {
n = qse_awk_rtx_valtonum (rtx, a2, &lcount, &rcount); n = qse_awk_rtx_valtonum (rtx, a2, &lcount, &rcount);
if (n == -1) if (n <= -1)
{ {
if (a0->type != QSE_AWK_VAL_STR) if (a0->type != QSE_AWK_VAL_STR)
QSE_AWK_FREE (rtx->awk, str); QSE_AWK_FREE (rtx->awk, str);
@ -658,16 +658,17 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
{ {
qse_size_t nargs; qse_size_t nargs;
qse_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref; qse_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref;
qse_char_t* str, * str_free, * p;
qse_size_t str_len, str_left, org_len; qse_cstr_t str, fs;
qse_cstr_t tok; qse_char_t* str_free = QSE_NULL, * fs_free = QSE_NULL;
qse_long_t nflds; const qse_char_t* p;
qse_char_t key[QSE_SIZEOF(qse_long_t)*8+2]; qse_size_t str_left, org_len;
qse_size_t key_len;
qse_char_t* fs_ptr, * fs_free;
qse_size_t fs_len;
void* fs_rex = QSE_NULL; void* fs_rex = QSE_NULL;
void* fs_rex_free = QSE_NULL; void* fs_rex_free = QSE_NULL;
qse_cstr_t tok;
qse_long_t nflds;
qse_awk_errnum_t errnum; qse_awk_errnum_t errnum;
nargs = qse_awk_rtx_getnargs (run); nargs = qse_awk_rtx_getnargs (run);
@ -705,15 +706,14 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
if (a0->type == QSE_AWK_VAL_STR) if (a0->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)a0)->val.ptr; str.ptr = ((qse_awk_val_str_t*)a0)->val.ptr;
str_len = ((qse_awk_val_str_t*)a0)->val.len; str.len = ((qse_awk_val_str_t*)a0)->val.len;
str_free = QSE_NULL;
} }
else else
{ {
str = qse_awk_rtx_valtocpldup (run, a0, &str_len); str.ptr = qse_awk_rtx_valtocpldup (run, a0, &str.len);
if (str == QSE_NULL) return -1; if (str.ptr == QSE_NULL) return -1;
str_free = str; str_free = (qse_char_t*)str.ptr;
} }
if (a2 == QSE_NULL) if (a2 == QSE_NULL)
@ -722,126 +722,90 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
t1 = qse_awk_rtx_getgbl (run, QSE_AWK_GBL_FS); t1 = qse_awk_rtx_getgbl (run, QSE_AWK_GBL_FS);
if (t1->type == QSE_AWK_VAL_NIL) if (t1->type == QSE_AWK_VAL_NIL)
{ {
fs_ptr = QSE_T(" "); fs.ptr = QSE_T(" ");
fs_len = 1; fs.len = 1;
fs_free = QSE_NULL;
} }
else if (t1->type == QSE_AWK_VAL_STR) else if (t1->type == QSE_AWK_VAL_STR)
{ {
fs_ptr = ((qse_awk_val_str_t*)t1)->val.ptr; fs.ptr = ((qse_awk_val_str_t*)t1)->val.ptr;
fs_len = ((qse_awk_val_str_t*)t1)->val.len; fs.len = ((qse_awk_val_str_t*)t1)->val.len;
fs_free = QSE_NULL;
} }
else else
{ {
fs_ptr = qse_awk_rtx_valtocpldup (run, t1, &fs_len); fs.ptr = qse_awk_rtx_valtocpldup (run, t1, &fs.len);
if (fs_ptr == QSE_NULL) if (fs.ptr == QSE_NULL) goto oops;
{ fs_free = (qse_char_t*)fs.ptr;
if (str_free != QSE_NULL)
QSE_AWK_FREE (run->awk, str_free);
return -1;
}
fs_free = fs_ptr;
} }
if (fs_len > 1) if (fs.len > 1) fs_rex = run->gbl.fs;
{
fs_rex = run->gbl.fs;
fs_rex_free = QSE_NULL;
}
} }
else if (a2->type == QSE_AWK_VAL_REX) else if (a2->type == QSE_AWK_VAL_REX)
{ {
/* the third parameter is a regular expression */ /* the third parameter is a regular expression */
fs_rex = ((qse_awk_val_rex_t*)a2)->code; fs_rex = ((qse_awk_val_rex_t*)a2)->code;
fs_rex_free = QSE_NULL;
/* make the loop below to take fs_rex by /* make the loop below to take fs_rex by
* setting fs_len greater than 1*/ * setting fs_len greater than 1*/
fs_ptr = QSE_NULL; fs.ptr = QSE_NULL;
fs_free = QSE_NULL; fs.len = 2;
fs_len = 2;
} }
else else
{ {
if (a2->type == QSE_AWK_VAL_STR) if (a2->type == QSE_AWK_VAL_STR)
{ {
fs_ptr = ((qse_awk_val_str_t*)a2)->val.ptr; fs.ptr = ((qse_awk_val_str_t*)a2)->val.ptr;
fs_len = ((qse_awk_val_str_t*)a2)->val.len; fs.len = ((qse_awk_val_str_t*)a2)->val.len;
fs_free = QSE_NULL;
} }
else else
{ {
fs_ptr = qse_awk_rtx_valtocpldup (run, a2, &fs_len); fs.ptr = qse_awk_rtx_valtocpldup (run, a2, &fs.len);
if (fs_ptr == QSE_NULL) if (fs.ptr == QSE_NULL) goto oops;
{ fs_free = (qse_char_t*)fs.ptr;
if (str_free != QSE_NULL)
QSE_AWK_FREE (run->awk, str_free);
return -1;
}
fs_free = fs_ptr;
} }
if (fs_len > 1) if (fs.len > 1)
{ {
fs_rex = QSE_AWK_BUILDREX ( fs_rex = QSE_AWK_BUILDREX (
run->awk, fs_ptr, fs_len, &errnum); run->awk, fs.ptr, fs.len, &errnum);
if (fs_rex == QSE_NULL) if (fs_rex == QSE_NULL)
{ {
if (str_free != QSE_NULL)
QSE_AWK_FREE (run->awk, str_free);
if (fs_free != QSE_NULL)
QSE_AWK_FREE (run->awk, fs_free);
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL); qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
return -1; goto oops;
} }
fs_rex_free = fs_rex; fs_rex_free = fs_rex;
} }
} }
t1 = qse_awk_rtx_makemapval (run); t1 = qse_awk_rtx_makemapval (run);
if (t1 == QSE_NULL) if (t1 == QSE_NULL) goto oops;
{
if (str_free != QSE_NULL)
QSE_AWK_FREE (run->awk, str_free);
if (fs_free != QSE_NULL)
QSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != QSE_NULL)
QSE_AWK_FREEREX (run->awk, fs_rex_free);
return -1;
}
qse_awk_rtx_refdownval (run, *a1_ref); qse_awk_rtx_refdownval (run, *a1_ref);
*a1_ref = t1; *a1_ref = t1;
qse_awk_rtx_refupval (run, *a1_ref); qse_awk_rtx_refupval (run, *a1_ref);
p = str; str_left = str_len; org_len = str_len; p = str.ptr; str_left = str.len; org_len = str.len;
nflds = 0; nflds = 0;
while (p != QSE_NULL) while (p != QSE_NULL)
{ {
if (fs_len <= 1) qse_char_t key_buf[QSE_SIZEOF(qse_long_t)*8+2];
qse_size_t key_len;
if (fs.len <= 1)
{ {
p = qse_awk_rtx_strxntok (run, p = qse_awk_rtx_strxntok (run,
p, str_len, fs_ptr, fs_len, &tok); p, str.len, fs.ptr, fs.len, &tok);
} }
else else
{ {
p = qse_awk_rtx_strxntokbyrex ( p = qse_awk_rtx_strxntokbyrex (
run, str, org_len, p, str_len, run, str.ptr, org_len, p, str.len,
fs_rex, &tok, &errnum fs_rex, &tok, &errnum
); );
if (p == QSE_NULL && errnum != QSE_AWK_ENOERR) if (p == QSE_NULL && errnum != QSE_AWK_ENOERR)
{ {
if (str_free != QSE_NULL)
QSE_AWK_FREE (run->awk, str_free);
if (fs_free != QSE_NULL)
QSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != QSE_NULL)
QSE_AWK_FREEREX (run->awk, fs_rex_free);
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL); qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
return -1; goto oops;
} }
} }
@ -855,55 +819,27 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
/* create the field string */ /* create the field string */
t2 = qse_awk_rtx_makestrval (run, tok.ptr, tok.len); t2 = qse_awk_rtx_makestrval (run, tok.ptr, tok.len);
if (t2 == QSE_NULL) if (t2 == QSE_NULL) goto oops;
{
if (str_free != QSE_NULL)
QSE_AWK_FREE (run->awk, str_free);
if (fs_free != QSE_NULL)
QSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != QSE_NULL)
QSE_AWK_FREEREX (run->awk, fs_rex_free);
return -1;
}
/* put it into the map */ /* put it into the map */
key_len = qse_awk_longtostr ( key_len = qse_awk_longtostr (
run->awk, ++nflds, 10, QSE_NULL, key, QSE_COUNTOF(key)); run->awk, ++nflds, 10, QSE_NULL, key_buf, QSE_COUNTOF(key_buf));
QSE_ASSERT (key_len != (qse_size_t)-1); QSE_ASSERT (key_len != (qse_size_t)-1);
/* don't forget to update the reference count when you if (qse_awk_rtx_setmapvalfld (
* handle the assignment-like situation. anyway, it is run, t1, key_buf, key_len, t2) == QSE_NULL)
* incremented in advance as if the assignment was successful.
* it is decremented if the assignement fails. */
qse_awk_rtx_refupval (run, t2);
if (qse_htb_insert (
((qse_awk_val_map_t*)t1)->map,
key, key_len, t2, 0) == QSE_NULL)
{ {
/* assignment failed. restore the reference counter */ qse_awk_rtx_refupval (run, t2);
qse_awk_rtx_refdownval (run, t2); qse_awk_rtx_refdownval (run, t2);
goto oops;
if (str_free != QSE_NULL)
QSE_AWK_FREE (run->awk, str_free);
if (fs_free != QSE_NULL)
QSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != QSE_NULL)
QSE_AWK_FREEREX (run->awk, fs_rex_free);
/* qse_htb_insert() fails if the key exists.
* that can't happen here. so set the error code
* to ENOMEM */
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
} }
str_len = str_left - (p - str); str.len = str_left - (p - str.ptr);
} }
if (str_free != QSE_NULL) QSE_AWK_FREE (run->awk, str_free); if (str_free) QSE_AWK_FREE (run->awk, str_free);
if (fs_free != QSE_NULL) QSE_AWK_FREE (run->awk, fs_free); if (fs_free) QSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != QSE_NULL) QSE_AWK_FREEREX (run->awk, fs_rex_free); if (fs_rex_free) QSE_AWK_FREEREX (run->awk, fs_rex_free);
/*nflds--;*/ /*nflds--;*/
@ -912,6 +848,12 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
qse_awk_rtx_setretval (run, t1); qse_awk_rtx_setretval (run, t1);
return 0; return 0;
oops:
if (str_free) QSE_AWK_FREE (run->awk, str_free);
if (fs_free) QSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free) QSE_AWK_FREEREX (run->awk, fs_rex_free);
return -1;
} }
static int fnc_tolower (qse_awk_rtx_t* run, const qse_cstr_t* fnm) static int fnc_tolower (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
@ -995,15 +937,19 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
qse_cstr_t s0, s1, s2; qse_cstr_t s0, s1, s2;
const qse_char_t* s2_end; const qse_char_t* s2_end;
qse_char_t* s0_free = QSE_NULL; qse_char_t* s0_free = QSE_NULL;
qse_char_t* s1_free = QSE_NULL; qse_char_t* s1_free = QSE_NULL;
qse_char_t* s2_free = QSE_NULL; qse_char_t* s2_free = QSE_NULL;
void* rex = QSE_NULL; void* rex = QSE_NULL;
int opt, n; void* rex_free = QSE_NULL;
qse_cstr_t mat, pmat, cur;
qse_size_t i, m;
qse_str_t new; qse_str_t new;
int new_inited = 0, opt;
qse_cstr_t mat, pmat, cur;
qse_long_t sub_count; qse_long_t sub_count;
nargs = qse_awk_rtx_getnargs (run); nargs = qse_awk_rtx_getnargs (run);
@ -1015,17 +961,6 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
QSE_ASSERT (a2 == QSE_NULL || a2->type == QSE_AWK_VAL_REF); QSE_ASSERT (a2 == QSE_NULL || a2->type == QSE_AWK_VAL_REF);
#define FREE_A_PTRS(awk) \
do { \
if (s2_free != QSE_NULL) QSE_AWK_FREE (awk, s2_free); \
if (s1_free != QSE_NULL) QSE_AWK_FREE (awk, s1_free); \
if (s0_free != QSE_NULL) QSE_AWK_FREE (awk, s0_free); \
} while (0)
#define FREE_A0_REX(awk,rex) \
do { \
if (a0->type != QSE_AWK_VAL_REX) QSE_AWK_FREEREX (awk, rex); \
} while (0)
if (a0->type == QSE_AWK_VAL_REX) if (a0->type == QSE_AWK_VAL_REX)
{ {
rex = ((qse_awk_val_rex_t*)a0)->code; rex = ((qse_awk_val_rex_t*)a0)->code;
@ -1038,11 +973,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
else else
{ {
s0.ptr = qse_awk_rtx_valtocpldup (run, a0, &s0.len); s0.ptr = qse_awk_rtx_valtocpldup (run, a0, &s0.len);
if (s0.ptr == QSE_NULL) if (s0.ptr == QSE_NULL) goto oops;
{
FREE_A_PTRS (run->awk);
return -1;
}
s0_free = (qse_char_t*)s0.ptr; s0_free = (qse_char_t*)s0.ptr;
} }
@ -1054,11 +985,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
else else
{ {
s1.ptr = qse_awk_rtx_valtocpldup (run, a1, &s1.len); s1.ptr = qse_awk_rtx_valtocpldup (run, a1, &s1.len);
if (s1.ptr == QSE_NULL) if (s1.ptr == QSE_NULL) goto oops;
{
FREE_A_PTRS (run->awk);
return -1;
}
s1_free = (qse_char_t*)s1.ptr; s1_free = (qse_char_t*)s1.ptr;
} }
@ -1095,10 +1022,9 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
if ((*a2_ref)->type == QSE_AWK_VAL_MAP) if ((*a2_ref)->type == QSE_AWK_VAL_MAP)
{ {
FREE_A_PTRS (run->awk);
/* a map is not allowed as the third parameter */ /* a map is not allowed as the third parameter */
qse_awk_rtx_seterrnum (run, QSE_AWK_EMAPNA, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_EMAPNA, QSE_NULL);
return -1; goto oops;
} }
if ((*a2_ref)->type == QSE_AWK_VAL_STR) if ((*a2_ref)->type == QSE_AWK_VAL_STR)
@ -1109,21 +1035,17 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
else else
{ {
s2.ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &s2.len); s2.ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &s2.len);
if (s2.ptr == QSE_NULL) if (s2.ptr == QSE_NULL) goto oops;
{
FREE_A_PTRS (run->awk);
return -1;
}
s2_free = (qse_char_t*)s2.ptr; s2_free = (qse_char_t*)s2.ptr;
} }
} }
if (qse_str_init (&new, run->awk->mmgr, s2.len) == QSE_NULL) if (qse_str_init (&new, run->awk->mmgr, s2.len) == QSE_NULL)
{ {
FREE_A_PTRS (run->awk);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; goto oops;
} }
new_inited = 1;
if (a0->type != QSE_AWK_VAL_REX) if (a0->type != QSE_AWK_VAL_REX)
{ {
@ -1133,11 +1055,11 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
run->awk, s0.ptr, s0.len, &errnum); run->awk, s0.ptr, s0.len, &errnum);
if (rex == QSE_NULL) if (rex == QSE_NULL)
{ {
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL); qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
return -1; goto oops;
} }
rex_free = rex;
} }
opt = (run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0; opt = (run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0;
@ -1155,6 +1077,8 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
while (cur.ptr <= s2_end) while (cur.ptr <= s2_end)
{ {
qse_awk_errnum_t errnum; qse_awk_errnum_t errnum;
int n;
qse_size_t m, i;
if (max_count == 0 || sub_count < max_count) if (max_count == 0 || sub_count < max_count)
{ {
@ -1167,10 +1091,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
if (n <= -1) if (n <= -1)
{ {
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL); qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
FREE_A0_REX (run->awk, rex); goto oops;
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
return -1;
} }
if (n == 0) if (n == 0)
@ -1179,11 +1100,8 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
if (qse_str_ncat ( if (qse_str_ncat (
&new, cur.ptr, cur.len) == (qse_size_t)-1) &new, cur.ptr, cur.len) == (qse_size_t)-1)
{ {
FREE_A0_REX (run->awk, rex);
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; goto oops;
} }
break; break;
} }
@ -1200,11 +1118,8 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
if (qse_str_ncat ( if (qse_str_ncat (
&new, cur.ptr, mat.ptr - cur.ptr) == (qse_size_t)-1) &new, cur.ptr, mat.ptr - cur.ptr) == (qse_size_t)-1)
{ {
FREE_A0_REX (run->awk, rex);
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; goto oops;
} }
for (i = 0; i < s1.len; i++) for (i = 0; i < s1.len; i++)
@ -1227,11 +1142,8 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
if (m == (qse_size_t)-1) if (m == (qse_size_t)-1)
{ {
FREE_A0_REX (run->awk, rex);
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; goto oops;
} }
} }
@ -1249,30 +1161,28 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
m = qse_str_ncat (&new, cur.ptr, 1); m = qse_str_ncat (&new, cur.ptr, 1);
if (m == (qse_size_t)-1) if (m == (qse_size_t)-1)
{ {
FREE_A0_REX (run->awk, rex);
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; goto oops;
} }
cur.ptr++; cur.len--; cur.ptr++; cur.len--;
} }
} }
FREE_A0_REX (run->awk, rex); if (rex_free)
{
QSE_AWK_FREEREX (run->awk, rex_free);
rex_free = QSE_NULL;
}
if (sub_count > 0) if (sub_count > 0)
{ {
if (a2 == QSE_NULL) if (a2 == QSE_NULL)
{ {
if (qse_awk_rtx_setrec (run, 0, int n;
QSE_STR_PTR(&new), QSE_STR_LEN(&new)) == -1) n = qse_awk_rtx_setrec (run, 0,
{ QSE_STR_PTR(&new), QSE_STR_LEN(&new));
qse_str_fini (&new); if (n <= -1) goto oops;
FREE_A_PTRS (run->awk);
return -1;
}
} }
else if (((qse_awk_val_ref_t*)a2)->id == QSE_AWK_VAL_REF_POS) else if (((qse_awk_val_ref_t*)a2)->id == QSE_AWK_VAL_REF_POS)
{ {
@ -1282,23 +1192,13 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
run, (qse_size_t)((qse_awk_val_ref_t*)a2)->adr, run, (qse_size_t)((qse_awk_val_ref_t*)a2)->adr,
QSE_STR_PTR(&new), QSE_STR_LEN(&new)); QSE_STR_PTR(&new), QSE_STR_LEN(&new));
if (n == -1) if (n <= -1) goto oops;
{
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
return -1;
}
} }
else else
{ {
v = qse_awk_rtx_makestrval (run, v = qse_awk_rtx_makestrval (run,
QSE_STR_PTR(&new), QSE_STR_LEN(&new)); QSE_STR_PTR(&new), QSE_STR_LEN(&new));
if (v == QSE_NULL) if (v == QSE_NULL) goto oops;
{
qse_str_fini (&new);
FREE_A_PTRS (run->awk);
return -1;
}
qse_awk_rtx_refdownval (run, *a2_ref); qse_awk_rtx_refdownval (run, *a2_ref);
*a2_ref = v; *a2_ref = v;
@ -1307,16 +1207,24 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
} }
qse_str_fini (&new); qse_str_fini (&new);
FREE_A_PTRS (run->awk);
#undef FREE_A0_REX if (s2_free) QSE_AWK_FREE (run->awk, s2_free);
#undef FREE_A_PTRS if (s1_free) QSE_AWK_FREE (run->awk, s1_free);
if (s0_free) QSE_AWK_FREE (run->awk, s0_free);
v = qse_awk_rtx_makeintval (run, sub_count); v = qse_awk_rtx_makeintval (run, sub_count);
if (v == QSE_NULL) return -1; if (v == QSE_NULL) return -1;
qse_awk_rtx_setretval (run, v); qse_awk_rtx_setretval (run, v);
return 0; return 0;
oops:
if (rex_free) QSE_AWK_FREEREX (run->awk, rex_free);
if (new_inited) qse_str_fini (&new);
if (s2_free) QSE_AWK_FREE (run->awk, s2_free);
if (s1_free) QSE_AWK_FREE (run->awk, s1_free);
if (s0_free) QSE_AWK_FREE (run->awk, s0_free);
return -1;
} }
static int fnc_gsub (qse_awk_rtx_t* run, const qse_cstr_t* fnm) static int fnc_gsub (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
@ -1465,14 +1373,14 @@ static int fnc_match (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
qse_awk_rtx_refupval (rtx, a1); qse_awk_rtx_refupval (rtx, a1);
if (qse_awk_rtx_setgbl (rtx, QSE_AWK_GBL_RSTART, a0) == -1) if (qse_awk_rtx_setgbl (rtx, QSE_AWK_GBL_RSTART, a0) <= -1)
{ {
qse_awk_rtx_refdownval (rtx, a1); qse_awk_rtx_refdownval (rtx, a1);
qse_awk_rtx_refdownval (rtx, a0); qse_awk_rtx_refdownval (rtx, a0);
return -1; return -1;
} }
if (qse_awk_rtx_setgbl (rtx, QSE_AWK_GBL_RLENGTH, a1) == -1) if (qse_awk_rtx_setgbl (rtx, QSE_AWK_GBL_RLENGTH, a1) <= -1)
{ {
qse_awk_rtx_refdownval (rtx, a1); qse_awk_rtx_refdownval (rtx, a1);
qse_awk_rtx_refdownval (rtx, a0); qse_awk_rtx_refdownval (rtx, a0);
@ -1491,6 +1399,7 @@ static int fnc_sprintf (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
qse_size_t nargs; qse_size_t nargs;
qse_awk_val_t* a0; qse_awk_val_t* a0;
qse_str_t out, fbu; qse_str_t out, fbu;
int out_inited = 0, fbu_inited = 0;
qse_xstr_t cs0; qse_xstr_t cs0;
qse_xstr_t x; qse_xstr_t x;
@ -1500,14 +1409,16 @@ static int fnc_sprintf (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
if (qse_str_init (&out, run->awk->mmgr, 256) == QSE_NULL) if (qse_str_init (&out, run->awk->mmgr, 256) == QSE_NULL)
{ {
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; goto oops;
} }
out_inited = 1;
if (qse_str_init (&fbu, run->awk->mmgr, 256) == QSE_NULL) if (qse_str_init (&fbu, run->awk->mmgr, 256) == QSE_NULL)
{ {
qse_str_fini (&out);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; goto oops;
} }
fbu_inited = 1;
a0 = qse_awk_rtx_getarg (run, 0); a0 = qse_awk_rtx_getarg (run, 0);
if (a0->type == QSE_AWK_VAL_STR) if (a0->type == QSE_AWK_VAL_STR)
@ -1517,38 +1428,28 @@ static int fnc_sprintf (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
else else
{ {
cs0.ptr = qse_awk_rtx_valtocpldup (run, a0, &cs0.len); cs0.ptr = qse_awk_rtx_valtocpldup (run, a0, &cs0.len);
if (cs0.ptr == QSE_NULL) if (cs0.ptr == QSE_NULL) goto oops;
{
qse_str_fini (&fbu);
qse_str_fini (&out);
return -1;
}
} }
x.ptr = qse_awk_rtx_format (run, x.ptr = qse_awk_rtx_format (run,
&out, &fbu, cs0.ptr, cs0.len, nargs, QSE_NULL, &x.len); &out, &fbu, cs0.ptr, cs0.len, nargs, QSE_NULL, &x.len);
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, cs0.ptr); if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, cs0.ptr);
if (x.ptr == QSE_NULL) if (x.ptr == QSE_NULL) goto oops;
{
qse_str_fini (&fbu);
qse_str_fini (&out);
return -1;
}
/*a0 = qse_awk_rtx_makestrval_nodup (run, x.ptr, x.len);*/ /*a0 = qse_awk_rtx_makestrval_nodup (run, x.ptr, x.len);*/
a0 = qse_awk_rtx_makestrval (run, x.ptr, x.len); a0 = qse_awk_rtx_makestrval (run, x.ptr, x.len);
if (a0 == QSE_NULL) if (a0 == QSE_NULL) goto oops;
{
qse_str_fini (&fbu);
qse_str_fini (&out);
return -1;
}
qse_str_fini (&fbu); qse_str_fini (&fbu);
/*qse_str_yield (&out, QSE_NULL, 0);*/ /*qse_str_yield (&out, QSE_NULL, 0);*/
qse_str_fini (&out); qse_str_fini (&out);
qse_awk_rtx_setretval (run, a0); qse_awk_rtx_setretval (run, a0);
return 0; return 0;
oops:
if (fbu_inited) qse_str_fini (&fbu);
if (out_inited) qse_str_fini (&out);
return -1;
} }
static int fnc_math_1 ( static int fnc_math_1 (
@ -1656,7 +1557,7 @@ static int fnc_int (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
a0 = qse_awk_rtx_getarg (run, 0); a0 = qse_awk_rtx_getarg (run, 0);
n = qse_awk_rtx_valtonum (run, a0, &lv, &rv); n = qse_awk_rtx_valtonum (run, a0, &lv, &rv);
if (n == -1) return -1; if (n <= -1) return -1;
if (n == 1) lv = (qse_long_t)rv; if (n == 1) lv = (qse_long_t)rv;
r = qse_awk_rtx_makeintval (run, lv); r = qse_awk_rtx_makeintval (run, lv);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: run.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * $Id: run.c 463 2011-05-19 02:50:51Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -472,7 +472,7 @@ static int set_global (
qse_real_t rv; qse_real_t rv;
n = qse_awk_rtx_valtonum (rtx, val, &lv, &rv); n = qse_awk_rtx_valtonum (rtx, val, &lv, &rv);
if (n == -1) return -1; if (n <= -1) return -1;
if (n == 1) lv = (qse_long_t)rv; if (n == 1) lv = (qse_long_t)rv;
rtx->gbl.nr = lv; rtx->gbl.nr = lv;
@ -3614,7 +3614,7 @@ static qse_awk_val_t* do_assignment_pos (
if (val->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr); if (val->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr);
if (n == -1) return QSE_NULL; if (n <= -1) return QSE_NULL;
return (lv == 0)? run->inrec.d0: run->inrec.flds[lv-1].val; return (lv == 0)? run->inrec.d0: run->inrec.flds[lv-1].val;
} }
@ -6344,7 +6344,7 @@ static qse_awk_val_t* eval_getline (qse_awk_rtx_t* run, qse_awk_nde_t* nde)
/* set $0 with the input value */ /* set $0 with the input value */
if (qse_awk_rtx_setrec (run, 0, if (qse_awk_rtx_setrec (run, 0,
QSE_STR_PTR(&buf), QSE_STR_PTR(&buf),
QSE_STR_LEN(&buf)) == -1) QSE_STR_LEN(&buf)) <= -1)
{ {
qse_str_fini (&buf); qse_str_fini (&buf);
return QSE_NULL; return QSE_NULL;
@ -6449,7 +6449,7 @@ static int read_record (qse_awk_rtx_t* run)
if (qse_awk_rtx_setrec (run, 0, if (qse_awk_rtx_setrec (run, 0,
QSE_STR_PTR(&run->inrec.line), QSE_STR_PTR(&run->inrec.line),
QSE_STR_LEN(&run->inrec.line)) == -1) return -1; QSE_STR_LEN(&run->inrec.line)) <= -1) return -1;
return 1; return 1;
} }