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

migration of concat to macro based implementation

This commit is contained in:
nova
2025-11-16 11:34:11 +01:00
parent be9436570c
commit ddcf4d4105
4 changed files with 102 additions and 74 deletions

View File

@@ -1,14 +1,40 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
char* concat(const char *s1, const char *s2){ #define concat(out, s1, s2, _free) { \
const size_t len1 = strlen(s1); concat## _free(out, s1, s2); \
const size_t len2 = strlen(s2);
char *result = malloc(len1 + len2 + 1);
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2 + 1);
return result;
} }
#define concat0(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
out = result;
#define concat1(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s1); \
out = result;
#define concat2(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s2); \
out = result;
#define concat3(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s1); \
free(s2); \
out = result;
char* smartstrcasestr(const char *haystack, const char *needle){ char* smartstrcasestr(const char *haystack, const char *needle){
char smart = 0; char smart = 0;
char *ret; char *ret;

View File

@@ -5,5 +5,5 @@
#endif #endif
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);

View File

@@ -85,8 +85,9 @@ char* text(char *path, unsigned long *file_size){
} }
} }
char* generic(char *path){ char* generic(char *path){
char *cmd = concat("file ./\"", path); char *cmd;
cmd = concat(cmd, "\""); concat(cmd, "file ./\"", path, 0);
concat(cmd, cmd, "\"", 1);
FILE *cmd_open = popen(cmd, "r"); FILE *cmd_open = popen(cmd, "r");
char *line = NULL; char *line = NULL;

View File

@@ -236,9 +236,10 @@ void move_right(){
if (extension != NULL) { if (extension != NULL) {
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 = concat(file_extension_default_cmd[i].command, " ./\""); char *cmd;
cmd = concat(cmd, mid_content[selected_file_current].file_name); concat(cmd, file_extension_default_cmd[i].command, " ./\"", 0);
cmd = concat(cmd, "\""); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1);
concat(cmd, cmd, "\"", 1);
if (system(cmd) == -1) { if (system(cmd) == -1) {
@@ -255,10 +256,10 @@ 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 = concat(mimetype_default_cmd[i].command, " ./\""); char *cmd;
cmd = concat(cmd, mid_content[selected_file_current].file_name); concat(cmd, mimetype_default_cmd[i].command, " ./\"", 0);
cmd = concat(cmd, "\""); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1);
concat(cmd, cmd, "\"", 1);
if (system(cmd) == -1) { if (system(cmd) == -1) {
@@ -315,8 +316,8 @@ 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*/
btm_buffer = concat("open \"", mid_content[selected_file_current].file_name); concat(btm_buffer, "open \"", mid_content[selected_file_current].file_name, 0);
btm_buffer = concat(btm_buffer, "\" with:"); concat(btm_buffer, btm_buffer, "\" with:", 1);
window_btm(win_b, 1); window_btm(win_b, 1);
@@ -328,9 +329,10 @@ void open_with(){
if (err == 0) { if (err == 0) {
char *cmd = concat(str, " ./\""); char *cmd;
cmd = concat(cmd, mid_content[selected_file_current].file_name); concat(cmd, str, " ./\"", 0);
cmd = concat(cmd, "\""); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1);
concat(cmd, cmd, "\"", 1);
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear(); images_clear();
@@ -358,8 +360,8 @@ 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*/
btm_buffer = concat("rename \"", mid_content[selected_file_current].file_name); concat(btm_buffer, "rename \"", mid_content[selected_file_current].file_name, 0);
btm_buffer = concat(btm_buffer, "\" to:"); concat(btm_buffer, btm_buffer, "\" to:", 1);
window_btm(win_b, 1); window_btm(win_b, 1);
@@ -370,10 +372,11 @@ void rename_hovered(){
if (!err) { if (!err) {
char *cmd = concat("mv ./\"", mid_content[selected_file_current].file_name); char *cmd;
cmd = concat(cmd, "\" ./\""); concat(cmd, "mv ./\"", mid_content[selected_file_current].file_name, 0);
cmd = concat(cmd, str); concat(cmd, cmd, "\" ./\"", 1);
cmd = concat(cmd, "\""); concat(cmd, cmd, str, 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");
@@ -397,12 +400,12 @@ void delete(){
unsigned int i = 0; unsigned int i = 0;
unsigned int hits = 0; unsigned int hits = 0;
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) {
file_str = concat(file_str, "\""); concat(file_str, file_str, "\"", 0);
file_str = concat(file_str, mid_content[i].file_name); concat(file_str, file_str, mid_content[i].file_name, 1);
file_str = concat(file_str, "\" "); concat(file_str, file_str, "\" ", 1);
hits++; hits++;
} }
} }
@@ -497,7 +500,7 @@ void makedir(){
int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str);
if (!err) { if (!err) {
btm_buffer = concat(btm_buffer, str); concat(btm_buffer, btm_buffer, str, 0);
mode_t mask = umask(0); mode_t mask = umask(0);
mkdir(str, 0755); /*magic number from default permissions as created by mkdir*/ mkdir(str, 0755); /*magic number from default permissions as created by mkdir*/
umask(mask); umask(mask);
@@ -527,7 +530,7 @@ void makefile(){
int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str);
if (!err) { if (!err) {
btm_buffer = concat(btm_buffer, str); concat(btm_buffer, btm_buffer, str, 0);
FILE *fp; FILE *fp;
fp = fopen(str, "w"); fp = fopen(str, "w");
fclose(fp); fclose(fp);
@@ -590,7 +593,7 @@ void jump_to_dir(unsigned long passes, int index){
env_str[env_len-1] = '\0'; env_str[env_len-1] = '\0';
env_parsed = getenv(env_str); env_parsed = getenv(env_str);
if (env_parsed) { if (env_parsed) {
path = concat(env_parsed, (char*)key_binding[index].black_magic + env_len); concat(path, env_parsed, (char*)key_binding[index].black_magic + env_len, 0);
} else { } else {
path = malloc(strlen((char*)key_binding[index].black_magic)); path = malloc(strlen((char*)key_binding[index].black_magic));
memcpy(path, (char*)key_binding[index].black_magic, strlen((char*)key_binding[index].black_magic)+1); memcpy(path, (char*)key_binding[index].black_magic, strlen((char*)key_binding[index].black_magic)+1);
@@ -636,29 +639,30 @@ void cmd_on_selected(unsigned long passes, int index){
pthread_mutex_lock(&mutex_btm); pthread_mutex_lock(&mutex_btm);
char *btm_buffer_tmp = btm_buffer; char *btm_buffer_tmp = btm_buffer;
btm_buffer = "";
unsigned int i = 0; unsigned int i = 0;
unsigned int hits = 0; unsigned int hits = 0;
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) {
file_str = concat(file_str, "\""); concat(file_str, file_str, "\"", 0);
file_str = concat(file_str, mid_content[i].file_name); concat(file_str, file_str, mid_content[i].file_name, 1);
file_str = concat(file_str, "\" "); concat(file_str, file_str, "\" ", 1);
hits++; hits++;
} }
} }
if (hits) { if (hits) {
btm_buffer = concat(key_binding[index].black_magic, file_str); concat(btm_buffer, key_binding[index].black_magic, file_str, 2);
} else { } else {
btm_buffer = concat(key_binding[index].black_magic, "\""); concat(btm_buffer, key_binding[index].black_magic, "\"", 0);
btm_buffer = concat(btm_buffer, mid_content[selected_file_current].file_name); concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1);
btm_buffer = concat(btm_buffer, "\""); concat(btm_buffer, btm_buffer, "\"", 1);
} }
btm_buffer = concat(btm_buffer, "?"); concat(btm_buffer, btm_buffer, "?", 1);
btm_buffer = concat(btm_buffer, "\n\n"); concat(btm_buffer, btm_buffer, "\n\n", 1);
btm_buffer = concat(btm_buffer, "(y/N)"); concat(btm_buffer, btm_buffer, "(y/N)", 1);
werase(win_b); werase(win_b);
mvwin(win_b, terminal_height-6, 0); mvwin(win_b, terminal_height-6, 0);
@@ -678,9 +682,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);
cmd = concat((char*)key_binding[index].black_magic, " \""); concat(cmd, (char*)key_binding[index].black_magic, " \"", 0);
cmd = concat(cmd, mid_content[i].file_name); concat(cmd, cmd, mid_content[i].file_name, 1);
cmd = concat(cmd, "\""); concat(cmd, cmd, "\"", 1);
if (system(cmd) != 0) { if (system(cmd) != 0) {
/*do nothing*/ /*do nothing*/
} }
@@ -688,9 +692,9 @@ void cmd_on_selected(unsigned long passes, int index){
} }
} else { } else {
free(cmd); free(cmd);
cmd = concat((char*)key_binding[index].black_magic, " \""); concat(cmd, (char*)key_binding[index].black_magic, " \"", 0);
cmd = concat(cmd, mid_content[selected_file_current].file_name); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1);
cmd = concat(cmd, "\""); concat(cmd, cmd, "\"", 1);
if (system(cmd) != 0) { if (system(cmd) != 0) {
/*do nothing*/ /*do nothing*/
} }
@@ -706,9 +710,6 @@ void cmd_on_selected(unsigned long passes, int index){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
if (hits) {
free(file_str);
}
pthread_mutex_unlock(&mutex_btm); pthread_mutex_unlock(&mutex_btm);
} }
void yank_text(unsigned long passes, int index){ void yank_text(unsigned long passes, int index){
@@ -716,16 +717,16 @@ 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);
cmd = concat("echo \"", path); concat(cmd, "echo \"", path, 0);
cmd = concat(cmd, "/"); concat(cmd, cmd, "/", 1);
cmd = concat(cmd, mid_content[selected_file_current].file_name); concat(cmd, cmd, mid_content[selected_file_current].file_name, 1);
cmd = concat(cmd, "\" | "); concat(cmd, cmd, "\" | ", 1);
cmd = concat(cmd, clipboard_cmd); concat(cmd, cmd, clipboard_cmd, 1);
free(path); free(path);
} else { } else {
cmd = concat("echo \"", mid_content[selected_file_current].file_name); concat(cmd, "echo \"", mid_content[selected_file_current].file_name, 0);
cmd = concat(cmd, "\" | "); concat(cmd, cmd, "\" | ", 1);
cmd = concat(cmd, clipboard_cmd); concat(cmd, cmd, clipboard_cmd, 1);
} }
if (system(cmd) == -1) { if (system(cmd) == -1) {
/*do nothing*/ /*do nothing*/
@@ -784,15 +785,15 @@ void paste(){
/*TODO(2025-08-14T22:10:44) escape path*/ /*TODO(2025-08-14T22:10:44) escape path*/
char *cmd; char *cmd;
if (yank_files.status & YANK_COPY) { if (yank_files.status & YANK_COPY) {
cmd = concat("false | cp -ri ", yank_files.path); concat(cmd, "false | cp -ri ", yank_files.path, 0);
} else { } else {
cmd = concat("mv ", yank_files.path); concat(cmd, "mv ", yank_files.path, 0);
} }
cmd = concat(cmd, "/"); concat(cmd, cmd, "/", 1);
cmd = concat(cmd, *yank_files.list); concat(cmd, cmd, *yank_files.list, 1);
cmd = concat(cmd, " ./"); concat(cmd, cmd, " ./", 1);
cmd = concat(cmd, *yank_files.list); concat(cmd, cmd, *yank_files.list, 1);
cmd = concat(cmd, " 2>&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;
while (1) { while (1) {
@@ -802,12 +803,12 @@ void paste(){
} }
if (strstr(line, "are the same file")) { if (strstr(line, "are the same file")) {
cmd[strlen(cmd)-strlen(" 2>&1")] = '\0'; cmd[strlen(cmd)-strlen(" 2>&1")] = '\0';
cmd = concat(cmd, "_"); concat(cmd, cmd, "_", 1);
cmd = concat(cmd, " 2>&1"); concat(cmd, cmd, " 2>&1", 1);
} else if ((strstr(line, "overwrite"))) { } else if ((strstr(line, "overwrite"))) {
cmd[strlen(cmd)-strlen(" 2>&1")] = '\0'; cmd[strlen(cmd)-strlen(" 2>&1")] = '\0';
cmd = concat(cmd, "_"); concat(cmd, cmd, "_", 1);
cmd = concat(cmd, " 2>&1"); concat(cmd, cmd, " 2>&1", 1);
} else if ((strstr(line, "No such file or directory"))) { } else if ((strstr(line, "No such file or directory"))) {
pclose(cmd_open); pclose(cmd_open);
break; break;