diff --git a/qse/cmd/awk/awk.c b/qse/cmd/awk/awk.c index aac3f210..5a0e0931 100644 --- a/qse/cmd/awk/awk.c +++ b/qse/cmd/awk/awk.c @@ -105,8 +105,7 @@ struct arg_t struct gvmv_t { int idx; - qse_char_t* ptr; - qse_size_t len; + qse_cstr_t str; }; static void dprint (const qse_char_t* fmt, ...) @@ -285,7 +284,7 @@ static qse_htb_walk_t set_global ( qse_awk_rtx_t* rtx = (qse_awk_rtx_t*)arg; struct gvmv_t* gvmv = (struct gvmv_t*)QSE_HTB_VPTR(pair); - v = qse_awk_rtx_makenstrval (rtx, gvmv->ptr, gvmv->len); + v = qse_awk_rtx_makenstrvalwithcstr (rtx, &gvmv->str); if (v == QSE_NULL) return QSE_HTB_WALK_STOP; qse_awk_rtx_refupval (rtx, v); @@ -302,7 +301,7 @@ static int apply_fs_and_gvm (qse_awk_rtx_t* rtx, struct arg_t* arg) qse_awk_val_t* fs; /* compose a string value to use to set FS to */ - fs = qse_awk_rtx_makestrval0 (rtx, arg->fs); + fs = qse_awk_rtx_makestrvalwithstr (rtx, arg->fs); if (fs == QSE_NULL) return -1; /* change FS according to the command line argument */ @@ -666,8 +665,8 @@ static int comparg (int argc, qse_char_t* argv[], struct arg_t* arg) *eq = QSE_T('\0'); gvmv.idx = -1; - gvmv.ptr = ++eq; - gvmv.len = qse_strlen(eq); + gvmv.str.ptr = ++eq; + gvmv.str.len = qse_strlen(eq); if (qse_htb_upsert (gvm, opt.arg, qse_strlen(opt.arg), &gvmv, 1) == QSE_NULL) { diff --git a/qse/include/qse/awk/awk.h b/qse/include/qse/awk/awk.h index 47d45af1..7e708c8b 100644 --- a/qse/include/qse/awk/awk.h +++ b/qse/include/qse/awk/awk.h @@ -279,6 +279,31 @@ typedef struct qse_awk_val_map_itr_t qse_awk_val_map_itr_t; #define QSE_AWK_VAL_MAP_ITR_VAL(itr) \ ((const qse_awk_val_t*)QSE_HTB_VPTR((itr)->pair)) + +enum qse_awk_val_map_data_type_t +{ + QSE_AWK_VAL_MAP_DATA_INT = 0, + QSE_AWK_VAL_MAP_DATA_FLT, + QSE_AWK_VAL_MAP_DATA_STR, + QSE_AWK_VAL_MAP_DATA_MBS, + QSE_AWK_VAL_MAP_DATA_WCS, + QSE_AWK_VAL_MAP_DATA_CSTR, + QSE_AWK_VAL_MAP_DATA_XSTR, + QSE_AWK_VAL_MAP_DATA_MCSTR, + QSE_AWK_VAL_MAP_DATA_MXSTR, + QSE_AWK_VAL_MAP_DATA_WCSTR, + QSE_AWK_VAL_MAP_DATA_WXSTR +}; +typedef enum qse_awk_val_map_data_type_t qse_awk_val_map_data_type_t; + +struct qse_awk_val_map_data_t +{ + qse_cstr_t key; + qse_awk_val_map_data_type_t type; + void* vptr; +}; +typedef struct qse_awk_val_map_data_t qse_awk_val_map_data_t; + /** * The qse_awk_nde_type_t defines the node types. */ @@ -2113,8 +2138,7 @@ int qse_awk_rtx_clrrec ( int qse_awk_rtx_setrec ( qse_awk_rtx_t* rtx, /**< runtime context */ qse_size_t idx, /**< 0 for $0, N for $N */ - const qse_char_t* str, /**< string pointer */ - qse_size_t len /**< string length */ + const qse_cstr_t* str /**< string */ ); /** @@ -2145,14 +2169,53 @@ qse_awk_val_t* qse_awk_rtx_makefltval ( ); /** - * The qse_awk_rtx_makestrval0() function creates a string value. + * The qse_awk_rtx_makestrvalwithstr() function creates a string value. * @return value on success, QSE_NULL on failure */ -qse_awk_val_t* qse_awk_rtx_makestrval0 ( +qse_awk_val_t* qse_awk_rtx_makestrvalwithstr ( qse_awk_rtx_t* rtx, const qse_char_t* str ); +/** + * The qse_awk_rtx_makestrvalwithmbs() function creates a string value + * from a null-terminated multibyte string. + * @return value on success, QSE_NULL on failure + */ +qse_awk_val_t* qse_awk_rtx_makestrvalwithmbs ( + qse_awk_rtx_t* rtx, + const qse_mchar_t* mbs +); + +/** + * The qse_awk_rtx_makestrvalwithwcs() function creates a string value + * from a null-terminated wide-character string. + * @return value on success, QSE_NULL on failure + */ +qse_awk_val_t* qse_awk_rtx_makestrvalwithwcs ( + qse_awk_rtx_t* rtx, + const qse_wchar_t* wcs +); + +/** + * The qse_awk_rtx_makestrvalwithcstr() function creates a string value. + * @return value on success, QSE_NULL on failure + */ +qse_awk_val_t* qse_awk_rtx_makestrvalwithcstr ( + qse_awk_rtx_t* rtx, + const qse_cstr_t* str +); + +qse_awk_val_t* qse_awk_rtx_makestrvalwithmcstr ( + qse_awk_rtx_t* rtx, + const qse_mcstr_t* mcstr +); + +qse_awk_val_t* qse_awk_rtx_makestrvalwithwcstr ( + qse_awk_rtx_t* rtx, + const qse_wcstr_t* wcstr +); + /** * The qse_awk_rtx_makestrval() function creates a string value. * @return value on success, QSE_NULL on failure @@ -2163,18 +2226,6 @@ qse_awk_val_t* qse_awk_rtx_makestrval ( qse_size_t len ); -/** - * The qse_awk_rtx_makestrval_nodup() function creates a string value. - * The @a len character array pointed to by @a str is not duplicated. - * Instead the pointer and the lengh are just reused. - * @return value on success, QSE_NULL on failure - */ -qse_awk_val_t* qse_awk_rtx_makestrval_nodup ( - qse_awk_rtx_t* rtx, - qse_char_t* str, - qse_size_t len -); - /** * The qse_awk_rtx_makestrval2() function creates a string value combining * two strings. @@ -2194,10 +2245,9 @@ qse_awk_val_t* qse_awk_rtx_makestrval2 ( * is 1. * @return value on success, QSE_NULL on failure */ -qse_awk_val_t* qse_awk_rtx_makenstrval ( +qse_awk_val_t* qse_awk_rtx_makenstrvalwithcstr ( qse_awk_rtx_t* rtx, - const qse_char_t* str, - qse_size_t len + const qse_cstr_t* str ); /** @@ -2219,6 +2269,10 @@ qse_awk_val_t* qse_awk_rtx_makemapval ( qse_awk_rtx_t* rtx ); +qse_awk_val_t* qse_awk_rtx_makemapvalwithdata ( + qse_awk_rtx_t* rtx, + qse_awk_val_map_data_t data[] +); /** * The qse_awk_rtx_setmapvalfld() function sets a field value in a map. diff --git a/qse/include/qse/cmn/mbwc.h b/qse/include/qse/cmn/mbwc.h index 07ce0fa7..8df936e4 100644 --- a/qse/include/qse/cmn/mbwc.h +++ b/qse/include/qse/cmn/mbwc.h @@ -138,6 +138,22 @@ int qse_mbsntowcsnuptowithcmgr ( qse_cmgr_t* cmgr ); +qse_wchar_t* qse_mbsntowcsdupwithcmgr ( + const qse_mchar_t* mbs, + qse_size_t* mbslen, + qse_size_t* wcslen, + qse_mmgr_t* mmgr, + qse_cmgr_t* cmgr +); + +qse_wchar_t* qse_mbsntowcsalldupwithcmgr ( + const qse_mchar_t* mbs, + qse_size_t* mbslen, + qse_size_t* wcslen, + qse_mmgr_t* mmgr, + qse_cmgr_t* cmgr +); + qse_wchar_t* qse_mbstowcsdupwithcmgr ( const qse_mchar_t* mbs, qse_size_t* wcslen, @@ -295,6 +311,20 @@ int qse_mbsntowcsnupto ( qse_wchar_t stopper ); +qse_wchar_t* qse_mbsntowcsdup ( + const qse_mchar_t* mbs, + qse_size_t* mbslen, + qse_size_t* wcslen, + qse_mmgr_t* mmgr +); + +qse_wchar_t* qse_mbsntowcsalldup ( + const qse_mchar_t* mbs, + qse_size_t* mbslen, + qse_size_t* wcslen, + qse_mmgr_t* mmgr +); + qse_wchar_t* qse_mbstowcsdup ( const qse_mchar_t* mbs, qse_size_t* wcslen, diff --git a/qse/include/qse/cmn/path.h b/qse/include/qse/cmn/path.h index 50950a42..a867113e 100644 --- a/qse/include/qse/cmn/path.h +++ b/qse/include/qse/cmn/path.h @@ -33,7 +33,7 @@ * segment in a path name. It maps to qse_mbsbasename() if #QSE_CHAR_IS_MCHAR * is defined; it maps to qse_wcsbasename() if #QSE_CHAR_IS_WCHAR is defined. */ -#ifdef QSE_CHAR_IS_MCHAR +#if defined(QSE_CHAR_IS_MCHAR) # define qse_basename(path) qse_mbsbasename(path) #else # define qse_basename(path) qse_wcsbasename(path) diff --git a/qse/lib/awk/Awk.cpp b/qse/lib/awk/Awk.cpp index d1210237..e972cb95 100644 --- a/qse/lib/awk/Awk.cpp +++ b/qse/lib/awk/Awk.cpp @@ -593,7 +593,7 @@ int Awk::Value::setStr (const char_t* str) int Awk::Value::setStr (Run* r, const char_t* str) { val_t* tmp; - tmp = qse_awk_rtx_makestrval0 (r->rtx, str); + tmp = qse_awk_rtx_makestrvalwithstr (r->rtx, str); if (tmp == QSE_NULL) { r->awk->retrieveError (r); @@ -751,7 +751,7 @@ int Awk::Value::setIndexedStr (const Index& idx, const char_t* str) int Awk::Value::setIndexedStr (Run* r, const Index& idx, const char_t* str) { val_t* tmp; - tmp = qse_awk_rtx_makestrval0 (r->rtx, str); + tmp = qse_awk_rtx_makestrvalwithstr (r->rtx, str); if (tmp == QSE_NULL) { r->awk->retrieveError (r); diff --git a/qse/lib/awk/awk.h b/qse/lib/awk/awk.h index 6a7dea03..c5dd6c5d 100644 --- a/qse/lib/awk/awk.h +++ b/qse/lib/awk/awk.h @@ -244,8 +244,8 @@ struct qse_awk_rtx_t qse_size_t stack_limit; int exit_level; - qse_awk_val_ref_t* fcache[128]; - qse_size_t fcache_count; + qse_awk_val_ref_t* rcache[128]; + qse_size_t rcache_count; #ifdef ENABLE_FEATURE_SCACHE qse_awk_val_str_t* scache diff --git a/qse/lib/awk/fnc.c b/qse/lib/awk/fnc.c index 1ce4a0ba..82c6c91c 100644 --- a/qse/lib/awk/fnc.c +++ b/qse/lib/awk/fnc.c @@ -774,7 +774,7 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) QSE_ASSERT ((tok.ptr != QSE_NULL && tok.len > 0) || tok.len == 0); /* create the field string */ - t2 = qse_awk_rtx_makestrval (run, tok.ptr, tok.len); + t2 = qse_awk_rtx_makestrvalwithcstr (run, &tok); if (t2 == QSE_NULL) goto oops; /* put it into the map */ @@ -815,9 +815,9 @@ oops: static int fnc_tolower (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) { qse_size_t nargs; - qse_char_t* str; - qse_size_t len, i; + qse_size_t i; qse_awk_val_t* a0, * r; + qse_xstr_t str; nargs = qse_awk_rtx_getnargs (run); QSE_ASSERT (nargs == 1); @@ -826,25 +826,25 @@ static int fnc_tolower (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) if (a0->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)a0)->val.ptr; - len = ((qse_awk_val_str_t*)a0)->val.len; + str.ptr = ((qse_awk_val_str_t*)a0)->val.ptr; + str.len = ((qse_awk_val_str_t*)a0)->val.len; } else { - str = qse_awk_rtx_valtocpldup (run, a0, &len); - if (str == QSE_NULL) return -1; + str.ptr = qse_awk_rtx_valtocpldup (run, a0, &str.len); + if (str.ptr == QSE_NULL) return -1; } - for (i = 0; i < len; i++) str[i] = QSE_AWK_TOLOWER (run->awk, str[i]); + for (i = 0; i < str.len; i++) str.ptr[i] = QSE_AWK_TOLOWER (run->awk, str.ptr[i]); - r = qse_awk_rtx_makestrval (run, str, len); + r = qse_awk_rtx_makestrvalwithcstr (run, &str); if (r == QSE_NULL) { - if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str); + if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr); return -1; } - if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str); + if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr); qse_awk_rtx_setretval (run, r); return 0; } @@ -852,9 +852,9 @@ static int fnc_tolower (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) static int fnc_toupper (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) { qse_size_t nargs; - qse_char_t* str; - qse_size_t len, i; + qse_size_t i; qse_awk_val_t* a0, * r; + qse_xstr_t str; nargs = qse_awk_rtx_getnargs (run); QSE_ASSERT (nargs == 1); @@ -863,25 +863,25 @@ static int fnc_toupper (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) if (a0->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)a0)->val.ptr; - len = ((qse_awk_val_str_t*)a0)->val.len; + str.ptr = ((qse_awk_val_str_t*)a0)->val.ptr; + str.len = ((qse_awk_val_str_t*)a0)->val.len; } else { - str = qse_awk_rtx_valtocpldup (run, a0, &len); - if (str == QSE_NULL) return -1; + str.ptr = qse_awk_rtx_valtocpldup (run, a0, &str.len); + if (str.ptr == QSE_NULL) return -1; } - for (i = 0; i < len; i++) str[i] = QSE_AWK_TOUPPER (run->awk, str[i]); + for (i = 0; i < str.len; i++) str.ptr[i] = QSE_AWK_TOUPPER (run->awk, str.ptr[i]); - r = qse_awk_rtx_makestrval (run, str, len); + r = qse_awk_rtx_makestrvalwithcstr (run, &str); if (r == QSE_NULL) { - if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str); + if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr); return -1; } - if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str); + if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr); qse_awk_rtx_setretval (run, r); return 0; } @@ -1136,8 +1136,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) if (a2 == QSE_NULL) { int n; - n = qse_awk_rtx_setrec (run, 0, - QSE_STR_PTR(&new), QSE_STR_LEN(&new)); + n = qse_awk_rtx_setrec (run, 0, QSE_STR_CSTR(&new)); if (n <= -1) goto oops; } else if (((qse_awk_val_ref_t*)a2)->id == QSE_AWK_VAL_REF_POS) @@ -1146,14 +1145,13 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) n = qse_awk_rtx_setrec ( run, (qse_size_t)((qse_awk_val_ref_t*)a2)->adr, - QSE_STR_PTR(&new), QSE_STR_LEN(&new)); + QSE_STR_CSTR(&new)); if (n <= -1) goto oops; } else { - v = qse_awk_rtx_makestrval (run, - QSE_STR_PTR(&new), QSE_STR_LEN(&new)); + v = qse_awk_rtx_makestrvalwithcstr (run, QSE_STR_CSTR(&new)); if (v == QSE_NULL) goto oops; qse_awk_rtx_refdownval (run, *a2_ref); @@ -1390,12 +1388,10 @@ static int fnc_sprintf (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, cs0.ptr); if (x.ptr == QSE_NULL) goto oops; - /*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_makestrvalwithcstr (run, &x); if (a0 == QSE_NULL) goto oops; qse_str_fini (&fbu); - /*qse_str_yield (&out, QSE_NULL, 0);*/ qse_str_fini (&out); qse_awk_rtx_setretval (run, a0); return 0; diff --git a/qse/lib/awk/mpi.c b/qse/lib/awk/mpi.c index 0687a11a..3009dd4a 100644 --- a/qse/lib/awk/mpi.c +++ b/qse/lib/awk/mpi.c @@ -137,35 +137,17 @@ qse_awk_rtx_t* qse_awk_rtx_openmpi ( case 0: /* MPI_HOST */ { char buf[MPI_MAX_PROCESSOR_NAME]; - int len; -#if defined(QSE_CHAR_IS_MCHAR) - /* nothing */ -#else - qse_mmgr_t* mmgr; - qse_char_t* tmp; -#endif - if (MPI_Get_processor_name(buf, &len) != MPI_SUCCESS) + qse_mcstr_t mcstr; + + if (MPI_Get_processor_name(buf, &mcstr.len) != MPI_SUCCESS) { qse_awk_rtx_close (rtx); qse_awk_seterrnum (awk, QSE_AWK_ESYSERR, QSE_NULL); return QSE_NULL; } -#if defined(QSE_CHAR_IS_MCHAR) - v_tmp = qse_awk_rtx_makestrval (rtx, buf, len); -#else - - mmgr = qse_awk_getmmgr(awk); - tmp = qse_mbstowcsdup (buf, QSE_NULL, mmgr); - if (tmp == QSE_NULL) - { - qse_awk_rtx_close (rtx); - qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; - } - - v_tmp = qse_awk_rtx_makestrval0 (rtx, tmp); - QSE_MMGR_FREE (mmgr, tmp); -#endif + + mcstr.ptr = buf; + v_tmp = qse_awk_rtx_makestrvalwithmcstr (rtx, &mcstr); break; } diff --git a/qse/lib/awk/rec.c b/qse/lib/awk/rec.c index f54c5b82..a0d91d6f 100644 --- a/qse/lib/awk/rec.c +++ b/qse/lib/awk/rec.c @@ -22,19 +22,17 @@ static int split_record (qse_awk_rtx_t* run); static int recomp_record_fields ( - qse_awk_rtx_t* run, qse_size_t lv, - const qse_char_t* str, qse_size_t len); + qse_awk_rtx_t* run, qse_size_t lv, const qse_cstr_t* str); int qse_awk_rtx_setrec ( - qse_awk_rtx_t* run, qse_size_t idx, - const qse_char_t* str, qse_size_t len) + qse_awk_rtx_t* run, qse_size_t idx, const qse_cstr_t* str) { qse_awk_val_t* v; if (idx == 0) { - if (str == QSE_STR_PTR(&run->inrec.line) && - len == QSE_STR_LEN(&run->inrec.line)) + if (str->ptr == QSE_STR_PTR(&run->inrec.line) && + str->len == QSE_STR_LEN(&run->inrec.line)) { if (qse_awk_rtx_clrrec (run, 1) == -1) return -1; } @@ -42,7 +40,7 @@ int qse_awk_rtx_setrec ( { if (qse_awk_rtx_clrrec (run, 0) == -1) return -1; - if (qse_str_ncpy (&run->inrec.line, str, len) == (qse_size_t)-1) + if (qse_str_ncpy (&run->inrec.line, str->ptr, str->len) == (qse_size_t)-1) { qse_awk_rtx_clrrec (run, 0); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); @@ -50,7 +48,7 @@ int qse_awk_rtx_setrec ( } } - v = qse_awk_rtx_makenstrval (run, str, len); + v = qse_awk_rtx_makenstrvalwithcstr (run, str); if (v == QSE_NULL) { @@ -72,16 +70,14 @@ int qse_awk_rtx_setrec ( } else { - if (recomp_record_fields (run, idx, str, len) == -1) + if (recomp_record_fields (run, idx, str) <= -1) { qse_awk_rtx_clrrec (run, 0); return -1; } /* recompose $0 */ - v = qse_awk_rtx_makestrval (run, - QSE_STR_PTR(&run->inrec.line), - QSE_STR_LEN(&run->inrec.line)); + v = qse_awk_rtx_makestrvalwithcstr (run, QSE_STR_CSTR(&run->inrec.line)); if (v == QSE_NULL) { qse_awk_rtx_clrrec (run, 0); @@ -328,14 +324,11 @@ static int split_record (qse_awk_rtx_t* rtx) rtx->inrec.flds[rtx->inrec.nflds].ptr = tok.ptr; rtx->inrec.flds[rtx->inrec.nflds].len = tok.len; - - rtx->inrec.flds[rtx->inrec.nflds].val = - qse_awk_rtx_makenstrval (rtx, tok.ptr, tok.len); + rtx->inrec.flds[rtx->inrec.nflds].val = qse_awk_rtx_makenstrvalwithcstr (rtx, &tok); if (rtx->inrec.flds[rtx->inrec.nflds].val == QSE_NULL) { - if (fs_free != QSE_NULL) - QSE_AWK_FREE (rtx->awk, fs_free); + if (fs_free) QSE_AWK_FREE (rtx->awk, fs_free); return -1; } @@ -402,8 +395,7 @@ int qse_awk_rtx_clrrec (qse_awk_rtx_t* run, int skip_inrec_line) } static int recomp_record_fields ( - qse_awk_rtx_t* run, qse_size_t lv, - const qse_char_t* str, qse_size_t len) + qse_awk_rtx_t* run, qse_size_t lv, const qse_cstr_t* str) { qse_awk_val_t* v; qse_size_t max, i, nflds; @@ -444,10 +436,7 @@ static int recomp_record_fields ( { if (i > 0) { - if (qse_str_ncat ( - &run->inrec.line, - run->gbl.ofs.ptr, - run->gbl.ofs.len) == (qse_size_t)-1) + if (qse_str_ncat (&run->inrec.line, run->gbl.ofs.ptr, run->gbl.ofs.len) == (qse_size_t)-1) { qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); return -1; @@ -461,16 +450,15 @@ static int recomp_record_fields ( run->inrec.flds[i].ptr = QSE_STR_PTR(&run->inrec.line) + QSE_STR_LEN(&run->inrec.line); - run->inrec.flds[i].len = len; + run->inrec.flds[i].len = str->len; - if (qse_str_ncat ( - &run->inrec.line, str, len) == (qse_size_t)-1) + if (qse_str_ncat (&run->inrec.line, str->ptr, str->len) == (qse_size_t)-1) { qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); return -1; } - tmp = qse_awk_rtx_makestrval (run, str,len); + tmp = qse_awk_rtx_makestrvalwithcstr (run, str); if (tmp == QSE_NULL) return -1; if (i < nflds) @@ -487,8 +475,7 @@ static int recomp_record_fields ( QSE_STR_LEN(&run->inrec.line); run->inrec.flds[i].len = 0; - if (qse_str_cat ( - &run->inrec.line, QSE_T("")) == (qse_size_t)-1) + if (qse_str_cat (&run->inrec.line, QSE_T("")) == (qse_size_t)-1) { qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); return -1; diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index b79079e5..093bfa03 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -1093,9 +1093,9 @@ static void fini_rtx (qse_awk_rtx_t* rtx, int fini_globals) qse_htb_close (rtx->named); /* destroy values in free list */ - while (rtx->fcache_count > 0) + while (rtx->rcache_count > 0) { - qse_awk_val_ref_t* tmp = rtx->fcache[--rtx->fcache_count]; + qse_awk_val_ref_t* tmp = rtx->rcache[--rtx->rcache_count]; qse_awk_rtx_freeval (rtx, (qse_awk_val_t*)tmp, 0); } @@ -1240,7 +1240,7 @@ static int defaultify_globals (qse_awk_rtx_t* rtx) } else { - tmp = qse_awk_rtx_makestrval0 (rtx, gtab[i].str); + tmp = qse_awk_rtx_makestrvalwithstr (rtx, gtab[i].str); if (tmp == QSE_NULL) return -1; } @@ -1600,7 +1600,7 @@ qse_awk_val_t* qse_awk_rtx_callwithstrs ( for (i = 0; i < nargs; i++) { - v[i] = qse_awk_rtx_makestrval0 (rtx, args[i]); + v[i] = qse_awk_rtx_makestrvalwithstr (rtx, args[i]); if (v[i] == QSE_NULL) { ret = QSE_NULL; @@ -3695,7 +3695,7 @@ static qse_awk_val_t* do_assignment_pos ( str = out.u.cpldup; } - n = qse_awk_rtx_setrec (run, (qse_size_t)lv, str.ptr, str.len); + n = qse_awk_rtx_setrec (run, (qse_size_t)lv, &str); if (val->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr); @@ -6456,14 +6456,14 @@ read_console_again: if (p->var == QSE_NULL) { /* set $0 with the input value */ - x = qse_awk_rtx_setrec (rtx, 0, QSE_STR_PTR(buf), QSE_STR_LEN(buf)); + x = qse_awk_rtx_setrec (rtx, 0, QSE_STR_CSTR(buf)); if (x <= -1) return QSE_NULL; } else { qse_awk_val_t* v; - v = qse_awk_rtx_makestrval (rtx, QSE_STR_PTR(buf), QSE_STR_LEN(buf)); + v = qse_awk_rtx_makestrvalwithcstr (rtx, QSE_STR_CSTR(buf)); if (v == QSE_NULL) { ADJERR_LOC (rtx, &nde->loc); @@ -6562,7 +6562,7 @@ read_again: } } - if (qse_awk_rtx_setrec (rtx, 0, QSE_STR_PTR(buf), QSE_STR_LEN(buf)) <= -1 || + if (qse_awk_rtx_setrec (rtx, 0, QSE_STR_CSTR(buf)) <= -1 || update_fnr (rtx, rtx->gbl.fnr + 1, rtx->gbl.nr + 1) <= -1) return -1; return 1; @@ -6644,8 +6644,7 @@ static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds) if (ofs_free != QSE_NULL) QSE_AWK_FREE (run->awk, ofs_free); if (nflds > 1) qse_awk_rtx_refdownval (run, v); - v = (qse_awk_val_t*) qse_awk_rtx_makestrval ( - run, QSE_STR_PTR(&tmp), QSE_STR_LEN(&tmp)); + v = (qse_awk_val_t*) qse_awk_rtx_makestrvalwithcstr (run, QSE_STR_CSTR(&tmp)); if (v == QSE_NULL) { qse_str_fini (&tmp); diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index ba328312..993c2c6c 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -1620,7 +1620,7 @@ static int build_argcv ( qse_awk_rtx_refupval (rtx, v_argv); /* make ARGV[0] */ - v_tmp = qse_awk_rtx_makestrval0 (rtx, id); + v_tmp = qse_awk_rtx_makestrvalwithstr (rtx, id); if (v_tmp == QSE_NULL) { qse_awk_rtx_refdownval (rtx, v_argv); @@ -1652,7 +1652,7 @@ static int build_argcv ( { for (argc = 1, p = icf; *p; p++, argc++) { - v_tmp = qse_awk_rtx_makestrval0 (rtx, *p); + v_tmp = qse_awk_rtx_makestrvalwithstr (rtx, *p); if (v_tmp == QSE_NULL) { qse_awk_rtx_refdownval (rtx, v_argv); @@ -1779,7 +1779,7 @@ static int __build_environ ( *eq = QSE_WT('='); #endif - v_tmp = qse_awk_rtx_makestrval0 (rtx, vptr); + v_tmp = qse_awk_rtx_makestrvalwithstr (rtx, vptr); if (v_tmp == QSE_NULL) { #if ((defined(QSE_ENV_CHAR_IS_MCHAR) && defined(QSE_CHAR_IS_MCHAR)) || \ @@ -2461,7 +2461,7 @@ static int fnc_getioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) #if defined(QSE_CHAR_IS_WCHAR) else if (qse_strcasecmp (ptr[1], QSE_T("codepage")) == 0) { - rv = qse_awk_rtx_makestrval0 (rtx, ioattr->cmgr_name); + rv = qse_awk_rtx_makestrvalwithstr (rtx, ioattr->cmgr_name); if (rv == QSE_NULL) { ret = -1; diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index 83a8699d..7949539e 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -19,6 +19,7 @@ */ #include "awk.h" +#include #ifdef DEBUG_VAL #include @@ -64,7 +65,7 @@ qse_awk_val_t* qse_awk_val_one = (qse_awk_val_t*)&awk_int[2]; qse_awk_val_t* qse_awk_rtx_makenilval (qse_awk_rtx_t* rtx) { - return &awk_nil; + return (qse_awk_val_t*)&awk_nil; } qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* rtx, qse_long_t v) @@ -187,17 +188,109 @@ qse_awk_val_t* qse_awk_rtx_makefltval (qse_awk_rtx_t* rtx, qse_flt_t v) return (qse_awk_val_t*)val; } -qse_awk_val_t* qse_awk_rtx_makestrval0 ( +qse_awk_val_t* qse_awk_rtx_makestrvalwithmbs ( + qse_awk_rtx_t* rtx, const qse_mchar_t* mbs) +{ +#if defined(QSE_CHAR_IS_MCHAR) + return qse_awk_rtx_makestrval (rtx, mbs, qse_mbslen(mbs)); +#else + qse_awk_val_t* v; + qse_wxstr_t tmp; + + tmp.ptr = qse_mbstowcsdup (mbs, &tmp.len, rtx->awk->mmgr); + if (tmp.ptr == QSE_NULL) + { + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); + return QSE_NULL; + } + + v = qse_awk_rtx_makestrvalwithcstr (rtx, &tmp); + QSE_AWK_FREE (rtx->awk, tmp.ptr); + return v; +#endif +} + +qse_awk_val_t* qse_awk_rtx_makestrvalwithwcs ( + qse_awk_rtx_t* rtx, const qse_wchar_t* wcs) +{ +#if defined(QSE_CHAR_IS_MCHAR) + qse_awk_val_t* v; + qse_mxstr_t tmp; + + tmp.ptr = qse_wcstombsdup (wcs, &tmp.len, rtx->awk->mmgr); + if (tmp.ptr == QSE_NULL) + { + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); + return QSE_NULL; + } + + v = qse_awk_rtx_makestrvalwithcstr (rtx, &tmp); + QSE_AWK_FREE (rtx->awk, tmp.ptr); + return v; +#else + return qse_awk_rtx_makestrval (rtx, wcs, qse_wcslen(wcs)); +#endif +} + +qse_awk_val_t* qse_awk_rtx_makestrvalwithstr ( qse_awk_rtx_t* rtx, const qse_char_t* str) { return qse_awk_rtx_makestrval (rtx, str, qse_strlen(str)); } -qse_awk_val_t* qse_awk_rtx_makestrval ( - qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t len) +qse_awk_val_t* qse_awk_rtx_makestrvalwithmcstr ( + qse_awk_rtx_t* rtx, const qse_mcstr_t* mcstr) +{ +#if defined(QSE_CHAR_IS_MCHAR) + return qse_awk_rtx_makestrvalwithcstr (rtx, mcstr); +#else + qse_awk_val_t* v; + qse_wcstr_t tmp; + qse_size_t mbslen; + + mbslen = mcstr->len; + tmp.ptr = qse_mbsntowcsdup (mcstr->ptr, &mbslen, &tmp.len, rtx->awk->mmgr); + if (tmp.ptr == QSE_NULL) + { + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); + return QSE_NULL; + } + + v = qse_awk_rtx_makestrvalwithcstr (rtx, &tmp); + QSE_AWK_FREE (rtx->awk, tmp.ptr); + return v; +#endif +} + +qse_awk_val_t* qse_awk_rtx_makestrvalwithwcstr ( + qse_awk_rtx_t* rtx, const qse_wcstr_t* wcstr) +{ +#if defined(QSE_CHAR_IS_MCHAR) + qse_awk_val_t* v; + qse_mcstr_t tmp; + qse_size_t wcslen; + + wcslen = wcstr->len; + tmp.ptr = qse_wcsntombsdup (wcstr->ptr, &wcslen, &tmp.len, rtx->awk->mmgr); + if (tmp.ptr == QSE_NULL) + { + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); + return QSE_NULL; + } + + v = qse_awk_rtx_makestrvalwithcstr (rtx, &tmp); + QSE_AWK_FREE (rtx->awk, tmp.ptr); + return v; +#else + return qse_awk_rtx_makestrvalwithcstr (rtx, wcstr); +#endif +} + +qse_awk_val_t* qse_awk_rtx_makestrvalwithcstr ( + qse_awk_rtx_t* rtx, const qse_cstr_t* str) { qse_awk_val_str_t* val = QSE_NULL; - qse_size_t rlen = len; + qse_size_t rlen = str->len; #ifdef ENABLE_FEATURE_SCACHE qse_size_t i; @@ -217,7 +310,7 @@ qse_awk_val_t* qse_awk_rtx_makestrval ( val = (qse_awk_val_str_t*) QSE_AWK_ALLOC ( rtx->awk, QSE_SIZEOF(qse_awk_val_str_t) + - (rlen+1)*QSE_SIZEOF(qse_char_t)); + (rlen + 1) * QSE_SIZEOF(qse_char_t)); if (val == QSE_NULL) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); @@ -230,9 +323,9 @@ init: val->type = QSE_AWK_VAL_STR; val->ref = 0; val->nstr = 0; - val->val.len = len; + val->val.len = str->len; val->val.ptr = (qse_char_t*)(val + 1); - qse_strncpy (val->val.ptr, str, len); + qse_strncpy (val->val.ptr, str->ptr, str->len); #ifdef DEBUG_VAL qse_dprintf (QSE_T("makestrval => %p\n"), val); @@ -240,25 +333,13 @@ init: return (qse_awk_val_t*)val; } -qse_awk_val_t* qse_awk_rtx_makestrval_nodup ( - qse_awk_rtx_t* rtx, qse_char_t* str, qse_size_t len) +qse_awk_val_t* qse_awk_rtx_makestrval ( + qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t len) { - qse_awk_val_str_t* val; - - val = (qse_awk_val_str_t*) QSE_AWK_ALLOC ( - rtx->awk, QSE_SIZEOF(qse_awk_val_str_t)); - if (val == QSE_NULL) - { - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; - } - - val->type = QSE_AWK_VAL_STR; - val->ref = 0; - val->nstr = 0; - val->val.len = len; - val->val.ptr = str; - return (qse_awk_val_t*)val; + qse_cstr_t cstr; + cstr.ptr = str; + cstr.len = len; + return qse_awk_rtx_makestrvalwithcstr (rtx, &cstr); } qse_awk_val_t* qse_awk_rtx_makestrval2 ( @@ -311,16 +392,15 @@ init: return (qse_awk_val_t*)val; } -qse_awk_val_t* qse_awk_rtx_makenstrval ( - qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t len) +qse_awk_val_t* qse_awk_rtx_makenstrvalwithcstr (qse_awk_rtx_t* rtx, const qse_cstr_t* str) { int x; qse_awk_val_t* v; qse_long_t l; qse_flt_t r; - x = qse_awk_rtx_strtonum (rtx, 1, str, len, &l, &r); - v = qse_awk_rtx_makestrval (rtx, str, len); + x = qse_awk_rtx_strtonum (rtx, 1, str->ptr, str->len, &l, &r); + v = qse_awk_rtx_makestrvalwithcstr (rtx, str); if (v == QSE_NULL) return QSE_NULL; @@ -468,6 +548,70 @@ qse_awk_val_t* qse_awk_rtx_makemapval (qse_awk_rtx_t* rtx) return (qse_awk_val_t*)val; } +qse_awk_val_t* qse_awk_rtx_makemapvalwithdata (qse_awk_rtx_t* rtx, qse_awk_val_map_data_t data[]) +{ + qse_awk_val_t* map, * tmp; + qse_awk_val_map_data_t* p; + + map = qse_awk_rtx_makemapval (rtx); + if (map == QSE_NULL) return QSE_NULL; + + for (p = data; p->key.ptr; p++) + { + switch (p->type) + { + case QSE_AWK_VAL_MAP_DATA_INT: + tmp = qse_awk_rtx_makeintval (rtx, *(qse_long_t*)p->vptr); + break; + + case QSE_AWK_VAL_MAP_DATA_FLT: + tmp = qse_awk_rtx_makefltval (rtx, *(qse_flt_t*)p->vptr); + break; + + case QSE_AWK_VAL_MAP_DATA_STR: + tmp = qse_awk_rtx_makestrvalwithstr (rtx, (qse_char_t*)p->vptr); + break; + + case QSE_AWK_VAL_MAP_DATA_MBS: + tmp = qse_awk_rtx_makestrvalwithmbs (rtx, (qse_mchar_t*)p->vptr); + break; + + case QSE_AWK_VAL_MAP_DATA_WCS: + tmp = qse_awk_rtx_makestrvalwithwcs (rtx, (qse_wchar_t*)p->vptr); + break; + + case QSE_AWK_VAL_MAP_DATA_XSTR: + case QSE_AWK_VAL_MAP_DATA_CSTR: + tmp = qse_awk_rtx_makestrvalwithcstr (rtx, (qse_cstr_t*)p->vptr); + break; + + case QSE_AWK_VAL_MAP_DATA_MXSTR: + case QSE_AWK_VAL_MAP_DATA_MCSTR: + tmp = qse_awk_rtx_makestrvalwithmcstr (rtx, (qse_mcstr_t*)p->vptr); + break; + + case QSE_AWK_VAL_MAP_DATA_WXSTR: + case QSE_AWK_VAL_MAP_DATA_WCSTR: + tmp = qse_awk_rtx_makestrvalwithwcstr (rtx, (qse_wcstr_t*)p->vptr); + break; + + default: + tmp = QSE_NULL; + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); + break; + } + + if (tmp == QSE_NULL || qse_awk_rtx_setmapvalfld (rtx, map, p->key.ptr, p->key.len, tmp) == QSE_NULL) + { + if (tmp) qse_awk_rtx_freeval (rtx, tmp, 1); + qse_awk_rtx_freeval (rtx, map, 1); + return QSE_NULL; + } + } + + return map; +} + qse_awk_val_t* qse_awk_rtx_setmapvalfld ( qse_awk_rtx_t* rtx, qse_awk_val_t* map, const qse_char_t* kptr, qse_size_t klen, qse_awk_val_t* v) @@ -554,9 +698,9 @@ qse_awk_val_t* qse_awk_rtx_makerefval ( { qse_awk_val_ref_t* val; - if (rtx->fcache_count > 0) + if (rtx->rcache_count > 0) { - val = rtx->fcache[--rtx->fcache_count]; + val = rtx->rcache[--rtx->rcache_count]; } else { @@ -675,9 +819,9 @@ void qse_awk_rtx_freeval ( case QSE_AWK_VAL_REF: { - if (cache && rtx->fcache_count < QSE_COUNTOF(rtx->fcache)) + if (cache && rtx->rcache_count < QSE_COUNTOF(rtx->rcache)) { - rtx->fcache[rtx->fcache_count++] = (qse_awk_val_ref_t*)val; + rtx->rcache[rtx->rcache_count++] = (qse_awk_val_ref_t*)val; } else QSE_AWK_FREE (rtx->awk, val); break; diff --git a/qse/lib/cmn/mbwc-str.c b/qse/lib/cmn/mbwc-str.c index 78ab8921..87e1815b 100644 --- a/qse/lib/cmn/mbwc-str.c +++ b/qse/lib/cmn/mbwc-str.c @@ -245,6 +245,40 @@ int qse_mbsntowcsnuptowithcmgr ( return ret; } +static qse_wchar_t* mbsn_to_wcs_dup_with_cmgr ( + const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, + qse_mmgr_t* mmgr, qse_cmgr_t* cmgr, int all) +{ + qse_size_t ml, wl; + qse_wchar_t* wcs; + + ml = *mbslen; + if (mbsn_to_wcsn_with_cmgr ( + mbs, &ml, QSE_NULL, &wl, cmgr, all) <= -1) return QSE_NULL; + + wl++; /* for terminating null */ + wcs = QSE_MMGR_ALLOC (mmgr, wl * QSE_SIZEOF(*wcs)); + if (wcs == QSE_NULL) return QSE_NULL; + + mbsn_to_wcsn_with_cmgr (mbs, mbslen, wcs, &wl, cmgr, all); + wcs[wl] = QSE_WT('\0'); + + if (wcslen) *wcslen = wl; + return wcs; +} + +qse_wchar_t* qse_mbsntowcsdupwithcmgr ( + const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr) +{ + return mbsn_to_wcs_dup_with_cmgr (mbs, mbslen, wcslen, mmgr, cmgr, 0); +} + +qse_wchar_t* qse_mbsntowcsalldupwithcmgr ( + const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr) +{ + return mbsn_to_wcs_dup_with_cmgr (mbs, mbslen, wcslen, mmgr, cmgr, 1); +} + static qse_wchar_t* mbs_to_wcs_dup_with_cmgr ( const qse_mchar_t* mbs, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr, int all) { diff --git a/qse/lib/cmn/mbwc.c b/qse/lib/cmn/mbwc.c index 05f1b3b2..85feba82 100644 --- a/qse/lib/cmn/mbwc.c +++ b/qse/lib/cmn/mbwc.c @@ -183,6 +183,18 @@ qse_wchar_t* qse_mbstowcsalldup ( return qse_mbstowcsalldupwithcmgr (mbs, wcslen, mmgr, dfl_cmgr); } +qse_wchar_t* qse_mbsntowcsdup ( + const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr) +{ + return qse_mbsntowcsdupwithcmgr (mbs, mbslen, wcslen, mmgr, dfl_cmgr); +} + +qse_wchar_t* qse_mbsntowcsalldup ( + const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr) +{ + return qse_mbsntowcsalldupwithcmgr (mbs, mbslen, wcslen, mmgr, dfl_cmgr); +} + qse_wchar_t* qse_mbsatowcsdup ( const qse_mchar_t* mbs[], qse_size_t* wcslen, qse_mmgr_t* mmgr) { diff --git a/qse/mod/awk/uci.c b/qse/mod/awk/uci.c index ca6d4f31..44a79d7e 100644 --- a/qse/mod/awk/uci.c +++ b/qse/mod/awk/uci.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "../../lib/cmn/mem.h" #if defined(HAVE_UCI_H) @@ -9,6 +10,8 @@ # 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; @@ -33,21 +36,21 @@ struct uctx_list_t int capa; int high; } map; + + int errnum; }; static uctx_node_t* new_uctx_node (qse_awk_rtx_t* rtx, uctx_list_t* list) { /* create a new context node and append it to the list tail */ uctx_node_t* node; - qse_mmgr_t* mmgr; node = QSE_NULL; - mmgr = qse_awk_rtx_getmmgr(rtx); if (list->free) node = list->free; else { - node = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(*node)); + node = qse_awk_rtx_callocmem (rtx, QSE_SIZEOF(*node)); if (!node) goto oops; } @@ -65,8 +68,8 @@ static uctx_node_t* new_uctx_node (qse_awk_rtx_t* rtx, uctx_list_t* list) newcapa = list->map.capa + 64; if (newcapa < list->map.capa) goto oops; /* overflow */ - tmp = (uctx_node_t**) QSE_MMGR_REALLOC ( - mmgr, list->map.tab, QSE_SIZEOF(*tmp) * newcapa); + tmp = (uctx_node_t**) qse_awk_rtx_reallocmem ( + rtx, list->map.tab, QSE_SIZEOF(*tmp) * newcapa); if (!tmp) goto oops; QSE_MEMSET (&tmp[list->map.capa], 0, @@ -92,7 +95,7 @@ static uctx_node_t* new_uctx_node (qse_awk_rtx_t* rtx, uctx_list_t* list) return node; oops: - if (node) QSE_MMGR_FREE (mmgr, node); + if (node) qse_awk_rtx_freemem (rtx, node); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); return QSE_NULL; } @@ -106,7 +109,10 @@ static void free_uctx_node (qse_awk_rtx_t* rtx, uctx_list_t* list, uctx_node_t* list->map.tab[node->id] = QSE_NULL; - if (node->ctx) uci_free_context (node->ctx); + if (node->ctx) + { + uci_free_context (node->ctx); + } if (list->map.high == node->id + 1) { @@ -140,7 +146,6 @@ static void free_uctx_node (qse_awk_rtx_t* rtx, uctx_list_t* list, uctx_node_t* QSE_MMGR_FREE (mmgr, curnode); } -qse_printf (QSE_T("freeing map...\n")); QSE_MMGR_FREE (mmgr, list->map.tab); list->map.high = 0; list->map.capa = 0; @@ -148,17 +153,310 @@ qse_printf (QSE_T("freeing map...\n")); } } -static int free_uctx_node_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) +/* ------------------------------------------------------------------------ */ + +static int close_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) { + int x = ERR_WRONG_HANDLE; + if (id >= 0 && id < list->map.high && list->map.tab[id]) { free_uctx_node (rtx, list, list->map.tab[id]); + x = UCI_OK; + } + + 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) +{ + int x = ERR_WRONG_HANDLE; + + 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; +} + +static int unload_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) +{ + int x = ERR_WRONG_HANDLE; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + x = uci_unload (list->map.tab[id]->ctx, QSE_NULL); return 0; } - return -1; + return -x; } +static int save_byid (qse_awk_rtx_t* rtx, uctx_list_t* list, qse_long_t id) +{ + int x = ERR_WRONG_HANDLE; + + if (id >= 0 && id < list->map.high && list->map.tab[id]) + { + x = uci_save (list->map.tab[id]->ctx, QSE_NULL); + } + + 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) +{ + int x = ERR_WRONG_HANDLE; + + 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, mtuple, 1); + if (x == UCI_OK) + { + if (ptr.flags & UCI_LOOKUP_COMPLETE) + { + struct uci_element* e; + + 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)); + + 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[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; + + } + else x = UCI_ERR_NOTFOUND; + } + + QSE_MMGR_FREE (mmgr, mtuple); + } + else x = UCI_ERR_MEM; + } + + return -x; +} + +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) +{ + int x = ERR_WRONG_HANDLE; + + 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, mtuple, 1); + if (x == UCI_OK) + { + if (ptr.flags & UCI_LOOKUP_COMPLETE) + { + struct uci_element* e; + + e = ptr.last; + if (e->type == UCI_TYPE_OPTION) + { + struct uci_option* uo = ptr.o; + + if (uo->type == UCI_TYPE_STRING) + { + 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 + { + 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"); + + 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++; + } + + 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; + } + + QSE_MMGR_FREE (mmgr, mtuple); + } + else x = UCI_ERR_MEM; + } + + return -x; +} +/* ------------------------------------------------------------------------ */ + static QSE_INLINE uctx_list_t* rtx_to_list (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { qse_rbt_pair_t* pair; @@ -167,23 +465,42 @@ static QSE_INLINE uctx_list_t* rtx_to_list (qse_awk_rtx_t* rtx, const qse_awk_fn return (uctx_list_t*)QSE_RBT_VPTR(pair); } +static int fnc_uci_errno (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) +{ + uctx_list_t* list; + qse_awk_val_t* retv; + + list = rtx_to_list (rtx, fi); + + retv = qse_awk_rtx_makeintval (rtx, list->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; uctx_node_t* node; - qse_long_t ret = -1; + qse_long_t ret; qse_awk_val_t* retv; list = rtx_to_list (rtx, fi); node = new_uctx_node (rtx, list); - if (node) ret = node->id; + ret = node? node->id: -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_close (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) @@ -191,16 +508,21 @@ static int fnc_uci_close (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 n; + int ret; list = rtx_to_list (rtx, fi); - n = qse_awk_rtx_valtolong ( - rtx, qse_awk_rtx_getarg (rtx, 0), &id); - if (n <= -1) return -1; + ret = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &id); + if (ret <= -1) ret = -UCI_ERR_INVAL; + else ret = close_byid (rtx, list, id); - retv = qse_awk_rtx_makeintval ( - rtx, free_uctx_node_byid (rtx, list, id)); + 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); @@ -209,12 +531,286 @@ static int fnc_uci_close (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) static int fnc_uci_load (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { - return 0 ; + 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_awk_val_t* v; + + v = qse_awk_rtx_getarg(rtx, 1); + if (v->type == QSE_AWK_VAL_STR) + { + 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); + } + } + } + + 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_unload (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 ret = unload_byid (rtx, list, id); + + 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_save (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { - return 0 ; + 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 ret = save_byid (rtx, list, id); + + 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_setconfdir (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_awk_val_t* v; + + v = qse_awk_rtx_getarg(rtx, 1); + if (v->type == QSE_AWK_VAL_STR) + { + 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); + } + } + } + + 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_setsavedir (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_awk_val_t* v; + + v = qse_awk_rtx_getarg(rtx, 1); + if (v->type == QSE_AWK_VAL_STR) + { + ret = setsavedir_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 = setsavedir_byid (rtx, list, id, out.u.cpldup.ptr); + qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr); + } + } + + } + + 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_getoption (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_awk_val_t* v; + + v = qse_awk_rtx_getarg(rtx, 1); + if (v->type == QSE_AWK_VAL_STR) + { + 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); + } + } + } + + if (ret <= -1) + { + list->errnum = ret; + } + else + { + qse_awk_rtx_setretval (rtx, retv); + } + return 0; +} + +static int fnc_uci_getsection (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 >= 0) + { + qse_awk_val_t* v; + + v = qse_awk_rtx_getarg(rtx, 1); + if (v->type == QSE_AWK_VAL_STR) + { + 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); + } + } + } + + if (ret <= -1) + { + list->errnum = ret; + } + 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) @@ -229,6 +825,7 @@ static int fnc_uci_revert (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) return 0; } +/* ------------------------------------------------------------------------ */ typedef struct fnctab_t fnctab_t; struct fnctab_t @@ -239,14 +836,22 @@ struct fnctab_t static fnctab_t fnctab[] = { - { QSE_T("close"), { { 1, 1 }, fnc_uci_close } }, - { QSE_T("commit"), { { 0, 0 }, fnc_uci_commit } }, - { QSE_T("load"), { { 1, 1 }, fnc_uci_load } }, - { QSE_T("open"), { { 0, 0 }, fnc_uci_open } }, - { QSE_T("revert"), { { 0, 0 }, fnc_uci_revert } }, - { QSE_T("save"), { { 0, 0 }, fnc_uci_save } } + { QSE_T("close"), { { 1, 1 }, fnc_uci_close } }, + { QSE_T("commit"), { { 1, 1 }, fnc_uci_commit } }, + { QSE_T("errno"), { { 0, 0 }, fnc_uci_errno } }, + { 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("setconfdir"), { { 2, 2 }, fnc_uci_setconfdir } }, + { QSE_T("setsavedir"), { { 2, 2 }, fnc_uci_setsavedir } }, + { QSE_T("unload"), { { 1, 1 }, fnc_uci_unload } } }; +/* ------------------------------------------------------------------------ */ + static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym) { qse_cstr_t ea; @@ -282,7 +887,6 @@ static int init (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx) qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); return -1; } -qse_printf (QSE_T("initialized ... module... %p %p\n"), mod, mod->ctx); return 0; } @@ -292,7 +896,6 @@ static void fini (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx) qse_rbt_t* rbt; qse_rbt_pair_t* pair; -qse_printf (QSE_T("fini ... module...%p\n"), mod); rbt = (qse_rbt_t*)mod->ctx; /* garbage clean-up */ @@ -321,7 +924,6 @@ static void unload (qse_awk_mod_t* mod, qse_awk_t* awk) rbt = (qse_rbt_t*)mod->ctx; -qse_printf (QSE_T("unloaded ... module...%p\n"), mod); QSE_ASSERT (QSE_RBT_SIZE(rbt) == 0); qse_rbt_close (rbt); } @@ -344,7 +946,6 @@ int load (qse_awk_mod_t* mod, qse_awk_t* awk) } qse_rbt_setmancbs (rbt, qse_getrbtmancbs(QSE_RBT_MANCBS_INLINE_COPIERS)); -qse_printf (QSE_T("loaded ... module...%p\n"), mod); mod->ctx = rbt; return 0; }