-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command_Line_Interpreter.c
276 lines (248 loc) · 7.18 KB
/
Command_Line_Interpreter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Mihir Kavatkar
Program:
Following program is a Command Interpreter that executes unix commands using a system call and even few of the DOS commands.
Steps to compile and execute:
1.
syntax: gcc location/filename.c
Eg.
gcc /Users/mihirkavatkar/Documents/Advance\ Computer\ System/Assignment\ Solution/Assignment_1.c
2.
~/a.out
Note:
while executing commands maintain white spaces
example:
1. ls -l
2. ls -l | wc
3. ls > test.txt
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
#include <stdlib.h>
#define string_size 100
int parser(char *, char **, char **, int *);
void dos_run(char *input, char **array)
{
char *cmdargv[string_size], *suppl = NULL;
int flag=0;
parser(input, cmdargv, &suppl, &flag);
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
printf("Child Process Failed");
exit(0);
}
else if (pid == 0) {
if (execvp(*cmdargv, cmdargv) < 0) {
printf("Execution Failed");
exit(0);
}
}
else {
while (wait(&status) != pid)
;
}
}
void execute(char **cmdargv, int flag, char **supplptr)
{
pid_t pid1,pid2;
char *cmdargv1[string_size],*suppl1 = NULL;
int pfds[2];
int flag1 = 0,status;
FILE *fp;
if(flag == 4)
{
if(pipe(pfds))
{
fprintf(stderr, "Error: Pipe failure");
exit(-1);
}
parser(*supplptr, cmdargv1, &suppl1, &flag1);
}
pid1 = fork();
if(pid1 < 0)
{
printf("Error");
exit(-1);
}
else if(pid1 == 0)
{
if(flag == 2)
{
fp = fopen(*supplptr, "w+");
dup2(fileno(fp), 1);
}
if(flag == 5)
{
fp = fopen(*supplptr, "a");
dup2(fileno(fp), 1);
}
if(flag == 3)
{
fp = fopen(*supplptr, "r");
dup2(fileno(fp), 1);
}
if(flag == 4)
{
close(pfds[0]);
dup2(pfds[1], fileno(stdout));
close(pfds[1]);
}
execvp(*cmdargv, cmdargv);
}
else
{
if(flag == 1)
;
else if(flag == 4)
{
waitpid(pid1, &status, 0);
pid2 = fork();
if(pid2 < 0)
{
printf("Error");
exit(-1);
}
else if(pid2==0)
{
close(pfds[1]);
dup2(pfds[0],fileno(stdin));
close(pfds[0]);
execvp(*cmdargv1,cmdargv1);
}
else
{;
close(pfds[0]);
close(pfds[1]);
}
}
else
waitpid(pid1,&status,0);
}
}
void strip(char *stptr)
{
while(*stptr != ' ' && *stptr != '\n' && *stptr != '\t')
stptr++;
*stptr = '\0';
}
int parser(char *input, char *cmdargv[], char **supplptr, int *flagptr)
{
char *stptr = input;
int cmdargs = 0,finish = 0;
//printf("%s\n", input);
while(*stptr != '\0' && finish == 0)
{
*cmdargv = stptr;
cmdargs++;
//printf("%d\n", *cmdargv);
while(*stptr != ' ' && *stptr != '\n' && *stptr != '\0' && *stptr != '\t' && finish == 0)
{
switch(*stptr)
{
case '&':
*flagptr = 1;
break;
case '>':
*flagptr = 2;
*cmdargv = NULL;
stptr++;
if(*stptr == '>')
{
*flagptr = 5;
stptr++;
}
while(*stptr == ' ' || *stptr == '\t')
{
stptr++;
}
*supplptr = stptr;
strip(*supplptr);
finish = 1;
break;
case '<':
*flagptr = 3;
*cmdargv = NULL;
stptr++;
while(*stptr == ' ' || *stptr == '\t')
{
stptr++;
}
*supplptr = stptr;
strip(*supplptr);
finish = 1;
break;
case '|':
*flagptr = 4;
*cmdargv = NULL;
stptr++;
while(*stptr == ' ' || *stptr == '\t')
stptr++;
*supplptr = stptr;
finish = 1;
break;
}
stptr++;
}
while((*stptr == ' ' || *stptr == '\n' || *stptr =='\t') && finish == 0)
{
*stptr = '\0';
stptr++;
}
cmdargv++;
}
*cmdargv = NULL;
//printf("%d\n", cmdargs);
return cmdargs;
}
int main(int argc, char *argv[])
{
char *input,*dos_input,*cmdargv[string_size], *suppl = NULL;
char *list[9][2]={ {"dir", "ls"}, {"del", "rm"}, {"copy", "cp"}, {"move", "mv"}, {"type", "cat"}, {"more < file", "more file"}, {"md", "mkdir"}, {"rd", "rmdir"}, {"cls", "clear"} };
//char *list[9]= {"dir", "del", "copy", "move", "type", "more < file", "md", "rd", "cls"};
char curDir[100],*array[80];
int flag = 0,cmdargs,mode=0777;
size_t len = string_size;
input = (char*)malloc(sizeof(char)*string_size);
while(1)
{
flag = 0;
getcwd(curDir, 100);
printf("%s@%s->", getlogin(),curDir);
getline(&input, &len, stdin);
//printf("%s\n", input);
//printf("%d%s\n", result,list[result][1]);
//printf("%s\n", dos_input);
if(strcmp(input,"exit\n")==0)
exit(0);
cmdargs = parser(input, cmdargv, &suppl, &flag);
//printf("%s\n", *cmdargv);
for(int i=0;i<9;i++)
{
for(int j=0;j<2;j++)
{
if(strcmp(input,list[i][j])==0)
{
dos_input = list[i][j+1];
dos_run(dos_input,array);
}
else
break;
}
}
//printf("%s\n", *cmdargv);
if(strcmp(*cmdargv,"cd")==0)
{
chdir(cmdargv[1]);
}
else
{
execute(cmdargv, flag, &suppl);
}
}
return 0;
}