summaryrefslogtreecommitdiffstats
path: root/src/m_loop_next.c
blob: 90ffc010be8d3505404d43a38525397d62320ca4 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   m_loop_next.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 <unistd.h>

#include "m_prompt.h"
#include "s_struct.h"
#include "c_init.h"

static char
	*m_counter_line_backslash(int32_t fd, uint8_t psx, char *line, t_msh *msh)
{
	char	*counter_line;

	c_gnl(fd, &counter_line, psx, msh);
	line = ft_nrealloc(line,
		ft_strlen(line) + 1,
		ft_strlen(line) + ft_strlen(counter_line));
	ft_memcpy(line + ft_strlen(line) - 1,
		counter_line,
		ft_strlen(counter_line) + 1);
	ft_memdel((void*)&counter_line);
	return (line);
}

static char
	*m_counter_line_pipes(int32_t fd, uint8_t psx, char *line, t_msh *msh)
{
	char	*counter_line;

	if (fd == STDIN_FILENO)
		m_prompt_psx(psx, msh);
	c_gnl(fd, &counter_line, psx, msh);
	if (counter_line[0] != 0)
	{
		line = ft_nrealloc(line,
			ft_strlen(line) + 1,
			ft_strlen(line) + ft_strlen(counter_line) + 1);
		ft_memcpy(line + ft_strlen(line),
			counter_line,
			ft_strlen(counter_line) + 1);
	}
	ft_memdel((void*)&counter_line);
	return (line);
}

char
	*m_check_multi_backslash(int32_t fd, char *line, t_msh *msh)
{
	if (line[ft_strlen(line) - 1] == '\\')
	{
		line = m_counter_line_backslash(fd, 2, line, msh);
		line = m_check_multi_backslash(fd, line, msh);
	}
	return (line);
}

char
	*m_check_multi_pipe(int32_t fd, char *line, t_msh *msh)
{
	char	*pipe;

	if ((pipe = ft_strrchr(line, '|')) != NULL)
	{
		pipe++;
		while (*pipe != '\0')
		{
			if (ft_isspace(*pipe) == FALSE)
			{
				return (line);
			}
			pipe++;
		}
		line = m_counter_line_pipes(fd, 2, line, msh);
		line = m_check_multi_backslash(fd, line, msh);
		line = m_check_multi_pipe(fd, line, msh);
	}
	return (line);
}