summaryrefslogtreecommitdiffstats
path: root/src/m_loop_multis.c
blob: 8fc9e7d381a52e53758abb30007dc33a55882b99 (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
108
109
110
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   m_loop_multis.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 "m_loop_counter.h"
#include "m_loop_next.h"
#include "u_utils.h"

static t_bool	m_check_dquote(char **ptr, char line[])
{
	if (((*ptr - line) == 0) ||
		((*ptr - line) == 1 && *(*ptr - 1) == C_BACKS) ||
		((*ptr - line) > 1 && *(*ptr - 1) == C_BACKS && *(*ptr - 2) != C_BACKS))
	{
		return (FALSE);
	}
	else
	{
		return (TRUE);
	}
}

static t_bool	m_find_next_quote(char **ptr, char line[], t_quote_mode mode)
{
	char	c;

	if (mode == Q_NONE)
		return (FALSE);
	c = (mode == Q_SINGLE) ? (C_SQUOTE) : (C_DQUOTE);
	(*ptr) += 1;
	while (**ptr != C_NUL)
	{
		if (**ptr == c && c == C_DQUOTE)
		{
			if (m_check_dquote(ptr, line) == FALSE)
			{
				(*ptr)++;
				if (**ptr == C_NUL)
					break ;
			}
			else
				return (FALSE);
		}
		else if (**ptr == c && c == C_SQUOTE)
			return (FALSE);
		(*ptr)++;
	}
	return (TRUE);
}

static t_bool	m_check_missing_quotes(char line[])
{
	char			*ptr;
	t_quote_mode	mode;

	ptr = line;
	mode = Q_NONE;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_SQUOTE || *ptr == C_DQUOTE)
		{
			if (u_is_not_escaped(line, ptr) == TRUE)
			{
				mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE);
				if (m_find_next_quote(&ptr, line, mode) == FALSE)
					mode = Q_NONE;
				else
					return (TRUE);
			}
		}
		ptr++;
	}
	return (FALSE);
}

char			*m_check_multi_quotes(int fd, char line[], t_msh *msh)
{
	t_bool	reparse;

	reparse = FALSE;
	reparse = m_check_missing_quotes(line);
	if (reparse == TRUE)
	{
		line = m_counter_line_quotes(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);
		line = m_check_multi_quotes(fd, line, msh);
	}
	return (line);
}

char			*m_check_multis(int fd, char line[], t_msh *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);
	line = m_check_multi_quotes(fd, line, msh);
	return (line);
}