#include <readline/history.h>
#include <readline/readline.h>
#endif
+#ifdef PCRE
+#include <pcre.h>
+#else
#include <regex.h>
+#endif
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
} kv;
typedef struct {
+#ifdef PCRE
+ const pcre *pcrex;
+#else
const regex_t *regex;
+#endif
const char *str;
} fltrexp_t;
static kv plug[PLUGIN_MAX];
static uchar g_tmpfplen;
static uchar blk_shift = BLK_SHIFT_512;
+#ifdef PCRE
+static pcre *archive_pcre;
+#else
static regex_t archive_re;
+#endif
/* Retain old signal handlers */
#ifdef __linux__
}
static char * (*fnstrstr)(const char *haystack, const char *needle) = &strcasestr;
+#ifdef PCRE
+static const unsigned char *tables;
+static int pcreflags = PCRE_NO_AUTO_CAPTURE | PCRE_EXTENDED | PCRE_CASELESS;
+#else
static int regflags = REG_NOSUB | REG_EXTENDED | REG_ICASE;
+#endif
+
+#ifdef PCRE
+static int setfilter(pcre **pcrex, const char *filter)
+{
+ const char *errstr = NULL;
+ int erroffset = 0;
+ *pcrex = pcre_compile(filter, pcreflags, &errstr, &erroffset, tables);
+
+ return errstr ? -1 : 0;
+}
+#else
static int setfilter(regex_t *regex, const char *filter)
{
return regcomp(regex, filter, regflags);
}
+#endif
static int visible_re(const fltrexp_t *fltrexp, const char *fname)
{
+#ifdef PCRE
+ return pcre_exec(fltrexp->pcrex, NULL, fname, strlen(fname), 0, 0, NULL, 0);
+#else
return regexec(fltrexp->regex, fname, 0, NULL, 0) == 0;
+#endif
}
static int visible_str(const fltrexp_t *fltrexp, const char *fname)
*pdent2 = *(&_dent);
}
-/*
- * Move non-matching entries to the end
- */
+#ifdef PCRE
+static int fill(const char *fltr, pcre *pcrex)
+#else
static int fill(const char *fltr, regex_t *re)
+#endif
{
int count = 0;
+#ifdef PCRE
+ fltrexp_t fltrexp = { .pcrex = pcrex, .str = fltr };
+#else
fltrexp_t fltrexp = { .regex = re, .str = fltr };
+#endif
for (; count < ndents; ++count) {
if (filterfn(&fltrexp, dents[count].name) == 0) {
static int matches(const char *fltr)
{
+#ifdef PCRE
+ pcre *pcrex = NULL;
+
+ /* Search filter */
+ if (cfg.regex && setfilter(&pcrex, fltr))
+ return -1;
+
+ ndents = fill(fltr, pcrex);
+
+ if (cfg.regex)
+ pcre_free(pcrex);
+#else
regex_t re;
/* Search filter */
- if (cfg.regex && setfilter(&re, fltr) != 0)
+ if (cfg.regex && setfilter(&re, fltr))
return -1;
ndents = fill(fltr, &re);
+
if (cfg.regex)
regfree(&re);
+#endif
qsort(dents, ndents, sizeof(*dents), entrycmpfn);
/* Toggle case-sensitivity */
if (*ch == CASE) {
fnstrstr = (fnstrstr == &strcasestr) ? &strstr : &strcasestr;
+#ifdef PCRE
+ pcreflags ^= PCRE_CASELESS;
+#else
regflags ^= REG_ICASE;
+#endif
showfilter(ln);
continue;
}
goto nochange;
}
+#ifdef PCRE
+ if (!pcre_exec(archive_pcre, NULL, dents[cur].name,
+ strlen(dents[cur].name), 0, 0, NULL, 0)) {
+#else
if (!regexec(&archive_re, dents[cur].name, 0, NULL, 0)) {
+#endif
r = get_input(messages[MSG_ARCHIVE_OPTS]);
if (r == 'l' || r == 'x') {
mkpath(path, dents[cur].name, newpath);
/* Set archive handling (enveditor used as tmp var) */
enveditor = getenv(env_cfg[NNN_ARCHIVE]);
+#ifdef PCRE
+ if (setfilter(&archive_pcre, (enveditor ? enveditor : archive_regex))) {
+#else
if (setfilter(&archive_re, (enveditor ? enveditor : archive_regex))) {
+#endif
fprintf(stderr, "%s\n", messages[MSG_INVALID_REG]);
return _FAILURE;
}
#ifndef NOLOCALE
/* Set locale */
setlocale(LC_ALL, "");
+#ifdef PCRE
+ tables = pcre_maketables();
+#endif
#endif
#ifndef NORL
unlink(g_selpath);
/* Free the regex */
+#ifdef PCRE
+ pcre_free(archive_pcre);
+#else
regfree(&archive_re);
+#endif
/* Free the selection buffer */
free(pselbuf);