sopen

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

commit b28d45766b30ce194cc918d1d5b8994f039de1bb
parent 82ec5b5cdb2a59c53f22be40318c4574136813ef
Author: Pollux <pollux@pollux.codes>
Date:   Tue, 11 Feb 2025 00:36:24 -0600

style: Refactor code

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

Diffstat:
Msopen.c | 170++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 90 insertions(+), 80 deletions(-)

diff --git a/sopen.c b/sopen.c @@ -21,9 +21,87 @@ typedef struct { const char *program; } Rule; +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" 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) { + + regex_t regex; + int res; + + res = regcomp(&regex, rule.url_regex, REG_EXTENDED); + if(res > 0) { + printf("Failed to compile url regex: %s\n", rule.url_regex); + return 1; + } + res = regexec(&regex, url, 0, NULL, 0); + 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); + return 1; + } + res = regexec(&regex, mime_type, 0, NULL, 0); + if(res > 0) + return 1; + + return 0; +} + +int main(int argc, const char **argv) { int res; @@ -31,22 +109,14 @@ main(int argc, const char **argv) { magic_t cookie; regex_t has_protocol; - regex_t regex_url; - - regex_t protocol_regex; - regex_t url_regex; - regex_t mime_regex; - regex_t file_regex; - - regmatch_t match[8]; const char *url; const char *mime_type; + char protocol[16]; + char file[1024]; char *child_args[3]; - char fragment[128]; - cookie = magic_open(MAGIC_MIME_TYPE); if(cookie == NULL) { printf("Failed to open magic cookie.\n"); @@ -60,10 +130,6 @@ main(int argc, const char **argv) { if(res > 0) goto error; - res = regcomp(&regex_url, REGEX_URL, REG_EXTENDED); - if(res > 0) - goto error; - mime_type = ""; res = regexec(&has_protocol, url, 0, NULL, 0); @@ -72,78 +138,22 @@ main(int argc, const char **argv) { for(int n = 0; n < sizeof(rules) / sizeof(Rule); n++) { - res = regcomp(&protocol_regex, rules[n].protocol_regex, - REG_EXTENDED); - if(res > 0) { - printf("Failed to compile protocol regex: %s\n", - rules[n].protocol_regex); - goto error; - } - res = regcomp(&url_regex, rules[n].url_regex, REG_EXTENDED); - if(res > 0) { - printf("Failed to compile url regex: %s\n", - rules[n].url_regex); - goto error; - } - res = regcomp(&mime_regex, rules[n].mime_regex, REG_EXTENDED); - if(res > 0) { - printf("Failed to compile mime regex: %s\n", - rules[n].mime_regex); - goto error; - } - res = regcomp(&file_regex, rules[n].file_regex, REG_EXTENDED); - if(res > 0) { - printf("Failed to compile file regex: %s\n", - rules[n].file_regex); - goto error; - } - - res = regexec(&regex_url, url, regex_url.re_nsub + 1, match, 0); - if(res > 0) { - printf("Wat..."); - goto error; - } - - memcpy(fragment, &url[match[2].rm_so], - match[2].rm_eo - match[2].rm_so); - fragment[match[2].rm_eo - match[2].rm_so] = '\0'; - - res = regexec(&protocol_regex, fragment, 0, NULL, 0); - if(res > 0) { - continue; - } - - res = regexec(&url_regex, url, 0, NULL, 0); - if(res > 0) { - continue; - } - - res = regexec(&mime_regex, mime_type, 0, NULL, 0); - if(res > 0) { + res = parse_url(url, &protocol[0], &file[0]); + if(res > 0) continue; - } - memcpy(fragment, &url[match[4].rm_so], - match[4].rm_eo - match[4].rm_so); - fragment[match[4].rm_eo - match[4].rm_so] = '\0'; + res = match_rule(rules[n], url, protocol, file, mime_type); - res = regexec(&file_regex, fragment, 0, NULL, 0); - if(res > 0) { - continue; + if(res == 0) { + child_args[0] = (char *)rules[n].program; + child_args[1] = (char *)url; + child_args[2] = NULL; + execvp(rules[n].program, child_args); + return 0; } - - printf("Match: %s\n", rules[n].program); - - child_args[0] = (char *)rules[n].program; - child_args[1] = (char *)url; - child_args[2] = NULL; - - execvp(rules[n].program, child_args); - - exit(0); } error: magic_close(cookie); - return 0; + return 1; }