From 38b00c07567b0d65661f60f2c0a50bb3ba76b173 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 26 May 2011 08:42:26 +0000 Subject: [PATCH] touched up code --- qse/lib/awk/std.c | 343 +++++++++++++++++++++------------------- qse/lib/awk/val.c | 261 +++++++++++++++++------------- qse/samples/awk/awk03.c | 4 +- 3 files changed, 332 insertions(+), 276 deletions(-) diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 453d0014..3a0aaa49 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -1,5 +1,5 @@ /* - * $Id: std.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ + * $Id: std.c 481 2011-05-25 14:42:26Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -279,7 +279,7 @@ qse_awk_t* qse_awk_openstdwithmmgr (qse_mmgr_t* mmgr, qse_size_t xtnsize) QSE_MEMSET (xtn, 0, QSE_SIZEOF(xtn_t)); /* add intrinsic functions */ - if (add_functions (awk) == -1) + if (add_functions (awk) <= -1) { qse_awk_close (awk); return QSE_NULL; @@ -348,9 +348,11 @@ static qse_ssize_t sf_in_open ( xtn->s.in.handle = QSE_NULL; return 1; - default: - return -1; } + + /* this should never happen */ + qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL); + return -1; } else { @@ -470,10 +472,10 @@ static qse_ssize_t sf_in_read ( } return n; } - - default: - return -1; } + + qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL); + return -1; } else { @@ -509,9 +511,10 @@ static qse_ssize_t sf_in ( case QSE_AWK_SIO_READ: return sf_in_read (awk, arg, data, size, xtn); - default: - return -1; } + + qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL); + return -1; } static qse_ssize_t sf_out ( @@ -520,120 +523,133 @@ static qse_ssize_t sf_out ( { xtn_t* xtn = QSE_XTN (awk); - if (cmd == QSE_AWK_SIO_OPEN) + switch (cmd) { - switch (xtn->s.out.type) + case QSE_AWK_SIO_OPEN: { - case QSE_AWK_PARSESTD_FILE: - if (xtn->s.out.u.file == QSE_NULL) return -1; + switch (xtn->s.out.type) + { + case QSE_AWK_PARSESTD_FILE: + if (xtn->s.out.u.file == QSE_NULL) return -1; + + if (xtn->s.out.u.file[0] == QSE_T('-') && + xtn->s.out.u.file[1] == QSE_T('\0')) + { + /* special file name '-' */ + xtn->s.out.handle = qse_sio_out; + } + else + { + xtn->s.out.handle = qse_sio_open ( + awk->mmgr, + 0, + xtn->s.out.u.file, + QSE_SIO_WRITE | + QSE_SIO_CREATE | + QSE_SIO_TRUNCATE + ); + if (xtn->s.out.handle == QSE_NULL) + { + qse_cstr_t ea; + ea.ptr = xtn->s.out.u.file; + ea.len = qse_strlen(ea.ptr); + qse_awk_seterrnum (awk, QSE_AWK_EOPEN, &ea); + return -1; + } + } + return 1; - if (xtn->s.out.u.file[0] == QSE_T('-') && - xtn->s.out.u.file[1] == QSE_T('\0')) - { - /* special file name '-' */ + case QSE_AWK_PARSESTD_STDIO: xtn->s.out.handle = qse_sio_out; - } - else + return 1; + + case QSE_AWK_PARSESTD_CP: + case QSE_AWK_PARSESTD_CPL: + xtn->s.out.handle = QSE_NULL; + return 1; + } + + break; + } + + + case QSE_AWK_SIO_CLOSE: + { + switch (xtn->s.out.type) + { + case QSE_AWK_PARSESTD_FILE: + case QSE_AWK_PARSESTD_STDIO: + + qse_sio_flush (xtn->s.out.handle); + if (xtn->s.out.handle != qse_sio_in && + xtn->s.out.handle != qse_sio_out && + xtn->s.out.handle != qse_sio_err) + { + qse_sio_close (xtn->s.out.handle); + } + return 0; + + case QSE_AWK_PARSESTD_CP: + *xtn->s.out.u.cp = QSE_T('\0'); + return 0; + + case QSE_AWK_PARSESTD_CPL: + xtn->s.out.u.cpl.osp->len = + xtn->s.out.u.cpl.ptr - + xtn->s.out.u.cpl.osp->ptr; + return 0; + } + + break; + } + + case QSE_AWK_SIO_WRITE: + { + switch (xtn->s.out.type) + { + case QSE_AWK_PARSESTD_FILE: + case QSE_AWK_PARSESTD_STDIO: { - xtn->s.out.handle = qse_sio_open ( - awk->mmgr, - 0, - xtn->s.out.u.file, - QSE_SIO_WRITE | - QSE_SIO_CREATE | - QSE_SIO_TRUNCATE - ); - if (xtn->s.out.handle == QSE_NULL) + qse_ssize_t n; + QSE_ASSERT (xtn->s.out.handle != QSE_NULL); + n = qse_sio_putsn (xtn->s.out.handle, data, size); + if (n == -1) { qse_cstr_t ea; - ea.ptr = xtn->s.out.u.file; + ea.ptr = xtn->s.in.u.file; ea.len = qse_strlen(ea.ptr); - qse_awk_seterrnum (awk, QSE_AWK_EOPEN, &ea); - return -1; + qse_awk_seterrnum (awk, QSE_AWK_EWRITE, &ea); } + + return n; } - return 1; - - case QSE_AWK_PARSESTD_STDIO: - xtn->s.out.handle = qse_sio_out; - return 1; - - case QSE_AWK_PARSESTD_CP: - case QSE_AWK_PARSESTD_CPL: - xtn->s.out.handle = QSE_NULL; - return 1; - } - } - else if (cmd == QSE_AWK_SIO_CLOSE) - { - switch (xtn->s.out.type) - { - case QSE_AWK_PARSESTD_FILE: - case QSE_AWK_PARSESTD_STDIO: - - qse_sio_flush (xtn->s.out.handle); - if (xtn->s.out.handle != qse_sio_in && - xtn->s.out.handle != qse_sio_out && - xtn->s.out.handle != qse_sio_err) + + case QSE_AWK_PARSESTD_CP: { - qse_sio_close (xtn->s.out.handle); + qse_size_t n = 0; + while (n < size && *xtn->s.out.u.cp != QSE_T('\0')) + { + *xtn->s.out.u.cp++ = data[n++]; + } + return n; } - return 0; - case QSE_AWK_PARSESTD_CP: - *xtn->s.out.u.cp = QSE_T('\0'); - return 0; - - case QSE_AWK_PARSESTD_CPL: - xtn->s.out.u.cpl.osp->len = - xtn->s.out.u.cpl.ptr - - xtn->s.out.u.cpl.osp->ptr; - return 0; - } - } - else if (cmd == QSE_AWK_SIO_WRITE) - { - switch (xtn->s.out.type) - { - case QSE_AWK_PARSESTD_FILE: - case QSE_AWK_PARSESTD_STDIO: - { - qse_ssize_t n; - QSE_ASSERT (xtn->s.out.handle != QSE_NULL); - n = qse_sio_putsn (xtn->s.out.handle, data, size); - if (n == -1) + case QSE_AWK_PARSESTD_CPL: { - qse_cstr_t ea; - ea.ptr = xtn->s.in.u.file; - ea.len = qse_strlen(ea.ptr); - qse_awk_seterrnum (awk, QSE_AWK_EWRITE, &ea); + qse_size_t n = 0; + while (n < size && xtn->s.out.u.cpl.ptr < xtn->s.out.u.cpl.end) + { + *xtn->s.out.u.cpl.ptr++ = data[n++]; + } + return n; } - - return n; } - case QSE_AWK_PARSESTD_CP: - { - qse_size_t n = 0; - while (n < size && *xtn->s.out.u.cp != QSE_T('\0')) - { - *xtn->s.out.u.cp++ = data[n++]; - } - return n; - } - - case QSE_AWK_PARSESTD_CPL: - { - qse_size_t n = 0; - while (n < size && xtn->s.out.u.cpl.ptr < xtn->s.out.u.cpl.end) - { - *xtn->s.out.u.cpl.ptr++ = data[n++]; - } - return n; - } + break; } } + qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL); return -1; } @@ -675,9 +691,10 @@ int qse_awk_parsestd ( qse_awk_seterrnum (awk, QSE_AWK_EINVAL, QSE_NULL); return -1; } + xtn->s.in.type = in->type; xtn->s.in.handle = QSE_NULL; - sio.in = sf_in; + sio.in = sf_in; if (out == QSE_NULL) sio.out = QSE_NULL; else @@ -1139,37 +1156,73 @@ static qse_ssize_t awk_rio_console ( qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, qse_char_t* data, qse_size_t size) { - if (cmd == QSE_AWK_RIO_OPEN) + switch (cmd) { - return open_rio_console (rtx, riod); - } - else if (cmd == QSE_AWK_RIO_CLOSE) - { - if (riod->handle != QSE_NULL && - riod->handle != qse_sio_in && - riod->handle != qse_sio_out && - riod->handle != qse_sio_err) + case QSE_AWK_RIO_OPEN: + return open_rio_console (rtx, riod); + + case QSE_AWK_RIO_CLOSE: + if (riod->handle != QSE_NULL && + riod->handle != qse_sio_in && + riod->handle != qse_sio_out && + riod->handle != qse_sio_err) + { + qse_sio_close ((qse_sio_t*)riod->handle); + } + + return 0; + + case QSE_AWK_RIO_READ: { - qse_sio_close ((qse_sio_t*)riod->handle); + qse_ssize_t nn; + + while ((nn = qse_sio_getsn((qse_sio_t*)riod->handle,data,size)) == 0) + { + int n; + qse_sio_t* sio = (qse_sio_t*)riod->handle; + + n = open_rio_console (rtx, riod); + if (n <= -1) return -1; + + if (n == 0) + { + /* no more input console */ + return 0; + } + + if (sio != QSE_NULL && + sio != qse_sio_in && + sio != qse_sio_out && + sio != qse_sio_err) + { + qse_sio_close (sio); + } + } + + return nn; } - return 0; - } - else if (cmd == QSE_AWK_RIO_READ) - { - qse_ssize_t nn; + case QSE_AWK_RIO_WRITE: + return qse_sio_putsn ( + (qse_sio_t*)riod->handle, + data, + size + ); - while ((nn = qse_sio_getsn((qse_sio_t*)riod->handle,data,size)) == 0) + case QSE_AWK_RIO_FLUSH: + return qse_sio_flush ((qse_sio_t*)riod->handle); + + case QSE_AWK_RIO_NEXT: { int n; qse_sio_t* sio = (qse_sio_t*)riod->handle; - + n = open_rio_console (rtx, riod); - if (n == -1) return -1; - + if (n <= -1) return -1; + if (n == 0) { - /* no more input console */ + /* if there is no more file, keep the previous handle */ return 0; } @@ -1180,47 +1233,13 @@ static qse_ssize_t awk_rio_console ( { qse_sio_close (sio); } + + return n; } - return nn; - } - else if (cmd == QSE_AWK_RIO_WRITE) - { - return qse_sio_putsn ( - (qse_sio_t*)riod->handle, - data, - size - ); - } - else if (cmd == QSE_AWK_RIO_FLUSH) - { - return qse_sio_flush ((qse_sio_t*)riod->handle); - } - else if (cmd == QSE_AWK_RIO_NEXT) - { - int n; - qse_sio_t* sio = (qse_sio_t*)riod->handle; - - n = open_rio_console (rtx, riod); - if (n == -1) return -1; - - if (n == 0) - { - /* if there is no more file, keep the previous handle */ - return 0; - } - - if (sio != QSE_NULL && - sio != qse_sio_in && - sio != qse_sio_out && - sio != qse_sio_err) - { - qse_sio_close (sio); - } - - return n; } + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINTERN, QSE_NULL); return -1; } @@ -1296,7 +1315,7 @@ qse_awk_rtx_t* qse_awk_rtx_openstd ( rxtn = (rxtn_t*) QSE_XTN (rtx); QSE_MEMSET (rxtn, 0, QSE_SIZEOF(rxtn_t)); - if (qse_gettime (&now) == -1) rxtn->seed = 0; + if (qse_gettime (&now) <= -1) rxtn->seed = 0; else rxtn->seed = (unsigned int) now; srand (rxtn->seed); diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index 342c2241..4d5e60f4 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ + * $Id: val.c 481 2011-05-25 14:42:26Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -599,75 +599,93 @@ void qse_awk_rtx_freeval ( qse_dprintf (QSE_T("\n")); #endif - if (val->type == QSE_AWK_VAL_NIL) + switch (val->type) { - QSE_AWK_FREE (rtx->awk, val); - } - else if (val->type == QSE_AWK_VAL_INT) - { - ((qse_awk_val_int_t*)val)->nde = - (qse_awk_nde_int_t*)rtx->vmgr.ifree; - rtx->vmgr.ifree = (qse_awk_val_int_t*)val; - } - else if (val->type == QSE_AWK_VAL_REAL) - { - ((qse_awk_val_real_t*)val)->nde = - (qse_awk_nde_real_t*)rtx->vmgr.rfree; - rtx->vmgr.rfree = (qse_awk_val_real_t*)val; - } - else if (val->type == QSE_AWK_VAL_STR) - { -#ifdef ENABLE_FEATURE_SCACHE - if (cache) + case QSE_AWK_VAL_NIL: { - qse_awk_val_str_t* v = (qse_awk_val_str_t*)val; - int i; + QSE_AWK_FREE (rtx->awk, val); + break; + } + + case QSE_AWK_VAL_INT: + { + ((qse_awk_val_int_t*)val)->nde = + (qse_awk_nde_int_t*)rtx->vmgr.ifree; + rtx->vmgr.ifree = (qse_awk_val_int_t*)val; + break; + } - i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT; - if (i < QSE_COUNTOF(rtx->scache_count) && - rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i])) + case QSE_AWK_VAL_REAL: + { + ((qse_awk_val_real_t*)val)->nde = + (qse_awk_nde_real_t*)rtx->vmgr.rfree; + rtx->vmgr.rfree = (qse_awk_val_real_t*)val; + break; + } + + case QSE_AWK_VAL_STR: + { + #ifdef ENABLE_FEATURE_SCACHE + if (cache) { - rtx->scache[i][rtx->scache_count[i]++] = v; - v->nstr = 0; + qse_awk_val_str_t* v = (qse_awk_val_str_t*)val; + int i; + + i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT; + if (i < QSE_COUNTOF(rtx->scache_count) && + rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i])) + { + rtx->scache[i][rtx->scache_count[i]++] = v; + v->nstr = 0; + } + else QSE_AWK_FREE (rtx->awk, val); + } + else + #endif + QSE_AWK_FREE (rtx->awk, val); + + break; + } + + case QSE_AWK_VAL_REX: + { + /* don't free ptr as it is inlined to val + QSE_AWK_FREE (rtx->awk, ((qse_awk_val_rex_t*)val)->ptr); + */ + + /* code is just a pointer to a regular expression stored + * in parse tree nodes. so don't free it. + QSE_AWK_FREEREX (rtx->awk, ((qse_awk_val_rex_t*)val)->code); + */ + + QSE_AWK_FREE (rtx->awk, val); + break; + } + + case QSE_AWK_VAL_MAP: + { + qse_htb_fini (((qse_awk_val_map_t*)val)->map); + QSE_AWK_FREE (rtx->awk, val); + break; + } + + case QSE_AWK_VAL_REF: + { + if (cache && rtx->fcache_count < QSE_COUNTOF(rtx->fcache)) + { + rtx->fcache[rtx->fcache_count++] = (qse_awk_val_ref_t*)val; } else QSE_AWK_FREE (rtx->awk, val); + break; } - else -#endif - QSE_AWK_FREE (rtx->awk, val); - } - else if (val->type == QSE_AWK_VAL_REX) - { - /* don't free ptr as it is inlined to val - QSE_AWK_FREE (rtx->awk, ((qse_awk_val_rex_t*)val)->ptr); - */ - - /* code is just a pointer to a regular expression stored - * in parse tree nodes. so don't free it. - QSE_AWK_FREEREX (rtx->awk, ((qse_awk_val_rex_t*)val)->code); - */ - QSE_AWK_FREE (rtx->awk, val); - } - else if (val->type == QSE_AWK_VAL_MAP) - { - qse_htb_fini (((qse_awk_val_map_t*)val)->map); - QSE_AWK_FREE (rtx->awk, val); - } - else if (val->type == QSE_AWK_VAL_REF) - { - if (cache && rtx->fcache_count < QSE_COUNTOF(rtx->fcache)) + default: { - rtx->fcache[rtx->fcache_count++] = - (qse_awk_val_ref_t*)val; + QSE_ASSERTX ( + !"should never happen - invalid value type", + "the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h"); + break; } - else QSE_AWK_FREE (rtx->awk, val); - } - else - { - QSE_ASSERTX ( - !"should never happen - invalid value type", - "the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h"); } } @@ -745,12 +763,13 @@ qse_bool_t qse_awk_rtx_valtobool (qse_awk_rtx_t* run, const qse_awk_val_t* val) return QSE_FALSE; /* TODO: is this correct? */ case QSE_AWK_VAL_REF: return QSE_FALSE; /* TODO: is this correct? */ - } - QSE_ASSERTX ( - !"should never happen - invalid value type", - "the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h"); - return QSE_FALSE; + default: + QSE_ASSERTX ( + !"should never happen - invalid value type", + "the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h"); + return QSE_FALSE; + } } static int str_to_str ( @@ -809,7 +828,6 @@ static int str_to_str ( qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); return -1; } - return 0; } @@ -823,13 +841,15 @@ static int str_to_str ( qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); return -1; } - return 0; } - } - qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); - return -1; + default: + { + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); + return -1; + } + } } static int val_int_to_str ( @@ -924,6 +944,12 @@ static int val_int_to_str ( } break; } + + default: + { + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); + return -1; + } } t = v->val; @@ -1090,16 +1116,19 @@ int qse_awk_rtx_valtostr ( qse_awk_val_str_t* vs = (qse_awk_val_str_t*)v; return str_to_str (rtx, vs->val.ptr, vs->val.len, out); } + + default: + { + #ifdef DEBUG_VAL + qse_dprintf ( + QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtostr\n"), + v->type + ); + #endif + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL); + return -1; + } } - -#ifdef DEBUG_VAL - qse_dprintf ( - QSE_T("ERROR: WRONG VALUE TYPE [%d] in qse_awk_rtx_valtostr\n"), - v->type); -#endif - - qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL); - return -1; } qse_char_t* qse_awk_rtx_valtocpldup ( @@ -1117,42 +1146,50 @@ qse_char_t* qse_awk_rtx_valtocpldup ( int qse_awk_rtx_valtonum ( qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_long_t* l, qse_real_t* r) { - if (v->type == QSE_AWK_VAL_NIL) + switch (v->type) { - *l = 0; - return 0; + case QSE_AWK_VAL_NIL: + { + *l = 0; + return 0; + } + + case QSE_AWK_VAL_INT: + { + *l = ((qse_awk_val_int_t*)v)->val; + return 0; /* long */ + } + + case QSE_AWK_VAL_REAL: + { + *r = ((qse_awk_val_real_t*)v)->val; + return 1; /* real */ + } + + case QSE_AWK_VAL_STR: + { + return qse_awk_rtx_strtonum ( + rtx, 0, + ((qse_awk_val_str_t*)v)->val.ptr, + ((qse_awk_val_str_t*)v)->val.len, + l, r + ); + } + + default: + { + #ifdef DEBUG_VAL + qse_dprintf ( + QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtonum\n"), + v->type + ); + #endif + + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL); + return -1; /* error */ + } } - if (v->type == QSE_AWK_VAL_INT) - { - *l = ((qse_awk_val_int_t*)v)->val; - return 0; /* long */ - } - - if (v->type == QSE_AWK_VAL_REAL) - { - *r = ((qse_awk_val_real_t*)v)->val; - return 1; /* real */ - } - - if (v->type == QSE_AWK_VAL_STR) - { - return qse_awk_rtx_strtonum ( - rtx, 0, - ((qse_awk_val_str_t*)v)->val.ptr, - ((qse_awk_val_str_t*)v)->val.len, - l, r - ); - } - -#ifdef DEBUG_VAL - qse_dprintf ( - QSE_T("ERROR: WRONG VALUE TYPE [%d] in qse_awk_rtx_valtonum\n"), - v->type); -#endif - - qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL); - return -1; /* error */ } int qse_awk_rtx_strtonum ( diff --git a/qse/samples/awk/awk03.c b/qse/samples/awk/awk03.c index 20131620..e0438117 100644 --- a/qse/samples/awk/awk03.c +++ b/qse/samples/awk/awk03.c @@ -1,5 +1,5 @@ /* - * $Id: awk03.c 457 2011-05-12 16:16:57Z hyunghwan.chung $ + * $Id: awk03.c 481 2011-05-25 14:42:26Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -25,7 +25,7 @@ static const qse_char_t* src = QSE_T( "function init() { a = 20; return a; }" "function main() { return ++a; }" - "function fini() { print ++a; return a; }" + "function fini() { print \"a in fini() =>\", ++a; return a; }" ); static const qse_char_t* fnc[] =