summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsalad <fmoenne-@student.le-101.fr>2020-11-03 00:03:48 +0100
committersalad <fmoenne-@student.le-101.fr>2020-11-03 00:03:48 +0100
commitb18fbce9a7e33f7e54949ae8de32fd7fc42e6ad1 (patch)
tree859e4d58e05622ef7a402e50ddd2ec913a51ef5d
parentstrplen (diff)
download42-minishell-b18fbce9a7e33f7e54949ae8de32fd7fc42e6ad1.tar.gz
42-minishell-b18fbce9a7e33f7e54949ae8de32fd7fc42e6ad1.tar.bz2
42-minishell-b18fbce9a7e33f7e54949ae8de32fd7fc42e6ad1.tar.xz
42-minishell-b18fbce9a7e33f7e54949ae8de32fd7fc42e6ad1.tar.zst
42-minishell-b18fbce9a7e33f7e54949ae8de32fd7fc42e6ad1.zip
this is it \(2009\) \: a Micheal Jackson Biopic
-rw-r--r--libft/src/plen0
-rw-r--r--src/c_init.c50
-rw-r--r--src/c_init.h1
-rw-r--r--src/c_input.c3
-rw-r--r--src/c_input.h8
-rw-r--r--src/c_keys.c2
-rw-r--r--src/c_utils.c15
-rw-r--r--src/m_loop.c6
-rw-r--r--src/m_prompt.c32
-rw-r--r--src/m_prompt.h3
10 files changed, 59 insertions, 61 deletions
diff --git a/libft/src/plen b/libft/src/plen
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/libft/src/plen
diff --git a/src/c_init.c b/src/c_init.c
index 53af0a3..1023ced 100644
--- a/src/c_init.c
+++ b/src/c_init.c
@@ -14,6 +14,7 @@
#include <stdlib.h>
#include <libft.h>
#include <signal.h>
+#include <string.h>
#include <term.h>
#include "c_init.h"
@@ -23,30 +24,6 @@
#include "s_struct.h"
#include "m_prompt.h"
-unsigned int
- c_plen(char *prompt)
-{
- unsigned int i;
- unsigned int size;
-
- i = 0;
- size = 0;
- while (prompt[i] != '\0')
- {
- if (ft_isalpha(prompt[i]))
- {
- i++;
- size++;
- }
- else
- {
-
- i++;
- }
- }
- return (size);
-}
-
short
c_set_term_raw(char mode)
{
@@ -76,7 +53,7 @@ static void
{
*line = c_insert_char(*line, buf[0], tcaps);
tcaps->cpos++;
- if ((((tcaps->cpos) + ft_strlen(msh->ps[tcaps->psx])) %
+ if ((((tcaps->cpos) + tcaps->plen) %
tcaps->ws.ws_col) == 0)
{
write(1, "\n\r", 2);
@@ -88,21 +65,18 @@ static void
static short
c_read_cap(char *buf, char *line, t_caps *tcaps, t_msh *msh)
{
- unsigned int 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));
+ return (c_key_left(tcaps->plen, tcaps));
else if (((*((unsigned int *)buf)) == RIGHT_K) ||
((*((unsigned int *)buf)) == CTRL_F))
- return (c_key_right(ft_strlen(line), plen, tcaps));
+ return (c_key_right(ft_strlen(line), tcaps->plen, tcaps));
else if (((*((unsigned int *)buf)) == HOME_K) ||
((*((unsigned int *)buf)) == CTRL_A))
- return (c_home_key(plen, tcaps));
+ return (c_home_key(tcaps->plen, tcaps));
else if (((*((unsigned int *)buf)) == END_K) ||
((*((unsigned int *)buf)) == CTRL_E))
- return (c_end_key(ft_strlen(line), plen, tcaps));
+ return (c_end_key(ft_strlen(line), tcaps->plen, tcaps));
else if ((*((unsigned int *)buf)) == CTRL_L)
return (c_ctrl_l(line, tcaps, msh));
else if ((*((unsigned int *)buf)) == CTRL_C)
@@ -128,8 +102,8 @@ static char
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])) %
+ c_back_slash(&line, tcaps->plen, tcaps);
+ if ((((tcaps->cpos) + tcaps->plen) %
tcaps->ws.ws_col) == 0)
{
return (NULL);
@@ -152,17 +126,13 @@ short
c_init_line(psx, &tcaps);
tputs(tgetstr("cr", NULL), 1, ft_putchar);
m_prompt_psx(psx, msh);
- ft_printf("%u", c_plen(msh->ps[0]));
ft_bzero(nread, 5);
+ tcaps.plen = m_plen(msh->ps[psx - 1]);
+ tcaps.plen += (psx != 1);
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);
diff --git a/src/c_init.h b/src/c_init.h
index db7d777..ac380dd 100644
--- a/src/c_init.h
+++ b/src/c_init.h
@@ -41,6 +41,7 @@ typedef struct s_caps
unsigned int lpos;
unsigned int nlines;
unsigned short psx;
+ unsigned short plen;
} t_caps;
short c_gnl(int fd, char **line, char psx, t_msh *msh);
diff --git a/src/c_input.c b/src/c_input.c
index 24e45e3..7a23b90 100644
--- a/src/c_input.c
+++ b/src/c_input.c
@@ -58,7 +58,8 @@ unsigned short
}
unsigned int
- c_get_line_num(char *line, unsigned int cpos, unsigned int plen, t_caps *tcaps)
+ c_get_line_num(char *line, unsigned int cpos,
+ unsigned int plen, t_caps *tcaps)
{
unsigned int it;
unsigned int line_num;
diff --git a/src/c_input.h b/src/c_input.h
index 01aaa15..7ba697f 100644
--- a/src/c_input.h
+++ b/src/c_input.h
@@ -13,11 +13,11 @@
#ifndef C_INPUT_H
# define C_INPUT_H
-short c_back_slash(char **line, unsigned int plen, t_caps *tcaps);
-short c_ctrl_c(char *line, t_caps *tcaps, t_msh *msh);
+short c_back_slash(char **line, unsigned int plen, t_caps *tcaps);
+short c_ctrl_c(char *line, t_caps *tcaps, t_msh *msh);
unsigned int c_get_line_num(char *line, unsigned int cpos, unsigned int plen,
- t_caps *tcaps);
-short c_init_line(char psx, t_caps *tcaps);
+ t_caps *tcaps);
+short c_init_line(char psx, t_caps *tcaps);
unsigned short c_get_win_size(struct winsize *ws);
#endif
diff --git a/src/c_keys.c b/src/c_keys.c
index 593c6d4..6d472c4 100644
--- a/src/c_keys.c
+++ b/src/c_keys.c
@@ -30,7 +30,7 @@ short
c_home_key(unsigned int plen, t_caps *tcaps)
{
unsigned int i;
- short j;
+ short j;
i = tcaps->cpos;
j = tcaps->lpos;
diff --git a/src/c_utils.c b/src/c_utils.c
index 3198dff..a25a35a 100644
--- a/src/c_utils.c
+++ b/src/c_utils.c
@@ -24,8 +24,8 @@
char
*c_new_line(char *line, t_caps *tcaps)
{
- char *ret;
- size_t i;
+ char *ret;
+ size_t i;
i = tcaps->lpos;
ret = NULL;
@@ -50,7 +50,7 @@ char
char
*c_insert_char(char *str, char c, t_caps *tcaps)
{
- int i;
+ int i;
size_t j;
size_t len;
char *dst;
@@ -77,9 +77,9 @@ char
char
*c_delchar(char *str, unsigned short cpos)
{
- char *dst;
unsigned short i;
unsigned short j;
+ char *dst;
j = 0;
i = 0;
@@ -135,8 +135,9 @@ short
unsigned int i;
unsigned int j;
+ (void)msh;
tcaps->nlines = c_get_line_num(line, tcaps->cpos,
- ft_strlen(msh->ps[tcaps->psx]), tcaps);
+ tcaps->plen, tcaps);
i = 0;
j = tcaps->lpos;
tputs(tgetstr("cr", NULL), 1, ft_putchar);
@@ -146,10 +147,10 @@ short
tputs(tgetstr("up", NULL), 1, ft_putchar);
tputs(tgetstr("up", NULL), 1, ft_putchar);
}
- while (i++ < ft_strlen(msh->ps[tcaps->psx]))
+ while (i++ < tcaps->plen)
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) :
+ c_redraw_next(tcaps->plen, ft_strlen(line), tcaps) :
ft_strlen(line));
}
diff --git a/src/m_loop.c b/src/m_loop.c
index 83caf6a..03ce8e2 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -138,10 +138,8 @@ unsigned char m_loop(int fd, t_msh *msh)
msh->curr_hist_ptr = hist;
while (gnl > 0)
{
- if (fd == STDIN_FILENO)
- gnl = c_gnl(fd, &line, 1, msh);
- else
- gnl = get_next_line(fd, &line);
+ gnl = (fd == STDIN_FILENO) ? c_gnl(fd, &line, 1, msh) :
+ get_next_line(fd, &line);
m_delete_comments(line);
if (line[0] != C_NUL)
{
diff --git a/src/m_prompt.c b/src/m_prompt.c
index e36a07e..0fbd9f2 100644
--- a/src/m_prompt.c
+++ b/src/m_prompt.c
@@ -25,7 +25,7 @@
#include "s_struct.h"
#include "u_vars.h"
-static void m_subst_prompt_rice(char var[], t_msh *msh)
+static void m_subst_prompt_rice(char var[], t_msh *msh)
{
size_t i;
@@ -43,7 +43,7 @@ static void m_subst_prompt_rice(char var[], t_msh *msh)
}
}
-static void m_update_psx(unsigned char x, t_msh *msh)
+static void m_update_psx(unsigned char x, t_msh *msh)
{
char var[ARG_MAX];
char psx[5];
@@ -59,7 +59,33 @@ static void m_update_psx(unsigned char x, t_msh *msh)
ft_strlcpy(msh->ps[x - 1], var, 255);
}
-void m_prompt_psx(unsigned char x, t_msh *msh)
+unsigned int m_plen(char *prompt)
+{
+ unsigned int i;
+ unsigned int size;
+
+ i = 0;
+ size = 0;
+ while (prompt[i] != '\0')
+ {
+ if (prompt[i] == 27)
+ {
+ while (prompt[i] != 'm' && prompt[i] != '\0')
+ i++;
+ }
+ if (ft_isalnum(prompt[i]) ||
+ ft_ischarset("<>", prompt[i]))
+ {
+ size++;
+ i++;
+ }
+ else
+ i++;
+ }
+ return (size);
+}
+
+void m_prompt_psx(unsigned char x, t_msh *msh)
{
m_update_psx(x, msh);
ft_dprintf(STDERR_FILENO, "%s", msh->ps[x - 1]);
diff --git a/src/m_prompt.h b/src/m_prompt.h
index 3170ecc..59f627f 100644
--- a/src/m_prompt.h
+++ b/src/m_prompt.h
@@ -15,6 +15,7 @@
# include "s_struct.h"
-void m_prompt_psx(unsigned char x, t_msh *msh);
+unsigned int m_plen(char *prompt);
+void m_prompt_psx(unsigned char x, t_msh *msh);
#endif