Merge remote-tracking branch 'Kernel/master'

This commit is contained in:
EnderIce2
2024-11-20 05:00:33 +02:00
468 changed files with 112800 additions and 1 deletions

50
Kernel/kshell/cmds.hpp Normal file
View File

@ -0,0 +1,50 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __FENNIX_KERNEL_SHELL_CMDS_H__
#define __FENNIX_KERNEL_SHELL_CMDS_H__
#include <types.h>
void cmd_clear(const char *args);
void cmd_echo(const char *args);
void cmd_ls(const char *args);
void cmd_tree(const char *args);
void cmd_cd(const char *args);
void cmd_cat(const char *args);
void cmd_ps(const char *args);
void cmd_uptime(const char *args);
void cmd_whoami(const char *args);
void cmd_uname(const char *args);
void cmd_mem(const char *args);
void cmd_kill(const char *args);
void cmd_killall(const char *args);
void cmd_top(const char *args);
void cmd_exit(const char *args);
void cmd_shutdown(const char *args);
void cmd_reboot(const char *args);
void cmd_lspci(const char *args);
void cmd_lsacpi(const char *args);
void cmd_lsmod(const char *args);
void cmd_modinfo(const char *args);
void cmd_panic(const char *args);
void cmd_dump(const char *args);
void cmd_theme(const char *args);
#define IF_ARG(x) strcmp(args, x) == 0
#endif // !__FENNIX_KERNEL_SHELL_CMDS_H__

View File

@ -0,0 +1,61 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_cat(const char *args)
{
if (args[0] == '\0')
return;
FileNode *node = fs->GetByPath(args, nullptr);
if (node == nullptr)
{
printf("cat: %s: No such file or directory\n", args);
return;
}
if (!node->IsRegularFile() && !node->IsCharacterDevice())
{
printf("cat: %s: Not a regular file or character device\n", args);
return;
}
if (node->IsCharacterDevice())
{
printf("cat: %s: Character devices are not supported yet\n", args);
return;
}
kstat stat = {};
node->Stat(&stat);
uint8_t *buffer = new uint8_t[stat.Size + 1];
ssize_t rBytes = node->Read(buffer, stat.Size, 0);
if (rBytes > 0)
printf("%s\n", buffer);
else
printf("cat: %s: Could not read file\n", args);
delete[] buffer;
}

View File

@ -0,0 +1,46 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_cd(const char *args)
{
if (args[0] == '\0')
return;
FileNode *node = fs->GetByPath(args, nullptr);
if (node == nullptr)
{
printf("cd: %s: No such file or directory\n", args);
return;
}
if (!node->IsDirectory())
{
printf("cd: %s: Not a directory\n", args);
return;
}
thisProcess->CWD = node;
}

View File

@ -0,0 +1,25 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_clear(const char *)
{
printf("\x1b[2J");
}

View File

@ -0,0 +1,77 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <acpi.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_dump(const char *args)
{
if (args[0] == '\0')
return;
// args == "0xdeadbeef 512" aka "address size"
char *strAddr = (char *)args;
char *strLen = (char *)args;
while (*strLen != ' ')
strLen++;
*strLen = '\0';
strLen++;
void *Address = (void *)strtoul(strAddr, nullptr, 16);
unsigned long Length = strtoul(strLen, nullptr, 10);
{
unsigned char *AddressChar = (unsigned char *)Address;
unsigned char Buffer[17];
uint32_t Iterate;
printf("Dumping %lu bytes from %#lx\n", Length, (uintptr_t)AddressChar);
for (Iterate = 0; Iterate < (uint32_t)Length; Iterate++)
{
if ((Iterate % 16) == 0)
{
if (Iterate != 0)
printf(" %s\n", Buffer);
printf(" %04x ", Iterate);
}
printf(" %02x", AddressChar[Iterate]);
if ((AddressChar[Iterate] < 0x20) || (AddressChar[Iterate] > 0x7E))
Buffer[Iterate % 16] = '.';
else
Buffer[Iterate % 16] = AddressChar[Iterate];
Buffer[(Iterate % 16) + 1] = '\0';
}
while ((Iterate % 16) != 0)
{
printf(" ");
Iterate++;
}
putchar('\n');
}
}

View File

@ -0,0 +1,25 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_echo(const char *args)
{
printf("%s\n", args);
}

View File

@ -0,0 +1,33 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
void cmd_exit(const char *)
{
KernelShutdownThread(false);
// TaskManager->KillThread(thisThread, KILL_SUCCESS);
CPU::Halt(true);
}

View File

