summaryrefslogtreecommitdiffstats
path: root/src/p_subst_alias.c
blob: 9e250877ae1906f9abffaa4737a0082f3aae271e (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   p_subst_alias.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 <stddef.h>

#include "s_struct.h"
#include "u_alias.h"
#include "u_parse.h"
#include "u_utils.h"
#include "u_vars.h"

static char		*p_skip_whitespace(char *ptr)
{
	while (*ptr != C_NUL && ft_iswhitespace(*ptr))
	{
		ptr++;
	}
	return (ptr);
}

static t_bool	p_meet_whitespace(char *head, char *ptr, t_quote_mode mode)
{
	if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE)
	{
		return (TRUE);
	}
	return (FALSE);
}

static char		*p_set_ptr(char *ptr, char word[], t_bool *good, size_t locat[])
{
	t_quote_mode	mode;

	mode = Q_NONE;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_DQUOTE)
			mode = u_meet_dquote(word, ptr, mode);
		if (*ptr == C_SQUOTE)
			mode = u_meet_squote(word, ptr, mode);
		if (mode == Q_NONE && *ptr == C_EQUALS)
			*good = FALSE;
		if (ft_iswhitespace(*ptr) == TRUE
			&& p_meet_whitespace((char*)word, ptr, mode) == TRUE)
		{
			locat[1] = (ptr - word);
			if (*good == TRUE)
				break ;
			else
			{
				ptr = p_skip_whitespace(ptr);
				locat[0] = (ptr - word);
				ptr -= 1;
				*good = TRUE;
			}
		}
		ptr++;
	}
	return (ptr);
}

size_t			p_subst_alias(char word[], t_bool reset, t_msh *msh)
{
	static size_t	used[4096];
	static size_t	i = 0;
	char			value[ARG_MAX];
	char			tmp[255];
	size_t			locat[2];
	size_t			j;
	size_t			usedcmp;
	char			*ptr;
	t_bool			good;

	if (reset == TRUE)
	{
		i = 0;
		while (i < 4096)
		{
			used[i] = 0;
			i++;
		}
		i = 0;
	}
	ptr = word;
	ptr = p_skip_whitespace(ptr);
	good = TRUE;
	locat[0] = (ptr - word);
	locat[1] = (ptr - word);
	ptr = p_set_ptr(ptr, word, &good, locat);
	if (*ptr == C_NUL && good == TRUE)
		locat[1] = (ptr - word);
	if (good == TRUE)
	{
		ft_strlcpy(tmp,
			word + locat[0],
			((locat[1] - locat[0] < 253) ? (locat[1] - locat[0]) : (254)) + 1);
		if ((usedcmp = u_get_alias_value(value, tmp, ARG_MAX, msh)) != 0)
		{
			j = 0;
			good = TRUE;
			while (j < i)
			{
				if (used[j] == usedcmp)
					good = FALSE;
				j++;
			}
			if (good == TRUE)
			{
				(void)ft_memmove(word + (locat[0] + ft_strlen(value)),
					word + locat[1],
					ft_strlen(word + locat[1]) + 1 * sizeof(char));
				(void)ft_memmove(word + locat[0],
					value,
					ft_strlen(value) * sizeof(char));
				used[i] = usedcmp;
				i++;
				return (usedcmp);
			}
		}
	}
	return (0);
}