-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct.h
159 lines (148 loc) · 3.38 KB
/
direct.h
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
#include "global.h"
#include "ls.h"
#include "prompt.h"
#include "input.h"
#include "builtins.h"
//#include "clear.h"
#include "history.h"
#include "pinfo.h"
#include "jobs.h"
#include "cronjob.h"
#include "execute.h"
#include "env.h"
#include "jobs_commands.h"
// ll pressedkey = 0;
ll NUM_COMMANDS;
ll reversemapping[3000000];
ll bgind;
ll procaarray[30000];
ll shell_loop()
{
char *c = getcwd(homedirectory, sizeof(homedirectory));
if (!c)
{
perror("Error getting the current directory");
exit(EXIT_FAILURE);
}
printf(ANSI_CLEAR_SCREEN);
lengthofhomedirectory = strlen(homedirectory);
for (ll n = 0; n < lengthofhomedirectory; n++)
{
past_present_directory[n] = homedirectory[n];
}
for (ll i = 0; i < lengthofhomedirectory; i++)
{
HISTORYY[i] = homedirectory[i];
}
ll o = lengthofhomedirectory + 1;
char copystring[100] = "/.vcsh_history";
for(ll y=0;y<15;y++){
HISTORYY[y+o-1] = copystring[y];
}
printf("******* Welcome to vcsh ☺️ ***************\n");
signal(SIGCHLD, signal_handler);
signal(SIGINT,ctrlccross);
signal(SIGTSTP,ctrlzcross);
signal(SIGQUIT,sigquit);
shell_gid = getpgid(getpid());
ll cl = 0;
ll beforepressed = 0;
save_out = dup(1); // SAVE OUTPUT
save_in = dup(0); // SAVE INPUT
while (1)
{
// SET SHELL ID
shellid = getpid();
parentid = shellid;
// SET PWD
setpwd();
// DISPLAY PROMPT
prompt_display();
// TAKE INPUT
buffer = input();
if(pressedkey)
{
// Display num command from last at prompt and expect user to hit enter
char BUF[512][512];
int ind = 0;
FILE *fd = fopen(HISTORYY, "r");
if (!fd)
{
perror("Can't open .vcsh_history");
}
char *u = NULL;
size_t len;
ll a = getline(&u, &len, fd);
int cnt = 0;
strcpy(BUF[cnt],u);
while(a != -1)
{
strcpy(BUF[cnt], u);
cnt++;
a = getline(&u, &len, fd);
}
strcpy(buffer,BUF[cnt-numpressed]);
prompt_display();
buffer[strlen(buffer)-1] = '\0';
}
else if(!pressedkey)
{
history_vcsh(2, buffer, 0);
}
// PARSE THE INPUT
char **totalcommands = parse_by_colon(buffer);
for (ll n = 0; n <= NUM_COMMANDS; n++)
{
strcpy(curr_command,totalcommands[n]);
char **totalpipes = parse_by_pipe(totalcommands[n]);
// PIPE THE OUTPUT OF LAST TO FIRST ONE
int fdarray[2];
fdarray[0] = -1;
fdarray[1] = -1;
if(NUM_PIPES>=1)
{
for(int j=0;j <= NUM_PIPES; j++)
{
pipe(fdarray);
int pid = fork();
if(pid<0)
{
perror("Error forking");
}
else if(pid==0)
{
if(j!=NUM_PIPES)
{
close(1); // Close stdout so that it is assigned to fdarray[1]
dup(fdarray[1]); // Now child will write to a copy of fdarray[1]
close(fdarray[1]); // Close the original fdarray[1]
}
close(0); // Close stdin so that it is assigned to fdarray[0]
dup(glob_in); // Now child will read from a copy of fdarray[0]
close(fdarray[0]); // Close the original fdarray[0]
parse_it(totalpipes[j]);
exit(1);
}
else
{
wait(NULL);
close(fdarray[1]); // Close fdarray[1] as only child will write to it
glob_in = fdarray[0]; // Make parent read from pipe
}
}
}
else
{
parse_it(totalcommands[n]);
}
signal(SIGCHLD, signal_handler);
updatejobs();
setpwd();
resize();
}
pressedkey = 0;
nikal = 0;
iscronjob = 0;
}
return EXIT_FAILURE;
}