@ -0,0 +1,39 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
void cmd_kill(const char *args)
{
PID pid = atoi(args);
PCB *pcb = TaskManager->GetProcessByID(pid);
if (pcb == nullptr)
{
printf("No process with PID %d\n", pid);
return;
}
TaskManager->KillProcess(pcb, KILL_BY_OTHER_PROCESS);
}

View File

@ -0,0 +1,37 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
void cmd_killall(const char *args)
{
foreach (auto Proc in TaskManager->GetProcessList())
{
if (strcmp(Proc->Name, args) == 0)
{
TaskManager->KillProcess(Proc, KILL_BY_OTHER_PROCESS);
}
}
}

View File

@ -0,0 +1,135 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
const char *ColorNodeType(FileNode *node)
{
if (node->IsRegularFile())
return "\x1b[32m";
else if (node->IsDirectory())
return "\x1b[34m";
else if (node->IsBlockDevice())
return "\x1b[33m";
else if (node->IsCharacterDevice())
return "\x1b[33m";
else if (node->IsFIFO())
return "\x1b[33m";
else if (node->IsSymbolicLink())
return "\x1b[35m";
else
return "\x1b[0m";
}
__no_sanitize("alignment") size_t MaxNameLength(FileNode *nodes)
{
size_t maxLength = 0;
kdirent *dirBuffer = new kdirent[16];
ssize_t read = 0;
off_t offset = 0;
while ((read = nodes->ReadDir(dirBuffer, sizeof(kdirent) * 16, offset, LONG_MAX)) > 0)
{
if (read / sizeof(kdirent) == 0)
break;
off_t bufOffset = 0;
debug("There are %ld entries in this directory", read / sizeof(kdirent));
for (size_t i = 0; i < read / sizeof(kdirent); i++)
{
kdirent *dirent = (kdirent *)((uintptr_t)dirBuffer + bufOffset);
if (dirent->d_reclen == 0)
break;
bufOffset += dirent->d_reclen;
maxLength = std::max(maxLength, strlen(dirent->d_name));
debug("dirent->d_name: %s (max length: %ld)", dirent->d_name, maxLength);
}
offset += read / sizeof(kdirent);
}
delete[] dirBuffer;
return maxLength;
}
__no_sanitize("alignment") void PrintLS(FileNode *node)
{
size_t maxNameLength = MaxNameLength(node);
int count = 0;
bool first = true;
kdirent *dirBuffer = new kdirent[16];
ssize_t read = 0;
off_t offset = 0;
while ((read = node->ReadDir(dirBuffer, sizeof(kdirent) * 16, offset, LONG_MAX)) > 0)
{
if (read / sizeof(kdirent) == 0)
break;
off_t bufOffset = 0;
for (size_t i = 0; i < read / sizeof(kdirent); i++)
{
if (count % 5 == 0 && !first)
printf("\n");
kdirent *dirent = (kdirent *)((uintptr_t)dirBuffer + bufOffset);
if (dirent->d_reclen == 0)
break;
bufOffset += dirent->d_reclen;
printf(" %s%-*s ", ColorNodeType(node), (int)maxNameLength, dirent->d_name);
count++;
first = false;
}
offset += read / sizeof(kdirent);
}
printf("\x1b[0m\n");
delete[] dirBuffer;
}
void cmd_ls(const char *args)
{
if (args[0] == '\0')
{
FileNode *rootNode = thisProcess->CWD;
if (rootNode == nullptr)
rootNode = fs->GetRoot(0);
PrintLS(rootNode);
return;
}
FileNode *thisNode = fs->GetByPath(args, nullptr);
if (thisNode == nullptr)
{
printf("ls: %s: No such file or directory\n", args);
return;
}
if (!thisNode->IsDirectory())
{
printf("%s%s\n", ColorNodeType(thisNode), thisNode->Path.c_str());
return;
}
PrintLS(thisNode);
}

View File

@ -0,0 +1,39 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <acpi.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_lsacpi(const char *)
{
ACPI::ACPI *acpi = (ACPI::ACPI *)PowerManager->GetACPI();
foreach (auto Table in acpi->Tables)
{
printf("%#lx: %.4s [%.6s:%.8s] %d bytes\n",
(uintptr_t)Table.second,
Table.second->Signature,
Table.second->OEMID,
Table.second->OEMTableID,
Table.second->Length);
}
}

View File

