-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.c
More file actions
76 lines (66 loc) · 1.27 KB
/
shell.c
File metadata and controls
76 lines (66 loc) · 1.27 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
#include "shell.h"
/**
* main - a simple shell interpreter
* @ac: arg count
* @av: arg vector
* @env: the evironment
* Return: never returns until a kill signal is sent
*/
int main(int ac __attribute__((unused)), char **av, char **env)
{
char **args, *buff;
ssize_t flag;
int index;
signal(SIGINT, sigint_handler);
if (av[1])
{
file_mode(av, env);
}
read_history();
aliasing(NULL);
flag = 1;
while (flag > 0)
{
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "my_shell$ ", 10);
buff = NULL;
flag = get_input(&buff, av, env);
if (add_to_history(buff) == -1)
perror(av[0]);
comments(buff);
while ((buff = check_separator(buff)))
{
while ((buff = logical_and(buff)))
{
while ((buff = logical_or(buff)))
{
args = NULL;
args = parse_command(buff, " ");
free(buff);
expand_vars(args);
index = check_builtin(args[0]);
if (index != -1)
exec_builtin(index, args);
else
if (execute(args, env) == -1)
perror(av[0]);
free_args(args);
}
}
}
}
return (0);
}
/**
* sigint_handler - restarts the program on ctrl-c
* @sig: the signal number
* Return: nothing
*/
void sigint_handler(int sig)
{
if (sig == 2)
{
write(STDOUT_FILENO, "\nmy_shell$ ", 11);
return;
}
}