sopen

Suckless mimeopen alternative
git clone git://pollux.codes/git/sopen.git
Log | Files | Refs | README | LICENSE

commit c7c4484517ffc696d694bd656043d6b163039066
parent cb7b7fe47e882187db766280e3ed6b56a660d221
Author: Pollux <pollux@pollux.codes>
Date:   Wed, 12 Feb 2025 12:17:32 -0600

feat!: Combine protocol, url, and path regexes

Signed-off-by: Pollux <pollux@pollux.codes>

Diffstat:
Mconfig.def.h | 17++++++++---------
Msopen.c | 61+++----------------------------------------------------------
2 files changed, 11 insertions(+), 67 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -2,23 +2,22 @@ static const Rule rules[] = { /* Format: - * protocol regex, url regex, mime type regex, - * file path regex, program */ + * url regex, mime type regex, program */ /* Documents */ - {"", "", "application/pdf", "", "zathura"}, + {"", "application/pdf", "zathura"}, /* Images */ - {"", "", "^image", "", "imv"}, + {"", "^image", "imv"}, /* Audio/Video */ - {"", "", "^(audio|video)", "", "mpv"}, - {"^http", "youtube.com", "", "", "mpv"}, - {"^http", "", "", "\\.mp3$", "mpv"}, + {"", "^(audio|video)", "mpv"}, + {"^https?://youtube.com", "", "mpv"}, + {"^http.*\\.mp3$", "", "mpv"}, /* Text */ - {"", "", "^text", "", "nvim"}, + {"", "^text", "nvim"}, /* Web */ - {"^http", "", "", "", "firefox"}, + {"^http", "", "firefox"}, }; diff --git a/sopen.c b/sopen.c @@ -12,18 +12,14 @@ #define _POSIX_C_SOURCE 200809L #define REGEX_PROTOCOL "^[a-z]*://" -#define REGEX_URL "^(([a-z]*)://(.*)/)?(.*)$" - #define USAGE "usage: sopen [-h] [-v] [-d] <url/file>" #define STR_LITERAL(x) #x #define STRINGIFY(x) STR_LITERAL(x) typedef struct { - const char *protocol_regex; const char *url_regex; const char *mime_regex; - const char *file_regex; const char *program; } Rule; @@ -31,9 +27,7 @@ void print_help(); void print_version(); void parse_args(const int argc, char *const *argv, int *dry_run, int *url_ind); -int parse_url(const char *url, char *protocol, char *file); int match_rule(const Rule rule, const char *url, - const char *protocol, const char *file, const char *mime_type); #include "config.h" @@ -83,34 +77,7 @@ parse_args(const int argc, char *const *argv, int *dry_run, int *url_ind) { } int -parse_url(const char *url, char *protocol, char *file) { - - regex_t regex_url; - regmatch_t match[8]; - int res; - - res = regcomp(&regex_url, REGEX_URL, REG_EXTENDED); - if(res > 0) - return 1; - - res = regexec(&regex_url, url, regex_url.re_nsub + 1, match, 0); - if(res > 0) { - printf("Failed to parse url: %s\n", url); - return 1; - } - - memcpy(protocol, url + match[2].rm_so, match[2].rm_eo - match[2].rm_so); - protocol[match[2].rm_eo - match[2].rm_so] = '\0'; - - memcpy(file, url + match[4].rm_so, match[4].rm_eo - match[4].rm_so); - file[match[4].rm_eo - match[4].rm_so] = '\0'; - - return 0; -} - -int -match_rule(const Rule rule, const char *url, const char *protocol, - const char *file, const char *mime_type) { +match_rule(const Rule rule, const char *url, const char *mime_type) { regex_t regex; int res; @@ -126,25 +93,6 @@ match_rule(const Rule rule, const char *url, const char *protocol, if(res > 0) return 1; - res = regcomp(&regex, rule.protocol_regex, REG_EXTENDED); - if(res > 0) { - printf("Failed to compile protocol regex: %s\n", - rule.protocol_regex); - return 1; - } - res = regexec(&regex, protocol, 0, NULL, 0); - if(res > 0) - return 1; - - res = regcomp(&regex, rule.file_regex, REG_EXTENDED); - if(res > 0) { - printf("Failed to compile file regex: %s\n", rule.file_regex); - return 1; - } - res = regexec(&regex, file, 0, NULL, 0); - if(res > 0) - return 1; - res = regcomp(&regex, rule.mime_regex, REG_EXTENDED); if(res > 0) { printf("Failed to compile mime regex: %s\n", rule.mime_regex); @@ -183,6 +131,7 @@ main(int argc, char *const *argv) { } magic_load(cookie, NULL); + dry_run = 0; parse_args(argc, argv, &dry_run, &url_ind); url = argv[url_ind]; @@ -198,11 +147,7 @@ main(int argc, char *const *argv) { for(int n = 0; n < sizeof(rules) / sizeof(Rule); n++) { - res = parse_url(url, &protocol[0], &file[0]); - if(res > 0) - continue; - - res = match_rule(rules[n], url, protocol, file, mime_type); + res = match_rule(rules[n], url, mime_type); if(res == 0) {