sfm

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

commit 108170547b215e1883673c3e43eaf952afd25211
parent 3305c44909d940566a1e2c018b36a54131d0c629
Author: afify <hassan@afify.dev>
Date:   Mon, 12 Oct 2020 07:49:15 +0300

[feat] spawn with args

- pass to software arguments in config file, must be NULL terminated.

Diffstat:
Mconfig.def.h | 57+++++++++++++++++++++++++++++++++++----------------------
Msfm.c | 80+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
2 files changed, 83 insertions(+), 54 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -23,23 +23,36 @@ static Bookmark bmarks[] = { {'\\', "/root"}, }; -/* openwith */ -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 *arts[] = { "xcf" }; -static const char *videos[] = { "avi", "flv", "wav", "webm", "wma", "wmv", - "m2v", "m4a", "m4v", "mkv", "mov", "mp3", "mp4", "mpeg", "mpg" }; +/* software */ +static const char *mpv[] = { "mpv", "--fullscreen", NULL }; +static const char *sxiv[] = { "sxiv", NULL }; +static const char *surf[] = { "surf", NULL }; +static const char *mupdf[] = { "mupdf", "-I", NULL }; +static const char *libreoffice[] = { "libreoffice", NULL }; +static const char *gimp[] = { "gimp", NULL }; +static const char *r2[] = { "r2", "-c", "vv", NULL }; +static const char *binary[] = { "r2", "-d", "-c", "vv", NULL }; + +/* extension */ +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 *arts[] = { "xcf" }; +static const char *obj[] = { "o", "a", "so" }; +static const char *videos[] = { "avi", "flv", "wav", "webm", "wma", "wmv", + "m2v", "m4a", "m4v", "mkv", "mov", "mp3", + "mp4", "mpeg", "mpg" }; static const char *documents[] = { "odt", "doc", "docx", "xls", "xlsx", "odp", - "ods", "pptx", "odg" }; + "ods", "pptx", "odg" }; 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) }, + {videos, LEN(videos), mpv}, + {images, LEN(images), sxiv}, + {web, LEN(web), surf}, + {pdf, LEN(pdf), mupdf}, + {documents, LEN(documents), libreoffice}, + {arts, LEN(arts), gimp}, + {obj, LEN(obj), r2}, }; static Key keys[] = { @@ -76,13 +89,13 @@ static const int show_size = 1; static const char dtfmt[] = "%F %R"; /* date time format */ /* unicode chars */ -static const uint32_t u_cne = 0x2510; -static const uint32_t u_cnw = 0x250C; -static const uint32_t u_cse = 0x2518; -static const uint32_t u_csw = 0x2514; -static const uint32_t u_hl = 0x2500; -static const uint32_t u_mn = 0x252C; -static const uint32_t u_ms = 0x2534; -static const uint32_t u_vl = 0x2502; +#define u_hl 0x2500 /* ─ */ +#define u_vl 0x2502 /* │ */ +#define u_cnw 0x250C /* ┌ */ +#define u_cne 0x2510 /* ┐ */ +#define u_csw 0x2514 /* └ */ +#define u_cse 0x2518 /* ┘ */ +#define u_mn 0x252C /* ┬ */ +#define u_ms 0x2534 /* ┴ */ #endif /* CONFIG_H */ diff --git a/sfm.c b/sfm.c @@ -32,6 +32,7 @@ #define MAX_P 4095 #define MAX_N 255 #define MAX_USRI 32 +#define MAX_EXT 4 /* enums */ enum { AskDel, DAskDel }; /* delete directory */ @@ -72,9 +73,9 @@ typedef struct { } Bookmark; typedef struct { - char *soft; const char **ext; - size_t len; + size_t exlen; + const void *v; } Rule; typedef union { @@ -136,8 +137,8 @@ static void scrdwns(void); static void scrup(void); static void scrups(void); static int get_usrinput(char *, size_t, char *); -static char *frules(char *); -static char *getsw(char *); +static int frules(char *); +static int spawn(const void *, char *); static int opnf(char *); static int fsev_init(void); static int addwatch(void); @@ -1207,50 +1208,44 @@ get_usrinput(char *out, size_t sout, char *prompt) return -1; } -static char * +static int frules(char *ex) { size_t c, d; for (c = 0; c < LEN(rules); c++) - for (d = 0; d < rules[c].len; d++) - if (strcmp(rules[c].ext[d], ex) == 0) - return rules[c].soft; - return NULL; -} - -static char * -getsw(char *fn) -{ - char *ed, *ex, *sw; - - ed = getenv("EDITOR"); - if (ed == NULL) - ed = fed; - ex = get_ext(fn); - sw = frules(ex); - if (sw == NULL) - sw = ed; - free(ex); - return sw; + for (d = 0; d < rules[c].exlen; d++) + if (strncmp(rules[c].ext[d], ex, MAX_EXT) == 0) + return c; + return -1; } static int -opnf(char *fn) +spawn(const void *v, char *fn) { - char *sw; - int ws; + int ws, x, argc; pid_t pid, r; - sw = getsw(fn); - char *filex[] = { sw, fn, NULL }; - pid = fork(); + x = 0; + argc = 0; + + /* count args */ + while (((char **)v)[x++] != NULL) + argc++; + + char *argv[argc + 2]; + for ( x = 0; x < argc; x++) + argv[x] = ((char **)v)[x]; + argv[argc] = fn; + argv[argc + 1] = NULL; + + pid = fork(); switch (pid) { case -1: return -1; case 0: - execvp(filex[0], filex); + execvp(argv[0], argv); exit(EXIT_SUCCESS); default: while ((r = waitpid(pid, &ws, 0)) == -1 && errno == EINTR) @@ -1264,6 +1259,27 @@ opnf(char *fn) } static int +opnf(char *fn) +{ + char *ed[2], *ex; + int c; + + ex = get_ext(fn); + c = frules(ex); + free(ex); + + if (c < 0) { /* extension not found open in editor */ + ed[0] = getenv("EDITOR"); + ed[1] = NULL; + if (ed[0] == NULL) + ed[0] = fed; + return spawn(ed, fn); + } else { + return spawn((char **)rules[c].v, fn); + } +} + +static int fsev_init(void) { #if defined _SYS_INOTIFY_H