2024-09-25 16:25:35 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2024-09-21 19:57:27 -04:00
|
|
|
#include "alloc.h"
|
2024-09-25 15:31:44 -04:00
|
|
|
#include "commands.h"
|
2024-10-09 14:26:44 -04:00
|
|
|
#include "drivers.h"
|
2024-09-10 14:03:33 -04:00
|
|
|
#include "kprintf.h"
|
|
|
|
#include "shell.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "terminal.h"
|
2024-09-25 16:58:25 -04:00
|
|
|
#include "utils.h"
|
2024-09-10 14:03:33 -04:00
|
|
|
|
2024-09-25 15:31:44 -04:00
|
|
|
#define BORDER "==========================================================="
|
|
|
|
#define HEADER "Welcome to bozOShell - Available Commands"
|
2024-09-10 14:03:33 -04:00
|
|
|
|
2024-09-18 18:05:21 -04:00
|
|
|
struct shell_command {
|
|
|
|
char name[16];
|
|
|
|
char description[256];
|
2024-09-25 15:31:44 -04:00
|
|
|
void (*fn)(char *arg);
|
2024-09-18 18:05:21 -04:00
|
|
|
};
|
|
|
|
|
2024-09-25 16:58:25 -04:00
|
|
|
const struct shell_command cmds[] = {
|
|
|
|
{"help", "Print this help menu", help_cmd},
|
|
|
|
{"reboot", "Reboot the system", reboot_cmd},
|
|
|
|
{"poweroff", "Shut down the system", poweroff_cmd},
|
|
|
|
{"halt", "Stop all CPU functions", halt_cmd},
|
|
|
|
{"stack", "Print the stack trace", stack_cmd},
|
|
|
|
{"heap", "Print the heaps", heap_cmd},
|
|
|
|
{"clear", "Clear the current terminal", clear_cmd},
|
|
|
|
{"date", "Display the current time and date", date_cmd},
|
|
|
|
{"merdella", "Surprise", merdella_cmd},
|
|
|
|
{"color", "Change the screen color", color_cmd},
|
2024-09-18 18:05:21 -04:00
|
|
|
};
|
|
|
|
|
2024-09-25 16:58:25 -04:00
|
|
|
#define NB_CMDS ARRAY_SIZE(cmds)
|
2024-09-25 15:31:44 -04:00
|
|
|
|
|
|
|
extern struct screen *screen;
|
2024-10-09 14:26:44 -04:00
|
|
|
extern int line_status;
|
2024-09-25 15:31:44 -04:00
|
|
|
|
|
|
|
void help_cmd(char *arg)
|
2024-09-10 14:03:33 -04:00
|
|
|
{
|
2024-09-18 18:05:21 -04:00
|
|
|
const size_t padding = 15;
|
2024-09-10 14:03:33 -04:00
|
|
|
|
2024-09-25 15:31:44 -04:00
|
|
|
(void)arg;
|
2024-09-20 06:40:36 -04:00
|
|
|
kprintf("%s\n", BORDER);
|
|
|
|
kprintf(" %s\n", HEADER);
|
|
|
|
kprintf("%s\n", BORDER);
|
2024-09-18 18:05:21 -04:00
|
|
|
|
2024-09-21 19:57:27 -04:00
|
|
|
for (size_t i = 0; i < NB_CMDS; i++) {
|
2024-09-25 16:58:25 -04:00
|
|
|
kprintf(" %s", cmds[i].name);
|
|
|
|
for (size_t j = 0; j < (padding - strlen(cmds[i].name)); j++)
|
2024-09-20 06:40:36 -04:00
|
|
|
kprintf(" ");
|
2024-09-25 16:58:25 -04:00
|
|
|
kprintf(": %s\n", cmds[i].description);
|
2024-09-18 18:05:21 -04:00
|
|
|
}
|
|
|
|
|
2024-09-20 06:40:36 -04:00
|
|
|
kprintf("%s\n", BORDER);
|
2024-09-18 18:05:21 -04:00
|
|
|
}
|
|
|
|
|
2024-10-09 14:26:44 -04:00
|
|
|
void auto_complete(void)
|
2024-09-18 18:05:21 -04:00
|
|
|
{
|
|
|
|
const size_t len = strlen(screen->line);
|
|
|
|
int nb_matches = 0;
|
|
|
|
const char *last_match;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < NB_CMDS; i++) {
|
2024-09-25 16:58:25 -04:00
|
|
|
if (!strncmp(screen->line, cmds[i].name, len)) {
|
2024-09-18 18:05:21 -04:00
|
|
|
nb_matches++;
|
2024-09-25 16:58:25 -04:00
|
|
|
last_match = cmds[i].name;
|
2024-09-10 14:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
2024-09-18 18:05:21 -04:00
|
|
|
if (nb_matches != 1)
|
|
|
|
return;
|
|
|
|
for (size_t i = len; last_match[i]; i++) {
|
|
|
|
screen->line[i] = last_match[i];
|
2024-09-20 06:40:36 -04:00
|
|
|
kprintf("%c", screen->line[i]);
|
2024-09-18 18:05:21 -04:00
|
|
|
}
|
2024-09-10 14:03:33 -04:00
|
|
|
}
|
|
|
|
|
2024-10-09 14:26:44 -04:00
|
|
|
void shell_init(void)
|
2024-09-10 14:03:33 -04:00
|
|
|
{
|
2024-10-09 16:10:22 -04:00
|
|
|
char *line;
|
2024-10-09 14:26:44 -04:00
|
|
|
kprintf(PROMPT);
|
2024-10-09 16:10:22 -04:00
|
|
|
while ((line = get_line())) {
|
2024-09-18 18:05:21 -04:00
|
|
|
bool invalid = true;
|
2024-09-21 19:57:27 -04:00
|
|
|
for (unsigned i = 0; i < NB_CMDS; i++) {
|
2024-09-25 16:58:25 -04:00
|
|
|
if (!strncmp(cmds[i].name, screen->line,
|
|
|
|
strlen(cmds[i].name))) {
|
|
|
|
char *args = strchr(screen->line, ' ');
|
|
|
|
if (args)
|
|
|
|
args++; // skip the space
|
|
|
|
cmds[i].fn(args);
|
2024-09-18 18:05:21 -04:00
|
|
|
invalid = false;
|
|
|
|
break;
|
|
|
|
}
|
2024-09-10 14:03:33 -04:00
|
|
|
}
|
2024-09-18 18:05:21 -04:00
|
|
|
if (invalid && screen->line[0])
|
2024-09-21 06:17:27 -04:00
|
|
|
kprintf(KERN_WARNING "invalid command: %s\n",
|
|
|
|
screen->line);
|
2024-09-11 09:51:40 -04:00
|
|
|
memset(screen->line, '\0', sizeof(screen->line));
|
2024-10-09 14:26:44 -04:00
|
|
|
kprintf(PROMPT);
|
2024-09-10 14:03:33 -04:00
|
|
|
}
|
|
|
|
}
|