Updated linenoise and printf to better work
This commit is contained in:
parent
0e18e20181
commit
8808f81221
28 changed files with 657 additions and 579 deletions
524
line/src/line.c
524
line/src/line.c
|
|
@ -1,3 +1,14 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 19:22:53 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
|
@ -20,8 +31,8 @@
|
|||
#include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
|
||||
static t_str linenoiseNoTTY(void);
|
||||
static void refreshLineWithFlags(t_line_state *l, int flags);
|
||||
t_raw_mode_state *get_raw_mode_state(void);
|
||||
t_vec_str *get_history(void);
|
||||
|
||||
t_vec_str *get_history(void)
|
||||
{
|
||||
|
|
@ -36,75 +47,92 @@ t_vec_str *get_history(void)
|
|||
return (&history);
|
||||
}
|
||||
|
||||
t_raw_mode_state *get_raw_mode_state(void)
|
||||
{
|
||||
static t_raw_mode_state state = {};
|
||||
|
||||
return (&state);
|
||||
}
|
||||
|
||||
enum e_key_action
|
||||
{
|
||||
KEY_NULL = 0, /* NULL */
|
||||
CTRL_A = 1, /* Ctrl+a */
|
||||
CTRL_B = 2, /* Ctrl-b */
|
||||
CTRL_C = 3, /* Ctrl-c */
|
||||
CTRL_D = 4, /* Ctrl-d */
|
||||
CTRL_E = 5, /* Ctrl-e */
|
||||
CTRL_F = 6, /* Ctrl-f */
|
||||
CTRL_H = 8, /* Ctrl-h */
|
||||
TAB = 9, /* Tab */
|
||||
CTRL_K = 11, /* Ctrl+k */
|
||||
CTRL_L = 12, /* Ctrl+l */
|
||||
ENTER = 13, /* Enter */
|
||||
CTRL_N = 14, /* Ctrl-n */
|
||||
CTRL_P = 16, /* Ctrl-p */
|
||||
CTRL_T = 20, /* Ctrl-t */
|
||||
CTRL_U = 21, /* Ctrl+u */
|
||||
CTRL_W = 23, /* Ctrl+w */
|
||||
ESC = 27, /* Escape */
|
||||
BACKSPACE = 127 /* Backspace */
|
||||
K_KEY_NULL = 0, /* NULL */
|
||||
K_CTRL_A = 1, /* Ctrl+a */
|
||||
K_CTRL_B = 2, /* Ctrl-b */
|
||||
K_CTRL_C = 3, /* Ctrl-c */
|
||||
K_CTRL_D = 4, /* Ctrl-d */
|
||||
K_CTRL_E = 5, /* Ctrl-e */
|
||||
K_CTRL_F = 6, /* Ctrl-f */
|
||||
K_CTRL_H = 8, /* Ctrl-h */
|
||||
K_TAB = 9, /* Tab */
|
||||
K_CTRL_K = 11, /* Ctrl+k */
|
||||
K_CTRL_L = 12, /* Ctrl+l */
|
||||
K_ENTER = 13, /* Enter */
|
||||
K_CTRL_N = 14, /* Ctrl-n */
|
||||
K_CTRL_P = 16, /* Ctrl-p */
|
||||
K_CTRL_T = 20, /* Ctrl-t */
|
||||
K_CTRL_U = 21, /* Ctrl+u */
|
||||
K_CTRL_W = 23, /* Ctrl+w */
|
||||
K_ESC = 27, /* Escape */
|
||||
K_BACKSPACE = 127 /* Backspace */
|
||||
};
|
||||
|
||||
#define REFRESH_CLEAN (1 << 0) // Clean the old prompt from the screen
|
||||
#define REFRESH_WRITE (1 << 1) // Rewrite the prompt on the screen.
|
||||
#define REFRESH_ALL (REFRESH_CLEAN | REFRESH_WRITE) // Do both.
|
||||
enum e_line_flags
|
||||
{
|
||||
REFRESH_CLEAN = 1 << 0, // Clean the old prompt from the screen
|
||||
REFRESH_WRITE = 1 << 1, // Rewrite the prompt on the screen.
|
||||
REFRESH_ALL = REFRESH_CLEAN | REFRESH_WRITE, // Do both.
|
||||
};
|
||||
|
||||
int linenoiseHistoryAdd(const char *line);
|
||||
static void linenoise_uninit_lib(void);
|
||||
static void refreshLine(t_line_state *l);
|
||||
typedef enum e_line_flags t_line_flags;
|
||||
|
||||
/* Debugging macro. */
|
||||
#if 0
|
||||
FILE *lndebug_fp = NULL;
|
||||
# define lndebug(...) \
|
||||
do \
|
||||
{ \
|
||||
if (lndebug_fp == NULL) \
|
||||
{ \
|
||||
lndebug_fp = fopen("/tmp/lndebug.txt", "a"); \
|
||||
fprintf(lndebug_fp, "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", (int)l->len, (int)l->pos, (int)l->oldpos, \
|
||||
plen, rows, rpos, (int)l->oldrows, old_rows); \
|
||||
} \
|
||||
fprintf(lndebug_fp, ", " __VA_ARGS__); \
|
||||
fflush(lndebug_fp); \
|
||||
} while (0)
|
||||
#else
|
||||
# define lndebug(fmt, ...)
|
||||
#endif
|
||||
void linenoise_clear_screen(t_fd *output);
|
||||
void linenoise_disable_raw_mode(t_fd *fd);
|
||||
t_error linenoise_enable_raw_mode(t_fd *fd);
|
||||
t_u32 linenoise_get_columns(t_fd *input, t_fd *output);
|
||||
t_error linenoise_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_out);
|
||||
t_error linenoise_history_add(t_const_str line);
|
||||
t_str linenoise_no_tty_impl(void);
|
||||
void linenoise_refresh_line(t_line_state *l);
|
||||
void linenoise_refresh_line_with_flags(t_line_state *l, t_line_flags flags);
|
||||
void linenoise_uninit_lib(void);
|
||||
|
||||
/* Free the history, but does not reset it. Only used when we have to
|
||||
* exit() to avoid memory leaks are reported by valgrind & co. */
|
||||
void free_history(void)
|
||||
{
|
||||
t_vec_str *history = get_history();
|
||||
vec_str_free(*history);
|
||||
}
|
||||
|
||||
/* At exit we'll try to fix the terminal to the initial conditions. */
|
||||
__attribute__((destructor)) void linenoise_uninit_lib(void)
|
||||
{
|
||||
linenoise_disable_raw_mode(get_stdin());
|
||||
free_history();
|
||||
}
|
||||
|
||||
#define lndebug(fmt, ...)
|
||||
|
||||
/* ======================= Low level terminal handling ====================== */
|
||||
|
||||
/* Use the ESC [6n escape sequence to query the horizontal cursor position
|
||||
* and return it. On error -1 is returned, on success the position of the
|
||||
* cursor. */
|
||||
static int getCursorPosition(int ifd, int ofd)
|
||||
t_error linenoise_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_out)
|
||||
{
|
||||
char buf[32];
|
||||
int cols, rows;
|
||||
unsigned int i = 0;
|
||||
|
||||
/* Report cursor location */
|
||||
if (write(ofd, "\x1b[6n", 4) != 4)
|
||||
return -1;
|
||||
if (write(output->fd, "\x1b[6n", 4) != 4)
|
||||
return (ERROR);
|
||||
|
||||
/* Read the response: ESC [ rows ; cols R */
|
||||
while (i < sizeof(buf) - 1)
|
||||
{
|
||||
if (read(ifd, buf + i, 1) != 1)
|
||||
if (read(input->fd, buf + i, 1) != 1)
|
||||
break;
|
||||
if (buf[i] == 'R')
|
||||
break;
|
||||
|
|
@ -113,77 +141,63 @@ static int getCursorPosition(int ifd, int ofd)
|
|||
buf[i] = '\0';
|
||||
|
||||
/* Parse it. */
|
||||
if (buf[0] != ESC || buf[1] != '[')
|
||||
return -1;
|
||||
if (buf[0] != K_ESC || buf[1] != '[')
|
||||
return (ERROR);
|
||||
if (sscanf(buf + 2, "%d;%d", &rows, &cols) != 2)
|
||||
return -1;
|
||||
return cols;
|
||||
return (ERROR);
|
||||
return (*column_out = cols, NO_ERROR);
|
||||
}
|
||||
|
||||
/* Try to get the number of columns in the current terminal, or assume 80
|
||||
* if it fails. */
|
||||
static t_u32 getColumns(int ifd, int ofd)
|
||||
t_u32 linenoise_get_columns(t_fd *input, t_fd *output)
|
||||
{
|
||||
struct winsize ws;
|
||||
t_u32 start, cols;
|
||||
t_u32 cols;
|
||||
t_u32 start;
|
||||
|
||||
if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0)
|
||||
{
|
||||
/* ioctl() failed. Try to query the terminal itself. */
|
||||
|
||||
/* Get the initial position so we can restore it later. */
|
||||
start = getCursorPosition(ifd, ofd);
|
||||
if (start == -1)
|
||||
return 80;
|
||||
if (linenoise_get_cursor_position(input, output, &start))
|
||||
return (80);
|
||||
|
||||
/* Go to right margin and get position. */
|
||||
if (write(ofd, "\x1b[999C", 6) != 6)
|
||||
return 80;
|
||||
cols = getCursorPosition(ifd, ofd);
|
||||
if (cols == -1)
|
||||
return 80;
|
||||
me_eprintf("going to the right;\n");
|
||||
me_printf_fd(output, "\x1b[999C");
|
||||
if (linenoise_get_cursor_position(input, output, &cols))
|
||||
return (80);
|
||||
|
||||
/* Restore position. */
|
||||
if (cols > start)
|
||||
{
|
||||
t_u8 seq[32];
|
||||
me_printf(seq, 32, "\x1b[%dD", cols - start);
|
||||
(void)!(write(ofd, seq, strlen(seq)) == -1);
|
||||
me_eprintf("reseting col;\n");
|
||||
me_printf_fd(output, "\x1b[%dD", cols - start);
|
||||
}
|
||||
return cols;
|
||||
return (cols);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ws.ws_col;
|
||||
}
|
||||
|
||||
failed:
|
||||
return (ws.ws_col);
|
||||
}
|
||||
|
||||
/* Clear the screen. Used to handle ctrl+l */
|
||||
void linenoiseClearScreen(void)
|
||||
void linenoise_clear_screen(t_fd *output)
|
||||
{
|
||||
(void)!write(STDOUT_FILENO, "\x1b[H\x1b[2J", 7);
|
||||
}
|
||||
|
||||
t_raw_mode_state *get_raw_mode_state(void)
|
||||
{
|
||||
static t_raw_mode_state state = {};
|
||||
|
||||
return (&state);
|
||||
me_eprintf("clear screen;\n");
|
||||
me_printf_fd(output, "\x1b[H\x1b[2J");
|
||||
}
|
||||
|
||||
/* Raw mode: 1960 magic shit. */
|
||||
static t_error enableRawMode(int fd)
|
||||
t_error linenoise_enable_raw_mode(t_fd *fd)
|
||||
{
|
||||
struct termios raw;
|
||||
t_raw_mode_state *raw_state;
|
||||
|
||||
raw_state = get_raw_mode_state();
|
||||
|
||||
if (!isatty(STDIN_FILENO))
|
||||
if (!isatty(fd->fd))
|
||||
return (errno = ENOTTY, ERROR);
|
||||
if (tcgetattr(fd, &raw_state->state) == -1)
|
||||
if (tcgetattr(fd->fd, &raw_state->state) == -1)
|
||||
return (errno = ENOTTY, ERROR);
|
||||
|
||||
raw = raw_state->state; /* modify the original mode */
|
||||
|
|
@ -203,18 +217,18 @@ static t_error enableRawMode(int fd)
|
|||
raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
|
||||
|
||||
/* put terminal in raw mode after flushing */
|
||||
if (tcsetattr(fd, TCSAFLUSH, &raw) < 0)
|
||||
if (tcsetattr(fd->fd, TCSAFLUSH, &raw) < 0)
|
||||
return (errno = ENOTTY, ERROR);
|
||||
raw_state->enabled = true;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
static void disableRawMode(int fd)
|
||||
void linenoise_disable_raw_mode(t_fd *fd)
|
||||
{
|
||||
t_raw_mode_state *state;
|
||||
|
||||
state = get_raw_mode_state();
|
||||
if (state->enabled && tcsetattr(fd, TCSAFLUSH, &state->state) != -1)
|
||||
if (state->enabled && tcsetattr(fd->fd, TCSAFLUSH, &state->state) != -1)
|
||||
state->enabled = false;
|
||||
}
|
||||
|
||||
|
|
@ -224,33 +238,6 @@ static void disableRawMode(int fd)
|
|||
* allocated string where we can append to. This is useful in order to
|
||||
* write all the escape sequences in a buffer and flush them to the standard
|
||||
* output in a single call, to avoid flickering effects. */
|
||||
struct abuf
|
||||
{
|
||||
char *b;
|
||||
int len;
|
||||
};
|
||||
|
||||
static void abInit(struct abuf *ab)
|
||||
{
|
||||
ab->b = NULL;
|
||||
ab->len = 0;
|
||||
}
|
||||
|
||||
static void abAppend(struct abuf *ab, const char *s, int len)
|
||||
{
|
||||
char *new = mem_realloc(ab->b, ab->len + len);
|
||||
|
||||
if (new == NULL)
|
||||
return;
|
||||
memcpy(new + ab->len, s, len);
|
||||
ab->b = new;
|
||||
ab->len += len;
|
||||
}
|
||||
|
||||
static void abFree(struct abuf *ab)
|
||||
{
|
||||
mem_free(ab->b);
|
||||
}
|
||||
|
||||
/* Single line low level line refresh.
|
||||
*
|
||||
|
|
@ -259,85 +246,76 @@ static void abFree(struct abuf *ab)
|
|||
*
|
||||
* Flags is REFRESH_* macros. The function can just remove the old
|
||||
* prompt, just write it, or both. */
|
||||
static void refreshSingleLine(struct s_line_state *l, int flags)
|
||||
void linenoise_refresh_single_line(t_line_state *l, t_line_flags flags)
|
||||
{
|
||||
char seq[64];
|
||||
size_t plen = strlen(l->prompt);
|
||||
int fd = l->output_fd;
|
||||
char *buf = l->buf;
|
||||
size_t len = l->len;
|
||||
size_t pos = l->pos;
|
||||
struct abuf ab;
|
||||
size_t prompt_len = strlen(l->prompt);
|
||||
t_str input_buf = l->buf;
|
||||
size_t input_len = l->len;
|
||||
size_t cursor_pos = l->pos;
|
||||
t_string str;
|
||||
|
||||
while ((plen + pos) >= l->columns)
|
||||
while ((prompt_len + cursor_pos) >= l->columns)
|
||||
{
|
||||
buf++;
|
||||
len--;
|
||||
pos--;
|
||||
}
|
||||
while (plen + len > l->columns)
|
||||
{
|
||||
len--;
|
||||
input_buf++;
|
||||
input_len--;
|
||||
cursor_pos--;
|
||||
}
|
||||
while (prompt_len + input_len > l->columns)
|
||||
input_len--;
|
||||
|
||||
abInit(&ab);
|
||||
/* Cursor to left edge */
|
||||
snprintf(seq, sizeof(seq), "\r");
|
||||
abAppend(&ab, seq, strlen(seq));
|
||||
str = string_new(16);
|
||||
string_push_char(&str, '\r');
|
||||
|
||||
if (flags & REFRESH_WRITE)
|
||||
{
|
||||
/* Write the prompt and the current buffer content */
|
||||
abAppend(&ab, l->prompt, strlen(l->prompt));
|
||||
abAppend(&ab, buf, len);
|
||||
string_push(&str, l->prompt);
|
||||
string_push(&str, input_buf); // , len);
|
||||
}
|
||||
string_push(&str, "\x1b[0K");
|
||||
|
||||
/* Erase to right */
|
||||
snprintf(seq, sizeof(seq), "\x1b[0K");
|
||||
abAppend(&ab, seq, strlen(seq));
|
||||
|
||||
if (flags & REFRESH_WRITE)
|
||||
{
|
||||
/* Move cursor to original position. */
|
||||
snprintf(seq, sizeof(seq), "\r\x1b[%dC", (int)(pos + plen));
|
||||
abAppend(&ab, seq, strlen(seq));
|
||||
// TODO: finish this line with me_printf_str
|
||||
me_printf_str(&str, "\r\x1b[%dC", (int)(cursor_pos + prompt_len));
|
||||
}
|
||||
|
||||
if (write(fd, ab.b, ab.len) == -1)
|
||||
{
|
||||
} /* Can't recover from write error. */
|
||||
abFree(&ab);
|
||||
me_eprintf("refresh single line;\n");
|
||||
me_printf_fd(l->output_fd, "%s", str.buf);
|
||||
string_free(str);
|
||||
}
|
||||
|
||||
/* Calls the two low level functions refreshSingleLine() or
|
||||
* refreshMultiLine() according to the selected mode. */
|
||||
static void refreshLineWithFlags(struct s_line_state *l, int flags)
|
||||
void linenoise_refresh_line_with_flags(t_line_state *l, t_line_flags flags)
|
||||
{
|
||||
refreshSingleLine(l, flags);
|
||||
linenoise_refresh_single_line(l, flags);
|
||||
}
|
||||
|
||||
/* Utility function to avoid specifying REFRESH_ALL all the times. */
|
||||
static void refreshLine(struct s_line_state *l)
|
||||
void linenoise_refresh_line(t_line_state *l)
|
||||
{
|
||||
refreshLineWithFlags(l, REFRESH_ALL);
|
||||
linenoise_refresh_line_with_flags(l, REFRESH_ALL);
|
||||
}
|
||||
|
||||
/* Hide the current line, when using the multiplexing API. */
|
||||
void linenoiseHide(struct s_line_state *l)
|
||||
void linenoise_hide(t_line_state *l)
|
||||
{
|
||||
refreshSingleLine(l, REFRESH_CLEAN);
|
||||
linenoise_refresh_single_line(l, REFRESH_CLEAN);
|
||||
}
|
||||
|
||||
/* Show the current line, when using the multiplexing API. */
|
||||
void linenoiseShow(struct s_line_state *l)
|
||||
void linenoise_show(t_line_state *l)
|
||||
{
|
||||
refreshLineWithFlags(l, REFRESH_WRITE);
|
||||
linenoise_refresh_line_with_flags(l, REFRESH_WRITE);
|
||||
}
|
||||
|
||||
/* Insert the character 'c' at cursor current position.
|
||||
*
|
||||
* On error writing to the terminal -1 is returned, otherwise 0. */
|
||||
int linenoiseEditInsert(struct s_line_state *l, char c)
|
||||
t_error linenoise_edit_insert(t_line_state *l, char c)
|
||||
{
|
||||
if (l->len < l->buflen)
|
||||
{
|
||||
|
|
@ -347,7 +325,7 @@ int linenoiseEditInsert(struct s_line_state *l, char c)
|
|||
l->pos++;
|
||||
l->len++;
|
||||
l->buf[l->len] = '\0';
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -356,49 +334,49 @@ int linenoiseEditInsert(struct s_line_state *l, char c)
|
|||
l->len++;
|
||||
l->pos++;
|
||||
l->buf[l->len] = '\0';
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
/* Move cursor on the left. */
|
||||
void linenoiseEditMoveLeft(struct s_line_state *l)
|
||||
void linenoise_edit_move_left(t_line_state *l)
|
||||
{
|
||||
if (l->pos > 0)
|
||||
{
|
||||
l->pos--;
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
|
||||
/* Move cursor on the right. */
|
||||
void linenoiseEditMoveRight(struct s_line_state *l)
|
||||
void linenoise_edit_move_right(t_line_state *l)
|
||||
{
|
||||
if (l->pos != l->len)
|
||||
{
|
||||
l->pos++;
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
|
||||
/* Move cursor to the start of the line. */
|
||||
void linenoiseEditMoveHome(struct s_line_state *l)
|
||||
void linenoiseEditMoveHome(t_line_state *l)
|
||||
{
|
||||
if (l->pos != 0)
|
||||
{
|
||||
l->pos = 0;
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
|
||||
/* Move cursor to the end of the line. */
|
||||
void linenoiseEditMoveEnd(struct s_line_state *l)
|
||||
void linenoise_edit_move_end(t_line_state *l)
|
||||
{
|
||||
if (l->pos != l->len)
|
||||
{
|
||||
l->pos = l->len;
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -406,7 +384,7 @@ void linenoiseEditMoveEnd(struct s_line_state *l)
|
|||
* entry as specified by 'dir'. */
|
||||
#define LINENOISE_HISTORY_NEXT 0
|
||||
#define LINENOISE_HISTORY_PREV 1
|
||||
void linenoiseEditHistoryNext(struct s_line_state *l, int dir)
|
||||
void linenoise_edit_history_next(t_line_state *l, int dir)
|
||||
{
|
||||
t_vec_str *history;
|
||||
|
||||
|
|
@ -425,7 +403,7 @@ void linenoiseEditHistoryNext(struct s_line_state *l, int dir)
|
|||
l->history_index = 0;
|
||||
return;
|
||||
}
|
||||
else if (l->history_index >= history->len)
|
||||
else if ((t_usize)l->history_index >= history->len)
|
||||
{
|
||||
l->history_index = history->len - 1;
|
||||
return;
|
||||
|
|
@ -433,25 +411,25 @@ void linenoiseEditHistoryNext(struct s_line_state *l, int dir)
|
|||
strncpy(l->buf, history->buffer[history->len - 1 - l->history_index], l->buflen);
|
||||
l->buf[l->buflen - 1] = '\0';
|
||||
l->len = l->pos = strlen(l->buf);
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
|
||||
/* Delete the character at the right of the cursor without altering the cursor
|
||||
* position. Basically this is what happens with the "Delete" keyboard key. */
|
||||
void linenoiseEditDelete(struct s_line_state *l)
|
||||
void linenoise_edit_delete(t_line_state *l)
|
||||
{
|
||||
if (l->len > 0 && l->pos < l->len)
|
||||
{
|
||||
memmove(l->buf + l->pos, l->buf + l->pos + 1, l->len - l->pos - 1);
|
||||
l->len--;
|
||||
l->buf[l->len] = '\0';
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
|
||||
/* Backspace implementation. */
|
||||
void linenoiseEditBackspace(struct s_line_state *l)
|
||||
void linenoise_edit_backspace(t_line_state *l)
|
||||
{
|
||||
if (l->pos > 0 && l->len > 0)
|
||||
{
|
||||
|
|
@ -459,13 +437,13 @@ void linenoiseEditBackspace(struct s_line_state *l)
|
|||
l->pos--;
|
||||
l->len--;
|
||||
l->buf[l->len] = '\0';
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
}
|
||||
|
||||
/* Delete the previosu word, maintaining the cursor at the start of the
|
||||
* current word. */
|
||||
void linenoiseEditDeletePrevWord(struct s_line_state *l)
|
||||
void linenoise_edit_delete_prev_word(t_line_state *l)
|
||||
{
|
||||
size_t old_pos = l->pos;
|
||||
size_t diff;
|
||||
|
|
@ -477,7 +455,7 @@ void linenoiseEditDeletePrevWord(struct s_line_state *l)
|
|||
diff = old_pos - l->pos;
|
||||
memmove(l->buf + l->pos, l->buf + old_pos, l->len - old_pos + 1);
|
||||
l->len -= diff;
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
}
|
||||
|
||||
/* This function is part of the multiplexed API of Linenoise, that is used
|
||||
|
|
@ -504,25 +482,24 @@ void linenoiseEditDeletePrevWord(struct s_line_state *l)
|
|||
* fails. If stdin_fd or stdout_fd are set to -1, the default is to use
|
||||
* STDIN_FILENO and STDOUT_FILENO.
|
||||
*/
|
||||
int linenoiseEditStart(struct s_line_state *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt)
|
||||
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)
|
||||
{
|
||||
/* Populate the linenoise state that we pass to functions implementing
|
||||
* specific editing functionalities. */
|
||||
l->in_completion = 0;
|
||||
l->input_fd = stdin_fd != -1 ? stdin_fd : STDIN_FILENO;
|
||||
l->output_fd = stdout_fd != -1 ? stdout_fd : STDOUT_FILENO;
|
||||
l->input_fd = stdin_fd;
|
||||
l->output_fd = stdout_fd;
|
||||
l->buf = buf;
|
||||
l->buflen = buflen;
|
||||
l->prompt = prompt;
|
||||
l->prompt_len = strlen(prompt);
|
||||
l->oldpos = l->pos = 0;
|
||||
l->old_pos = l->pos = 0;
|
||||
l->len = 0;
|
||||
|
||||
// /* Enter raw mode. */
|
||||
// if (enableRawMode(l->ifd) == -1)
|
||||
// return -1;
|
||||
|
||||
l->columns = getColumns(stdin_fd, stdout_fd);
|
||||
l->columns = linenoise_get_columns(stdin_fd, stdout_fd);
|
||||
l->old_rows = 0;
|
||||
l->history_index = 0;
|
||||
|
||||
|
|
@ -533,16 +510,16 @@ int linenoiseEditStart(struct s_line_state *l, int stdin_fd, int stdout_fd, char
|
|||
/* If stdin is not a tty, stop here with the initialization. We
|
||||
* will actually just read a line from standard input in blocking
|
||||
* mode later, in linenoiseEditFeed(). */
|
||||
if (!isatty(l->input_fd))
|
||||
if (!isatty(l->input_fd->fd))
|
||||
return 0;
|
||||
|
||||
/* The latest history entry is always our current buffer, that
|
||||
* initially is just an empty string. */
|
||||
linenoise_history_add("");
|
||||
|
||||
if (write(l->output_fd, prompt, l->prompt_len) == -1)
|
||||
return -1;
|
||||
return 0;
|
||||
if (write_fd(l->output_fd, (t_u8 *)prompt, l->prompt_len, NULL))
|
||||
return (ERROR);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
char *linenoiseEditMore = "If you see this, you are misusing the API: when linenoiseEditFeed() is called, if it returns linenoiseEditMore "
|
||||
|
|
@ -566,79 +543,64 @@ char *linenoiseEditMore = "If you see this, you are misusing the API: when linen
|
|||
*
|
||||
* Some other errno: I/O error.
|
||||
*/
|
||||
char *linenoiseEditFeed(struct s_line_state *l)
|
||||
t_str linenoise_edit_feed(t_line_state *l)
|
||||
{
|
||||
/* Not a TTY, pass control to line reading without character
|
||||
* count limits. */
|
||||
if (!isatty(l->input_fd))
|
||||
return linenoiseNoTTY();
|
||||
if (!isatty(l->input_fd->fd))
|
||||
return linenoise_no_tty_impl();
|
||||
|
||||
char c;
|
||||
int nread;
|
||||
t_isize nread;
|
||||
char seq[3];
|
||||
t_vec_str *history = get_history();
|
||||
|
||||
nread = read(l->input_fd, &c, 1);
|
||||
if (nread <= 0)
|
||||
return NULL;
|
||||
if (read_fd(l->input_fd, (t_u8 *)&c, 1, &nread))
|
||||
return (printf("null1\n"), NULL);
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case ENTER: /* enter */
|
||||
case K_ENTER: /* enter */
|
||||
history->len--;
|
||||
mem_free(history->buffer[history->len]);
|
||||
return strdup(l->buf);
|
||||
case CTRL_C: /* ctrl-c */
|
||||
case K_CTRL_C: /* ctrl-c */
|
||||
errno = EAGAIN;
|
||||
return NULL;
|
||||
case BACKSPACE: /* backspace */
|
||||
case 8: /* ctrl-h */
|
||||
linenoiseEditBackspace(l);
|
||||
case K_BACKSPACE: /* backspace */
|
||||
case 8: /* ctrl-h */
|
||||
linenoise_edit_backspace(l);
|
||||
break;
|
||||
case 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. */
|
||||
if (l->len > 0)
|
||||
{
|
||||
linenoiseEditDelete(l);
|
||||
}
|
||||
linenoise_edit_delete(l);
|
||||
else
|
||||
{
|
||||
history->len--;
|
||||
mem_free(history->buffer[history->len]);
|
||||
errno = ENOENT;
|
||||
return NULL;
|
||||
return (NULL);
|
||||
}
|
||||
break;
|
||||
case CTRL_T: /* ctrl-t, swaps current character with previous. */
|
||||
if (l->pos > 0 && l->pos < l->len)
|
||||
{
|
||||
int aux = l->buf[l->pos - 1];
|
||||
l->buf[l->pos - 1] = l->buf[l->pos];
|
||||
l->buf[l->pos] = aux;
|
||||
if (l->pos != l->len - 1)
|
||||
l->pos++;
|
||||
refreshLine(l);
|
||||
}
|
||||
case K_CTRL_B: /* ctrl-b */
|
||||
linenoise_edit_move_left(l);
|
||||
break;
|
||||
case CTRL_B: /* ctrl-b */
|
||||
linenoiseEditMoveLeft(l);
|
||||
case K_CTRL_F: /* ctrl-f */
|
||||
linenoise_edit_move_right(l);
|
||||
break;
|
||||
case CTRL_F: /* ctrl-f */
|
||||
linenoiseEditMoveRight(l);
|
||||
case K_CTRL_P: /* ctrl-p */
|
||||
linenoise_edit_history_next(l, LINENOISE_HISTORY_PREV);
|
||||
break;
|
||||
case CTRL_P: /* ctrl-p */
|
||||
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_PREV);
|
||||
case K_CTRL_N: /* ctrl-n */
|
||||
linenoise_edit_history_next(l, LINENOISE_HISTORY_NEXT);
|
||||
break;
|
||||
case CTRL_N: /* ctrl-n */
|
||||
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT);
|
||||
break;
|
||||
case ESC: /* escape sequence */
|
||||
case K_ESC: /* escape sequence */
|
||||
/* Read the next two bytes representing the escape sequence.
|
||||
* Use two calls to handle slow terminals returning the two
|
||||
* chars at different times. */
|
||||
if (read(l->input_fd, seq, 1) == -1)
|
||||
if (read_fd(l->input_fd, (t_u8 *)seq, 1, NULL))
|
||||
break;
|
||||
if (read(l->input_fd, seq + 1, 1) == -1)
|
||||
if (read_fd(l->input_fd, (t_u8 *)(seq + 1), 1, NULL))
|
||||
break;
|
||||
|
||||
/* ESC [ sequences. */
|
||||
|
|
@ -647,39 +609,32 @@ char *linenoiseEditFeed(struct s_line_state *l)
|
|||
if (seq[1] >= '0' && seq[1] <= '9')
|
||||
{
|
||||
/* Extended escape, read additional byte. */
|
||||
if (read(l->input_fd, seq + 2, 1) == -1)
|
||||
if (read_fd(l->input_fd, (t_u8 *)(seq + 2), 1, NULL))
|
||||
break;
|
||||
if (seq[2] == '~')
|
||||
{
|
||||
switch (seq[1])
|
||||
{
|
||||
case '3': /* Delete key. */
|
||||
linenoiseEditDelete(l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (seq[1] == '3' && seq[2] == '~')
|
||||
linenoise_edit_delete(l);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (seq[1])
|
||||
{
|
||||
case 'A': /* Up */
|
||||
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_PREV);
|
||||
linenoise_edit_history_next(l, LINENOISE_HISTORY_PREV);
|
||||
break;
|
||||
case 'B': /* Down */
|
||||
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT);
|
||||
linenoise_edit_history_next(l, LINENOISE_HISTORY_NEXT);
|
||||
break;
|
||||
case 'C': /* Right */
|
||||
linenoiseEditMoveRight(l);
|
||||
linenoise_edit_move_right(l);
|
||||
break;
|
||||
case 'D': /* Left */
|
||||
linenoiseEditMoveLeft(l);
|
||||
linenoise_edit_move_left(l);
|
||||
break;
|
||||
case 'H': /* Home */
|
||||
linenoiseEditMoveHome(l);
|
||||
break;
|
||||
case 'F': /* End*/
|
||||
linenoiseEditMoveEnd(l);
|
||||
linenoise_edit_move_end(l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -694,37 +649,37 @@ char *linenoiseEditFeed(struct s_line_state *l)
|
|||
linenoiseEditMoveHome(l);
|
||||
break;
|
||||
case 'F': /* End*/
|
||||
linenoiseEditMoveEnd(l);
|
||||
linenoise_edit_move_end(l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (linenoiseEditInsert(l, c))
|
||||
if (linenoise_edit_insert(l, c))
|
||||
return NULL;
|
||||
break;
|
||||
case CTRL_U: /* Ctrl+u, delete the whole line. */
|
||||
case K_CTRL_U: /* Ctrl+u, delete the whole line. */
|
||||
l->buf[0] = '\0';
|
||||
l->pos = l->len = 0;
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
break;
|
||||
case 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';
|
||||
l->len = l->pos;
|
||||
refreshLine(l);
|
||||
linenoise_refresh_line(l);
|
||||
break;
|
||||
case 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);
|
||||
break;
|
||||
case CTRL_E: /* ctrl+e, go to the end of the line */
|
||||
linenoiseEditMoveEnd(l);
|
||||
case K_CTRL_E: /* ctrl+e, go to the end of the line */
|
||||
linenoise_edit_move_end(l);
|
||||
break;
|
||||
case CTRL_L: /* ctrl+l, clear screen */
|
||||
linenoiseClearScreen();
|
||||
refreshLine(l);
|
||||
case K_CTRL_L: /* ctrl+l, clear screen */
|
||||
linenoise_clear_screen(l->output_fd);
|
||||
linenoise_refresh_line(l);
|
||||
break;
|
||||
case CTRL_W: /* ctrl+w, delete previous word */
|
||||
linenoiseEditDeletePrevWord(l);
|
||||
case K_CTRL_W: /* ctrl+w, delete previous word */
|
||||
linenoise_edit_delete_prev_word(l);
|
||||
break;
|
||||
}
|
||||
return linenoiseEditMore;
|
||||
|
|
@ -734,33 +689,30 @@ char *linenoiseEditFeed(struct s_line_state *l)
|
|||
* for more information. This function is called when linenoiseEditFeed()
|
||||
* returns something different than NULL. At this point the user input
|
||||
* is in the buffer, and we can restore the terminal in normal mode. */
|
||||
void linenoiseEditStop(struct s_line_state *l)
|
||||
void linenoiseEditStop(t_line_state *l)
|
||||
{
|
||||
if (!isatty(l->input_fd))
|
||||
if (!isatty(l->input_fd->fd))
|
||||
return;
|
||||
// disableRawMode(l->ifd);
|
||||
printf("\n");
|
||||
linenoise_disable_raw_mode(l->input_fd);
|
||||
me_printf_fd(l->output_fd, "\n");
|
||||
}
|
||||
|
||||
/* This just implements a blocking loop for the multiplexed API.
|
||||
* In many applications that are not event-drivern, we can just call
|
||||
* the blocking linenoise API, wait for the user to complete the editing
|
||||
* and return the buffer. */
|
||||
static char *linenoiseBlockingEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt)
|
||||
char *linenoise_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, char *buf, size_t buflen, t_const_str prompt)
|
||||
{
|
||||
t_line_state l;
|
||||
|
||||
/* Editing without a buffer is invalid. */
|
||||
if (buflen == 0)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return (NULL);
|
||||
}
|
||||
return (errno = EINVAL, NULL);
|
||||
|
||||
linenoiseEditStart(&l, stdin_fd, stdout_fd, buf, buflen, prompt);
|
||||
linenoise_edit_start(&l, stdin_fd, stdout_fd, buf, buflen, prompt);
|
||||
char *res;
|
||||
while ((res = linenoiseEditFeed(&l)) == linenoiseEditMore)
|
||||
;
|
||||
while ((res = linenoise_edit_feed(&l)) == linenoiseEditMore)
|
||||
; // printf("edit\n");
|
||||
linenoiseEditStop(&l);
|
||||
return res;
|
||||
}
|
||||
|
|
@ -802,7 +754,7 @@ void linenoisePrintKeyCodes(void)
|
|||
* program using linenoise is called in pipe or with a file redirected
|
||||
* to its standard input. In this case, we want to be able to return the
|
||||
* line regardless of its length (by default we are limited to 4k). */
|
||||
static t_str linenoiseNoTTY(void)
|
||||
t_str linenoise_no_tty_impl(void)
|
||||
{
|
||||
t_string line;
|
||||
t_isize ret;
|
||||
|
|
@ -812,8 +764,7 @@ static t_str linenoiseNoTTY(void)
|
|||
while (true)
|
||||
{
|
||||
chr = '\n';
|
||||
ret = read(STDIN_FILENO, &chr, 1);
|
||||
if (ret < 0)
|
||||
if (read_fd(get_stdin(), (t_u8 *)&chr, 1, &ret))
|
||||
return (string_free(line), NULL);
|
||||
if (ret == 0 || chr == '\n')
|
||||
return (line.buf);
|
||||
|
|
@ -827,20 +778,21 @@ static t_str linenoiseNoTTY(void)
|
|||
* 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
|
||||
* something even in the most desperate of the conditions. */
|
||||
char *linenoise(const char *prompt)
|
||||
t_str linenoise(const char *prompt)
|
||||
{
|
||||
char buf[4096];
|
||||
char buf[4096];
|
||||
t_str retval;
|
||||
|
||||
if (!isatty(STDIN_FILENO))
|
||||
if (!isatty(get_stdin()->fd))
|
||||
{
|
||||
printf("not_tty\n");
|
||||
/* 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. */
|
||||
return linenoiseNoTTY();
|
||||
}
|
||||
{
|
||||
char *retval = linenoiseBlockingEdit(STDIN_FILENO, STDOUT_FILENO, buf, 4096, prompt);
|
||||
return retval;
|
||||
return linenoise_no_tty_impl();
|
||||
}
|
||||
printf("is_tty\n");
|
||||
retval = linenoise_blocking_edit(get_stdin(), get_stdout(), buf, 4096, prompt);
|
||||
return (retval);
|
||||
}
|
||||
|
||||
/* This is just a wrapper the user may want to call in order to make sure
|
||||
|
|
@ -856,21 +808,6 @@ void linenoise_free(void *ptr)
|
|||
|
||||
/* ================================ History ================================= */
|
||||
|
||||
/* Free the history, but does not reset it. Only used when we have to
|
||||
* exit() to avoid memory leaks are reported by valgrind & co. */
|
||||
static void free_history(void)
|
||||
{
|
||||
t_vec_str *history = get_history();
|
||||
vec_str_free(*history);
|
||||
}
|
||||
|
||||
/* At exit we'll try to fix the terminal to the initial conditions. */
|
||||
static void linenoise_uninit_lib(void)
|
||||
{
|
||||
// disableRawMode(STDIN_FILENO);
|
||||
free_history();
|
||||
}
|
||||
|
||||
/* This is the API call to add a new entry in the linenoise history.
|
||||
* It uses a fixed array of char pointers that are shifted (memmoved)
|
||||
* when the history max length is reached in order to remove the older
|
||||
|
|
@ -945,6 +882,7 @@ t_error linenoise_history_load(t_str name)
|
|||
fd = open_fd(name, FD_READ, FD_CLOSE_ON_EXEC, FP_ALL_READ);
|
||||
if (fd == NULL)
|
||||
return (ERROR);
|
||||
history = get_history();
|
||||
while (!gnl_wrapper(fd, &tmp))
|
||||
{
|
||||
while (tmp.len != 0 && (tmp.buf[tmp.len - 1] == '\n' || tmp.buf[tmp.len - 1] == '\r'))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue