2024-09-10 14:03:33 -04:00
|
|
|
#include "kprintf.h"
|
|
|
|
#include "shell.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "terminal.h"
|
|
|
|
|
|
|
|
extern struct screen *screen;
|
|
|
|
|
|
|
|
static CMD_TOK find_command(char *line)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
bool uwu = false;
|
|
|
|
CMD_TOK command = ERROR;
|
|
|
|
|
|
|
|
if (!line[0])
|
|
|
|
return command;
|
|
|
|
while (line[i]) {
|
|
|
|
if (line[i] == ' ') {
|
|
|
|
uwu = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
line[i] = '\0';
|
|
|
|
if (!strcmp(line, "help"))
|
|
|
|
command = HELP;
|
|
|
|
else if (!strcmp(line, "reboot"))
|
|
|
|
command = REBOOT;
|
|
|
|
else if (!strcmp(line, "poweroff"))
|
|
|
|
command = POWEROFF;
|
2024-09-11 11:43:18 -04:00
|
|
|
else if (!strcmp(line, "halt"))
|
|
|
|
command = HALT;
|
2024-09-17 05:10:41 -04:00
|
|
|
else if (!strcmp(line, "stack"))
|
|
|
|
command = STACK;
|
2024-09-17 07:57:52 -04:00
|
|
|
else if (!strcmp(line, "clear"))
|
|
|
|
command = CLEAR;
|
2024-09-10 14:03:33 -04:00
|
|
|
else if (!strcmp(line, "echo"))
|
|
|
|
command = ECHO;
|
|
|
|
else if (!strcmp(line, "color"))
|
|
|
|
command = COLOR;
|
|
|
|
else if (!strcmp(line, "merdella"))
|
|
|
|
command = MERDELLA;
|
2024-09-18 15:45:18 -04:00
|
|
|
else if (!strcmp(line, "date"))
|
|
|
|
command = DATE;
|
2024-09-10 14:03:33 -04:00
|
|
|
else
|
|
|
|
kprintf(0, "invalid command: %s\n", line);
|
|
|
|
if (uwu)
|
|
|
|
line[i] = ' ';
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2024-09-11 09:51:40 -04:00
|
|
|
static void read_line(void)
|
2024-09-10 14:03:33 -04:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
struct key_event ev;
|
|
|
|
uint8_t prev_scan_code = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ev = terminal_getkey();
|
|
|
|
if (ev.c == '\n')
|
|
|
|
break;
|
2024-09-11 09:51:40 -04:00
|
|
|
char *buf = screen->line;
|
|
|
|
i = strlen(screen->line);
|
|
|
|
const size_t size = sizeof(screen->line);
|
2024-09-10 14:03:33 -04:00
|
|
|
if (prev_scan_code != ev.scan_code && ev.scan_code) {
|
|
|
|
if (ev.scan_code == KEY_BACKSPACE && i) {
|
|
|
|
buf[--i] = '\0';
|
|
|
|
move_cursor(LEFT);
|
|
|
|
terminal_putchar(' ');
|
|
|
|
move_cursor(LEFT);
|
|
|
|
}
|
|
|
|
if (ev.c && i < size - 1) {
|
|
|
|
kprintf(0, "%c", ev.c);
|
|
|
|
buf[i++] = ev.c;
|
|
|
|
}
|
|
|
|
if (i >= size)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev_scan_code = ev.scan_code;
|
|
|
|
}
|
|
|
|
kprintf(0, "\n");
|
2024-09-11 09:51:40 -04:00
|
|
|
screen->line[i] = '\0';
|
2024-09-10 14:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void shell_init(void)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
kprintf(0, PROMPT);
|
2024-09-11 09:51:40 -04:00
|
|
|
read_line();
|
2024-09-10 14:03:33 -04:00
|
|
|
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:
|
2024-09-11 10:46:39 -04:00
|
|
|
reboot();
|
2024-09-10 14:03:33 -04:00
|
|
|
break;
|
|
|
|
case POWEROFF:
|
|
|
|
kprintf(0, "powering off\n");
|
|
|
|
break;
|
2024-09-11 11:43:18 -04:00
|
|
|
case HALT:
|
|
|
|
halt();
|
|
|
|
break;
|
2024-09-17 05:10:41 -04:00
|
|
|
case STACK:
|
|
|
|
print_stack();
|
|
|
|
break;
|
2024-09-17 07:57:52 -04:00
|
|
|
case CLEAR:
|
|
|
|
terminal_clear();
|
|
|
|
break;
|
2024-09-10 14:03:33 -04:00
|
|
|
case ECHO:
|
|
|
|
kprintf(0, "echoing\n");
|
|
|
|
break;
|
|
|
|
case COLOR:
|
|
|
|
kprintf(0, "coloring\n");
|
|
|
|
break;
|
|
|
|
case MERDELLA: {
|
|
|
|
kprintf(0, POOP);
|
2024-09-11 10:25:23 -04:00
|
|
|
if (!strcmp("merdella --credits", screen->line))
|
2024-09-10 14:03:33 -04:00
|
|
|
kprintf(
|
|
|
|
0,
|
|
|
|
"\nThis ascii masterpiece has been created "
|
|
|
|
"by Targon (/)\n");
|
|
|
|
break;
|
|
|
|
}
|
2024-09-18 15:45:18 -04:00
|
|
|
case DATE:
|
|
|
|
date();
|
|
|
|
break;
|
2024-09-10 14:03:33 -04:00
|
|
|
case ERROR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2024-09-11 09:51:40 -04:00
|
|
|
memset(screen->line, '\0', sizeof(screen->line));
|
2024-09-10 14:03:33 -04:00
|
|
|
}
|
|
|
|
}
|