1
0
mirror of https://gittea.dev/nova/th.git synced 2026-01-30 16:50:10 -05:00

Compare commits

...

2 Commits

Author SHA1 Message Date
nova
66f3f3bef6 small changes 2026-01-05 22:15:11 +01:00
nova
393864c80f better string handling 2025-12-12 20:28:39 +01:00
6 changed files with 77 additions and 63 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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
@@ -19,20 +21,20 @@ static const mimetype mimetype_default_cmd[] = {
* file --mime-type -b ./image.gif * file --mime-type -b ./image.gif
* gives us "image/gif", thusly if we want to open gif in mpv rather than feh, * gives us "image/gif", thusly if we want to open gif in mpv rather than feh,
* we need to define "gif" before "image" */ * we need to define "gif" before "image" */
{ "text", "$EDITOR" }, { "text", "$EDITOR" },
{ "gif", "mpv --loop-file=\"inf\"" }, { "gif", "nohup mpv --loop-file=\"inf\" % >/dev/null 2>&1 &" },
{ "image", "feh" }, { "image", "feh % >/dev/null 2>&1 &" },
{ "video", "mpv" }, { "video", "nohup mpv % >/dev/null 2>&1 &" },
{ "audio", "mpv" }, { "audio", "mpv" },
{ "pdf", "zathura" }, { "pdf", "zathura" },
}; };
static const extension file_extension_default_cmd[] = { static const extension file_extension_default_cmd[] = {
/* extension shell command /* extension shell command
* 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"},
}; };

9
dir.c
View File

@@ -139,18 +139,13 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
} }
free(full_path); free(full_path);
free(file); free(file);
free(entry[i]);
} }
} }
qsort(dir_content, *dir_file_count, sizeof(file), order_func); qsort(dir_content, *dir_file_count, sizeof(file), order_func);
for (i = 0; i < *dir_file_count; i++) { free(entry);
free(entry[i]);
}
if (entry != NULL) {
free(entry);
} else {
}
} }

View File

@@ -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;

View File

@@ -118,19 +118,13 @@ int sort_random(const void *file0, const void *file1){
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) { if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1; return 1;
} }
time_t num = (time_t)&seed; time_t num = ((time_t)&sort_random)^((time_t)sort_natural);
time_t i; int i = 1;
for (i = num%2; i < 6+(((time_t)&seed)%2); i++){ for (; i < 6; i++) {
num ^= *seed; num += *seed;
if (num%2) { num ^= num << ((((time_t)&seed)%6)+i);
num ^= (time_t)&num; num ^= num >> ((((time_t)&num)%9)+i);
num ^= num << (i + (((time_t)&seed)%5));
} else {
num ^= (time_t)&seed;
num ^= num >> (i + (((time_t)&num)%5));
}
} }
num ^= getpid();
return ((num%3) - 1); return ((num%3) - 1);