fix: compilation warning for maybe unused variables

feature: shell - autocomplete and help menu improved
This commit is contained in:
0x35c 2024-09-19 00:05:21 +02:00
parent 9b3a6cb5a4
commit 8fd17276b2
10 changed files with 84 additions and 109 deletions

View File

@ -2,7 +2,7 @@
#include <stdint.h> #include <stdint.h>
static const char *keymap[128] = { [[__maybe_unused__]] static const char *keymap[128] = {
[2] = "1!", [3] = "2@", [4] = "3#", [5] = "4$", [6] = "5%", [2] = "1!", [3] = "2@", [4] = "3#", [5] = "4$", [6] = "5%",
[7] = "6^", [8] = "7&", [9] = "8*", [10] = "9(", [11] = "0)", [7] = "6^", [8] = "7&", [9] = "8*", [10] = "9(", [11] = "0)",
[12] = "-_", [13] = "=+", [16] = "qQ", [17] = "wW", [18] = "eE", [12] = "-_", [13] = "=+", [16] = "qQ", [17] = "wW", [18] = "eE",

View File

@ -2,7 +2,7 @@
#define PROMPT "> " #define PROMPT "> "
static const char *POOP = [[__maybe_unused__]] static const char *POOP =
" / ____/ / _ \\\n" " / ____/ / _ \\\n"
" _/ ___/_ / / \\___ \\_\n" " _/ ___/_ / / \\___ \\_\n"
" / _/'-, `---._ / / \\_ \\\n" " / _/'-, `---._ / / \\_ \\\n"
@ -43,22 +43,9 @@ static const char *POOP =
" \\_ \\___\"\"\"/\"\" / `\"\"/\"\" " " \\_ \\___\"\"\"/\"\" / `\"\"/\"\" "
"\n"; "\n";
typedef enum {
HELP,
REBOOT,
POWEROFF,
HALT,
STACK,
CLEAR,
ECHO,
COLOR,
MERDELLA,
DATE,
ERROR
} CMD_TOK;
void shell_init(void); void shell_init(void);
void reboot(void); void reboot(void);
void halt(void); void halt(void);
void print_stack(void); void print_stack(void);
void date(void); void date(void);
void merdella(void);

View File

@ -50,4 +50,3 @@ void terminal_clear(void);
struct key_event terminal_getkey(void); struct key_event terminal_getkey(void);
void update_cursor(void); void update_cursor(void);
void move_cursor(int direction); void move_cursor(int direction);
struct screen *get_screen(void);

View File

@ -15,4 +15,5 @@
mov ss, ax mov ss, ax
jmp 0x08:.flush // 0x08 is the offset to our code segment: far jump on it jmp 0x08:.flush // 0x08 is the offset to our code segment: far jump on it
.flush: .flush:
ret ret

View File

@ -31,7 +31,7 @@ uint8_t bcd_mode_to_bin(uint8_t value)
struct rtc_date get_date(void) struct rtc_date get_date(void)
{ {
struct rtc_date rv; struct rtc_date rv = {};
uint8_t century; uint8_t century;
rv.second = read_register(SECOND_REGISTER); rv.second = read_register(SECOND_REGISTER);
@ -55,4 +55,4 @@ struct rtc_date get_date(void)
} }
rv.year += century * 100 - 98; // -108 = bozo offset rv.year += century * 100 - 98; // -108 = bozo offset
return rv; return rv;
} }

View File

View File

View File

@ -3,49 +3,68 @@
#include "string.h" #include "string.h"
#include "terminal.h" #include "terminal.h"
#define NB_CMDS 8
#define BORDER "==========================================================="
#define HEADER "Welcome to bozOShell - Available Commands"
extern struct screen *screen; extern struct screen *screen;
static CMD_TOK find_command(char *line) struct shell_command {
{ char name[16];
size_t i = 0; char description[256];
bool uwu = false; void (*fn)(void);
CMD_TOK command = ERROR; };
if (!line[0]) static void help(void);
return command;
while (line[i]) { static const struct shell_command commands[NB_CMDS] = {
if (line[i] == ' ') { {"help", "Print this help menu", help},
uwu = true; {"reboot", "Reboot the system", reboot},
break; {"poweroff", "Shut down the system", halt},
} {"halt", "Stop all CPU functions", halt},
i++; {"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) static void read_line(void)
@ -67,8 +86,9 @@ static void read_line(void)
move_cursor(LEFT); move_cursor(LEFT);
terminal_putchar(' '); terminal_putchar(' ');
move_cursor(LEFT); move_cursor(LEFT);
} } else if (ev.scan_code == KEY_TAB && i) {
if (ev.c && i < size - 1) { auto_complete();
} else if (ev.c && i < size - 1) {
kprintf(0, "%c", ev.c); kprintf(0, "%c", ev.c);
buf[i++] = ev.c; buf[i++] = ev.c;
} }
@ -86,50 +106,16 @@ void shell_init(void)
while (1) { while (1) {
kprintf(0, PROMPT); kprintf(0, PROMPT);
read_line(); read_line();
switch (find_command(screen->line)) { bool invalid = true;
case HELP: for (int i = 0; i < NB_CMDS; i++) {
kprintf(0, "Welcome to bozOShell, the shell of " if (!strcmp(commands[i].name, screen->line)) {
"bozOS\nAvailable commands: help, reboot, " commands[i].fn();
"poweroff, echo, color, merdella\n"); invalid = false;
break; 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;
} }
if (invalid && screen->line[0])
kprintf(0, "invalid command: %s\n", screen->line);
memset(screen->line, '\0', sizeof(screen->line)); memset(screen->line, '\0', sizeof(screen->line));
} }
} }

7
src/shell/misc_cmds.c Normal file
View File

@ -0,0 +1,7 @@
#include "kprintf.h"
#include "shell.h"
void merdella(void)
{
kprintf(0, POOP);
}

View File

@ -183,8 +183,3 @@ void move_cursor(int direction)
outb(0x3D4, 0x0E); outb(0x3D4, 0x0E);
outb(0x3D5, (pos >> 8) & 0xFF); outb(0x3D5, (pos >> 8) & 0xFF);
} }
struct screen *get_screen(void)
{
return screen;
}