regexp core-dump (obs from Ryszard Szopa)

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@2163 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2008-03-25 11:54:08 +00:00
parent fa73e89807
commit 84c15fda0b
3 changed files with 35 additions and 27 deletions

View File

@ -9813,19 +9813,19 @@ according to options @var{Opts}. The options may be:
be treated as lower case during the matching process. be treated as lower case during the matching process.
@end itemize @end itemize
@item regexp(+@var{RegExp},+@var{String},+@var{Opts},@var{SubMatchVars}) @item regexp(+@var{RegExp},+@var{String},+@var{Opts},?@var{SubMatchVars})
@findex regexp/4 @findex regexp/4
@snindex regexp/4 @snindex regexp/4
@cnindex regexp/4 @cnindex regexp/4
Match regular expression @var{RegExp} to input string @var{String} Match regular expression @var{RegExp} to input string @var{String}
according to options @var{Opts}. The variable @var{SubMatchVars} should according to options @var{Opts}. The variable @var{SubMatchVars} should
be originally a list of unbound variables all will contain a sequence of be originally unbound or a list of unbound variables all will contain a
matches, that is, the head of @var{SubMatchVars} will contain the sequence of matches, that is, the head of @var{SubMatchVars} will
characters in @var{String} that matched the leftmost parenthesized contain the characters in @var{String} that matched the leftmost
subexpression within @var{RegExp}, the next head of list will contain parenthesized subexpression within @var{RegExp}, the next head of list
the characters that matched the next parenthesized subexpression to the will contain the characters that matched the next parenthesized
right in @var{RegExp}, and so on. subexpression to the right in @var{RegExp}, and so on.
The options may be: The options may be:
@itemize @bullet @itemize @bullet

View File

@ -88,12 +88,13 @@ static int regexp(void)
char *buf, *sbuf; char *buf, *sbuf;
regex_t reg; regex_t reg;
int out; int out;
long int nmatch = YAP_IntOfTerm(YAP_ARG7); size_t nmatch;
regmatch_t *pmatch; regmatch_t *pmatch;
long int tout; long int tout;
int yap_flags = YAP_IntOfTerm(YAP_ARG5); int yap_flags = YAP_IntOfTerm(YAP_ARG5);
int regcomp_flags = REG_EXTENDED; int regcomp_flags = REG_EXTENDED;
if ((buf = (char *)YAP_AllocSpaceFromYap(buflen)) == NULL) { if ((buf = (char *)YAP_AllocSpaceFromYap(buflen)) == NULL) {
/* early exit */ /* early exit */
return(FALSE); return(FALSE);
@ -106,8 +107,15 @@ static int regexp(void)
if (yap_flags & 1) if (yap_flags & 1)
regcomp_flags |= REG_ICASE; regcomp_flags |= REG_ICASE;
/* cool, now I have my string in the buffer, let's have some fun */ /* cool, now I have my string in the buffer, let's have some fun */
if (yap_regcomp(&reg,buf, regcomp_flags) != 0) if (yap_regcomp(&reg,buf, regcomp_flags) != 0) {
YAP_FreeSpaceFromYap(buf);
return(FALSE); return(FALSE);
}
if (YAP_IsVarTerm(YAP_ARG7)) {
nmatch = reg.re_nsub;
} else {
nmatch = YAP_IntOfTerm(YAP_ARG7);
}
if ((sbuf = (char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) { if ((sbuf = (char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) {
/* early exit */ /* early exit */
yap_regfree(&reg); yap_regfree(&reg);
@ -121,36 +129,37 @@ static int regexp(void)
YAP_FreeSpaceFromYap(sbuf); YAP_FreeSpaceFromYap(sbuf);
return(FALSE); return(FALSE);
} }
pmatch = YAP_AllocSpaceFromYap(sizeof(regmatch_t)*nmatch); pmatch = YAP_AllocSpaceFromYap(sizeof(regmatch_t)*(nmatch+1));
out = yap_regexec(&reg,sbuf,(int)nmatch,pmatch,0); out = yap_regexec(&reg,sbuf,nmatch+1,pmatch,0);
if (out == 0) { if (out == 0) {
/* match succeed, let's fill the match in */ /* match succeed, let's fill the match in */
long int i; long int i;
YAP_Term TNil = YAP_MkAtomTerm(YAP_LookupAtom("[]")); YAP_Term TNil = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
YAP_Functor FDiff = YAP_MkFunctor(YAP_LookupAtom("-"),2); YAP_Functor FDiff = YAP_MkFunctor(YAP_LookupAtom("-"),2);
tout = YAP_ARG6; tout = TNil;
for (i = 0; i < nmatch; i++) { for (i = nmatch-1; i >= 0; --i) {
int j; int j;
YAP_Term t = TNil; YAP_Term t = TNil;
if (pmatch[i].rm_so == -1) break; if (pmatch[i].rm_so != -1) {
if (yap_flags & 2) { if (yap_flags & 2) {
YAP_Term to[2]; YAP_Term to[2];
to[0] = YAP_MkIntTerm(pmatch[i].rm_so); to[0] = YAP_MkIntTerm(pmatch[i].rm_so);
to[1] = YAP_MkIntTerm(pmatch[i].rm_eo); to[1] = YAP_MkIntTerm(pmatch[i].rm_eo);
t = YAP_MkApplTerm(FDiff,2,to); t = YAP_MkApplTerm(FDiff,2,to);
} else { } else {
for (j = pmatch[i].rm_eo-1; j >= pmatch[i].rm_so; j--) { for (j = pmatch[i].rm_eo-1; j >= pmatch[i].rm_so; j--) {
t = YAP_MkPairTerm(YAP_MkIntTerm(sbuf[j]),t); t = YAP_MkPairTerm(YAP_MkIntTerm(sbuf[j]),t);
}
} }
tout = YAP_MkPairTerm(t,tout);
} }
YAP_Unify(t,YAP_HeadOfTerm(tout));
tout = YAP_TailOfTerm(tout);
} }
out = !YAP_Unify(tout, YAP_ARG6);
} }
else if (out != REG_NOMATCH) { else if (out != REG_NOMATCH) {
return(FALSE); out = 0;
} }
yap_regfree(&reg); yap_regfree(&reg);
YAP_FreeSpaceFromYap(buf); YAP_FreeSpaceFromYap(buf);

View File

@ -39,8 +39,7 @@ regexp(RegExp, String, Opts, OUT) :-
% OUT must be bound to a list of unbound variables. % OUT must be bound to a list of unbound variables.
% Check this and count how many. % Check this and count how many.
% %
check_out(V,_,_,G) :- var(V), !, check_out(V,_,_,G) :- var(V), !.
throw(error(instantiation_error,G)).
check_out([],I,I,_) :- !. check_out([],I,I,_) :- !.
check_out([V|L],I0,IF,G) :- !, check_out([V|L],I0,IF,G) :- !,
(nonvar(V) -> throw(error(type_error(variable,V),G)) ; true), (nonvar(V) -> throw(error(type_error(variable,V),G)) ; true),