From cd66370fccea6963e2d9ba13ebfdefdae0afc2ae Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Mon, 8 Jul 2024 17:03:53 +0200 Subject: [PATCH] line works 'normally'TM --- line/src/line.c | 96 +++++++++++++++++++++++++++----------- sources/main.c | 12 +++-- sources/signal_handler.c | 16 ++++--- stdme/src/fs/fs_internal.c | 5 +- 4 files changed, 90 insertions(+), 39 deletions(-) diff --git a/line/src/line.c b/line/src/line.c index 80e4de9a..b65a8bac 100644 --- a/line/src/line.c +++ b/line/src/line.c @@ -65,6 +65,7 @@ enum e_key_action K_CTRL_F = 6, /* Ctrl-f */ K_CTRL_H = 8, /* Ctrl-h */ K_TAB = 9, /* Tab */ + K_NEWLINE = 10, /* Newline */ K_CTRL_K = 11, /* Ctrl+k */ K_CTRL_L = 12, /* Ctrl+l */ K_ENTER = 13, /* Enter */ @@ -126,13 +127,13 @@ t_error linenoise_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_o unsigned int i = 0; /* Report cursor location */ - if (write(output->fd, "\x1b[6n", 4) != 4) + if (write_fd(output, (t_u8*)"\x1b[6n", 4, NULL)) return (ERROR); /* Read the response: ESC [ rows ; cols R */ while (i < sizeof(buf) - 1) { - if (read(input->fd, buf + i, 1) != 1) + if (read_fd(input, (t_u8 *)(buf + i), 1, NULL)) break; if (buf[i] == 'R') break; @@ -239,6 +240,50 @@ void linenoise_disable_raw_mode(t_fd *fd) * write all the escape sequences in a buffer and flush them to the standard * output in a single call, to avoid flickering effects. */ +t_usize linenoise_get_prompt_len(t_const_str s) +{ + t_usize i; + t_usize ret; + t_isize color; + + if (s == NULL) + return (0); + i = 0; + ret = 0; + while (s[i]) + { + if (s[i] == '\x1b') + { + i++; + if (s[i] == '[') + { + i++; + color = 0; + while (color >= 0) + { + color--; + while (s[i] >= '0' && s[i] <= '9') + { + i++; + color += 2; + } + if (s[i] == ';') + i++; + else if (s[i] == 'm') + { + i++; + break; + } + } + } + } + i++; + ret++; + } + //printf("ret = %zu\n", ret); + return (ret); +} + /* Single line low level line refresh. * * Rewrite the currently edited line accordingly to the buffer content, @@ -248,20 +293,11 @@ void linenoise_disable_raw_mode(t_fd *fd) * prompt, just write it, or both. */ void linenoise_refresh_single_line(t_line_state *l, t_line_flags flags) { - size_t prompt_len = strlen(l->prompt); + size_t prompt_len = linenoise_get_prompt_len(l->prompt); t_str input_buf = l->buf; - size_t input_len = l->len; size_t cursor_pos = l->pos; t_string str; - while ((prompt_len + cursor_pos) >= l->columns) - { - input_buf++; - input_len--; - cursor_pos--; - } - while (prompt_len + input_len > l->columns) - input_len--; str = string_new(16); string_push_char(&str, '\r'); @@ -273,17 +309,17 @@ void linenoise_refresh_single_line(t_line_state *l, t_line_flags flags) string_push(&str, input_buf); // , len); } string_push(&str, "\x1b[0K"); - /* Erase to right */ if (flags & REFRESH_WRITE) { /* Move cursor to original position. */ - // TODO: finish this line with me_printf_str - me_printf_str(&str, "\r\x1b[%dC", (int)(cursor_pos + prompt_len)); + me_printf_str(&str, "\r\x1b[%iC", (int)(cursor_pos + prompt_len)); } - me_eprintf("refresh single line;\n"); - me_printf_fd(l->output_fd, "%s", str.buf); + //me_eprintf("refresh single line;\n"); + write_fd(l->output_fd, (t_u8 *)str.buf, str.len, NULL); + + //printf("cursor_pos = %zu\n", cursor_pos); string_free(str); } @@ -322,7 +358,7 @@ t_error linenoise_edit_insert(t_line_state *l, char c) if (l->len == l->pos) { l->buf[l->pos] = c; - l->pos++; + //l->pos++; l->len++; l->buf[l->len] = '\0'; linenoise_refresh_line(l); @@ -491,7 +527,7 @@ t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, c l->buf = buf; l->buflen = buflen; l->prompt = prompt; - l->prompt_len = strlen(prompt); + l->prompt_len = str_len(l->prompt); l->old_pos = l->pos = 0; l->len = 0; @@ -516,6 +552,7 @@ t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, c /* The latest history entry is always our current buffer, that * initially is just an empty string. */ linenoise_history_add(""); + linenoise_enable_raw_mode(l->output_fd); if (write_fd(l->output_fd, (t_u8 *)prompt, l->prompt_len, NULL)) return (ERROR); @@ -559,6 +596,7 @@ t_str linenoise_edit_feed(t_line_state *l) switch (c) { + case K_NEWLINE: /* enter */ case K_ENTER: /* enter */ history->len--; mem_free(history->buffer[history->len]); @@ -567,7 +605,7 @@ t_str linenoise_edit_feed(t_line_state *l) errno = EAGAIN; return NULL; case K_BACKSPACE: /* backspace */ - case 8: /* ctrl-h */ + case K_CTRL_H: /* ctrl-h */ linenoise_edit_backspace(l); break; case K_CTRL_D: /* ctrl-d, remove char at right of cursor, or if the @@ -599,9 +637,15 @@ t_str linenoise_edit_feed(t_line_state *l) * Use two calls to handle slow terminals returning the two * chars at different times. */ if (read_fd(l->input_fd, (t_u8 *)seq, 1, NULL)) + { + printf("Failed read\n"); break; + } if (read_fd(l->input_fd, (t_u8 *)(seq + 1), 1, NULL)) + { + printf("Failed read2\n"); break; + } /* ESC [ sequences. */ if (seq[0] == '[') @@ -654,10 +698,6 @@ t_str linenoise_edit_feed(t_line_state *l) } } break; - default: - if (linenoise_edit_insert(l, c)) - return NULL; - break; case K_CTRL_U: /* Ctrl+u, delete the whole line. */ l->buf[0] = '\0'; l->pos = l->len = 0; @@ -681,6 +721,10 @@ t_str linenoise_edit_feed(t_line_state *l) case K_CTRL_W: /* ctrl+w, delete previous word */ linenoise_edit_delete_prev_word(l); break; + default: + if (linenoise_edit_insert(l, c)) + return NULL; + break; } return linenoiseEditMore; } @@ -847,8 +891,8 @@ t_error linenoise_history_save(t_str name) j = 0; while (j < history->len) { - write(fd->fd, history->buffer[j], str_len(history->buffer[j])); - write(fd->fd, "\n", 1); + write_fd(fd, (t_u8 *)history->buffer[j], str_len(history->buffer[j]), NULL); + write_fd(fd, (t_u8 *)"\n", 1, NULL); j++; } close_fd(fd); diff --git a/sources/main.c b/sources/main.c index 51971ccc..dced5764 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ -/* Updated: 2024/07/07 18:25:43 by maiboyer ### ########.fr */ +/* Updated: 2024/07/08 17:02:15 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -136,7 +136,6 @@ void ft_take_args(t_utils *shcat) free(cmd); shcat->current_node = parse_str(&shcat->parser, shcat->str_input); exec_shcat(shcat); - add_history(shcat->str_input); mem_free(shcat->str_input); } } @@ -159,6 +158,9 @@ void free_myparser(t_parser self) ts_parser_delete(self.parser); } +#define IGN_START "\1" +#define IGN_END "\2" + t_i32 main(t_i32 argc, t_str argv[], t_str envp[]) { t_utils utils; @@ -173,11 +175,11 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[]) utils.env = create_env_map(); if (populate_env(utils.env, envp)) me_abort("Unable to build env hashmap"); - utils.name_shell = "\001\x1B[93m\002" + utils.name_shell = "\x1B[93m" "42sh" - "\001\x1B[32m\002" + "\x1B[32m" ">" - "\001\x1B[0m\002" + "\x1B[0m" "$ "; ft_take_args(&utils); } diff --git a/sources/signal_handler.c b/sources/signal_handler.c index 638a5626..fdb8e4fe 100644 --- a/sources/signal_handler.c +++ b/sources/signal_handler.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */ -/* Updated: 2024/05/23 17:07:03 by maiboyer ### ########.fr */ +/* Updated: 2024/07/08 15:54:21 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,10 @@ void sigint_handle(int sig, siginfo_t *info, void *ucontext) (void)(info); (void)(ucontext); printf("\n"); - rl_replace_line("", 0); - rl_on_new_line(); - rl_redisplay(); + // TODO: change this to the linenoise verison + // rl_replace_line("", 0); + // rl_on_new_line(); + // rl_redisplay(); } void sigquit_handle(int sig, siginfo_t *info, void *ucontext) @@ -34,9 +35,10 @@ void sigquit_handle(int sig, siginfo_t *info, void *ucontext) (void)(info); (void)(ucontext); printf("\n"); - rl_replace_line("", 0); - rl_on_new_line(); - rl_redisplay(); + // + // rl_replace_line("", 0); + // rl_on_new_line(); + // rl_redisplay(); } void sigsegv_handle(int sig, siginfo_t *info, void *ucontext) diff --git a/stdme/src/fs/fs_internal.c b/stdme/src/fs/fs_internal.c index 45068392..dff2b60d 100644 --- a/stdme/src/fs/fs_internal.c +++ b/stdme/src/fs/fs_internal.c @@ -113,8 +113,11 @@ t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options, t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count) { t_isize ret; + t_isize false_ret; - if (fd == NULL || buffer == NULL || read_count == NULL || fd->fd == -1 || + if (read_count == NULL) + read_count = &false_ret; + if (fd == NULL || buffer == NULL || fd->fd == -1 || !(fd->perms & FD_READ)) return (ERROR); ret = read(fd->fd, buffer, size);