-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
284 lines (260 loc) · 10.4 KB
/
main.cpp
File metadata and controls
284 lines (260 loc) · 10.4 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
#include "FileSystem.h"
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
vector<string> tokenize(const string& line) {
vector<string> tokens;
stringstream ss(line);
string token;
while (ss >> token) {
tokens.push_back(token);
}
return tokens;
}
struct Command {
vector<string> args;
string redirect_out;
string redirect_append;
bool has_redirect = false;
bool is_append = false;
};
Command parseCommand(const string& line) {
Command cmd;
stringstream ss(line);
string token;
bool redirect_mode = false;
while (ss >> token) {
if (token == ">") {
redirect_mode = true;
cmd.has_redirect = true;
cmd.is_append = false;
} else if (token == ">>") {
redirect_mode = true;
cmd.has_redirect = true;
cmd.is_append = true;
} else if (redirect_mode) {
if (cmd.is_append) {
cmd.redirect_append = token;
} else {
cmd.redirect_out = token;
}
redirect_mode = false;
} else {
cmd.args.push_back(token);
}
}
return cmd;
}
void runTerminal(FileSystem& fs) {
string line;
cout << "\n" << endl;
cout << "Linux-подобный терминал файловой системы" << endl;
cout << "Введите 'help' для списка команд, 'exit' для выхода" << endl;
while (true) {
const char* user = getenv("USER");
if (!user) user = "user";
cout << "\033[1;32m" << user << "@filesystem"
<< "\033[0m:\033[1;34m" << fs.getCurrentPath()
<< "\033[0m$ ";
if (!getline(cin, line)) break;
if (line.empty()) continue;
Command cmd = parseCommand(line);
if (cmd.args.empty()) continue;
string command = cmd.args[0];
if (command == "exit" || command == "quit") {
cout << "Выход из терминала..." << endl;
break;
}
else if (command == "help") {
cout << "Доступные команды:" << endl;
cout << " pwd - показать текущий путь" << endl;
cout << " ls [-l] - список файлов" << endl;
cout << " cd <path> - перейти в директорию" << endl;
cout << " mkdir <name> - создать директорию" << endl;
cout << " touch <name> - создать пустой файл" << endl;
cout << " cat <file> - вывести содержимое файла" << endl;
cout << " echo <text> - вывести текст (можно с > file или >> file)" << endl;
cout << " rm <name> - удалить файл" << endl;
cout << " rm -r <name> - удалить директорию рекурсивно" << endl;
cout << " chmod <mode> <f> - изменить права (например: chmod 755 file)" << endl;
cout << " find <pattern> - найти файлы по шаблону" << endl;
cout << " tree - показать дерево файловой системы" << endl;
cout << " clear - очистить экран" << endl;
cout << " debug - переключить режим отладки" << endl;
cout << " ed <f> <op> [...] - редактор (insert/delete/append/find)" << endl;
cout << " exit - выход" << endl;
}
else if (command == "pwd") {
cout << fs.getCurrentPath() << endl;
}
else if (command == "ls") {
bool showDetails = false;
string path = "";
for (size_t i = 1; i < cmd.args.size(); i++) {
if (cmd.args[i] == "-l") {
showDetails = true;
} else {
path = cmd.args[i];
}
}
if (!path.empty()) {
fs.ls(path, showDetails);
} else {
fs.ls(showDetails);
}
}
else if (command == "cd") {
if (cmd.args.size() < 2) {
fs.changeDirectory("/");
} else {
fs.changeDirectory(cmd.args[1]);
}
}
else if (command == "mkdir") {
if (cmd.args.size() < 2) {
cout << "mkdir: отсутствует операнд" << endl;
} else {
fs.mkdir(cmd.args[1]);
}
}
else if (command == "touch") {
if (cmd.args.size() < 2) {
cout << "touch: отсутствует операнд" << endl;
} else {
fs.touch(cmd.args[1]);
}
}
else if (command == "cat") {
if (cmd.args.size() < 2) {
cout << "cat: отсутствует операнд" << endl;
} else {
fs.cat(cmd.args[1]);
}
}
else if (command == "echo") {
if (cmd.args.size() < 2) {
cout << endl;
} else {
string text;
for (size_t i = 1; i < cmd.args.size(); i++) {
if (i > 1) text += " ";
text += cmd.args[i];
}
if (cmd.has_redirect) {
text += "\n";
if (cmd.is_append) {
fs.appendFile(cmd.redirect_append, text);
} else {
fs.writeFile(cmd.redirect_out, text);
}
} else {
cout << text << endl;
}
}
}
else if (command == "rm") {
if (cmd.args.size() < 2) {
cout << "rm: отсутствует операнд" << endl;
} else if (cmd.args.size() == 2) {
fs.rm(cmd.args[1], false);
} else if (cmd.args[1] == "-r" && cmd.args.size() >= 3) {
fs.rm(cmd.args[2], true);
} else {
fs.rm(cmd.args[1], false);
}
}
else if (command == "chmod") {
if (cmd.args.size() < 3) {
cout << "chmod: отсутствует операнд" << endl;
} else {
fs.chmod(cmd.args[1], cmd.args[2]);
}
}
else if (command == "find") {
if (cmd.args.size() < 2) {
cout << "find: отсутствует операнд" << endl;
} else {
fs.findFiles(cmd.args[1]);
}
}
else if (command == "tree") {
fs.visualize();
}
else if (command == "clear") {
cout << "\033[2J\033[1;1H";
}
else if (command == "debug") {
fs.toggleDebug();
}
else if (command == "ed") {
if (cmd.args.size() < 3) {
cout << "ed: использование: ed <file> <operation> [args...]" << endl;
cout << " операции:" << endl;
cout << " insert <pos> <text> - вставить текст на позицию" << endl;
cout << " delete <substring> - удалить первое вхождение подстроки" << endl;
cout << " append <text> - добавить текст в конец" << endl;
cout << " find <substring> - найти позицию подстроки" << endl;
} else {
string filename = cmd.args[1];
string operation = cmd.args[2];
if (operation == "insert" && cmd.args.size() >= 5) {
int pos = stoi(cmd.args[3]);
string text;
for (size_t i = 4; i < cmd.args.size(); i++) {
if (i > 4) text += " ";
text += cmd.args[i];
}
if (fs.insertInFile(filename, pos, text)) {
cout << "Текст вставлен на позицию " << pos << endl;
}
} else if (operation == "delete" && cmd.args.size() >= 4) {
string substr;
for (size_t i = 3; i < cmd.args.size(); i++) {
if (i > 3) substr += " ";
substr += cmd.args[i];
}
if (fs.deleteFromFile(filename, substr)) {
cout << "Подстрока удалена" << endl;
}
} else if (operation == "append" && cmd.args.size() >= 4) {
string text;
for (size_t i = 3; i < cmd.args.size(); i++) {
if (i > 3) text += " ";
text += cmd.args[i];
}
if (fs.appendFile(filename, text)) {
cout << "Текст добавлен" << endl;
}
} else if (operation == "find" && cmd.args.size() >= 4) {
string substr;
for (size_t i = 3; i < cmd.args.size(); i++) {
if (i > 3) substr += " ";
substr += cmd.args[i];
}
int pos = fs.findInFile(filename, substr);
if (pos >= 0) {
cout << "Найдено на позиции: " << pos << endl;
} else {
cout << "Не найдено" << endl;
}
} else {
cout << "ed: неверная операция или аргументы" << endl;
}
}
}
else {
cout << command << ": команда не найдена" << endl;
}
}
}
int main() {
FileSystem fs;
fs.createDirectory("/home");
fs.createDirectory("/home/user");
fs.createDirectory("/etc");
fs.createDirectory("/var");
fs.createDirectory("/tmp");
runTerminal(fs);
return 0;
}