-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminishell.c
125 lines (115 loc) · 2.72 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
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ynafiss <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/14 14:23:24 by ynafiss #+# #+# */
/* Updated: 2023/05/17 16:24:51 by ynafiss ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_ret(char **ret)
{
int i;
int j;
i = 0;
j = 0;
while (ret[i])
{
j = 0;
while (ret[i][j])
{
write(1, &ret[i][j], 1);
j++;
}
write(1, "\n", 1);
i++;
}
}
void print_queue(t_queue *queue)
{
int i;
t_queue_node *node;
t_cmd *cmd;
node = queue->head;
while (node)
{
i = 0;
cmd = node->ptr;
printf("Args: ");
while (cmd->args[i])
printf("(%s) ", cmd->args[i++]);
printf("\n");
i = 0;
printf("Files: ");
while (cmd->files[i])
{
printf("(%s type: %d, in quotes: %d) ", cmd->files[i]->filename,
cmd->files[i]->type, cmd->files[i]->inqt);
i++;
}
printf("\n");
printf("COMMANDs: %d\n", queue->len);
node = node->next;
}
}
void free_cmd(void *v)
{
t_cmd *p;
int i;
i = 0;
p = v;
ft_free(p->args);
while (p->files[i])
{
free(p->files[i]->filename);
free(p->files[i]);
i++;
}
free(p->files);
free(p);
}
void while_1(t_data *vars, char **env, t_vars t, struct termios *term)
{
t.env = full_vars(env);
while (1)
{
tcgetattr(0, term);
handle_signals(0);
g_data.sigflag = 0;
vars->line = readline("minishell → ");
if (!vars->line)
(ft_putstr_fd("exit\n", 2), exit (0));
if (!vars->line[0] || parse_start(vars, vars->env))
{
if (vars->line[0])
(add_history(vars->line), free(vars->line));
else
free(vars->line);
continue ;
}
g_data.sigflag = 1;
multipipe(vars, env, vars->env, t);
(queue_free(&vars->commands, free_cmd), add_history(vars->line));
free(vars->line);
g_data.sigflag = 0;
tcsetattr(0, TCSANOW, term);
}
}
int main(int argc, char **argv, char **env)
{
t_data *vars;
t_vars *t;
struct termios term;
(void)argc;
(void)argv;
t = malloc(sizeof(t_vars));
vars = malloc(sizeof(t_data));
vars->env = full_env(env);
g_data.g_exit = 0;
g_data.sigflag = 0;
t->fd_h = NULL;
while_1(vars, env, *t, &term);
}