cleaned up regular expression functions in misc.c

This commit is contained in:
hyung-hwan 2019-12-19 09:11:40 +00:00
parent 9387994622
commit c9e0adfde0
10 changed files with 145 additions and 168 deletions

View File

@ -1158,8 +1158,8 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[])
goto oops; goto oops;
} }
rtx = hawk_rtx_openstd ( rtx = hawk_rtx_openstdwithbcstr(
awk, 0, HAWK_T("hawk"), awk, 0, "hawk",
(arg.call? HAWK_NULL: (const hawk_ooch_t**)arg.icf.ptr), /* console input */ (arg.call? HAWK_NULL: (const hawk_ooch_t**)arg.icf.ptr), /* console input */
HAWK_NULL, /* console output */ HAWK_NULL, /* console output */
arg.console_cmgr arg.console_cmgr

View File

@ -928,10 +928,9 @@ int hawk_fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
} }
else else
{ {
p = hawk_rtx_strxntokbyrex(rtx, str.ptr, org_len, p, str.len, fs_rex, &tok, &errnum); p = hawk_rtx_strxntokbyrex(rtx, str.ptr, org_len, p, str.len, fs_rex, &tok);
if (p == HAWK_NULL && errnum != HAWK_ENOERR) if (p == HAWK_NULL && hawk_rtx_geterrnum(rtx) != HAWK_ENOERR)
{ {
hawk_rtx_seterrnum (rtx, errnum, HAWK_NULL);
goto oops; goto oops;
} }
} }
@ -1170,25 +1169,16 @@ static int __substitute (hawk_rtx_t* rtx, hawk_int_t max_count)
* end of string($) needs to be tested */ * end of string($) needs to be tested */
while (cur.ptr <= s2_end) while (cur.ptr <= s2_end)
{ {
hawk_errnum_t errnum;
int n; int n;
hawk_oow_t m, i; hawk_oow_t m, i;
if (max_count == 0 || sub_count < max_count) if (max_count == 0 || sub_count < max_count)
{ {
n = hawk_matchrex ( n = hawk_rtx_matchrex(rtx, rex, &s2, &cur, &mat, HAWK_NULL);
hawk_rtx_gethawk(rtx), rex, rtx->gbl.ignorecase, if (n <= -1) goto oops;
&s2, &cur, &mat, HAWK_NULL, &errnum
);
} }
else n = 0; else n = 0;
if (n <= -1)
{
hawk_rtx_seterrnum (rtx, errnum, HAWK_NULL);
goto oops;
}
if (n == 0) if (n == 0)
{ {
/* no more match found */ /* no more match found */
@ -1366,7 +1356,7 @@ int hawk_fnc_match (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
tmp.ptr = str0 + start - 1; tmp.ptr = str0 + start - 1;
tmp.len = len0 - start + 1; tmp.len = len0 - start + 1;
n = hawk_rtx_matchrex(rtx, a1, &tmp, &tmp, &mat, (nargs >= 4? submat: HAWK_NULL)); n = hawk_rtx_matchval(rtx, a1, &tmp, &tmp, &mat, (nargs >= 4? submat: HAWK_NULL));
if (n <= -1) return -1; if (n <= -1) return -1;
} }

View File

@ -125,7 +125,8 @@ HAWK_EXPORT int hawk_tre_execx (
hawk_oow_t len, hawk_oow_t len,
hawk_tre_match_t* pmatch, hawk_tre_match_t* pmatch,
hawk_oow_t nmatch, hawk_oow_t nmatch,
int eflags int eflags,
hawk_gem_t* errgem
); );
HAWK_EXPORT int hawk_tre_exec ( HAWK_EXPORT int hawk_tre_exec (
@ -133,7 +134,8 @@ HAWK_EXPORT int hawk_tre_exec (
const hawk_ooch_t* str, const hawk_ooch_t* str,
hawk_tre_match_t* pmatch, hawk_tre_match_t* pmatch,
hawk_oow_t nmatch, hawk_oow_t nmatch,
int eflags int eflags,
hawk_gem_t* errgem
); );
#if defined(__cplusplus) #if defined(__cplusplus)

View File

@ -55,9 +55,8 @@ hawk_ooch_t* hawk_rtx_strxntokbyrex (
hawk_oow_t len, hawk_oow_t len,
const hawk_ooch_t* substr, const hawk_ooch_t* substr,
hawk_oow_t sublen, hawk_oow_t sublen,
void* rex, hawk_tre_t* rex,
hawk_oocs_t* tok, hawk_oocs_t* tok
hawk_errnum_t* errnum
); );
hawk_ooch_t* hawk_rtx_strxnfld ( hawk_ooch_t* hawk_rtx_strxnfld (
@ -80,20 +79,22 @@ int hawk_buildrex (
hawk_tre_t** icode hawk_tre_t** icode
); );
int hawk_matchrex (
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, hawk_tre_t* code, hawk_tre_t* icode); void hawk_freerex (hawk_t* awk, hawk_tre_t* code, hawk_tre_t* icode);
int hawk_rtx_matchrex ( int hawk_rtx_matchval (
hawk_rtx_t* rtx, hawk_val_t* val, hawk_rtx_t* rtx, hawk_val_t* val,
const hawk_oocs_t* str, const hawk_oocs_t* substr, const hawk_oocs_t* str, const hawk_oocs_t* substr,
hawk_oocs_t* match, hawk_oocs_t submat[9] hawk_oocs_t* match, hawk_oocs_t submat[9]
); );
int hawk_rtx_matchrex (
hawk_rtx_t* rtx, hawk_tre_t* code,
const hawk_oocs_t* str, const hawk_oocs_t* substr,
hawk_oocs_t* match, hawk_oocs_t submat[9]
);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -244,8 +244,7 @@ hawk_ooch_t* hawk_rtx_strxntokbyrex (
hawk_rtx_t* rtx, hawk_rtx_t* rtx,
const hawk_ooch_t* str, hawk_oow_t len, const hawk_ooch_t* str, hawk_oow_t len,
const hawk_ooch_t* substr, hawk_oow_t sublen, const hawk_ooch_t* substr, hawk_oow_t sublen,
void* rex, hawk_oocs_t* tok, hawk_tre_t* rex, hawk_oocs_t* tok)
hawk_errnum_t* errnum)
{ {
int n; int n;
hawk_oow_t i; hawk_oow_t i;
@ -262,17 +261,15 @@ hawk_ooch_t* hawk_rtx_strxntokbyrex (
while (cursub.len > 0) while (cursub.len > 0)
{ {
n = hawk_matchrex ( n = hawk_rtx_matchrex(rtx, rex, &s, &cursub, &match, HAWK_NULL);
rtx->awk, rex, rtx->gbl.ignorecase, if (n <= -1) return HAWK_NULL;
&s, &cursub, &match, HAWK_NULL, errnum);
if (n == -1) return HAWK_NULL;
if (n == 0) if (n == 0)
{ {
/* no match has been found. /* no match has been found. return the entire string as a token */
* return the entire string as a token */ hawk_rtx_seterrnum (rtx, HAWK_ENOERR, HAWK_NULL); /* reset HAWK_EREXNOMAT to no error */
tok->ptr = realsub.ptr; tok->ptr = realsub.ptr;
tok->len = realsub.len; tok->len = realsub.len;
*errnum = HAWK_ENOERR;
return HAWK_NULL; return HAWK_NULL;
} }
@ -310,11 +307,12 @@ hawk_ooch_t* hawk_rtx_strxntokbyrex (
} }
exit_loop: exit_loop:
hawk_rtx_seterrnum (rtx, HAWK_ENOERR, HAWK_NULL);
if (cursub.len <= 0) if (cursub.len <= 0)
{ {
tok->ptr = realsub.ptr; tok->ptr = realsub.ptr;
tok->len = realsub.len; tok->len = realsub.len;
*errnum = HAWK_ENOERR;
return HAWK_NULL; return HAWK_NULL;
} }
@ -326,13 +324,11 @@ exit_loop:
if (!hawk_is_ooch_space(match.ptr[i])) if (!hawk_is_ooch_space(match.ptr[i]))
{ {
/* the match contains a non-space character. */ /* the match contains a non-space character. */
*errnum = HAWK_ENOERR;
return (hawk_ooch_t*)match.ptr+match.len; return (hawk_ooch_t*)match.ptr+match.len;
} }
} }
/* the match is all spaces */ /* the match is all spaces */
*errnum = HAWK_ENOERR;
if (rtx->gbl.striprecspc > 0 || (rtx->gbl.striprecspc < 0 && (rtx->awk->opt.trait & HAWK_STRIPRECSPC))) if (rtx->gbl.striprecspc > 0 || (rtx->gbl.striprecspc < 0 && (rtx->awk->opt.trait & HAWK_STRIPRECSPC)))
{ {
/* if the match reached the last character in the input string, /* if the match reached the last character in the input string,
@ -486,21 +482,17 @@ int hawk_buildrex (hawk_t* awk, const hawk_ooch_t* ptn, hawk_oow_t len, hawk_err
return 0; return 0;
} }
static int matchtre ( static int matchtre (hawk_tre_t* tre, int opt, const hawk_oocs_t* str, hawk_oocs_t* mat, hawk_oocs_t submat[9], hawk_gem_t* errgem)
hawk_tre_t* tre, int opt,
const hawk_oocs_t* str, hawk_oocs_t* mat,
hawk_oocs_t submat[9], hawk_errnum_t* errnum)
{ {
int n; int n;
/*hawk_tre_match_t match[10] = { { 0, 0 }, };*/ /*hawk_tre_match_t match[10] = { { 0, 0 }, };*/
hawk_tre_match_t match[10]; hawk_tre_match_t match[10];
HAWK_MEMSET (match, 0, HAWK_SIZEOF(match)); HAWK_MEMSET (match, 0, HAWK_SIZEOF(match));
n = hawk_tre_execx(tre, str->ptr, str->len, match, HAWK_COUNTOF(match), opt); n = hawk_tre_execx(tre, str->ptr, str->len, match, HAWK_COUNTOF(match), opt, errgem);
if (n <= -1) if (n <= -1)
{ {
if (hawk_gem_geterrnum(tre->gem) == HAWK_EREXNOMAT) return 0; if (hawk_gem_geterrnum(errgem) == HAWK_EREXNOMAT) return 0;
*errnum = hawk_gem_geterrnum(tre->gem);
return -1; return -1;
} }
@ -529,39 +521,22 @@ static int matchtre (
return 1; return 1;
} }
int hawk_matchrex (
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)
{
int x;
int opt = HAWK_TRE_BACKTRACKING; /* TODO: option... HAWK_TRE_BACKTRACKING ??? */
x = matchtre (
code, ((str->ptr == substr->ptr)? opt: (opt | HAWK_TRE_NOTBOL)),
substr, match, submat, errnum
);
return x;
}
void hawk_freerex (hawk_t* awk, hawk_tre_t* code, hawk_tre_t* icode) void hawk_freerex (hawk_t* awk, hawk_tre_t* code, hawk_tre_t* icode)
{ {
if (icode && icode != code) hawk_tre_close (icode); if (icode && icode != code) hawk_tre_close (icode);
if (code) hawk_tre_close (code); if (code) hawk_tre_close (code);
} }
int hawk_rtx_matchrex ( int hawk_rtx_matchval (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])
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])
{ {
hawk_tre_t* code;
int ignorecase, x; int ignorecase, x;
hawk_errnum_t awkerr; int opt = HAWK_TRE_BACKTRACKING; /* TODO: option... HAWK_TRE_BACKTRACKING ??? */
hawk_tre_t* code;
hawk_errnum_t awkerr; /*TODO: get rid of this */
ignorecase = rtx->gbl.ignorecase; ignorecase = rtx->gbl.ignorecase;
if (HAWK_RTX_GETVALTYPE (rtx, val) == HAWK_VAL_REX) if (HAWK_RTX_GETVALTYPE(rtx, val) == HAWK_VAL_REX)
{ {
code = ((hawk_val_rex_t*)val)->code[ignorecase]; code = ((hawk_val_rex_t*)val)->code[ignorecase];
} }
@ -570,7 +545,7 @@ int hawk_rtx_matchrex (
/* convert to a string and build a regular expression */ /* convert to a string and build a regular expression */
hawk_oocs_t tmp; hawk_oocs_t tmp;
tmp.ptr = hawk_rtx_getvaloocstr (rtx, val, &tmp.len); tmp.ptr = hawk_rtx_getvaloocstr(rtx, val, &tmp.len);
if (tmp.ptr == HAWK_NULL) return -1; if (tmp.ptr == HAWK_NULL) return -1;
x = ignorecase? hawk_buildrex(rtx->awk, tmp.ptr, tmp.len, &awkerr, HAWK_NULL, &code): x = ignorecase? hawk_buildrex(rtx->awk, tmp.ptr, tmp.len, &awkerr, HAWK_NULL, &code):
@ -583,11 +558,10 @@ int hawk_rtx_matchrex (
} }
} }
x = matchtre ( x = matchtre(
code, ((str->ptr == substr->ptr)? HAWK_TRE_BACKTRACKING: (HAWK_TRE_BACKTRACKING | HAWK_TRE_NOTBOL)), code, ((str->ptr == substr->ptr)? opt: (opt | HAWK_TRE_NOTBOL)),
substr, match, submat, &awkerr substr, match, submat, hawk_rtx_getgem(rtx)
); );
if (x <= -1) hawk_rtx_seterrnum (rtx, awkerr, HAWK_NULL);
if (HAWK_RTX_GETVALTYPE(rtx, val) == HAWK_VAL_REX) if (HAWK_RTX_GETVALTYPE(rtx, val) == HAWK_VAL_REX)
{ {
@ -595,11 +569,17 @@ int hawk_rtx_matchrex (
} }
else else
{ {
if (ignorecase) hawk_tre_close (code);
hawk_freerex (rtx->awk, HAWK_NULL, code);
else
hawk_freerex (rtx->awk, code, HAWK_NULL);
} }
return x; return x;
} }
int hawk_rtx_matchrex (hawk_rtx_t* rtx, hawk_tre_t* code, const hawk_oocs_t* str, const hawk_oocs_t* substr, hawk_oocs_t* match, hawk_oocs_t submat[9])
{
int opt = HAWK_TRE_BACKTRACKING; /* TODO: option... HAWK_TRE_BACKTRACKING ??? */
return matchtre(
code, ((str->ptr == substr->ptr)? opt: (opt | HAWK_TRE_NOTBOL)),
substr, match, submat, hawk_rtx_getgem(rtx)
);
}

View File

@ -166,26 +166,23 @@ static int split_record (hawk_rtx_t* rtx)
switch (how) switch (how)
{ {
case 0: case 0:
p = hawk_rtx_strxntok (rtx, p = hawk_rtx_strxntok (rtx, p, len, fs_ptr, fs_len, &tok);
p, len, fs_ptr, fs_len, &tok);
break; break;
case 1: case 1:
break; break;
default: default:
p = hawk_rtx_strxntokbyrex ( p = hawk_rtx_strxntokbyrex(
rtx, rtx,
HAWK_OOECS_PTR(&rtx->inrec.line), HAWK_OOECS_PTR(&rtx->inrec.line),
HAWK_OOECS_LEN(&rtx->inrec.line), HAWK_OOECS_LEN(&rtx->inrec.line),
p, len, p, len,
rtx->gbl.fs[rtx->gbl.ignorecase], &tok, &errnum rtx->gbl.fs[rtx->gbl.ignorecase], &tok
); );
if (p == HAWK_NULL && errnum != HAWK_ENOERR) if (p == HAWK_NULL && hawk_rtx_geterrnum(rtx) != HAWK_ENOERR)
{ {
if (fs_free != HAWK_NULL) if (fs_free) hawk_rtx_freemem (rtx, fs_free);
hawk_rtx_freemem (rtx, fs_free);
hawk_rtx_seterrnum (rtx, errnum, HAWK_NULL);
return -1; return -1;
} }
} }
@ -262,22 +259,19 @@ static int split_record (hawk_rtx_t* rtx)
default: default:
/* all other cases */ /* all other cases */
p = hawk_rtx_strxntokbyrex ( p = hawk_rtx_strxntokbyrex(
rtx, rtx,
HAWK_OOECS_PTR(&rtx->inrec.line), HAWK_OOECS_PTR(&rtx->inrec.line),
HAWK_OOECS_LEN(&rtx->inrec.line), HAWK_OOECS_LEN(&rtx->inrec.line),
p, len, p, len,
rtx->gbl.fs[rtx->gbl.ignorecase], &tok, &errnum rtx->gbl.fs[rtx->gbl.ignorecase], &tok
); );
if (p == HAWK_NULL && errnum != HAWK_ENOERR) if (p == HAWK_NULL && hawk_rtx_geterrnum(rtx) != HAWK_ENOERR)
{ {
if (fs_free != HAWK_NULL) if (fs_free) hawk_rtx_freemem (rtx, fs_free);
hawk_rtx_freemem (rtx, fs_free);
hawk_rtx_seterrnum (rtx, errnum, HAWK_NULL);
return -1; return -1;
} }
} }
#if 1 #if 1
if (rtx->inrec.nflds == 0 && p == HAWK_NULL && tok.len == 0) if (rtx->inrec.nflds == 0 && p == HAWK_NULL && tok.len == 0)
{ {

View File

@ -228,15 +228,11 @@ static HAWK_INLINE int match_long_rs (hawk_rtx_t* rtx, hawk_ooecs_t* buf, hawk_r
HAWK_ASSERT (hawk_rtx_gethawk(rtx), rtx->gbl.rs[0] != 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); HAWK_ASSERT (hawk_rtx_gethawk(rtx), rtx->gbl.rs[1] != HAWK_NULL);
ret = hawk_matchrex( ret = hawk_rtx_matchrex(
rtx->awk, rtx->gbl.rs[rtx->gbl.ignorecase], rtx, rtx->gbl.rs[rtx->gbl.ignorecase],
rtx->gbl.ignorecase, HAWK_OOECS_OOCS(buf), HAWK_OOECS_OOCS(buf), HAWK_OOECS_OOCS(buf), HAWK_OOECS_OOCS(buf),
&match, HAWK_NULL, &errnum); &match, HAWK_NULL);
if (ret <= -1) if (ret >= 1)
{
hawk_rtx_seterrnum (rtx, errnum, HAWK_NULL);
}
else if (ret >= 1)
{ {
if (p->in.eof) if (p->in.eof)
{ {

View File

@ -3238,7 +3238,7 @@ static hawk_val_t* eval_expression (hawk_rtx_t* rtx, hawk_nde_t* nde)
vs.len = ((hawk_val_str_t*)rtx->inrec.d0)->val.len; vs.len = ((hawk_val_str_t*)rtx->inrec.d0)->val.len;
} }
n = hawk_rtx_matchrex(rtx, v, &vs, &vs, HAWK_NULL, HAWK_NULL); n = hawk_rtx_matchval(rtx, v, &vs, &vs, HAWK_NULL, HAWK_NULL);
if (n <= -1) if (n <= -1)
{ {
ADJERR_LOC (rtx, &nde->loc); ADJERR_LOC (rtx, &nde->loc);
@ -5261,7 +5261,7 @@ static hawk_val_t* eval_binop_match0 (
out.ptr = hawk_rtx_getvaloocstr (rtx, left, &out.len); out.ptr = hawk_rtx_getvaloocstr (rtx, left, &out.len);
if (out.ptr == HAWK_NULL) return HAWK_NULL; if (out.ptr == HAWK_NULL) return HAWK_NULL;
n = hawk_rtx_matchrex (rtx, right, &out, &out, HAWK_NULL, HAWK_NULL); n = hawk_rtx_matchval(rtx, right, &out, &out, HAWK_NULL, HAWK_NULL);
hawk_rtx_freevaloocstr (rtx, left, out.ptr); hawk_rtx_freevaloocstr (rtx, left, out.ptr);
if (n <= -1) if (n <= -1)

View File

@ -160,14 +160,14 @@ typedef struct rxtn_t
struct struct
{ {
struct { struct {
const hawk_ooch_t*const* files; const hawk_ooch_t** files;
hawk_oow_t index; hawk_oow_t index;
hawk_oow_t count; hawk_oow_t count;
} in; } in;
struct struct
{ {
const hawk_ooch_t*const* files; const hawk_ooch_t** files;
hawk_oow_t index; hawk_oow_t index;
hawk_oow_t count; hawk_oow_t count;
} out; } out;
@ -894,16 +894,14 @@ oops:
return HAWK_NULL; return HAWK_NULL;
} }
static hawk_sio_t* open_sio (hawk_t* awk, const hawk_ooch_t* file, int flags) static hawk_sio_t* open_sio (hawk_t* hawk, const hawk_ooch_t* file, int flags)
{ {
hawk_sio_t* sio; hawk_sio_t* sio;
sio = hawk_sio_open(hawk_getgem(awk), 0, file, flags); sio = hawk_sio_open(hawk_getgem(hawk), 0, file, flags);
if (sio == HAWK_NULL) if (sio == HAWK_NULL)
{ {
hawk_oocs_t errarg; const hawk_ooch_t* bem = hawk_backuperrmsg(hawk);
errarg.ptr = (hawk_ooch_t*)file; hawk_seterrbfmt (hawk, HAWK_NULL, HAWK_EOPEN, "unable to open %js - %js", file, bem);
errarg.len = hawk_count_oocstr(file);
hawk_seterrnum (awk, HAWK_EOPEN, &errarg);
} }
return sio; return sio;
} }
@ -914,10 +912,8 @@ static hawk_sio_t* open_sio_rtx (hawk_rtx_t* rtx, const hawk_ooch_t* file, int f
sio = hawk_sio_open(hawk_rtx_getgem(rtx), 0, file, flags); sio = hawk_sio_open(hawk_rtx_getgem(rtx), 0, file, flags);
if (sio == HAWK_NULL) if (sio == HAWK_NULL)
{ {
hawk_oocs_t errarg; const hawk_ooch_t* bem = hawk_rtx_backuperrmsg(rtx);
errarg.ptr = (hawk_ooch_t*)file; hawk_rtx_seterrbfmt (rtx, HAWK_NULL, HAWK_EOPEN, "unable to open %js - %js", file, bem);
errarg.len = hawk_count_oocstr(file);
hawk_rtx_seterrnum (rtx, HAWK_EOPEN, &errarg);
} }
return sio; return sio;
} }
@ -1234,21 +1230,43 @@ static hawk_ooi_t sf_in_read (hawk_t* awk, hawk_sio_arg_t* arg, hawk_ooch_t* dat
again: again:
switch (xtn->s.in.x[xtn->s.in.xindex].type) switch (xtn->s.in.x[xtn->s.in.xindex].type)
{ {
#if defined(HAWK_OOCH_IS_UCH)
case HAWK_PARSESTD_FILE: case HAWK_PARSESTD_FILE:
case HAWK_PARSESTD_FILEB: #endif
case HAWK_PARSESTD_FILEU: case HAWK_PARSESTD_FILEU:
HAWK_ASSERT (awk, arg->handle != HAWK_NULL); HAWK_ASSERT (awk, arg->handle != HAWK_NULL);
n = hawk_sio_getoochars (arg->handle, data, size); n = hawk_sio_getoochars(arg->handle, data, size);
if (n <= -1) if (n <= -1)
{ {
hawk_oocs_t ea; const hawk_ooch_t* bem = hawk_backuperrmsg(awk);
ea.ptr = (hawk_ooch_t*)xtn->s.in.x[xtn->s.in.xindex].u.file.path; const hawk_uch_t* path;
if (ea.ptr == HAWK_NULL) ea.ptr = sio_std_names[HAWK_SIO_STDIN].ptr; path = xtn->s.in.x[xtn->s.in.xindex].u.fileu.path;
ea.len = hawk_count_oocstr(ea.ptr); if (path)
hawk_seterrnum (awk, HAWK_EREAD, &ea); hawk_seterrbfmt (awk, HAWK_NULL, HAWK_EREAD, "unable to read %ls - %js", path, bem);
else
hawk_seterrbfmt (awk, HAWK_NULL, HAWK_EREAD, "unable to read %js - %js", sio_std_names[HAWK_SIO_STDIN].ptr, bem);
} }
break; break;
#if defined(HAWK_OOCH_IS_BCH)
case HAWK_PARSESTD_FILE:
#endif
case HAWK_PARSESTD_FILEB:
HAWK_ASSERT (awk, arg->handle != HAWK_NULL);
n = hawk_sio_getoochars(arg->handle, data, size);
if (n <= -1)
{
const hawk_ooch_t* bem = hawk_backuperrmsg(awk);
const hawk_bch_t* path;
path = xtn->s.in.x[xtn->s.in.xindex].u.fileb.path;
if (path)
hawk_seterrbfmt (awk, HAWK_NULL, HAWK_EREAD, "unable to read %hs - %js", path, bem);
else
hawk_seterrbfmt (awk, HAWK_NULL, HAWK_EREAD, "unable to read %js - %js", sio_std_names[HAWK_SIO_STDIN].ptr, bem);
}
break;
case HAWK_PARSESTD_OOCS: case HAWK_PARSESTD_OOCS:
parsestd_str: parsestd_str:
n = 0; n = 0;
@ -1999,10 +2017,7 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod)
if (rxtn->c.in.count == 0) if (rxtn->c.in.count == 0)
{ {
sio = open_sio_std_rtx ( sio = open_sio_std_rtx (rtx, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
rtx, HAWK_SIO_STDIN,
HAWK_SIO_READ | HAWK_SIO_IGNOREECERR
);
if (sio == HAWK_NULL) return -1; if (sio == HAWK_NULL) return -1;
if (rxtn->c.cmgr) hawk_sio_setcmgr (sio, rxtn->c.cmgr); if (rxtn->c.cmgr) hawk_sio_setcmgr (sio, rxtn->c.cmgr);
@ -2111,10 +2126,8 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod)
file = as.ptr; file = as.ptr;
sio = (file[0] == HAWK_T('-') && file[1] == HAWK_T('\0'))? sio = (file[0] == HAWK_T('-') && file[1] == HAWK_T('\0'))?
open_sio_std_rtx (rtx, HAWK_SIO_STDIN, open_sio_std_rtx (rtx, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR):
HAWK_SIO_READ | HAWK_SIO_IGNOREECERR): open_sio_rtx (rtx, file, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
open_sio_rtx (rtx, file,
HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
if (sio == HAWK_NULL) if (sio == HAWK_NULL)
{ {
hawk_rtx_freevaloocstr (rtx, v, as.ptr); hawk_rtx_freevaloocstr (rtx, v, as.ptr);
@ -2180,13 +2193,8 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod)
} }
sio = (file[0] == HAWK_T('-') && file[1] == HAWK_T('\0'))? sio = (file[0] == HAWK_T('-') && file[1] == HAWK_T('\0'))?
open_sio_std_rtx ( open_sio_std_rtx (rtx, HAWK_SIO_STDOUT, HAWK_SIO_WRITE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK):
rtx, HAWK_SIO_STDOUT, open_sio_rtx (rtx, file, HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR);
HAWK_SIO_WRITE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK):
open_sio_rtx (
rtx, file,
HAWK_SIO_WRITE | HAWK_SIO_CREATE |
HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR);
if (sio == HAWK_NULL) return -1; if (sio == HAWK_NULL) return -1;
if (rxtn->c.cmgr) hawk_sio_setcmgr (sio, rxtn->c.cmgr); if (rxtn->c.cmgr) hawk_sio_setcmgr (sio, rxtn->c.cmgr);
@ -2214,7 +2222,7 @@ static hawk_ooi_t awk_rio_console (hawk_rtx_t* rtx, hawk_rio_cmd_t cmd, hawk_rio
switch (cmd) switch (cmd)
{ {
case HAWK_RIO_CMD_OPEN: case HAWK_RIO_CMD_OPEN:
return open_rio_console (rtx, riod); return open_rio_console(rtx, riod);
case HAWK_RIO_CMD_CLOSE: case HAWK_RIO_CMD_CLOSE:
if (riod->handle) hawk_sio_close ((hawk_sio_t*)riod->handle); if (riod->handle) hawk_sio_close ((hawk_sio_t*)riod->handle);
@ -2307,7 +2315,7 @@ static int build_argcv (
hawk_rtx_refupval (rtx, v_argv); hawk_rtx_refupval (rtx, v_argv);
/* make ARGV[0] */ /* make ARGV[0] */
v_tmp = hawk_rtx_makestrvalwithoocstr (rtx, id); v_tmp = hawk_rtx_makestrvalwithoocstr(rtx, id);
if (v_tmp == HAWK_NULL) if (v_tmp == HAWK_NULL)
{ {
hawk_rtx_refdownval (rtx, v_argv); hawk_rtx_refdownval (rtx, v_argv);
@ -2661,15 +2669,18 @@ hawk_rtx_t* hawk_rtx_openstdwithbcstr (
rtx = open_rtx_std(awk, xtnsize, wid, (const hawk_uch_t**)wicf, (const hawk_uch_t**)wocf, cmgr); rtx = open_rtx_std(awk, xtnsize, wid, (const hawk_uch_t**)wicf, (const hawk_uch_t**)wocf, cmgr);
done: done:
if (wocf) if (!rtx)
{ {
for (i = 0; wocf[i]; i++) hawk_freemem (awk, wocf[i]); if (wocf)
hawk_freemem (awk, wocf); {
} for (i = 0; wocf[i]; i++) hawk_freemem (awk, wocf[i]);
if (wicf) hawk_freemem (awk, wocf);
{ }
for (i = 0; wicf[i]; i++) hawk_freemem (awk, wicf[i]); if (wicf)
hawk_freemem (awk, wicf); {
for (i = 0; wicf[i]; i++) hawk_freemem (awk, wicf[i]);
hawk_freemem (awk, wicf);
}
} }
if (wid) hawk_freemem (awk, wid); if (wid) hawk_freemem (awk, wid);
@ -2726,15 +2737,18 @@ hawk_rtx_t* hawk_rtx_openstdwithucstr (
rtx = open_rtx_std(awk, xtnsize, mid, (const hawk_bch_t**)micf, (const hawk_bch_t**)mocf, cmgr); rtx = open_rtx_std(awk, xtnsize, mid, (const hawk_bch_t**)micf, (const hawk_bch_t**)mocf, cmgr);
done: done:
if (mocf) if (!rtx)
{ {
for (i = 0; mocf[i]; i++) hawk_freemem (awk, mocf[i]); if (mocf)
hawk_freemem (awk, mocf); {
} for (i = 0; mocf[i]; i++) hawk_freemem (awk, mocf[i]);
if (micf) hawk_freemem (awk, mocf);
{ }
for (i = 0; micf[i]; i++) hawk_freemem (awk, micf[i]); if (micf)
hawk_freemem (awk, micf); {
for (i = 0; micf[i]; i++) hawk_freemem (awk, micf[i]);
hawk_freemem (awk, micf);
}
} }
if (mid) hawk_freemem (awk, mid); if (mid) hawk_freemem (awk, mid);

View File

@ -98,8 +98,8 @@ int hawk_tre_comp (hawk_tre_t* tre, const hawk_ooch_t* regex, unsigned int* nsub
/* Fills the POSIX.2 regmatch_t array according to the TNFA tag and match /* Fills the POSIX.2 regmatch_t array according to the TNFA tag and match
endpoint values. */ endpoint values. */
void tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags, void tre_fill_pmatch (size_t nmatch, regmatch_t pmatch[], int cflags,
const tre_tnfa_t *tnfa, int *tags, int match_eo) const tre_tnfa_t *tnfa, int *tags, int match_eo)
{ {
tre_submatch_data_t *submatch_data; tre_submatch_data_t *submatch_data;
unsigned int i, j; unsigned int i, j;
@ -168,13 +168,13 @@ void tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags,
Wrapper functions for POSIX compatible regexp matching. Wrapper functions for POSIX compatible regexp matching.
*/ */
int tre_have_backrefs(const regex_t *preg) int tre_have_backrefs (const regex_t *preg)
{ {
tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD; tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
return tnfa->have_backrefs; return tnfa->have_backrefs;
} }
static int tre_match( static int tre_match (
const regex_t* preg, const void *string, hawk_oow_t len, const regex_t* preg, const void *string, hawk_oow_t len,
tre_str_type_t type, hawk_oow_t nmatch, regmatch_t pmatch[], tre_str_type_t type, hawk_oow_t nmatch, regmatch_t pmatch[],
int eflags) int eflags)
@ -184,7 +184,7 @@ static int tre_match(
int *tags = HAWK_NULL, eo; int *tags = HAWK_NULL, eo;
if (tnfa->num_tags > 0 && nmatch > 0) if (tnfa->num_tags > 0 && nmatch > 0)
{ {
tags = xmalloc (preg->gem, sizeof(*tags) * tnfa->num_tags); tags = xmalloc(preg->gem, sizeof(*tags) * tnfa->num_tags);
if (tags == HAWK_NULL) return REG_ESPACE; if (tags == HAWK_NULL) return REG_ESPACE;
} }
@ -213,14 +213,14 @@ static int tre_match(
int hawk_tre_execx ( int hawk_tre_execx (
hawk_tre_t* tre, const hawk_ooch_t *str, hawk_oow_t len, hawk_tre_t* tre, const hawk_ooch_t *str, hawk_oow_t len,
regmatch_t* pmatch, hawk_oow_t nmatch, int eflags) regmatch_t* pmatch, hawk_oow_t nmatch, int eflags, hawk_gem_t* errgem)
{ {
int ret; int ret;
if (tre->TRE_REGEX_T_FIELD == HAWK_NULL) if (tre->TRE_REGEX_T_FIELD == HAWK_NULL)
{ {
/* regular expression is bad as none is compiled yet */ /* regular expression is bad as none is compiled yet */
hawk_gem_seterrnum (tre->gem, HAWK_NULL, HAWK_EREXBADPAT); hawk_gem_seterrnum ((errgem? errgem: tre->gem), HAWK_NULL, HAWK_EREXBADPAT);
return -1; return -1;
} }
#if defined(HAWK_OOCH_IS_UCH) #if defined(HAWK_OOCH_IS_UCH)
@ -230,7 +230,7 @@ int hawk_tre_execx (
#endif #endif
if (ret > 0) if (ret > 0)
{ {
hawk_gem_seterrnum (tre->gem, HAWK_NULL, ret); hawk_gem_seterrnum ((errgem? errgem: tre->gem), HAWK_NULL, ret);
return -1; return -1;
} }
@ -239,7 +239,7 @@ int hawk_tre_execx (
int hawk_tre_exec ( int hawk_tre_exec (
hawk_tre_t* tre, const hawk_ooch_t* str, hawk_tre_t* tre, const hawk_ooch_t* str,
regmatch_t* pmatch, hawk_oow_t nmatch, int eflags) regmatch_t* pmatch, hawk_oow_t nmatch, int eflags, hawk_gem_t* errgem)
{ {
return hawk_tre_execx (tre, str, (hawk_oow_t)-1, pmatch, nmatch, eflags); return hawk_tre_execx(tre, str, (hawk_oow_t)-1, pmatch, nmatch, eflags, errgem);
} }