~nloomans/ft_select

ref: a4ef762331941c5e33670f7bf81eb964fb761edb ft_select/src/read.c -rw-r--r-- 1.5 KiB
a4ef7623Noah Loomans fix build on linux 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
31
32
33
34
35
36
37
38
39
40
41
42
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#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],
	});
}