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

145 lines
4.4 KiB
C
Raw Normal View History

2019-05-08 00:28:42 +02:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
2019-05-08 15:09:16 +02:00
// Desc: Kernel shell //
2019-05-08 00:28:42 +02:00
// //
// //
// 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 <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
2019-05-08 15:09:16 +02:00
#include "shell.h"
2019-05-08 00:28:42 +02:00
2019-05-08 17:20:27 +02:00
int shargc = 0;
char *argv0 = 0;
char **shargv = 0;
2019-05-08 15:09:16 +02:00
void ExecuteCommand(char *cmdbuf)
{
error_t rc;
Command_t *cmd;
bool found = false;
2019-05-08 20:45:28 +02:00
if (!cmdbuf || !*cmdbuf)
return;
2019-05-08 15:09:16 +02:00
memzero(*shargv, ARG_MAX);
rc = KalCmdLineToArgVec(cmdbuf, &shargc, shargv);
if (rc) KeStartPanic("Shell: Couldn't parse command line: %d", rc);
for (cmd = cmdtable; cmd->name != NULL; cmd++) {
if (!strcmp(cmd->name, shargv[0])) {
2019-05-08 17:20:27 +02:00
cmd->func(shargc, shargv, cmdbuf);
2019-05-08 15:09:16 +02:00
found = true;
break;
}
}
if (found == false) {
2019-05-08 17:20:27 +02:00
KernLog("err: command not found: '%s' (%ld)\n",
shargv[0], strlen(shargv[0]));
2019-05-08 15:09:16 +02:00
}
}
2019-05-08 00:28:42 +02:00
void KeStartShell(void)
{
uchar ch;
error_t rc;
2019-05-08 15:09:16 +02:00
2019-05-13 20:34:41 +02:00
char *cmdbuf = malloc(CMDBUFSIZE);
2019-05-08 15:09:16 +02:00
char *bufptr = cmdbuf;
2019-05-08 00:28:42 +02:00
2019-05-08 15:09:16 +02:00
argv0 = malloc(ARG_MAX);
2019-05-08 17:20:27 +02:00
memzero(argv0, ARG_MAX);
2019-05-08 15:09:16 +02:00
shargv = &argv0;
2019-05-09 11:33:07 +02:00
KernLog("\nshell> ");
2019-05-08 01:01:53 +02:00
BFlushBuf(BStdOut);
2019-05-08 17:20:27 +02:00
while ((rc = BGetFromBuf(BStdIn, &ch)) == EOK || rc == EENDF) {
2019-05-08 15:09:16 +02:00
// Reset BStdIn's content
if (rc == EENDF) {
if (!(BStdIn->flags & BF_ERR)) {
*bufptr = 0;
bufptr = cmdbuf;
ExecuteCommand(cmdbuf);
2019-05-13 20:34:41 +02:00
memzero(cmdbuf, CMDBUFSIZE);
2019-05-08 15:09:16 +02:00
BFlushBuf(BStdIn);
KernLog("shell> ");
BFlushBuf(BStdOut);
}
else break;
}
2019-05-08 00:28:42 +02:00
switch (ch) {
2019-05-08 20:23:52 +02:00
case KEY_BS:
*bufptr = 0;
if (bufptr > cmdbuf) {
bufptr--;
}
break;
2019-05-08 10:33:17 +02:00
case KEY_BEL:
2019-05-08 00:28:42 +02:00
if (rand() % 64 == 0) {
IoDoStarWars();
}
else IoDoBeep();
break;
2019-05-08 10:33:17 +02:00
case KEY_DC1:
2019-05-08 00:28:42 +02:00
IoScrollUp();
break;
2019-05-08 10:33:17 +02:00
case KEY_DC2:
2019-05-08 00:28:42 +02:00
IoScrollDown();
break;
2019-05-08 10:33:17 +02:00
case KEY_ESC:
2019-05-09 10:27:44 +02:00
PoShutdown();
2019-05-08 00:28:42 +02:00
break;
2019-05-08 15:09:16 +02:00
default:
*bufptr++ = (char)ch;
// End of buffer?
if (bufptr != cmdbuf+CMDBUFSIZE-1) {
break; // No
}
KernLog("\n");
// Else, fallthrough to case '\n'
case '\n':
*bufptr = 0;
bufptr = cmdbuf;
ExecuteCommand(cmdbuf);
2019-05-13 20:34:41 +02:00
memzero(cmdbuf, CMDBUFSIZE);
2019-05-08 15:09:16 +02:00
KernLog("shell> ");
BFlushBuf(BStdIn);
2019-05-08 10:33:17 +02:00
BFlushBuf(BStdOut);
break;
2019-05-08 00:28:42 +02:00
}
2019-05-08 15:09:16 +02:00
2019-05-08 10:33:17 +02:00
KePauseCPU();
2019-05-08 00:28:42 +02:00
}
KernLog("[EOI]\n");
2019-05-08 00:28:42 +02:00
}