Add regex support

This commit is contained in:
sin 2014-10-09 10:33:49 +01:00 committed by lostd
parent 368b43572d
commit 46669d7606

34
noice.c
View file

@ -7,6 +7,7 @@
#include <curses.h> #include <curses.h>
#include <libgen.h> #include <libgen.h>
#include <locale.h> #include <locale.h>
#include <regex.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
@ -35,21 +36,17 @@
#define ISODD(x) ((x) & 1) #define ISODD(x) ((x) & 1)
struct assoc { struct assoc {
char *ext; /* Extension */ char *regex; /* Regex to match on filename */
char *bin; /* Program */ char *bin; /* Program */
}; };
/* Configuration */ /* Configuration */
struct assoc assocs[] = { struct assoc assocs[] = {
{ ".avi", "mplayer" }, { ".(avi|mp4|mkv|mp3|ogg)$", "mplayer" },
{ ".mp4", "mplayer" }, { ".srt$", "less" },
{ ".mkv", "mplayer" }, { ".txt$", "less" },
{ ".mp3", "mplayer" }, { ".sh$", "sh" },
{ ".ogg", "mplayer" }, { "^README$", "less" },
{ ".srt", "less" },
{ ".txt", "less" },
{ ".sh", "sh" },
{ "README", "less" },
}; };
#define CWD "cwd: " #define CWD "cwd: "
@ -78,18 +75,19 @@ int die = 0;
char * char *
openwith(char *file) openwith(char *file)
{ {
char *ext = NULL; regex_t regex;
char *bin = NULL; char *bin = NULL;
int i; int i;
ext = strrchr(file, '.'); for (i = 0; i < LEN(assocs); i++) {
if (ext == NULL) if (regcomp(&regex, assocs[i].regex,
ext = file; REG_NOSUB | REG_EXTENDED) != 0)
DPRINTF_S(ext); continue;
if (regexec(&regex, file, 0, NULL, 0) != REG_NOMATCH) {
for (i = 0; i < LEN(assocs); i++)
if (strcmp(assocs[i].ext, ext) == 0)
bin = assocs[i].bin; bin = assocs[i].bin;
break;
}
}
DPRINTF_S(bin); DPRINTF_S(bin);
return bin; return bin;