2019-05-08 00:28:42 +02:00
|
|
|
//----------------------------------------------------------------------------//
|
2020-09-27 17:33:48 +02:00
|
|
|
// OS on Kaleid //
|
2019-05-08 00:28:42 +02:00
|
|
|
// //
|
2019-05-08 15:09:16 +02:00
|
|
|
// Desc: Kernel shell //
|
2019-05-08 00:28:42 +02:00
|
|
|
// //
|
|
|
|
// //
|
2020-02-06 14:23:26 +01:00
|
|
|
// Copyright © 2018-2020 The OS/K Team //
|
2019-05-08 00:28:42 +02:00
|
|
|
// //
|
|
|
|
// 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>
|
2020-02-19 22:19:58 +01:00
|
|
|
#include <libbuf.h>
|
2019-05-29 14:34:37 +02:00
|
|
|
#include <sh/argv.h>
|
|
|
|
#include <sh/shell.h>
|
|
|
|
#include <po/shtdwn.h>
|
2020-02-02 13:37:26 +01:00
|
|
|
#include <io/cursor.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;
|
|
|
|
|
2020-02-06 15:01:37 +01:00
|
|
|
char *shprompt = "shell> ";
|
|
|
|
size_t shpromptlen = 7;
|
|
|
|
|
2019-05-29 14:34:37 +02:00
|
|
|
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
|
|
|
|
2020-02-06 15:01:37 +01:00
|
|
|
#define CMDBUFSIZE (BStdOut->lineLen - shpromptlen - 2)
|
2020-02-02 13:37:26 +01: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;
|
2020-02-02 13:37:26 +01:00
|
|
|
char *ptr; // temp
|
|
|
|
|
|
|
|
// Current line - beginning & current pos
|
2019-05-13 20:34:41 +02:00
|
|
|
char *cmdbuf = malloc(CMDBUFSIZE);
|
2019-05-08 15:09:16 +02:00
|
|
|
char *bufptr = cmdbuf;
|
2020-02-02 13:37:26 +01:00
|
|
|
char *bufend = cmdbuf;
|
|
|
|
|
|
|
|
// History...
|
|
|
|
// We use a basic N-entries command history
|
2020-02-06 15:01:37 +01:00
|
|
|
#define N 10 // must be >1
|
2020-02-02 13:37:26 +01:00
|
|
|
char *history = malloc(CMDBUFSIZE * N);
|
|
|
|
int historyIndex = 0;
|
|
|
|
int historyScroll = 0;
|
|
|
|
|
|
|
|
bool insertMode = 0;
|
2020-02-06 15:01:37 +01:00
|
|
|
size_t nLines;
|
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
|
|
|
|
2020-02-06 15:01:37 +01:00
|
|
|
shprompt = malloc(SHPROMPT_MAXLEN);
|
|
|
|
strcpy(shprompt, "shell> ");
|
|
|
|
shpromptlen = 7;
|
|
|
|
|
|
|
|
KernLog("\n%C%s%C", VGA_COLOR_WHITE, shprompt, 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);
|
2020-02-06 15:01:37 +01:00
|
|
|
KernLog("%C%s%C", VGA_COLOR_WHITE, shprompt, shcol);
|
2019-05-08 15:09:16 +02:00
|
|
|
BFlushBuf(BStdOut);
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2019-05-08 00:28:42 +02:00
|
|
|
switch (ch) {
|
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
// Backspace character
|
|
|
|
case K_BACKSPACE:
|
|
|
|
if (bufptr == cmdbuf)
|
|
|
|
break;
|
2019-05-08 20:23:52 +02:00
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
memmove(bufptr-1, bufptr, bufend-bufptr+1);
|
|
|
|
bufptr--;
|
|
|
|
bufend--;
|
|
|
|
goto print_current_line;
|
|
|
|
|
|
|
|
// DEL character
|
|
|
|
case K_DEL:
|
|
|
|
if (bufptr == bufend)
|
|
|
|
break;
|
|
|
|
|
|
|
|
memmove(bufptr, bufptr+1, bufend-bufptr+1);
|
|
|
|
IoSetCursorOffset(IoGetCursorOffset()+1);
|
|
|
|
bufend--;
|
|
|
|
goto print_current_line;
|
|
|
|
|
|
|
|
case K_BELL: IoDoBeep(); break;
|
|
|
|
case K_ESCAPE: PoShutdown(); break;
|
|
|
|
case K_PAGE_UP: IoScrollUp(); break;
|
|
|
|
case K_PAGE_DOWN: IoScrollDown(); break;
|
|
|
|
|
|
|
|
case K_INSERT:
|
|
|
|
insertMode = 1 - insertMode;
|
2019-05-08 00:28:42 +02:00
|
|
|
break;
|
2020-02-02 13:37:26 +01:00
|
|
|
|
|
|
|
// Move within buffer
|
|
|
|
case K_ARRW_LEFT:
|
|
|
|
if (!(bufptr > cmdbuf))
|
|
|
|
break;
|
2019-05-08 00:28:42 +02:00
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
bufptr--;
|
|
|
|
|
|
|
|
IoSetCursorOffset(IoGetCursorOffset()-1);
|
|
|
|
BFlushBuf(BStdOut);
|
2019-05-08 00:28:42 +02:00
|
|
|
break;
|
2020-02-02 13:37:26 +01:00
|
|
|
|
|
|
|
case K_ARRW_RIGHT:
|
|
|
|
if (bufptr == bufend)
|
|
|
|
break;
|
2019-05-08 00:28:42 +02:00
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
bufptr++;
|
|
|
|
IoSetCursorOffset(IoGetCursorOffset()+1);
|
|
|
|
BFlushBuf(BStdOut);
|
2019-05-08 00:28:42 +02:00
|
|
|
break;
|
2020-02-02 13:37:26 +01:00
|
|
|
|
|
|
|
case K_HOME:
|
|
|
|
if (bufptr == cmdbuf)
|
|
|
|
break;
|
2019-05-08 00:28:42 +02:00
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
IoSetCursorOffset(IoGetCursorOffset()-(int)(bufptr-cmdbuf));
|
|
|
|
bufptr = cmdbuf;
|
|
|
|
BFlushBuf(BStdOut);
|
2019-05-08 00:28:42 +02:00
|
|
|
break;
|
2019-05-08 15:09:16 +02:00
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
case K_END:
|
|
|
|
if (bufptr == bufend)
|
|
|
|
break;
|
|
|
|
|
|
|
|
IoSetCursorOffset(IoGetCursorOffset()+(int)(bufend-bufptr));
|
|
|
|
bufptr = bufend;
|
|
|
|
BFlushBuf(BStdOut);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Handle history scroll up/down
|
|
|
|
case K_ARRW_UP:
|
|
|
|
case K_ARRW_DOWN:
|
|
|
|
if (historyIndex == 0)
|
|
|
|
break; // No history yet
|
|
|
|
|
|
|
|
if (ch == K_ARRW_UP) {
|
|
|
|
memmove(cmdbuf, &history[historyScroll*CMDBUFSIZE], CMDBUFSIZE);
|
|
|
|
if (historyScroll > 0)
|
|
|
|
historyScroll--;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (historyScroll < historyIndex-1) {
|
|
|
|
historyScroll++;
|
|
|
|
memmove(cmdbuf, &history[historyScroll*CMDBUFSIZE], CMDBUFSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (bufend = cmdbuf; *bufend && bufend < cmdbuf+CMDBUFSIZE; bufend++);
|
|
|
|
bufptr = bufend;
|
|
|
|
|
|
|
|
IoSetCursorOffset(0);
|
|
|
|
goto print_current_line;
|
|
|
|
|
|
|
|
// Regular character
|
2019-05-08 15:09:16 +02:00
|
|
|
default:
|
2020-02-02 13:37:26 +01:00
|
|
|
if (bufptr >= bufend) {
|
|
|
|
*bufptr++ = (char)ch;
|
|
|
|
|
|
|
|
bufend = bufptr;
|
|
|
|
*bufptr = 0;
|
|
|
|
|
|
|
|
// Advance cursor
|
|
|
|
if (IoGetCursorOffset() < 0)
|
|
|
|
IoSetCursorOffset(IoGetCursorOffset() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
if (!insertMode) {
|
|
|
|
*++bufend = 0;
|
|
|
|
for (ptr = bufend-1; ptr != bufptr; ptr--)
|
|
|
|
*ptr = *(ptr - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
else { // insertMode
|
|
|
|
// Advance cursor
|
|
|
|
if (IoGetCursorOffset() < 0)
|
|
|
|
IoSetCursorOffset(IoGetCursorOffset() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
*bufptr++ = (char)ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_current_line:
|
2019-05-24 11:16:59 +02:00
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
BLockBuf(BStdOut);
|
|
|
|
|
|
|
|
BStdOut->wp -= BStdOut->lastLF;
|
|
|
|
BStdOut->size -= BStdOut->lastLF;
|
|
|
|
BStdOut->lastLF = 0;
|
2020-02-06 15:01:37 +01:00
|
|
|
bprintf(BStdOut, "%C%s%C%s",
|
|
|
|
VGA_COLOR_WHITE, shprompt, shcol, cmdbuf);
|
2020-02-02 13:37:26 +01:00
|
|
|
|
|
|
|
BUnlockBuf(BStdOut);
|
|
|
|
|
|
|
|
IoSetScroll(1);
|
|
|
|
IoScrollDown(); // This calls the flusher
|
2019-05-08 15:09:16 +02:00
|
|
|
|
|
|
|
// End of buffer?
|
2020-02-02 13:37:26 +01:00
|
|
|
if (bufend != cmdbuf+CMDBUFSIZE-1) {
|
2019-05-08 15:09:16 +02:00
|
|
|
break; // No
|
|
|
|
}
|
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
// Else, *FALLTHROUGH* to case '\n'
|
2019-05-24 11:16:59 +02:00
|
|
|
|
2019-05-08 15:09:16 +02:00
|
|
|
case '\n':
|
2020-02-02 13:37:26 +01:00
|
|
|
BPutOnBuf(BStdOut, '\n');
|
|
|
|
|
2019-05-20 20:28:18 +02:00
|
|
|
while (IoGetScroll() > 0) {
|
|
|
|
IoScrollDown();
|
|
|
|
}
|
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
IoSetCursorOffset(0);
|
|
|
|
|
|
|
|
bufptr = cmdbuf; // Reset bufptr
|
|
|
|
bufend = cmdbuf;
|
|
|
|
|
|
|
|
if (cmdbuf[0] && !isspace(cmdbuf[0])) {
|
|
|
|
// Forget old commands
|
|
|
|
if (historyIndex >= N) {
|
|
|
|
assert(historyIndex == N);
|
|
|
|
memmove(history, history+CMDBUFSIZE, CMDBUFSIZE * (N-1));
|
|
|
|
historyIndex = historyScroll = N-1;
|
|
|
|
|
|
|
|
}
|
|
|
|
// Update history
|
|
|
|
memmove(&history[historyIndex*CMDBUFSIZE], cmdbuf, CMDBUFSIZE);
|
|
|
|
historyScroll = historyIndex;
|
|
|
|
historyIndex++;
|
|
|
|
|
|
|
|
// Execute & reset
|
|
|
|
ExecuteCommand(cmdbuf, shcmdtable);
|
|
|
|
memzero(cmdbuf, CMDBUFSIZE);
|
|
|
|
}
|
2019-05-24 11:16:59 +02:00
|
|
|
|
2020-02-06 15:01:37 +01:00
|
|
|
KernLog("%C%s%C", VGA_COLOR_WHITE, shprompt, 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
|
|
|
}
|
|
|
|
}
|