/
main.c
123 lines (112 loc) · 2.77 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cboussau <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/24 14:38:13 by cboussau #+# #+# */
/* Updated: 2016/04/04 20:56:00 by cboussau ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void push_node(t_lst *node, t_lst **head)
{
t_lst *tmp;
if (*head == NULL)
{
*head = node;
return ;
}
else
tmp = *head;
while (tmp->next)
tmp = tmp->next;
tmp->next = node;
}
t_lst *init_lst(char **env)
{
t_lst *node;
t_lst *head;
head = NULL;
while (*env)
{
node = (t_lst *)malloc(sizeof(t_lst));
if (!node)
return (NULL);
node->next = NULL;
node->line = ft_strdup(*env);
node->name = ft_strsub(*env, 0, ft_strlen_char(*env, '='));
if (ft_strccmp(*env, "USER=", '=') == 0)
node->user = ft_strsub(*env, 5, ft_strlen(*env) - 5);
if (ft_strncmp(*env, "HOME=", 5) == 0)
node->home = ft_strchr(*env, '/');
push_node(node, &head);
env++;
}
return (head);
}
static int do_arg(t_lst *node, char *arg)
{
int i;
char **cmd;
i = 0;
cmd = ft_strsplit_ws(arg);
if (!*cmd)
return (-1);
if (ft_strcmp(*cmd, "exit") == 0)
i = do_exit(arg);
else if (ft_strcmp(*cmd, "env") == 0)
i = deal_with_env(node, arg);
else if (ft_strcmp(*cmd, "setenv") == 0)
i = do_setenv(node, arg);
else if (ft_strcmp(*cmd, "unsetenv") == 0)
i = do_unsetenv(node, arg);
else if (ft_strcmp(*cmd, "cd") == 0)
i = do_cd(node, arg);
else if (*cmd)
i = deal_with_command(node, cmd);
ft_strdel(cmd);
return (i);
}
static int main_minishell(t_lst *node, char **env)
{
char *line;
char **arg;
line = NULL;
while (1)
{
if (check_lst(node) == 0)
node = init_lst(env);
get_prompt(node);
if (get_next_line(0, &line) != 1)
break ;
arg = ft_strsplit(line, ';');
while (*arg)
{
if (do_arg(node, *arg) >= 0)
{
ft_putstr("exit\n");
ft_strdel(arg);
return (0);
}
ft_putchar('\n');
arg++;
}
}
return (0);
}
int main(int ac, char **av, char **env)
{
t_lst *node;
av = NULL;
node = init_lst(env);
signal(SIGINT, SIG_IGN);
if (ac == 1)
main_minishell(node, env);
else
return (1);
if (node)
free_list(node);
return (0);
}