@ -0,0 +1,37 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_lsmod(const char *)
{
std::unordered_map<dev_t, Driver::DriverObject> drivers =
DriverManager->GetDrivers();
printf("DRIVER | ID | INIT | MEMORY\n");
foreach (auto &drv in drivers)
{
printf("%-15s | %5ld | %s | %ld KiB\n",
drv.second.Name,
drv.first,
drv.second.Initialized ? "Y" : "N",
TO_KiB(drv.second.vma->GetAllocatedMemorySize()));
}
}

View File

@ -0,0 +1,80 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_lspci(const char *args)
{
if (args)
{
if (IF_ARG("-i") || IF_ARG("--info"))
{
foreach (auto Device in PCIManager->GetDevices())
{
const char *HdrType;
switch (Device.Header->HeaderType)
{
case 0:
HdrType = "Normal ";
break;
case 1:
HdrType = "PCI-PCI";
break;
case 2:
HdrType = "Cardbus";
break;
default:
HdrType = "Unknown";
break;
}
printf("%04x:%04x | %s:%03d | %02x:%02x.%d | %s: %s %s\n",
Device.Header->VendorID,
Device.Header->DeviceID,
HdrType, Device.Header->HeaderType,
Device.Bus,
Device.Device,
Device.Function,
PCI::Descriptors::GetSubclassName(Device.Header->Class,
Device.Header->Subclass),
PCI::Descriptors::GetVendorName(Device.Header->VendorID),
PCI::Descriptors::GetDeviceName(Device.Header->VendorID,
Device.Header->DeviceID));
}
return;
}
}
foreach (auto Device in PCIManager->GetDevices())
{
printf("%02x:%02x.%d: %s: %s %s\n",
Device.Bus,
Device.Device,
Device.Function,
PCI::Descriptors::GetSubclassName(Device.Header->Class,
Device.Header->Subclass),
PCI::Descriptors::GetVendorName(Device.Header->VendorID),
PCI::Descriptors::GetDeviceName(Device.Header->VendorID,
Device.Header->DeviceID));
}
}

View File

@ -0,0 +1,64 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
void cmd_mem(const char *)
{
uint64_t total = KernelAllocator.GetTotalMemory();
uint64_t used = KernelAllocator.GetUsedMemory();
uint64_t free = KernelAllocator.GetFreeMemory();
uint64_t reserved = KernelAllocator.GetReservedMemory();
int usedPercent = (int)((used * 100) / total);
int usedBar = (int)(usedPercent / 2);
int reservedPercent = (int)((reserved * 100) / total);
int reservedBar = (int)(reservedPercent / 2);
printf("[");
for (int i = 0; i < usedBar; i++)
printf("=");
for (int i = 0; i < 50 - usedBar; i++)
printf(" ");
printf("] %d%% Used\n", usedPercent);
printf("[");
for (int i = 0; i < reservedBar; i++)
printf("=");
for (int i = 0; i < 50 - reservedBar; i++)
printf(" ");
printf("] %d%% Reserved\n", reservedPercent);
// printf("Total: %d MiB\n", (int)(TO_MiB(total)));
// printf("Used: %d MiB\n", (int)(TO_MiB(used)));
// printf("Free: %d MiB\n", (int)(TO_MiB(free)));
// printf("Reserved: %d MiB\n", (int)(TO_MiB(reserved)));
printf("TOTAL USED FREE RESERVED\n");
printf("%d MiB %d MiB %d MiB %d MiB\n",
(int)(TO_MiB(total)), (int)(TO_MiB(used)),
(int)(TO_MiB(free)), (int)(TO_MiB(reserved)));
}

View File

@ -0,0 +1,78 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_modinfo(const char *args)
{
if (args[0] == '\0')
{
printf("Usage: modinfo <driver id/name>\n");
return;
}
dev_t id = atoi(args);
std::unordered_map<dev_t, Driver::DriverObject> drivers =
DriverManager->GetDrivers();
if (drivers.find(id) == drivers.end())
{
bool found = false;
foreach (auto var in drivers)
{
if (strcmp(var.second.Name, args) == 0)
{
id = var.first;
found = true;
break;
}
}
if (!found)
{
printf("Driver not found\n");
return;
}
}
Driver::DriverObject drv = drivers[id];
char drvVersion[32];
snprintf(drvVersion, sizeof(drvVersion), "%d.%d.%d",
drv.Version.Major, drv.Version.Minor, drv.Version.Patch);
printf("Base Info:\n");
printf(" Name: %s\n", drv.Name);
printf(" Description: %s\n", drv.Description);
printf(" Author: %s\n", drv.Author);
printf(" Version: %s\n", drvVersion);
printf(" License: %s\n", drv.License);
printf("Resource Info:\n");
printf(" Initialized: %s\n", drv.Initialized ? "yes" : "no");
printf(" Error Code: %i (%s)\n", drv.ErrorCode, strerror(drv.ErrorCode));
printf(" Path: %s\n", drv.Path.c_str());
printf(" Used Memory: %ld KiB\n", TO_KiB(drv.vma->GetAllocatedMemorySize()));
printf(" Used IRQs:%s\n", drv.InterruptHandlers->empty() ? " none" : "");
foreach (auto var in *drv.InterruptHandlers)
{
printf(" IRQ%-2d: %#lx\n",
var.first, (uintptr_t)var.second);
}
}

