From 527199c23d9d4ee5d4044aa7507d27e2f69c67cb Mon Sep 17 00:00:00 2001 From: Aleksandr Lebedev Date: Tue, 21 Apr 2026 15:43:51 +0200 Subject: [PATCH] Code Cleanup + Ctrl+D fix + Ctrl+L clears screen - Removed some code duplication - Ctrl+D is fixed and now works as it should (exit if user didn't write anything, ignore otherwise) - Ctrl+L clears the terminal screen --- main.c | 94 ++++++++++++++++++++++++---------------------------------- 1 file changed, 39 insertions(+), 55 deletions(-) diff --git a/main.c b/main.c index 0e6e717..2ee9348 100644 --- a/main.c +++ b/main.c @@ -48,10 +48,11 @@ void* ccalloc(size_t size, size_t n) } static constexpr size_t max_line = 1024; -static char string_buffer[max_line]; + char* __readline_file(FILE* stream) { - if(fgets(string_buffer, max_line, stream) == NULL) + static char buffer[max_line]; + if(fgets(buffer, sizeof(buffer), stream) == NULL) { if(ferror(stream)) { @@ -62,13 +63,13 @@ char* __readline_file(FILE* stream) return NULL; } - size_t length = strlen(string_buffer); + size_t length = strlen(buffer); //Remove trailing \n - if (length > 0 && string_buffer[length - 1] == '\n') + if (length > 0 && buffer[length - 1] == '\n') { - string_buffer[length - 1] = '\0'; + buffer[length - 1] = '\0'; } - return string_buffer; + return buffer; } static struct termios orig_termios; @@ -139,23 +140,8 @@ size_t utf8_display_width(const char *buf, size_t len) while (i < len) { unsigned char c = buf[i]; - - if ((c & 0x80) == 0) { - i += 1; - width += 1; - } - else if ((c & 0xE0) == 0xC0) { - i += 2; - width += 1; - } - else if ((c & 0xF0) == 0xE0) { - i += 3; - width += 1; - } - else { - i += 4; - width += 1; - } + i += utf8_char_len(c); + width += 1; } return width; @@ -180,23 +166,8 @@ size_t visible_width(const char *s) } unsigned char c = s[i]; - - if ((c & 0x80) == 0) { - i += 1; - width += 1; - } - else if ((c & 0xE0) == 0xC0) { - i += 2; - width += 1; - } - else if ((c & 0xF0) == 0xE0) { - i += 3; - width += 1; - } - else { - i += 4; - width += 1; - } + i += utf8_char_len(c); + width += 1; } return width; @@ -205,7 +176,7 @@ size_t visible_width(const char *s) char* __readline_interactive(char* prompt) { enable_raw_mode(); - static char buffer[max_length]; + static char buffer[max_line]; size_t cursor = 0; size_t len = 0; ssize_t swrite(int fd, const char* str) @@ -282,6 +253,19 @@ char* __readline_interactive(char* prompt) } } } + else if (utf8[0] == 0x04) //CTRL + D + { + if(len == 0) + { + disable_raw_mode(); + return NULL; + } + } + else if (utf8[0] == 0x0C) //CTRL + L + { + swrite(1, "\x1b[2J"); // clear screen + swrite(1, "\x1b[H"); // cursor home + } else { if(len < sizeof(buffer)) { @@ -866,7 +850,7 @@ int execute_pipeline(struct pipeline* pl, int capture, char** out) if(strcmp(cmd_name, "cd") == 0) { - char* path = cmd->argv[1] ? cmd->argv[1] : getenv("HOME"); + char* path = cmd->argv[1] ? cmd->argv[1] : get_home(); if (cd(path) < 0) { char err_buf[max_length] = {0}; @@ -923,43 +907,43 @@ int execute_pipeline(struct pipeline* pl, int capture, char** out) return WEXITSTATUS(status); } -char username_buf[max_env_var_length] = {0}; char* get_user() { + static char buf[max_env_var_length] = {0}; char* result = getenv("USER"); if(!result) { return "UNKNOWN"; } - size_t length = min_size_t(sizeof(username_buf), strlen(result) + 1); - memcpy(username_buf, result, length); - return username_buf; + size_t length = min_size_t(sizeof(buf), strlen(result) + 1); + memcpy(buf, result, length); + return buf; } -char home_buf[max_length] = {0}; char* get_home() { + static char buf[max_length] = {0}; char* result = getenv("HOME"); if(!result) { return "UNKNOWN_HOME_PATH"; } - size_t length = min_size_t(sizeof(home_buf), strlen(result) + 1); - memcpy(home_buf, result, length); - return home_buf; + size_t length = min_size_t(sizeof(buf), strlen(result) + 1); + memcpy(buf, result, length); + return buf; } -char pwd_buf[max_length] = {0}; char* get_pwd() { + static char buf[max_length] = {0}; char* result = getenv("PWD"); if(!result) { return "UNKNOWN"; } - size_t length = min_size_t(sizeof(pwd_buf), strlen(result) + 1); - memcpy(pwd_buf, result, length); - return pwd_buf; + size_t length = min_size_t(sizeof(buf), strlen(result) + 1); + memcpy(buf, result, length); + return buf; } char* prettify_pwd(char* pwd) @@ -988,9 +972,9 @@ char* prettify_pwd(char* pwd) return pwd; } -char prompt_buf[max_length] = {0}; char* generate_ps1_prompt() { + static char prompt_buf[max_length] = {0}; char env_buf[max_env_var_length] = {0}; char* ps1 = getenv("PS1"); if(!ps1)