diff options
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | libft/Makefile | 2 | ||||
-rw-r--r-- | src/b_cd.c | 2 | ||||
-rw-r--r-- | src/b_exit.c | 1 | ||||
-rw-r--r-- | src/c_init.c | 149 | ||||
-rw-r--r-- | src/c_init.h | 49 | ||||
-rw-r--r-- | src/c_input.c | 91 | ||||
-rw-r--r-- | src/c_input.h | 23 | ||||
-rw-r--r-- | src/c_keys.c | 125 | ||||
-rw-r--r-- | src/c_keys.h | 22 | ||||
-rw-r--r-- | src/c_utils.c | 156 | ||||
-rw-r--r-- | src/c_utils.h | 23 | ||||
-rw-r--r-- | src/m_loop.c | 6 | ||||
-rw-r--r-- | src/m_loop_next.c | 9 | ||||
-rw-r--r-- | src/p_lcom_next.c | 134 | ||||
-rw-r--r-- | src/p_lcom_vars.c | 139 | ||||
-rw-r--r-- | src/p_lcom_vars.h | 24 | ||||
-rw-r--r-- | src/ps | bin | 0 -> 6854 bytes | |||
-rw-r--r-- | src/s_com.c | 17 | ||||
-rw-r--r-- | src/s_init.c | 2 | ||||
-rw-r--r-- | src/u_utils.c | 3 |
21 files changed, 962 insertions, 24 deletions
@@ -32,6 +32,10 @@ SRCS_NAME += b_sqb_nbr SRCS_NAME += b_sqb_str SRCS_NAME += b_type SRCS_NAME += b_unset +SRCS_NAME += c_init +SRCS_NAME += c_input +SRCS_NAME += c_keys +SRCS_NAME += c_utils SRCS_NAME += e_builtins SRCS_NAME += e_externs SRCS_NAME += e_externs_pipes @@ -101,19 +105,20 @@ ifeq (${OS}, FreeBSD) CC = /usr/bin/cc endif ifeq (${OS}, Linux) -CC = /usr/bin/clang-9 +CC = clang endif ifeq (${OS}, Darwin) CC = clang endif CFLAGS = -std=c89 -CFLAGS += -Wall +CFLAGS = -Wall CFLAGS += -Wextra CFLAGS += -Werror CFLAGS += -pedantic #------------------------------------------------------------------------------# LDFLAGS = -L${LFT_DIR} LDFLAGS += -lft +LDFLAGS += -lncurses #==============================================================================# #--------------------------------- UNIX ---------------------------------------# #==============================================================================# diff --git a/libft/Makefile b/libft/Makefile index 3c11199..4489b9a 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -130,7 +130,7 @@ ifeq (${OS}, FreeBSD) CC = /usr/bin/cc endif ifeq (${OS}, Linux) -CC = /usr/bin/clang-9 +CC = clang endif ifeq (${OS}, Darwin) CC = clang @@ -13,7 +13,7 @@ #include <libft.h> #include <stdint.h> #include <unistd.h> -#include <limits.h> +#include <linux/limits.h> #include "b_export_next.h" #include "d_define.h" diff --git a/src/b_exit.c b/src/b_exit.c index cb8988f..0877d0c 100644 --- a/src/b_exit.c +++ b/src/b_exit.c @@ -37,6 +37,7 @@ uint8_t { ret = ft_atoi(args[0]); /* TODO: non numeric args[0] */ + /* TODO: return term to normal state (c_set_term_raw(0))*/ } else ret = msh->ret; diff --git a/src/c_init.c b/src/c_init.c new file mode 100644 index 0000000..77d3023 --- /dev/null +++ b/src/c_init.c @@ -0,0 +1,149 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include <stdlib.h> +#include <libft.h> +#include <signal.h> +#include <term.h> + +#include "c_init.h" +#include "c_keys.h" +#include "c_input.h" +#include "c_utils.h" +#include "s_struct.h" +#include "m_prompt.h" + +int16_t + c_set_term_raw(uint8_t mode) +{ + struct termios tios; + + ft_memset(&tios, 0, sizeof(tios)); + tcgetattr(STDIN_FILENO, &tios); + if (mode) + { + tios.c_lflag &= ~(ECHO | ICANON | ISIG); + tios.c_oflag &= ~(OPOST); + tios.c_cc[VMIN] = 1; + tios.c_cc[VTIME] = 0; + } + else + { + tios.c_lflag |= (ECHO | ICANON | ISIG); + tios.c_oflag |= (OPOST); + } + tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios); + mode ? tputs(tgetstr("ns", NULL), 1, ft_putchar) : 0; + return (1); +} + +static void + c_add_char(char **line, char *buf, t_caps *tcaps, t_msh *msh) +{ + *line = c_insert_char(*line, buf[0], tcaps); + tcaps->cpos++; + if ((((tcaps->cpos) + ft_strlen(msh->ps[tcaps->psx])) % + tcaps->ws.ws_col) == 0) + { + write(1, "\n\r", 2); + return ; + } + c_redraw_line(*line, tcaps, msh); +} + +static int16_t + c_read_cap(char *buf, char *line, t_caps *tcaps, t_msh *msh) +{ + uint32_t plen; + + plen = ft_strlen(msh->ps[tcaps->psx]); + if (((*((unsigned int *)buf)) == LEFT_K) || + ((*((unsigned int *)buf)) == CTRL_B)) + return (c_key_left(plen, tcaps)); + else if (((*((unsigned int *)buf)) == RIGHT_K) || + ((*((unsigned int *)buf)) == CTRL_F)) + return (c_key_right(ft_strlen(line), plen, tcaps)); + else if (((*((unsigned int *)buf)) == HOME_K) || + ((*((unsigned int *)buf)) == CTRL_A)) + return (c_home_key(plen, tcaps)); + else if (((*((unsigned int *)buf)) == END_K) || + ((*((unsigned int *)buf)) == CTRL_E)) + return (c_end_key(ft_strlen(line), plen, tcaps)); + else if ((*((unsigned int *)buf)) == CTRL_L) + return (c_ctrl_l(line, tcaps, msh)); + else if ((*((unsigned int *)buf)) == CTRL_C) + { + msh->ret = 130; + return (1); + } + else + return (0); +} + +static char + *c_process_key(char *buf, t_caps *tcaps, t_msh *msh) +{ + static char *line = NULL; + int i; + + i = -1; + if (line == NULL) + if (!(line = ft_calloc(1, sizeof(char)))) + return (NULL); + if (ft_isprint(buf[0])) + c_add_char(&line, buf, tcaps, msh); + else if ((*((unsigned int *)buf)) == DEL_K) + { + c_back_slash(&line, ft_strlen(msh->ps[tcaps->psx]), tcaps); + if ((((tcaps->cpos) + ft_strlen(msh->ps[tcaps->psx])) % + tcaps->ws.ws_col) == 0) + { + return (NULL); + } + c_redraw_line(line, tcaps, msh); + } + else + { + c_read_cap(buf, line, tcaps, msh); + } + return ((buf[0] == '\n') ? c_new_line(line, tcaps) : NULL); +} + +int16_t + c_gnl(int32_t fd, char **line, uint8_t psx, t_msh *msh) +{ + t_caps tcaps; + char nread[5]; + + c_init_line(psx, &tcaps); + tputs(tgetstr("cr", NULL), 1, ft_putchar); + m_prompt_psx(psx, msh); + ft_bzero(nread, 5); + if (!(c_get_win_size(&tcaps.ws))) + return (-1); + while (!(ft_strchr(nread, '\n'))) + { + if (msh->ret == 130) + { + *line = ft_strdup("\0"); + return (1); + } + ft_bzero(nread, 5); + if (!(read(fd, nread, 4))) + return (0); + else + *line = c_process_key(nread, &tcaps, msh); + tputs(tgetstr("ve", NULL), 1, ft_putchar); + } + return (1); +} diff --git a/src/c_init.h b/src/c_init.h new file mode 100644 index 0000000..e723563 --- /dev/null +++ b/src/c_init.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_init.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef C_INIT_H +# define C_INIT_H + +# include <termios.h> +# include <sys/ioctl.h> + +# include "s_struct.h" + +# define LEFT_K 0x445b1b +# define RIGHT_K 0x435b1b +# define HOME_K 0x485b1b +# define END_K 0x7e345b1b +# define DEL_K 0x7f +# define RET_K 0x0d + +# define CTRL_A 0x01 +# define CTRL_B 0x02 +# define CTRL_C 0x03 +# define CTRL_D 0x04 +# define CTRL_E 0x05 +# define CTRL_F 0x06 +# define CTRL_L 0x0c + +typedef struct s_caps +{ + struct termios tios; + struct winsize ws; + uint32_t cpos; + uint32_t lpos; + uint32_t nlines; + uint8_t psx; +} t_caps; + +int16_t c_gnl(int32_t fd, char **line, uint8_t psx, t_msh *msh); +int16_t c_set_term_raw(uint8_t mode); + +#endif diff --git a/src/c_input.c b/src/c_input.c new file mode 100644 index 0000000..5855112 --- /dev/null +++ b/src/c_input.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_input.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include <stdlib.h> +#include <libft.h> +#include <signal.h> +#include <term.h> + +#include "c_init.h" +#include "c_keys.h" +#include "c_input.h" +#include "c_utils.h" +#include "m_prompt.h" +#include "m_loop.h" + +int16_t + c_init_line(uint8_t psx, t_caps *tcaps) +{ + char *term; + + if (tcaps) + { + term = getenv("TERM"); + if (!tgetent(NULL, term)) + return (-1); + c_set_term_raw(1); + signal(SIGINT, SIG_IGN); + tcaps->cpos = 0; + tcaps->lpos = 0; + tcaps->psx = psx - 1; + return (1); + } + else + return (-1); +} + +uint16_t + c_get_win_size(struct winsize *ws) +{ + if (!(ioctl(STDOUT_FILENO, TIOCGWINSZ, ws))) + return (-1); + if (!ws->ws_col) + ws->ws_col = 80; + if (!ws->ws_row) + ws->ws_row = 80; + return (1); +} + +uint32_t + c_get_line_num(char *line, uint32_t cpos, uint32_t plen, t_caps *tcaps) +{ + uint32_t it; + uint32_t line_num; + uint32_t len; + + it = 0; + line_num = 0; + tcaps->lpos = 1; + len = ft_strlen(line); + if ((len) < (tcaps->ws.ws_col - plen)) + return (1); + while (it < len) + { + it += (it == 0) ? (tcaps->ws.ws_col - plen) : tcaps->ws.ws_col; + tcaps->lpos += (it < cpos) ? 1 : 0; + line_num++; + } + return (line_num); +} + +int16_t + c_back_slash(char **line, uint32_t plen, t_caps *tcaps) +{ + if (tcaps->cpos >= 1) + { + *line = c_delchar(*line, tcaps->cpos); + c_key_left(plen, tcaps); + tputs(tgetstr("cd", NULL), 1, ft_putchar); + } + return (1); +} diff --git a/src/c_input.h b/src/c_input.h new file mode 100644 index 0000000..f22166a --- /dev/null +++ b/src/c_input.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_input.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef C_INPUT_H +# define C_INPUT_H + +int16_t c_back_slash(char **line, uint32_t plen, t_caps *tcaps); +int16_t c_ctrl_c(char *line, t_caps *tcaps, t_msh *msh); +uint32_t c_get_line_num(char *line, uint32_t cpos, uint32_t plen, + t_caps *tcaps); +int16_t c_init_line(uint8_t psx, t_caps *tcaps); +uint16_t c_get_win_size(struct winsize *ws); + +#endif diff --git a/src/c_keys.c b/src/c_keys.c new file mode 100644 index 0000000..e0f734c --- /dev/null +++ b/src/c_keys.c @@ -0,0 +1,125 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_keys.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <term.h> +#include <libft.h> + +#include "c_init.h" +#include "c_utils.h" +#include "m_prompt.h" + +int16_t + c_ctrl_l(char *line, t_caps *tcaps, t_msh *msh) +{ + tputs(tgetstr("cl", NULL), 1, ft_putchar); + m_prompt_psx(1, msh); + c_redraw_line(line, tcaps, msh); + return (1); +} + +int16_t + c_home_key(uint32_t plen, t_caps *tcaps) +{ + uint32_t i; + int16_t j; + + i = tcaps->cpos; + j = tcaps->lpos; + if (tcaps->cpos > 0 && j > 0) + { + i = 0; + while (--j > 0) + tputs(tgetstr("up", NULL), 1, ft_putchar); + tputs(tgetstr("cr", NULL), 1, ft_putchar); + while (++i <= plen) + tputs(tgetstr("nd", NULL), 1, ft_putchar); + } + else if (i >= 1) + { + while (--i >= 0) + tputs(tgetstr("le", NULL), 1, ft_putchar); + } + tcaps->cpos = 0; + tcaps->lpos = 1; + return (1); +} + +int16_t + c_end_key(uint16_t size, uint32_t plen, t_caps *tcaps) +{ + uint16_t i; + uint16_t j; + + i = tcaps->cpos; + j = tcaps->lpos; + if ((tcaps->cpos < size) && (j < tcaps->nlines)) + { + i = 0; + while (j < tcaps->nlines) + { + tputs(tgetstr("do", NULL), 1, ft_putchar); + i += tcaps->ws.ws_col; + j++; + } + tputs(tgetstr("cr", NULL), 1, ft_putchar); + while (i++ < (size + plen)) + tputs(tgetstr("nd", NULL), 1, ft_putchar); + } + else + while (i++ < size) + tputs(tgetstr("nd", NULL), 1, ft_putchar); + tcaps->cpos = size; + tcaps->lpos = tcaps->nlines; + return (1); +} + +int16_t + c_key_right(uint32_t len, uint32_t plen, t_caps *tcaps) +{ + if ((((tcaps->cpos + plen + 1) % (tcaps->ws.ws_col)) == 0) && + tcaps->cpos < len) + { + tputs(tgetstr("do", NULL), 1, ft_putchar); + tputs(tgetstr("cr", NULL), 1, ft_putchar); + tcaps->cpos++; + tcaps->lpos++; + } + else if (tcaps->cpos < len) + { + tputs(tgetstr("nd", NULL), 1, ft_putchar); + tcaps->cpos++; + } + return (1); +} + +int16_t + c_key_left(uint32_t plen, t_caps *tcaps) +{ + int32_t pos; + + pos = -1; + if (((tcaps->cpos + plen) % tcaps->ws.ws_col) == 0 && + tcaps->cpos >= 1) + { + tputs(tgetstr("up", NULL), 1, ft_putchar); + while (++pos <= tcaps->ws.ws_col) + tputs(tgetstr("nd", NULL), 1, ft_putchar); + tcaps->cpos--; + tcaps->lpos--; + } + else if (tcaps->cpos >= 1) + { + tputs(tgetstr("le", NULL), 1, ft_putchar); + tcaps->cpos--; + } + return (1); +} diff --git a/src/c_keys.h b/src/c_keys.h new file mode 100644 index 0000000..12b17ee --- /dev/null +++ b/src/c_keys.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_keys.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef C_KEYS_H +# define C_KEYS_H + +int16_t c_key_right(uint32_t len, uint32_t plen, t_caps *tcaps); +int16_t c_key_left(uint32_t plen, t_caps *tcaps); +int16_t c_home_key(uint32_t plen, t_caps *tcaps); +int16_t c_end_key(uint16_t size, uint32_t plen, t_caps *tcaps); +int16_t c_ctrl_l(char *line, t_caps *tcaps, t_msh *msh); + +#endif diff --git a/src/c_utils.c b/src/c_utils.c new file mode 100644 index 0000000..f879f34 --- /dev/null +++ b/src/c_utils.c @@ -0,0 +1,156 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <term.h> +#include <unistd.h> + +#include "c_init.h" +#include "c_input.h" +#include "m_prompt.h" +#include "m_loop.h" +#include "s_struct.h" + +/* +** TODO: +** delchar MOVE +** NORME +** c_redraw_next TOUJOURS CALL --> DLA MERDE +** C-c globul pid +*/ + +char + *c_new_line(char *line, t_caps *tcaps) +{ + char *ret; + + ret = NULL; + write(1, "\n", 1); + tputs(tgetstr("cr", NULL), 1, ft_putchar); + c_set_term_raw(0); + if (line != NULL) + { + ret = ft_strdup(line); + line[0] = '\0'; + } + tcaps->cpos = 0; + tputs(tgetstr("cr", NULL), 1, ft_putchar); + return (ret); +} + +char + *c_insert_char(char *str, char c, t_caps *tcaps) +{ + int32_t i; + size_t j; + size_t len; + char *dst; + + i = -1; + j = 0; + len = (ft_strlen(str) + 2); + if (!(dst = (char*)malloc((len) * sizeof(char)))) + return (NULL); + while (++i < (int32_t)len) + { + if (i == (int32_t)tcaps->cpos) + dst[i] = c; + else + { + dst[i] = str[j]; + j++; + } + } + ft_memdel((void**)&str); + return (dst); +} + +char + *c_delchar(char *str, uint16_t cpos) +{ + char *dst; + uint16_t i; + uint16_t j; + + j = 0; + i = 0; + if (!(dst = malloc((ft_strlen(str)) * sizeof(char)))) + return (NULL); + cpos -= 1; + while (j < (ft_strlen(str) - 1)) + { + if (i == cpos) + i++; + dst[j] = str[i]; + i++; + j++; + } + ft_memdel((void**)&str); + dst[j] = '\0'; + return (dst); +} + +static int16_t + c_redraw_next(size_t plen, uint32_t len, t_caps *tcaps) +{ + uint32_t i; + uint32_t j; + + i = (tcaps->nlines == tcaps->lpos) ? 0 : + tcaps->ws.ws_col * (tcaps->lpos - 1); + j = tcaps->nlines; + if (tcaps->nlines != tcaps->lpos) + { + while (--j > tcaps->lpos) + tputs(tgetstr("up", NULL), 1, ft_putchar); + tputs(tgetstr("up", NULL), 1, ft_putchar); + tputs(tgetstr("cr", NULL), 1, ft_putchar); + while (i < (tcaps->cpos + plen)) + { + tputs(tgetstr("nd", NULL), 1, ft_putchar); + i++; + } + } + else + while (len > tcaps->cpos) + { + tputs(tgetstr("le", NULL), 1, ft_putchar); + len--; + } + return (len); +} + +int16_t + c_redraw_line(char *line, t_caps *tcaps, t_msh *msh) +{ + uint32_t i; + uint32_t j; + + tcaps->nlines = c_get_line_num(line, tcaps->cpos, + ft_strlen(msh->ps[tcaps->psx]), tcaps); + i = 0; + j = tcaps->lpos; + tputs(tgetstr("cr", NULL), 1, ft_putchar); + while (--j > 0) + { + tputs(tgetstr("sf", NULL), 1, ft_putchar); + tputs(tgetstr("up", NULL), 1, ft_putchar); + tputs(tgetstr("up", NULL), 1, ft_putchar); + } + while (i++ < ft_strlen(msh->ps[tcaps->psx])) + tputs(tgetstr("nd", NULL), 1, ft_putchar); + ft_printf("%s", line); + return ((tcaps->cpos != ft_strlen(line)) ? + c_redraw_next(ft_strlen(msh->ps[tcaps->psx]), ft_strlen(line), tcaps) : + ft_strlen(line)); +} diff --git a/src/c_utils.h b/src/c_utils.h new file mode 100644 index 0000000..730d29a --- /dev/null +++ b/src/c_utils.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef C_UTILS_H +# define C_UTILS_H + +# include "s_struct.h" + +char *c_new_line(char *line, t_caps *tcaps); +char *c_insert_char(char *str, char c, t_caps *tcaps); +char *c_delchar(char *str, uint16_t cpos); +int16_t c_redraw_line(char *line, t_caps *tcaps, t_msh *msh); + +#endif diff --git a/src/m_loop.c b/src/m_loop.c index 41c9e36..3762aac 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -15,6 +15,7 @@ #include <stdlib.h> #include <fcntl.h> #include <unistd.h> +#include <signal.h> #include <limits.h> #include "d_define.h" @@ -25,6 +26,7 @@ #include "s_com.h" #include "s_lpipes.h" #include "s_line.h" +#include "c_init.h" #include "u_utils.h" #include "u_parse.h" #include "u_vars.h" @@ -132,9 +134,7 @@ uint8_t gnl = 1; while (gnl > 0) { - if (fd == STDIN_FILENO) - m_prompt_psx(1, msh); - gnl = get_next_line(fd, &line); + gnl = c_gnl(fd, &line, 1, msh); m_delete_comments(line); if (line[0] != C_NUL) { diff --git a/src/m_loop_next.c b/src/m_loop_next.c index d138206..e088cda 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -18,6 +18,7 @@ #include "m_loop_next.h" #include "m_prompt.h" #include "s_struct.h" +#include "c_init.h" #include "u_utils.h" static char @@ -25,9 +26,7 @@ static char { char *counter_line; - if (fd == STDIN_FILENO) - m_prompt_psx(psx, msh); - get_next_line(fd, &counter_line); + c_gnl(fd, &counter_line, psx, msh); if (counter_line[0] != C_NUL) { line = ft_nrealloc(line, @@ -46,9 +45,7 @@ static char { char *counter_line; - if (fd == STDIN_FILENO) - m_prompt_psx(psx, msh); - get_next_line(fd, &counter_line); + c_gnl(fd, &counter_line, psx, msh); if (counter_line[0] != C_NUL) { line = ft_nrealloc(line, diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c new file mode 100644 index 0000000..19e3287 --- /dev/null +++ b/src/p_lcom_next.c @@ -0,0 +1,134 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_line_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdint.h> + +#include "d_enum.h" +#include "s_destroy.h" +#include "f_fail.h" +#include "s_struct.h" +#include "u_vars.h" +#include "u_vars_next.h" +#include "p_lcom_vars.h" + +/* TODO: norme : 2 fonctions + de 35l*/ + +static void + p_register_word(char word[], + t_msh *msh) +{ + char name[255]; + char val[255]; + char *ptr; + size_t i; + + name[0] = '$'; + ptr = word; + i = 1; + while (*ptr != '=' && *ptr != '\0') + { + name[i] = *ptr; + i++; + ptr++; + } + name[i] = '\0'; + ptr++; + i = 0; + while (*ptr != '\0') + { + val[i] = *ptr; + i++; + ptr++; + } + val[i] = '\0'; + u_subst_var_value(name, val, msh); +} + +static char + **p_add_to_variables_and_delete(char *words[], + t_bool reg, + int64_t i, + t_msh *msh) +{ + int64_t j; + int64_t k; + char **rewords; + + j = 0; + if (reg == TRUE) + while (words[j] && j < i) + { + p_register_word(words[j], msh); + j++; + } + j = 0; + while (words[i + j] != NULL) + j++; + if (!(rewords = (char**)malloc((j + 1) * sizeof(char*)))) + { + ft_delwords(words); + f_alloc_and_destroy_msh(msh); + } + k = i; + while (i - k < j) + { + if (!(rewords[i - k] = ft_strdup(words[i]))) + { + ft_delwords(words); + f_alloc_and_destroy_msh(msh); + } + i++; + } + rewords[i - k] = 0; + ft_delwords(words); + return (rewords); +} + +char + **p_check_args_equals(char *words[], + t_msh *msh) +{ + char *ptr; + t_bool reg; + t_bool isvar; + int64_t i; + + i = 0; + reg = FALSE; + isvar = FALSE; + while (words[i]) + { + ptr = words[i]; + while (*ptr != '\0' && *ptr != '=') + ptr++; + reg = (*ptr == '=') ? TRUE : FALSE; + isvar = (*ptr == '=') ? TRUE : FALSE; + if (*ptr == '\0' || words[i][0] == '=' || + ft_isdigit(words[i][0]) == TRUE) + { + reg = FALSE; + if (i == 0) + isvar = FALSE; + if (isvar == TRUE) + p_add_to_env_fork(i, words, msh); + else + msh->env_fork_tmp[0][0] = '\0'; + break ; + } + i++; + } + if (isvar == TRUE) + return (p_add_to_variables_and_delete(words, reg, i, msh)); + return (words); +} diff --git a/src/p_lcom_vars.c b/src/p_lcom_vars.c new file mode 100644 index 0000000..754333c --- /dev/null +++ b/src/p_lcom_vars.c @@ -0,0 +1,139 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_line_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdint.h> + +#include "d_enum.h" +#include "s_destroy.h" +#include "f_fail.h" +#include "s_struct.h" +#include "u_vars.h" +#include "u_vars_next.h" + + +static int8_t + subst_those_vars(int64_t i, + char **p_words, + t_msh *msh) +{ + size_t varlen; + char *s_varname; + char *varval; + + varval = NULL; + s_varname = NULL; + varlen = i + 1; + while ((*p_words)[varlen] != '\0' && + ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE) + varlen += 1; + if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) + return (-1); + varval = u_get_var_value(s_varname, msh); + *p_words = ft_strsubst(*p_words, s_varname, varval); + ft_memdel((void*)&s_varname); + ft_memdel((void*)&varval); + return (0); +} + +char + **p_subst_vars(char *words[], + t_msh *msh) +{ + char **p_words; + int64_t i; + + p_words = words; + i = 0; + while (*p_words) + { + while ((i = ft_strlchr((*p_words), '$')) != -1) + { + if (*(*p_words) + i - 1 != '\\') + { + if (subst_those_vars(i, p_words, msh) != 0) + return (NULL); + } + } + p_words += 1; + } + return (words); +} + +void + p_add_to_env_fork(int64_t i, + char *words[], + t_msh *msh) +{ + int64_t j; + + j = 0; + while(j < i) + { + ft_strlcpy(msh->env_fork_tmp[j], words[j], ft_strlen(words[j]) + 1); + j++; + } + msh->env_fork_tmp[j][0] = '\0'; +} + +char + **p_subst_args(const char word[], + int8_t redir) +{ + char **words; + char *subst; + size_t i; + + if (redir == 0) + { + if (!(words = ft_split(word, ' '))) + return (NULL); + return (words); + } + i = 0; + while (word[i] && ft_ischarset("<>", word[i]) == FALSE) + i++; + while (redir > 0 && ft_isdigit(word[i]) == TRUE) + i--; + if (!(subst = ft_substr(word, 0, i))) + return (NULL); + if (!(words = ft_split(subst, ' '))) + { + ft_memdel((void*)&subst); + return (NULL); + } + ft_memdel((void*)&subst); + return (words); +} + +char + **p_subst_home(char *words[], + t_msh *msh) +{ + char *path; + char **ptr; + + if ((path = u_get_var_value("$HOME", msh)) == NULL) + return (words); + ptr = words; + while (*ptr != NULL) + { + if (*ptr[0] == '~') + { + *ptr = ft_strsubst(*ptr, "~", path); + } + ptr++; + } + ft_memdel((void*)&path); + return (words); +} diff --git a/src/p_lcom_vars.h b/src/p_lcom_vars.h new file mode 100644 index 0000000..d0732ef --- /dev/null +++ b/src/p_lcom_vars.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_lcom_vars.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_LCOM_VARS_H +#define P_LCOM_VARS_H + +#include <stdint.h> + +#include "s_struct.h" + +void p_add_to_env_fork(int64_t i, char *words[], t_msh *msh); +char **p_subst_args(const char word[], int8_t redir); +char **p_subst_home(char *word[], t_msh *msh); + +#endif Binary files differdiff --git a/src/s_com.c b/src/s_com.c index 28b460f..ebf7b87 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -19,14 +19,15 @@ #include "p_args.h" #include "p_lblock.h" #include "p_lblock_next.h" +>>>>>>> master #include "s_struct.h" static int8_t s_fill_com(char *words[], t_com **com) { - /* TODO: norme */ - uint64_t i; - uint64_t j; + /* TODO: norme is bav (for now)*/ + int64_t i; + int64_t j; i = 0; if (words[0] != NULL) @@ -38,23 +39,19 @@ static int8_t } else return (0); - while(words[i]) + while(words[++i]) { /* TODO: cut fd number "msh ~> echo a 2>file" */ /* ^ */ if (ft_ischarset("<>", words[i][0]) == TRUE) break ; - i++; } if (!((*com)->argv = (char**)malloc((i + 1) * sizeof(char*)))) return (-1); - j = 0; - while (i > 0 && j < i) - { + j = -1; + while (++j < i) if (!((*com)->argv[j] = ft_strdup(words[j]))) return (-1); - j++; - } (*com)->argv[j] = 0; return (0); } diff --git a/src/s_init.c b/src/s_init.c index b229cda..d2cd751 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -14,7 +14,7 @@ #include <stdint.h> #include <stdlib.h> #include <unistd.h> -#include <limits.h> +#include <linux/limits.h> #include <dirent.h> #include <errno.h> diff --git a/src/u_utils.c b/src/u_utils.c index d56d7c0..3d84be3 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -14,6 +14,9 @@ #include <stdlib.h> #include <stdint.h> #include <unistd.h> +#include <signal.h> +#include <term.h> +#include <curses.h> #include "d_define.h" #include "f_fail.h" |