View File

@ -0,0 +1,30 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
nsa void cmd_panic(const char *)
{
asmv("int $0x0");
__unreachable;
}

View File

@ -0,0 +1,33 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
void cmd_ps(const char *)
{
printf("PID Name\n");
foreach (auto p in TaskManager->GetProcessList())
printf("%d %s\n", p->ID, p->Name);
}

View File

@ -0,0 +1,32 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
void cmd_reboot(const char *)
{
KernelShutdownThread(true);
CPU::Halt(true);
}

View File

@ -0,0 +1,32 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
void cmd_shutdown(const char *)
{
KernelShutdownThread(false);
CPU::Halt(true);
}

View File

@ -0,0 +1,25 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_theme(const char *args)
{
KernelConsole::SetTheme(args);
}

View File

@ -0,0 +1,75 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <task.hpp>
#include "../../kernel.h"
using namespace vfs;
using namespace Tasking;
const char *TaskStateStrings[] = {
"Unknown", // Unknown
"Ready", // Ready
"Running", // Running
"Sleeping", // Sleeping
"Blocked", // Blocked
"Stopped", // Stopped
"Waiting", // Waiting
"CoreDump", // Core dump
"Zombie", // Zombie
"Terminated", // Terminated
"Frozen", // Frozen
};
void cmd_top(const char *)
{
printf("PID Name State Priority Memory Usage CPU Usage\n");
foreach (auto Proc in TaskManager->GetProcessList())
{
#if defined(a64)
printf("%-4d %-20s %s %d %ld KiB %ld\n",
Proc->ID, Proc->Name, TaskStateStrings[Proc->State.load()],
Proc->Info.Priority, TO_KiB(Proc->GetSize()),
Proc->Info.UserTime + Proc->Info.KernelTime);
#elif defined(a32)
printf("%-4d %-20s %s %d %lld KiB %lld\n",
Proc->ID, Proc->Name, TaskStateStrings[Proc->State.load()],
Proc->Info.Priority, TO_KiB(Proc->GetSize()),
Proc->Info.UserTime + Proc->Info.KernelTime);
#endif
foreach (auto Thrd in Proc->Threads)
{
#if defined(a64)
printf(" %-4d %-20s %s %d %ld KiB %ld\n",
Thrd->ID, Thrd->Name, TaskStateStrings[Thrd->State.load()],
Thrd->Info.Priority, TO_KiB(Thrd->GetSize()),
Thrd->Info.UserTime + Thrd->Info.KernelTime);
#elif defined(a32)
printf(" %-4d %-20s %s %d %lld KiB %lld\n",
Thrd->ID, Thrd->Name, TaskStateStrings[Thrd->State.load()],
Thrd->Info.Priority, TO_KiB(Thrd->GetSize()),
Thrd->Info.UserTime + Thrd->Info.KernelTime);
#endif
}
}
}

View File

@ -0,0 +1,101 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
void tree_loop(FileNode *rootNode, int depth = 0)
{
// foreach (auto Child in rootNode->GetChildren(true))
// {
// Display->UpdateBuffer();
// if (Child->Stat.IsType(DIRECTORY) || Child->Stat.IsType(MOUNTPOINT))
// {
// printf("%*s%*s%*s|- %s\n",
// depth, "",
// depth, "",
// depth, "",
// Child->FileName);
// tree_loop(Child, depth + 1);
// }
// else
// printf("%*s%*s%*s|- %s\n",
// depth, "",
// depth, "",
// depth, "",
// Child->FileName);
// }
kdirent *dirBuffer = new kdirent[16];
ssize_t read = 0;
off_t offset = 0;
while ((read = rootNode->ReadDir(dirBuffer, sizeof(kdirent) * 16, offset, LONG_MAX)) > 0)
{
if (read / sizeof(kdirent) == 0)
break;
off_t bufOffset = 0;
for (size_t i = 0; i < read / sizeof(kdirent); i++)
{
kdirent *dirent = (kdirent *)((uintptr_t)dirBuffer + bufOffset);
if (dirent->d_reclen == 0)
break;
bufOffset += dirent->d_reclen;
if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
continue;
FileNode *node = fs->GetByPath(dirent->d_name, rootNode);
if (node == nullptr)
continue;
for (int i = 0; i < depth; i++)
printf(" ");
printf("|- %s\n", dirent->d_name);
if (node->IsDirectory())
tree_loop(node, depth + 1);
}
offset += read;
}
delete[] dirBuffer;
}
void cmd_tree(const char *args)
{
FileNode *rootNode = thisProcess->CWD;
if (args[0] == '\0')
{
if (rootNode == nullptr)
rootNode = fs->GetRoot(0);
}
else
{
rootNode = fs->GetByPath(args, nullptr);
if (rootNode == nullptr)
{
printf("ls: %s: No such file or directory\n", args);
return;
}
}
printf("%s\n", rootNode->Name.c_str());
tree_loop(rootNode);
}

