-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
126 lines (115 loc) · 3.04 KB
/
input.c
File metadata and controls
126 lines (115 loc) · 3.04 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fnagy <fnagy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/30 00:29:49 by elavrich #+# #+# */
/* Updated: 2025/07/17 10:30:32 by fnagy ### ########.fr */
/* */
/* ************************************************************************** */
#include "Minishell.h"
static int handle_meta(char *str, t_token **tokens, int i, bool quoted)
{
int start;
char *word;
start = i;
while (str[i] && is_meta(str[i]))
i++;
word = ft_substr(str, start, i - start);
if (!word)
return (-1);
add_token(tokens, word, quoted);
free(word);
return (i);
}
int input(char *str, t_token **tokens, t_shell *shell)
{
int i;
i = 0;
while (str[i])
{
while (str[i] == ' ')
i++;
if (!str[i])
break ;
if (is_meta(str[i]))
i = handle_meta(str, tokens, i, false);
else
{
i = make_tok(tokens, str, i, shell);
if (!i)
close_free(tokens, shell);
}
if (i < 0)
return (printf("error!\n"), i);
}
return (i);
}
int make_tok(t_token **tokens, char *str, int i, t_shell *shell)
{
t_token_b *tks;
tks = malloc(sizeof(t_token_b));
if (!tks)
return (-1);
tks->builder = ft_strdup("");
tks->quoted = false;
if (!tks->builder)
return (free(tks), -1);
while (str[i] && str[i] != ' ' && !is_meta(str[i]))
{
if (is_meta(str[i]))
return (i);
if (str[i] == '\'' || str[i] == '"')
{
i = handle_q(&tks, str, i, shell);
tks->quoted = true;
}
else
i = simple_word(&tks, str, i, shell);
if (i < 0)
return (free(tks->builder), free(tks), -1);
}
add_token(tokens, tks->builder, tks->quoted);
return (free(tks->builder), free(tks), i);
}
int handle_q(t_token_b **tks, char *str, int i, t_shell *shell)
{
int start;
int flag;
char *word;
int tmp;
tmp = i;
start = ++i;
if (str[tmp] == '\'')
{
flag = NO_EXP;
while (str[i] && str[i] != '\'')
i++;
}
else if (str[tmp] == '"')
{
flag = EXPAND;
while (str[i] && str[i] != '"')
i++;
}
if (!str[i])
return (printf("Unclosed quote\n"), -1);
word = process_word(ft_substr(str, start, i - start), shell, flag);
(*tks)->builder = join_and_free((*tks)->builder, word);
i++;
return (i);
}
int simple_word(t_token_b **tks, char *str, int i, t_shell *shell)
{
int start;
char *word;
start = i;
while (str[i] && str[i] != ' ' && !is_meta(str[i]) && str[i] != '\''
&& str[i] != '"')
i++;
word = process_word(ft_substr(str, start, i - start), shell, EXPAND);
(*tks)->builder = join_and_free((*tks)->builder, word);
return (i);
}