-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptReader.cpp
More file actions
291 lines (244 loc) · 7.1 KB
/
scriptReader.cpp
File metadata and controls
291 lines (244 loc) · 7.1 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
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
//Made By Junhyeong(0511) : Linux Shell Scriptor Reader Project (24.02.03 ~ 24.02.04)
/**
* Debug 및 수정예정 사항
*
* 예정
* 1. chdir 대신 cd를 자동으로 chdir로 인식
* 2. 실험 Experience >> CoTTa, xv6 Virtual OS Project
*/
#include <iostream>
#include <map>
#include <queue>
#include <cstdio>
#include <deque>
#include <cstring>
//My HeaderFile (Junhyeong)
#include "ospath.h"
#include "stringExpand.h"
//Linux System Header
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <wait.h>
#include <fcntl.h>
using namespace std;
#define PROMPT cout<<"junhyeong >> "
#define EXECUTE_FILE ".jh"
#define MOD_INSTALL 0
#define MOD_RUN 1
#define MOD_DELETE 2
#define MOD_COPY 3
#define MOD_CHDIR 4
#define MOD_RECHDIR 5
#define SMODE_FILECHECK "[filecheck]"
const char* modeList[6] = {
"install:",
"run:",
"delete:",
"copy:",
"chdir:",
"reChdir:"
};
deque<pair<int, string>> cmdQ;
deque<string> pathStack;
char* strToChar(string s);
void transformer();
//Special 명령어 실행
void doSpecialCmd(int mode) {
if (mode == -1)
return;
}
// 패키지 설치 관련
void installer(string s);
void installer(deque<string> q);
// systemFunc
void system(string s);
//deleteSystem
void deleteFiles(string s);
//change Directory
void chdirs(string s);
int main(void)
{
transformer();
int curSet = -1;
while (!cmdQ.empty()) {
pair<int, string> element_cmd = cmdQ.front();
cmdQ.pop_front();
// White Space 제거
if (!strip(element_cmd.second).length())
continue;
switch (element_cmd.first) {
case MOD_INSTALL:
installer(element_cmd.second);
break;
case MOD_RUN:
system(element_cmd.second);
break;
case MOD_DELETE:
deleteFiles(element_cmd.second);
break;
case MOD_COPY:
{
deque<string> cpyQ = splitString(string(element_cmd.second), "->", 2);
if (cpyQ.empty())
{
fprintf(stderr, "Copy Cmd is Error : %s\n", element_cmd.second.c_str());
break;
}
string prevPath = strip(cpyQ.at(0)), nextPath = strip(cpyQ.at(1));
path p(prevPath);
if (!p.getCurPath().length()) //Not file prev_path
break;
p.dirCopy(prevPath, nextPath);
break;
}
case MOD_CHDIR:
chdirs(element_cmd.second);
break;
case MOD_RECHDIR:
{
if (pathStack.empty())
break;
string retAddr;
if (element_cmd.second.compare("init")) {
retAddr = string(pathStack.front());
pathStack.clear();
}
else {
retAddr = string(pathStack.back());
pathStack.pop_back();
}
if (chdir(retAddr.c_str()) < 0) {
fprintf(stderr, "chdir error : ");
break;
}
cout<<"curPath : "<<getcwd(NULL, 0)<<endl;
break;
}
default:
break;
}
curSet = element_cmd.first;
}
exit(0);
}
void chdirs(string s) {
path p(s);
if (!p.getCurPath().length())
return;
pathStack.push_back(getcwd(NULL, 0));
if (chdir(p.getCurPath().c_str()) < 0) {
fprintf(stderr, "chdir error : %s\n", p.getCurPath().c_str());
pathStack.pop_back();
}
cout<<"curPath : "<<getcwd(NULL,0)<<endl;
}
void deleteFiles(string s)
{
path p(s);
if (!p.getCurPath().length())
return;
p.fileDelete();
}
//@OverLoading -> int system (const char* _cmd);
/**
* Linux Standard Header : Deafault Shell Cshell
* >> system() Library 는 기본적으로 subRoutine (Procedure) 를 가지지 않기 때문에
* fork() + execl() 로 cpp에서 구현해야 conda activate 를 구현가능함
* --> Python에서 subprogram을 지원하기 때문에 파이썬으로 자동연결 예정 (24.02.04 : (jh0511))
**/
void system(string s)
{
system(s.c_str());
}
void transformer() {
path p(".");
deque<string> fileQ = p.readDir(), scriptQ;
while(!fileQ.empty()) {
string f = fileQ.front();
fileQ.pop_front();
if(strstr(f.c_str(), EXECUTE_FILE)) {
scriptQ.push_back(f);
}
}
//스크립트 선택
int select = -1;
for (int cnt = 0 ; select < 0 || select >= scriptQ.size() ; cnt++) {
if (cnt) cout<<"\nRetry, Your Key is Wrong"<<endl;
cout << "===== Select Script File =====" << endl;
for (int idx = 0; idx < scriptQ.size(); idx++) {
cout << "[" << idx << "] : " << scriptQ.at(idx) << endl;
}
PROMPT;
cin >> select;
cin.ignore();
}
// Read The Script
string scriptFname = string(scriptQ.at(select));
FILE* fp = fopen(scriptFname.c_str(), "r");
if (fp == NULL) {
fprintf(stderr, "fopen error for %s\n", scriptFname.c_str());
return;
}
char lineOrigin[1024] = {0,}, *line;
int curMod = -1;
while(!feof(fp)) {
line = lineOrigin;
fgets(line, 1024, fp);
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
// 주석 삭제
char* ptr = strstr(line, "#");
if (ptr != NULL) *ptr = 0;
if (!strlen(line))
continue;
// 빈칸 삭제
for(; *line == ' ' ; line++);
// 스크립트 적용 및 queue에 삽입
int chMod = false;
for (int mod = 0 ; mod < sizeof(modeList) / sizeof(modeList[0]) ; mod++) {
if (!strncmp(modeList[mod], line, strlen(modeList[mod]))) {
curMod = mod;
chMod = true;
break;
}
}
if (chMod) {
chMod = false;
memset(line, 0, 1024);
continue;
}
//Special Command Cheker (24.02.05 : Junhyeong)
// --> Script 에서 요구하는 Special 명령어 검사
int isFile = false;
if (strstr(line, SMODE_FILECHECK)) {
deque<string> tmpString = splitString(line, SMODE_FILECHECK);
path p(strip(tmpString.back()));
if (p.isFile(p.getCurPath()))
isFile = true;
else
strcpy(line, strip(tmpString.at(0)).c_str());
}
if (curMod == -1)
cout<<"wrong mode"<<endl;
else if (isFile)
cout<<"your file is already existed {does not execute : "<<line<<":}"<<endl;
else
cmdQ.push_back(pair<int,string>(curMod, line));
memset(line, 0, 1024);
}
fclose(fp);
}
void installer (deque<string> q) {
while (!q.empty()) {
installer(q.front());
q.pop_front();
}
}
void installer(string s) {
const char* originStr = "sudo apt-get install";
char line[1024];
sprintf(line, "%s %s", originStr, s.c_str());
cout<<line<<endl;
system((const char*) line);
}