-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.h
More file actions
214 lines (191 loc) · 5.13 KB
/
Copy pathexecute.h
File metadata and controls
214 lines (191 loc) · 5.13 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
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
//
// Created by mrhb on 12/14/20.
//
#ifndef SHELL_EXECUTE_H
#define SHELL_EXECUTE_H
extern bool finishedProgram = false;
extern bool isExecutingCommand = false;
char history[400][500];
int countOfHistoryCommand=0;
#include <cstdlib>
#include <wait.h>
#include <unistd.h>
#include "command.h"
#include <cstdio>
#include "keyHandler.h"
#include "massaging.h"
#endif
bool executeSingleLineCommand(char *command);
bool executePipeLineCommand(char *command);
bool execute(char *command);
void showHistorty();
void changeDir(char *command);
bool execute(char *command) {
bool result ;
strcpy( history[countOfHistoryCommand],command);
countOfHistoryCommand++;
int type = getCommandType(command);
switch (type) {
case SINGLE_LINE_COMMAND:
result = executeSingleLineCommand(command);
break;
case PIPE_LINE_COMMAND:
result = executePipeLineCommand(command);
break;
case SEND_MSG:
sendMassage(command);
usleep(10000);
isExecutingCommand= false;
result= true;
break;
case HISTORY:
showHistorty();
result= true;
break;
case CD:
changeDir(command);
result= true;
break;
case QUIT :
finishedProgram = true;
default:
result = false;
}
return result;
}
void changeDir(char *command){
isExecutingCommand= true;
trim(command);
char last[1024];
getcwd(last, sizeof(last));
strRemove(command,"\n");
int spaceCount = getSplitedArrayLength(command, " ");
char *args[spaceCount];
split(command, reinterpret_cast<char **>(&args), " ");
chdir(args[1]);
char cur[1024];
getcwd(cur, sizeof(cur));
if (strcmp(cur,last)==0){
printf("directory not changed (same dir or invalid dir)\n");
}
isExecutingCommand= false;
}
void showHistorty() {
isExecutingCommand= true;
printf("command history : \n");
for (int i = 0; i< countOfHistoryCommand; i++) {
printf("-%d %s \n",i+1,history[i]);
}
isExecutingCommand= false;
}
bool executeSingleLineCommand(char *command) {
isExecutingCommand= true;
pid_t pid;
pid = fork();
if (pid == 0) {
trim(command);
strRemove(command,"\n");
int spaceCount = getSplitedArrayLength(command, " ");
char *args[spaceCount];
split(command, reinterpret_cast<char **>(&args), " ");
if ((execvp(args[0], args)) == -1) {
usleep(6000);
printf("command %s not found",args[0]);
usleep(5000);
isExecutingCommand= false;
exit(0);
}
} else if (pid > 0) {
wait(0);
usleep(6000);
isExecutingCommand= false;
return true;
} else {
usleep(6000);
isExecutingCommand= false;
return false;
}
}
bool executePipeLineCommand(char *command) {
isExecutingCommand= true;
strRemove(command,"\"");
strRemove(command,"\n");
char *commands[2];
int pipefd[2];
int pid1=1,pid2=1;
trim(command);
split(command,commands,"|");
pipe(pipefd);
pid1=fork();
if(0 == pid1){
close(pipefd[1]);
dup2(pipefd[0],STDIN_FILENO);
trim(commands[1]);
int spaceCount = getSplitedArrayLength(commands[1], " ");
char *args[spaceCount];
split(commands[1], reinterpret_cast<char **>(&args), " ");
if ((execvp(args[0], args)) == -1) {
printf("command not found or not supported");
exit(0);
}
}else{
pid2 = fork() ;
if (pid2 == 0){
close(pipefd[0]);
dup2(pipefd[1],STDOUT_FILENO);
trim(commands[0]);
int spaceCount = getSplitedArrayLength((commands[0]), " ");
char *args[spaceCount];
split((commands[0]), reinterpret_cast<char **>(&args), " ");
if ((execvp(args[0], args)) == -1) {
printf("command not found or not supported");
exit(0);
}
}else{
usleep(8000);
close(pipefd[0]);
close(pipefd[1]);
wait(0);
wait(0);
isExecutingCommand= false;
return true;
}
}
}
bool lunch(char *command){
trim(command);
if (strlen(command) == 0) {
return true;
}
if (!execute(command)){
if (!finishedProgram){
printf("command not found or not supported");
}else{
printf("GOOD BYE !! \n");
exit(0);
}
}
return true;
}
void executeBatchFile(char *fileName){
FILE * batchFile;
char * line = nullptr;
size_t len = 0;
batchFile = fopen(fileName, "r");
if (batchFile == nullptr) {
fprintf(stderr," \n file not found \n ");
return;
}
while ((getline(&line, &len, batchFile)) != -1 && !finishedProgram) {
if (strcmp(line,"\n")==0){
continue;
}
printf("\n>>> %s",line);
lunch(line);
usleep(15000);
}
fclose(batchFile);
if (line) {
free(line);
}
}