-
Notifications
You must be signed in to change notification settings - Fork 0
/
dollar.c
127 lines (117 loc) · 3.05 KB
/
dollar.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dollar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <zhabri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 13:06:15 by zhabri #+# #+# */
/* Updated: 2023/01/13 14:03:50 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void infile_exception(t_token *var)
{
int i;
char *second_part;
char **arg_split;
arg_split = ft_split_quotes_keep_sep(var->arg, "<>|");
second_part = ft_calloc(1, sizeof(char));
if (arg_split[0] && arg_split[1])
{
i = 1;
while (arg_split[i])
{
infile_exception_tmp(&second_part, arg_split[i]);
i++;
}
}
free(var->arg);
if (arg_split[0] && (arg_split[0][0] == '<' \
|| arg_split[0][0] == '>' || arg_split[0][0] == '|'))
var->arg = ft_strjoinf(ft_strjoin("\"", arg_split[0]), "\"");
else
var->arg = ft_strdup(arg_split[0]);
var->arg = ft_strjoinf(var->arg, second_part);
free(second_part);
free_tab(arg_split);
}
static void expand_bis(t_list *curr, t_token *var, char *env, char *tmp)
{
if (tmp[0] != '?')
{
if (curr && env)
{
var->arg = ft_strdup(ft_strchr(env, '=') + 1);
infile_exception(var);
}
else
{
if (ft_strlen(tmp) == 1)
var->arg = ft_strdup("$");
else
var->arg = ft_strdup("");
}
}
else
var->arg = ft_strjoinf(ft_itoa(g_glob->exit_ret), \
var->not_expanded + 1);
free(tmp);
}
void expand(t_token *var)
{
char *tmp;
char *env;
t_list *curr;
tmp = ft_strjoin(var->arg, "=");
var->not_expanded = ft_strdup(var->arg);
curr = *g_glob->envp;
env = (char *)curr->content;
while (curr && ft_strncmp(env, tmp, ft_strlen(tmp)))
{
curr = curr->next;
if (curr)
env = (char *)curr->content;
}
free(var->arg);
expand_bis(curr, var, env, tmp);
}
static bool str_is_op(char *needle)
{
int i;
static const char *op_tab[13] = {"<<", ">>", "|", \
">", "<", " ", "$", "\"", "'", "/", "\t", NULL};
i = 0;
while (op_tab[i])
{
if (!strncmp(op_tab[i], needle, ft_strlen(op_tab[i])))
return (true);
i++;
}
return (false);
}
void find_var(t_token *token, char *input)
{
int i;
size_t len;
len = 0;
if (token)
{
i = token->str_idx + 1;
if (input[i] == '?')
token->arg = ft_strdup("?");
else
{
while (input[i] && !str_is_op(input + i))
{
i++;
len++;
}
if (len == 0 && (input[i] == '\'' || input[i] == '\"') \
&& i > 1 && input[i - 2] != '\"')
token->arg = ft_strdup("-");
else
token->arg = ft_substr(input, token->str_idx + 1, len);
}
}
}