char * pcre_subst(const pcre *ppat, const pcre_extra *extra, const char *str, int len, int offset, int options, const char *rep) { int nmat; int ovec[MAXCAPTURE * 3]; nmat = pcre_exec(ppat, extra, str, len, offset, options, ovec, sizeof(ovec)); #ifdef DEBUG_PCRE_SUBST dumpmatch(str, len, rep, nmat, ovec); #endif if (nmat <= 0) return NULL; return(edit(str, len, rep, nmat, ovec)); }
char * regexp_replace(char * from, char *to, char *text) { int nmat, offset = 0, textlen; int ovec[MAXCAPTURE]; char *res, *ret, *pom; const char *overfl = NULL; /* warning, go away */ int global, i; #ifdef HAVE_PCRE const char *er_ptr; int erroffset; #else regmatch_t pmat[MAXCAPTURE/3]; regex_t ppat_data; regex_t *ppat; #endif if( from == NULL || to == NULL || text == NULL) { if(text == NULL) return NULL; ret = (unsigned char *)js_mem_alloc(strlen(text)+1); strcpy(ret,text); return ret; } while(*from == ' ' || *from == '\t') from++; #ifdef HAVE_PCRE pom = pcre_malloc(strlen(from)+1); #else /* HAVE_PCRE */ pom = mem_alloc(strlen(from)+1); #endif /* HAVE_PCRE */ if(*from != '/') { strcpy(pom, from); global = 0; } else { for( i = strlen(from)-1; i > 1 && (from[i] == ' ' || from[i] == '\t'); i--); if( from[i] == '/') { strncpy(pom, from+1, i-1); pom[i-1] = '\0'; global = 0; }else if( i > 1 && from[i] == 'g' && from[i-1] == '/') { strncpy(pom, from+1, i-2); pom[i-2] = '\0'; global = 1; }else { strncpy(pom, from, i+1); pom[i+1] = '\0'; global = 0; } } #ifdef REGEX_DEBUG printf("Search pattern is '%s', global = %d\n",pom,global); #endif /* REGEX_DEBUG */ #ifdef HAVE_PCRE pcre *ppat = pcre_compile(pom, 0/*PCRE_ANCHORED*/, &er_ptr, &erroffset, NULL); pcre_free(pom); #else /* HAVE_PCRE */ ppat = &ppat_data; if (regcomp(ppat, pom, REG_EXTENDED)) ppat = NULL; mem_free(pom); #endif /* HAVE_PCRE */ if (ppat == NULL) { if(text == NULL) return NULL; ret = (unsigned char *)js_mem_alloc(strlen(text)+1); strcpy(ret,text); return ret; } textlen = strlen(text); #ifdef HAVE_PCRE res = pcre_malloc(MAXCAPTURE+textlen); #else /* HAVE_PCRE */ res = mem_alloc(MAXCAPTURE+textlen); #endif /* HAVE_PCRE */ cp = res; ep = res+MAXCAPTURE+textlen; if(global) { do { #ifdef HAVE_PCRE nmat = pcre_exec(ppat, NULL, text, textlen, offset, 0, ovec, sizeof(ovec)/sizeof(int)); #else /* HAVE_PCRE */ if (regexec(ppat, text+offset, MAXCAPTURE/3, pmat, 0)) nmat = 0; else for( nmat = 0; nmat < MAXCAPTURE/3; nmat++ ) if((ovec[nmat<<1] = pmat[nmat].rm_so) == -1 || (ovec[(nmat<<1)+1] = pmat[nmat].rm_eo) == -1) break; #endif /* HAVE_PCRE */ #ifdef HAVE_PCRE for(i = 0; i < nmat*2; i++) ovec[i]-=offset; #endif /* HAVE_PCRE */ #ifdef REGEX_DEBUG dumpmatch(text+offset, textlen-offset, to, nmat, ovec); #endif /* REGEX_DEBUG */ if(nmat > 0) { overfl = edit(text+offset, textlen - offset, to, nmat, ovec, res); offset += ovec[1]; } } while (nmat >0 && overfl); } else { #ifdef HAVE_PCRE nmat = pcre_exec(ppat, NULL, text, textlen, 0, 0, ovec, sizeof(ovec)/sizeof(int)); #else /* HAVE_PCRE */ if (regexec(ppat, text, MAXCAPTURE/3, pmat, 0)) nmat = 0; else for( nmat = 0; nmat < MAXCAPTURE/3; nmat++ ) if((ovec[nmat<<1] = pmat[nmat].rm_so) == -1 || (ovec[(nmat<<1)+1] = pmat[nmat].rm_eo) == -1) break; #endif /* HAVE_PCRE */ #ifdef REGEX_DEBUG dumpmatch(text+offset, textlen-offset, to, nmat, ovec); #endif /* REGEX_DEBUG */ if(nmat > 0) { overfl = edit(text+offset, textlen - offset, to, nmat, ovec, res); offset += ovec[1]; } } if ( textlen >= offset && cp + textlen - offset < ep) { strncpy(cp, text+offset, textlen - offset); *(cp +textlen - offset) = '\0'; } else *(ep-1) = '\0'; ret = (unsigned char *)js_mem_alloc(strlen(res)+1); strcpy(ret,res); #ifdef HAVE_PCRE pcre_free(res); pcre_free(ppat); #else /* HAVE_PCRE */ mem_free(res); regfree(ppat); #endif /* HAVE_PCRE */ return ret; }