-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
162 lines (153 loc) · 4.5 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "headers.h"
int identifier = 0;
char *last_command = NULL;
int last_ex_t = 0;
pid_t fgProcessPid = 0;
struct termios original_termios;
struct BackgroundProcess bgProcesses[MAX_COMMANDS];
int numBgProcesses = 0;
int main()
{
char command[10000];
char inp[10000];
char *uname = malloc(sizeof(char) * 10000);
char *sname = malloc(sizeof(char) * 100000);
char cwd[MAX_INPUT_SIZE];
char pwd[MAX_INPUT_SIZE];
char prev_dir[MAX_INPUT_SIZE];
char *home_dir;
char var[MAX_INPUT_SIZE];
char relative_dir[10000];
getcwd(cwd, MAX_INPUT_SIZE);
strcpy(var, cwd);
char past_events[MAX_COMMANDS][MAX_COMMAND_LENGTH];
int count = 0;
load_past_events(past_events, &count);
int flag = 0;
signal(SIGINT, handleSIGINT);
struct termios original_termios;
tcgetattr(STDIN_FILENO, &original_termios);
while (1)
{
handleBackgroundProcessTermination();
get_prompt(uname, sname, cwd, &home_dir, relative_dir, var);
if (fgets(command, 10000, stdin) == NULL)
{
break;
}
command[strcspn(command, "\n")] = 0;
strcpy(inp, command);
if (strncmp(command, "pastevents", 10) != 0 && strncmp(command, "pastevents execute", 18) != 0 && strstr(command, "pastevents") == 0)
{
load_past_events(past_events, &count);
if (count == 0 || strcmp(command, past_events[count - 1]) != 0)
{
if (count == MAX_COMMANDS)
{
for (int i = 0; i < count - 1; i++)
{
strcpy(past_events[i], past_events[i + 1]);
}
count--;
}
strcpy(past_events[count], command);
count++;
save_past_events(past_events, count);
}
}
if (strcmp(command, "exit") == 0)
{
break;
}
if (strncmp(command, "proclore", 8) == 0)
{
flag = 1;
read_command(command);
}
if (strstr(command, ">") || strstr(command, "<"))
{
if (!strstr(command, "|"))
{
flag = 1;
redirection(command);
}
}
if (strstr(command, "|") && !(strstr(command, ">") || strstr(command, "<")))
{
flag = 1;
execute_pipe(command);
}
if (strstr(command, "|") && (strstr(command, ">") || strstr(command, "<")))
{
flag = 1;
execute_pipe_dir(command);
}
if (strcmp(command, "activities") == 0)
{
flag = 1;
printActivities(bgProcesses, numBgProcesses);
}
if (command[strlen(command) - 2] == '&')
{
flag = 1;
printf("hi\n");
executeInBackground(command);
}
if (strncmp(command, "ping", 4) == 0)
{
flag = 1;
handle_ping(command);
}
if (strncmp(command, "fg", 2) == 0)
{
flag = 1;
handle_fg(command);
}
if (strncmp(command, "bg", 2) == 0)
{
flag = 1;
handle_bg(command);
}
if (strncmp(command, "neonate -n ", 11) == 0)
{
flag = 1;
int time_arg = atoi(command + 11);
if (time_arg <= 0)
{
fprintf(stderr, "Invalid time_arg. Please provide a positive integer.\n");
return 1;
}
// Register the handle_signal function to handle Ctrl+C
signal(SIGINT, handle_signal);
handle_neonate(time_arg);
}
if (strncmp(command, "iMan", 4) == 0)
{
flag = 1;
// Extract the command name from the string
char *command_name = command + 5;
// Check if the command name is not empty
if (*command_name != '\0')
{
fetch_man_page(command_name);
}
else
{
fprintf(stderr, "Usage: iMan <command_name>\n");
return 1;
}
}
if (flag == 0)
{
parse_and_execute(command, &count, cwd, home_dir, var, prev_dir, past_events);
}
flag = 0;
identifier = 0;
}
while (numBgProcesses > 0)
{
handleBackgroundProcessTermination();
}
tcsetattr(STDIN_FILENO, TCSANOW, &original_termios);
return 0;
}