fix: compilation warning for maybe unused variables
feature: shell - autocomplete and help menu improved
This commit is contained in:
154
src/shell/exec.c
154
src/shell/exec.c
@ -3,49 +3,68 @@
|
||||
#include "string.h"
|
||||
#include "terminal.h"
|
||||
|
||||
#define NB_CMDS 8
|
||||
#define BORDER "==========================================================="
|
||||
#define HEADER "Welcome to bozOShell - Available Commands"
|
||||
|
||||
extern struct screen *screen;
|
||||
|
||||
static CMD_TOK find_command(char *line)
|
||||
{
|
||||
size_t i = 0;
|
||||
bool uwu = false;
|
||||
CMD_TOK command = ERROR;
|
||||
struct shell_command {
|
||||
char name[16];
|
||||
char description[256];
|
||||
void (*fn)(void);
|
||||
};
|
||||
|
||||
if (!line[0])
|
||||
return command;
|
||||
while (line[i]) {
|
||||
if (line[i] == ' ') {
|
||||
uwu = true;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
static void help(void);
|
||||
|
||||
static const struct shell_command commands[NB_CMDS] = {
|
||||
{"help", "Print this help menu", help},
|
||||
{"reboot", "Reboot the system", reboot},
|
||||
{"poweroff", "Shut down the system", halt},
|
||||
{"halt", "Stop all CPU functions", halt},
|
||||
{"stack", "Print the stack trace", print_stack},
|
||||
{"clear", "Clear the current terminal", terminal_clear},
|
||||
{"date", "Display the current time and date (UTC+0)", date},
|
||||
{"merdella", "Surprise", merdella},
|
||||
};
|
||||
|
||||
static void help(void)
|
||||
{
|
||||
const size_t padding = 15;
|
||||
|
||||
kprintf(0, "%s\n", BORDER);
|
||||
kprintf(0, " %s\n", HEADER);
|
||||
kprintf(0, "%s\n", BORDER);
|
||||
|
||||
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
|
||||
kprintf(0, " %s", commands[i].name);
|
||||
for (size_t j = 0; j < (padding - strlen(commands[i].name));
|
||||
j++)
|
||||
kprintf(0, " ");
|
||||
kprintf(0, ": %s\n", commands[i].description);
|
||||
}
|
||||
|
||||
kprintf(0, "%s\n", BORDER);
|
||||
}
|
||||
|
||||
static void auto_complete(void)
|
||||
{
|
||||
const size_t len = strlen(screen->line);
|
||||
int nb_matches = 0;
|
||||
const char *last_match;
|
||||
|
||||
for (size_t i = 0; i < NB_CMDS; i++) {
|
||||
if (!strncmp(screen->line, commands[i].name, len)) {
|
||||
nb_matches++;
|
||||
last_match = commands[i].name;
|
||||
}
|
||||
}
|
||||
if (nb_matches != 1)
|
||||
return;
|
||||
for (size_t i = len; last_match[i]; i++) {
|
||||
screen->line[i] = last_match[i];
|
||||
kprintf(0, "%c", screen->line[i]);
|
||||
}
|
||||
line[i] = '\0';
|
||||
if (!strcmp(line, "help"))
|
||||
command = HELP;
|
||||
else if (!strcmp(line, "reboot"))
|
||||
command = REBOOT;
|
||||
else if (!strcmp(line, "poweroff"))
|
||||
command = POWEROFF;
|
||||
else if (!strcmp(line, "halt"))
|
||||
command = HALT;
|
||||
else if (!strcmp(line, "stack"))
|
||||
command = STACK;
|
||||
else if (!strcmp(line, "clear"))
|
||||
command = CLEAR;
|
||||
else if (!strcmp(line, "echo"))
|
||||
command = ECHO;
|
||||
else if (!strcmp(line, "color"))
|
||||
command = COLOR;
|
||||
else if (!strcmp(line, "merdella"))
|
||||
command = MERDELLA;
|
||||
else if (!strcmp(line, "date"))
|
||||
command = DATE;
|
||||
else
|
||||
kprintf(0, "invalid command: %s\n", line);
|
||||
if (uwu)
|
||||
line[i] = ' ';
|
||||
return command;
|
||||
}
|
||||
|
||||
static void read_line(void)
|
||||
@ -67,8 +86,9 @@ static void read_line(void)
|
||||
move_cursor(LEFT);
|
||||
terminal_putchar(' ');
|
||||
move_cursor(LEFT);
|
||||
}
|
||||
if (ev.c && i < size - 1) {
|
||||
} else if (ev.scan_code == KEY_TAB && i) {
|
||||
auto_complete();
|
||||
} else if (ev.c && i < size - 1) {
|
||||
kprintf(0, "%c", ev.c);
|
||||
buf[i++] = ev.c;
|
||||
}
|
||||
@ -86,50 +106,16 @@ void shell_init(void)
|
||||
while (1) {
|
||||
kprintf(0, PROMPT);
|
||||
read_line();
|
||||
switch (find_command(screen->line)) {
|
||||
case HELP:
|
||||
kprintf(0, "Welcome to bozOShell, the shell of "
|
||||
"bozOS\nAvailable commands: help, reboot, "
|
||||
"poweroff, echo, color, merdella\n");
|
||||
break;
|
||||
case REBOOT:
|
||||
reboot();
|
||||
break;
|
||||
case POWEROFF:
|
||||
kprintf(0, "powering off\n");
|
||||
break;
|
||||
case HALT:
|
||||
halt();
|
||||
break;
|
||||
case STACK:
|
||||
print_stack();
|
||||
break;
|
||||
case CLEAR:
|
||||
terminal_clear();
|
||||
break;
|
||||
case ECHO:
|
||||
kprintf(0, "echoing\n");
|
||||
break;
|
||||
case COLOR:
|
||||
kprintf(0, "coloring\n");
|
||||
break;
|
||||
case MERDELLA: {
|
||||
kprintf(0, POOP);
|
||||
if (!strcmp("merdella --credits", screen->line))
|
||||
kprintf(
|
||||
0,
|
||||
"\nThis ascii masterpiece has been created "
|
||||
"by Targon (/)\n");
|
||||
break;
|
||||
}
|
||||
case DATE:
|
||||
date();
|
||||
break;
|
||||
case ERROR:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
bool invalid = true;
|
||||
for (int i = 0; i < NB_CMDS; i++) {
|
||||
if (!strcmp(commands[i].name, screen->line)) {
|
||||
commands[i].fn();
|
||||
invalid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (invalid && screen->line[0])
|
||||
kprintf(0, "invalid command: %s\n", screen->line);
|
||||
memset(screen->line, '\0', sizeof(screen->line));
|
||||
}
|
||||
}
|
||||
|
7
src/shell/misc_cmds.c
Normal file
7
src/shell/misc_cmds.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include "kprintf.h"
|
||||
#include "shell.h"
|
||||
|
||||
void merdella(void)
|
||||
{
|
||||
kprintf(0, POOP);
|
||||
}
|
Reference in New Issue
Block a user