aboutsummaryrefslogtreecommitdiffstats
path: root/noice.c
diff options
context:
space:
mode:
authorsin <sin@2f30.org>2014-10-09 10:33:49 +0100
committerlostd <lostd@2f30.org>2014-10-09 13:40:40 +0300
commit46669d7606e7e763f63aa7b2533291a3699d5911 (patch)
treeeffc4237e6c81cdc5d72495b85b3574a535b54b8 /noice.c
parent368b43572d3017542b2c2f056b196a4f7c79820f (diff)
downloadnoice-46669d7606e7e763f63aa7b2533291a3699d5911.tar.gz
Add regex support
Diffstat (limited to 'noice.c')
-rw-r--r--noice.c36
1 files changed, 17 insertions, 19 deletions
diff --git a/noice.c b/noice.c
index 66d856b..60dbfbd 100644
--- a/noice.c
+++ b/noice.c
@@ -7,6 +7,7 @@
#include <curses.h>
#include <libgen.h>
#include <locale.h>
+#include <regex.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
@@ -35,21 +36,17 @@
#define ISODD(x) ((x) & 1)
struct assoc {
- char *ext; /* Extension */
- char *bin; /* Program */
+ char *regex; /* Regex to match on filename */
+ char *bin; /* Program */
};
/* Configuration */
struct assoc assocs[] = {
- { ".avi", "mplayer" },
- { ".mp4", "mplayer" },
- { ".mkv", "mplayer" },
- { ".mp3", "mplayer" },
- { ".ogg", "mplayer" },
- { ".srt", "less" },
- { ".txt", "less" },
- { ".sh", "sh" },
- { "README", "less" },
+ { ".(avi|mp4|mkv|mp3|ogg)$", "mplayer" },
+ { ".srt$", "less" },
+ { ".txt$", "less" },
+ { ".sh$", "sh" },
+ { "^README$", "less" },
};
#define CWD "cwd: "
@@ -78,18 +75,19 @@ int die = 0;
char *
openwith(char *file)
{
- char *ext = NULL;
+ regex_t regex;
char *bin = NULL;
int i;
- ext = strrchr(file, '.');
- if (ext == NULL)
- ext = file;
- DPRINTF_S(ext);
-
- for (i = 0; i < LEN(assocs); i++)
- if (strcmp(assocs[i].ext, ext) == 0)
+ for (i = 0; i < LEN(assocs); i++) {
+ if (regcomp(&regex, assocs[i].regex,
+ REG_NOSUB | REG_EXTENDED) != 0)
+ continue;
+ if (regexec(&regex, file, 0, NULL, 0) != REG_NOMATCH) {
bin = assocs[i].bin;
+ break;
+ }
+ }
DPRINTF_S(bin);
return bin;
Un proyecto texto-plano.xyz