summaryrefslogtreecommitdiffstats
path: root/src/c_init.c
blob: 1659f3f3d15edbaf332e4f2b3ef03d0c76734dda (plain)
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
168
169
170
171
172
173
174
175
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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 <libft.h>
#include <signal.h>
#include <string.h>
#include <term.h>

#include "b_exit.h"
#include "c_ctrls.h"
#include "c_init.h"
#include "c_keys.h"
#include "c_utils_next.h"
#include "c_input.h"
#include "c_utils.h"
#include "d_define.h"
#include "m_prompt.h"
#include "s_struct.h"
#include "s_com.h"
#include "s_line.h"

short
	c_set_term_raw(char 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 short
	c_read_cap(char *buf, char *line, t_caps *tcaps, t_msh *msh)
{
	if (((*((unsigned int *)buf)) == LEFT_K) ||
		((*((unsigned int *)buf)) == CTRL_B))
		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), tcaps->plen, tcaps));
	else if (((*((unsigned int *)buf)) == HOME_K) ||
			((*((unsigned int *)buf)) == CTRL_A))
		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), tcaps->plen, tcaps));
	else if ((*((unsigned int *)buf)) == CTRL_L)
		return (c_ctrl_l(line, tcaps, msh));
	else if ((*((unsigned int *)buf)) == CTRL_D &&
			line[0] == '\0')
		return (c_ctrl_d(msh));
	return (0);
}

char
	*c_set_ptr(t_bool reset, char key, t_msh *msh)
{
	static char		*p = NULL;
	static char		*context = NULL;
	static t_bool	sw = FALSE;

	if (reset == TRUE)
	{
		p = msh->curr_hist_ptr + ft_strlen(msh->curr_hist_ptr);
		context = msh->curr_hist_ptr;
		return (NULL);
	}
	if (key == -1 && context == msh->curr_hist_ptr
		&& p - context == 0 && msh->prev_hist != NULL)
	{
		p = msh->prev_hist + ft_strlen(msh->prev_hist);
		context = msh->prev_hist;
	}
	if (key == 1 && context == msh->prev_hist
		&& p == ft_strrnchr(msh->prev_hist, '\n', 2) + 1)
	{
		p = msh->curr_hist_ptr;
		context = msh->curr_hist_ptr;
		sw = TRUE;
	}
	if ((key == -1 && p - context != 0) || key == 1)
	{
		p += (key == -1) ? (2 * key) : (0);
		while (sw == FALSE && ((key == -1 && p - context != 0) || key == 1)
			&& *p != C_LF && *p != C_NUL)
			p += key;
		if (sw == TRUE)
			sw = FALSE;
		if (p - context != 0 && *p != C_NUL)
			p += 1;
	}
	return (p);
}

static char
	*c_process_key(char **line, char *buf, t_caps *tcaps, t_msh *msh)
{
	if (ft_isprint(buf[0]))
		c_add_char(line, buf, tcaps);
	else if (((*((unsigned int *)buf)) == UP_K) ||
			((*((unsigned int *)buf)) == CTRL_P) ||
			((*((unsigned int *)buf)) == DOWN_K) ||
			((*((unsigned int *)buf)) == CTRL_N))
	{
		tcaps->key = c_set_key(buf);
		c_key_up_down(line, tcaps, msh);
	}
	else if ((*((unsigned int *)buf)) == DEL_K)
	{
		c_back_slash(line, tcaps->plen, tcaps);
		if ((((tcaps->cpos) + tcaps->plen) %
			tcaps->ws.ws_col) == 0)
			return (NULL);
		c_redraw_line(*line, tcaps);
	}
	else if ((*((unsigned int *)buf)) == CTRL_C)
		return (c_ctrl_c(line, buf, msh));
	else
		c_read_cap(buf, *line, tcaps, msh);
	return ((buf[0] == '\n') ? c_new_line(tcaps) : NULL);
}

short
	c_gnl(int fd, char **line, char psx, t_msh *msh)
{
	t_caps	tcaps;
	char	nread[4];

	c_init_line(psx, &tcaps);
	tputs(tgetstr("cr", NULL), 1, ft_putchar);
	m_prompt_psx(psx, msh);
	ft_bzero(nread, 4);
	msh->sig = 0;
	tcaps.plen = (unsigned int)m_plen(msh->ps[psx - 1]);
	*line = NULL;
	if (*line == NULL)
		if (!(*line = ft_calloc(sizeof(char), 1)))
			return (-1);
	if (!(c_get_win_size(&tcaps.ws)))
		return (-1);
	while (!(ft_strchr(nread, '\n')))
	{
		ft_bzero(nread, 4);
		if (!(read(fd, nread, 3)))
			return (0);
		else
			c_process_key(line, nread, &tcaps, msh);
		tputs(tgetstr("ve", NULL), 1, ft_putchar);
	}
	return (1);
}