luxos/Kernel/debug/console.c

118 lines
3.0 KiB
C
Raw Permalink Normal View History

#include <debugio.h>
#include <stdlib.h>
#include <ctype.h>
#include "commands.c"
void _process_command (string params[16], int32 count);
2021-09-14 15:53:27 +00:00
int32 _command_does_not_exist(string command);
void ConsoleMain()
{
char buffer[512];
string params[CONSOLE_MAX_PARAMS];
CommandOsver();
for (;;)
{
// Read a string
ConsoleWrite("\n%#] ", Color(ColorBlack, ColorYellow));
ConsoleReadString(buffer, 512, '\n');
// Split buffer in params
params[0] = buffer;
int32 i=0, len = strlen(buffer), param_count = 0;
while (i < len && param_count < CONSOLE_MAX_PARAMS)
{
// Skip spaces before
while (i < len && isspace(buffer[i])) buffer[i++] = 0;
if (i == len) break;
params[param_count++] = &buffer[i];
// Skip non-spaces
while (i < len && !isspace(buffer[i])) i++;
}
// Send command to processing
_process_command(params, param_count);
}
}
void _process_command (string params[CONSOLE_MAX_PARAMS], int32 count)
{
int32 Cmd = -1;
if (count == 0)
{
ConsoleWrite ("%#! You must enter a command!\n", Color(0, ColorLightRed));
return;
}
// Lookup command in list
int32 i;
for (i = 0; i < ConsoleCommandsCount && Cmd == -1; i++)
if (strcmp(params[0], ConsoleCommands[i]) == 0) Cmd = i;
2021-09-14 15:53:27 +00:00
loop:
switch (Cmd)
{
2021-09-14 15:53:27 +00:00
case -1: Cmd = _command_does_not_exist(params[0]);
if (Cmd != -1) goto loop;
break;
case 0: CommandOsver(); break;
case 1: CommandTime(); break;
case 2: ConsoleClear(); break;
case 3: CommandHelp(params, count); break;
case 4: CommandDump (params, count); break;
case 5: CommandMem(params, count); break;
case 6: CommandCrash(); break;
case 7: CommandMouse(); break;
case 8: CommandRead(params, count); break;
case 9:
case 10: SystemReboot(); break;
case 11: CommandDir(params, count); break;
case 12: CommandCat(params, count); break;
2021-09-14 16:02:20 +00:00
case 13: CommandFork(); break;
default: ConsoleWrite ("%#! Command %#%s%# was not implemented (yet)!\n",
Color(0,ColorLightRed), Color(0,ColorWhite), params[0], Color(0,ColorLightRed)); break;
}
}
2021-09-14 15:53:27 +00:00
int32 _command_does_not_exist(string command)
{
2021-09-14 15:53:27 +00:00
// Try finding a command that starts with strlen(command) letters
int32 count, i, last;
for (i = 0, count = 0; i < ConsoleCommandsCount; i++)
if (!strncmp(command, ConsoleCommands[i], strlen(command))) { ++count; last = i; }
// Found a good command
if (count == 1) {
ConsoleWrite ("%#Did you mean %#%s%#?\n", ColorYellow, ColorWhite, ConsoleCommands[last], ColorYellow);
return last;
}
// Shorten if too long
if (strlen(command) > 20)
{
command[18] = command[19] = command[20] = '.';
command[21] = null;
}
2021-09-14 15:53:27 +00:00
// Display error
ConsoleWrite ("%#! Command %#%s%# does not exist!\n", Color(0,ColorLightRed), Color(0,ColorWhite), command, Color(0,ColorLightRed));
if (count > 0)
{
ConsoleWrite ("Available options:\n");
for (i = 0; i < ConsoleCommandsCount; i++)
if (!strncmp(command, ConsoleCommands[i], strlen(command))) ConsoleWrite ("\t>%# %s\n", ColorYellow, ConsoleCommands[i]);
}
2021-09-14 15:53:27 +00:00
return -1;
}