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

#include "d_define.h"
#include "e_line.h"
#include "m_loop_counter.h"
#include "m_loop_multis.h"
#include "s_struct.h"
#include "u_utils.h"
#include "s_com.h"
#include "s_lpipes.h"
#include "u_vars_next.h"

short	m_set_all(t_line_block *ptr, unsigned char previf,
				t_bool pipe, t_msh *msh)
{
	size_t			i;

	if ((pipe == TRUE && s_split_pipes(ptr->lblock, msh) == NULL)
		|| (msh->com = s_com_new(ptr->lblock, msh)) == NULL)
	{
		previf = ptr->nextif;
		ptr = ptr->next;
		return (-1);
	}
	if (msh->com != NULL && pipe == FALSE)
	{
		i = 0;
		while (msh->com->argv != NULL && msh->com->argv[i] != NULL)
			i++;
		if (msh->com->argv != NULL && msh->com->argv[0] != NULL)
			u_subst_var_value("$_", msh->com->argv[i - 1], msh);
		else
			u_subst_var_value("$_", msh->com->bin, msh);
	}
	e_line_block(msh);
	s_com_destroy(&msh->com);
	s_lpipes_clear(&msh->pipes);
	return (1);
}

char	*m_check_multi_backslash(int 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(int fd, char line[], t_msh *msh)
{
	char	*pipe;

	if ((pipe = ft_strrchr(line, '|')) != NULL)
	{
		pipe += 1;
		while (*pipe != '\0')
		{
			if (ft_iswhitespace(*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);
}

char	*m_check_multi_and(int fd, char line[], t_msh *msh)
{
	char	*and;

	if ((and = ft_strrchr(line, '&')) != NULL && *(and - 1) == '&')
	{
		and += 1;
		while (*and != '\0')
		{
			if (ft_iswhitespace(*and) == FALSE)
			{
				return (line);
			}
			and++;
		}
		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);
		line = m_check_multi_and(fd, line, msh);
	}
	return (line);
}