~nloomans/ft_select

ref: 1d90313037b5c7d0545be79e58d8f50a102501ff ft_select/src/read.c -rw-r--r-- 608 bytes
1d903130Noah Loomans handle CTRL+Z and fg 2 years ago
                                                                                
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
#include <unistd.h>
#include <errno.h>
#include <libft.h>
#include <ft_printf.h>
#include "read.h"

struct s_read_seq	read_seq(void)
{
	char				seq[4 + 1];
	struct s_read_seq	ret;

	ft_memset(&ret, '\0', sizeof(ret));
	ft_memset(&seq, '\0', sizeof(seq));
	if (read(STDIN_FILENO, &seq, sizeof(seq) - 1) == -1)
	{
		return ((struct s_read_seq){
			.error = errorf("%s", strerror(errno)),
		});
	}
	if (seq[0] == '\x1b' && seq[1] == '[')
	{
		return ((struct s_read_seq){
			.c = seq[2],
			.type = seq[3] == '~' ? READ_TYPE_ESC_SQL : READ_TYPE_ESC,
		});
	}
	return ((struct s_read_seq){
		.c = seq[0],
	});
}