-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
143 lines (122 loc) · 3.21 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
#include "headers.h"
#include "functions.h"
char HomeDirectory[MY_LEN];
char PreviousDirectory[MY_LEN];
char hispath[MY_LEN];
char extension[MY_LEN];
int QUIT;
int errno;
char *bgp[MY_LEN];
int bgpid[MY_LEN];
int bexists[MY_LEN];
int bgpno;
char *fgp[MY_LEN];
int fgpid[MY_LEN];
int fexists[MY_LEN];
int fgpno;
int initSTDIN;
int initSTDOUT;
int fd1;
int fd2;
int redirread;
int redirwrite;
int main()
{
init();
bindSignals();
struct utsname unameBuffer;
dup2(0, STDIN_FILENO);
dup2(1, STDOUT_FILENO);
char buff[MY_LEN];
char *ret = getcwd(buff, MY_LEN);
strcpy(HomeDirectory, buff);
errno = 0;
if (uname(&unameBuffer) < 0)
{
perror("uname");
exit(EXIT_FAILURE);
}
promptInit();
char path[MY_LEN];
strcpy(path, HomeDirectory);
for (int i = 0; i < MY_LEN; i++)
{
bgp[i] = (char *)malloc(MY_LEN * sizeof(char));
fgp[i] = (char *)malloc(MY_LEN * sizeof(char));
}
prompt(path);
while (1)
{
// char command[MY_LEN];
// char *inputstatus = fgets(command, MY_LEN, stdin);
char* command = get_line();
if(debug) printf("get_line returned %s\n",command);
if (command == NULL)
{
printf("\nEOF reached in input Stream. I QUIT!\n");
return 0;
}
command[strlen(command) - 1] = '\0';
AddHistory(command);
strcat(command, ";");
if (debug)
printf("command = %s\n", command);
char *Commands[MY_LEN];
for (int i = 0; i < MY_LEN; i++)
{
Commands[i] = malloc(MY_LEN * sizeof(char));
}
char *token = strtok(command, ";");
int totalCommands = 0;
while (token != NULL)
{
if (debug)
printf("token = %s\n", token);
// executeCommand(token, path);
strcpy(Commands[totalCommands], token);
totalCommands++;
if (debug)
printf("hellp\n");
token = strtok(NULL, ";");
}
if (debug)
printf("total commands = %d", totalCommands);
for (int k = 0; k < totalCommands; k++)
{
char** y = pipeLine(Commands[k],path);
}
LookforBG();
if (QUIT)
{
for (int i = 0; i < MY_LEN; i++)
{
free(Commands[i]);
}
if (LookforBG())
{
printf("There are some processes still running.\n");
printf("Do you still wanna quit? [Y/n]:");
char c;
scanf("%c", &c);
if (c == 'y' || c == 'Y')
{
KillAllBG();
printf("All running processes killed\n");
}
else
{
printf("quit declined\n");
}
return 0;
}
else
{
return 0;
}
}
// printf("\033[1;36m<%s@%s:%s\033[0m\033[1;33m%s%s>\033[0m $ ", username, unameBuffer.sysname, "", Tildify(path), extension);
// fflush(stdout);
prompt(path);
strcpy(extension, "");
}
}