Added more string utils and line stuff
This commit is contained in:
parent
13ba83d6af
commit
6b5bc68de9
9 changed files with 229 additions and 151 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/05 18:07:02 by maiboyer #+# #+# */
|
/* Created: 2024/07/05 18:07:02 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/07/05 19:59:47 by maiboyer ### ########.fr */
|
/* Updated: 2024/07/08 22:21:38 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
#define LINE_H
|
#define LINE_H
|
||||||
|
|
||||||
#include "me/fs/fs.h"
|
#include "me/fs/fs.h"
|
||||||
|
#include "me/string/string.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
|
||||||
|
|
@ -31,13 +32,11 @@ struct s_line_state
|
||||||
{
|
{
|
||||||
t_fd *input_fd; /* Terminal stdin file descriptor. */
|
t_fd *input_fd; /* Terminal stdin file descriptor. */
|
||||||
t_fd *output_fd; /* Terminal stdout file descriptor. */
|
t_fd *output_fd; /* Terminal stdout file descriptor. */
|
||||||
t_str buf; /* Edited line buffer. */
|
t_string buf; /* Edited line buffer. */
|
||||||
t_usize buflen; /* Edited line buffer size. */
|
|
||||||
t_const_str prompt; /* Prompt to display. */
|
t_const_str prompt; /* Prompt to display. */
|
||||||
t_usize prompt_len; /* Prompt length. */
|
t_usize prompt_len; /* Prompt length. */
|
||||||
t_usize pos; /* Current cursor position. */
|
t_usize pos; /* Current cursor position. */
|
||||||
t_usize old_pos; /* Previous refresh cursor position. */
|
t_usize old_pos; /* Previous refresh cursor position. */
|
||||||
t_usize len; /* Current edited line length. */
|
|
||||||
t_usize columns; /* Number of columns in terminal. */
|
t_usize columns; /* Number of columns in terminal. */
|
||||||
t_usize old_rows; /* Rows used by last refrehsed line (multiline mode) */
|
t_usize old_rows; /* Rows used by last refrehsed line (multiline mode) */
|
||||||
t_i32 history_index; /* The history index we are currently editing. */
|
t_i32 history_index; /* The history index we are currently editing. */
|
||||||
|
|
|
||||||
173
line/src/line.c
173
line/src/line.c
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */
|
/* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/07/08 19:42:52 by maiboyer ### ########.fr */
|
/* Updated: 2024/07/08 22:23:24 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -142,7 +142,7 @@ t_error linenoise_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_o
|
||||||
buf[i] = '\0';
|
buf[i] = '\0';
|
||||||
|
|
||||||
/* Parse it. */
|
/* Parse it. */
|
||||||
if (buf[0] != K_ESC || buf[1] != '[')
|
if (buf[0] != K_ESC && buf[1] != '[')
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (sscanf(buf + 2, "%d;%d", &rows, &cols) != 2)
|
if (sscanf(buf + 2, "%d;%d", &rows, &cols) != 2)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
|
|
@ -165,14 +165,12 @@ t_u32 linenoise_get_columns(t_fd *input, t_fd *output)
|
||||||
return (80);
|
return (80);
|
||||||
|
|
||||||
/* Go to right margin and get position. */
|
/* Go to right margin and get position. */
|
||||||
me_eprintf("going to the right;\n");
|
|
||||||
me_printf_fd(output, "\x1b[999C");
|
me_printf_fd(output, "\x1b[999C");
|
||||||
if (linenoise_get_cursor_position(input, output, &cols))
|
if (linenoise_get_cursor_position(input, output, &cols))
|
||||||
return (80);
|
return (80);
|
||||||
|
|
||||||
if (cols > start)
|
if (cols > start)
|
||||||
{
|
{
|
||||||
me_eprintf("reseting col;\n");
|
|
||||||
me_printf_fd(output, "\x1b[%dD", cols - start);
|
me_printf_fd(output, "\x1b[%dD", cols - start);
|
||||||
}
|
}
|
||||||
return (cols);
|
return (cols);
|
||||||
|
|
@ -184,7 +182,6 @@ t_u32 linenoise_get_columns(t_fd *input, t_fd *output)
|
||||||
/* Clear the screen. Used to handle ctrl+l */
|
/* Clear the screen. Used to handle ctrl+l */
|
||||||
void linenoise_clear_screen(t_fd *output)
|
void linenoise_clear_screen(t_fd *output)
|
||||||
{
|
{
|
||||||
me_eprintf("clear screen;\n");
|
|
||||||
me_printf_fd(output, "\x1b[H\x1b[2J");
|
me_printf_fd(output, "\x1b[H\x1b[2J");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -293,23 +290,13 @@ t_usize linenoise_get_prompt_len(t_const_str s)
|
||||||
* prompt, just write it, or both. */
|
* prompt, just write it, or both. */
|
||||||
void linenoise_refresh_single_line(t_line_state *l, t_line_flags flags)
|
void linenoise_refresh_single_line(t_line_state *l, t_line_flags flags)
|
||||||
{
|
{
|
||||||
size_t prompt_len = linenoise_get_prompt_len(l->prompt);
|
|
||||||
t_str input_buf = l->buf;
|
|
||||||
size_t cursor_pos = l->pos;
|
|
||||||
t_string str;
|
t_string str;
|
||||||
|
|
||||||
str = string_new(16);
|
str = string_new(64);
|
||||||
string_push(&str, "\r\x1b[2K");
|
string_push(&str, "\r\x1b[2K");
|
||||||
|
|
||||||
if (flags & REFRESH_WRITE)
|
if (flags & REFRESH_WRITE)
|
||||||
{
|
me_printf_str(&str, "%s%s\x1b[0G\x1b[%uC", l->prompt, l->buf.buf, l->pos + linenoise_get_prompt_len(l->prompt));
|
||||||
/* Write the prompt and the current buffer content */
|
|
||||||
string_push(&str, l->prompt);
|
|
||||||
string_push(&str, input_buf); // , len);
|
|
||||||
me_printf_str(&str, "\x1b[0G\x1b[%uC", cursor_pos + prompt_len);
|
|
||||||
}
|
|
||||||
me_printf_fd(l->output_fd, "%s", str.buf);
|
me_printf_fd(l->output_fd, "%s", str.buf);
|
||||||
|
|
||||||
string_free(str);
|
string_free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -343,26 +330,15 @@ void linenoise_show(t_line_state *l)
|
||||||
* On error writing to the terminal -1 is returned, otherwise 0. */
|
* On error writing to the terminal -1 is returned, otherwise 0. */
|
||||||
t_error linenoise_edit_insert(t_line_state *l, char c)
|
t_error linenoise_edit_insert(t_line_state *l, char c)
|
||||||
{
|
{
|
||||||
if (l->len < l->buflen)
|
if (l->pos == l->buf.len)
|
||||||
{
|
{
|
||||||
if (l->len == l->pos)
|
if (string_push_char(&l->buf, c))
|
||||||
{
|
return (ERROR);
|
||||||
l->buf[l->pos] = c;
|
}
|
||||||
|
else if (string_insert_char(&l->buf, l->pos, c))
|
||||||
|
return (ERROR);
|
||||||
l->pos++;
|
l->pos++;
|
||||||
l->len++;
|
|
||||||
l->buf[l->len] = '\0';
|
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
memmove(l->buf + l->pos + 1, l->buf + l->pos, l->len - l->pos);
|
|
||||||
l->buf[l->pos] = c;
|
|
||||||
l->len++;
|
|
||||||
l->pos++;
|
|
||||||
l->buf[l->len] = '\0';
|
|
||||||
linenoise_refresh_line(l);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -379,7 +355,7 @@ void linenoise_edit_move_left(t_line_state *l)
|
||||||
/* Move cursor on the right. */
|
/* Move cursor on the right. */
|
||||||
void linenoise_edit_move_right(t_line_state *l)
|
void linenoise_edit_move_right(t_line_state *l)
|
||||||
{
|
{
|
||||||
if (l->pos != l->len)
|
if (l->pos != l->buf.len)
|
||||||
{
|
{
|
||||||
l->pos++;
|
l->pos++;
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
|
|
@ -387,7 +363,7 @@ void linenoise_edit_move_right(t_line_state *l)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Move cursor to the start of the line. */
|
/* Move cursor to the start of the line. */
|
||||||
void linenoiseEditMoveHome(t_line_state *l)
|
void linenoise_edit_move_home(t_line_state *l)
|
||||||
{
|
{
|
||||||
if (l->pos != 0)
|
if (l->pos != 0)
|
||||||
{
|
{
|
||||||
|
|
@ -399,9 +375,9 @@ void linenoiseEditMoveHome(t_line_state *l)
|
||||||
/* Move cursor to the end of the line. */
|
/* Move cursor to the end of the line. */
|
||||||
void linenoise_edit_move_end(t_line_state *l)
|
void linenoise_edit_move_end(t_line_state *l)
|
||||||
{
|
{
|
||||||
if (l->pos != l->len)
|
if (l->pos != l->buf.len)
|
||||||
{
|
{
|
||||||
l->pos = l->len;
|
l->pos = l->buf.len;
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -421,7 +397,7 @@ void linenoise_edit_history_next(t_line_state *l, int dir)
|
||||||
/* Update the current history entry before to
|
/* Update the current history entry before to
|
||||||
* overwrite it with the next one. */
|
* overwrite it with the next one. */
|
||||||
mem_free(history->buffer[history->len - 1 - l->history_index]);
|
mem_free(history->buffer[history->len - 1 - l->history_index]);
|
||||||
history->buffer[history->len - 1 - l->history_index] = strdup(l->buf);
|
history->buffer[history->len - 1 - l->history_index] = str_clone(l->buf.buf);
|
||||||
/* Show the new entry */
|
/* Show the new entry */
|
||||||
l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
|
l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
|
||||||
if (l->history_index < 0)
|
if (l->history_index < 0)
|
||||||
|
|
@ -434,9 +410,9 @@ void linenoise_edit_history_next(t_line_state *l, int dir)
|
||||||
l->history_index = history->len - 1;
|
l->history_index = history->len - 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
strncpy(l->buf, history->buffer[history->len - 1 - l->history_index], l->buflen);
|
string_clear(&l->buf);
|
||||||
l->buf[l->buflen - 1] = '\0';
|
string_push(&l->buf, history->buffer[history->len - 1 - l->history_index]);
|
||||||
l->len = l->pos = strlen(l->buf);
|
l->pos = l->buf.len;
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -445,45 +421,21 @@ void linenoise_edit_history_next(t_line_state *l, int dir)
|
||||||
* position. Basically this is what happens with the "Delete" keyboard key. */
|
* position. Basically this is what happens with the "Delete" keyboard key. */
|
||||||
void linenoise_edit_delete(t_line_state *l)
|
void linenoise_edit_delete(t_line_state *l)
|
||||||
{
|
{
|
||||||
if (l->len > 0 && l->pos < l->len)
|
string_remove(&l->buf, l->pos, NULL);
|
||||||
{
|
|
||||||
memmove(l->buf + l->pos, l->buf + l->pos + 1, l->len - l->pos - 1);
|
|
||||||
l->len--;
|
|
||||||
l->buf[l->len] = '\0';
|
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Backspace implementation. */
|
/* Backspace implementation. */
|
||||||
void linenoise_edit_backspace(t_line_state *l)
|
void linenoise_edit_backspace(t_line_state *l)
|
||||||
{
|
{
|
||||||
if (l->pos > 0 && l->len > 0)
|
if (l->pos > 0)
|
||||||
{
|
{
|
||||||
memmove(l->buf + l->pos - 1, l->buf + l->pos, l->len - l->pos);
|
string_remove(&l->buf, l->pos - 1, NULL);
|
||||||
l->pos--;
|
l->pos--;
|
||||||
l->len--;
|
|
||||||
l->buf[l->len] = '\0';
|
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Delete the previosu word, maintaining the cursor at the start of the
|
|
||||||
* current word. */
|
|
||||||
void linenoise_edit_delete_prev_word(t_line_state *l)
|
|
||||||
{
|
|
||||||
size_t old_pos = l->pos;
|
|
||||||
size_t diff;
|
|
||||||
|
|
||||||
while (l->pos > 0 && l->buf[l->pos - 1] == ' ')
|
|
||||||
l->pos--;
|
|
||||||
while (l->pos > 0 && l->buf[l->pos - 1] != ' ')
|
|
||||||
l->pos--;
|
|
||||||
diff = old_pos - l->pos;
|
|
||||||
memmove(l->buf + l->pos, l->buf + old_pos, l->len - old_pos + 1);
|
|
||||||
l->len -= diff;
|
|
||||||
linenoise_refresh_line(l);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This function is part of the multiplexed API of Linenoise, that is used
|
/* This function is part of the multiplexed API of Linenoise, that is used
|
||||||
* in order to implement the blocking variant of the API but can also be
|
* in order to implement the blocking variant of the API but can also be
|
||||||
* called by the user directly in an event driven program. It will:
|
* called by the user directly in an event driven program. It will:
|
||||||
|
|
@ -508,18 +460,22 @@ void linenoise_edit_delete_prev_word(t_line_state *l)
|
||||||
* fails. If stdin_fd or stdout_fd are set to -1, the default is to use
|
* fails. If stdin_fd or stdout_fd are set to -1, the default is to use
|
||||||
* STDIN_FILENO and STDOUT_FILENO.
|
* STDIN_FILENO and STDOUT_FILENO.
|
||||||
*/
|
*/
|
||||||
t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, char *buf, size_t buflen, t_const_str prompt)
|
t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, t_string buf, t_const_str prompt)
|
||||||
{
|
{
|
||||||
|
if (stdin_fd == NULL)
|
||||||
|
stdin_fd = get_stdin();
|
||||||
|
if (stdout_fd == NULL)
|
||||||
|
stdout_fd = get_stdout();
|
||||||
|
|
||||||
/* Populate the linenoise state that we pass to functions implementing
|
/* Populate the linenoise state that we pass to functions implementing
|
||||||
* specific editing functionalities. */
|
* specific editing functionalities. */
|
||||||
l->input_fd = stdin_fd;
|
l->input_fd = stdin_fd;
|
||||||
l->output_fd = stdout_fd;
|
l->output_fd = stdout_fd;
|
||||||
l->buf = buf;
|
l->buf = buf;
|
||||||
l->buflen = buflen;
|
|
||||||
l->prompt = prompt;
|
l->prompt = prompt;
|
||||||
l->prompt_len = str_len(l->prompt);
|
l->prompt_len = str_len(l->prompt);
|
||||||
l->old_pos = l->pos = 0;
|
l->old_pos = 0;
|
||||||
l->len = 0;
|
l->pos = 0;
|
||||||
|
|
||||||
// /* Enter raw mode. */
|
// /* Enter raw mode. */
|
||||||
// if (enableRawMode(l->ifd) == -1)
|
// if (enableRawMode(l->ifd) == -1)
|
||||||
|
|
@ -529,15 +485,11 @@ t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, c
|
||||||
l->old_rows = 0;
|
l->old_rows = 0;
|
||||||
l->history_index = 0;
|
l->history_index = 0;
|
||||||
|
|
||||||
/* Buffer starts empty. */
|
|
||||||
l->buf[0] = '\0';
|
|
||||||
l->buflen--; /* Make sure there is always space for the nulterm */
|
|
||||||
|
|
||||||
/* If stdin is not a tty, stop here with the initialization. We
|
/* If stdin is not a tty, stop here with the initialization. We
|
||||||
* will actually just read a line from standard input in blocking
|
* will actually just read a line from standard input in blocking
|
||||||
* mode later, in linenoiseEditFeed(). */
|
* mode later, in linenoiseEditFeed(). */
|
||||||
if (!isatty(l->input_fd->fd))
|
if (!isatty(l->input_fd->fd))
|
||||||
return 0;
|
return (NO_ERROR);
|
||||||
|
|
||||||
/* The latest history entry is always our current buffer, that
|
/* The latest history entry is always our current buffer, that
|
||||||
* initially is just an empty string. */
|
* initially is just an empty string. */
|
||||||
|
|
@ -582,7 +534,7 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
char seq[3];
|
char seq[3];
|
||||||
t_vec_str *history = get_history();
|
t_vec_str *history = get_history();
|
||||||
if (read_fd(l->input_fd, (t_u8 *)&c, 1, &nread))
|
if (read_fd(l->input_fd, (t_u8 *)&c, 1, &nread))
|
||||||
return (printf("null1\n"), NULL);
|
return (NULL);
|
||||||
|
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
|
|
@ -590,7 +542,7 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
case K_ENTER: /* enter */
|
case K_ENTER: /* enter */
|
||||||
history->len--;
|
history->len--;
|
||||||
mem_free(history->buffer[history->len]);
|
mem_free(history->buffer[history->len]);
|
||||||
return strdup(l->buf);
|
return str_clone(l->buf.buf);
|
||||||
case K_CTRL_C: /* ctrl-c */
|
case K_CTRL_C: /* ctrl-c */
|
||||||
errno = EAGAIN;
|
errno = EAGAIN;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -600,7 +552,7 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
break;
|
break;
|
||||||
case K_CTRL_D: /* ctrl-d, remove char at right of cursor, or if the
|
case K_CTRL_D: /* ctrl-d, remove char at right of cursor, or if the
|
||||||
line is empty, act as end-of-file. */
|
line is empty, act as end-of-file. */
|
||||||
if (l->len > 0)
|
if (l->buf.len > 0)
|
||||||
linenoise_edit_delete(l);
|
linenoise_edit_delete(l);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -627,15 +579,9 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
* Use two calls to handle slow terminals returning the two
|
* Use two calls to handle slow terminals returning the two
|
||||||
* chars at different times. */
|
* chars at different times. */
|
||||||
if (read_fd(l->input_fd, (t_u8 *)seq, 1, NULL))
|
if (read_fd(l->input_fd, (t_u8 *)seq, 1, NULL))
|
||||||
{
|
|
||||||
printf("Failed read\n");
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
if (read_fd(l->input_fd, (t_u8 *)(seq + 1), 1, NULL))
|
if (read_fd(l->input_fd, (t_u8 *)(seq + 1), 1, NULL))
|
||||||
{
|
|
||||||
printf("Failed read2\n");
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
/* ESC [ sequences. */
|
/* ESC [ sequences. */
|
||||||
if (seq[0] == '[')
|
if (seq[0] == '[')
|
||||||
|
|
@ -647,6 +593,8 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
break;
|
break;
|
||||||
if (seq[1] == '3' && seq[2] == '~')
|
if (seq[1] == '3' && seq[2] == '~')
|
||||||
linenoise_edit_delete(l);
|
linenoise_edit_delete(l);
|
||||||
|
if (seq[1] == '1' && seq[2] == ';')
|
||||||
|
me_printf("Something");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -665,7 +613,7 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
linenoise_edit_move_left(l);
|
linenoise_edit_move_left(l);
|
||||||
break;
|
break;
|
||||||
case 'H': /* Home */
|
case 'H': /* Home */
|
||||||
linenoiseEditMoveHome(l);
|
linenoise_edit_move_home(l);
|
||||||
break;
|
break;
|
||||||
case 'F': /* End*/
|
case 'F': /* End*/
|
||||||
linenoise_edit_move_end(l);
|
linenoise_edit_move_end(l);
|
||||||
|
|
@ -680,7 +628,7 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
switch (seq[1])
|
switch (seq[1])
|
||||||
{
|
{
|
||||||
case 'H': /* Home */
|
case 'H': /* Home */
|
||||||
linenoiseEditMoveHome(l);
|
linenoise_edit_move_home(l);
|
||||||
break;
|
break;
|
||||||
case 'F': /* End*/
|
case 'F': /* End*/
|
||||||
linenoise_edit_move_end(l);
|
linenoise_edit_move_end(l);
|
||||||
|
|
@ -689,17 +637,16 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case K_CTRL_U: /* Ctrl+u, delete the whole line. */
|
case K_CTRL_U: /* Ctrl+u, delete the whole line. */
|
||||||
l->buf[0] = '\0';
|
string_clear(&l->buf);
|
||||||
l->pos = l->len = 0;
|
l->pos = 0;
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
break;
|
break;
|
||||||
case K_CTRL_K: /* Ctrl+k, delete from current to end of line. */
|
case K_CTRL_K: /* Ctrl+k, delete from current to end of line. */
|
||||||
l->buf[l->pos] = '\0';
|
string_clear_after(&l->buf, l->pos);
|
||||||
l->len = l->pos;
|
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
break;
|
break;
|
||||||
case K_CTRL_A: /* Ctrl+a, go to the start of the line */
|
case K_CTRL_A: /* Ctrl+a, go to the start of the line */
|
||||||
linenoiseEditMoveHome(l);
|
linenoise_edit_move_home(l);
|
||||||
break;
|
break;
|
||||||
case K_CTRL_E: /* ctrl+e, go to the end of the line */
|
case K_CTRL_E: /* ctrl+e, go to the end of the line */
|
||||||
linenoise_edit_move_end(l);
|
linenoise_edit_move_end(l);
|
||||||
|
|
@ -708,12 +655,9 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
linenoise_clear_screen(l->output_fd);
|
linenoise_clear_screen(l->output_fd);
|
||||||
linenoise_refresh_line(l);
|
linenoise_refresh_line(l);
|
||||||
break;
|
break;
|
||||||
case K_CTRL_W: /* ctrl+w, delete previous word */
|
|
||||||
linenoise_edit_delete_prev_word(l);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
if (linenoise_edit_insert(l, c))
|
if (linenoise_edit_insert(l, c))
|
||||||
return NULL;
|
return (printf("lool\n"), NULL);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return linenoiseEditMore;
|
return linenoiseEditMore;
|
||||||
|
|
@ -723,38 +667,35 @@ t_str linenoise_edit_feed(t_line_state *l)
|
||||||
* for more information. This function is called when linenoiseEditFeed()
|
* for more information. This function is called when linenoiseEditFeed()
|
||||||
* returns something different than NULL. At this point the user input
|
* returns something different than NULL. At this point the user input
|
||||||
* is in the buffer, and we can restore the terminal in normal mode. */
|
* is in the buffer, and we can restore the terminal in normal mode. */
|
||||||
void linenoiseEditStop(t_line_state *l)
|
void linenoise_edit_stop(t_line_state *l)
|
||||||
{
|
{
|
||||||
if (!isatty(l->input_fd->fd))
|
if (!isatty(l->input_fd->fd))
|
||||||
return;
|
return;
|
||||||
linenoise_disable_raw_mode(l->input_fd);
|
linenoise_disable_raw_mode(l->input_fd);
|
||||||
me_printf_fd(l->output_fd, "\n");
|
me_printf_fd(l->output_fd, "\n");
|
||||||
|
string_free(l->buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This just implements a blocking loop for the multiplexed API.
|
/* This just implements a blocking loop for the multiplexed API.
|
||||||
* In many applications that are not event-drivern, we can just call
|
* In many applications that are not event-drivern, we can just call
|
||||||
* the blocking linenoise API, wait for the user to complete the editing
|
* the blocking linenoise API, wait for the user to complete the editing
|
||||||
* and return the buffer. */
|
* and return the buffer. */
|
||||||
char *linenoise_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, char *buf, size_t buflen, t_const_str prompt)
|
char *linenoise_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, t_string buf, t_const_str prompt)
|
||||||
{
|
{
|
||||||
t_line_state l;
|
t_line_state l;
|
||||||
|
|
||||||
/* Editing without a buffer is invalid. */
|
linenoise_edit_start(&l, stdin_fd, stdout_fd, buf, prompt);
|
||||||
if (buflen == 0)
|
|
||||||
return (errno = EINVAL, NULL);
|
|
||||||
|
|
||||||
linenoise_edit_start(&l, stdin_fd, stdout_fd, buf, buflen, prompt);
|
|
||||||
char *res;
|
char *res;
|
||||||
while ((res = linenoise_edit_feed(&l)) == linenoiseEditMore)
|
while ((res = linenoise_edit_feed(&l)) == linenoiseEditMore)
|
||||||
; // printf("edit\n");
|
; // printf("edit\n");
|
||||||
linenoiseEditStop(&l);
|
linenoise_edit_stop(&l);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This special mode is used by linenoise in order to print scan codes
|
/* This special mode is used by linenoise in order to print scan codes
|
||||||
* on screen for debugging / development purposes. It is implemented
|
* on screen for debugging / development purposes. It is implemented
|
||||||
* by the linenoise_example program using the --keycodes option. */
|
* by the linenoise_example program using the --keycodes option. */
|
||||||
void linenoisePrintKeyCodes(void)
|
void linenoise_print_key_codes(void)
|
||||||
{
|
{
|
||||||
char quit[4];
|
char quit[4];
|
||||||
|
|
||||||
|
|
@ -762,18 +703,19 @@ void linenoisePrintKeyCodes(void)
|
||||||
"Press keys to see scan codes. Type 'quit' at any time to exit.\n");
|
"Press keys to see scan codes. Type 'quit' at any time to exit.\n");
|
||||||
// if (enableRawMode(STDIN_FILENO) == -1)
|
// if (enableRawMode(STDIN_FILENO) == -1)
|
||||||
// return;
|
// return;
|
||||||
memset(quit, ' ', 4);
|
mem_set(quit, ' ', 4);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
int nread;
|
t_isize nread;
|
||||||
|
|
||||||
nread = read(STDIN_FILENO, &c, 1);
|
if (read_fd(get_stdin(), (void *)&c, 1, &nread))
|
||||||
|
continue;
|
||||||
if (nread <= 0)
|
if (nread <= 0)
|
||||||
continue;
|
continue;
|
||||||
memmove(quit, quit + 1, sizeof(quit) - 1); /* shift string to left. */
|
mem_move(quit, quit + 1, sizeof(quit) - 1); /* shift string to left. */
|
||||||
quit[sizeof(quit) - 1] = c; /* Insert current char on the right. */
|
quit[sizeof(quit) - 1] = c; /* Insert current char on the right. */
|
||||||
if (memcmp(quit, "quit", sizeof(quit)) == 0)
|
if (mem_compare(quit, "quit", sizeof(quit)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c, (int)c);
|
printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c, (int)c);
|
||||||
|
|
@ -812,20 +754,17 @@ t_str linenoise_no_tty_impl(void)
|
||||||
* for a blacklist of stupid terminals, and later either calls the line
|
* for a blacklist of stupid terminals, and later either calls the line
|
||||||
* editing function or uses dummy fgets() so that you will be able to type
|
* editing function or uses dummy fgets() so that you will be able to type
|
||||||
* something even in the most desperate of the conditions. */
|
* something even in the most desperate of the conditions. */
|
||||||
t_str linenoise(const char *prompt)
|
t_str linenoise(t_const_str prompt)
|
||||||
{
|
{
|
||||||
char buf[4096];
|
|
||||||
t_str retval;
|
t_str retval;
|
||||||
|
|
||||||
if (!isatty(get_stdin()->fd))
|
if (!isatty(get_stdin()->fd))
|
||||||
{
|
{
|
||||||
printf("not_tty\n");
|
|
||||||
/* Not a tty: read from file / pipe. In this mode we don't want any
|
/* Not a tty: read from file / pipe. In this mode we don't want any
|
||||||
* limit to the line size, so we call a function to handle that. */
|
* limit to the line size, so we call a function to handle that. */
|
||||||
return linenoise_no_tty_impl();
|
return linenoise_no_tty_impl();
|
||||||
}
|
}
|
||||||
printf("is_tty\n");
|
retval = linenoise_blocking_edit(get_stdin(), get_stdout(), string_new(1024), prompt);
|
||||||
retval = linenoise_blocking_edit(get_stdin(), get_stdout(), buf, 4096, prompt);
|
|
||||||
return (retval);
|
return (retval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/07/08 17:02:15 by maiboyer ### ########.fr */
|
/* Updated: 2024/07/08 21:11:43 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -132,8 +132,8 @@ void ft_take_args(t_utils *shcat)
|
||||||
cmd = linenoise((t_const_str)shcat->name_shell);
|
cmd = linenoise((t_const_str)shcat->name_shell);
|
||||||
if (cmd == NULL)
|
if (cmd == NULL)
|
||||||
ft_exit(shcat, 0);
|
ft_exit(shcat, 0);
|
||||||
shcat->str_input = str_clone(cmd);
|
shcat->str_input = cmd;
|
||||||
free(cmd);
|
linenoise_history_add(shcat->str_input);
|
||||||
shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
|
shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
|
||||||
exec_shcat(shcat);
|
exec_shcat(shcat);
|
||||||
mem_free(shcat->str_input);
|
mem_free(shcat->str_input);
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,14 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */
|
/* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/07/08 15:54:21 by maiboyer ### ########.fr */
|
/* Updated: 2024/07/08 21:19:35 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "app/signal_handler.h"
|
#include "app/signal_handler.h"
|
||||||
|
#include "me/fs/fs.h"
|
||||||
#include "me/printf/printf.h"
|
#include "me/printf/printf.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include "readline/readline.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ void sigint_handle(int sig, siginfo_t *info, void *ucontext)
|
||||||
(void)(sig);
|
(void)(sig);
|
||||||
(void)(info);
|
(void)(info);
|
||||||
(void)(ucontext);
|
(void)(ucontext);
|
||||||
printf("\n");
|
write_fd(get_stdout(), (void *)"\n", 1, NULL);
|
||||||
// TODO: change this to the linenoise verison
|
// TODO: change this to the linenoise verison
|
||||||
// rl_replace_line("", 0);
|
// rl_replace_line("", 0);
|
||||||
// rl_on_new_line();
|
// rl_on_new_line();
|
||||||
|
|
@ -34,8 +34,7 @@ void sigquit_handle(int sig, siginfo_t *info, void *ucontext)
|
||||||
(void)(sig);
|
(void)(sig);
|
||||||
(void)(info);
|
(void)(info);
|
||||||
(void)(ucontext);
|
(void)(ucontext);
|
||||||
printf("\n");
|
write_fd(get_stdout(), (void *)"\n", 1, NULL);
|
||||||
//
|
|
||||||
// rl_replace_line("", 0);
|
// rl_replace_line("", 0);
|
||||||
// rl_on_new_line();
|
// rl_on_new_line();
|
||||||
// rl_redisplay();
|
// rl_redisplay();
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/16 17:54:28 by maiboyer #+# #+# */
|
/* Created: 2023/11/16 17:54:28 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/04/30 14:14:42 by maiboyer ### ########.fr */
|
/* Updated: 2024/07/08 21:58:11 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef STRING_H
|
#ifndef STRING_H
|
||||||
# define STRING_H
|
#define STRING_H
|
||||||
# include "me/types.h"
|
#include "me/types.h"
|
||||||
|
|
||||||
typedef struct s_string
|
typedef struct s_string
|
||||||
{
|
{
|
||||||
|
|
@ -27,7 +27,6 @@ typedef struct s_string
|
||||||
/// @return true if it failed, false otherwise
|
/// @return true if it failed, false otherwise
|
||||||
t_error string_push(t_string *buf, t_const_str to_push);
|
t_error string_push(t_string *buf, t_const_str to_push);
|
||||||
|
|
||||||
|
|
||||||
/// @brief Push a character to a buffer
|
/// @brief Push a character to a buffer
|
||||||
/// @param buf the string to be pushed to
|
/// @param buf the string to be pushed to
|
||||||
/// @param to_push the character to push
|
/// @param to_push the character to push
|
||||||
|
|
@ -74,4 +73,33 @@ static inline char string_pop(t_string *buf)
|
||||||
return (c);
|
return (c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Insert a string into self
|
||||||
|
/// @param self the string to insert into
|
||||||
|
/// @param pos the index to start inserting at
|
||||||
|
/// @param str the string to insert
|
||||||
|
/// @return Error in case pos was over the string length, self was equal to NULL
|
||||||
|
/// or str was equal to NULL
|
||||||
|
t_error string_insert(t_string *self, t_usize pos, t_str str);
|
||||||
|
|
||||||
|
/// @brief Insert a char into self
|
||||||
|
/// @param self the string to insert into
|
||||||
|
/// @param pos the index to start inserting at
|
||||||
|
/// @param str the character to insert
|
||||||
|
/// @return Error in case pos was over the string length, self was equal to NULL
|
||||||
|
/// or chr was '\0'
|
||||||
|
t_error string_insert_char(t_string *self, t_usize pos, char chr);
|
||||||
|
|
||||||
|
/// @brief Clear the string, keeping everything before `pos`
|
||||||
|
/// @param self the string to operate on
|
||||||
|
/// @param pos the position to start remove from
|
||||||
|
/// @return returns an Error if self is null or pos is after the string length
|
||||||
|
t_error string_clear_after(t_string *self, t_usize pos);
|
||||||
|
|
||||||
|
/// @brief Remove a single character from the string, putting it in `out`
|
||||||
|
/// @param self the string to operate on
|
||||||
|
/// @param pos the position to start remove
|
||||||
|
/// @param out[out] the place to put the removed character, if any. Can be NULL
|
||||||
|
/// @return returns an Error if self is null or pos is after the string length
|
||||||
|
t_error string_remove(t_string *self, t_usize pos, char *out);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -106,3 +106,6 @@ str/str_split
|
||||||
str/str_substring
|
str/str_substring
|
||||||
str/str_trim
|
str/str_trim
|
||||||
string/mod
|
string/mod
|
||||||
|
string/string_insert
|
||||||
|
string/string_remove
|
||||||
|
string/string_reserve
|
||||||
|
|
|
||||||
44
stdme/src/string/string_insert.c
Normal file
44
stdme/src/string/string_insert.c
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* string_insert.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/08 21:32:17 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/07/08 22:25:52 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/mem/mem.h"
|
||||||
|
#include "me/str/str.h"
|
||||||
|
#include "me/string/string.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
|
||||||
|
t_error string_insert(t_string *self, t_usize pos, t_str str)
|
||||||
|
{
|
||||||
|
t_usize len;
|
||||||
|
if (self == NULL || str == NULL || pos > self->len)
|
||||||
|
return (ERROR);
|
||||||
|
if (pos == self->len)
|
||||||
|
return (string_push(self, str));
|
||||||
|
len = str_len(str);
|
||||||
|
if (string_reserve(self, self->len + len))
|
||||||
|
return (ERROR);
|
||||||
|
mem_move(&self->buf[pos + len], &self->buf[pos], self->len - pos);
|
||||||
|
mem_copy(&self->buf[pos], str, len);
|
||||||
|
self->len += len;
|
||||||
|
self->buf[self->len] = '\0';
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error string_insert_char(t_string *self, t_usize pos, char chr)
|
||||||
|
{
|
||||||
|
char tmp[2];
|
||||||
|
|
||||||
|
if (chr == '\0')
|
||||||
|
return (ERROR);
|
||||||
|
tmp[0] = chr;
|
||||||
|
tmp[1] = '\0';
|
||||||
|
return (string_insert(self, pos, tmp));
|
||||||
|
}
|
||||||
41
stdme/src/string/string_remove.c
Normal file
41
stdme/src/string/string_remove.c
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* string_remove.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/08 21:50:27 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/07/08 21:54:45 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/mem/mem.h"
|
||||||
|
#include "me/str/str.h"
|
||||||
|
#include "me/string/string.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
|
||||||
|
t_error string_remove(t_string *self, t_usize pos, char *out)
|
||||||
|
{
|
||||||
|
char fake_out;
|
||||||
|
|
||||||
|
if (out == NULL)
|
||||||
|
out = &fake_out;
|
||||||
|
if (self == NULL || pos >= self->len)
|
||||||
|
return (ERROR);
|
||||||
|
*out = self->buf[pos];
|
||||||
|
mem_move(&self->buf[pos], &self->buf[pos] + 1, self->len - pos);
|
||||||
|
self->len--;
|
||||||
|
self->buf[self->len] = '\0';
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error string_clear_after(t_string *self, t_usize pos)
|
||||||
|
{
|
||||||
|
if (self == NULL || pos >= self->len)
|
||||||
|
return (ERROR);
|
||||||
|
mem_set_zero(&self->buf[pos], self->len - pos);
|
||||||
|
self->len = pos;
|
||||||
|
self->buf[self->len] = '\0';
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
25
stdme/src/string/string_reserve.c
Normal file
25
stdme/src/string/string_reserve.c
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* string_reserve.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/08 22:02:49 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/07/08 22:04:49 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/mem/mem.h"
|
||||||
|
#include "me/string/string.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
|
||||||
|
t_error string_reserve(t_string *self, t_usize capacity)
|
||||||
|
{
|
||||||
|
if (self == NULL)
|
||||||
|
return (ERROR);
|
||||||
|
if (self->capacity >= capacity)
|
||||||
|
return (NO_ERROR);
|
||||||
|
self->buf = mem_realloc(self->buf, capacity);
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue