-
Notifications
You must be signed in to change notification settings - Fork 0
/
aweShell.c
executable file
·366 lines (357 loc) · 8.46 KB
/
aweShell.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* "awesome_shell" presented by :
srikar chundury - 01fb15ecs306 ,
sumanth s rao - 01fb15ecs314 ,
varun venkatesh - 01fb15ecs331
*/
#include<unistd.h>
#include<sys/wait.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sysexits.h>
#include "fcntl.h"
#define C_MAX 10000
#define ARG_MAX 5
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
#define HIST ".shell_history.txt"
#define ALIAS ".alias.txt"
#define PATH_MAX 10000
char CWD[PATH_MAX];
FILE *hist;
int saved_stdout,saved_stdin;
int in, out;
int ou=0,inter=0;
int status,status1;
pid_t pid,pid1;
char **cd_stack;
int pd=0;
char *pre_process(char *inp){
char *new = (char *)malloc(sizeof(char)*(strlen(inp)+10));
int k=0,i;
for(i=0;i<strlen(inp);i++){
if(inp[i]=='|' || inp[i]=='<' || inp[i]=='>'){
new[k++]=' ';new[k++]=inp[i];new[k++]=' ';
}
else{
new[k++]=inp[i];
}
}
int j;
char *line = (char *)malloc(sizeof(char)*(strlen(inp)+10));
line = new;
for(i=0;line[i]!='\0';++i){
while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0' ||line[i] == '|' ||line[i] == '<' || line[i] == '>' || line[i] == '-' || line[i] == ' ' || line[i] == '_' || line[i] == '.' || line[i] == ':'|| line[i]=='/' || line[i]=='0' || line[i]=='1'|| line[i]=='2'|| line[i]=='3'|| line[i]=='4'|| line[i]=='5'|| line[i]=='6'|| line[i]=='7'|| line[i]=='8'|| line[i]=='9')){
for(j = i; line[j] != '\0'; ++j){
line[j] = line[j+1];
}
line[j] = '\0';
}
}
// printf("line: %s\n", line);
return line;
}
char **com_n_args(char *inp) {
char **res;
res = (char **)malloc(sizeof(char *) * ARG_MAX);
int k=0;
char *token = strtok(inp," ");
while(token != NULL){
res[k]=(char *)malloc(sizeof(char) * C_MAX);
strcpy(res[k++],token);
token = strtok(NULL," ");
}
res[k] = (char *)malloc(sizeof(char) * C_MAX);
res[k] = NULL;
return res;
}
void pip_pls(char **inp){
int pipefd[2];
int m=0,j,r=0;
char *cmd1 = (char *)malloc(sizeof(char)*C_MAX);
char *cmd2 = (char *)malloc(sizeof(char)*C_MAX);
while( strcmp(inp[m],"|")!=0 ){
for(j=0;j<strlen(inp[m]);j++){
cmd1[r++] = inp[m][j];
}
cmd1[r++]=' ';
cmd1[r]='\0';
m++;
}
cmd1[r-1]='\0';
m++;
r=0;
while( inp[m]!=NULL ){
for(j=0;j<strlen(inp[m]);j++){
cmd2[r++] = inp[m][j];
}
cmd2[r++]=' ';
cmd2[r]='\0';
m++;
}
cmd2[r-1]='\0';
char *p_1 = pre_process(cmd1);
char **list1 = com_n_args(p_1);
char *p_2 = pre_process(cmd2);
char **list2 = com_n_args(p_2);
pipe(pipefd);
pid1 = fork();
if (pid1 == 0){
pid_t pid2 = fork();
if(pid2==0){
dup2(pipefd[0],0);
close(pipefd[1]);
execvp(*list2,list2);
}
else{
dup2(pipefd[1],1);
close(pipefd[0]);
execvp(*list1,list1);
}
}
else{
waitpid(pid1,&status1,0);
}
}
char *do_pip_stuff(char **inp){
int i=0;
char *new = (char *)malloc(sizeof(char)*(ARG_MAX*C_MAX));
int k=0;
do{
if( strcmp(inp[i],">")==0 ){
char dest[C_MAX];
strcpy(dest,inp[i+1]);
out = open(dest, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
saved_stdout = dup(1);
dup2(out, 1);
close(out);
ou = 1;
i++;
}
else if( strcmp(inp[i],"<")==0 ){
char src[C_MAX];
strcpy(src,inp[i+1]);
in = open(src, O_RDONLY);
saved_stdin = dup(0);
dup2(in, 0);
close(in);
inter = 1;
i++;
}
else if( strcmp(inp[i],"|")==0 ){
return "piped";
}
else{
int j;
for(j=0;j<strlen(inp[i]);j++){
new[k++] = inp[i][j];
}
new[k++]=' ';
}
i++;
}while( inp[i] != NULL );
new[k]='\0';
return new;
}
void del(char* str, char ch){
int i, j = 0;
int size;
char ch1;
char str1[10];
size = strlen(str);
for(i=0;i<size;i++){
if(str[i] != ch){
ch1 = str[i];
str1[j] = ch1;
j++;
}
}
str1[j] = '\0';
}
void do_func() {
char *input = (char *)malloc(sizeof(char) * C_MAX) ;
char *input2 = (char *)malloc(sizeof(char) * C_MAX);
// fgets(input2,C_MAX,0);
gets(input2);
// scanf("%s", input2);
// printf("entered: %s\n", input2);
if(strchr(input2,'\\') != NULL){
int i, j = 0;
int size;
char ch1;
size = strlen(input2);
for(i=0;i<size;i++){
if(input2[i] != '\\'){
ch1 = input2[i];
input[j] = ch1;
j++;
}
}
input[j] = '\0';
// printf("%s\n", input);
do_func();
}
else{
input = input2;
}
if( strlen(input) == 0 ) return;
char *p_input = pre_process(input);
char **list = com_n_args(p_input);
p_input = do_pip_stuff(list);
if( strcmp(p_input, "piped") ==0 ) {
hist = fopen(HIST, "ab+");
fprintf(hist, "%s\n", input);
fclose(hist);
pip_pls(list);
}
else {
list = com_n_args(p_input);
if(strcmp(*list, "hist") == 0) {
char line[C_MAX];
FILE *new = fopen(HIST, "ab+");
while( fgets(line, C_MAX, new) != NULL ){
printf("%s", line);
}
hist = fopen(HIST, "ab+");
fprintf(hist, "hist\n");
fclose(hist);
fclose(new);
}
if(1) {
char line[C_MAX];
FILE *new = fopen(ALIAS, "ab+");
while( fgets(line, C_MAX, new) !=NULL ){
int z=0, q=0, r=0;
char alias[C_MAX];
char actual[C_MAX];
while(line[z]!=':'){
alias[q++]=line[z];
z++;
}
alias[q]='\0';
z++;
if ( strcmp(*list, alias)==0 ) {
while(line[z]!='\0'){
actual[r++]=line[z];
z++;
}
actual[r-1]='\0';
strcpy(input,actual);
p_input = pre_process(input);
list = com_n_args(p_input);
}
}
fclose(new);
}
if(strcmp(*list, "cd")==0) {
cd_stack[pd] = malloc(sizeof(char *) * C_MAX);
getcwd(cd_stack[pd], C_MAX);
printf("\n---cd_stack[%d]=%s---\n", pd, cd_stack[pd]);
pd++;
chdir(list[1]);
hist = fopen(HIST, "ab+");
fprintf(hist, "%s\n", input);
fclose(hist);
}
else if(strcmp(*list, "weather")==0) {
// printf("bext\n");
pid_t pid6;
char *str1 = "python3 ";
char *str3 = "/weather.py";
char *python_command = (char *)malloc(sizeof(char) * (strlen(str1) + strlen(CWD) + strlen(str3) + 1));
strcpy(python_command, str1);
strcat(python_command, CWD);
strcat(python_command, str3);
printf("%s\n", python_command);
system(python_command);
/*if(pid6=fork()<0){
fprintf(stderr,"awe_shell : can't fork : %s\n",strerror(errno));
}
if(pid6==0){
system("python weather.py");
}
int status6;
if( (pid6=waitpid(pid6,&status6,0)) < 0 ){
fprintf(stderr,"awe_shell : waitpid error : %s\n",strerror(errno));
}
*/
}
else if(strcmp(*list, "prevdir")==0) {
chdir(cd_stack[pd-1]);
pd--;
hist = fopen(HIST, "ab+");
fprintf(hist, "%s\n", input);
fclose(hist);
}
else if(strcmp(*list, "alias")==0) {
FILE *new = fopen(ALIAS, "ab+");
int x = 1;
while(list[x]!=NULL){
fprintf(new, "%s ", list[x]);
x++;
}
fprintf(new, "\n");
hist = fopen(HIST, "ab+");
fprintf(hist, "%s\n", input);
fclose(hist);
fclose(new);
return;
}
else {
// printf("%s\n", input);
hist = fopen(HIST, "ab+");
fprintf(hist, "%s\n", input);
fclose(hist);
if((pid=fork()) == -1) {
fprintf(stderr, "aweShell : can't fork : %s\n", strerror(errno));
}
else if(pid==0) {
execvp(*list, list);
fprintf(stderr, "aweShell : can't exec : %s\n", strerror(errno));
exit(EX_DATAERR);
}
if( (pid=waitpid(pid, &status, 0)) < 0) {
fprintf(stderr, "aweShell : waitpid error : %s\n", strerror(errno));
}
}
}
}
int main(){
if (getcwd(CWD, sizeof(CWD)) != NULL) {
printf("Current working dir: %s\n", CWD);
} else {
perror("getcwd() error");
return 1;
}
cd_stack = malloc(sizeof(char *)*10);
printf(KNRM "\n-----------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf(KYEL "\t\t\t\t\t\t\t\tWELCOME TO 'AWESOME' SHELL [PHASE - 3]\n\t\t");
printf(KCYN "\t\t\t\t\t\t* Srikar Chundury - 01FB15ECS306\n\t\t");
printf(KMAG "\t\t\t\t\t\t* Sumanth S Rao - 01FB15ECS314\n\t\t");
printf(KGRN "\t\t\t\t\t\t* Varun V - 01FB15ECS331");
printf(KNRM "\n-----------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n");
while(1){
char* path = (char *)malloc(sizeof(char) * C_MAX);
getcwd(path, C_MAX);
printf(KRED "awesome_shell_:");
printf(KBLU "%s",path);
printf(KRED "$");
do_func();
if(ou){
dup2(saved_stdout, 1);
close(saved_stdout);
}
if(inter){
dup2(saved_stdin, 0);
close(saved_stdin);
}
}
return 0;
}