From 9387994622f8d3ad383dc51d04f0ad68bd59d80c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 18 Dec 2019 15:28:31 +0000 Subject: [PATCH] removed unneeded code from tre --- hawk/lib/err.c | 22 ++++++------ hawk/lib/fnc.c | 8 ++--- hawk/lib/hawk-cmn.h | 26 +++++++------- hawk/lib/hawk-prv.h | 9 ++++- hawk/lib/hawk-tio.h | 6 ++-- hawk/lib/hawk-tre.h | 37 -------------------- hawk/lib/hawk.h | 7 ++-- hawk/lib/misc-prv.h | 10 +++--- hawk/lib/misc.c | 84 ++++++++++++++------------------------------- hawk/lib/pma.c | 2 +- hawk/lib/rio.c | 19 +++++----- hawk/lib/run.c | 9 +++-- hawk/lib/tre-prv.h | 31 ++++++++--------- hawk/lib/tre.c | 36 ++----------------- hawk/lib/tree-prv.h | 2 +- hawk/lib/val-prv.h | 2 +- hawk/lib/val.c | 68 ++++++++++++------------------------ 17 files changed, 129 insertions(+), 249 deletions(-) diff --git a/hawk/lib/err.c b/hawk/lib/err.c index 358bfb72..c0b4b5b2 100644 --- a/hawk/lib/err.c +++ b/hawk/lib/err.c @@ -167,18 +167,18 @@ const hawk_ooch_t* hawk_dflerrstr (hawk_t* awk, hawk_errnum_t errnum) HAWK_T("invalid character in CONVFMT"), HAWK_T("invalid character in OFMT"), - HAWK_T("failed to build regular expression"), HAWK_T("failed to match regular expression"), - HAWK_T("recursion too deep in regular expression"), - HAWK_T("right parenthesis expected in regular expression"), - HAWK_T("right bracket expected in regular expression"), - HAWK_T("right brace expected in regular expression"), - HAWK_T("colon expected in regular expression"), - HAWK_T("invalid character range in regular expression"), - HAWK_T("invalid character class in regular expression"), - HAWK_T("invalid occurrence bound in regular expression"), - HAWK_T("special character at wrong position"), - HAWK_T("premature end of regular expression") + HAWK_T("invalid regular expression in regular expression"), + HAWK_T("unknown collating element in regular expression"), + HAWK_T("unknown character clas name in regular expression"), + HAWK_T("trailing backslash in regular expression"), + HAWK_T("invalid backreference in regular expression"), + HAWK_T("imbalanced bracket in regular expression"), + HAWK_T("imbalanced parenthesis in regular expression"), + HAWK_T("imbalanced brace in regular expression"), + HAWK_T("invalid content inside braces in regular expression"), + HAWK_T("invalid use of range operator in regular expression"), + HAWK_T("invalid use of repetition operator in regular expression") }; return (errnum >= 0 && errnum < HAWK_COUNTOF(errstr))? errstr[errnum]: HAWK_T("unknown error"); diff --git a/hawk/lib/fnc.c b/hawk/lib/fnc.c index 4b72ddf0..3765a650 100644 --- a/hawk/lib/fnc.c +++ b/hawk/lib/fnc.c @@ -808,8 +808,8 @@ int hawk_fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_ooch_t* fs_free = HAWK_NULL; const hawk_ooch_t* p; hawk_oow_t str_left, org_len; - void* fs_rex = HAWK_NULL; - void* fs_rex_free = HAWK_NULL; + hawk_tre_t* fs_rex = HAWK_NULL; + hawk_tre_t* fs_rex_free = HAWK_NULL; hawk_oocs_t tok; hawk_int_t nflds; @@ -1082,8 +1082,8 @@ static int __substitute (hawk_rtx_t* rtx, hawk_int_t max_count) hawk_ooch_t* s0_free = HAWK_NULL; hawk_ooch_t* s2_free = HAWK_NULL; - void* rex = HAWK_NULL; - void* rex_free = HAWK_NULL; + hawk_tre_t* rex = HAWK_NULL; + hawk_tre_t* rex_free = HAWK_NULL; hawk_ooecs_t new; int new_inited = 0; diff --git a/hawk/lib/hawk-cmn.h b/hawk/lib/hawk-cmn.h index 363e4b9e..f93879b3 100644 --- a/hawk/lib/hawk-cmn.h +++ b/hawk/lib/hawk-cmn.h @@ -898,19 +898,19 @@ enum hawk_errnum_t HAWK_EOFMTCHR, /**< invalid character in OFMT */ /* regular expression error */ - HAWK_EREXBL, /**< failed to build regular expression */ - HAWK_EREXMA, /**< failed to match regular expression */ - HAWK_EREXRECUR, /**< recursion too deep */ - HAWK_EREXRPAREN, /**< a right parenthesis is expected */ - HAWK_EREXRBRACK, /**< a right bracket is expected */ - HAWK_EREXRBRACE, /**< a right brace is expected */ - HAWK_EREXCOLON, /**< a colon is expected */ - HAWK_EREXCRANGE, /**< invalid character range */ - HAWK_EREXCCLASS, /**< invalid character class */ - HAWK_EREXBOUND, /**< invalid occurrence bound */ - HAWK_EREXSPCAWP, /**< special character at wrong position */ - HAWK_EREXPREEND, /**< premature end of regular expression */ - + HAWK_EREXNOMAT, /**< failed to match regular expression */ + HAWK_EREXBADPAT, /**< invalid regular expression */ + HAWK_EREXCOLLATE, /**< unknown collating element */ + HAWK_EREXCTYPE, /**< unknown character clas name */ + HAWK_EREXESCAPE, /**< trailing backslash */ + HAWK_EREXSUBREG, /**< invalid backreference */ + HAWK_EREXBRACK, /**< imbalanced bracket */ + HAWK_EREXPAREN, /**< imbalanced parenthesis */ + HAWK_EREXBRACE, /**< imbalanced brace */ + HAWK_EREXBADBR, /**< invalid content inside braces */ + HAWK_EREXRANGE, /**< invalid use of range operator */ + HAWK_EREXBADRPT, /**< invalid use of repetition operator */ + /* the number of error numbers, internal use only */ HAWK_NUMERRNUM }; diff --git a/hawk/lib/hawk-prv.h b/hawk/lib/hawk-prv.h index fd58ef0b..20d10406 100644 --- a/hawk/lib/hawk-prv.h +++ b/hawk/lib/hawk-prv.h @@ -499,7 +499,14 @@ int hawk_init (hawk_t* awk, hawk_mmgr_t* mmgr, hawk_cmgr_t* cmgr, const hawk_prm void hawk_fini (hawk_t* awk); -/* TODO: should i make this public? */ +/* TODO: should i make these public? */ + +#if defined(HAWK_HAVE_INLINE) +static HAWK_INLINE hawk_errnum_t hawk_gem_geterrnum (hawk_gem_t* gem) { return gem->errnum; } +#else +#define hawk_gem_geterrnum(gem) (((hawk_gem_t*)(gem))->errnum) +#endif + void hawk_gem_seterrnum (hawk_gem_t* gem, const hawk_loc_t* errloc, hawk_errnum_t errnum); #if defined(__cplusplus) diff --git a/hawk/lib/hawk-tio.h b/hawk/lib/hawk-tio.h index 50c8ac49..40f2ef0e 100644 --- a/hawk/lib/hawk-tio.h +++ b/hawk/lib/hawk-tio.h @@ -161,10 +161,10 @@ HAWK_EXPORT void hawk_tio_setcmgr ( * @return 0 on success, -1 on failure */ HAWK_EXPORT int hawk_tio_attachin ( - hawk_tio_t* tio, + hawk_tio_t* tio, hawk_tio_io_impl_t input, - hawk_bch_t* bufptr, - hawk_oow_t bufcapa + hawk_bch_t* bufptr, + hawk_oow_t bufcapa ); /** diff --git a/hawk/lib/hawk-tre.h b/hawk/lib/hawk-tre.h index 0ae24269..250cc7ca 100644 --- a/hawk/lib/hawk-tre.h +++ b/hawk/lib/hawk-tre.h @@ -29,42 +29,14 @@ #include -enum hawk_tre_errnum_t -{ - HAWK_TRE_ENOERR, - HAWK_TRE_EOTHER, - HAWK_TRE_ENOIMPL, - HAWK_TRE_ESYSERR, - HAWK_TRE_EINTERN, - - HAWK_TRE_ENOMEM, /* Out of memory */ - HAWK_TRE_ENOMATCH, /* No match */ - HAWK_TRE_EBADPAT, /* Invalid regular expression */ - HAWK_TRE_ECOLLATE, /* Unknown collating element */ - HAWK_TRE_ECTYPE, /* Unknown character class name */ - HAWK_TRE_EESCAPE, /* Traling backslash */ - HAWK_TRE_ESUBREG, /* Invalid backreference */ - HAWK_TRE_EBRACK, /* "[]" imbalance */ - HAWK_TRE_EPAREN, /* "\(\)" or "()" imbalance */ - HAWK_TRE_EBRACE, /* "\{\}" or "{}" imbalance */ - HAWK_TRE_EBADBR, /* Invalid content of {} */ - HAWK_TRE_ERANGE, /* Invalid use of range operator */ - HAWK_TRE_EBADRPT /* Invalid use of repetition operators */ -}; -typedef enum hawk_tre_errnum_t hawk_tre_errnum_t; - typedef struct hawk_tre_t hawk_tre_t; struct hawk_tre_t { hawk_gem_t* gem; - hawk_tre_errnum_t errnum; - hawk_oow_t re_nsub; /* Number of parenthesized subexpressions. */ void* value; /* For internal use only. */ }; -#define HAWK_TRE_ERRNUM(tre) ((const hawk_tre_errnum_t)((tre)->errnum)) - typedef int hawk_tre_off_t; typedef struct hawk_tre_match_t hawk_tre_match_t; @@ -132,15 +104,6 @@ static HAWK_INLINE void* hawk_tre_getxtn (hawk_tre_t* tre) { return (void*)(tre #define hawk_tre_getxtn(awk) ((void*)((hawk_tre_t*)(tre) + 1)) #endif - -HAWK_EXPORT hawk_tre_errnum_t hawk_tre_geterrnum ( - hawk_tre_t* tre -); - -HAWK_EXPORT const hawk_ooch_t* hawk_tre_geterrmsg ( - hawk_tre_t* tre -); - HAWK_EXPORT int hawk_tre_compx ( hawk_tre_t* tre, const hawk_ooch_t* regex, diff --git a/hawk/lib/hawk.h b/hawk/lib/hawk.h index 29f28dbb..ca0127e5 100644 --- a/hawk/lib/hawk.h +++ b/hawk/lib/hawk.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -224,7 +225,7 @@ struct hawk_val_rex_t { HAWK_VAL_HDR; hawk_oocs_t str; - void* code[2]; + hawk_tre_t* code[2]; }; typedef struct hawk_val_rex_t hawk_val_rex_t; @@ -1245,7 +1246,7 @@ enum hawk_trait_t HAWK_STRIPSTRSPC | HAWK_STRICTNAMING, HAWK_MODERN = - HAWK_CLASSIC | HAWK_FLEXMAP | + HAWK_CLASSIC | HAWK_FLEXMAP | HAWK_REXBOUND | HAWK_RWPIPE | HAWK_TOLERANT | HAWK_NEXTOFILE /*| HAWK_NCMPONSTR*/ }; typedef enum hawk_trait_t hawk_trait_t; @@ -2684,7 +2685,7 @@ hawk_val_t* hawk_rtx_makembsvalwithbcs ( HAWK_EXPORT hawk_val_t* hawk_rtx_makerexval ( hawk_rtx_t* rtx, const hawk_oocs_t* str, - void* code[2] + hawk_tre_t* code[2] ); /** diff --git a/hawk/lib/misc-prv.h b/hawk/lib/misc-prv.h index 11531ca5..af02364f 100644 --- a/hawk/lib/misc-prv.h +++ b/hawk/lib/misc-prv.h @@ -27,6 +27,8 @@ #ifndef _HAWK_MISC_PRV_H_ #define _HAWK_MISC_PRV_H_ +#include + #if defined(__cplusplus) extern "C" { #endif @@ -74,17 +76,17 @@ int hawk_buildrex ( const hawk_ooch_t* ptn, hawk_oow_t len, hawk_errnum_t* errnum, - void** code, - void** icode + hawk_tre_t** code, + hawk_tre_t** icode ); int hawk_matchrex ( - hawk_t* awk, void* code, int icase, + hawk_t* awk, hawk_tre_t* code, int icase, const hawk_oocs_t* str, const hawk_oocs_t* substr, hawk_oocs_t* match, hawk_oocs_t submat[9], hawk_errnum_t* errnum ); -void hawk_freerex (hawk_t* awk, void* code, void* icode); +void hawk_freerex (hawk_t* awk, hawk_tre_t* code, hawk_tre_t* icode); int hawk_rtx_matchrex ( hawk_rtx_t* rtx, hawk_val_t* val, diff --git a/hawk/lib/misc.c b/hawk/lib/misc.c index af4878cd..696591e7 100644 --- a/hawk/lib/misc.c +++ b/hawk/lib/misc.c @@ -25,7 +25,6 @@ */ #include "hawk-prv.h" -#include hawk_ooch_t* hawk_rtx_strtok ( hawk_rtx_t* rtx, const hawk_ooch_t* s, @@ -338,15 +337,13 @@ exit_loop: { /* if the match reached the last character in the input string, * it returns HAWK_NULL to terminate tokenization. */ - return (match.ptr+match.len >= substr+sublen)? - HAWK_NULL: ((hawk_ooch_t*)match.ptr+match.len); + return (match.ptr+match.len >= substr+sublen)? HAWK_NULL: ((hawk_ooch_t*)match.ptr+match.len); } else { /* if the match went beyond the the last character in the input * string, it returns HAWK_NULL to terminate tokenization. */ - return (match.ptr+match.len > substr+sublen)? - HAWK_NULL: ((hawk_ooch_t*)match.ptr+match.len); + return (match.ptr+match.len > substr+sublen)? HAWK_NULL: ((hawk_ooch_t*)match.ptr+match.len); } } @@ -439,9 +436,7 @@ hawk_ooch_t* hawk_rtx_strxnfld ( return HAWK_NULL; } -int hawk_buildrex ( - hawk_t* awk, const hawk_ooch_t* ptn, hawk_oow_t len, - hawk_errnum_t* errnum, void** code, void** icode) +int hawk_buildrex (hawk_t* awk, const hawk_ooch_t* ptn, hawk_oow_t len, hawk_errnum_t* errnum, hawk_tre_t** code, hawk_tre_t** icode) { hawk_tre_t* tre = HAWK_NULL; hawk_tre_t* itre = HAWK_NULL; @@ -452,7 +447,7 @@ int hawk_buildrex ( tre = hawk_tre_open(hawk_getgem(awk), 0); if (tre == HAWK_NULL) { - *errnum = HAWK_ENOMEM; + *errnum = hawk_geterrnum(awk); return -1; } @@ -460,14 +455,7 @@ int hawk_buildrex ( if (hawk_tre_compx(tre, ptn, len, HAWK_NULL, opt) <= -1) { -#if 0 /* TODO */ - - if (HAWK_TRE_ERRNUM(tre) == HAWK_TRE_ENOMEM) *errnum = HAWK_ENOMEM; - else - SETERR1 (awk, HAWK_EREXBL, str->ptr, str->len, loc); -#endif - *errnum = (HAWK_TRE_ERRNUM(tre) == HAWK_TRE_ENOMEM)? - HAWK_ENOMEM: HAWK_EREXBL; + *errnum = hawk_gem_geterrnum(tre->gem); hawk_tre_close (tre); return -1; } @@ -478,22 +466,15 @@ int hawk_buildrex ( itre = hawk_tre_open(hawk_getgem(awk), 0); if (itre == HAWK_NULL) { + *errnum = hawk_geterrnum(awk); if (tre) hawk_tre_close (tre); - *errnum = HAWK_ENOMEM; return -1; } /* ignorecase is a compile option for TRE */ if (hawk_tre_compx(itre, ptn, len, HAWK_NULL, opt | HAWK_TRE_IGNORECASE) <= -1) { -#if 0 /* TODO */ - - if (HAWK_TRE_ERRNUM(tre) == HAWK_TRE_ENOMEM) *errnum = HAWK_ENOMEM; - else - SETERR1 (awk, HAWK_EREXBL, str->ptr, str->len, loc); -#endif - *errnum = (HAWK_TRE_ERRNUM(tre) == HAWK_TRE_ENOMEM)? - HAWK_ENOMEM: HAWK_EREXBL; + *errnum = hawk_gem_geterrnum(itre->gem); hawk_tre_close (itre); if (tre) hawk_tre_close (tre); return -1; @@ -506,7 +487,7 @@ int hawk_buildrex ( } static int matchtre ( - hawk_t* awk, hawk_tre_t* tre, int opt, + hawk_tre_t* tre, int opt, const hawk_oocs_t* str, hawk_oocs_t* mat, hawk_oocs_t submat[9], hawk_errnum_t* errnum) { @@ -518,19 +499,12 @@ static int matchtre ( n = hawk_tre_execx(tre, str->ptr, str->len, match, HAWK_COUNTOF(match), opt); if (n <= -1) { - if (HAWK_TRE_ERRNUM(tre) == HAWK_TRE_ENOMATCH) return 0; - -#if 0 /* TODO: */ - *errnum = (HAWK_TRE_ERRNUM(tre) == HAWK_TRE_ENOMEM)? - HAWK_ENOMEM: HAWK_EREXMA; - SETERR0 (sed, errnum, loc); -#endif - *errnum = (HAWK_TRE_ERRNUM(tre) == HAWK_TRE_ENOMEM)? - HAWK_ENOMEM: HAWK_EREXMA; + if (hawk_gem_geterrnum(tre->gem) == HAWK_EREXNOMAT) return 0; + *errnum = hawk_gem_geterrnum(tre->gem); return -1; } - HAWK_ASSERT (awk, match[0].rm_so != -1); + /* HAWK_ASSERT (awk, match[0].rm_so != -1); <--- add back with gem*/ if (mat) { mat->ptr = &str->ptr[match[0].rm_so]; @@ -556,7 +530,7 @@ static int matchtre ( } int hawk_matchrex ( - hawk_t* awk, void* code, int icase, + hawk_t* awk, hawk_tre_t* code, int ignorecase, const hawk_oocs_t* str, const hawk_oocs_t* substr, hawk_oocs_t* match, hawk_oocs_t submat[9], hawk_errnum_t* errnum) { @@ -564,39 +538,32 @@ int hawk_matchrex ( int opt = HAWK_TRE_BACKTRACKING; /* TODO: option... HAWK_TRE_BACKTRACKING ??? */ x = matchtre ( - awk, code, - ((str->ptr == substr->ptr)? opt: (opt | HAWK_TRE_NOTBOL)), + code, ((str->ptr == substr->ptr)? opt: (opt | HAWK_TRE_NOTBOL)), substr, match, submat, errnum ); + return x; } -void hawk_freerex (hawk_t* awk, void* code, void* icode) +void hawk_freerex (hawk_t* awk, hawk_tre_t* code, hawk_tre_t* icode) { - if (code) - { - hawk_tre_close (code); - } - - if (icode && icode != code) - { - hawk_tre_close (icode); - } + if (icode && icode != code) hawk_tre_close (icode); + if (code) hawk_tre_close (code); } int hawk_rtx_matchrex ( hawk_rtx_t* rtx, hawk_val_t* val, const hawk_oocs_t* str, const hawk_oocs_t* substr, hawk_oocs_t* match, hawk_oocs_t submat[9]) { - void* code; - int icase, x; + hawk_tre_t* code; + int ignorecase, x; hawk_errnum_t awkerr; - icase = rtx->gbl.ignorecase; + ignorecase = rtx->gbl.ignorecase; if (HAWK_RTX_GETVALTYPE (rtx, val) == HAWK_VAL_REX) { - code = ((hawk_val_rex_t*)val)->code[icase]; + code = ((hawk_val_rex_t*)val)->code[ignorecase]; } else { @@ -606,8 +573,8 @@ int hawk_rtx_matchrex ( tmp.ptr = hawk_rtx_getvaloocstr (rtx, val, &tmp.len); if (tmp.ptr == HAWK_NULL) return -1; - x = icase? hawk_buildrex(rtx->awk, tmp.ptr, tmp.len, &awkerr, HAWK_NULL, &code): - hawk_buildrex(rtx->awk, tmp.ptr, tmp.len, &awkerr, &code, HAWK_NULL); + x = ignorecase? hawk_buildrex(rtx->awk, tmp.ptr, tmp.len, &awkerr, HAWK_NULL, &code): + hawk_buildrex(rtx->awk, tmp.ptr, tmp.len, &awkerr, &code, HAWK_NULL); hawk_rtx_freevaloocstr (rtx, val, tmp.ptr); if (x <= -1) { @@ -617,8 +584,7 @@ int hawk_rtx_matchrex ( } x = matchtre ( - rtx->awk, code, - ((str->ptr == substr->ptr)? HAWK_TRE_BACKTRACKING: (HAWK_TRE_BACKTRACKING | HAWK_TRE_NOTBOL)), + code, ((str->ptr == substr->ptr)? HAWK_TRE_BACKTRACKING: (HAWK_TRE_BACKTRACKING | HAWK_TRE_NOTBOL)), substr, match, submat, &awkerr ); if (x <= -1) hawk_rtx_seterrnum (rtx, awkerr, HAWK_NULL); @@ -629,7 +595,7 @@ int hawk_rtx_matchrex ( } else { - if (icase) + if (ignorecase) hawk_freerex (rtx->awk, HAWK_NULL, code); else hawk_freerex (rtx->awk, code, HAWK_NULL); diff --git a/hawk/lib/pma.c b/hawk/lib/pma.c index 64105aee..4e7e2692 100644 --- a/hawk/lib/pma.c +++ b/hawk/lib/pma.c @@ -91,7 +91,7 @@ void hawk_pma_fini (hawk_pma_t* pma) void hawk_pma_clear (hawk_pma_t* pma) { - hawk_t* gem = pma->gem; + hawk_gem_t* gem = pma->gem; hawk_pma_blk_t* tmp, * l = pma->blocks; while (l != HAWK_NULL) diff --git a/hawk/lib/rio.c b/hawk/lib/rio.c index f1feed29..afe0df9c 100644 --- a/hawk/lib/rio.c +++ b/hawk/lib/rio.c @@ -219,22 +219,22 @@ static HAWK_INLINE int resolve_rs (hawk_rtx_t* rtx, hawk_val_t* rs, hawk_oocs_t* return ret; } -static HAWK_INLINE int match_long_rs (hawk_rtx_t* run, hawk_ooecs_t* buf, hawk_rio_arg_t* p) +static HAWK_INLINE int match_long_rs (hawk_rtx_t* rtx, hawk_ooecs_t* buf, hawk_rio_arg_t* p) { hawk_oocs_t match; hawk_errnum_t errnum; int ret; - HAWK_ASSERT (hawk_rtx_gethawk(rtx), run->gbl.rs[0] != HAWK_NULL); - HAWK_ASSERT (hawk_rtx_gethawk(rtx), run->gbl.rs[1] != HAWK_NULL); + HAWK_ASSERT (hawk_rtx_gethawk(rtx), rtx->gbl.rs[0] != HAWK_NULL); + HAWK_ASSERT (hawk_rtx_gethawk(rtx), rtx->gbl.rs[1] != HAWK_NULL); - ret = hawk_matchrex ( - run->awk, run->gbl.rs[run->gbl.ignorecase], - run->gbl.ignorecase, HAWK_OOECS_OOCS(buf), HAWK_OOECS_OOCS(buf), + ret = hawk_matchrex( + rtx->awk, rtx->gbl.rs[rtx->gbl.ignorecase], + rtx->gbl.ignorecase, HAWK_OOECS_OOCS(buf), HAWK_OOECS_OOCS(buf), &match, HAWK_NULL, &errnum); if (ret <= -1) { - hawk_rtx_seterrnum (run, errnum, HAWK_NULL); + hawk_rtx_seterrnum (rtx, errnum, HAWK_NULL); } else if (ret >= 1) { @@ -246,10 +246,7 @@ static HAWK_INLINE int match_long_rs (hawk_rtx_t* run, hawk_ooecs_t* buf, hawk_r * as the previous call to this function. * A match in this case must end at the end of * the current record buffer */ - HAWK_ASSERT (hawk_rtx_gethawk(rtx), - HAWK_OOECS_PTR(buf) + HAWK_OOECS_LEN(buf) == - match.ptr + match.len - ); + HAWK_ASSERT (hawk_rtx_gethawk(rtx), HAWK_OOECS_PTR(buf) + HAWK_OOECS_LEN(buf) == match.ptr + match.len); /* drop the RS part. no extra character after RS to drop * because we're at EOF and the EOF condition didn't diff --git a/hawk/lib/run.c b/hawk/lib/run.c index 9920aee8..b0b2ba0d 100644 --- a/hawk/lib/run.c +++ b/hawk/lib/run.c @@ -429,14 +429,13 @@ static int set_global (hawk_rtx_t* rtx, int idx, hawk_nde_var_t* var, hawk_val_t /* it's a regular expression if FS contains multiple characters. * however, it's not a regular expression if it's 5 character * string beginning with a question mark. */ - void* rex, * irex; + hawk_tre_t* rex, * irex; hawk_errnum_t errnum; - if (hawk_buildrex (hawk_rtx_gethawk(rtx), fs_ptr, fs_len, &errnum, &rex, &irex) <= -1) + if (hawk_buildrex(hawk_rtx_gethawk(rtx), fs_ptr, fs_len, &errnum, &rex, &irex) <= -1) { SETERR_COD (rtx, errnum); - if (vtype != HAWK_VAL_STR) - hawk_rtx_freemem (rtx, fs_ptr); + if (vtype != HAWK_VAL_STR) hawk_rtx_freemem (rtx, fs_ptr); return -1; } @@ -583,7 +582,7 @@ static int set_global (hawk_rtx_t* rtx, int idx, hawk_nde_var_t* var, hawk_val_t if (rss.len > 1) { - void* rex, * irex; + hawk_tre_t* rex, * irex; hawk_errnum_t errnum; /* compile the regular expression */ diff --git a/hawk/lib/tre-prv.h b/hawk/lib/tre-prv.h index b551307e..612db7d6 100644 --- a/hawk/lib/tre-prv.h +++ b/hawk/lib/tre-prv.h @@ -180,23 +180,22 @@ typedef hawk_ooci_t tre_cint_t; #define size_t hawk_oow_t #define regex_t hawk_tre_t #define regmatch_t hawk_tre_match_t -#define reg_errcode_t hawk_tre_errnum_t +#define reg_errcode_t hawk_errnum_t - -#define REG_OK HAWK_TRE_ENOERR -#define REG_ESPACE HAWK_TRE_ENOMEM -#define REG_NOMATCH HAWK_TRE_ENOMATCH -#define REG_BADPAT HAWK_TRE_EBADPAT -#define REG_ECOLLATE HAWK_TRE_ECOLLATE -#define REG_ECTYPE HAWK_TRE_ECTYPE -#define REG_EESCAPE HAWK_TRE_EESCAPE -#define REG_ESUBREG HAWK_TRE_ESUBREG -#define REG_EBRACK HAWK_TRE_EBRACK -#define REG_EPAREN HAWK_TRE_EPAREN -#define REG_EBRACE HAWK_TRE_EBRACE -#define REG_BADBR HAWK_TRE_EBADBR -#define REG_ERANGE HAWK_TRE_ERANGE -#define REG_BADRPT HAWK_TRE_EBADRPT +#define REG_OK HAWK_ENOERR +#define REG_ESPACE HAWK_ENOMEM +#define REG_NOMATCH HAWK_EREXNOMAT +#define REG_BADPAT HAWK_EREXBADPAT +#define REG_ECOLLATE HAWK_EREXCOLLATE +#define REG_ECTYPE HAWK_EREXCTYPE +#define REG_EESCAPE HAWK_EREXESCAPE +#define REG_ESUBREG HAWK_EREXSUBREG +#define REG_EBRACK HAWK_EREXBRACK +#define REG_EPAREN HAWK_EREXPAREN +#define REG_EBRACE HAWK_EREXBRACE +#define REG_BADBR HAWK_EREXBADBR +#define REG_ERANGE HAWK_EREXRANGE +#define REG_BADRPT HAWK_EREXBADRPT /* The maximum number of iterations in a bound expression. */ #undef RE_DUP_MAX diff --git a/hawk/lib/tre.c b/hawk/lib/tre.c index 0afb39ef..1f8d8066 100644 --- a/hawk/lib/tre.c +++ b/hawk/lib/tre.c @@ -80,7 +80,7 @@ int hawk_tre_compx (hawk_tre_t* tre, const hawk_ooch_t* regex, hawk_oow_t n, uns if (ret > 0) { tre->TRE_REGEX_T_FIELD = HAWK_NULL; /* just to make sure */ - tre->errnum = ret; + hawk_gem_seterrnum (tre->gem, HAWK_NULL, ret); return -1; } @@ -220,7 +220,7 @@ int hawk_tre_execx ( if (tre->TRE_REGEX_T_FIELD == HAWK_NULL) { /* regular expression is bad as none is compiled yet */ - tre->errnum = HAWK_TRE_EBADPAT; + hawk_gem_seterrnum (tre->gem, HAWK_NULL, HAWK_EREXBADPAT); return -1; } #if defined(HAWK_OOCH_IS_UCH) @@ -230,7 +230,7 @@ int hawk_tre_execx ( #endif if (ret > 0) { - tre->errnum = ret; + hawk_gem_seterrnum (tre->gem, HAWK_NULL, ret); return -1; } @@ -243,33 +243,3 @@ int hawk_tre_exec ( { return hawk_tre_execx (tre, str, (hawk_oow_t)-1, pmatch, nmatch, eflags); } - -hawk_tre_errnum_t hawk_tre_geterrnum (hawk_tre_t* tre) -{ - return tre->errnum; -} - -const hawk_ooch_t* hawk_tre_geterrmsg (hawk_tre_t* tre) -{ - static const hawk_ooch_t* errstr[] = - { - HAWK_T("no error"), - HAWK_T("no sufficient memory available"), - HAWK_T("no match"), - HAWK_T("invalid regular expression"), - HAWK_T("unknown collating element"), - HAWK_T("unknown character class name"), - HAWK_T("trailing backslash"), - HAWK_T("invalid backreference"), - HAWK_T("bracket imbalance"), - HAWK_T("parenthesis imbalance"), - HAWK_T("brace imbalance"), - HAWK_T("invalid bracket content"), - HAWK_T("invalid use of range operator"), - HAWK_T("invalid use of repetition operators") - }; - - return (tre->errnum >= 0 && tre->errnum < HAWK_COUNTOF(errstr))? - errstr[tre->errnum]: HAWK_T("unknown error"); -} - diff --git a/hawk/lib/tree-prv.h b/hawk/lib/tree-prv.h index 15bae803..95d018c9 100644 --- a/hawk/lib/tree-prv.h +++ b/hawk/lib/tree-prv.h @@ -171,7 +171,7 @@ struct hawk_nde_rex_t { HAWK_NDE_HDR; hawk_oocs_t str; - void* code[2]; /* [0]: case sensitive, [1]: case insensitive */ + hawk_tre_t* code[2]; /* [0]: case sensitive, [1]: case insensitive */ }; /* HAWK_NDE_FUN - function as a value */ diff --git a/hawk/lib/val-prv.h b/hawk/lib/val-prv.h index 456de2fe..9ae5820e 100644 --- a/hawk/lib/val-prv.h +++ b/hawk/lib/val-prv.h @@ -142,7 +142,7 @@ extern hawk_val_t* hawk_val_zls; void hawk_rtx_freeval ( hawk_rtx_t* rtx, hawk_val_t* val, - int cache + int cache ); void hawk_rtx_freevalchunk ( diff --git a/hawk/lib/val.c b/hawk/lib/val.c index cb77e071..bb793006 100644 --- a/hawk/lib/val.c +++ b/hawk/lib/val.c @@ -400,7 +400,7 @@ hawk_val_t* hawk_rtx_makembsvalwithbcs (hawk_rtx_t* rtx, const hawk_bcs_t* mxstr return hawk_rtx_makembsval(rtx, mxstr->ptr, mxstr->len); } -hawk_val_t* hawk_rtx_makerexval (hawk_rtx_t* rtx, const hawk_oocs_t* str, void* code[2]) +hawk_val_t* hawk_rtx_makerexval (hawk_rtx_t* rtx, const hawk_oocs_t* str, hawk_tre_t* code[2]) { hawk_val_rex_t* val; hawk_oow_t totsz; @@ -1371,19 +1371,13 @@ int hawk_rtx_valtostr (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_rtx_valtostr_o switch (vtype) { case HAWK_VAL_NIL: - { return str_to_str(rtx, HAWK_T(""), 0, out); - } case HAWK_VAL_INT: - { return val_int_to_str(rtx, (hawk_val_int_t*)v, out); - } case HAWK_VAL_FLT: - { return val_flt_to_str(rtx, (hawk_val_flt_t*)v, out); - } case HAWK_VAL_STR: { @@ -1402,31 +1396,27 @@ int hawk_rtx_valtostr (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_rtx_valtostr_o } case HAWK_VAL_FUN: - { return str_to_str(rtx, ((hawk_val_fun_t*)v)->fun->name.ptr, ((hawk_val_fun_t*)v)->fun->name.len, out); - } case HAWK_VAL_MAP: - { if (rtx->awk->opt.trait & HAWK_FLEXMAP) { return str_to_str(rtx, HAWK_T("#MAP"), 4, out); } - break; - } + goto invalid; case HAWK_VAL_REF: - { return val_ref_to_str(rtx, (hawk_val_ref_t*)v, out); - } + + case HAWK_VAL_REX: + default: + invalid: + #if defined(DEBUG_VAL) + hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T(">>WRONG VALUE TYPE [%d] in hawk_rtx_valtostr\n"), v->type); + #endif + hawk_rtx_seterrnum (rtx, HAWK_EVALTOSTR, HAWK_NULL); + return -1; } - - -#if defined(DEBUG_VAL) - hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T(">>WRONG VALUE TYPE [%d] in hawk_rtx_valtostr\n"), v->type); -#endif - hawk_rtx_seterrnum (rtx, HAWK_EVALTOSTR, HAWK_NULL); - return -1; } hawk_bch_t* hawk_rtx_valtobcstrdupwithcmgr (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_oow_t* len, hawk_cmgr_t* cmgr) @@ -1664,71 +1654,57 @@ int hawk_rtx_valtonum (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_int_t* l, hawk switch (vtype) { case HAWK_VAL_NIL: - { *l = 0; return 0; - } case HAWK_VAL_INT: - { *l = HAWK_RTX_GETINTFROMVAL(rtx, v); return 0; /* long */ - } case HAWK_VAL_FLT: - { *r = ((hawk_val_flt_t*)v)->val; return 1; /* real */ - } case HAWK_VAL_STR: - { return hawk_oochars_to_num( HAWK_OOCHARS_TO_NUM_MAKE_OPTION(0, (hawk->opt.trait & HAWK_STRIPSTRSPC), 0), ((hawk_val_str_t*)v)->val.ptr, ((hawk_val_str_t*)v)->val.len, l, r ); - } case HAWK_VAL_MBS: - { return hawk_bchars_to_num( HAWK_OOCHARS_TO_NUM_MAKE_OPTION(0, (hawk->opt.trait & HAWK_STRIPSTRSPC), 0), ((hawk_val_mbs_t*)v)->val.ptr, ((hawk_val_mbs_t*)v)->val.len, l, r ); - } case HAWK_VAL_FUN: - { /* unable to convert a function to a number */ - break; - } + goto invalid; case HAWK_VAL_MAP: - { if (rtx->awk->opt.trait & HAWK_FLEXMAP) { *l = HAWK_HTB_SIZE(((hawk_val_map_t*)v)->map); return 0; /* long */ } - break; - } + goto invalid; case HAWK_VAL_REF: - { return val_ref_to_num(rtx, (hawk_val_ref_t*)v, l, r); - } + + case HAWK_VAL_REX: + default: + invalid: + #if defined(DEBUG_VAL) + hawk_logfmt (hawk, HAWK_T(">>WRONG VALUE TYPE [%d] in hawk_rtx_valtonum()\n"), v->type); + #endif + hawk_rtx_seterrnum (rtx, HAWK_EVALTONUM, HAWK_NULL); + return -1; /* error */ } - -#if defined(DEBUG_VAL) - hawk_logfmt (hawk, HAWK_T(">>WRONG VALUE TYPE [%d] in hawk_rtx_valtonum()\n"), v->type); -#endif - - hawk_rtx_seterrnum (rtx, HAWK_EVALTONUM, HAWK_NULL); - return -1; /* error */ } int hawk_rtx_valtoint (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_int_t* l)