From 393864c80fca3604987b56b7a32b463ae927e918 Mon Sep 17 00:00:00 2001 From: nova Date: Fri, 12 Dec 2025 20:28:39 +0100 Subject: [PATCH] better string handling --- backend.c | 28 ++++++++++++++++++++++ backend.h | 2 ++ config.h | 10 ++++---- interactions.c | 65 ++++++++++++++++++++++---------------------------- 4 files changed, 65 insertions(+), 40 deletions(-) diff --git a/backend.c b/backend.c index 6a1d33d..846061f 100644 --- a/backend.c +++ b/backend.c @@ -1,5 +1,7 @@ #include +#include #include +#include "config.h" #define concat(out, s1, s2, _free) { \ concat## _free(out, s1, s2); \ @@ -78,3 +80,29 @@ char* smartstrcasestr(const char *haystack, const char *needle){ return ret; } +char* parse_cmd_char(const char *cmd, const char *path){ + char *index = strstr(cmd, SETTINGS_COMMAND_REPLACE_STR); + char *out; + + if (index) { + out = malloc(strlen(cmd) + 1 + strlen(path) + 1); + char *o = out; + memcpy(out, cmd, index - cmd); + o += index-cmd; + *o = '\"'; + o++; + memcpy(o, path, strlen(path)); + o += strlen(path); + *o = '\"'; + memcpy(o+1, index + 1, strlen(index+1)); + *(o+strlen(index+1)+1) = '\0'; + return out; + } else { + concat(out, cmd, " ./\"", 0); + concat(out, out, path, 1); + concat(out, out, "\"", 1); + } + + return out; + +} diff --git a/backend.h b/backend.h index 29e0fb0..eb8d5e9 100644 --- a/backend.h +++ b/backend.h @@ -7,3 +7,5 @@ /*char* concat(const char *s1, const char *s2);*/ char* smartstrcasestr(const char *haystack, const char *needle); +char* parse_cmd_char(const char *cmd, const char *path); + diff --git a/config.h b/config.h index 60dec6c..8dd1c4e 100644 --- a/config.h +++ b/config.h @@ -2,6 +2,8 @@ #define SETTINGS_UEBERZUG_IMAGE_PREVIEW 1 /* 0 is disabled, 1 is enabled, 2 is with caching; depends on ueberzug */ #define SETTINGS_RELOAD_DIR_DELTA 10 /* 0 is disabled, time in seconds between automatic refresh of dir contents */ #define SETTINGS_CURSES_TIMEOUT 100 /* read: inopts(3NCURSES), blocking time between user inputs */ +#define SETTINGS_COMMAND_REPLACE_STR "%" /* if a command in any cmd inside this fle contains this string, it will be replaced with the hovered/selected file name + * as of right now, this character cannot be escaped; i.e ``command\% --arg`` will parse into ``command\path_of_hovered_file --arg``*/ /* {{{ */ #ifndef CONFIG_GUARD @@ -21,8 +23,8 @@ static const mimetype mimetype_default_cmd[] = { * we need to define "gif" before "image" */ { "text", "$EDITOR" }, { "gif", "mpv --loop-file=\"inf\"" }, - { "image", "feh" }, - { "video", "mpv" }, + { "image", "feh % &" }, + { "video", "mpv % &" }, { "audio", "mpv" }, { "pdf", "zathura" }, }; @@ -31,8 +33,8 @@ static const extension file_extension_default_cmd[] = { * similar to mimetype_default_cmd, however it searches for exact string matches * and is checked before mimetype_default_cmd */ { ".exe", "wine"}, - { ".cbz", "mcomix"}, - { ".cbr", "mcomix"}, + { ".cbz", "mcomix % &"}, + { ".cbr", "mcomix % &"}, { ".json", "$EDITOR"}, { ".csv", "$EDITOR"}, }; diff --git a/interactions.c b/interactions.c index df8fcae..be0b966 100644 --- a/interactions.c +++ b/interactions.c @@ -238,9 +238,9 @@ void move_right(){ for (i = 0; i < file_extension_default_count; i++) { if (strstr(extension, file_extension_default_cmd[i].file_extension)) { char *cmd; - concat(cmd, file_extension_default_cmd[i].command, " ./\"", 0); + concat(cmd, file_extension_default_cmd[i].command, " ./\'", 0); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\"", 1); + concat(cmd, cmd, "\'", 1); if (system(cmd) == -1) { @@ -257,12 +257,7 @@ void move_right(){ for (i = 0; i < mimetype_default_count; i++) { if (strstr(mime, mimetype_default_cmd[i].mimetype)) { - char *cmd; - concat(cmd, mimetype_default_cmd[i].command, " ./\"", 0); - concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\"", 1); - - + char *cmd = parse_cmd_char(mimetype_default_cmd[i].command, mid_content[selected_file_current].file_name); if (system(cmd) == -1) { /*do nothing*/ } @@ -317,9 +312,9 @@ void open_with(){ mvwin(win_b, terminal_height-6, 0); wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - concat(btm_buffer, ui_open_with_text_0, " \"", 0); + concat(btm_buffer, ui_open_with_text_0, " \'", 0); concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1); - concat(btm_buffer, btm_buffer, "\" ", 1); + concat(btm_buffer, btm_buffer, "\' ", 1); concat(btm_buffer, btm_buffer, ui_open_with_text_1, 1); @@ -334,9 +329,9 @@ void open_with(){ if (err == 0) { char *cmd; - concat(cmd, str, " ./\"", 0); + concat(cmd, str, " ./\'", 0); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\"", 1); + concat(cmd, cmd, "\'", 1); #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 images_clear(); @@ -366,9 +361,9 @@ void rename_hovered(){ mvwin(win_b, terminal_height-6, 0); wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - concat(btm_buffer, ui_rename_text_0, " \"", 0); + concat(btm_buffer, ui_rename_text_0, " \'", 0); concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1); - concat(btm_buffer, btm_buffer, "\" ", 1); + concat(btm_buffer, btm_buffer, "\' ", 1); concat(btm_buffer, btm_buffer, ui_rename_text_1, 1); window_btm(win_b, 1); @@ -381,10 +376,10 @@ void rename_hovered(){ if (!err) { char *cmd; - concat(cmd, "mv ./\"", mid_content[selected_file_current].file_name, 0); - concat(cmd, cmd, "\" ./\"", 1); + concat(cmd, "mv ./\'", mid_content[selected_file_current].file_name, 0); + concat(cmd, cmd, "\' ./\'", 1); concat(cmd, cmd, str, 1); - concat(cmd, cmd, "\"", 1); + concat(cmd, cmd, "\'", 1); if (system(cmd) == -1) { FAIL("rename_hovered", "mv or creating subcommand failed"); @@ -411,9 +406,9 @@ void delete(){ char *file_str = ""; for (i = 0; i < mid_file_count; i++) { if (mid_content[i].status & FILE_STATUS_SELECTED) { - concat(file_str, file_str, "\"", 0); + concat(file_str, file_str, "\'", 0); concat(file_str, file_str, mid_content[i].file_name, 1); - concat(file_str, file_str, "\" ", 1); + concat(file_str, file_str, "\' ", 1); hits++; } } @@ -653,9 +648,9 @@ void cmd_on_selected(unsigned long passes, int index){ char *file_str = ""; for (i = 0; i < mid_file_count; i++) { if (mid_content[i].status & FILE_STATUS_SELECTED) { - concat(file_str, file_str, "\"", 0); + concat(file_str, file_str, "\'", 0); concat(file_str, file_str, mid_content[i].file_name, 1); - concat(file_str, file_str, "\" ", 1); + concat(file_str, file_str, "\' ", 1); hits++; } } @@ -663,9 +658,9 @@ void cmd_on_selected(unsigned long passes, int index){ if (hits) { concat(btm_buffer, key_binding[index].black_magic, file_str, 2); } else { - concat(btm_buffer, key_binding[index].black_magic, "\"", 0); + concat(btm_buffer, key_binding[index].black_magic, "\'", 0); concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1); - concat(btm_buffer, btm_buffer, "\"", 1); + concat(btm_buffer, btm_buffer, "\'", 1); } concat(btm_buffer, btm_buffer, "?", 1); @@ -691,9 +686,9 @@ void cmd_on_selected(unsigned long passes, int index){ for (i = 0; i < mid_file_count; i++) { if (mid_content[i].status & FILE_STATUS_SELECTED) { free(cmd); - concat(cmd, (char*)key_binding[index].black_magic, " \"", 0); + concat(cmd, (char*)key_binding[index].black_magic, " \'", 0); concat(cmd, cmd, mid_content[i].file_name, 1); - concat(cmd, cmd, "\"", 1); + concat(cmd, cmd, "\'", 1); if (system(cmd) != 0) { /*do nothing*/ } @@ -701,9 +696,9 @@ void cmd_on_selected(unsigned long passes, int index){ } } else { free(cmd); - concat(cmd, (char*)key_binding[index].black_magic, " \"", 0); + concat(cmd, (char*)key_binding[index].black_magic, " \'", 0); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\"", 1); + concat(cmd, cmd, "\'", 1); if (system(cmd) != 0) { /*do nothing*/ } @@ -727,15 +722,15 @@ void yank_text(unsigned long passes, int index){ char *cmd; if (strncmp((char*)key_binding[index].black_magic, "path", 4) == 0) { char *path=getcwd(NULL, 0); - concat(cmd, "echo \"", path, 0); + concat(cmd, "echo \'", path, 0); concat(cmd, cmd, "/", 1); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\" | ", 1); + concat(cmd, cmd, "\' | ", 1); concat(cmd, cmd, clipboard_cmd, 1); free(path); } else { - concat(cmd, "echo \"", mid_content[selected_file_current].file_name, 0); - concat(cmd, cmd, "\" | ", 1); + concat(cmd, "echo \'", mid_content[selected_file_current].file_name, 0); + concat(cmd, cmd, "\' | ", 1); concat(cmd, cmd, clipboard_cmd, 1); } if (system(cmd) == -1) { @@ -792,17 +787,15 @@ void yank_file(unsigned long passes, int index){ void paste(){ unsigned long i; for (i = 0; i < yank_files.count; i++) { - /*TODO(2025-08-14T22:10:44) escape path*/ char *cmd; if (yank_files.status & YANK_COPY) { - concat(cmd, "false | cp -ri ", yank_files.path, 0); + concat(cmd, "false | cp -ri \'", yank_files.path, 0); } else { - concat(cmd, "mv ", yank_files.path, 0); + concat(cmd, "mv \'", yank_files.path, 0); } concat(cmd, cmd, "/", 1); concat(cmd, cmd, *yank_files.list, 1); - concat(cmd, cmd, " ./", 1); - concat(cmd, cmd, *yank_files.list, 1); + concat(cmd, cmd, "\' ./", 1); concat(cmd, cmd, " 2>&1", 1); char *line = malloc(INPUT_BUFFER_SIZE); FILE *cmd_open;