Repository navigation
Expand file tree
/
Copy pathShellPrompt.cpp
More file actions
390 lines (351 loc) · 13.7 KB
/
Copy pathShellPrompt.cpp
File metadata and controls
390 lines (351 loc) · 13.7 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "ShellPrompt.h"
#include "Kernel.h"
#include "SystemConfigReader.h"
#include "Process.h"
#include <sstream>
#include <cstdint>
// ===================================================
// Anonymous helper namespace
// ===================================================
namespace {
const char* CSOPESY_ASCII_ART = R"(
___ ____ __ ____ ____ ____ _ _
/ __)/ ___) / \( _ \( __)/ ___)( \/ )
( (__ \___ \( O )) __/ ) _) \___ \ ) /
\___)(____/ \__/(__) (____)(____/(__/
)";
const std::string INITIAL_PROMPT_STR = "OS_Boot>";
const std::string MAIN_SHELL_PROMPT_STR = "root:\\>";
const std::string INITIAL_BOOT_INSTRUCTIONS = "To start the main shell, type 'initialize'. To shut down, type 'exit'.";
const std::string MAIN_SHELL_INSTRUCTIONS = "Type 'help' for available commands, or 'exit' to quit.";
}
struct ParsedCommand {
std::string commandName;
std::vector<std::string> args;
};
// ===================================================
// Public functions
// ===================================================
ShellPrompt::ShellPrompt(Kernel& kernel)
: kernel(kernel)
{
setupCommands();
}
void ShellPrompt::run() {
kernel.clearScreen();
showHeader(INITIAL_BOOT_INSTRUCTIONS);
if (!runInitialBootPrompt(INITIAL_PROMPT_STR)) {
return;
}
showHeader(MAIN_SHELL_INSTRUCTIONS);
runMainShellLoop(MAIN_SHELL_PROMPT_STR);
}
// ===================================================
// Private helper functions
// ===================================================
void ShellPrompt::showHeader(const std::string& instructions) const {
kernel.print(CSOPESY_ASCII_ART);
kernel.print("\n");
kernel.print("Hello! Welcome to the CSOPESY commandline!\n");
kernel.print("\n");
kernel.print(instructions);
kernel.print("\n");
kernel.printHorizontalLine();
}
bool ShellPrompt::runInitialBootPrompt(const std::string& prompt) {
std::string command;
while (true) {
command = kernel.readLine(prompt);
if (command == "initialize") {
initializeKernel();
return true;
} else if (command == "exit") {
return false;
} else {
kernel.print("Invalid command. Please type 'initialize' or 'exit'.\n");
}
}
}
void ShellPrompt::initializeKernel() {
kernel.print("Initializing main OS shell...\n");
SystemConfig loadedConfig;
bool configLoaded = readConfigFromFile("config.txt", loadedConfig, kernel);
if (configLoaded) {
kernel.print("Configuration loaded successfully. Sending to kernel...\n");
} else {
kernel.print("Configuration loading failed or had errors. Using default values for unconfigured/invalid parameters.\n");
}
kernel.initialize(loadedConfig);
}
void ShellPrompt::runMainShellLoop(const std::string& prompt) {
std::string command;
while (true) {
command = kernel.readLine(prompt);
ParsedCommand parsed = parseCommand(command);
if (!executeCommand(parsed)) {
break;
}
}
}
ParsedCommand ShellPrompt::parseCommand(const std::string& command) const {
ParsedCommand parsed;
std::istringstream iss(command);
// First word is the command name
iss >> parsed.commandName;
// The rest are args
std::string arg;
while (iss >> arg) {
parsed.args.push_back(arg);
}
return parsed;
}
bool ShellPrompt::executeCommand(const ParsedCommand& parsed) {
auto it = commandHandlers.find(parsed.commandName);
if (it != commandHandlers.end()) {
return it->second(parsed.args);
} else {
kernel.print("Unknown command: '" + parsed.commandName + "'. Type 'help' for assistance.\n");
return true;
}
}
void ShellPrompt::showHelp() const {
kernel.printHorizontalLine();
kernel.print("Available Commands\n");
kernel.print("exit - Quits the main OS shell.\n");
kernel.print("help - Displays this help message.\n");
kernel.print("echo - Echoes the command back.\n");
kernel.print("clear - Clears the terminal screen.\n");
kernel.print("scheduler-start - Starts automatic process generation.\n");
kernel.print("scheduler-stop - Stops automatic process generation.\n");
kernel.print("screen -ls - Lists CPU utilization, core usage, and a summary of all running and finished processes\n");
kernel.print("screen -r <process_name> - Reattach to the screen of an existing process\n");
kernel.print("screen -s <process_memory_size> <process_name> - Start a new process\n");
kernel.print("screen -c <process_name> \"<instructions>\" - Creates a new process with user-input instructions.\n");
kernel.print("process-smi - Print a summarized view of the memory allocation and CPU utilization\n");
kernel.print("vmstat - Print a detailed view of the memory allocation\n");
kernel.printHorizontalLine();
}
void ShellPrompt::setupCommands() {
commandHandlers["exit"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: exit\n");
return true;
}
return false;
};
commandHandlers["help"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: help\n");
return true;
}
showHelp();
return true;
};
commandHandlers["echo"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: echo <message>\n");
return true;
}
kernel.print("echo");
for (const auto& arg : args) {
kernel.print(" ");
kernel.print(arg);
}
kernel.print("\n");
return true;
};
commandHandlers["clear"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: clear\n");
return true;
}
kernel.clearScreen();
showHeader(MAIN_SHELL_INSTRUCTIONS);
return true;
};
commandHandlers["scheduler-start"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: scheduler-start\n");
return true;
}
kernel.startProcessGeneration();
return true;
};
commandHandlers["scheduler-stop"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: scheduler-stop\n");
return true;
}
kernel.stopProcessGeneration();
return true;
};
commandHandlers["process-smi"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: process-smi\n");
return true;
}
kernel.printMemoryUtilizationReport();
return true;
};
commandHandlers["vmstat"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: vmstat\n");
return true;
}
kernel.printMemoryStatistics();
return true;
};
commandHandlers["report-util"] = [this](const std::vector<std::string>& args) -> bool {
if (!args.empty()) {
kernel.print("Usage: report-util\n");
return true;
}
kernel.exportListStatusToFile("csopesy-log.txt");
return true;
};
commandHandlers["screen"] = [this](const std::vector<std::string>& args) -> bool {
if (args.empty()) {
kernel.print("Usage: screen <subcommand> [args...]\n");
kernel.print("Subcommands: -ls, -r, -s\n");
return true;
}
const std::string& subCommand = args[0];
if (subCommand == "-ls") {
if (args.size() != 1) {
kernel.print("Usage: screen -ls\n");
} else {
kernel.listStatus();
}
} else if (subCommand == "-r") {
if (args.size() != 2) {
kernel.print("Usage: screen -r <process_name>\n");
} else {
handleScreenReattach(args);
}
} else if (subCommand == "-s") {
if (args.size() != 3) {
kernel.print("Usage: screen -s <process_memory_size> <process_name>\n");
} else {
handleScreenStart(args);
}
} else if (subCommand == "-c") {
if (args.size() < 3) {
kernel.print("Usage: screen -c <process_name> \"<instructions>\"\n");
} else {
handleScreenCreate(args);
}
} else {
kernel.print("Unknown 'screen' subcommand: '" + subCommand + "'.\n");
kernel.print("Usage: screen <subcommand> [args...]\n");
kernel.print("Subcommands: -ls, -r, -s\n");
}
return true;
};
}
void ShellPrompt::handleScreenReattach(const std::vector<std::string>& args) const {
std::string process_name = args[1];
Process* processFound = kernel.reattachToProcess(process_name);
if (processFound) {
handleScreenMenu(processFound);
} else {
kernel.print("Process with name '" + process_name + "' not found or terminated.\n");
}
}
void ShellPrompt::handleScreenStart(const std::vector<std::string>& args) {
std::string memory_str = args[1];
std::string process_name = args[2];
uint32_t memValue = 64;
std::istringstream iss(memory_str);
unsigned long temp = 0;
if (iss >> temp && iss.eof()) {
if (temp >= 64 && temp <= std::numeric_limits<uint32_t>::max()) {
memValue = static_cast<uint32_t>(temp);
} else {
kernel.print("Error: Memory size must be a positive integer within the valid range.\n");
return;
}
} else {
kernel.print("Error: Invalid memory size format. Please enter a positive whole number.\n");
return;
}
Process* processFound = kernel.reattachToProcess(process_name);
if (processFound) {
handleScreenMenu(processFound);
} else {
processFound = kernel.startProcess(process_name, memValue);
handleScreenMenu(processFound);
}
}
void ShellPrompt::handleScreenCreate(const std::vector<std::string>& args) {
std::string processName = args[1];
// Reconstruct the instruction string from the command line arguments
std::string full_command_line;
for (size_t i = 0; i < args.size(); ++i) {
full_command_line += args[i];
if (i < args.size() - 1) {
full_command_line += " ";
}
}
// Find the quoted instruction block
size_t quoteStart = full_command_line.find('"');
size_t quoteEnd = std::string::npos;
if (quoteStart != std::string::npos) {
quoteEnd = full_command_line.rfind('"');
}
// Validate the quoted string
if (quoteStart == std::string::npos || quoteEnd == std::string::npos || quoteEnd <= quoteStart) {
kernel.print("Error: Malformed instructions. Please use a single, double-quoted string.\n");
return;
}
std::string instructionString = full_command_line.substr(quoteStart + 1, quoteEnd - quoteStart - 1);
// Trim leading and trailing whitespace from the instruction string
size_t first = instructionString.find_first_not_of(" \t\n\r");
if (std::string::npos == first) {
// The instructions are empty or only contain whitespace. Reject.
kernel.print("Error: Instructions cannot be empty.\n");
return;
}
size_t last = instructionString.find_last_not_of(" \t\n\r");
instructionString = instructionString.substr(first, (last - first + 1));
// Enforce the rule: the final instruction must end with a semicolon.
if (instructionString.empty() || instructionString.back() != ';') {
kernel.print("Error: All instructions must end with a semicolon.\n");
return;
}
// Split the instructions by semicolon
std::vector<std::string> rawInstructions;
std::string currentInstruction;
std::istringstream iss(instructionString);
while (std::getline(iss, currentInstruction, ';')) {
// Trim leading and trailing whitespace for each instruction
size_t trimmed_first = currentInstruction.find_first_not_of(" \t\n\r");
if (std::string::npos != trimmed_first) {
size_t trimmed_last = currentInstruction.find_last_not_of(" \t\n\r");
rawInstructions.push_back(currentInstruction.substr(trimmed_first, (trimmed_last - trimmed_first + 1)));
}
}
if (!rawInstructions.empty()) {
Process* processCreated = kernel.createUserDefinedProcess(processName, rawInstructions);
if (processCreated) {
handleScreenMenu(processCreated);
}
} else {
kernel.print("Error: Instructions cannot be empty.\n");
}
}
void ShellPrompt::handleScreenMenu(Process* process) const {
std::string screen_command;
const std::string screen_prompt_str = process->getPname() + ":\\>";
while (true) {
screen_command = kernel.readLine(screen_prompt_str);
if (screen_command == "exit") {
break;
} else if (screen_command == "process-smi") {
kernel.printSmi(process);
} else {
kernel.print("Invalid command. Please type 'process-smi' or 'exit'.\n");
}
}
kernel.clearScreen();
showHeader(MAIN_SHELL_INSTRUCTIONS);
}