-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
68 lines (53 loc) · 2 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
#include "./commands/prompt/prompt.h"
#include "./commands/headers.h"
#include "./commands/utils/utils.h"
#include "./commands/history/history.h"
#include "./commands/other_commands/other_commands.h"
#include "./commands/signal/signal.h"
int main(){
// Initialising all the extern variable
init();
signal(SIGCHLD, Terminate_Process); // Catches any terminated background process and calls Terminate_Process()
signal(SIGINT, ctrlc);
signal(SIGTSTP, ctrlz);
while (1){
// Printing the prompt
prompt();
// Getting input from user
size_t buff_size = BUFFER_SIZE;
char* buffer = (char *)malloc(BUFFER_SIZE * sizeof(char));
if(buffer == NULL){
perror("Unable to allocate buffer");
}
if(getline(&buffer, &buff_size, stdin) == -1){
save_history();
printf("\n");
exit(EXIT_SUCCESS);
}
if (!strcmp(buffer, "\n"))
continue;
// Updating command history
if(strcmp(prev_command, buffer) != 0){
update_history(buffer);
}
// Tokenising input command based in ';'
total_commands = tokenise_all_commands(buffer);
// Traversing through all the commands
commands_index = 0;
char *temp = (char *)malloc(BUFFER_SIZE * sizeof(char));
for(; commands_index < total_commands; commands_index++){
strcpy(temp, commands[commands_index]);
total_arguments = tokenise_single_command(temp); // tokenising each command based on " \n\t\r"
execute(commands[commands_index]); // executing the command
}
free(temp);
// updating the variable storing previous command
strcpy(prev_command, buffer);
// Making all the commands as NULL
for(int i=0; i<total_commands;i++){
commands[i] = 0; // executing the command
}
free(buffer);
}
return 0;
}