-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.cpp
237 lines (207 loc) · 6.88 KB
/
tools.cpp
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
#include "tools.h"
#include <string>
bool check(std::vector<std::string> &args,std::string &home){
for(int i=0;i<args.size();i++){
if(args[i]=="sudo"){
std::cout<<"Copyright © 2022, NEU. All Rights Reserved. \n";
return false;
}
}
for(int i=0;i<args.size();i++){
if(args.size()-i>=3&&args[i]=="rm"&&(args[i+1]=="-rf"||args[i+1]=="-fr")){
for(int j=i+2;j<args.size();i++){
if(args[j]=="/"){
std::cout<<"操作危险,不予执行。"<<std::endl;
return false;
}
}
}
}
return true;
}
void replace2(std::string &str,std::string &home){//把~替换回常规目录
while((int)str.find("~")!=-1){
str = str.replace(str.find("~"),1,home);
}
}
void trim(std::string & str){
std::string blanks("\f\v\r\t\n ");
str.erase(0,str.find_first_not_of(blanks));
str.erase(str.find_last_not_of(blanks) + 1);
}
bool testBlank(char a){
if(a==' '||a=='\t'||a=='\n'||a=='\r'||a=='\0'){
return true;
}
return false;
}
std::vector<int> findAll(std::string &str, char pattern) {
std::vector<int> result;
for(int i=0;i<str.size();i++){
if(str[i]==pattern){
result.push_back(i);
}
}
return result;
}
bool checkArgs(std::vector<std::string> &args){
std::vector<std::string> tmpargs;
for(int i=0;i<args.size();i++){
if(args[i]!="&"&&args[i]!="|"&&args[i]!=">"&&args[i]!="<"){
tmpargs.push_back(args[i]);
}else{
if(tmpargs.empty()){
tmpargs.push_back(args[i]);
}else{
std::string last = tmpargs[tmpargs.size()-1];
char c = last[last.size()-1];
if(c!='&'&&c!='|'&&c!='>'&&c!='<')
tmpargs.push_back(args[i]);
else{
tmpargs.pop_back();
tmpargs.push_back(last+args[i]);
}
}
}
}
args.swap(tmpargs);
std::vector<std::string>().swap(tmpargs);//置空
for(int i=0;i<args.size();i++){
char c = args[i][0];
if(c!='&'&&c!='|'&&c!='>'&&c!='<'){
continue;
}else{
if(args[i]!="&&"&&args[i]!="||"&&args[i]!="&"&&args[i]!="|"&&args[i]!=">"&&args[i]!=">>"&&args[i]!="<"){
return false;
}
}
}
return true;
}
std::vector<int> findpos(std::vector<std::string> &args,std::string pattern){
std::vector<int> pos;
for(int i=0;i<args.size();i++){
if(args[i]==pattern){
pos.push_back(i);
}
}
return pos;
}
int findPos(std::vector<int> &array,int i) {
for(int j=0;j<array.size();j++){
if(array[j]==i){
return j;
}
}
return -1;
}
bool quotationSplit(std::string &line,std::vector<argStru> &tmpargs){
auto simplePos = findAll(line,'\'');
auto doublePos = findAll(line,'\"');
int i=0;
int lastI = 0;
while(true){
int simpleI = findPos(simplePos, i);
int doubleI = findPos(doublePos, i);
if(simpleI!=-1){
argStru tmparg;
tmparg.arg = line.substr(lastI,i-lastI);
tmpargs.push_back(tmparg);
if(simpleI+1>=simplePos.size()){
return false;
}
argStru tmparg1;
tmparg1.arg = line.substr(i+1,simplePos[simpleI+1]-i-1);
tmparg1.iterate = false;
tmpargs.push_back(tmparg1);
lastI = simplePos[simpleI+1]+1;
i = lastI;
}else if(doubleI!=-1){
argStru tmparg;
tmparg.arg = line.substr(lastI,i-lastI);
tmpargs.push_back(tmparg);
if(doubleI+1>=doublePos.size()){
return false;
}
argStru tmparg1;
tmparg1.arg = line.substr(i+1,doublePos[doubleI+1]-i-1);
tmparg1.iterate = false;
tmpargs.push_back(tmparg1);
lastI = doublePos[doubleI+1]+1;
i = lastI;
}else
i++;
if(i>=line.size()){
argStru arg;
if(lastI<=line.size()-1&&lastI!=0){
arg.arg = line.substr(lastI+1);
tmpargs.push_back(arg);
}else if(lastI==0){
arg.arg = line.substr(lastI);
tmpargs.push_back(arg);
}
break;
}
}
return true;
}
void symbolSplit(std::vector<argStru> &tmpargs,std::vector<std::string> &args){
for(int i=0;i<tmpargs.size();i++){
if(tmpargs[i].iterate){
std::vector<std::string> tmp;
std::vector<std::string> strs = split(tmpargs[i].arg, "&");
for(int j=0;j<strs.size();j++){
std::vector<std::string> strs1 = split(strs[j], "|");
tmp.insert(tmp.end(),strs1.begin(),strs1.end());
}
strs.swap(tmp);
std::vector<std::string>().swap(tmp);//tmp置空,strs为分割完的
for(int j=0;j<strs.size();j++){
std::vector<std::string> strs1 = split(strs[j], ">");
tmp.insert(tmp.end(),strs1.begin(),strs1.end());
}
strs.swap(tmp);
std::vector<std::string>().swap(tmp);//tmp置空,strs为分割完的
for(int j=0;j<strs.size();j++){
std::vector<std::string> strs1 = split(strs[j], "<");
tmp.insert(tmp.end(),strs1.begin(),strs1.end());
}
strs.swap(tmp);
std::vector<std::string>().swap(tmp);//tmp置空,strs为分割完的
for(int j=0;j<strs.size();j++){
std::vector<std::string> strs1;
std::istringstream buffer(strs[j]);
std::copy(std::istream_iterator<std::string>(buffer), std::istream_iterator<std::string>(), std::back_inserter(strs1));
tmp.insert(tmp.end(),strs1.begin(),strs1.end());
}
strs.swap(tmp);
std::vector<std::string>().swap(tmp);//tmp置空,strs为分割完的
args.insert(args.end(),strs.begin(),strs.end());
}else{
args.push_back(tmpargs[i].arg);
}
}
}
std::vector<std::string> split(std::string str, std::string pattern) {
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
if(s!="")
result.push_back(s);
result.push_back(pattern);
i = pos + pattern.size() - 1;
}
}
result.pop_back();
// for(int i=0;i<result.size();i++){
// std::cout<<result[i]<<std::endl;
// }
return result;
}