Simplify associations and handle README files

This commit is contained in:
lostd 2014-10-07 14:32:03 +00:00 committed by lostd
parent 4384f09726
commit a543eed112

55
noice.c
View file

@ -50,36 +50,34 @@ struct assoc {
char *ext; /* Extension */ char *ext; /* Extension */
char *bin; /* Program */ char *bin; /* Program */
} assocs[] = { } assocs[] = {
{ "avi", "mplayer" }, { ".avi", "mplayer" },
{ "mp4", "mplayer" }, { ".mp4", "mplayer" },
{ "mkv", "mplayer" }, { ".mkv", "mplayer" },
{ "mp3", "mplayer" }, { ".mp3", "mplayer" },
{ "ogg", "mplayer" }, { ".ogg", "mplayer" },
{ "srt", "less" }, { ".srt", "less" },
{ "txt", "less" }, { ".txt", "less" },
{ "README", "less" },
}; };
char * char *
extension(char *file) openwith(char *file)
{
char *dot;
dot = strrchr(file, '.');
if (dot == NULL || dot == file)
return NULL;
else
return dot + 1;
}
char *
openwith(char *ext)
{ {
char *ext = NULL;
char *bin = NULL;
int i; int i;
ext = strrchr(file, '.');
if (ext == NULL)
ext = file;
DPRINTF_S(ext);
for (i = 0; i < LEN(assocs); i++) for (i = 0; i < LEN(assocs); i++)
if (strncmp(assocs[i].ext, ext, strlen(ext)) == 0) if (strncmp(assocs[i].ext, ext, strlen(ext) + 1) == 0)
return assocs[i].bin; bin = assocs[i].bin;
return NULL; DPRINTF_S(bin);
return bin;
} }
int int
@ -327,7 +325,7 @@ nochange:
if (ret == 3) { if (ret == 3) {
char *name, *file = NULL; char *name, *file = NULL;
char *newpath; char *newpath;
char *ext, *bin; char *bin;
pid_t pid; pid_t pid;
/* Cannot descend in empty directories */ /* Cannot descend in empty directories */
@ -357,18 +355,11 @@ nochange:
DPRINTF_S(file); DPRINTF_S(file);
/* Open with */ /* Open with */
ext = extension(name); bin = openwith(name);
if (ext == NULL) {
printwarn("invalid extension\n");
goto nochange;
}
bin = openwith(ext);
if (bin == NULL) { if (bin == NULL) {
printwarn("no association\n"); printwarn("no association\n");
goto nochange; goto nochange;
} }
DPRINTF_S(ext);
DPRINTF_S(bin);
exitcurses(); exitcurses();