View File

@ -0,0 +1,65 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_uname(const char *args)
{
if (args)
{
if (IF_ARG("-a") || IF_ARG("--all"))
{
printf("Fennix Kernel %s %s %s %s\n",
KERNEL_VERSION, KERNEL_NAME, __DATE__,
KERNEL_ARCH);
}
else if (IF_ARG("-s") || IF_ARG("--kernel-name"))
{
printf("%s\n", KERNEL_NAME);
}
else if (IF_ARG("-v") || IF_ARG("--kernel-version"))
{
printf("%s\n", KERNEL_VERSION);
}
else if (IF_ARG("-n") || IF_ARG("--nodename"))
{
printf("unknown\n");
}
else if (IF_ARG("-r") || IF_ARG("--kernel-release"))
{
printf("%s\n", KERNEL_NAME);
}
else if (IF_ARG("-m") || IF_ARG("--machine"))
{
printf("%s\n", KERNEL_ARCH);
}
else
{
printf("uname: invalid option: %s\n", args);
}
}
else
{
printf("Fennix Kernel\n");
}
}

View File

@ -0,0 +1,57 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_uptime(const char *)
{
if (TimeManager)
{
uint64_t Nanoseconds =
TimeManager->GetNanosecondsSinceClassCreation();
uint64_t Seconds = Nanoseconds / 10000000;
uint64_t Minutes = Seconds / 60;
uint64_t Hours = Minutes / 60;
uint64_t Days = Hours / 24;
debug("Nanoseconds: %ld", Nanoseconds);
Seconds %= 60;
Minutes %= 60;
Hours %= 24;
#if defined(a64)
printf("%ld days, %ld hours, %ld minutes, %ld %s\n",
Days, Hours, Minutes, Seconds,
Seconds == 1 ? "second" : "seconds");
#elif defined(a32)
printf("%lld days, %lld hours, %lld minutes, %lld %s\n",
Days, Hours, Minutes, Seconds,
Seconds == 1 ? "second" : "seconds");
#endif
}
else
{
printf("Could not get uptime\n");
}
}

View File

@ -0,0 +1,29 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_whoami(const char *)
{
printf("kernel\n");
}

637
Kernel/kshell/shell.cpp Normal file
View File

