|
|
@@ -1,11 +1,14 @@
|
|
|
-/* Terminal: prompt, datetime (top right), blinking cursor, echo loop. */
|
|
|
+/* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch. */
|
|
|
|
|
|
#include "io.h"
|
|
|
#include "rtc.h"
|
|
|
#include "terminal.h"
|
|
|
#include "timer.h"
|
|
|
|
|
|
+extern void program_hello_main(void);
|
|
|
+
|
|
|
#define DATETIME_MS 1000
|
|
|
+#define LINE_MAX 64
|
|
|
static unsigned int last_datetime_tick;
|
|
|
static char datetime_buf[IO_DATETIME_LEN + 1];
|
|
|
|
|
|
@@ -45,17 +48,51 @@ static void update_cursor_blink(void)
|
|
|
io_cursor_hide_block();
|
|
|
}
|
|
|
|
|
|
-static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
|
|
|
+static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
|
|
|
+#define BANNER_LEN 12
|
|
|
static const char prompt[] __attribute__((section(".text"))) = "\n> ";
|
|
|
+static const char prompt_first[] __attribute__((section(".text"))) = "> ";
|
|
|
+
|
|
|
+/* Draw row 0: banner left, datetime right. Call when terminal is active. */
|
|
|
+static void draw_header_row(void)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+ for (i = 0; i < BANNER_LEN && banner[i]; i++)
|
|
|
+ io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
|
|
|
+ for (; i < IO_DATETIME_POS; i++)
|
|
|
+ io_vga_write(i, (unsigned short)(0x0A << 8));
|
|
|
+ redraw_datetime();
|
|
|
+}
|
|
|
+
|
|
|
+static int is_cmd_hello(const char *line, unsigned int line_len)
|
|
|
+{
|
|
|
+ unsigned int i = 0;
|
|
|
+ while (i < line_len && line[i] == ' ')
|
|
|
+ i++;
|
|
|
+ if (i + 5 > line_len)
|
|
|
+ return 0;
|
|
|
+ if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
|
|
|
+ return 0;
|
|
|
+ i += 5;
|
|
|
+ while (i < line_len && line[i] == ' ')
|
|
|
+ i++;
|
|
|
+ return (i == line_len);
|
|
|
+}
|
|
|
|
|
|
void terminal_run(void)
|
|
|
{
|
|
|
+ static char line[LINE_MAX];
|
|
|
+ static unsigned int line_len;
|
|
|
+
|
|
|
last_cursor_tick = get_ticks_ms();
|
|
|
last_datetime_tick = get_ticks_ms();
|
|
|
cursor_visible = 1;
|
|
|
+ line_len = 0;
|
|
|
+ line[0] = '\0';
|
|
|
|
|
|
- io_putstr(msg);
|
|
|
- io_putstr(prompt);
|
|
|
+ io_drain_keyboard();
|
|
|
+ draw_header_row();
|
|
|
+ io_putstr(prompt_first);
|
|
|
|
|
|
for (;;) {
|
|
|
update_datetime();
|
|
|
@@ -63,11 +100,33 @@ void terminal_run(void)
|
|
|
{
|
|
|
char c = io_poll_key();
|
|
|
if (c) {
|
|
|
- io_putchar(c);
|
|
|
- if (c == '\n') {
|
|
|
+ if (c == '\b') {
|
|
|
+ if (line_len > 0) {
|
|
|
+ line_len--;
|
|
|
+ line[line_len] = '\0';
|
|
|
+ io_putchar(c);
|
|
|
+ }
|
|
|
+ } else if (c == '\n') {
|
|
|
+ io_putchar(c);
|
|
|
+ if (is_cmd_hello(line, line_len)) {
|
|
|
+ program_hello_main();
|
|
|
+ io_drain_keyboard();
|
|
|
+ draw_header_row();
|
|
|
+ }
|
|
|
+ line_len = 0;
|
|
|
+ line[0] = '\0';
|
|
|
io_putstr(prompt);
|
|
|
if (io_did_scroll())
|
|
|
redraw_datetime();
|
|
|
+ } else {
|
|
|
+ {
|
|
|
+ unsigned char u = (unsigned char)c;
|
|
|
+ if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
|
|
|
+ line[line_len++] = c;
|
|
|
+ line[line_len] = '\0';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ io_putchar(c);
|
|
|
}
|
|
|
} else {
|
|
|
__asm__ volatile ("hlt");
|