-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
150 lines (146 loc) · 2.83 KB
/
main.c
File metadata and controls
150 lines (146 loc) · 2.83 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pwd.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<signal.h>
#include<grp.h>
#include<time.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<termios.h>
#include<sys/utsname.h>
#include<stdbool.h>
#include<errno.h>
#include<dirent.h>
#include "gvar.h"
int main()
{
clear;
process_now = getpid();
// set home
getcwd(Home, sizeof(Home));
end = strlen(Home);
signal(SIGINT, nothing);
signal(SIGTSTP, sigint_handler);
hist_count = 0;
while(true)
{
displayShell(end, Home);
// signal(SIGTSTP, sigint_handler);
int sc = 0, t = 0, cmd_sc[10001], noc = 1;
// sets the final point as \n here
memset(line, '\n', 1000001);
fgets(line, 1000001, stdin);
// strtok returns NULL when there is no token left to retrieve
cmd[0] = strtok(line," \n\t");
if(cmd[0] == NULL)
continue;
while((cmd[noc] = strtok(NULL, " \n\t")) != NULL)
{
// seperating of commands using a ';' is implemented
// here. Note that I tried my best but you have to seperate
// the functions and the semicolon using a space.
// For example "pwd ; echo Chimichangas"
if(*cmd[noc] == ';')
{
sc = noc;
cmd_sc[t++] = noc;
}
noc++;
}
if(sc != noc-1)
cmd_sc[t++] = noc;
int pCom[1024];
int it = 0;
int pl = 0, pr = 0;
int in = 0, out1 = 0;
int fd_out, fd_in;
int i = 0;
if(!sc)
{
int k = 0;
while(k<noc)
{
if(strcmp(cmd[k],"|")==0)
pCom[it++]=k;
k++;
}
pCom[it++]=noc;
if(it!=1)
{
fd_in = dup(STDIN_FILENO);
fd_out = dup(STDOUT_FILENO);
int k = 0, left = 0, z = -1;
while(k<it)
{
int right = pCom[k];
z = piped_exec(&cmd[left],z,it-1-k,left,right);
left = right+1;
k++;
}
dup2(fd_in, STDIN_FILENO);
close(fd_in);
}
else
redirect(cmd, noc);
}
else
{
int left = 0, right;
int i = 0;
while(i<t)
{
it=0;
right=cmd_sc[i];
int iter = left;
while(iter<right)
{
if(strcmp(cmd[iter],"|")==0)
pCom[it++]=iter;
iter++;
}
pCom[it++]=right-left;
if(it==1)
redirect(&cmd[left],right-left);
else
{
fd_in = dup(STDIN_FILENO);
fd_out = dup(STDOUT_FILENO);
int g=left, z=-1;
int iter = 0;
while(iter<it)
{
int h = pCom[iter];
z = piped_exec(&cmd[g],z,it-1-iter,g,h);
g = h+1;
iter++;
}
dup2(fd_in, STDIN_FILENO);
close(fd_in);
}
left = right+1;
i++;
}
}
//redirect(cmd, noc);
//run_shell(cmd, noc);
// else
// {
// int l = 0, r, i = 0;
// while(i<t)
// {
// r=cmd_sc[i];
// run_shell(&(cmd[l]), r-l);
// l=r+1;
// i++;
// }
// }
// Only 20 history
hist_count++;
hist_count = hist_count%20;
}
return 0;
}