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; char *buf, *sbuf; regex_t reg; int out; int yap_flags = YAP_IntOfTerm(YAP_ARG5); int regcomp_flags = REG_NOSUB|REG_EXTENDED; if ((buf = (char *)YAP_AllocSpaceFromYap(buflen)) == NULL) { /* early exit */ return(FALSE); } if (YAP_StringToBuffer(YAP_ARG1,buf,buflen) == FALSE) { /* something went wrong, possibly a type checking error */ YAP_FreeSpaceFromYap(buf); return(FALSE); } if (yap_flags & 1) regcomp_flags |= REG_ICASE; /* cool, now I have my string in the buffer, let's have some fun */ if (yap_regcomp(®,buf, regcomp_flags) != 0) return(FALSE); if ((sbuf = (char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) { /* early exit */ yap_regfree(®); YAP_FreeSpaceFromYap(buf); return(FALSE); } if (YAP_StringToBuffer(YAP_ARG3,sbuf,sbuflen) == FALSE) { /* something went wrong, possibly a type checking error */ yap_regfree(®); YAP_FreeSpaceFromYap(buf); YAP_FreeSpaceFromYap(sbuf); return(FALSE); } out = yap_regexec(®,sbuf,0,NULL,0); yap_regfree(®); YAP_FreeSpaceFromYap(buf); YAP_FreeSpaceFromYap(sbuf); if (out != 0 && out != REG_NOMATCH) { return(FALSE); } return(out == 0); }
/* execute a command as a detached process */ static YAP_Bool do_shell(void) { #if defined(__MINGW32__) || _MSC_VER YAP_Error(0, 0L, "system not available in this configuration"); return (FALSE); #elif HAVE_SYSTEM char *buf = YAP_AllocSpaceFromYap(BUF_SIZE); int sys; if (buf == NULL) { YAP_Error(0, 0L, "No Temporary Space for Shell"); return (FALSE); } #if HAVE_STRNCPY strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)), BUF_SIZE); strncpy(buf, " ", BUF_SIZE); strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)), BUF_SIZE); strncpy(buf, " ", BUF_SIZE); strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), BUF_SIZE); #else strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1))); strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2))); strcpy(buf, " "); strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3))); #endif sys = system(buf); YAP_FreeSpaceFromYap(buf); if (sys < 0) { return YAP_Unify(YAP_ARG5, YAP_MkIntTerm(errno)); } return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(sys)); #else char *cptr[4]; int t; int sys; cptr[0] = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)); cptr[1] = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)); cptr[2] = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)); cptr[3] = NULL; t = fork(); if (t < 0) { return YAP_Unify(YAP_ARG5, YAP_MkIntTerm(errno)); } else if (t == 0) { t = execvp(cptr[0], cptr); return t; } else { t = wait(&sys); if (t < 0) { return YAP_Unify(YAP_ARG5, YAP_MkIntTerm(errno)); } } return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(sys)); #endif }
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; char *buf, *sbuf; regex_t reg; int out; size_t nmatch; regmatch_t *pmatch; long int tout; int yap_flags = YAP_IntOfTerm(YAP_ARG5); int regcomp_flags = REG_EXTENDED; if ((buf = (char *)YAP_AllocSpaceFromYap(buflen)) == NULL) { /* early exit */ return(FALSE); } if (YAP_StringToBuffer(YAP_ARG1,buf,buflen) == FALSE) { /* something went wrong, possibly a type checking error */ YAP_FreeSpaceFromYap(buf); return(FALSE); } if (yap_flags & 1) regcomp_flags |= REG_ICASE; /* cool, now I have my string in the buffer, let's have some fun */ if (yap_regcomp(®,buf, regcomp_flags) != 0) { YAP_FreeSpaceFromYap(buf); return(FALSE); } if (YAP_IsVarTerm(YAP_ARG7)) { nmatch = reg.re_nsub; } else { nmatch = YAP_IntOfTerm(YAP_ARG7); } if ((sbuf = (char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) { /* early exit */ yap_regfree(®); YAP_FreeSpaceFromYap(buf); return(FALSE); } if (YAP_StringToBuffer(YAP_ARG3,sbuf,sbuflen) == FALSE) { /* something went wrong, possibly a type checking error */ yap_regfree(®); YAP_FreeSpaceFromYap(buf); YAP_FreeSpaceFromYap(sbuf); return(FALSE); } pmatch = YAP_AllocSpaceFromYap(sizeof(regmatch_t)*(nmatch)); out = yap_regexec(®,sbuf,nmatch,pmatch,0); if (out == 0) { /* match succeed, let's fill the match in */ long int i; YAP_Term TNil = YAP_MkAtomTerm(YAP_LookupAtom("[]")); YAP_Functor FDiff = YAP_MkFunctor(YAP_LookupAtom("-"),2); tout = TNil; for (i = nmatch-1; i >= 0; --i) { int j; YAP_Term t = TNil; if (pmatch[i].rm_so != -1) { 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); } } out = !YAP_Unify(tout, YAP_ARG6); } else if (out != REG_NOMATCH) { out = 0; } yap_regfree(®); YAP_FreeSpaceFromYap(buf); YAP_FreeSpaceFromYap(sbuf); YAP_FreeSpaceFromYap(pmatch); return(out == 0); }