From a3af4c3eed1c18a1df94ad2a5aa0304e8d96fac0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 31 Oct 2012 08:31:58 +0000 Subject: [PATCH] added qse_awk_rtx_valtombs()/qse_awk_rtx_valtostr() added sys::kill() to awk added sys::SIGXXXX to awk added or enhanced uci::XXXX in awk --- qse/cmd/awk/awk.c | 12 +- qse/include/qse/awk/awk.h | 27 +- qse/lib/awk/awk.h | 30 +- qse/lib/awk/fnc.c | 36 +- qse/lib/awk/parse.c | 1 + qse/lib/awk/rio.c | 2 +- qse/lib/awk/run.c | 7 +- qse/lib/awk/std.c | 6 +- qse/lib/awk/val.c | 56 +- qse/mod/awk/sys.c | 163 ++++-- qse/mod/awk/uci.c | 1014 +++++++++++++++++++++++++------------ qse/samples/awk/awk03.c | 2 +- qse/samples/awk/awk04.c | 2 +- qse/samples/awk/awk10.c | 6 +- 14 files changed, 935 insertions(+), 429 deletions(-) diff --git a/qse/cmd/awk/awk.c b/qse/cmd/awk/awk.c index 5a0e0931..366c7a86 100644 --- a/qse/cmd/awk/awk.c +++ b/qse/cmd/awk/awk.c @@ -251,7 +251,7 @@ static qse_htb_walk_t print_awk_value ( qse_awk_rtx_geterrinf (rtx, &oerrinf); - str = qse_awk_rtx_valtocpldup (rtx, QSE_HTB_VPTR(pair), &len); + str = qse_awk_rtx_valtostrdup (rtx, QSE_HTB_VPTR(pair), &len); if (str == QSE_NULL) { if (qse_awk_rtx_geterrnum(rtx) == QSE_AWK_EVALTYPE) @@ -333,7 +333,7 @@ static void dprint_return (qse_awk_rtx_t* rtx, qse_awk_val_t* ret) } else { - str = qse_awk_rtx_valtocpldup (rtx, ret, &len); + str = qse_awk_rtx_valtostrdup (rtx, ret, &len); if (str == QSE_NULL) { dprint (QSE_T("[RETURN] - ***OUT OF MEMORY***\n")); @@ -1071,6 +1071,9 @@ static int awk_main (int argc, qse_char_t* argv[]) retv = arg.call? qse_awk_rtx_callwithstrs (rtx, arg.call, arg.icf.ptr, arg.icf.size): qse_awk_rtx_loop (rtx); + + unset_intr_run (); + if (retv) { qse_long_t tmp; @@ -1081,10 +1084,7 @@ static int awk_main (int argc, qse_char_t* argv[]) ret = 0; if (qse_awk_rtx_valtolong (rtx, retv, &tmp) >= 0) ret = tmp; } - - unset_intr_run (); - - if (ret <= -1) + else { print_rtxerr (rtx); goto oops; diff --git a/qse/include/qse/awk/awk.h b/qse/include/qse/awk/awk.h index 7e708c8b..dc6366c7 100644 --- a/qse/include/qse/awk/awk.h +++ b/qse/include/qse/awk/awk.h @@ -1960,6 +1960,15 @@ qse_awk_val_t* qse_awk_rtx_getarg ( qse_size_t idx ); +/** + * The qse_awk_rtx_getsubsep() function returns the + * pointer to the internal value of SUBSEP. It's a specialized + * version of qse_awk_rtx_getgbl (rtx, QSE_AWK_GBL_SUBSEP). + */ +const qse_xstr_t* qse_awk_rtx_getsubsep ( + qse_awk_rtx_t* rtx /**< runtime context */ +); + /** * The qse_awk_rtx_getgbl() gets the value of a global variable. * The global variable ID @a id is one of the predefined global @@ -2473,14 +2482,14 @@ int qse_awk_rtx_valtostr ( ); /** - * The qse_awk_rtx_valtocpldup() function provides a shortcut to the + * The qse_awk_rtx_valtostrdup() function provides a shortcut to the * qse_awk_rtx_valtostr() function with the #QSE_AWK_RTX_VALTOSTR_CPLDUP type. * It returns the pointer to a string converted from @a val and stores its * length to memory pointed to by @a len. You should free the returned * memory block after use. See the code snippet below for a simple usage. * * @code - * ptr = qse_awk_rtx_valtocpldup (rtx, v, &len); + * ptr = qse_awk_rtx_valtostrdup (rtx, v, &len); * if (str == QSE_NULL) handle_error(); * qse_printf (QSE_T("%.*s\n"), (int)len, ptr); * qse_awk_rtx_free (rtx, ptr); @@ -2489,7 +2498,19 @@ int qse_awk_rtx_valtostr ( * @return character pointer to a string converted on success, * #QSE_NULL on failure */ -qse_char_t* qse_awk_rtx_valtocpldup ( +qse_char_t* qse_awk_rtx_valtostrdup ( + qse_awk_rtx_t* rtx, /**< runtime context */ + const qse_awk_val_t* val, /**< value to convert */ + qse_size_t* len /**< result length */ +); + +qse_mchar_t* qse_awk_rtx_valtombsdup ( + qse_awk_rtx_t* rtx, /**< runtime context */ + const qse_awk_val_t* val, /**< value to convert */ + qse_size_t* len /**< result length */ +); + +qse_wchar_t* qse_awk_rtx_valtowcsdup ( qse_awk_rtx_t* rtx, /**< runtime context */ const qse_awk_val_t* val, /**< value to convert */ qse_size_t* len /**< result length */ diff --git a/qse/lib/awk/awk.h b/qse/lib/awk/awk.h index c5dd6c5d..b3c465de 100644 --- a/qse/lib/awk/awk.h +++ b/qse/lib/awk/awk.h @@ -298,31 +298,11 @@ struct qse_awk_rtx_t qse_long_t nr; qse_long_t fnr; - struct - { - qse_char_t* ptr; - qse_size_t len; - } convfmt; - struct - { - qse_char_t* ptr; - qse_size_t len; - } ofmt; - struct - { - qse_char_t* ptr; - qse_size_t len; - } ofs; - struct - { - qse_char_t* ptr; - qse_size_t len; - } ors; - struct - { - qse_char_t* ptr; - qse_size_t len; - } subsep; + qse_xstr_t convfmt; + qse_xstr_t ofmt; + qse_xstr_t ofs; + qse_xstr_t ors; + qse_xstr_t subsep; } gbl; /* rio chain */ diff --git a/qse/lib/awk/fnc.c b/qse/lib/awk/fnc.c index 82c6c91c..d384178a 100644 --- a/qse/lib/awk/fnc.c +++ b/qse/lib/awk/fnc.c @@ -245,7 +245,7 @@ static int fnc_close (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - name = qse_awk_rtx_valtocpldup (rtx, a0, &len); + name = qse_awk_rtx_valtostrdup (rtx, a0, &len); if (name == QSE_NULL) return -1; } @@ -258,7 +258,7 @@ static int fnc_close (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - opt = qse_awk_rtx_valtocpldup (rtx, a1, &optlen); + opt = qse_awk_rtx_valtostrdup (rtx, a1, &optlen); if (opt == QSE_NULL) { if (a1->type != QSE_AWK_VAL_STR) @@ -386,7 +386,7 @@ static int fnc_fflush (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) } else { - str0 = qse_awk_rtx_valtocpldup (run, a0, &len0); + str0 = qse_awk_rtx_valtostrdup (run, a0, &len0); if (str0 == QSE_NULL) return -1; } @@ -462,7 +462,7 @@ static int fnc_index (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - str0 = qse_awk_rtx_valtocpldup (rtx, a0, &len0); + str0 = qse_awk_rtx_valtostrdup (rtx, a0, &len0); if (str0 == QSE_NULL) return -1; } @@ -473,7 +473,7 @@ static int fnc_index (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - str1 = qse_awk_rtx_valtocpldup (rtx, a1, &len1); + str1 = qse_awk_rtx_valtostrdup (rtx, a1, &len1); if (str1 == QSE_NULL) { if (a0->type != QSE_AWK_VAL_STR) @@ -526,7 +526,7 @@ static int fnc_length (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - str = qse_awk_rtx_valtocpldup (rtx, v, &len); + str = qse_awk_rtx_valtostrdup (rtx, v, &len); if (str == QSE_NULL) return -1; QSE_AWK_FREE (rtx->awk, str); } @@ -562,7 +562,7 @@ static int fnc_substr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - str = qse_awk_rtx_valtocpldup (rtx, a0, &len); + str = qse_awk_rtx_valtostrdup (rtx, a0, &len); if (str == QSE_NULL) return -1; } @@ -667,7 +667,7 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) } else { - str.ptr = qse_awk_rtx_valtocpldup (run, a0, &str.len); + str.ptr = qse_awk_rtx_valtostrdup (run, a0, &str.len); if (str.ptr == QSE_NULL) return -1; str_free = (qse_char_t*)str.ptr; } @@ -688,7 +688,7 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) } else { - fs.ptr = qse_awk_rtx_valtocpldup (run, t1, &fs.len); + fs.ptr = qse_awk_rtx_valtostrdup (run, t1, &fs.len); if (fs.ptr == QSE_NULL) goto oops; fs_free = (qse_char_t*)fs.ptr; } @@ -714,7 +714,7 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) } else { - fs.ptr = qse_awk_rtx_valtocpldup (run, a2, &fs.len); + fs.ptr = qse_awk_rtx_valtostrdup (run, a2, &fs.len); if (fs.ptr == QSE_NULL) goto oops; fs_free = (qse_char_t*)fs.ptr; } @@ -831,7 +831,7 @@ static int fnc_tolower (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) } else { - str.ptr = qse_awk_rtx_valtocpldup (run, a0, &str.len); + str.ptr = qse_awk_rtx_valtostrdup (run, a0, &str.len); if (str.ptr == QSE_NULL) return -1; } @@ -868,7 +868,7 @@ static int fnc_toupper (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) } else { - str.ptr = qse_awk_rtx_valtocpldup (run, a0, &str.len); + str.ptr = qse_awk_rtx_valtostrdup (run, a0, &str.len); if (str.ptr == QSE_NULL) return -1; } @@ -928,7 +928,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) } else { - s0.ptr = qse_awk_rtx_valtocpldup (run, a0, &s0.len); + s0.ptr = qse_awk_rtx_valtostrdup (run, a0, &s0.len); if (s0.ptr == QSE_NULL) goto oops; s0_free = (qse_char_t*)s0.ptr; } @@ -940,7 +940,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) } else { - s1.ptr = qse_awk_rtx_valtocpldup (run, a1, &s1.len); + s1.ptr = qse_awk_rtx_valtostrdup (run, a1, &s1.len); if (s1.ptr == QSE_NULL) goto oops; s1_free = (qse_char_t*)s1.ptr; } @@ -990,7 +990,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) } else { - s2.ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &s2.len); + s2.ptr = qse_awk_rtx_valtostrdup (run, *a2_ref, &s2.len); if (s2.ptr == QSE_NULL) goto oops; s2_free = (qse_char_t*)s2.ptr; } @@ -1241,7 +1241,7 @@ static int fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - str0 = qse_awk_rtx_valtocpldup (rtx, a0, &len0); + str0 = qse_awk_rtx_valtostrdup (rtx, a0, &len0); if (str0 == QSE_NULL) return -1; } @@ -1260,7 +1260,7 @@ static int fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - str1 = qse_awk_rtx_valtocpldup (rtx, a1, &len1); + str1 = qse_awk_rtx_valtostrdup (rtx, a1, &len1); if (str1 == QSE_NULL) { if (a0->type != QSE_AWK_VAL_STR) @@ -1379,7 +1379,7 @@ static int fnc_sprintf (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) } else { - cs0.ptr = qse_awk_rtx_valtocpldup (run, a0, &cs0.len); + cs0.ptr = qse_awk_rtx_valtostrdup (run, a0, &cs0.len); if (cs0.ptr == QSE_NULL) goto oops; } diff --git a/qse/lib/awk/parse.c b/qse/lib/awk/parse.c index d687f82e..6d9e0eb2 100644 --- a/qse/lib/awk/parse.c +++ b/qse/lib/awk/parse.c @@ -4948,6 +4948,7 @@ static qse_awk_nde_t* parse_primary_ident_segs ( if (mod == QSE_NULL) { if (ISNOERR(awk)) SETERR_LOC (awk, QSE_AWK_ENOSUP, xloc); + else ADJERR_LOC (awk, xloc); } else { diff --git a/qse/lib/awk/rio.c b/qse/lib/awk/rio.c index 898f0e39..89441738 100644 --- a/qse/lib/awk/rio.c +++ b/qse/lib/awk/rio.c @@ -215,7 +215,7 @@ static QSE_INLINE int resolve_rs ( break; default: - rrs->ptr = qse_awk_rtx_valtocpldup (rtx, rs, &rrs->len); + rrs->ptr = qse_awk_rtx_valtostrdup (rtx, rs, &rrs->len); if (rrs->ptr == QSE_NULL) ret = -1; break; } diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index 093bfa03..fb27f55c 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -299,6 +299,11 @@ QSE_INLINE qse_awk_val_t* qse_awk_rtx_getgbl (qse_awk_rtx_t* run, int id) return STACK_GBL (run, id); } +const qse_xstr_t* qse_awk_rtx_getsubsep (qse_awk_rtx_t* run) +{ + return &run->gbl.subsep; +} + /* internal function to set a value to a global variable. * this function can handle a few special global variables that * require special treatment. */ @@ -602,7 +607,7 @@ static int set_global ( out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; - if (rtx->gbl.subsep.ptr != QSE_NULL) + if (rtx->gbl.subsep.ptr) QSE_AWK_FREE (rtx->awk, rtx->gbl.subsep.ptr); rtx->gbl.subsep.ptr = out.u.cpldup.ptr; rtx->gbl.subsep.len = out.u.cpldup.len; diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 993c2c6c..f2c518ee 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -2167,7 +2167,7 @@ static int fnc_system (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - str = qse_awk_rtx_valtocpldup (rtx, v, &len); + str = qse_awk_rtx_valtostrdup (rtx, v, &len); if (str == QSE_NULL) return -1; } @@ -2302,7 +2302,7 @@ static int fnc_setioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - ptr[i] = qse_awk_rtx_valtocpldup (rtx, v[i], &len[i]); + ptr[i] = qse_awk_rtx_valtostrdup (rtx, v[i], &len[i]); if (ptr[i] == QSE_NULL) { ret = -1; @@ -2431,7 +2431,7 @@ static int fnc_getioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - ptr[i] = qse_awk_rtx_valtocpldup (rtx, v[i], &len[i]); + ptr[i] = qse_awk_rtx_valtostrdup (rtx, v[i], &len[i]); if (ptr[i] == QSE_NULL) { ret = -1; diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index 7949539e..2559fea4 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -245,7 +245,7 @@ qse_awk_val_t* qse_awk_rtx_makestrvalwithmcstr ( return qse_awk_rtx_makestrvalwithcstr (rtx, mcstr); #else qse_awk_val_t* v; - qse_wcstr_t tmp; + qse_wxstr_t tmp; qse_size_t mbslen; mbslen = mcstr->len; @@ -267,7 +267,7 @@ qse_awk_val_t* qse_awk_rtx_makestrvalwithwcstr ( { #if defined(QSE_CHAR_IS_MCHAR) qse_awk_val_t* v; - qse_mcstr_t tmp; + qse_mxstr_t tmp; qse_size_t wcslen; wcslen = wcstr->len; @@ -1266,7 +1266,7 @@ int qse_awk_rtx_valtostr ( return -1; } -qse_char_t* qse_awk_rtx_valtocpldup ( +qse_char_t* qse_awk_rtx_valtostrdup ( qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len) { qse_awk_rtx_valtostr_out_t out; @@ -1274,10 +1274,58 @@ qse_char_t* qse_awk_rtx_valtocpldup ( out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return QSE_NULL; - *len = out.u.cpldup.len; + if (len) *len = out.u.cpldup.len; return out.u.cpldup.ptr; } +qse_mchar_t* qse_awk_rtx_valtombsdup ( + qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len) +{ +#if defined(QSE_CHAR_IS_MCHAR) + qse_awk_rtx_valtostr_out_t out; + + out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return QSE_NULL; + + if (len) *len = out.u.cpldup.len; + return out.u.cpldup.ptr; +#else + qse_awk_rtx_valtostr_out_t out; + qse_mchar_t* mbs; + + out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return QSE_NULL; + + mbs = qse_wcsntombsdup (out.u.cpldup.ptr, out.u.cpldup.len, len, rtx->awk->mmgr); + QSE_AWK_FREE (rtx->awk, out.u.cpldup.ptr); + return mbs; +#endif +} + +qse_wchar_t* qse_awk_rtx_valtowcsdup ( + qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len) +{ +#if defined(QSE_CHAR_IS_MCHAR) + qse_awk_rtx_valtostr_out_t out; + qse_wchar_t* wcs; + + out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return QSE_NULL; + + wcs = qse_mbsntombsdup (out.u.cpldup.ptr, out.u.cpldup.len, len, rtx->awk->mmgr); + QSE_AWK_FREE (rtx->awk, out.u.cpldup.ptr); + return wcs; +#else + qse_awk_rtx_valtostr_out_t out; + + out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return QSE_NULL; + + if (len) *len = out.u.cpldup.len; + return out.u.cpldup.ptr; +#endif +} + int qse_awk_rtx_valtonum ( qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_long_t* l, qse_flt_t* r) { diff --git a/qse/mod/awk/sys.c b/qse/mod/awk/sys.c index 12a848db..525ecd53 100644 --- a/qse/mod/awk/sys.c +++ b/qse/mod/awk/sys.c @@ -13,64 +13,99 @@ # include #else # include +# include # include # include #endif static int fnc_fork (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { -#if defined(_WIN32) - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOIMPL, QSE_NULL); - return -1; - -#elif defined(__OS2__) - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOIMPL, QSE_NULL); - return -1; - -#elif defined(__DOS__) - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOIMPL, QSE_NULL); - return -1; - -#else - pid_t pid; + qse_long_t pid; qse_awk_val_t* retv; +#if defined(_WIN32) + /* TOOD: implement this*/ + pid = -1; + +#elif defined(__OS2__) + /* TOOD: implement this*/ + pid = -1; + +#elif defined(__DOS__) + /* TOOD: implement this*/ + pid = -1; + +#else pid = fork (); +#endif + retv = qse_awk_rtx_makeintval (rtx, pid); if (retv == QSE_NULL) return -1; + qse_awk_rtx_setretval (rtx, retv); return 0; -#endif } static int fnc_wait (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { - qse_long_t lv; + qse_long_t pid; qse_awk_val_t* retv; - int n; + int rx; /* TODO: handle more parameters */ - n = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &lv); - if (n <= -1) return -1; - + rx = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &pid); + if (rx >= 0) + { #if defined(_WIN32) - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOIMPL, QSE_NULL); - return -1; - + /* TOOD: implement this*/ + rx = -1; #elif defined(__OS2__) - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOIMPL, QSE_NULL); - return -1; - + /* TOOD: implement this*/ + rx = -1; #elif defined(__DOS__) - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOIMPL, QSE_NULL); - return -1; - + /* TOOD: implement this*/ + rx = -1; #else - n = waitpid (lv, QSE_NULL, 0); + rx = waitpid (pid, QSE_NULL, 0); #endif + } - retv = qse_awk_rtx_makeintval (rtx, n); + retv = qse_awk_rtx_makeintval (rtx, rx); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_kill (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + qse_long_t pid, sig; + qse_awk_val_t* retv; + int rx; + + if (qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &pid) <= -1 || + qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 1), &sig) <= -1) + { + rx = -1; + } + else + { +#if defined(_WIN32) + /* TOOD: implement this*/ + rx = -1; +#elif defined(__OS2__) + /* TOOD: implement this*/ + rx = -1; +#elif defined(__DOS__) + /* TOOD: implement this*/ + rx = -1; +#else + rx = kill (pid, sig); +#endif + } + + retv = qse_awk_rtx_makeintval (rtx, rx); if (retv == QSE_NULL) return -1; qse_awk_rtx_setretval (rtx, retv); @@ -81,25 +116,26 @@ static int fnc_sleep (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { qse_long_t lv; qse_awk_val_t* retv; - int n; + int rx; - n = qse_awk_rtx_valtolong ( + rx = qse_awk_rtx_valtolong ( rtx, qse_awk_rtx_getarg (rtx, 0), &lv); - if (n <= -1) return -1; - + if (rx >= 0) + { #if defined(_WIN32) - Sleep ((DWORD)(lv * 1000)); - n = 0; + Sleep ((DWORD)(lv * 1000)); + rx = 0; #elif defined(__OS2__) - DosSleep ((ULONG)(lv * 1000)); - n = 0; + DosSleep ((ULONG)(lv * 1000)); + rx = 0; #elif defined(__DOS__) - n = sleep (lv); + rx = sleep (lv); #else - n = sleep (lv); + rx = sleep (lv); #endif + } - retv = qse_awk_rtx_makeintval (rtx, n); + retv = qse_awk_rtx_makeintval (rtx, rx); if (retv == QSE_NULL) return -1; qse_awk_rtx_setretval (rtx, retv); @@ -122,14 +158,51 @@ struct inttab_t static fnctab_t fnctab[] = { - { QSE_T("fork"), { { 0, 0 }, fnc_fork } }, - { QSE_T("sleep"), { { 1, 1 }, fnc_sleep } }, + { QSE_T("fork"), { { 0, 0 }, fnc_fork } }, + { QSE_T("kill"), { { 2, 2 }, fnc_kill } }, + { QSE_T("sleep"), { { 1, 1 }, fnc_sleep } }, { QSE_T("wait"), { { 1, 1 }, fnc_wait } } }; +#if !defined(SIGHUP) +# define SIGHUP 1 +#endif +#if !defined(SIGINT) +# define SIGINT 2 +#endif +#if !defined(SIGQUIT) +# define SIGQUIT 3 +#endif +#if !defined(SIGABRT) +# define SIGABRT 6 +#endif +#if !defined(SIGKILL) +# define SIGKILL 9 +#endif +#if !defined(SIGSEGV) +# define SIGSEGV 11 +#endif +#if !defined(SIGALRM) +# define SIGALRM 14 +#endif +#if !defined(SIGTERM) +# define SIGKILL 15 +#endif + static inttab_t inttab[] = { - { QSE_T("WNOHANG"), { WNOHANG } } + { QSE_T("SIGABRT"), { SIGABRT } }, + { QSE_T("SIGALRM"), { SIGALRM } }, + { QSE_T("SIGHUP"), { SIGHUP } }, + { QSE_T("SIGINT"), { SIGINT } }, + { QSE_T("SIGKILL"), { SIGKILL } }, + { QSE_T("SIGSEGV"), { SIGSEGV } }, + { QSE_T("SIGTERM"), { SIGTERM } }, + { QSE_T("SIGQUIT"), { SIGQUIT } } + +/* + { QSE_T("WNOHANG"), { WNOHANG } }, +*/ }; static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym) diff --git a/qse/mod/awk/uci.c b/qse/mod/awk/uci.c index 44a79d7e..5ac30039 100644 --- a/qse/mod/awk/uci.c +++ b/qse/mod/awk/uci.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "../../lib/cmn/mem.h" #if defined(HAVE_UCI_H) @@ -10,8 +11,6 @@ # error this module needs uci.h #endif -#define ERR_WRONG_HANDLE 999 - typedef struct uctx_list_t uctx_list_t; typedef struct uctx_node_t uctx_node_t; @@ -157,7 +156,7 @@ static void free_uctx_node (qse_awk_rtx_t* rtx, uctx_list_t* list, uctx_node_t* static int close_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) { - int x = ERR_WRONG_HANDLE; + int x = UCI_ERR_INVAL; if (id >= 0 && id < list->map.high && list->map.tab[id]) { @@ -168,76 +167,14 @@ static int close_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) return -x; } -static int setconfdir_byid ( - qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, const qse_char_t* path) -{ - int x = ERR_WRONG_HANDLE; - - if (id >= 0 && id < list->map.high && list->map.tab[id]) - { - #if defined(QSE_CHAR_IS_MCHAR) - x = uci_set_confdir (list->map.tab[id]->ctx, path); - #else - qse_mchar_t* tmp; - qse_mmgr_t* mmgr = qse_awk_rtx_getmmgr(rtx); - tmp = qse_wcstombsdup (path, QSE_NULL, mmgr); - if (tmp) - { - x = uci_set_confdir (list->map.tab[id]->ctx, tmp); - QSE_MMGR_FREE (mmgr, tmp); - } - else x = UCI_ERR_MEM; - #endif - } - - return -x; -} - -static int setsavedir_byid ( - qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, const qse_char_t* path) -{ - int x = ERR_WRONG_HANDLE; - - if (id >= 0 && id < list->map.high && list->map.tab[id]) - { - #if defined(QSE_CHAR_IS_MCHAR) - x = uci_set_savedir (list->map.tab[id]->ctx, path); - #else - qse_mchar_t* tmp; - qse_mmgr_t* mmgr = qse_awk_rtx_getmmgr(rtx); - tmp = qse_wcstombsdup (path, QSE_NULL, mmgr); - if (tmp) - { - x = uci_set_savedir (list->map.tab[id]->ctx, tmp); - QSE_MMGR_FREE (mmgr, tmp); - } - else x = UCI_ERR_MEM; - #endif - } - - return -x; -} - static int load_byid ( - qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, const qse_char_t* path) + qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* path) { - int x = ERR_WRONG_HANDLE; + int x = UCI_ERR_INVAL; if (id >= 0 && id < list->map.high && list->map.tab[id]) { - #if defined(QSE_CHAR_IS_MCHAR) x = uci_load (list->map.tab[id]->ctx, path, QSE_NULL); - #else - qse_mchar_t* tmp; - qse_mmgr_t* mmgr = qse_awk_rtx_getmmgr(rtx); - tmp = qse_wcstombsdup (path, QSE_NULL, mmgr); - if (tmp) - { - x = uci_load (list->map.tab[id]->ctx, tmp, QSE_NULL); - QSE_MMGR_FREE (mmgr, tmp); - } - else x = UCI_ERR_MEM; - #endif } return -x; @@ -245,7 +182,7 @@ static int load_byid ( static int unload_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) { - int x = ERR_WRONG_HANDLE; + int x = UCI_ERR_INVAL; if (id >= 0 && id < list->map.high && list->map.tab[id]) { @@ -256,99 +193,258 @@ static int unload_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) return -x; } -static int save_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) +static int save_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item) { - int x = ERR_WRONG_HANDLE; + int x = UCI_ERR_INVAL; if (id >= 0 && id < list->map.high && list->map.tab[id]) { - x = uci_save (list->map.tab[id]->ctx, QSE_NULL); + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); + + if (x == UCI_OK) + { + + if (!ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE) && (ptr.last->type == UCI_TYPE_PACKAGE)) + x = uci_save (list->map.tab[id]->ctx, ptr.p); + else + x = UCI_ERR_NOTFOUND; + } } return -x; } -#if 0 - else if (e->type == UCI_TYPE_PACKAGE) - { - /* anything to do? */ - [type] = "PACKAGE"; - [section.type] = "xxxxxxxxxx"; - [option.type] = "string"; - [option.type] = "list"; - [option.value] = "xxxxxxx"; - [option.value.num] = "xxxxxxx"; - [option.value.1] - [option.value.2] - } -#endif - -static int getsection_byid ( - qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, - const qse_char_t* tuple, qse_awk_val_t** retv) +static int commit_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item) { - int x = ERR_WRONG_HANDLE; + int x = UCI_ERR_INVAL; if (id >= 0 && id < list->map.high && list->map.tab[id]) { - qse_mmgr_t* mmgr; - qse_mchar_t* mtuple; struct uci_ptr ptr; - mmgr = qse_awk_rtx_getmmgr (rtx); + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); - #if defined(QSE_CHAR_IS_MCHAR) - mtuple = qse_mbsdup (tuple, mmgr); - #else - mtuple = qse_wcstombsdup (tuple, QSE_NULL, mmgr); - #endif - if (mtuple) + if (x == UCI_OK) { - x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, mtuple, 1); - if (x == UCI_OK) + if (!ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE) && (ptr.last->type == UCI_TYPE_PACKAGE)) + x = uci_commit (list->map.tab[id]->ctx, &ptr.p, 0); + else + x = UCI_ERR_NOTFOUND; + } + } + + return -x; +} + +static int revert_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); + + if (x == UCI_OK) + { + if (!ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE)) + x = uci_revert (list->map.tab[id]->ctx, &ptr); + else + x = UCI_ERR_NOTFOUND; + } + } + + return -x; +} + +static int delete_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); + + if (x == UCI_OK) + { + if (!ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE)) + x = uci_delete (list->map.tab[id]->ctx, &ptr); + else + x = UCI_ERR_NOTFOUND; + } + } + + return -x; +} + +static int rename_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); + + if (x == UCI_OK) + { + if (ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE)) + x = uci_rename (list->map.tab[id]->ctx, &ptr); + else + x = UCI_ERR_NOTFOUND; + } + } + + return -x; +} + +static int set_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); + + if (x == UCI_OK) + { + x = ptr.value? uci_set (list->map.tab[id]->ctx, &ptr): UCI_ERR_INVAL; + } + } + + return -x; +} + +static int addsection_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item, qse_mchar_t* type) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); + + if (x == UCI_OK) + { + /* add an unnamed section. use set to add a named section */ + struct uci_section* s = QSE_NULL; + if (!ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE) && (ptr.last->type == UCI_TYPE_PACKAGE)) + x = uci_add_section (list->map.tab[id]->ctx, ptr.p, type, &s); + else x = UCI_ERR_INVAL; + } + } + + return -x; +} + +static int addlist_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* item) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, item, 1); + + if (x == UCI_OK) + { + x = ptr.value? uci_add_list (list->map.tab[id]->ctx, &ptr): UCI_ERR_INVAL; + } + } + + return -x; +} + +static int setconfdir_byid ( + qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* path) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + x = uci_set_confdir (list->map.tab[id]->ctx, path); + } + + return -x; +} + +static int setsavedir_byid ( + qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, qse_mchar_t* path) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + x = uci_set_savedir (list->map.tab[id]->ctx, path); + } + + return -x; +} + + +static int getsection_byid ( + qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, + qse_mchar_t* tuple, qse_awk_val_t** retv) +{ + int x = UCI_ERR_INVAL; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + struct uci_ptr ptr; + + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, tuple, 1); + if (x == UCI_OK) + { + /* ptr.value is not null if the tuple specified contains + assignment like network.wan.ifname=eth0 */ + if (!ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE)) { - if (ptr.flags & UCI_LOOKUP_COMPLETE) + struct uci_element* e; + + e = ptr.last; + if (e->type == UCI_TYPE_SECTION) { - struct uci_element* e; + qse_awk_val_map_data_t md[4]; + qse_long_t lv; + qse_awk_val_t* tmp; - e = ptr.last; - if (e->type == UCI_TYPE_SECTION) - { - qse_awk_val_map_data_t md[4]; - qse_long_t lv; - qse_awk_val_t* tmp; - - QSE_MEMSET (md, 0, QSE_SIZEOF(md)); + QSE_MEMSET (md, 0, QSE_SIZEOF(md)); - md[0].key.ptr = QSE_T("type"); - md[0].key.len = 4; - md[0].type = QSE_AWK_VAL_MAP_DATA_MBS; - md[0].vptr = ptr.s->type; + md[0].key.ptr = QSE_T("type"); + md[0].key.len = 4; + md[0].type = QSE_AWK_VAL_MAP_DATA_MBS; + md[0].vptr = ptr.s->type; - md[1].key.ptr = QSE_T("name"); - md[1].key.len = 4; - md[1].type = QSE_AWK_VAL_MAP_DATA_MBS; - md[1].vptr = ptr.s->e.name; /* e->name == ptr.s->e.name */ + md[1].key.ptr = QSE_T("name"); + md[1].key.len = 4; + md[1].type = QSE_AWK_VAL_MAP_DATA_MBS; + md[1].vptr = ptr.s->e.name; /* e->name == ptr.s->e.name */ - md[2].key.ptr = QSE_T("anon"); - md[2].key.len = 4; - md[2].vptr = QSE_AWK_VAL_MAP_DATA_INT; - lv = ptr.s->anonymous; - md[2].vptr = &lv; + md[2].key.ptr = QSE_T("anon"); + md[2].key.len = 4; + md[2].vptr = QSE_AWK_VAL_MAP_DATA_INT; + lv = ptr.s->anonymous; + md[2].vptr = &lv; - tmp = qse_awk_rtx_makemapvalwithdata (rtx, md); - if (tmp) *retv = tmp; - else x = UCI_ERR_MEM; - } - else x = UCI_ERR_NOTFOUND; - + tmp = qse_awk_rtx_makemapvalwithdata (rtx, md); + if (tmp) *retv = tmp; + else x = UCI_ERR_MEM; } else x = UCI_ERR_NOTFOUND; + } - - QSE_MMGR_FREE (mmgr, mtuple); + else x = UCI_ERR_NOTFOUND; } - else x = UCI_ERR_MEM; } return -x; @@ -356,101 +452,131 @@ static int getsection_byid ( static int getoption_byid ( qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id, - const qse_char_t* tuple, qse_awk_val_t** retv) + qse_mchar_t* tuple, qse_awk_val_t** retv) { - int x = ERR_WRONG_HANDLE; + int x = UCI_ERR_INVAL; if (id >= 0 && id < list->map.high && list->map.tab[id]) { - qse_mmgr_t* mmgr; - qse_mchar_t* mtuple; struct uci_ptr ptr; - mmgr = qse_awk_rtx_getmmgr (rtx); - - #if defined(QSE_CHAR_IS_MCHAR) - mtuple = qse_mbsdup (tuple, mmgr); - #else - mtuple = qse_wcstombsdup (tuple, QSE_NULL, mmgr); - #endif - if (mtuple) + x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, tuple, 1); + if (x == UCI_OK) { - x = uci_lookup_ptr (list->map.tab[id]->ctx, &ptr, mtuple, 1); - if (x == UCI_OK) + /* ptr.value is not null if the tuple specified contains + assignment like network.wan.ifname=eth0 */ + if (!ptr.value && (ptr.flags & UCI_LOOKUP_COMPLETE)) { - if (ptr.flags & UCI_LOOKUP_COMPLETE) + struct uci_element* e; + + e = ptr.last; + if (e->type == UCI_TYPE_OPTION) { - struct uci_element* e; + struct uci_option* uo = ptr.o; - e = ptr.last; - if (e->type == UCI_TYPE_OPTION) + if (uo->type == UCI_TYPE_STRING) { - struct uci_option* uo = ptr.o; + qse_awk_val_t* map; + qse_awk_val_map_data_t md[3]; - if (uo->type == UCI_TYPE_STRING) + QSE_MEMSET (md, 0, QSE_SIZEOF(md)); + + md[0].key.ptr = QSE_T("type"); + md[0].key.len = 4; + md[0].type = QSE_AWK_VAL_MAP_DATA_STR; + md[0].vptr = QSE_T("string"); + + md[1].key.ptr = QSE_T("value"); + md[1].key.len = 5; + md[1].type = QSE_AWK_VAL_MAP_DATA_MBS; + md[1].vptr = uo->v.string; + + map = qse_awk_rtx_makemapvalwithdata (rtx, md); + if (map) *retv = map; + else x = UCI_ERR_MEM; + } + else if (uo->type == UCI_TYPE_LIST) + { + qse_awk_val_t* map, * fld; + qse_awk_val_map_data_t md[3]; + struct uci_element* tmp; + qse_long_t count; + + count = 0; + uci_foreach_element(&uo->v.list, tmp) { count++; } + + QSE_MEMSET (md, 0, QSE_SIZEOF(md)); + + md[0].key.ptr = QSE_T("type"); + md[0].key.len = 4; + md[0].type = QSE_AWK_VAL_MAP_DATA_STR; + md[0].vptr = QSE_T("list"); + + md[1].key.ptr = QSE_T("count"); + md[1].key.len = 5; + md[1].type = QSE_AWK_VAL_MAP_DATA_INT; + md[1].vptr = &count; + + map = qse_awk_rtx_makemapvalwithdata (rtx, md); + if (map) { - qse_awk_val_map_data_t md[3]; - qse_awk_val_t* tmp; - - QSE_MEMSET (md, 0, QSE_SIZEOF(md)); - - md[0].key.ptr = QSE_T("type"); - md[0].key.len = 4; - md[0].type = QSE_AWK_VAL_MAP_DATA_STR; - md[0].vptr = QSE_T("string"); - - md[1].key.ptr = QSE_T("value"); - md[1].key.len = 5; - md[1].type = QSE_AWK_VAL_MAP_DATA_MBS; - md[1].vptr = uo->v.string; - - tmp = qse_awk_rtx_makemapvalwithdata (rtx, md); - if (tmp) *retv = tmp; - else x = UCI_ERR_MEM; - } - else if (uo->type == UCI_TYPE_LIST) - { - struct uci_element* tmp; - qse_size_t count = 0; - qse_awk_val_map_data_t* md; - - uci_foreach_element(&uo->v.list, tmp) { count++; } - - md = qse_awk_rtx_callocmem (rtx, (count + 2) * QSE_SIZEOF(*md)); - if (md == QSE_NULL) x = UCI_ERR_MEM; - else + count = 1; + uci_foreach_element(&uo->v.list, tmp) { - md[0].key.ptr = QSE_T("type"); - md[0].key.len = 4; - md[0].type = QSE_AWK_VAL_MAP_DATA_STR; - md[0].vptr = QSE_T("list"); + const qse_xstr_t* subsep; + qse_cstr_t k[4]; + qse_char_t idxbuf[64]; + qse_char_t* kp; + qse_size_t kl; - count = 1; - uci_foreach_element(&uo->v.list, tmp) - { - md[count].key.ptr = QSE_T("value.idx"); - md[count].key.len = 7; - md[count].type = QSE_AWK_VAL_MAP_DATA_MBS; - md[count].vptr = tmp->name; - count++; + fld = qse_awk_rtx_makestrvalwithmbs (rtx, tmp->name); + if (!fld) + { + qse_awk_rtx_refupval (rtx, map); + qse_awk_rtx_refdownval (rtx, map); + map = QSE_NULL; + x = UCI_ERR_MEM; + break; } - tmp = qse_awk_rtx_makemapvalwithdata (rtx, md); - qse_awk_rtx_freemem (rtx, md); - if (tmp) *retv = tmp; - else x = UCI_ERR_MEM; - } - } - } - else x = UCI_ERR_NOTFOUND; - - } - else x = UCI_ERR_NOTFOUND; - } + subsep = qse_awk_rtx_getsubsep (rtx); - QSE_MMGR_FREE (mmgr, mtuple); + k[0].ptr = QSE_T("value"); + k[0].len = 5; + k[1].ptr = subsep->ptr; + k[1].len = subsep->len; + k[2].ptr = idxbuf; + k[2].len = qse_fmtuintmax (idxbuf, QSE_COUNTOF(idxbuf), count, 10, -1, QSE_T('\0'), QSE_NULL); + k[3].ptr = QSE_NULL; + k[3].len = 0; + + kp = qse_wcstradup (k, &kl, qse_awk_rtx_getmmgr(rtx)); + if (kp == QSE_NULL || qse_awk_rtx_setmapvalfld (rtx, map, kp, kl, fld) == QSE_NULL) + { + if (kp) QSE_MMGR_FREE (qse_awk_rtx_getmmgr(rtx), kp); + qse_awk_rtx_refupval (rtx, fld); + qse_awk_rtx_refdownval (rtx, fld); + qse_awk_rtx_refupval (rtx, map); + qse_awk_rtx_refdownval (rtx, map); + map = QSE_NULL; + x = UCI_ERR_MEM; + break; + } + + QSE_MMGR_FREE (qse_awk_rtx_getmmgr(rtx), kp); + count++; + } + + if (map) *retv = map; + } + else x = UCI_ERR_MEM; + } + else x = UCI_ERR_INVAL; /* uo->type */ + } + else x = UCI_ERR_NOTFOUND; /* e->type */ + } + else x = UCI_ERR_NOTFOUND; } - else x = UCI_ERR_MEM; } return -x; @@ -479,6 +605,41 @@ static int fnc_uci_errno (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) return 0; } +static qse_char_t* errmsg[] = +{ + QSE_T("no error"), + QSE_T("out of memory"), + QSE_T("invalid data"), + QSE_T("not found"), + QSE_T("I/O error"), + QSE_T("parse error"), + QSE_T("duplicate data"), + QSE_T("unknown error") +}; + +static int fnc_uci_errstr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t errnum; + + list = rtx_to_list (rtx, fi); + + if (qse_awk_rtx_getnargs (rtx) <= 0 || + qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &errnum) <= -1) + { + errnum = list->errnum; + } + + if (errnum < 0 || errnum >= QSE_COUNTOF(errmsg)) errnum = QSE_COUNTOF(errmsg) - 1; + + retv = qse_awk_rtx_makestrvalwithstr (rtx, errmsg[errnum]); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + static int fnc_uci_open (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { uctx_list_t* list; @@ -492,7 +653,7 @@ static int fnc_uci_open (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; ret = -1; } @@ -518,7 +679,7 @@ static int fnc_uci_close (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; ret = -1; } @@ -542,30 +703,20 @@ static int fnc_uci_load (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) if (ret <= -1) ret = -UCI_ERR_INVAL; else { - qse_awk_val_t* v; + qse_mchar_t* item; - v = qse_awk_rtx_getarg(rtx, 1); - if (v->type == QSE_AWK_VAL_STR) + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) { - ret = load_byid (rtx, list, id, ((qse_awk_val_str_t*)v)->val.ptr); - } - else - { - qse_awk_rtx_valtostr_out_t out; - out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - ret = qse_awk_rtx_valtostr (rtx, qse_awk_rtx_getarg (rtx, 1), &out); - if (ret <= -1) ret = -UCI_ERR_MEM; - else - { - ret = load_byid (rtx, list, id, out.u.cpldup.ptr); - qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); - } + ret = load_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); } + else ret = -UCI_ERR_MEM; } if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; ret = -1; } @@ -591,7 +742,7 @@ static int fnc_uci_unload (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; ret = -1; } @@ -613,11 +764,284 @@ static int fnc_uci_save (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); if (ret <= -1) ret = -UCI_ERR_INVAL; - else ret = save_byid (rtx, list, id); + else + { + qse_mchar_t* item; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) + { + ret = save_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); + } + else ret = -UCI_ERR_MEM; + } if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; + ret = -1; + } + + retv = qse_awk_rtx_makeintval (rtx, ret); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_uci_commit (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t id; + int ret; + + list = rtx_to_list (rtx, fi); + + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else + { + qse_mchar_t* item; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) + { + ret = commit_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); + } + else ret = -UCI_ERR_MEM; + } + + if (ret <= -1) + { + list->errnum = -ret; + ret = -1; + } + + retv = qse_awk_rtx_makeintval (rtx, ret); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_uci_revert (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t id; + int ret; + + list = rtx_to_list (rtx, fi); + + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else + { + qse_mchar_t* item; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) + { + ret = revert_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); + } + else ret = -UCI_ERR_MEM; + } + + if (ret <= -1) + { + list->errnum = -ret; + ret = -1; + } + + retv = qse_awk_rtx_makeintval (rtx, ret); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_uci_delete (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t id; + int ret; + + list = rtx_to_list (rtx, fi); + + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else + { + qse_mchar_t* item; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) + { + ret = delete_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); + } + else ret = -UCI_ERR_MEM; + } + + if (ret <= -1) + { + list->errnum = -ret; + ret = -1; + } + + retv = qse_awk_rtx_makeintval (rtx, ret); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_uci_rename (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t id; + int ret; + + list = rtx_to_list (rtx, fi); + + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else + { + qse_mchar_t* item; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) + { + ret = rename_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); + } + else ret = -UCI_ERR_MEM; + } + + if (ret <= -1) + { + list->errnum = -ret; + ret = -1; + } + + retv = qse_awk_rtx_makeintval (rtx, ret); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_uci_set (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t id; + int ret; + + list = rtx_to_list (rtx, fi); + + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else + { + qse_mchar_t* item; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) + { + ret = set_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); + } + else ret = -UCI_ERR_MEM; + } + + if (ret <= -1) + { + list->errnum = -ret; + ret = -1; + } + + retv = qse_awk_rtx_makeintval (rtx, ret); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_uci_addsection (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t id; + int ret; + + list = rtx_to_list (rtx, fi); + + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else + { + qse_mchar_t* item, * type; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + type = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 2), QSE_NULL); + if (item && type) + { + ret = addsection_byid (rtx, list, id, item, type); + } + else ret = -UCI_ERR_MEM; + + if (type) qse_awk_rtx_freemem (rtx, type); + if (item) qse_awk_rtx_freemem (rtx, item); + } + + if (ret <= -1) + { + list->errnum = -ret; + ret = -1; + } + + retv = qse_awk_rtx_makeintval (rtx, ret); + if (retv == QSE_NULL) return -1; + + qse_awk_rtx_setretval (rtx, retv); + return 0; +} + +static int fnc_uci_addlist (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + qse_long_t id; + int ret; + + list = rtx_to_list (rtx, fi); + + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else + { + qse_mchar_t* item; + + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) + { + ret = addlist_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); + } + else ret = -UCI_ERR_MEM; + } + + if (ret <= -1) + { + list->errnum = -ret; ret = -1; } @@ -641,30 +1065,20 @@ static int fnc_uci_setconfdir (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi if (ret <= -1) ret = -UCI_ERR_INVAL; else { - qse_awk_val_t* v; + qse_mchar_t* item; - v = qse_awk_rtx_getarg(rtx, 1); - if (v->type == QSE_AWK_VAL_STR) + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) { - ret = setconfdir_byid (rtx, list, id, ((qse_awk_val_str_t*)v)->val.ptr); - } - else - { - qse_awk_rtx_valtostr_out_t out; - out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - ret = qse_awk_rtx_valtostr (rtx, qse_awk_rtx_getarg (rtx, 1), &out); - if (ret <= -1) ret = -UCI_ERR_MEM; - else - { - ret = setconfdir_byid (rtx, list, id, out.u.cpldup.ptr); - qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); - } + ret = setconfdir_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); } + else ret = -UCI_ERR_MEM; } if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; ret = -1; } @@ -689,31 +1103,20 @@ static int fnc_uci_setsavedir (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi if (ret <= -1) ret = -UCI_ERR_INVAL; else { - qse_awk_val_t* v; + qse_mchar_t* item; - v = qse_awk_rtx_getarg(rtx, 1); - if (v->type == QSE_AWK_VAL_STR) + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) { - ret = setsavedir_byid (rtx, list, id, ((qse_awk_val_str_t*)v)->val.ptr); + ret = setsavedir_byid (rtx, list, id, item); + qse_awk_rtx_freemem (rtx, item); } - else - { - qse_awk_rtx_valtostr_out_t out; - out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - ret = qse_awk_rtx_valtostr (rtx, qse_awk_rtx_getarg (rtx, 1), &out); - if (ret <= -1) ret = -UCI_ERR_MEM; - else - { - ret = setsavedir_byid (rtx, list, id, out.u.cpldup.ptr); - qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); - } - } - + else ret = -UCI_ERR_MEM; } if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; ret = -1; } @@ -737,30 +1140,20 @@ static int fnc_uci_getoption (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) if (ret <= -1) ret = -UCI_ERR_INVAL; else { - qse_awk_val_t* v; + qse_mchar_t* item; - v = qse_awk_rtx_getarg(rtx, 1); - if (v->type == QSE_AWK_VAL_STR) + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) { - ret = getoption_byid (rtx, list, id, ((qse_awk_val_str_t*)v)->val.ptr, &retv); - } - else - { - qse_awk_rtx_valtostr_out_t out; - out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - ret = qse_awk_rtx_valtostr (rtx, qse_awk_rtx_getarg (rtx, 1), &out); - if (ret <= -1) ret = -UCI_ERR_MEM; - else - { - ret = getoption_byid (rtx, list, id, out.u.cpldup.ptr, &retv); - qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); - } + ret = getoption_byid (rtx, list, id, item, &retv); + qse_awk_rtx_freemem (rtx, item); } + else ret = -UCI_ERR_MEM; } if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; } else { @@ -779,52 +1172,31 @@ static int fnc_uci_getsection (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) list = rtx_to_list (rtx, fi); ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); - if (ret >= 0) + if (ret <= -1) ret = -UCI_ERR_INVAL; + else { - qse_awk_val_t* v; + qse_mchar_t* item; - v = qse_awk_rtx_getarg(rtx, 1); - if (v->type == QSE_AWK_VAL_STR) + item = qse_awk_rtx_valtombsdup (rtx, qse_awk_rtx_getarg(rtx, 1), QSE_NULL); + if (item) { - ret = getsection_byid (rtx, list, id, ((qse_awk_val_str_t*)v)->val.ptr, &retv); - } - else - { - qse_awk_rtx_valtostr_out_t out; - out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - ret = qse_awk_rtx_valtostr (rtx, qse_awk_rtx_getarg (rtx, 1), &out); - if (ret <= -1) ret = -UCI_ERR_MEM; - else - { - ret = getsection_byid (rtx, list, id, out.u.cpldup.ptr, &retv); - qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); - } + ret = getsection_byid (rtx, list, id, item, &retv); + qse_awk_rtx_freemem (rtx, item); } + else ret = -UCI_ERR_MEM; } - if (ret <= -1) + if (ret <= -1) { - list->errnum = ret; + list->errnum = -ret; } - else + else { qse_awk_rtx_setretval (rtx, retv); } return 0; } -static int fnc_uci_commit (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) -{ - //uci_commit (); - return 0 ; -} - -static int fnc_uci_revert (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) -{ - //uci_revert (); - return 0; -} - /* ------------------------------------------------------------------------ */ typedef struct fnctab_t fnctab_t; @@ -836,15 +1208,21 @@ struct fnctab_t static fnctab_t fnctab[] = { + { QSE_T("addlist"), { { 2, 2 }, fnc_uci_addlist } }, + { QSE_T("addsection"), { { 3, 3 }, fnc_uci_addsection } }, { QSE_T("close"), { { 1, 1 }, fnc_uci_close } }, - { QSE_T("commit"), { { 1, 1 }, fnc_uci_commit } }, + { QSE_T("commit"), { { 2, 2 }, fnc_uci_commit } }, + { QSE_T("delete"), { { 2, 2 }, fnc_uci_delete } }, { QSE_T("errno"), { { 0, 0 }, fnc_uci_errno } }, + { QSE_T("errstr"), { { 0, 1 }, fnc_uci_errstr } }, { QSE_T("getoption"), { { 2, 2 }, fnc_uci_getoption } }, { QSE_T("getsection"), { { 2, 2 }, fnc_uci_getsection } }, { QSE_T("load"), { { 2, 2 }, fnc_uci_load } }, { QSE_T("open"), { { 0, 0 }, fnc_uci_open } }, - { QSE_T("revert"), { { 1, 1 }, fnc_uci_revert } }, - { QSE_T("save"), { { 1, 1 }, fnc_uci_save } }, + { QSE_T("rename"), { { 2, 2 }, fnc_uci_rename } }, + { QSE_T("revert"), { { 2, 2 }, fnc_uci_revert } }, + { QSE_T("save"), { { 2, 2 }, fnc_uci_save } }, + { QSE_T("set"), { { 2, 2 }, fnc_uci_set } }, { QSE_T("setconfdir"), { { 2, 2 }, fnc_uci_setconfdir } }, { QSE_T("setsavedir"), { { 2, 2 }, fnc_uci_setsavedir } }, { QSE_T("unload"), { { 1, 1 }, fnc_uci_unload } } diff --git a/qse/samples/awk/awk03.c b/qse/samples/awk/awk03.c index c6d3aa22..1e430746 100644 --- a/qse/samples/awk/awk03.c +++ b/qse/samples/awk/awk03.c @@ -103,7 +103,7 @@ int main () ret = -1; goto oops; } - str = qse_awk_rtx_valtocpldup (rtx, v, &len); + str = qse_awk_rtx_valtostrdup (rtx, v, &len); if (str == QSE_NULL) { qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"), diff --git a/qse/samples/awk/awk04.c b/qse/samples/awk/awk04.c index c5d30dc3..fcbdcaf8 100644 --- a/qse/samples/awk/awk04.c +++ b/qse/samples/awk/awk04.c @@ -111,7 +111,7 @@ int main () ret = -1; goto oops; } - str = qse_awk_rtx_valtocpldup (rtx, rtv, &len); + str = qse_awk_rtx_valtostrdup (rtx, rtv, &len); if (str == QSE_NULL) { qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"), diff --git a/qse/samples/awk/awk10.c b/qse/samples/awk/awk10.c index 5c099d60..88b66faa 100644 --- a/qse/samples/awk/awk10.c +++ b/qse/samples/awk/awk10.c @@ -105,7 +105,7 @@ int main () { qse_awk_val_t* v, * fv; - fv = qse_awk_rtx_makestrval0 (rtx, xxx[i].vptr); + fv = qse_awk_rtx_makestrvalwithstr (rtx, xxx[i].vptr); if (fv == QSE_NULL) { qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"), @@ -143,7 +143,7 @@ int main () { qse_xstr_t str; - str.ptr = qse_awk_rtx_valtocpldup ( + str.ptr = qse_awk_rtx_valtostrdup ( rtx, QSE_AWK_VAL_MAP_ITR_VAL(iptr), &str.len); if (str.ptr == QSE_NULL) { @@ -166,7 +166,7 @@ int main () { qse_xstr_t str; - str.ptr = qse_awk_rtx_valtocpldup (rtx, rtv, &str.len); + str.ptr = qse_awk_rtx_valtostrdup (rtx, rtv, &str.len); if (str.ptr == QSE_NULL) { qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),