-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_ops.c
106 lines (95 loc) · 2.62 KB
/
find_ops.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_ops.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/27 19:39:54 by zhabri #+# #+# */
/* Updated: 2023/01/09 14:22:57 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int find_quote(const char *s, char c)
{
int i;
i = 0;
while (s[i] && s[i] != (char)c)
i++;
if (s[i] == (char)c)
return (i + 1);
return (1);
}
t_token *init_token(const char *str, int str_idx, int idx, t_label label)
{
t_token *new;
new = malloc(sizeof(t_token));
new->idx = idx;
new->str_idx = str_idx;
new->label = label;
new->str = str;
new->arg = NULL;
new->file = NULL;
new->not_expanded = NULL;
return (new);
}
t_label get_label(const char *op)
{
int i;
static const char *op_tab[7] = {"<<", ">>", "|", ">", "<", "$", NULL};
i = 0;
while (ft_strncmp(op_tab[i], op, ft_strlen(op_tab[i])))
i++;
if (i < 5)
return (i);
return (5);
}
int add_ops(const char *input, t_list **head, int i)
{
int j;
static int idx;
t_token *op;
static const char *op_tab[7] = {"<<", ">>", "|", ">", "<", "$", NULL};
j = 0;
if (!i)
idx = i;
while (op_tab[j])
{
if (!ft_strncmp(input + i, op_tab[j], ft_strlen(op_tab[j])))
{
op = init_token(ft_strdup(op_tab[j]), i, idx, get_label(op_tab[j]));
if ((*op_tab[j]) == '$')
{
find_var(op, (char *)input);
expand(op);
}
ft_lstadd_back(head, ft_lstnew(op));
idx++;
return (ft_strlen(op_tab[j]) - 1);
}
j++;
}
return (0);
}
void get_ops(const char *input, t_list **head)
{
bool single_q;
bool double_q;
size_t i;
i = 0;
single_q = false;
double_q = false;
while (i < ft_strlen(input) && input[i])
{
if (input[i] == '"' && !single_q)
double_q = !double_q;
else if (input[i] == '\'' && !double_q)
single_q = !single_q;
else if (input[i] == '$' && !ft_isprint_nospace_nodollar(input[i + 1]))
i++;
if (input[i] && ((!single_q && !double_q) \
|| (input[i] == '$' && double_q)))
i += add_ops(input, head, i);
i++;
}
}