-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.c
More file actions
129 lines (128 loc) · 2.61 KB
/
run.c
File metadata and controls
129 lines (128 loc) · 2.61 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
#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;
// set home
getcwd(Home, sizeof(Home));
end = strlen(Home);
hist_count = 0;
while(true)
{
int pCom[1024];
int fdout, fdin;
int i, pl = 0, pr = 0;
displayShell(end, Home);
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 in = 0, out1 = 0;
int dec = 0;
if(!sc)
{
int ch_bar=0;
// Check for piping in the command entered
while(ch_bar<noc)
{
if(strcmp(cmd[ch_bar],"|")==0)
pCom[dec++]=ch_bar;
ch_bar++;
}
pCom[dec++]=noc;
if(dec!=1)
{
fdin = dup(STDIN_FILENO);
fdout = dup(STDOUT_FILENO);
int left=0;
int exe=-1;
int j=0;
while(j<dec)
{
int right=pCom[j];
exe=piped_exec(&cmd[left], exe, dec-1-j, left, right);
left=right+1;
j++;
}
dup2(fdin, STDIN_FILENO);
close(fdin);
}
else
redirect(cmd,noc);
}
else
{
int left=0,right,i=0;
while(i<t)
{
dec=0;
right=cmd_sc[i];
int ch_bar = left;
while(ch_bar<right)
{
if(strcmp(cmd[ch_bar],"|")==0)
pCom[dec++]=ch_bar;
ch_bar++;
}
pCom[dec++]=right-left;
if(dec==1)
redirect(&cmd[left],right-left);
else
{
int cmd_l=left,exe=-1;
fdin = dup(STDIN_FILENO);
fdout = dup(STDOUT_FILENO);
for(int j=0; j<dec; j++)
{
int cmd_r=pCom[j];
exe=piped_exec(&cmd[g], exe, dec-1-j, cmd_l, cmd_r);
cmd_l=cmd_r+1;
}
dup2(fdin, STDIN_FILENO);
close(fdin);
}
left=right+1;
i++;
}
}
// Only 20 history
hist_count++;
hist_count = hist_count%20;
}
return 0;
}