| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | /*************************************************************************
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |  *									 * | 
					
						
							|  |  |  |  *	 YAP Prolog 							 * | 
					
						
							|  |  |  |  *									 * | 
					
						
							|  |  |  |  *	Yap Prolog was developed at NCCUP - Universidade do Porto	 * | 
					
						
							|  |  |  |  *									 * | 
					
						
							|  |  |  |  * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 * | 
					
						
							|  |  |  |  *									 * | 
					
						
							|  |  |  |  ************************************************************************** | 
					
						
							|  |  |  |  *									 * | 
					
						
							|  |  |  |  * File:		regexp.c * Last rev: | 
					
						
							|  |  |  |  ** mods: * comments:	regular expression interpreter * | 
					
						
							|  |  |  |  *									 * | 
					
						
							|  |  |  |  *************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file regexp.c | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * A port of the Unix regular expression compiler. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @namespace regexp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | #if HAVE_SYS_TYPES_H
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  | #include "YapInterface.h"
 | 
					
						
							| 
									
										
										
										
											2002-10-07 14:50:00 +00:00
										 |  |  | #if HAVE_REGEXEC
 | 
					
						
							| 
									
										
										
										
											2002-03-12 16:30:23 +00:00
										 |  |  | #include "regex.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | #define yap_regcomp(A, B, C) regcomp(A, B, C)
 | 
					
						
							|  |  |  | #define yap_regexec(A, B, C, D, E) regexec(A, B, C, D, E)
 | 
					
						
							| 
									
										
										
										
											2002-03-12 16:30:23 +00:00
										 |  |  | #define yap_regfree(A) regfree(A)
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | #define yap_regerror(A, B, C, D) regfree(A, B, C, D)
 | 
					
						
							| 
									
										
										
										
											2002-03-12 16:30:23 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | #include "yapregex.h"
 | 
					
						
							| 
									
										
										
										
											2002-03-12 16:30:23 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | /* for the sake of NULL */ | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | void init_regexp(void); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | static YAP_Bool check_regexp(void) { | 
					
						
							|  |  |  |   unsigned int buflen = (unsigned int)YAP_IntOfTerm(YAP_ARG2) + 1; | 
					
						
							|  |  |  |   unsigned int sbuflen = (unsigned int)YAP_IntOfTerm(YAP_ARG4) + 1; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   char *buf, *sbuf; | 
					
						
							|  |  |  |   regex_t reg; | 
					
						
							|  |  |  |   int out; | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   int yap_flags = YAP_IntOfTerm(YAP_ARG5); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   int regcomp_flags = REG_NOSUB | REG_EXTENDED; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   if ((buf = (char *)YAP_AllocSpaceFromYap(buflen)) == NULL) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* early exit */ | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   if (YAP_StringToBuffer(YAP_ARG1, buf, buflen) == FALSE) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* something went wrong, possibly a type checking error */ | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |     YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (yap_flags & 1) | 
					
						
							|  |  |  |     regcomp_flags |= REG_ICASE; | 
					
						
							|  |  |  |   /* cool, now I have my string in the buffer, let's have some fun */ | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   if (yap_regcomp(®, buf, regcomp_flags) != 0) | 
					
						
							|  |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   if ((sbuf = (char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* early exit */ | 
					
						
							|  |  |  |     yap_regfree(®); | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |     YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   if (YAP_StringToBuffer(YAP_ARG3, sbuf, sbuflen) == FALSE) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* something went wrong, possibly a type checking error */ | 
					
						
							|  |  |  |     yap_regfree(®); | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |     YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     YAP_FreeSpaceFromYap(sbuf); | 
					
						
							|  |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   out = yap_regexec(®, sbuf, 0, NULL, 0); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   yap_regfree(®); | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   YAP_FreeSpaceFromYap(sbuf); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   if (out != 0 && out != REG_NOMATCH) { | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   return (out == 0); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | static YAP_Bool regexp(void) { | 
					
						
							|  |  |  |   unsigned int buflen = (unsigned int)YAP_IntOfTerm(YAP_ARG2) + 1; | 
					
						
							|  |  |  |   unsigned int sbuflen = (unsigned int)YAP_IntOfTerm(YAP_ARG4) + 1; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   char *buf, *sbuf; | 
					
						
							|  |  |  |   regex_t reg; | 
					
						
							|  |  |  |   int out; | 
					
						
							| 
									
										
										
										
											2008-03-25 11:54:08 +00:00
										 |  |  |   size_t nmatch; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   regmatch_t *pmatch; | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   long int tout; | 
					
						
							|  |  |  |   int yap_flags = YAP_IntOfTerm(YAP_ARG5); | 
					
						
							|  |  |  |   int regcomp_flags = REG_EXTENDED; | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   if ((buf = (char *)YAP_AllocSpaceFromYap(buflen)) == NULL) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* early exit */ | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   if (YAP_StringToBuffer(YAP_ARG1, buf, buflen) == FALSE) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* something went wrong, possibly a type checking error */ | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |     YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (yap_flags & 1) | 
					
						
							|  |  |  |     regcomp_flags |= REG_ICASE; | 
					
						
							|  |  |  |   /* cool, now I have my string in the buffer, let's have some fun */ | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   if (yap_regcomp(®, buf, regcomp_flags) != 0) { | 
					
						
							| 
									
										
										
										
											2008-03-25 11:54:08 +00:00
										 |  |  |     YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2008-03-25 11:54:08 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (YAP_IsVarTerm(YAP_ARG7)) { | 
					
						
							|  |  |  |     nmatch = reg.re_nsub; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     nmatch = YAP_IntOfTerm(YAP_ARG7); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   if ((sbuf = (char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* early exit */ | 
					
						
							|  |  |  |     yap_regfree(®); | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |     YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   if (YAP_StringToBuffer(YAP_ARG3, sbuf, sbuflen) == FALSE) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     /* something went wrong, possibly a type checking error */ | 
					
						
							|  |  |  |     yap_regfree(®); | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |     YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     YAP_FreeSpaceFromYap(sbuf); | 
					
						
							|  |  |  |     return (FALSE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   pmatch = YAP_AllocSpaceFromYap(sizeof(regmatch_t) * (nmatch)); | 
					
						
							|  |  |  |   out = yap_regexec(®, sbuf, nmatch, pmatch, 0); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   if (out == 0) { | 
					
						
							|  |  |  |     /* match succeed, let's fill the match in */ | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |     long int i; | 
					
						
							|  |  |  |     YAP_Term TNil = YAP_MkAtomTerm(YAP_LookupAtom("[]")); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     YAP_Functor FDiff = YAP_MkFunctor(YAP_LookupAtom("-"), 2); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-25 11:54:08 +00:00
										 |  |  |     tout = TNil; | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |     for (i = nmatch - 1; i >= 0; --i) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       int j; | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |       YAP_Term t = TNil; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-25 11:54:08 +00:00
										 |  |  |       if (pmatch[i].rm_so != -1) { | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |         if (yap_flags & 2) { | 
					
						
							|  |  |  |           YAP_Term to[2]; | 
					
						
							|  |  |  |           to[0] = YAP_MkIntTerm(pmatch[i].rm_so); | 
					
						
							|  |  |  |           to[1] = YAP_MkIntTerm(pmatch[i].rm_eo); | 
					
						
							|  |  |  |           t = YAP_MkApplTerm(FDiff, 2, to); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           for (j = pmatch[i].rm_eo - 1; j >= pmatch[i].rm_so; j--) { | 
					
						
							|  |  |  |             t = YAP_MkPairTerm(YAP_MkIntTerm(sbuf[j]), t); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         tout = YAP_MkPairTerm(t, tout); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-03-25 11:54:08 +00:00
										 |  |  |     out = !YAP_Unify(tout, YAP_ARG6); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   } else if (out != REG_NOMATCH) { | 
					
						
							| 
									
										
										
										
											2008-03-25 11:54:08 +00:00
										 |  |  |     out = 0; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   yap_regfree(®); | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   YAP_FreeSpaceFromYap(buf); | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  |   YAP_FreeSpaceFromYap(sbuf); | 
					
						
							|  |  |  |   YAP_FreeSpaceFromYap(pmatch); | 
					
						
							|  |  |  |   return (out == 0); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | void init_regexp(void) { | 
					
						
							| 
									
										
										
										
											2002-09-09 17:40:12 +00:00
										 |  |  |   YAP_UserCPredicate("check_regexp", check_regexp, 5); | 
					
						
							|  |  |  |   YAP_UserCPredicate("check_regexp", regexp, 7); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 01:52:01 +00:00
										 |  |  | #if __WINDOWS__
 | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 01:52:01 +00:00
										 |  |  | int WINAPI win_regexp(HANDLE, DWORD, LPVOID); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 23:25:58 +01:00
										 |  |  | int WINAPI win_regexp(HANDLE hinst, DWORD reason, LPVOID reserved) { | 
					
						
							|  |  |  |   switch (reason) { | 
					
						
							|  |  |  |   case DLL_PROCESS_ATTACH: | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  |   case DLL_PROCESS_DETACH: | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  |   case DLL_THREAD_ATTACH: | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  |   case DLL_THREAD_DETACH: | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |