started the norminette
This commit is contained in:
parent
46e96a2120
commit
f4a5c0984e
7 changed files with 318 additions and 295 deletions
|
|
@ -18,9 +18,9 @@
|
|||
# include "me/types.h"
|
||||
# include "me/vec/vec_str.h"
|
||||
|
||||
|
||||
t_error line_edit_insert(t_line_state *state, char c);
|
||||
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt);
|
||||
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd,
|
||||
t_const_str prompt);
|
||||
t_str line_edit_feed(t_line_state *state);
|
||||
void line_edit_backspace(t_line_state *state);
|
||||
void line_edit_delete(t_line_state *state);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* _line_interal.h :+: :+: :+: */
|
||||
/* _line_internal.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 15:37:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 15:38:51 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 15:48:56 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef _LINE_INTERAL_H
|
||||
#define _LINE_INTERAL_H
|
||||
#ifndef _LINE_INTERNAL_H
|
||||
# define _LINE_INTERNAL_H
|
||||
|
||||
# include "line/_line_functions.h"
|
||||
# include "me/fs/fs.h"
|
||||
|
|
@ -26,7 +26,8 @@ void free_history(void);
|
|||
|
||||
t_error gnl_wrapper(t_fd *fd, t_string *out);
|
||||
void line_uninit_lib(void);
|
||||
t_error line_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_out);
|
||||
t_error line_get_cursor_position(t_fd *input, t_fd *output,
|
||||
t_u32 *column_out);
|
||||
t_u32 line_get_columns(t_fd *input, t_fd *output);
|
||||
t_usize line_get_prompt_len(t_const_str s);
|
||||
t_str line_no_tty_impl(void);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 15:24:34 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 15:32:35 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 15:53:54 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -24,16 +24,24 @@ typedef enum e_line_flags t_line_flags;
|
|||
typedef enum e_key_action t_key_action;
|
||||
typedef enum e_history_direction t_history_direction;
|
||||
|
||||
/// @param input_fd Terminal stdin file descriptor.
|
||||
/// @param output_fd Terminal stdout file descriptor.
|
||||
/// @param buf Edited line buffer.
|
||||
/// @param prompt_len Prompt to display.
|
||||
/// @param pos Prompt length.
|
||||
/// @param prompt_len Current cursor position.
|
||||
/// @param columns Number of columns in terminal.
|
||||
/// @param history_index The history index we are currently editing.
|
||||
struct s_line_state
|
||||
{
|
||||
t_fd *input_fd; /* Terminal stdin file descriptor. */
|
||||
t_fd *output_fd; /* Terminal stdout file descriptor. */
|
||||
t_string buf; /* Edited line buffer. */
|
||||
t_const_str prompt; /* Prompt to display. */
|
||||
t_usize prompt_len; /* Prompt length. */
|
||||
t_usize pos; /* Current cursor position. */
|
||||
t_usize columns; /* Number of columns in terminal. */
|
||||
t_i32 history_index; /* The history index we are currently editing. */
|
||||
t_fd *input_fd;
|
||||
t_fd *output_fd;
|
||||
t_string buf;
|
||||
t_const_str prompt;
|
||||
t_usize prompt_len;
|
||||
t_usize pos;
|
||||
t_usize columns;
|
||||
t_i32 history_index;
|
||||
};
|
||||
|
||||
struct s_raw_mode_state
|
||||
|
|
@ -44,33 +52,33 @@ struct s_raw_mode_state
|
|||
|
||||
enum e_key_action
|
||||
{
|
||||
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_NEWLINE = 10, /* Newline */
|
||||
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 */
|
||||
K_KEY_NULL = 0,
|
||||
K_CTRL_A = 1,
|
||||
K_CTRL_B = 2,
|
||||
K_CTRL_C = 3,
|
||||
K_CTRL_D = 4,
|
||||
K_CTRL_E = 5,
|
||||
K_CTRL_F = 6,
|
||||
K_CTRL_H = 8,
|
||||
K_TAB = 9,
|
||||
K_NEWLINE = 10,
|
||||
K_CTRL_K = 11,
|
||||
K_CTRL_L = 12,
|
||||
K_ENTER = 13,
|
||||
K_CTRL_N = 14,
|
||||
K_CTRL_P = 16,
|
||||
K_CTRL_T = 20,
|
||||
K_CTRL_U = 21,
|
||||
K_CTRL_W = 23,
|
||||
K_ESC = 27,
|
||||
K_BACKSPACE = 127
|
||||
};
|
||||
|
||||
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.
|
||||
REFRESH_CLEAN = 1 << 0,
|
||||
REFRESH_WRITE = 1 << 1,
|
||||
REFRESH_ALL = REFRESH_CLEAN | REFRESH_WRITE,
|
||||
};
|
||||
|
||||
enum e_history_direction
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
line
|
||||
line_globals
|
||||
|
|
|
|||
146
line/src/line.c
146
line/src/line.c
|
|
@ -6,21 +6,10 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 15:41:07 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 15:47:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "line/_line_internal.h"
|
||||
#include "line/line.h"
|
||||
#include "me/fs/fs.h"
|
||||
|
|
@ -31,47 +20,16 @@
|
|||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
|
||||
t_const_str get_unfinished_str(void)
|
||||
{
|
||||
return ("If you see this, you are misusing the API: when linenoiseEditFeed() is called, if it returns get_unfinished_str() "
|
||||
"the user is yet editing the line. See the README file for more information.");
|
||||
}
|
||||
|
||||
t_vec_str *get_history(void)
|
||||
{
|
||||
static t_vec_str history = {};
|
||||
static bool init = false;
|
||||
|
||||
if (!init)
|
||||
{
|
||||
history = vec_str_new(256, (void (*)())mem_free);
|
||||
init = true;
|
||||
}
|
||||
return (&history);
|
||||
}
|
||||
|
||||
t_raw_mode_state *get_raw_mode_state(void)
|
||||
{
|
||||
static t_raw_mode_state state = {};
|
||||
|
||||
return (&state);
|
||||
}
|
||||
|
||||
/* 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 line_uninit_lib(void)
|
||||
{
|
||||
line_disable_raw_mode(get_stdin());
|
||||
free_history();
|
||||
}
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define lndebug(fmt, ...)
|
||||
|
||||
|
|
@ -83,13 +41,13 @@ __attribute__((destructor)) void line_uninit_lib(void)
|
|||
t_error line_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_out)
|
||||
{
|
||||
char buf[32];
|
||||
int cols, rows;
|
||||
unsigned int i = 0;
|
||||
unsigned int i;
|
||||
|
||||
int cols, rows;
|
||||
i = 0;
|
||||
/* Report cursor location */
|
||||
if (write_fd(output, (t_u8 *)"\x1b[6n", 4, NULL))
|
||||
return (ERROR);
|
||||
|
||||
/* Read the response: ESC [ rows ; cols R */
|
||||
while (i < sizeof(buf) - 1)
|
||||
{
|
||||
|
|
@ -100,7 +58,6 @@ t_error line_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_out)
|
|||
i++;
|
||||
}
|
||||
buf[i] = '\0';
|
||||
|
||||
/* Parse it. */
|
||||
if (buf[0] != K_ESC && buf[1] != '[')
|
||||
return (ERROR);
|
||||
|
|
@ -123,12 +80,10 @@ t_u32 line_get_columns(t_fd *input, t_fd *output)
|
|||
/* Get the initial position so we can restore it later. */
|
||||
if (line_get_cursor_position(input, output, &start))
|
||||
return (80);
|
||||
|
||||
/* Go to right margin and get position. */
|
||||
me_printf_fd(output, "\x1b[999C");
|
||||
if (line_get_cursor_position(input, output, &cols))
|
||||
return (80);
|
||||
|
||||
if (cols > start)
|
||||
{
|
||||
me_printf_fd(output, "\x1b[%dD", cols - start);
|
||||
|
|
@ -152,12 +107,10 @@ t_error line_enable_raw_mode(t_fd *fd)
|
|||
t_raw_mode_state *raw_state;
|
||||
|
||||
raw_state = get_raw_mode_state();
|
||||
|
||||
if (!isatty(fd->fd))
|
||||
return (errno = ENOTTY, ERROR);
|
||||
if (tcgetattr(fd->fd, &raw_state->state) == -1)
|
||||
return (errno = ENOTTY, ERROR);
|
||||
|
||||
raw = raw_state->state; /* modify the original mode */
|
||||
/* input modes: no break, no CR to NL, no parity check, no strip char,
|
||||
* no start/stop output control. */
|
||||
|
|
@ -173,7 +126,6 @@ t_error line_enable_raw_mode(t_fd *fd)
|
|||
* We want read to return every single byte, without timeout. */
|
||||
raw.c_cc[VMIN] = 1;
|
||||
raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
|
||||
|
||||
/* put terminal in raw mode after flushing */
|
||||
if (tcsetattr(fd->fd, TCSAFLUSH, &raw) < 0)
|
||||
return (errno = ENOTTY, ERROR);
|
||||
|
|
@ -255,7 +207,8 @@ void line_refresh(t_line_state *state, t_line_flags flags)
|
|||
str = string_new(64);
|
||||
string_push(&str, "\r\x1b[2K");
|
||||
if (flags & REFRESH_WRITE)
|
||||
me_printf_str(&str, "%s%s\x1b[0G\x1b[%uC", state->prompt, state->buf.buf, state->pos + line_get_prompt_len(state->prompt));
|
||||
me_printf_str(&str, "%s%s\x1b[0G\x1b[%uC", state->prompt,
|
||||
state->buf.buf, state->pos + line_get_prompt_len(state->prompt));
|
||||
me_printf_fd(state->output_fd, "%s", str.buf);
|
||||
string_free(str);
|
||||
}
|
||||
|
|
@ -337,18 +290,19 @@ void line_edit_move_end(t_line_state *state)
|
|||
|
||||
/* Substitute the currently edited line with the next or previous history
|
||||
* entry as specified by 'dir'. */
|
||||
void line_edit_history_next(t_line_state *state, t_history_direction direction)
|
||||
void line_edit_history_next(t_line_state *state,
|
||||
t_history_direction direction)
|
||||
{
|
||||
t_vec_str *history;
|
||||
|
||||
history = get_history();
|
||||
|
||||
if (history->len > 1)
|
||||
{
|
||||
/* Update the current history entry before to
|
||||
* overwrite it with the next one. */
|
||||
mem_free(history->buffer[history->len - 1 - state->history_index]);
|
||||
history->buffer[history->len - 1 - state->history_index] = str_clone(state->buf.buf);
|
||||
history->buffer[history->len - 1
|
||||
- state->history_index] = str_clone(state->buf.buf);
|
||||
/* Show the new entry */
|
||||
state->history_index += (direction == HIST_PREV) ? 1 : -1;
|
||||
if (state->history_index < 0)
|
||||
|
|
@ -362,7 +316,8 @@ void line_edit_history_next(t_line_state *state, t_history_direction direction)
|
|||
return ;
|
||||
}
|
||||
string_clear(&state->buf);
|
||||
string_push(&state->buf, history->buffer[history->len - 1 - state->history_index]);
|
||||
string_push(&state->buf, history->buffer[history->len - 1
|
||||
- state->history_index]);
|
||||
state->pos = state->buf.len;
|
||||
line_refresh_line(state);
|
||||
}
|
||||
|
|
@ -411,13 +366,13 @@ void line_edit_backspace(t_line_state *state)
|
|||
* fails. If stdin_fd or stdout_fd are set to -1, the default is to use
|
||||
* STDIN_FILENO and STDOUT_FILENO.
|
||||
*/
|
||||
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt)
|
||||
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd,
|
||||
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
|
||||
* specific editing functionalities. */
|
||||
state->input_fd = stdin_fd;
|
||||
|
|
@ -426,24 +381,19 @@ t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_
|
|||
state->prompt = prompt;
|
||||
state->prompt_len = str_len(state->prompt);
|
||||
state->pos = 0;
|
||||
|
||||
/* Enter raw mode. */
|
||||
if (line_enable_raw_mode(state->input_fd))
|
||||
return (ERROR);
|
||||
|
||||
state->columns = line_get_columns(stdin_fd, stdout_fd);
|
||||
state->history_index = 0;
|
||||
|
||||
/* 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(state->input_fd->fd))
|
||||
return (NO_ERROR);
|
||||
|
||||
/* The latest history entry is always our current buffer, that
|
||||
* initially is just an empty string. */
|
||||
line_history_add("");
|
||||
|
||||
if (write_fd(state->output_fd, (t_u8 *)prompt, state->prompt_len, NULL))
|
||||
return (ERROR);
|
||||
return (NO_ERROR);
|
||||
|
|
@ -455,6 +405,7 @@ t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_
|
|||
* descriptor. In the case of blocking operations, this function can just be
|
||||
* called in a loop, and block.
|
||||
*
|
||||
|
||||
* The function returns get_unfinished_str() to signal that line editing is still
|
||||
* in progress, that is, the user didn't yet pressed enter / CTRL-D. Otherwise
|
||||
* the function returns the pointer to the heap-allocated buffer with the
|
||||
|
|
@ -469,26 +420,26 @@ t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_
|
|||
*/
|
||||
t_str line_edit_feed(t_line_state *state)
|
||||
{
|
||||
/* Not a TTY, pass control to line reading without character
|
||||
* count limits. */
|
||||
if (!isatty(state->input_fd->fd))
|
||||
return line_no_tty_impl();
|
||||
|
||||
char c;
|
||||
t_isize nread;
|
||||
char seq[3];
|
||||
t_vec_str *history = get_history();
|
||||
t_vec_str *history;
|
||||
t_str tmp;
|
||||
|
||||
/* Not a TTY, pass control to line reading without character
|
||||
* count limits. */
|
||||
if (!isatty(state->input_fd->fd))
|
||||
return (line_no_tty_impl());
|
||||
history = get_history();
|
||||
if (read_fd(state->input_fd, (t_u8 *)&c, 1, &nread))
|
||||
return (NULL);
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case K_NEWLINE: /* enter */
|
||||
case K_ENTER: /* enter */
|
||||
if (!vec_str_pop(history, &tmp))
|
||||
mem_free(tmp);
|
||||
return str_clone(state->buf.buf);
|
||||
return (str_clone(state->buf.buf));
|
||||
case K_CTRL_C: /* ctrl-c */
|
||||
return (errno = EAGAIN, NULL);
|
||||
case K_BACKSPACE: /* backspace */
|
||||
|
|
@ -526,7 +477,6 @@ t_str line_edit_feed(t_line_state *state)
|
|||
break ;
|
||||
if (read_fd(state->input_fd, (t_u8 *)(seq + 1), 1, NULL))
|
||||
break ;
|
||||
|
||||
/* ESC [ sequences. */
|
||||
if (seq[0] == '[')
|
||||
{
|
||||
|
|
@ -563,7 +513,6 @@ t_str line_edit_feed(t_line_state *state)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ESC O sequences. */
|
||||
else if (seq[0] == 'O')
|
||||
{
|
||||
|
|
@ -625,13 +574,13 @@ void line_edit_stop(t_line_state *state)
|
|||
t_str line_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt)
|
||||
{
|
||||
t_line_state l;
|
||||
t_str res;
|
||||
|
||||
line_edit_start(&l, stdin_fd, stdout_fd, prompt);
|
||||
t_str res;
|
||||
while ((res = line_edit_feed(&l)) == get_unfinished_str())
|
||||
;
|
||||
line_edit_stop(&l);
|
||||
return res;
|
||||
return (res);
|
||||
}
|
||||
|
||||
/* This special mode is used by linenoise in order to print scan codes
|
||||
|
|
@ -640,6 +589,8 @@ t_str line_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt)
|
|||
void line_print_key_codes(void)
|
||||
{
|
||||
char quit[4];
|
||||
char c;
|
||||
t_isize nread;
|
||||
|
||||
printf("Linenoise key codes debugging mode.\n"
|
||||
"Press keys to see scan codes. Type 'quit' at any time to exit.\n");
|
||||
|
|
@ -648,19 +599,17 @@ void line_print_key_codes(void)
|
|||
mem_set(quit, ' ', 4);
|
||||
while (1)
|
||||
{
|
||||
char c;
|
||||
t_isize nread;
|
||||
|
||||
if (read_fd(get_stdin(), (void *)&c, 1, &nread))
|
||||
continue ;
|
||||
if (nread <= 0)
|
||||
continue ;
|
||||
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 (mem_compare(quit, "quit", sizeof(quit)))
|
||||
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);
|
||||
printf("\r"); /* Go left edge manually, we are in raw mode. */
|
||||
fflush(stdout);
|
||||
}
|
||||
|
|
@ -708,7 +657,7 @@ t_str linenoise(t_const_str prompt)
|
|||
{
|
||||
/* 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 line_no_tty_impl();
|
||||
return (line_no_tty_impl());
|
||||
}
|
||||
retval = line_blocking_edit(get_stdin(), get_stdout(), prompt);
|
||||
return (retval);
|
||||
|
|
@ -729,7 +678,6 @@ bool line_history_add(t_const_str line)
|
|||
t_vec_str *history;
|
||||
|
||||
history = get_history();
|
||||
|
||||
if (history->len != 0 && !strcmp(history->buffer[history->len - 1], line))
|
||||
return (false);
|
||||
linecopy = str_clone(line);
|
||||
|
|
@ -748,14 +696,15 @@ t_error line_history_save(t_str name)
|
|||
t_vec_str *history;
|
||||
|
||||
history = get_history();
|
||||
|
||||
fd = open_fd(name, FD_READ, FD_CLOSE_ON_EXEC | FD_TRUNCATE | FD_CREATE, FP_OWRITE);
|
||||
fd = open_fd(name, FD_READ, FD_CLOSE_ON_EXEC | FD_TRUNCATE | FD_CREATE,
|
||||
FP_OWRITE);
|
||||
if (fd == NULL)
|
||||
return (ERROR);
|
||||
j = 0;
|
||||
while (j < history->len)
|
||||
{
|
||||
write_fd(fd, (t_u8 *)history->buffer[j], str_len(history->buffer[j]), NULL);
|
||||
write_fd(fd, (t_u8 *)history->buffer[j], str_len(history->buffer[j]),
|
||||
NULL);
|
||||
write_fd(fd, (t_u8 *)"\n", 1, NULL);
|
||||
j++;
|
||||
}
|
||||
|
|
@ -793,7 +742,8 @@ t_error line_history_load(t_str name)
|
|||
history = get_history();
|
||||
while (!gnl_wrapper(fd, &tmp))
|
||||
{
|
||||
while (tmp.len != 0 && (tmp.buf[tmp.len - 1] == '\n' || tmp.buf[tmp.len - 1] == '\r'))
|
||||
while (tmp.len != 0 && (tmp.buf[tmp.len - 1] == '\n' || tmp.buf[tmp.len
|
||||
- 1] == '\r'))
|
||||
string_pop(&tmp);
|
||||
vec_str_push(history, tmp.buf);
|
||||
}
|
||||
|
|
|
|||
63
line/src/line_globals.c
Normal file
63
line/src/line_globals.c
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* line_globals.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 15:47:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 15:48:09 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/types.h"
|
||||
#include "line/_line_internal.h"
|
||||
#include "line/_line_structs.h"
|
||||
#include "me/mem/mem.h"
|
||||
|
||||
t_const_str get_unfinished_str(void)
|
||||
{
|
||||
return ("If you see this,"
|
||||
" you are misusing the API"
|
||||
" if it returns get_unfinished_str() "
|
||||
" the user is yet editing the line. "
|
||||
"See the README file for more information.");
|
||||
}
|
||||
|
||||
t_vec_str *get_history(void)
|
||||
{
|
||||
static t_vec_str history = {};
|
||||
static bool init = false;
|
||||
|
||||
if (!init)
|
||||
{
|
||||
history = vec_str_new(256, (void (*)())mem_free);
|
||||
init = true;
|
||||
}
|
||||
return (&history);
|
||||
}
|
||||
|
||||
t_raw_mode_state *get_raw_mode_state(void)
|
||||
{
|
||||
static t_raw_mode_state state = {};
|
||||
|
||||
return (&state);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
history = get_history();
|
||||
vec_str_free(*history);
|
||||
}
|
||||
|
||||
/* At exit we'll try to fix the terminal to the initial conditions. */
|
||||
__attribute__((destructor)) void line_uninit_lib(void)
|
||||
{
|
||||
line_disable_raw_mode(get_stdin());
|
||||
free_history();
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue