-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
306 lines (272 loc) · 8.9 KB
/
main.cpp
File metadata and controls
306 lines (272 loc) · 8.9 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "includes/console.h"
#include "includes/executor.h"
#include "includes/fs.h"
#include "includes/os_status.h"
#include "shadow.h"
#include "src/utils.hpp"
#include "useradd.h"
#include <bits/types/time_t.h>
#include <clocale>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define OS_NAME "fleksOS"
using namespace std;
void clear() { system("clear"); }
static FS *fs = nullptr;
void start() {
clear();
try {
fs = new FS(FS_FILENAME, true);
} catch (std::exception e) {
FS::log(e.what(), LogLevel::error);
return;
}
std::string user_login, user_password;
bool mainLoop = true;
do {
std::string curr_time;
ConsoleInput *input = nullptr;
std::string error = "";
while (mainLoop) {
clear();
curr_time = utils::current_time();
if (error.length() > 0)
FS::log(error, LogLevel::error);
FS::log("Введите имя пользователя: ", LogLevel::info, false);
input = Console::prompt();
if (input->cmd.length() < 1 || input->cmd.length() > USER_LOGIN_MAX) {
error = "Введено неверное имя пользователя";
continue;
} else
user_login = input->cmd;
error.clear();
break;
}
while (mainLoop) {
clear();
curr_time = utils::current_time();
FS::log("Имя пользователя: \x1B[31m" + user_login + "\033[0m\n");
if (error.length() > 0)
FS::log(error, LogLevel::error);
FS::log("Введите пароль пользователя: ", LogLevel::info, false);
input = Console::prompt();
if (input->cmd.length() < 1 || input->cmd.length() > 15) {
error = "Введен неверный пароль пользователя";
continue;
} else
user_password = input->cmd;
error.clear();
break;
}
} while (!fs->login(user_login.c_str(), user_password.c_str()));
clear();
Executor *executor = new Executor(*fs);
while (mainLoop) {
std::string curr_time = utils::current_time();
std::cout << "\x1B[31m" << curr_time << " " << OS_NAME << "@"
<< fs->get_current_username() << ":~$ \033[0m";
ConsoleInput *input = Console::prompt();
if (input->cmd.length() > 0) {
switch (executor->execute(input->cmd, input->args)) {
case OS_ERROR:
printf("Команда '%s' не найдена\n", input->cmd.c_str());
break;
case OS_EXIT:
mainLoop = false;
clear();
break;
default:
continue;
}
}
}
}
void install() {
std::string curr_time;
u32 fs_size, block_size;
std::string password;
std::string user_login, user_password;
ConsoleInput *input = nullptr;
std::string error = "";
bool mainLoop = true;
while (mainLoop) {
clear();
curr_time = utils::current_time();
if (error.length() > 0)
FS::log(error, LogLevel::error);
FS::log("Введите размер файловой системы в мегабайтах (min:8): ",
LogLevel::info, false);
input = Console::prompt();
if (input->cmd.length() < 1)
continue;
else {
int size = atoi(input->cmd.c_str());
if (size == 0 || size < 8) {
error = "Введено неверное число.";
continue;
}
fs_size = atoi(input->cmd.c_str());
}
error.clear();
break;
}
while (mainLoop) {
clear();
curr_time = utils::current_time();
FS::log("Размер файловой системы: \x1B[31m" + std::to_string(fs_size) +
" Мбайт\033[0m\n");
if (error.length() > 0)
FS::log(error, LogLevel::error);
FS::log("Введите размер блока в байтах: ", LogLevel::info, false);
input = Console::prompt();
if (input->cmd.length() < 1)
continue;
else {
int size = atoi(input->cmd.c_str());
if (size == 0) {
error = "Введено неверное число.";
continue;
}
switch (size) {
case 1024:
case 2048:
case 4096:
block_size = size;
break;
default:
error = "Введен неверный размер блока. Допустимые значения: 1024, "
"2048, 4096.";
continue;
}
}
error.clear();
break;
}
while (mainLoop) {
clear();
curr_time = utils::current_time();
FS::log("Размер файловой системы: \x1B[31m" + std::to_string(fs_size) +
" Мбайт\033[0m\n");
FS::log("Выбранный размер блока: \x1B[31m" + std::to_string(block_size) +
" Байт\033[0m\n");
if (error.length() > 0)
FS::log(error, LogLevel::error);
FS::log("Введите пароль суперпользователя (root): ", LogLevel::info, false);
input = Console::prompt();
if (input->cmd.length() < 1 || input->cmd.length() > 15) {
error = "Введен неверный пароль.";
continue;
} else
password = input->cmd;
error.clear();
break;
}
std::string root_password_star(password.length(), '*');
while (mainLoop) {
clear();
curr_time = utils::current_time();
FS::log("Размер файловой системы: \x1B[31m" + std::to_string(fs_size) +
" Мбайт\033[0m\n");
FS::log("Выбранный размер блока: \x1B[31m" + std::to_string(block_size) +
" Байт\033[0m\n");
FS::log("Пароль суперпользователя: \x1B[31m" + root_password_star +
"\033[0m\n");
if (error.length() > 0)
FS::log(error, LogLevel::error);
FS::log("Введите имя пользователя: ", LogLevel::info, false);
input = Console::prompt();
if (input->cmd.length() < 1 || input->cmd.length() > USER_LOGIN_MAX) {
error = "Введено неверное имя пользователя.";
continue;
} else
user_login = input->cmd;
error.clear();
break;
}
while (mainLoop) {
clear();
curr_time = utils::current_time();
FS::log("Размер файловой системы: \x1B[31m" + std::to_string(fs_size) +
" Мбайт\033[0m\n");
FS::log("Выбранный размер блока: \x1B[31m" + std::to_string(block_size) +
" Байт\033[0m\n");
FS::log("Пароль суперпользователя: \x1B[31m" + root_password_star +
"\033[0m\n");
FS::log("Имя пользователя: \x1B[31m" + user_login + "\033[0m\n");
if (error.length() > 0)
FS::log(error, LogLevel::error);
FS::log("Введите пароль пользователя: ", LogLevel::info, false);
input = Console::prompt();
if (input->cmd.length() < 1 || input->cmd.length() > 15) {
error = "Введен неверный пароль пользователя.";
continue;
} else
user_password = input->cmd;
error.clear();
break;
}
auto user_data = std::make_pair(user_login, user_password);
clear();
FS::format(fs_size << 20, block_size, password, user_data);
system("stty raw");
getchar();
system("stty cooked");
}
void logo() {
clear();
const char *logo = "\x1B[31m ,-. _,---._ __ / \\\n"
" / ) .-' `./ / \\\n"
"( ( ,' `/ /|\n"
" \\ `-' \\'\\ / |\n"
" `. , \\ \\ / |\n"
" /`. ,'-`----Y |\n"
" ( ; | '\n"
" | ,-. ,-' | /\n"
" | | ( | \x1B[35mfleksOS\x1B[31m | /\n"
" ) | \\ `.___________|/\n"
" `--' `--'\n\033[0m";
cout << logo << std::endl << std::endl;
cout << "\x1B[35mДля продолжения нажмите любую кнопку...\033[0m";
system("stty raw");
getchar();
system("stty cooked");
}
int main() {
logo();
bool loop = true;
while (loop) {
clear();
cout << "\x1B[31m"
<< "Доступные действия: "
<< "\033[0m" << endl;
cout << "\x1B[35m[1] Запустить систему\033[0m" << endl;
cout << "\x1B[35m[2] Выполнить установку системы\033[0m" << endl;
cout << "\x1B[35m[3] Выход\033[0m" << endl << endl;
cout << "\x1B[31m"
<< "Ваш выбор: "
<< "\033[0m";
ConsoleInput *input = Console::prompt();
if (input->cmd.length() > 0) {
switch (atoi(input->cmd.c_str())) {
case 1:
start();
break;
case 2:
install();
break;
case 3:
clear();
loop = false;
break;
default:
cerr << "Неверный выбор" << endl;
break;
}
}
}
return 0;
}