31#define yap_regcomp(A, B, C) regcomp(A, B, C)
32#define yap_regexec(A, B, C, D, E) regexec(A, B, C, D, E)
33#define yap_regfree(A) regfree(A)
34#define yap_regerror(A, B, C, D) regfree(A, B, C, D)
41void init_regexp(
void);
43static YAP_Bool check_regexp(
void) {
44 unsigned int buflen = (
unsigned int)YAP_IntOfTerm(YAP_ARG2) + 1;
45 unsigned int sbuflen = (
unsigned int)YAP_IntOfTerm(YAP_ARG4) + 1;
49 int yap_flags = YAP_IntOfTerm(YAP_ARG5);
50 int regcomp_flags = REG_NOSUB | REG_EXTENDED;
52 if ((buf = (
char *)YAP_AllocSpaceFromYap(buflen)) == NULL) {
58 YAP_FreeSpaceFromYap(buf);
62 regcomp_flags |= REG_ICASE;
64 if (yap_regcomp(®, buf, regcomp_flags) != 0)
66 if ((sbuf = (
char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) {
69 YAP_FreeSpaceFromYap(buf);
75 YAP_FreeSpaceFromYap(buf);
76 YAP_FreeSpaceFromYap(sbuf);
79 out = yap_regexec(®, sbuf, 0, NULL, 0);
81 YAP_FreeSpaceFromYap(buf);
82 YAP_FreeSpaceFromYap(sbuf);
83 if (out != 0 && out != REG_NOMATCH) {
89static YAP_Bool regexp(
void) {
90 unsigned int buflen = (
unsigned int)YAP_IntOfTerm(YAP_ARG2) + 1;
91 unsigned int sbuflen = (
unsigned int)YAP_IntOfTerm(YAP_ARG4) + 1;
98 int yap_flags = YAP_IntOfTerm(YAP_ARG5);
99 int regcomp_flags = REG_EXTENDED;
101 if ((buf = (
char *)YAP_AllocSpaceFromYap(buflen)) == NULL) {
107 YAP_FreeSpaceFromYap(buf);
111 regcomp_flags |= REG_ICASE;
113 if (yap_regcomp(®, buf, regcomp_flags) != 0) {
114 YAP_FreeSpaceFromYap(buf);
117 if (YAP_IsVarTerm(YAP_ARG7)) {
118 nmatch = reg.re_nsub;
120 nmatch = YAP_IntOfTerm(YAP_ARG7);
122 if ((sbuf = (
char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) {
125 YAP_FreeSpaceFromYap(buf);
131 YAP_FreeSpaceFromYap(buf);
132 YAP_FreeSpaceFromYap(sbuf);
135 pmatch = YAP_AllocSpaceFromYap(
sizeof(
regmatch_t) * (nmatch));
136 out = yap_regexec(®, sbuf, nmatch, pmatch, 0);
140 YAP_Term TNil = YAP_MkAtomTerm(YAP_LookupAtom(
"[]"));
141 YAP_Functor FDiff = YAP_MkFunctor(YAP_LookupAtom(
"-"), 2);
144 for (i = nmatch - 1; i >= 0; --i) {
148 if (pmatch[i].rm_so != -1) {
151 to[0] = YAP_MkIntTerm(pmatch[i].rm_so);
152 to[1] = YAP_MkIntTerm(pmatch[i].rm_eo);
153 t = YAP_MkApplTerm(FDiff, 2, to);
155 for (j = pmatch[i].rm_eo - 1; j >= pmatch[i].rm_so; j--) {
156 t = YAP_MkPairTerm(YAP_MkIntTerm(sbuf[j]), t);
159 tout = YAP_MkPairTerm(t, tout);
162 out = !YAP_Unify(tout, YAP_ARG6);
163 }
else if (out != REG_NOMATCH) {
167 YAP_FreeSpaceFromYap(buf);
168 YAP_FreeSpaceFromYap(sbuf);
169 YAP_FreeSpaceFromYap(pmatch);
173void init_regexp(
void) {
174 YAP_UserCPredicate(
"check_regexp", check_regexp, 5);
175 YAP_UserCPredicate(
"check_regexp", regexp, 7);
182int WINAPI win_regexp(HANDLE, DWORD, LPVOID);
184int WINAPI win_regexp(HANDLE hinst, DWORD reason, LPVOID reserved) {
186 case DLL_PROCESS_ATTACH:
188 case DLL_PROCESS_DETACH:
190 case DLL_THREAD_ATTACH:
192 case DLL_THREAD_DETACH:
X_API char * YAP_StringToBuffer(Term t, char *buf, unsigned int bufsize)
copy a string to a buffer, the buffer must have been malloced