aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2019-03-31 13:39:04 +0200
committersin <sin@2f30.org>2019-03-31 12:43:18 +0100
commit05b99639cceb8d40d8d81c6d42993c273a5040cf (patch)
tree0b116a4b8d6136fedc470b818269bf74c65f7bd3
parent36de02e0658d7ddbf7d63879c812864d984e66a9 (diff)
downloadnoice-05b99639cceb8d40d8d81c6d42993c273a5040cf.tar.gz
compile all regexes at startup and show an error on failed compilation
-rw-r--r--noice.c37
1 files changed, 25 insertions, 12 deletions
diff --git a/noice.c b/noice.c
index eabb073..86c828c 100644
--- a/noice.c
+++ b/noice.c
@@ -45,6 +45,7 @@ struct assoc {
char *regex; /* Regex to match on filename */
char *file;
char *argv[NR_ARGS];
+ regex_t regcomp;
};
struct cpair {
@@ -246,23 +247,16 @@ xgetenv(char *name, char *fallback)
struct assoc *
openwith(char *file)
{
- regex_t regex;
- struct assoc *assoc = NULL;
int i;
for (i = 0; i < LEN(assocs); i++) {
- if (regcomp(&regex, assocs[i].regex,
- REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
- continue;
- if (regexec(&regex, file, 0, NULL, 0) == 0) {
- assoc = &assocs[i];
- regfree(&regex);
- break;
+ if (regexec(&assocs[i].regcomp, file, 0, NULL, 0) == 0) {
+ DPRINTF_S(assocs[i].argv[0]);
+ return &assocs[i];
}
- regfree(&regex);
}
- DPRINTF_S(assoc->argv[0]);
- return assoc;
+
+ return NULL;
}
int
@@ -911,6 +905,24 @@ usage(char *argv0)
exit(1);
}
+void
+initassocs(void)
+{
+ char errbuf[256];
+ int i, r;
+
+ for (i = 0; i < LEN(assocs); i++) {
+ r = regcomp(&assocs[i].regcomp, assocs[i].regex,
+ REG_NOSUB | REG_EXTENDED | REG_ICASE);
+ if (r != 0) {
+ regerror(r, &assocs[i].regcomp, errbuf, sizeof(errbuf));
+ fprintf(stderr, "invalid regex assocs[%d]: %s: %s\n",
+ i, assocs[i].regex, errbuf);
+ exit(1);
+ }
+ }
+}
+
int
main(int argc, char *argv[])
{
@@ -948,6 +960,7 @@ main(int argc, char *argv[])
/* Set locale before curses setup */
setlocale(LC_ALL, "");
+ initassocs();
initcurses();
browse(ipath, ifilter);
exitcurses();
Un proyecto texto-plano.xyz