os-k/kaleid/kernel/sh/shcmds.c

304 lines
8.7 KiB
C

//----------------------------------------------------------------------------//
// OS on Kaleid //
// //
// Desc: Kernel shell //
// //
// //
// Copyright © 2018-2020 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K 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 //
// any later version. //
// //
// OS/K 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 OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <vers.h>
#include <mm/paging.h>
#include <mm/map.h>
#include <drivers/ata.h>
#include <io/vga.h>
#include <io/spkr.h>
#include <ke/time.h>
#include <libbuf.h>
#include <sh/shell.h>
#include <po/shtdwn.h>
error_t CmdTest(int argc, char **argv, char *cmdline);
error_t CmdMemUsage(int argc, char **argv, char *cmdline);
error_t CmdBeep(int argc, char **argv, char *cmdline)
{
IoDoBeep();
return EOK;
}
error_t CmdClear(int argc, char **argv, char *cmdline)
{
BLockBuf(BStdOut);
BStdOut->wp = BStdOut->rp = BStdOut->buf;
BStdOut->lastLF = 0;
BUnlockBuf(BStdOut);
return EOK;
}
error_t CmdColor(int argc, char **argv, char *cmdline)
{
int col = 0;
char *p;
if (argc > 2) {
KernLog("Usage: 'color [#|list]'\n");
return EINVAL;
}
if (argc == 1) {
KernLog("Current color: %s (%d)\n", RtlColorNames[shcol], shcol);
}
else if (!strcmp(argv[1], "list")) {
KernLog("Available colors:\n");
for (col = 0; col <= VGA_COLOR_WHITE; col++) {
KernLog("%d - %s\n", col, RtlColorNames[col]);
}
}
else {
p = argv[1];
while (isdigit(*p) && col < VGA_COLOR_WHITE) {
col = 10 * col + *p++ - '0';
}
if (*p || col < VGA_COLOR_BLACK || col > VGA_COLOR_WHITE) {
KernLog("Invalid color: %d\n", col);
KernLog("Usage: 'color [#|list]'\n");
return EINVAL;
}
else {
shcol = col;
}
}
return EOK;
}
error_t CmdDate(int argc, char **argv, char *cmdline)
{
KernLog("%.10s\n", KeFormatCurTime());
return EOK;
}
error_t CmdDmesg(int argc, char **argv, char *cmdline)
{
char *ptr;
size_t N = 0;
if (argc == 1) N = 999;
else if (argc == 2) N = 1 + atoi(argv[1]);
else {
KernLog("dmesg: no more than one argument\n");
return EINVAL;
}
DebugLog("dmesg requested from kernel shell (N=%d)\n", N);
if (N == 0) return EOK;
BLockBuf(BStdDbg);
ptr = (char *)lmax((ulong)BStdDbg->buf,
(ulong)(BStdDbg->wp - BStdDbg->lastLF - (BStdDbg->lineLen * N)));
KernLog(ptr);
BUnlockBuf(BStdDbg);
return EOK;
}
error_t CmdDumpMem(int argc, char **argv, char *cmdline)
{
if (argc < 2 || argc > 3) {
KernLog("Invalid argument : argc = %d\n", argc);
return EINVAL;
}
char sector[8192] = {0};
int maximum = 0;
int x = 0;
int step = 8;
char *address = (char*)strtoul(argv[1], NULL, 16);
char *end = (char*)(strtoul(argv[1], NULL, 16) + step);
if (argc >= 3) {
end = (char*)(strtoul(argv[2], NULL, 16) + step);
}
maximum = (int)(end - address);
if (address > end) {
KernLog("Invalid argument\n");
return EINVAL;
}
//KernLog("Address begin: %p\tAddress end: %p\n", address, end);
for (int i = 0; i < 8192 && i < maximum; i++) {
sector[i] = *(address+i);
}
for (x = 0; x < maximum; x += step) {
KernLog("%C", shcol);
KernLog("[%p] ", (ulong)address + (ulong)x);
for (int i = 0; i < step; i++) {
KernLog("%02x ", (uchar)sector[i+x]);
}
KernLog(" %C ", VGA_COLOR_LIGHT_BLUE);
for (int i = 0; i < step; i++) {
if (isprint(sector[i+x]))
KernLog("%c",
sector[i+x]
);
else
KernLog("%c", 0);
}
KernLog("\n");
}
return EOK;
}
error_t CmdHelp(int argc, char **argv, char *cmdline)
{
uint i, count = 0;
Command_t *cmd;
KernLog("List of all shell built-ins:\n");
for (cmd = shcmdtable; cmd->name != NULL; cmd++, count++) {
KernLog("\t%s", cmd->name);
for (i = strlen(cmd->name)/4; i<3; i++) {
KernLog("\t");
}
KernLog("%C%s%C\n", VGA_COLOR_DARK_GREY, cmd->help, shcol);
}
KernLog("End of list; %u commands total\n", count);
return EOK;
}
error_t CmdMemMap(int argc, char **argv, char *cmdline)
{
MmPrintMemoryMap();
return EOK;
}
error_t CmdPrompt(int argc, char **argv, char *cmdline)
{
if (cmdline[6] == 0) {
shprompt[0] = 0;
shpromptlen = 0;
}
else {
strnzcpy(shprompt, &cmdline[7], SHPROMPT_MAXLEN);
shpromptlen = strlen(shprompt);
}
return EOK;
}
error_t CmdQuit(int argc, char **argv, char *cmdline)
{
PoShutdown();
return EOK;
}
error_t CmdSleep(int argc, char **argv, char *cmdline)
{
int delay = atoi(argv[1]);
KeSleep(delay);
return EOK;
}
error_t CmdStarWars(int argc, char **argv, char *cmdline)
{
IoDoStarWars();
return EOK;
}
error_t CmdTime(int argc, char **argv, char *cmdline)
{
KernLog("%s\n", &KeFormatCurTime()[13]);
return EOK;
}
error_t CmdVersion(int argc, char **argv, char *cmdline)
{
int CH = VGA_COLOR_LIGHT_BLUE;
int CN = shcol;
KernLog("OS/K version %C%s (x86-64)%C\n",
CH,
_KALEID_VERSION,
CN
);
KernLog("Copyright (C) 2018-2020 The OS/K Team\n\n");
KernLog("This program is free software, released under the\n");
KernLog("terms of the GNU GPL version 3 or later as published\n");
KernLog("by the Free Software Foundation.\n");
KernLog("You are free to change and redistribute it.\n");
KernLog("There is NO WARRANTY, to the extent permitted by law.\n");
KernLog("See <http://gnu.org/licenses/gpl.html>\n\n");
return EOK;
}
//----------------------------------------------------------------------------//
Command_t shcmdtable[] =
{
{ "beep", CmdBeep, "Make a beep" },
{ "cls", CmdClear, "Clears standard output" },
{ "color", CmdColor, "Change shell text color" },
{ "date", CmdDate, "Print date" },
{ "dmesg", CmdDmesg, "Print N lines of debug log" },
{ "exit", CmdQuit, "Initiate shutdown" },
{ "help", CmdHelp, "Show this message" },
{ "march", CmdStarWars, "Play the Imperial March" },
{ "memdump", CmdDumpMem, "Dump memory starting from addr to end"},
{ "mmap", CmdMemMap, "Show memory map" },
{ "musage", CmdMemUsage, "Show memory statistics" },
{ "prompt", CmdPrompt, "Change shell prompt" },
{ "quit", CmdQuit, "Alias for 'exit'" },
{ "sleep", CmdSleep, "Sleep N ms" },
{ "time", CmdTime, "Print time" },
{ "test", CmdTest, "Launch a test" },
{ "ver", CmdVersion, "Version and legal infos" },
{ NULL, NULL, NULL },
};