-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
74 lines (55 loc) · 2.01 KB
/
shell.cpp
File metadata and controls
74 lines (55 loc) · 2.01 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
#include "../core/programRegistry.hpp"
#include "../core/task.hpp"
#include "../drivers/deviceManager.hpp"
#include "shell.hpp"
bool __SHELL_INIT = false;
void program::shell::init() {
// only init the shell program once
if (__SHELL_INIT) return;
__SHELL_INIT = true;
os::programs.addProgram("shell", &program::shell::main);
os::programs.addAlias("sh", "shell");
}
String program::shell::version = "\nShell 0.1.0";
int program::shell::main(pEnv env, std::vector<String>* args) {
env.std_out->println(program::shell::version);
pEnv* envPtr = new pEnv(env);
program::shell::process* proc = new program::shell::process(envPtr);
proc->name = "shell";
globalEventloop.add(proc);
}
// takes a string command and executes programs based on parameters
int program::shell::execute(pEnv env, String command) {
// TODO
std::vector<String>* argList = os::programs.parseArgumentList(command);
// just in case a empty string has been entered
if (!argList->size()) return 0;
String programName = argList->at(0);
programRegistry::returnState returns = os::programs.runProgram(env, argList);
if (!returns.found) env.std_err->println("Program '" + programName + "' not fund!");
return returns.returncode;
}
// TASK CODE //
int program::shell::process::run() {
this->nextRun(0.1);
// delay(100);
// make sure to only run, if the env is not occupied
if (this->isEnvLocked()) return 0;
if (REQ_LOW_PERFORMANCE) {
this->nextRun(0.5);
}
if (!this->env->std_in->available()) return 0;
String input = "";
while (this->env->std_in->available()) {
input += this->env->std_in->read();
}
int len = input.length()-1;
Serial.println(String("#> ") + input[len]);
if (input[len] == '\n') len--;
input = input.substring(0,len);
this->env->std_out->println(">>> "+input);
if (input == ".lock") this->lockEnv();
if (input == ".unlock") this->unlockEnv();
program::shell::execute(*env,input);
return 0;
}