sfm

simple file manager
git clone git://git.afify.dev/sfm
Log | Files | Refs | README | LICENSE

commit 8fea79ffaed7e09f0c6e7663c2b37fafae615677
parent e86127b62bf7685b52ca5a45452f0103a503544d
Author: afify <hassan@afify.dev>
Date:   Wed,  1 Jul 2020 19:58:42 +0300

[feat] openfile by extentions

- add Rule struct hold extentions and software to open with
- set default softwares if all failed
- add tb_shutdown() to clean buffer
- press() remove uneeded vars

Diffstat:
Mconfig.def.h | 23++++++++++++++++++-----
Msfm.c | 70++++++++++++++++++++++++++++++++++------------------------------------
2 files changed, 52 insertions(+), 41 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -29,11 +29,24 @@ static Bookmark bmarks[] = { }; /* openwith */ -static char audio[] = "mpv"; -static char html[] = "firefox"; -static char images[] = "sxiv"; -static char pdf[] = "mupdf"; -static char videos[] = "mpv"; +static const char *images[] = { "bmp", "jpg", "jpeg", "png", "gif", "xpm" }; +static const char *web[] = { "htm", "html" }; +static const char *pdf[] = { "epub", "pdf" }; +static const char *videos[] = { "ac3", "avi", "divx", "flc", "fli", "flv", + "m2v", "m4a", "m4v", "mkv", "mov", "mp3", "mp4", "mpeg", "mpg", + "mts", "opus", "qt", "ra", "rm", "ts", "wav", "webm", "wma", "wmv" }; +static const char *documents[] = { "odt", "doc", "docx", "xls", "xlsx", "odp", + "ods", "pptx", "odg" }; +static const char *arts[] = { "xcf" }; + +static Rule rules[] = { + {"mpv", videos, LEN(videos) }, + {"sxiv", images, LEN(images) }, + {"firefox", web, LEN(web) }, + {"mupdf", pdf, LEN(pdf) }, + {"libreoffice", documents, LEN(documents) }, + {"gimp", arts, LEN(arts) }, +}; /* unicode chars */ static const uint32_t u_cne = 0x2510; diff --git a/sfm.c b/sfm.c @@ -47,6 +47,12 @@ typedef struct { char path[MAX_P]; } Bookmark; +typedef struct { + char *soft; + const char **ext; + size_t len; +} Rule; + #include "config.h" /* function declarations */ @@ -446,44 +452,41 @@ check_dir(char *path) static int open_files(char *filename) { - tb_clear(); // TODO /* open editor in other window */ /* wait option */ char *editor, *file_ex, *software, *term; - int status, r; - pid_t pid; - int wait_finish = 0; + int status; + size_t d, c; + pid_t pid, r; editor = getenv("EDITOR"); term = getenv("TERM"); - - if (term == NULL) - term = "xterm-256color"; - - if (editor == NULL) - editor = "vi"; - file_ex = get_extentions(filename); + software = NULL; - if (strcmp(file_ex, "png") == 0) { - software = images; - wait_finish = 1; - } else if (strcmp(file_ex, "mp4") == 0) { - software = videos; - wait_finish = 1; - } else if (strcmp(file_ex, "pdf") == 0) { - software = pdf; - } else { - software = editor; - wait_finish = 1; + /* find software in rules */ + for (c = 0; c < LEN(rules); c++) { + for (d = 0; d < rules[c].len; d++){ + if (strcmp(rules[c].ext[d], file_ex) == 0) { + software = rules[c].soft; + } + } } - free(file_ex); + /* default softwares */ + if (term == NULL) + term = "xterm-256color"; + if (editor == NULL) + editor = "vi"; + if (software == NULL) + software = editor; + free(file_ex); char *filex[] = {software, filename, NULL}; - + tb_shutdown(); pid = fork(); + switch (pid) { case -1: return -1; @@ -491,14 +494,12 @@ open_files(char *filename) (void)execvp(filex[0], filex); exit(EXIT_SUCCESS); default: - if (wait_finish == 1) { - while ((r = (int)waitpid(pid, &status, 0)) == -1 && errno == EINTR) + while ((r = waitpid(pid, &status, 0)) == -1 && errno == EINTR) continue; if (r == -1) return -1; if ((WIFEXITED(status) != 0) && (WEXITSTATUS(status) != 0)) return -1; - } } return 0; @@ -758,7 +759,7 @@ static void press(struct tb_event *ev, Pane *cpane, Pane *opane) { char *parent; - int isdir, ret, b; + int b; clear_error(); if (ev->ch == 'j') { @@ -784,8 +785,7 @@ press(struct tb_event *ev, Pane *cpane, Pane *opane) } free(parent); } else if (ev->ch == 'l') { - isdir = check_dir(cpane->high_dir); - switch (isdir) { + switch (check_dir(cpane->high_dir)) { case 0: strcpy(cpane->dirn, cpane->high_dir); clear_pane(cpane->dirx); @@ -796,17 +796,15 @@ press(struct tb_event *ev, Pane *cpane, Pane *opane) case 1: /* is not a directory open file */ if (open_files(cpane->high_dir) < 0) { - print_error("%s", strerror(errno)); + print_error("procces failed"); + return; } - tb_shutdown(); - ret = tb_init(); - if (ret != 0) - die("tb_init() %d\n", ret); + if (tb_init() != 0) + die("tb_init"); if (cpane->dirx == 2) /* if current left pane */ t_resize(cpane, opane); else t_resize(opane, cpane); - break; case -1: /* failed to open directory */