Refactor kernel shell code

This commit is contained in:
EnderIce2 2024-02-27 16:18:50 +02:00
parent ae67df10ff
commit e75dbfc8ce
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD

View File

@ -90,12 +90,12 @@ void StartKernelShell()
KPrint("Starting kernel shell..."); KPrint("Starting kernel shell...");
thisThread->SetPriority(Tasking::TaskPriority::High); thisThread->SetPriority(Tasking::TaskPriority::High);
std::string Buffer; std::string strBuf;
std::vector<std::string *> History; std::vector<std::string *> history;
size_t HistoryIndex = 0; size_t hIdx = 0;
bool CtrlDown = false; bool ctrlDown = false;
bool UpperCase = false; bool upperCase = false;
bool TabDoublePress = false; bool tabDblPress = false;
int kfd = fopen("/dev/key", "r"); int kfd = fopen("/dev/key", "r");
if (kfd < 0) if (kfd < 0)
@ -108,8 +108,8 @@ void StartKernelShell()
printf("Using \eCA21F6/dev/key\eCCCCCC for keyboard input.\n"); printf("Using \eCA21F6/dev/key\eCCCCCC for keyboard input.\n");
while (true) while (true)
{ {
size_t BackspaceCount = 0; size_t bsCount = 0;
Buffer.clear(); strBuf.clear();
vfs::Node *cwd = thisProcess->CurrentWorkingDirectory; vfs::Node *cwd = thisProcess->CurrentWorkingDirectory;
if (!cwd) if (!cwd)
@ -146,18 +146,18 @@ void StartKernelShell()
case KEY_RIGHT_CTRL: case KEY_RIGHT_CTRL:
{ {
if (sc & KEY_PRESSED) if (sc & KEY_PRESSED)
CtrlDown = true; ctrlDown = true;
else else
CtrlDown = false; ctrlDown = false;
continue; continue;
} }
case KEY_LEFT_SHIFT: case KEY_LEFT_SHIFT:
case KEY_RIGHT_SHIFT: case KEY_RIGHT_SHIFT:
{ {
if (sc & KEY_PRESSED) if (sc & KEY_PRESSED)
UpperCase = true; upperCase = true;
else else
UpperCase = false; upperCase = false;
continue; continue;
} }
case KEY_BACKSPACE: case KEY_BACKSPACE:
@ -165,12 +165,12 @@ void StartKernelShell()
if (!(sc & KEY_PRESSED)) if (!(sc & KEY_PRESSED))
continue; continue;
if (BackspaceCount == 0) if (bsCount == 0)
continue; continue;
Display->Print('\b'); Display->Print('\b');
Buffer.pop_back(); strBuf.pop_back();
BackspaceCount--; bsCount--;
Display->UpdateBuffer(); Display->UpdateBuffer();
continue; continue;
} }
@ -179,21 +179,21 @@ void StartKernelShell()
if (!(sc & KEY_PRESSED)) if (!(sc & KEY_PRESSED))
continue; continue;
if (History.size() == 0 || if (history.size() == 0 ||
HistoryIndex == 0) hIdx == 0)
continue; continue;
HistoryIndex--; hIdx--;
for (size_t i = 0; i < Buffer.size(); i++) for (size_t i = 0; i < strBuf.size(); i++)
Display->Print('\b'); Display->Print('\b');
Display->UpdateBuffer(); Display->UpdateBuffer();
Buffer = History[HistoryIndex]->c_str(); strBuf = history[hIdx]->c_str();
for (size_t i = 0; i < strlen(Buffer.c_str()); i++) for (size_t i = 0; i < strlen(strBuf.c_str()); i++)
Display->Print(Buffer[i]); Display->Print(strBuf[i]);
BackspaceCount = Buffer.size(); bsCount = strBuf.size();
Display->UpdateBuffer(); Display->UpdateBuffer();
continue; continue;
} }
@ -202,31 +202,31 @@ void StartKernelShell()
if (!(sc & KEY_PRESSED)) if (!(sc & KEY_PRESSED))
continue; continue;
if (History.size() == 0 || if (history.size() == 0 ||
HistoryIndex == History.size()) hIdx == history.size())
continue; continue;
if (HistoryIndex == History.size() - 1) if (hIdx == history.size() - 1)
{ {
HistoryIndex++; hIdx++;
for (size_t i = 0; i < Buffer.size(); i++) for (size_t i = 0; i < strBuf.size(); i++)
Display->Print('\b'); Display->Print('\b');
BackspaceCount = Buffer.size(); bsCount = strBuf.size();
Display->UpdateBuffer(); Display->UpdateBuffer();
continue; continue;
} }
for (size_t i = 0; i < Buffer.size(); i++) for (size_t i = 0; i < strBuf.size(); i++)
Display->Print('\b'); Display->Print('\b');
Display->UpdateBuffer(); Display->UpdateBuffer();
HistoryIndex++; hIdx++;
Buffer = History[HistoryIndex]->c_str(); strBuf = history[hIdx]->c_str();
for (size_t i = 0; i < strlen(Buffer.c_str()); i++) for (size_t i = 0; i < strlen(strBuf.c_str()); i++)
Display->Print(Buffer[i]); Display->Print(strBuf[i]);
BackspaceCount = Buffer.size(); bsCount = strBuf.size();
Display->UpdateBuffer(); Display->UpdateBuffer();
continue; continue;
} }
@ -235,13 +235,13 @@ void StartKernelShell()
if (!(sc & KEY_PRESSED)) if (!(sc & KEY_PRESSED))
continue; continue;
if (!TabDoublePress) if (!tabDblPress)
{ {
TabDoublePress = true; tabDblPress = true;
continue; continue;
} }
TabDoublePress = false; tabDblPress = false;
if (Buffer.size() == 0) if (strBuf.size() == 0)
{ {
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++)
printf("%s ", commands[i].Name); printf("%s ", commands[i].Name);
@ -251,18 +251,18 @@ void StartKernelShell()
goto SecLoopEnd; goto SecLoopEnd;
} }
for (size_t i = 0; i < Buffer.size(); i++) for (size_t i = 0; i < strBuf.size(); i++)
Display->Print('\b'); Display->Print('\b');
Display->UpdateBuffer(); Display->UpdateBuffer();
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++)
{ {
if (strncmp(Buffer.c_str(), commands[i].Name, Buffer.size()) == 0) if (strncmp(strBuf.c_str(), commands[i].Name, strBuf.size()) == 0)
{ {
Buffer = commands[i].Name; strBuf = commands[i].Name;
for (size_t i = 0; i < strlen(Buffer.c_str()); i++) for (size_t i = 0; i < strlen(strBuf.c_str()); i++)
Display->Print(Buffer[i]); Display->Print(strBuf[i]);
BackspaceCount = Buffer.size(); bsCount = strBuf.size();
Display->UpdateBuffer(); Display->UpdateBuffer();
break; break;
} }
@ -279,9 +279,9 @@ void StartKernelShell()
if (!Driver::IsValidChar(sc)) if (!Driver::IsValidChar(sc))
continue; continue;
char c = Driver::GetScanCode(sc, UpperCase); char c = Driver::GetScanCode(sc, upperCase);
if (CtrlDown) if (ctrlDown)
{ {
switch (std::toupper((char)c)) switch (std::toupper((char)c))
{ {
@ -311,33 +311,33 @@ void StartKernelShell()
Display->Print(c); Display->Print(c);
if (c == '\n') if (c == '\n')
{ {
if (Buffer.length() > 0) if (strBuf.length() > 0)
{ {
std::string *hBuff = new std::string(Buffer.c_str()); std::string *hBuff = new std::string(strBuf.c_str());
History.push_back(hBuff); history.push_back(hBuff);
HistoryIndex = History.size(); hIdx = history.size();
} }
break; break;
} }
Buffer += c; strBuf += c;
BackspaceCount++; bsCount++;
Display->UpdateBuffer(); Display->UpdateBuffer();
} }
SecLoopEnd: SecLoopEnd:
if (Buffer.length() == 0) if (strBuf.length() == 0)
continue; continue;
bool Found = false; bool Found = false;
for (size_t i = 0; i < sizeof(commands) / sizeof(Command); i++) for (size_t i = 0; i < sizeof(commands) / sizeof(Command); i++)
{ {
std::string cmd_extracted; std::string cmd_extracted;
for (size_t i = 0; i < Buffer.length(); i++) for (size_t i = 0; i < strBuf.length(); i++)
{ {
if (Buffer[i] == ' ') if (strBuf[i] == ' ')
break; break;
cmd_extracted += Buffer[i]; cmd_extracted += strBuf[i];
} }
// debug("cmd: %s, array[%d]: %s", cmd_extracted.c_str(), i, commands[i].Name); // debug("cmd: %s, array[%d]: %s", cmd_extracted.c_str(), i, commands[i].Name);
@ -350,19 +350,19 @@ void StartKernelShell()
std::string arg_only; std::string arg_only;
const char *cmd_name = commands[i].Name; const char *cmd_name = commands[i].Name;
for (size_t i = strlen(cmd_name) + 1; i < Buffer.length(); i++) for (size_t i = strlen(cmd_name) + 1; i < strBuf.length(); i++)
arg_only += Buffer[i]; arg_only += strBuf[i];
if (commands[i].Function) if (commands[i].Function)
commands[i].Function(arg_only.c_str()); commands[i].Function(arg_only.c_str());
else else
{ {
std::string cmd_only; std::string cmd_only;
for (size_t i = 0; i < Buffer.length(); i++) for (size_t i = 0; i < strBuf.length(); i++)
{ {
if (Buffer[i] == ' ') if (strBuf[i] == ' ')
break; break;
cmd_only += Buffer[i]; cmd_only += strBuf[i];
} }
printf("%s: command not implemented\n", printf("%s: command not implemented\n",
cmd_only.c_str()); cmd_only.c_str());
@ -374,11 +374,11 @@ void StartKernelShell()
if (!Found) if (!Found)
{ {
std::string cmd_only; std::string cmd_only;
for (size_t i = 0; i < Buffer.length(); i++) for (size_t i = 0; i < strBuf.length(); i++)
{ {
if (Buffer[i] == ' ') if (strBuf[i] == ' ')
break; break;
cmd_only += Buffer[i]; cmd_only += strBuf[i];
} }
printf("%s: command not found\n", printf("%s: command not found\n",
cmd_only.c_str()); cmd_only.c_str());