1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 */
/* */
/* ************************************************************************** */
# define FT_KEY_LEFT (char[4]){ 27, 91, 68, 0 }
# define FT_KEY_RIGHT (char[4]){ 27, 91, 67, 0 }
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <string.h>
#include <term.h>
#include <libft.h>
#include "c_init.h"
#include "c_input.h"
#include "c_utils.h"
#include "s_struct.h"
#include "m_prompt.h"
static char
*ft_strjoin_m(char *s1,
char *s2)
{
size_t i;
size_t j;
char *dst;
size_t size1;
size_t size2;
i = -1;
j = -1;
size1 = ft_strlen(s1);
size2 = ft_strlen(s2);
if (!(dst = (char*)malloc((size1 + size2 + 1) * sizeof(char))))
return (NULL);
while (++i < size1)
dst[i] = s1[i];
while (++j < size2)
dst[i + j] = s2[j];
dst[i + j] = '\0';
ft_memdel((void*)&s1);
return (dst);
}
static t_caps
*c_get_struct(int mode,
t_caps *src)
{
static t_caps *caps;
if (mode == 1)
{
caps = src;
}
return (caps);
}
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);
}
/* c_insert_char */
int16_t
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(2, sizeof(char))))
return (0);
if (ft_isprint(buf[0]))
{
if (tcaps->cpos == ft_strlen(line))
line = ft_strjoin_m(line, buf);
else
line = c_insert_char(line, buf[0], tcaps);
c_redraw_line(line, tcaps->cpos, msh);
tcaps->cpos++;
}
else
{
/* if (strncmp(buf, tgetstr("kr", NULL), ft_strlen(tgetstr("kr", NULL))) == 0) */
/* if ((*((unsigned int*)buf)) == FT_KEY_RIGHT) */
if (strncmp(buf, FT_KEY_LEFT, 4) == 0)
{
return (c_key_left(ft_strlen(line), tcaps));
}
else if (buf[0] == '\n')
{
return (c_new_line(buf, &line, msh, tcaps));
}
else if (strncmp(buf, tgetstr("kl", NULL), ft_strlen(tgetstr("kl", NULL))) == 0)
{
return (c_key_left(ft_strlen(line), tcaps));
}
else if (strncmp(buf, tgetstr("kb", NULL), ft_strlen(tgetstr("kb", NULL))) == 0)
{
return (c_back_slash(&line, tcaps));
}
}
return (0);
}
int16_t
c_init_tcaps(t_msh *msh)
{
t_caps tcaps;
char *bp;
char *term;
char nread[5];
int ret;
bp = NULL;
term = getenv("TERM");
if (!tgetent(bp, term))
return (-1);
tcaps.cpos = 0;
c_set_term_raw(1);
c_get_struct(1, &tcaps);
m_prompt_psx(1, msh);
if (!(c_get_win_size(&tcaps.ws)))
return (-1);
while (1)
{
ft_bzero(nread, 5);
if (!(read(STDIN_FILENO, nread, 4)))
return (0);
tputs(tgetstr("vi", NULL), 1, ft_putchar);
ret = c_process_key(nread, &tcaps, msh);
tputs(tgetstr("ve", NULL), 1, ft_putchar);
}
return (1);
}
|