summaryrefslogtreecommitdiffstats
path: root/src/p_redirs.c
blob: 2c9faa3686e77d54af74a1e1e53767837388f60d (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   p_redirs.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 <libft.h>
#include <stdint.h>
#include <stddef.h>
#include <limits.h>

#include "d_define.h"
#include "s_lredir.h"
#include "s_struct.h"
#include "u_parse.h"

static int32_t	p_get_fd(char word[], char *ptr)
{
	char	digit[255];
	char	*tmp;

	if (*ptr == '<' || (ptr - word) == 1)
		return (0);
	tmp = ptr;
	tmp -= 1;
	if (ft_isdigit(*tmp) == FALSE)
		return (0);
	while ((tmp - word) > 0 && ft_isdigit(*tmp) == TRUE)
		tmp--;
	if ((tmp - word) > 0 && ft_iswhitespace(*tmp) == FALSE)
		return (0);
	else
	{
		tmp += ((tmp - word) > 0) ? (1) : (0);
		ft_strlcpy(digit, word + (tmp - word), (ptr - tmp) + 1);
	}
	return (ft_atoi(digit));
}

static int8_t	p_get_redir(char word[], char *ptr, t_com *com)
{
	struct s_lredir	*rdr;
	/* char			path[PATH_MAX]; */
	size_t			pos[2];
	int32_t			fd;
	int8_t			redir;

	if ((fd = p_get_fd(word, ptr)) < 0)
		fd = 0;
	redir = (*ptr == '>') ? (1) : (-1);
	redir = (redir == 1 && *(ptr + 1) == '>') ? (2) : (redir);
	redir = (redir == -1 && *(ptr + 1) == '<') ? (-2) : (redir);
	if (fd == 0)
		pos[0] = (ptr - word);
	else
		pos[0] = (ptr - word) - ft_intlen(fd);
	/* p_get_path(path, ptr, word); */
	/* ft_printf("fd: %d   redir: %hhd  path: [%s]\n", fd, redir, path); */
	(void)com;
	(void)rdr;
	return (0);
}

int8_t			p_redirs(char word[], t_com **com)
{
	char			*ptr;
	t_quote_mode	mode;

	mode = Q_NONE;
	ptr = (char *)word;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_DQUOTE)
			mode = u_meet_dquote(word, ptr, mode);
		else if (*ptr == C_SQUOTE)
			mode = u_meet_squote(word, ptr, mode);
		else if (mode == Q_NONE && (*ptr == '<' || *ptr == '>') == 1)
		{
			if (p_get_redir(word, ptr, *com) != 0)
				return (1);
			ptr = word;
		}
		ptr++;
	}
	return (0);
}