//----------------------------------------------------------------------------// // GNU GPL OS/K // // // // Desc: Kernel shell // // // // // // Copyright © 2018-2019 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 . // //----------------------------------------------------------------------------// #include "shell.h" int ShAtoi(char* str) { int res = 0; for (int i = 0; str[i] != 0; ++i) res = res * 10 + str[i] - '0'; return res; } error_t CmdArgs(int argc, char **argv, char *cmdline) { int i; KernLog("cmdline: '%s'\nargc: %d\n", cmdline, argc); for (i = 0; i < argc; i++) { KernLog("argv[%d]: '%s'\n", i, argv[i]); } return EOK; } 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 CmdCpuid(int argc, char **argv, char *cmdline) { return EOK; } error_t CmdDate(int argc, char **argv, char *cmdline) { KernLog("%.10s\n", KeFormatCurTime()); return EOK; } error_t CmdDie(int argc, char **argv, char *cmdline) { *(char *)NULL += 1; return EOK; } error_t CmdDumpATASect(int argc, char **argv, char *cmdline) { char sector[513] = {0}; int sectNumber = ShAtoi(argv[1]); if (!sectNumber) { KernLog("Bad argument\n\n"); return EINVAL; } KernLog("Sector number: %d\n", sectNumber); IoReadATA(sector, 1, sectNumber); for (int i = 0; i < 513; i++) { if (isprint(sector[i])) KernLog("%c", sector[i]); else KernLog(" "); } KernLog("\n\n"); return EOK; } error_t CmdHelp(int argc, char **argv, char *cmdline) { uint i, count = 0; Command_t *cmd; if (argc == 1) { KernLog("List of all shell built-ins:\n"); for (cmd = cmdtable; 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 CmdMemUsage(int argc, char **argv, char *cmdline); error_t CmdPF(int argc, char **argv, char *cmdline) { for (int i = 0; i < 32; i++) { KernLog("%dGB ", i); *((char*)(i*GB)) = 1; } return EOK; } error_t CmdPsTest(int argc, char **argv, char *cmdline) { // pstest(); return EOK; } error_t CmdQuit(int argc, char **argv, char *cmdline) { PoShutdown(); return EOK; } error_t CmdReloadPage(int argc, char **argv, char *cmdline) { MmReloadPaging(); return EOK; } error_t CmdShell(int argc, char **argv, char *cmdline) { KeStartShell(); return EOK; } error_t CmdStackOverflow(int argc, char **argv, char *cmdline) { CmdStackOverflow(0, 0, 0); return EOK; } error_t CmdStackUnderflow(int argc, char **argv, char *cmdline) { for (int i = 0 ;; i++) { asm volatile ("pop %rax\n"); } return EOK; } error_t CmdStarWars(int argc, char **argv, char *cmdline) { IoDoStarWars(); return EOK; } error_t CmdTest(int argc, char **argv, char *cmdline) { KernLog("%s\n", &KeFormatCurTime()[13]); 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-2019 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 \n\n"); return EOK; } //----------------------------------------------------------------------------// Command_t cmdtable[] = { { "args", CmdArgs, "Print command line" }, { "beep", CmdBeep, "Make a beep" }, { "cls", CmdClear, "Clears standard output" }, { "color", CmdColor, "Change shell text color" }, { "cpuid", CmdCpuid, "Request a cpuid info." }, { "date", CmdDate, "Print date" }, { "die", CmdDie, "Die painfully" }, { "dmpsec", CmdDumpATASect, "Dump an ATA sector on screen" }, { "help", CmdHelp, "Show this message" }, { "mmap", CmdMemMap, "Show memory map" }, { "musage", CmdMemUsage, "Show memory statistics" }, { "pfault", CmdPF, "Provoke a PF. Usage: pfault
"}, { "pstest", CmdPsTest, "Scheduler test routine" }, { "exit", CmdQuit, "Initiate shutdown" }, { "quit", CmdQuit, "Alias for 'exit'" }, { "rpag", CmdReloadPage, "Reload the pages directory" }, { "shell", CmdShell, "Start a new shell (nested)", }, { "stkov", CmdStackOverflow, "Provoke a stack overflow" }, { "stkun", CmdStackUnderflow, "Provoke a stack underflow" }, { "march", CmdStarWars, "Play the Imperial March" }, { "time", CmdTime, "Print time" }, { "ver", CmdVersion, "Version and legal infos" }, { NULL, NULL, NULL } };