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

164 lines
4.9 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-29 14:34:37 +02:00
#include <io/vga.h>
#include <io/keyb.h>
#include <io/spkr.h>
#include <lib/buf.h>
#include <sh/argv.h>
#include <sh/shell.h>
#include <po/shtdwn.h>
2019-05-18 22:02:16 +02:00
2019-05-08 17:20:27 +02:00
int shargc = 0;
char **shargv = 0;
2019-05-29 14:34:37 +02:00
int shcol = VGA_COLOR_LIGHT_GREY;
static char *argvbuf = 0;
2019-05-08 17:20:27 +02:00
2019-11-25 17:22:54 +01:00
void ExecuteCommand(char *cmdbuf, Command_t *cmdtable)
2019-05-08 15:09:16 +02:00
{
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);
2019-05-29 14:34:37 +02:00
rc = ShCmdLineToArgVec(cmdbuf, &shargc, argvbuf);
2019-05-08 15:09:16 +02:00
if (rc) KeStartPanic("Shell: Couldn't parse command line: %d", rc);
2019-11-25 17:22:54 +01:00
for (cmd = cmdtable; cmd->name != NULL; cmd++) {
2019-05-08 15:09:16 +02:00
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;
}
}
2019-05-18 22:53:57 +02:00
assert(shargv[0] == argvbuf + ARG_MAX);
2019-05-08 15:09:16 +02:00
if (found == false) {
2019-05-18 22:53:57 +02:00
KernLog("err: command not found: '%.255s' (%ld)\n",
2019-05-08 17:20:27 +02:00
shargv[0], strlen(shargv[0]));
2019-05-08 15:09:16 +02:00
}
}
2019-05-08 00:28:42 +02:00
2019-05-29 14:34:37 +02:00
void ShStartShell(void)
2019-05-08 00:28:42 +02:00
{
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-18 22:53:57 +02:00
argvbuf = malloc(ARG_MAX * 2);
memzero(argvbuf, ARG_MAX * 2);
shargv = (char **)argvbuf;
2019-05-08 15:09:16 +02:00
2019-05-18 22:58:30 +02:00
KernLog("\n%Cshell> %C", VGA_COLOR_WHITE, shcol);
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;
2019-11-25 17:22:54 +01:00
ExecuteCommand(cmdbuf, shcmdtable);
2019-05-08 15:09:16 +02:00
2019-05-13 20:34:41 +02:00
memzero(cmdbuf, CMDBUFSIZE);
2019-05-08 15:09:16 +02:00
BFlushBuf(BStdIn);
2019-05-18 22:58:30 +02:00
KernLog("%Cshell> %C", VGA_COLOR_WHITE, shcol);
2019-05-08 15:09:16 +02:00
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-18 19:09:46 +02:00
if (rand() % 16 == 0) {
2019-05-08 00:28:42 +02:00
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:
2019-05-24 11:16:59 +02:00
while (IoGetScroll() > 0) {
IoScrollDown();
}
2019-05-08 15:09:16 +02:00
*bufptr++ = (char)ch;
// End of buffer?
if (bufptr != cmdbuf+CMDBUFSIZE-1) {
break; // No
}
KernLog("\n");
// Else, fallthrough to case '\n'
2019-05-24 11:16:59 +02:00
2019-05-08 15:09:16 +02:00
case '\n':
2019-05-20 20:28:18 +02:00
while (IoGetScroll() > 0) {
IoScrollDown();
}
2019-05-24 11:16:59 +02:00
*bufptr = 0;
bufptr = cmdbuf;
2019-11-25 17:22:54 +01:00
ExecuteCommand(cmdbuf, shcmdtable);
2019-05-24 11:16:59 +02:00
2019-05-13 20:34:41 +02:00
memzero(cmdbuf, CMDBUFSIZE);
2019-05-18 22:58:30 +02:00
KernLog("%Cshell> %C", VGA_COLOR_WHITE, shcol);
2019-05-08 15:09:16 +02:00
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
}