-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.c
109 lines (103 loc) · 2.48 KB
/
input.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
#include "pipeHandler.h"
#include "headers.h"
#include "history.h"
#include "input.h"
int parseString(char* inp, char** parsed){
int i=0;
parsed[i] = strtok(inp, " ");
i+=1;
while(1){
parsed[i] = strtok(NULL, " ");
if(parsed[i]==NULL){
return 0;
}
i+=1;
}
return -1;
}
int is_empty(char s[]) {
int i=0;
while (s[i] != '\0') {
if (s[i]!=' ')
return 0;
i++;
}
return 1;
}
int input() {
char *inp=NULL;
inp=malloc(PATH_MAX);
int len = 0;
while(1){
inp[len]=getchar();
if(inp[len]=='\n')
break;
if(inp[len]==EOF){
killallChilds();
printf("\n");
exit(0);
}
len++;
}
inp[len+1]='\0';
// Fixes for pressing enter without any command
if(inp[0]=='\n')
return 0;
if(is_empty(inp))
return 0;
inp[strlen(inp)-1]='\0';
add_history(inp);
checkMulti(inp);
inp=NULL;
return 0;
}
int checkMulti(char* inp){
char *temp=NULL;
int i=0;
/**
command[i] = strtok(inp, ";");
i+=1;
while(1){
char *parsed[1000];
char og[strlen(inp)];
strcpy(og, command[i-1]);
if(!strcmp("exit", command[i-1])){
exit(0);
}
parseString(command[i-1], parsed);
runCommand(parsed, og);
command[i] = strtok(NULL, ";");
printf("%s\n", command[i]);
if(command[i]==NULL){
return 0;
}
i+=1;
}
*/
do {
// Splitting at next ; if exists
for( temp = inp; *inp && *inp != ';'; inp++ )
;
if(*inp == ';'){
*inp='\0';
}
while(*(inp+1)==' ') *inp++; //Removing leading whitespaces
*inp++ = '\0';
if (temp) {
if(is_empty(temp))
continue;
char *parsed[1000];
char og[strlen(temp)];
strcpy(og, temp);
if(!strcmp("exit", temp) || !strcmp("quit", temp)){
killallChilds();
exit(0);
}
int i=0;
while(temp[i]==' ') i++;
parseString(&temp[i], parsed);
checkPipeandrunCommand(parsed, &og[i]);
memset(og, 0, strlen(og));
}
} while( *inp );
}