-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
106 lines (95 loc) · 2.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyumkim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 16:28:43 by jaemikim #+# #+# */
/* Updated: 2024/08/07 06:30:48 by kyumkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void parse_envp(t_line *line, char **envp);
char **env_split(char *str);
t_line *init_line(void);
void free_lines(t_line *line);
int main(int argc, char **argv, char **envp)
{
char *line;
struct termios term;
t_line *cmd_line;
if (argc != 1 || argv[1] != NULL)
print_error("main", NULL, "argument error");
cmd_line = init_line();
parse_envp(cmd_line, envp);
tcgetattr(0, &term);
set_signal();
line = readline("minishell$ ");
while (line)
{
if ((line[0] != '\0') && (!iswhitespace(line)))
{
add_history(line);
tokenize_main(line, cmd_line);
execute(cmd_line);
}
free(line);
line = readline("minishell$ ");
}
free_lines(cmd_line);
sig_term_handler();
tcsetattr(0, TCSANOW, &term);
}
t_line *init_line(void)
{
t_line *line;
line = (t_line *)malloc(sizeof(t_line));
if (line == NULL)
exit(1);
line->env = NULL;
line->exit_flag = 0;
return (line);
}
char **env_split(char *str)
{
char **ret;
int idx;
ret = (char **)malloc(sizeof(char *) * 2);
idx = 0;
while (str[idx] != '=' && str[idx] != '\0')
idx++;
if (str[idx] == '\0')
{
ret[0] = ft_strdup(str);
if (ret[0] == NULL)
exit(1);
ret[1] = NULL;
return (ret);
}
ret[0] = ft_substr(str, 0, idx);
ret[1] = ft_strdup(str + idx + 1);
if (ret[0] == NULL || ret[1] == NULL)
exit(1);
return (ret);
}
void parse_envp(t_line *line, char **envp)
{
char **split;
int idx;
idx = 0;
while (envp[idx] != NULL)
{
split = env_split(envp[idx]);
new_env(line, split[0], split[1]);
free(split[0]);
free(split[1]);
free(split);
idx++;
}
}
void free_lines(t_line *line)
{
free_env(line->env);
free(line);
}