mirror of
https://gittea.dev/nova/th.git
synced 2026-01-30 08:40:10 -05:00
better string handling
This commit is contained in:
28
backend.c
28
backend.c
@@ -1,5 +1,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
#define concat(out, s1, s2, _free) { \
|
#define concat(out, s1, s2, _free) { \
|
||||||
concat## _free(out, s1, s2); \
|
concat## _free(out, s1, s2); \
|
||||||
@@ -78,3 +80,29 @@ char* smartstrcasestr(const char *haystack, const char *needle){
|
|||||||
return ret;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,3 +7,5 @@
|
|||||||
|
|
||||||
/*char* concat(const char *s1, const char *s2);*/
|
/*char* concat(const char *s1, const char *s2);*/
|
||||||
char* smartstrcasestr(const char *haystack, const char *needle);
|
char* smartstrcasestr(const char *haystack, const char *needle);
|
||||||
|
char* parse_cmd_char(const char *cmd, const char *path);
|
||||||
|
|
||||||
|
|||||||
10
config.h
10
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_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_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_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
|
#ifndef CONFIG_GUARD
|
||||||
@@ -21,8 +23,8 @@ static const mimetype mimetype_default_cmd[] = {
|
|||||||
* we need to define "gif" before "image" */
|
* we need to define "gif" before "image" */
|
||||||
{ "text", "$EDITOR" },
|
{ "text", "$EDITOR" },
|
||||||
{ "gif", "mpv --loop-file=\"inf\"" },
|
{ "gif", "mpv --loop-file=\"inf\"" },
|
||||||
{ "image", "feh" },
|
{ "image", "feh % &" },
|
||||||
{ "video", "mpv" },
|
{ "video", "mpv % &" },
|
||||||
{ "audio", "mpv" },
|
{ "audio", "mpv" },
|
||||||
{ "pdf", "zathura" },
|
{ "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
|
* similar to mimetype_default_cmd, however it searches for exact string matches
|
||||||
* and is checked before mimetype_default_cmd */
|
* and is checked before mimetype_default_cmd */
|
||||||
{ ".exe", "wine"},
|
{ ".exe", "wine"},
|
||||||
{ ".cbz", "mcomix"},
|
{ ".cbz", "mcomix % &"},
|
||||||
{ ".cbr", "mcomix"},
|
{ ".cbr", "mcomix % &"},
|
||||||
{ ".json", "$EDITOR"},
|
{ ".json", "$EDITOR"},
|
||||||
{ ".csv", "$EDITOR"},
|
{ ".csv", "$EDITOR"},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -238,9 +238,9 @@ void move_right(){
|
|||||||
for (i = 0; i < file_extension_default_count; i++) {
|
for (i = 0; i < file_extension_default_count; i++) {
|
||||||
if (strstr(extension, file_extension_default_cmd[i].file_extension)) {
|
if (strstr(extension, file_extension_default_cmd[i].file_extension)) {
|
||||||
char *cmd;
|
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, mid_content[selected_file_current].file_name, 1);
|
||||||
concat(cmd, cmd, "\"", 1);
|
concat(cmd, cmd, "\'", 1);
|
||||||
|
|
||||||
|
|
||||||
if (system(cmd) == -1) {
|
if (system(cmd) == -1) {
|
||||||
@@ -257,12 +257,7 @@ void move_right(){
|
|||||||
for (i = 0; i < mimetype_default_count; i++) {
|
for (i = 0; i < mimetype_default_count; i++) {
|
||||||
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
|
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
|
||||||
|
|
||||||
char *cmd;
|
char *cmd = parse_cmd_char(mimetype_default_cmd[i].command, mid_content[selected_file_current].file_name);
|
||||||
concat(cmd, mimetype_default_cmd[i].command, " ./\"", 0);
|
|
||||||
concat(cmd, cmd, mid_content[selected_file_current].file_name, 1);
|
|
||||||
concat(cmd, cmd, "\"", 1);
|
|
||||||
|
|
||||||
|
|
||||||
if (system(cmd) == -1) {
|
if (system(cmd) == -1) {
|
||||||
/*do nothing*/
|
/*do nothing*/
|
||||||
}
|
}
|
||||||
@@ -317,9 +312,9 @@ void open_with(){
|
|||||||
mvwin(win_b, terminal_height-6, 0);
|
mvwin(win_b, terminal_height-6, 0);
|
||||||
wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/
|
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, 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);
|
concat(btm_buffer, btm_buffer, ui_open_with_text_1, 1);
|
||||||
|
|
||||||
|
|
||||||
@@ -334,9 +329,9 @@ void open_with(){
|
|||||||
|
|
||||||
if (err == 0) {
|
if (err == 0) {
|
||||||
char *cmd;
|
char *cmd;
|
||||||
concat(cmd, str, " ./\"", 0);
|
concat(cmd, str, " ./\'", 0);
|
||||||
concat(cmd, cmd, mid_content[selected_file_current].file_name, 1);
|
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
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||||
images_clear();
|
images_clear();
|
||||||
@@ -366,9 +361,9 @@ void rename_hovered(){
|
|||||||
mvwin(win_b, terminal_height-6, 0);
|
mvwin(win_b, terminal_height-6, 0);
|
||||||
wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/
|
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, 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);
|
concat(btm_buffer, btm_buffer, ui_rename_text_1, 1);
|
||||||
|
|
||||||
window_btm(win_b, 1);
|
window_btm(win_b, 1);
|
||||||
@@ -381,10 +376,10 @@ void rename_hovered(){
|
|||||||
|
|
||||||
if (!err) {
|
if (!err) {
|
||||||
char *cmd;
|
char *cmd;
|
||||||
concat(cmd, "mv ./\"", mid_content[selected_file_current].file_name, 0);
|
concat(cmd, "mv ./\'", mid_content[selected_file_current].file_name, 0);
|
||||||
concat(cmd, cmd, "\" ./\"", 1);
|
concat(cmd, cmd, "\' ./\'", 1);
|
||||||
concat(cmd, cmd, str, 1);
|
concat(cmd, cmd, str, 1);
|
||||||
concat(cmd, cmd, "\"", 1);
|
concat(cmd, cmd, "\'", 1);
|
||||||
|
|
||||||
if (system(cmd) == -1) {
|
if (system(cmd) == -1) {
|
||||||
FAIL("rename_hovered", "mv or creating subcommand failed");
|
FAIL("rename_hovered", "mv or creating subcommand failed");
|
||||||
@@ -411,9 +406,9 @@ void delete(){
|
|||||||
char *file_str = "";
|
char *file_str = "";
|
||||||
for (i = 0; i < mid_file_count; i++) {
|
for (i = 0; i < mid_file_count; i++) {
|
||||||
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
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, mid_content[i].file_name, 1);
|
||||||
concat(file_str, file_str, "\" ", 1);
|
concat(file_str, file_str, "\' ", 1);
|
||||||
hits++;
|
hits++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -653,9 +648,9 @@ void cmd_on_selected(unsigned long passes, int index){
|
|||||||
char *file_str = "";
|
char *file_str = "";
|
||||||
for (i = 0; i < mid_file_count; i++) {
|
for (i = 0; i < mid_file_count; i++) {
|
||||||
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
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, mid_content[i].file_name, 1);
|
||||||
concat(file_str, file_str, "\" ", 1);
|
concat(file_str, file_str, "\' ", 1);
|
||||||
hits++;
|
hits++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -663,9 +658,9 @@ void cmd_on_selected(unsigned long passes, int index){
|
|||||||
if (hits) {
|
if (hits) {
|
||||||
concat(btm_buffer, key_binding[index].black_magic, file_str, 2);
|
concat(btm_buffer, key_binding[index].black_magic, file_str, 2);
|
||||||
} else {
|
} 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, 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);
|
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++) {
|
for (i = 0; i < mid_file_count; i++) {
|
||||||
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
||||||
free(cmd);
|
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, mid_content[i].file_name, 1);
|
||||||
concat(cmd, cmd, "\"", 1);
|
concat(cmd, cmd, "\'", 1);
|
||||||
if (system(cmd) != 0) {
|
if (system(cmd) != 0) {
|
||||||
/*do nothing*/
|
/*do nothing*/
|
||||||
}
|
}
|
||||||
@@ -701,9 +696,9 @@ void cmd_on_selected(unsigned long passes, int index){
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
free(cmd);
|
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, mid_content[selected_file_current].file_name, 1);
|
||||||
concat(cmd, cmd, "\"", 1);
|
concat(cmd, cmd, "\'", 1);
|
||||||
if (system(cmd) != 0) {
|
if (system(cmd) != 0) {
|
||||||
/*do nothing*/
|
/*do nothing*/
|
||||||
}
|
}
|
||||||
@@ -727,15 +722,15 @@ void yank_text(unsigned long passes, int index){
|
|||||||
char *cmd;
|
char *cmd;
|
||||||
if (strncmp((char*)key_binding[index].black_magic, "path", 4) == 0) {
|
if (strncmp((char*)key_binding[index].black_magic, "path", 4) == 0) {
|
||||||
char *path=getcwd(NULL, 0);
|
char *path=getcwd(NULL, 0);
|
||||||
concat(cmd, "echo \"", path, 0);
|
concat(cmd, "echo \'", path, 0);
|
||||||
concat(cmd, cmd, "/", 1);
|
concat(cmd, cmd, "/", 1);
|
||||||
concat(cmd, cmd, mid_content[selected_file_current].file_name, 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);
|
concat(cmd, cmd, clipboard_cmd, 1);
|
||||||
free(path);
|
free(path);
|
||||||
} else {
|
} else {
|
||||||
concat(cmd, "echo \"", mid_content[selected_file_current].file_name, 0);
|
concat(cmd, "echo \'", mid_content[selected_file_current].file_name, 0);
|
||||||
concat(cmd, cmd, "\" | ", 1);
|
concat(cmd, cmd, "\' | ", 1);
|
||||||
concat(cmd, cmd, clipboard_cmd, 1);
|
concat(cmd, cmd, clipboard_cmd, 1);
|
||||||
}
|
}
|
||||||
if (system(cmd) == -1) {
|
if (system(cmd) == -1) {
|
||||||
@@ -792,17 +787,15 @@ void yank_file(unsigned long passes, int index){
|
|||||||
void paste(){
|
void paste(){
|
||||||
unsigned long i;
|
unsigned long i;
|
||||||
for (i = 0; i < yank_files.count; i++) {
|
for (i = 0; i < yank_files.count; i++) {
|
||||||
/*TODO(2025-08-14T22:10:44) escape path*/
|
|
||||||
char *cmd;
|
char *cmd;
|
||||||
if (yank_files.status & YANK_COPY) {
|
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 {
|
} else {
|
||||||
concat(cmd, "mv ", yank_files.path, 0);
|
concat(cmd, "mv \'", yank_files.path, 0);
|
||||||
}
|
}
|
||||||
concat(cmd, cmd, "/", 1);
|
concat(cmd, cmd, "/", 1);
|
||||||
concat(cmd, cmd, *yank_files.list, 1);
|
concat(cmd, cmd, *yank_files.list, 1);
|
||||||
concat(cmd, cmd, " ./", 1);
|
concat(cmd, cmd, "\' ./", 1);
|
||||||
concat(cmd, cmd, *yank_files.list, 1);
|
|
||||||
concat(cmd, cmd, " 2>&1", 1);
|
concat(cmd, cmd, " 2>&1", 1);
|
||||||
char *line = malloc(INPUT_BUFFER_SIZE);
|
char *line = malloc(INPUT_BUFFER_SIZE);
|
||||||
FILE *cmd_open;
|
FILE *cmd_open;
|
||||||
|
|||||||
Reference in New Issue
Block a user