@ -0,0 +1,637 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <kshell.hpp>
#include <interface/driver.h>
#include <interface/input.h>
#include <filesystem.hpp>
#include <driver.hpp>
#include <lock.hpp>
#include <exec.hpp>
#include <debug.h>
#include <thread>
#include <cctype>
#include "../kernel.h"
#include "cmds.hpp"
NewLock(ShellLock);
struct Command
{
const char *Name;
void (*Function)(const char *);
};
bool ignoreBuiltin = false;
void __cmd_builtin(const char *)
{
ignoreBuiltin = !ignoreBuiltin;
if (ignoreBuiltin)
printf("Builtin commands are now ignored.\n");
else
printf("Builtin commands are now accepted.\n");
}
static Command commands[] = {
{"ls", cmd_ls},
{"tree", cmd_tree},
{"cd", cmd_cd},
{"cat", cmd_cat},
{"echo", cmd_echo},
{"clear", cmd_clear},
{"help", nullptr},
{"exit", cmd_exit},
{"reboot", cmd_reboot},
{"shutdown", cmd_shutdown},
{"ps", cmd_ps},
{"kill", cmd_kill},
{"killall", cmd_killall},
{"top", cmd_top},
{"mem", cmd_mem},
{"uname", cmd_uname},
{"whoami", cmd_whoami},
{"uptime", cmd_uptime},
{"lspci", cmd_lspci},
{"lsacpi", cmd_lsacpi},
{"lsmod", cmd_lsmod},
{"modinfo", cmd_modinfo},
{"insmod", nullptr},
{"rmmod", nullptr},
{"modprobe", nullptr},
{"depmod", nullptr},
{"panic", cmd_panic},
{"dump", cmd_dump},
{"theme", cmd_theme},
{"builtin", __cmd_builtin},
};
void KShellThread()
{
assert(!ShellLock.Locked());
ShellLock.Lock(__FUNCTION__);
KPrint("Starting kernel shell...");
thisThread->SetPriority(Tasking::TaskPriority::Normal);
std::string strBuf = "";
std::vector<std::string *> history;
size_t hIdx = 0;
bool ctrlDown = false;
bool upperCase = false;
bool tabDblPress = false;
const char *keyDevPath = "/dev/input/keyboard";
FileNode *kfd = fs->GetByPath(keyDevPath, fs->GetRoot(0));
if (kfd == nullptr)
{
KPrint("Failed to open keyboard device!");
return;
}
/* This makes debugging easier. */
auto strBufBck = [&]()
{
for (size_t i = 0; i < strBuf.size(); i++)
{
putchar('\b');
putchar(' ');
putchar('\b');
}
};
printf("Using \x1b[1;34m%s\x1b[0m for keyboard input.\n", keyDevPath);
while (true)
{
size_t bsCount = 0;
uint32_t homeX = 0, homeY = 0;
uint32_t unseekX = 0, unseekY = 0;
size_t seekCount = 0;
debug("clearing strBuf(\"%s\")", strBuf.c_str());
strBuf.clear();
FileNode *cwd = thisProcess->CWD;
if (!cwd)
cwd = fs->GetRoot(0);
std::string cwdStr = fs->GetByNode(cwd);
debug("cwd: %*s", (int)cwdStr.size(), cwdStr.c_str());
printf("\x1b[1;34m%s@%s:%s$ \x1b[0m",
"kernel", "fennix",
cwdStr.c_str());
KeyboardReport scBuf{};
ssize_t nBytes;
while (true)
{
nBytes = kfd->Read(&scBuf, sizeof(KeyboardReport), 0);
if (nBytes == 0)
{
debug("Empty read from keyboard device!");
continue;
}
if (nBytes < (ssize_t)sizeof(KeyboardReport))
{
KPrint("Failed to read from keyboard device: %s",
strerror((int)nBytes));
return;
}
const KeyScanCodes &sc = scBuf.Key;
switch (sc & ~KEY_PRESSED)
{
case KEY_LEFT_CTRL:
case KEY_RIGHT_CTRL:
{
if (sc & KEY_PRESSED)
ctrlDown = true;
else
ctrlDown = false;
continue;
}
case KEY_LEFT_SHIFT:
case KEY_RIGHT_SHIFT:
{
if (sc & KEY_PRESSED)
upperCase = true;
else
upperCase = false;
continue;
}
case KEY_TAB:
{
if (!(sc & KEY_PRESSED))
continue;
if (!tabDblPress)
{
tabDblPress = true;
continue;
}
tabDblPress = false;
if (strBuf.size() == 0)
{
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++)
printf("%s ", commands[i].Name);
putchar('\n');
goto SecLoopEnd;
}
strBufBck();
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++)
{
if (strncmp(strBuf.c_str(), commands[i].Name, strBuf.size()) != 0)
continue;
strBuf = commands[i].Name;
for (size_t i = 0; i < strlen(strBuf.c_str()); i++)
putchar(strBuf[i]);
seekCount = bsCount = strBuf.size();
break;
}
continue;
}
case KEY_BACKSPACE:
{
if (!(sc & KEY_PRESSED))
continue;
if (bsCount == 0)
continue;
if (seekCount == bsCount)
{
debug("seekCount == bsCount (%d == %d)",
seekCount, bsCount);
putchar('\b');
putchar(' ');
putchar('\b');
strBuf.pop_back();
seekCount = --bsCount;
continue;
}
strBufBck();
size_t strSeek = seekCount ? seekCount - 1 : 0;
seekCount = strSeek;
debug("strSeek: %d: %s", strSeek, strBuf.c_str());
strBuf.erase(strSeek);
printf("%s", strBuf.c_str());
debug("after strBuf: %s", strBuf.c_str());
bsCount--;
continue;
}
case KEY_DELETE:
{
if (!(sc & KEY_PRESSED))
continue;
if (bsCount == 0)
continue;
if (seekCount == bsCount)
{
debug("seekCount == bsCount (%d == %d)",
seekCount, bsCount);
continue;
}
strBufBck();
debug("seekCount: %d: %s", seekCount, strBuf.c_str());
strBuf.erase(seekCount);
printf("%s", strBuf.c_str());
debug("after strBuf: %s", strBuf.c_str());
bsCount--;
continue;
}
case KEY_UP_ARROW:
{
if (!(sc & KEY_PRESSED))
continue;
if (history.size() == 0 ||
hIdx == 0)
continue;
hIdx--;
if (unseekX != 0 || unseekY != 0)
{
unseekX = unseekY = 0;
}
strBufBck();
strBuf = history[hIdx]->c_str();
for (size_t i = 0; i < strlen(strBuf.c_str()); i++)
putchar(strBuf[i]);
seekCount = bsCount = strBuf.size();
continue;
}
case KEY_DOWN_ARROW:
{
if (!(sc & KEY_PRESSED))
continue;
if (history.size() == 0 ||
hIdx == history.size())
continue;
if (hIdx == history.size() - 1)
{
if (unseekX != 0 || unseekY != 0)
unseekX = unseekY = 0;
hIdx++;
strBufBck();
seekCount = bsCount = strBuf.size();
continue;
}
if (unseekX != 0 || unseekY != 0)
unseekX = unseekY = 0;
strBufBck();
hIdx++;
strBuf = history[hIdx]->c_str();
for (size_t i = 0; i < strlen(strBuf.c_str()); i++)
putchar(strBuf[i]);
seekCount = bsCount = strBuf.size();
continue;
}
case KEY_LEFT_ARROW:
{
if (!(sc & KEY_PRESSED))
continue;
if (seekCount == 0)
continue;
debug("orig seekCount: %d", seekCount);
seekCount--;
if (ctrlDown)
{
uint32_t offset = 0;
/* We use unsigned so this will underflow to SIZE_MAX
and it is safe because we add 1 to it. */
while (seekCount != SIZE_MAX && strBuf[seekCount] == ' ')
{
seekCount--;
offset++;
}
while (seekCount != SIZE_MAX && strBuf[seekCount] != ' ')
{
seekCount--;
offset++;
}
seekCount++;
debug("offset: %d; seekCount: %d", offset, seekCount);
continue;
}
continue;
}
case KEY_RIGHT_ARROW:
{
if (!(sc & KEY_PRESSED))
continue;
if (seekCount == bsCount)
continue;
seekCount++;
debug("orig seekCount: %d", seekCount);
if (ctrlDown)
{
uint32_t offset = 0;
while (seekCount <= bsCount && strBuf[seekCount] != ' ')
{
seekCount++;
offset++;
}
while (seekCount <= bsCount && strBuf[seekCount] == ' ')
{
seekCount++;
offset++;
}
seekCount--;
debug("offset: %d; seekCount: %d", offset, seekCount);
continue;
}
continue;
}
case KEY_HOME:
{
if (!(sc & KEY_PRESSED))
continue;
if (homeX == 0 || homeY == 0)
continue;
seekCount = 0;
debug("seekCount set to 0");
continue;
}
case KEY_END:
{
if (!(sc & KEY_PRESSED))
continue;
if (unseekX == 0 || unseekY == 0)
continue;
seekCount = bsCount;
debug("seekCount set to bsCount (%d)", bsCount);
continue;
}
default:
break;
}
if (!(sc & KEY_PRESSED))
continue;
if (!Driver::IsValidChar(sc))
continue;
char c = Driver::GetScanCode(sc, upperCase);
debug("sc: %#lx, uc: %d -> %c", sc, upperCase, c);
if (ctrlDown)
{
switch (std::toupper((char)c))
{
case 'C':
{
putchar('^');
putchar('C');
putchar('\n');
fixme("No SIGINT handler yet.");
goto SecLoopEnd;
}
case 'D':
{
putchar('^');
putchar('D');
putchar('\n');
fixme("No SIGKILL handler yet.");
goto SecLoopEnd;
}
default:
continue;
}
}
if (c == '\n')
{
putchar(c);
if (strBuf.length() > 0)
{
std::string *hBuff = new std::string(strBuf.c_str());
debug("cloned strBuf(\"%s\") to hBuff(\"%s\")", strBuf.c_str(), hBuff->c_str());
history.push_back(hBuff);
hIdx = history.size();
debug("pushed \"%s\" to history; index: %d", hBuff->c_str(), hIdx);
}
break;
}
else if (seekCount >= bsCount)
{
putchar(c);
debug("BEFORE strBuf(\"%s\") %ld %ld", strBuf.c_str(), strBuf.size(), strBuf.capacity());
strBuf += c;
debug("AFTER strBuf(\"%s\") %ld %ld", strBuf.c_str(), strBuf.size(), strBuf.capacity());
seekCount = ++bsCount;
}
else
{
strBufBck();
debug("seekCount: %d; \"%s\"", seekCount, strBuf.c_str());
strBuf.insert(seekCount, (size_t)1, c);
printf("%s", strBuf.c_str());
debug("after strBuf: %s (seek and bs is +1 [seek: %d; bs: %d])",
strBuf.c_str(), seekCount + 1, bsCount + 1);
seekCount++;
bsCount++;
}
}
SecLoopEnd:
debug("strBuf.length(): %d", strBuf.length());
if (strBuf.length() == 0)
continue;
bool Found = false;
for (size_t i = 0; i < sizeof(commands) / sizeof(Command); i++)
{
if (unlikely(strncmp(strBuf.c_str(), "builtin", strBuf.length()) == 0))
{
__cmd_builtin(nullptr);
Found = true;
break;
}
if (ignoreBuiltin)
break;
std::string cmd_extracted;
for (size_t i = 0; i < strBuf.length(); i++)
{
if (strBuf[i] == ' ')
break;
cmd_extracted += strBuf[i];
}
// debug("cmd: %s, array[%d]: %s", cmd_extracted.c_str(), i, commands[i].Name);
if (strncmp(commands[i].Name, cmd_extracted.c_str(), cmd_extracted.size()) == 0)
{
if (strlen(commands[i].Name) != cmd_extracted.size())
continue;
Found = true;
std::string arg_only = "";
const char *cmd_name = commands[i].Name;
for (size_t i = strlen(cmd_name) + 1; i < strBuf.length(); i++)
arg_only += strBuf[i];
if (commands[i].Function)
commands[i].Function(arg_only.c_str());
else
{
std::string cmd_only;
for (size_t i = 0; i < strBuf.length(); i++)
{
if (strBuf[i] == ' ')
break;
cmd_only += strBuf[i];
}
printf("%s: command not implemented\n",
cmd_only.c_str());
}
break;
}
}
if (Found)
continue;
std::string cmd_only;
for (size_t i = 0; i < strBuf.length(); i++)
{
if (strBuf[i] == ' ')
break;
cmd_only += strBuf[i];
}
debug("cmd_only: %s", cmd_only.c_str());
std::string path = "/bin/";
if (fs->PathIsRelative(cmd_only.c_str()))
{
path += cmd_only;
if (!fs->PathExists(path.c_str(), nullptr))
path = "/usr/bin/" + cmd_only;
}
else
path = cmd_only;
debug("path: %s", path.c_str());
if (fs->PathExists(path.c_str(), nullptr))
{
const char *envp[5] = {
"PATH=/bin:/usr/bin",
"TERM=tty",
"HOME=/root",
"USER=root",
nullptr};
const char **argv;
if (strBuf.length() > cmd_only.length())
{
std::string arg_only;
for (size_t i = cmd_only.length() + 1; i < strBuf.length(); i++)
arg_only += strBuf[i];
argv = new const char *[3];
argv[0] = path.c_str();
argv[1] = new char[arg_only.length() + 1];
strcpy((char *)argv[1], arg_only.c_str());
argv[2] = nullptr;
debug("argv[0]: %s; argv[1]: %s", argv[0], argv[1]);
}
else
{
argv = new const char *[2];
argv[0] = path.c_str();
argv[1] = nullptr;
}
Tasking::TaskCompatibility compat = Tasking::Native;
if (Config.UseLinuxSyscalls)
compat = Tasking::Linux;
int ret = Execute::Spawn((char *)path.c_str(), argv, envp,
nullptr, false, compat, false);
if (argv[1])
delete argv[1];
delete argv;
if (ret >= 0)
{
Tasking::TCB *tcb;
Tasking::PCB *pcb;
pcb = TaskManager->GetProcessByID(ret);
if (pcb == nullptr)
{
printf("KShell: Failed to get process by ID\n");
continue;
}
pcb->SetWorkingDirectory(cwd);
tcb = TaskManager->GetThreadByID(ret, pcb);
if (tcb == nullptr)
{
printf("KShell: Failed to get thread by ID\n");
continue;
}
TaskManager->WaitForThread(tcb);
continue;
}
}
printf("%s: command not found\n",
cmd_only.c_str());
}
inf_loop;
}