-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathminishell.c
112 lines (103 loc) · 2.63 KB
/
minishell.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/14 15:35:02 by bmachado #+# #+# */
/* Updated: 2022/02/23 15:44:16 by bmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int execute_only_cmd(t_shell *tab, char **cmds)
{
int i;
i = 0;
while (cmds[i])
{
if (execute_cmd(tab, &cmds[i]))
break ;
i++;
}
return (1);
}
void exec_parsed_cmdlst(t_shell *table, char **cmds, t_token **tk_lst)
{
int i;
int start;
start = 0;
i = 0;
if (!is_chained_command(tk_lst))
execute_only_cmd(table, cmds);
else
{
set_initial_input(table);
i = in_redirected(cmds, table);
if (i == -1)
return ;
start = i;
while (cmds[i])
{
if (ft_strcmp(cmds[i], "|") == 0)
start = piped_cmd(table, cmds, start, i);
else if (!cmds[i + 1])
last_piped_cmd(table, cmds, start, i);
i++;
}
restore_initial_input(table);
}
wait_for_child(table->proc.child);
}
static void run_cmd(char *option, t_shell *table, t_token *token_lst)
{
if (!valid_quote(option))
{
printf("minishell: unclosed quotes\n");
g_shell.error = 1;
return ;
}
set_cmd_lst(table, token_lst, option);
unlink(TMP_PATH);
}
static void initialization(t_shell *shell, char **environ)
{
envtable(shell, environ);
shell->running = 1;
shell->proc.pid = 0;
g_shell.error = 0;
g_shell.env = NULL;
g_shell.path = NULL;
shell->proc.child = 0;
shell->proc.tmpin = 0;
shell->proc.tmpout = 0;
shell->proc.fdin = 0;
shell->proc.fdout = 0;
init_term();
}
int main(int argc, char **argv, char **environ)
{
t_shell *shell;
t_token *token_lst;
char *input;
(void)argc;
shell = create_table(CAPACITY);
initialization(shell, environ);
while (shell->running)
{
token_lst = NULL;
input = NULL;
if (argv[1])
{
input_argv(argv, &input);
run_cmd(input, shell, token_lst);
break ;
}
if (get_input(shell, &input))
run_cmd(input, shell, token_lst);
if (input)
free(input);
}
free_table(shell);
exit(g_shell.error);
}