-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
64 lines (58 loc) · 1.76 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
#include "include/main.h"
#include <stdio.h>
/**
* main - function that execute the commands.
* @argc: argument count @argv: array containing the command and the options Return: 0 on success. */
history_t *history = NULL;
int main(int argc, char **argv)
{
char *input, **tokens, *cmd;
(void)argc;
if (argc != 1) {
fprintf(stderr, RED "Low Level Shell: We don't accept arguments here\n");
exit(EXIT_FAILURE);
}
/* Print Version and Exit Information */
puts("\t\t\t\t+---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- ---+");
puts("\t\t\t\t| |");
puts("\t\t\t\t| |");
puts("\t\t\t\t| Low Level Shell Version 0.0.0.1.4 |");
puts("\t\t\t\t| |");
puts("\t\t\t\t| |");
puts("\t\t\t\t+--------------------------------------------------------+");
puts("\n\n\n");
signal(SIGINT, handle_signal);
atexit(free_his);
while (1)
{
if (isatty(STDIN_FILENO) == 1)
prompt("⚡", "➜");
input = get_input();
if (!input)
continue;
if (strcmp(input, "history") != 0)
add_his(input);
tokens = tokenize(input);
if (builtin(tokens, input) == 1) {
_free(2, input, tokens);
continue;
}
cmd = path_handler(tokens[0]);
if (cmd == NULL) {
_write_err(argv[0], "command not found: ", tokens[0]);
_free(2, input, tokens);
continue;
}
if (execute(tokens, cmd) == -100)
_write_err(argv[0], "An error occurred while executing -> ", tokens[0]);
_free(3, input, tokens, cmd);
}
return (0);
}
void handle_signal(int signum) {
if (signum == SIGINT) {
putchar('\n');
prompt("⚡", "➜");;
fflush(stdout);
}
}