2020-06-05 21:27:52 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <menu.h>
|
|
|
|
#include <lib/print.h>
|
|
|
|
#include <lib/blib.h>
|
|
|
|
#include <lib/libc.h>
|
|
|
|
#include <lib/config.h>
|
2020-09-02 10:55:56 +03:00
|
|
|
#include <lib/term.h>
|
2021-04-09 02:26:39 +03:00
|
|
|
#include <lib/gterm.h>
|
2020-10-27 10:09:27 +03:00
|
|
|
#include <lib/readline.h>
|
2020-11-02 11:20:34 +03:00
|
|
|
#include <lib/uri.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-09-24 22:48:49 +03:00
|
|
|
#include <drivers/vbe.h>
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2020-12-19 00:38:18 +03:00
|
|
|
static char *menu_branding = NULL;
|
2021-09-06 08:31:57 +03:00
|
|
|
static char *menu_branding_colour = NULL;
|
2020-11-18 02:13:54 +03:00
|
|
|
|
|
|
|
#define EDITOR_MAX_BUFFER_SIZE 4096
|
2021-07-07 02:27:07 +03:00
|
|
|
#define TOK_KEY 0
|
|
|
|
#define TOK_EQUALS 1
|
|
|
|
#define TOK_VALUE 2
|
|
|
|
#define TOK_BADKEY 3
|
2021-07-08 18:28:43 +03:00
|
|
|
#define TOK_COMMENT 4
|
2020-11-18 02:13:54 +03:00
|
|
|
|
|
|
|
static size_t get_line_offset(size_t *displacement, size_t index, const char *buffer) {
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t _index = index;
|
|
|
|
for (size_t i = 0; buffer[i]; i++) {
|
|
|
|
if (!_index--)
|
|
|
|
break;
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
offset = i + 1;
|
|
|
|
}
|
|
|
|
if (displacement)
|
|
|
|
*displacement = index - offset;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_line_length(size_t index, const char *buffer) {
|
|
|
|
size_t i;
|
|
|
|
for (i = index; buffer[i] != '\n' && buffer[i] != 0; i++);
|
|
|
|
return i - index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_next_line(size_t index, const char *buffer) {
|
|
|
|
if (buffer[index] == 0)
|
|
|
|
return index;
|
|
|
|
size_t displacement;
|
|
|
|
get_line_offset(&displacement, index, buffer);
|
2020-12-19 00:38:18 +03:00
|
|
|
while (buffer[index] != '\n') {
|
|
|
|
if (buffer[index] == 0)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
index++;
|
2020-11-18 02:13:54 +03:00
|
|
|
size_t next_line_length = get_line_length(index, buffer);
|
|
|
|
if (displacement > next_line_length)
|
|
|
|
displacement = next_line_length;
|
|
|
|
return index + displacement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_prev_line(size_t index, const char *buffer) {
|
|
|
|
size_t offset, displacement, prev_line_offset, prev_line_length;
|
|
|
|
offset = get_line_offset(&displacement, index, buffer);
|
|
|
|
if (offset) {
|
|
|
|
prev_line_offset = get_line_offset(NULL, offset - 1, buffer);
|
|
|
|
prev_line_length = get_line_length(prev_line_offset, buffer);
|
|
|
|
if (displacement > prev_line_length)
|
|
|
|
displacement = prev_line_length;
|
|
|
|
return prev_line_offset + displacement;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
static const char *VALID_KEYS[] = {
|
|
|
|
"TIMEOUT",
|
|
|
|
"DEFAULT_ENTRY",
|
|
|
|
"GRAPHICS",
|
|
|
|
"MENU_RESOLUTION",
|
|
|
|
"MENU_BRANDING",
|
2021-09-06 08:31:57 +03:00
|
|
|
"MENU_BRANDING_COLOUR",
|
|
|
|
"MENU_BRANDING_COLOR",
|
2021-07-07 02:27:07 +03:00
|
|
|
"MENU_FONT",
|
2021-08-18 15:06:55 +03:00
|
|
|
"MENU_FONT_SIZE",
|
2021-08-18 22:02:03 +03:00
|
|
|
"MENU_FONT_SCALE",
|
2021-08-20 20:25:42 +03:00
|
|
|
"MENU_FONT_SPACING",
|
2021-07-07 02:27:07 +03:00
|
|
|
"TERMINAL_FONT",
|
2021-08-18 22:02:03 +03:00
|
|
|
"TERMINAL_FONT_SIZE",
|
|
|
|
"TERMINAL_FONT_SCALE",
|
2021-08-20 20:25:42 +03:00
|
|
|
"TERMINAL_FONT_SPACING",
|
2021-07-07 02:27:07 +03:00
|
|
|
"THEME_COLOURS",
|
|
|
|
"THEME_COLORS",
|
2021-08-17 01:58:02 +03:00
|
|
|
"THEME_BRIGHT_COLOURS",
|
|
|
|
"THEME_BRIGHT_COLORS",
|
2021-07-07 02:27:07 +03:00
|
|
|
"THEME_BACKGROUND",
|
|
|
|
"THEME_FOREGROUND",
|
|
|
|
"THEME_MARGIN",
|
|
|
|
"THEME_MARGIN_GRADIENT",
|
|
|
|
"BACKGROUND_PATH",
|
|
|
|
"BACKGROUND_STYLE",
|
|
|
|
"BACKDROP_COLOUR",
|
|
|
|
"BACKDROP_COLOR",
|
|
|
|
"EDITOR_ENABLED",
|
|
|
|
"EDITOR_HIGHLIGHTING",
|
|
|
|
"EDITOR_VALIDATION",
|
|
|
|
"VERBOSE",
|
2021-08-17 01:58:02 +03:00
|
|
|
"RANDOMISE_MEMORY",
|
|
|
|
"RANDOMIZE_MEMORY",
|
|
|
|
"COMMENT",
|
2021-07-07 02:27:07 +03:00
|
|
|
"PROTOCOL",
|
|
|
|
"CMDLINE",
|
|
|
|
"KERNEL_CMDLINE",
|
|
|
|
"KERNEL_PATH",
|
|
|
|
"MODULE_PATH",
|
|
|
|
"MODULE_STRING",
|
|
|
|
"RESOLUTION",
|
2021-08-17 01:58:02 +03:00
|
|
|
"TEXTMODE",
|
2021-07-07 02:27:07 +03:00
|
|
|
"KASLR",
|
|
|
|
"DRIVE",
|
|
|
|
"PARTITION",
|
|
|
|
"IMAGE_PATH",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool validation_enabled = true;
|
|
|
|
static bool invalid_syntax = false;
|
|
|
|
|
|
|
|
static int validate_line(const char *buffer) {
|
|
|
|
if (!validation_enabled) return TOK_KEY;
|
2021-07-08 18:28:43 +03:00
|
|
|
if (buffer[0] == '#')
|
|
|
|
return TOK_COMMENT;
|
2021-07-07 02:27:07 +03:00
|
|
|
char keybuf[64];
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t i;
|
2021-07-07 02:27:07 +03:00
|
|
|
for (i = 0; buffer[i] && i < 64; i++) {
|
|
|
|
if (buffer[i] == '=') goto found_equals;
|
|
|
|
keybuf[i] = buffer[i];
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
if (i < 64) keybuf[i] = 0;
|
|
|
|
if (keybuf[0] == '\n' || (!keybuf[0] && buffer[0] != '=')) return TOK_KEY; // blank line is valid
|
2021-07-07 02:56:01 +03:00
|
|
|
invalid_syntax = true;
|
2021-07-07 02:27:07 +03:00
|
|
|
return TOK_BADKEY;
|
|
|
|
found_equals:
|
|
|
|
if (i < 64) keybuf[i] = 0;
|
|
|
|
for (i = 0; VALID_KEYS[i]; i++) {
|
|
|
|
if (!strcmp(keybuf, VALID_KEYS[i])) {
|
|
|
|
return TOK_KEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *config_entry_editor(const char *title, const char *orig_entry) {
|
2020-12-19 19:10:28 +03:00
|
|
|
size_t cursor_offset = 0;
|
|
|
|
size_t entry_size = strlen(orig_entry);
|
2021-07-07 05:23:34 +03:00
|
|
|
size_t _window_size = term_rows - 8;
|
2020-12-19 19:10:28 +03:00
|
|
|
size_t window_offset = 0;
|
|
|
|
size_t line_size = term_cols - 2;
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2020-12-19 20:28:11 +03:00
|
|
|
bool display_overflow_error = false;
|
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
// Skip leading newlines
|
|
|
|
while (*orig_entry == '\n') {
|
|
|
|
orig_entry++;
|
|
|
|
entry_size--;
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
if (entry_size >= EDITOR_MAX_BUFFER_SIZE) {
|
2020-11-18 02:13:54 +03:00
|
|
|
panic("Entry is too big to be edited.");
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
bool syntax_highlighting_enabled = true;
|
|
|
|
char *syntax_highlighting_enabled_config = config_get_value(NULL, 0, "EDITOR_HIGHLIGHTING");
|
|
|
|
if (!strcmp(syntax_highlighting_enabled_config, "no")) syntax_highlighting_enabled = false;
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
validation_enabled = true;
|
|
|
|
char *validation_enabled_config = config_get_value(NULL, 0, "EDITOR_VALIDATION");
|
|
|
|
if (!strcmp(validation_enabled_config, "no")) validation_enabled = false;
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-04-20 07:45:03 +03:00
|
|
|
static char *buffer = NULL;
|
|
|
|
if (buffer == NULL)
|
|
|
|
buffer = ext_mem_alloc(EDITOR_MAX_BUFFER_SIZE);
|
2020-11-18 02:13:54 +03:00
|
|
|
memcpy(buffer, orig_entry, entry_size);
|
|
|
|
buffer[entry_size] = 0;
|
|
|
|
|
|
|
|
refresh:
|
2021-07-07 02:56:01 +03:00
|
|
|
invalid_syntax = false;
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
clear(true);
|
|
|
|
disable_cursor();
|
2021-07-07 05:23:34 +03:00
|
|
|
{
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n");
|
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
set_cursor_pos(term_cols / 2 - DIV_ROUNDUP(strlen(menu_branding), 2), y);
|
2021-09-06 08:31:57 +03:00
|
|
|
print("\e[3%sm%s\e[37m", menu_branding_colour, menu_branding);
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n\n");
|
|
|
|
}
|
2021-07-07 02:27:07 +03:00
|
|
|
|
2021-07-07 05:23:34 +03:00
|
|
|
print(" \e[32mESC\e[0m Discard and Exit \e[32mF10\e[0m Boot\n");
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2020-12-19 00:38:18 +03:00
|
|
|
print("\n\xda");
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t i = 0; i < term_cols - 2; i++) {
|
2020-12-19 01:01:03 +03:00
|
|
|
switch (i) {
|
|
|
|
case 1: case 2: case 3:
|
|
|
|
if (window_offset > 0) {
|
|
|
|
print("\x18");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FALLTHRU
|
2021-07-07 02:56:01 +03:00
|
|
|
default: {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t title_length = strlen(title);
|
2021-07-15 13:51:46 +03:00
|
|
|
if (i == (term_cols / 2) - DIV_ROUNDUP(title_length, 2) - 1) {
|
2021-07-07 02:56:01 +03:00
|
|
|
print("%s", title);
|
|
|
|
i += title_length - 1;
|
2021-07-07 02:27:07 +03:00
|
|
|
} else {
|
|
|
|
print("\xc4");
|
|
|
|
}
|
2021-07-07 02:56:01 +03:00
|
|
|
}
|
2020-12-19 01:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 00:38:18 +03:00
|
|
|
print("\xbf\xb3");
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t cursor_x, cursor_y;
|
2020-12-19 19:10:28 +03:00
|
|
|
size_t current_line = 0, line_offset = 0, window_size = _window_size;
|
2020-12-19 00:38:18 +03:00
|
|
|
bool printed_cursor = false;
|
2021-07-07 02:27:07 +03:00
|
|
|
int token_type = validate_line(buffer);
|
2020-11-18 02:13:54 +03:00
|
|
|
for (size_t i = 0; ; i++) {
|
2021-07-07 02:27:07 +03:00
|
|
|
// newline
|
2020-12-19 00:38:18 +03:00
|
|
|
if (buffer[i] == '\n'
|
|
|
|
&& current_line < window_offset + window_size
|
|
|
|
&& current_line >= window_offset) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2020-12-19 00:38:18 +03:00
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
if (i == cursor_offset) {
|
|
|
|
cursor_x = x;
|
|
|
|
cursor_y = y;
|
|
|
|
printed_cursor = true;
|
|
|
|
}
|
|
|
|
set_cursor_pos(term_cols - 1, y);
|
|
|
|
if (current_line == window_offset + window_size - 1)
|
|
|
|
print("\xb3\xc0");
|
|
|
|
else
|
|
|
|
print("\xb3\xb3");
|
2020-12-19 19:10:28 +03:00
|
|
|
line_offset = 0;
|
2021-07-07 02:27:07 +03:00
|
|
|
token_type = validate_line(buffer+i+1);
|
2020-12-19 00:38:18 +03:00
|
|
|
current_line++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-19 19:10:28 +03:00
|
|
|
if (line_offset && !(line_offset % line_size)) {
|
|
|
|
window_size--;
|
|
|
|
if (current_line == window_offset + window_size)
|
|
|
|
print("\x1a\xc0");
|
|
|
|
else
|
|
|
|
print("\x1a\x1b\x1b");
|
|
|
|
}
|
|
|
|
|
2020-12-19 00:38:18 +03:00
|
|
|
if (i == cursor_offset
|
|
|
|
&& current_line < window_offset + window_size
|
|
|
|
&& current_line >= window_offset) {
|
2020-11-18 02:13:54 +03:00
|
|
|
get_cursor_pos(&cursor_x, &cursor_y);
|
2020-12-19 00:38:18 +03:00
|
|
|
printed_cursor = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == 0 || current_line >= window_offset + window_size) {
|
|
|
|
if (!printed_cursor) {
|
|
|
|
if (i <= cursor_offset) {
|
|
|
|
window_offset++;
|
|
|
|
goto refresh;
|
|
|
|
}
|
|
|
|
if (i > cursor_offset) {
|
|
|
|
window_offset--;
|
|
|
|
goto refresh;
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
break;
|
2020-12-19 00:38:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == '\n') {
|
2020-12-19 19:10:28 +03:00
|
|
|
line_offset = 0;
|
2020-12-19 00:38:18 +03:00
|
|
|
current_line++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-19 19:10:28 +03:00
|
|
|
if (current_line >= window_offset) {
|
|
|
|
line_offset++;
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
// switch to token type 1 if equals sign
|
|
|
|
if (token_type == TOK_KEY && buffer[i] == '=') token_type = TOK_EQUALS;
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
// syntax highlighting
|
|
|
|
if (syntax_highlighting_enabled) {
|
|
|
|
switch (token_type) {
|
2021-07-07 02:31:39 +03:00
|
|
|
case TOK_KEY:
|
|
|
|
print("\e[36m%c\e[0m", buffer[i]);
|
|
|
|
break;
|
|
|
|
case TOK_EQUALS:
|
|
|
|
print("\e[32m%c\e[0m", buffer[i]);
|
|
|
|
break;
|
2021-07-08 18:28:43 +03:00
|
|
|
case TOK_VALUE:
|
|
|
|
print("\e[39m%c\e[0m", buffer[i]);
|
|
|
|
break;
|
|
|
|
case TOK_BADKEY:
|
|
|
|
print("\e[31m%c\e[0m", buffer[i]);
|
|
|
|
break;
|
|
|
|
case TOK_COMMENT:
|
|
|
|
print("\e[33m%c\e[0m", buffer[i]);
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
2021-07-07 02:31:39 +03:00
|
|
|
} else {
|
|
|
|
print("%c", buffer[i]);
|
|
|
|
}
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:35:21 +03:00
|
|
|
// switch to token type 2 after equals sign
|
|
|
|
if (token_type == TOK_EQUALS) token_type = TOK_VALUE;
|
2020-12-19 19:10:28 +03:00
|
|
|
}
|
2020-12-19 00:38:18 +03:00
|
|
|
}
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
// syntax error alert
|
|
|
|
if (validation_enabled) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2021-07-07 02:27:07 +03:00
|
|
|
get_cursor_pos(&x, &y);
|
2021-07-07 05:23:34 +03:00
|
|
|
set_cursor_pos(0, term_rows-1);
|
2021-07-11 03:01:03 +03:00
|
|
|
scroll_disable();
|
2021-07-07 02:27:07 +03:00
|
|
|
if (invalid_syntax) {
|
|
|
|
print("\e[31mConfiguration is INVALID.\e[0m");
|
|
|
|
} else {
|
|
|
|
print("\e[32mConfiguration is valid.\e[0m");
|
|
|
|
}
|
2021-07-11 03:01:03 +03:00
|
|
|
scroll_enable();
|
2021-07-07 02:27:07 +03:00
|
|
|
set_cursor_pos(x, y);
|
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2020-12-19 00:38:18 +03:00
|
|
|
if (current_line - window_offset < window_size) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2020-12-19 00:38:18 +03:00
|
|
|
for (size_t i = 0; i < (window_size - (current_line - window_offset)) - 1; i++) {
|
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
set_cursor_pos(term_cols - 1, y);
|
|
|
|
print("\xb3\xb3");
|
|
|
|
}
|
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
set_cursor_pos(term_cols - 1, y);
|
|
|
|
print("\xb3\xc0");
|
2020-11-18 02:13:54 +03:00
|
|
|
}
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t i = 0; i < term_cols - 2; i++) {
|
2020-12-19 01:01:03 +03:00
|
|
|
switch (i) {
|
|
|
|
case 1: case 2: case 3:
|
|
|
|
if (current_line - window_offset >= window_size) {
|
|
|
|
print("\x19");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FALLTHRU
|
|
|
|
default:
|
|
|
|
print("\xc4");
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 00:38:18 +03:00
|
|
|
print("\xd9");
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2020-12-19 20:28:11 +03:00
|
|
|
if (display_overflow_error) {
|
2021-07-11 03:01:03 +03:00
|
|
|
scroll_disable();
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\e[31mText buffer not big enough, delete something instead.");
|
2021-07-11 03:01:03 +03:00
|
|
|
scroll_enable();
|
2020-12-19 20:28:11 +03:00
|
|
|
display_overflow_error = false;
|
|
|
|
}
|
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
// Hack to redraw the cursor
|
|
|
|
set_cursor_pos(cursor_x, cursor_y);
|
|
|
|
enable_cursor();
|
|
|
|
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer_flush();
|
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
int c = getchar();
|
|
|
|
switch (c) {
|
|
|
|
case GETCHAR_CURSOR_DOWN:
|
|
|
|
cursor_offset = get_next_line(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_UP:
|
|
|
|
cursor_offset = get_prev_line(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_LEFT:
|
|
|
|
if (cursor_offset) {
|
|
|
|
cursor_offset--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_RIGHT:
|
|
|
|
if (cursor_offset < strlen(buffer)) {
|
|
|
|
cursor_offset++;
|
|
|
|
}
|
|
|
|
break;
|
2020-12-19 21:27:10 +03:00
|
|
|
case GETCHAR_HOME: {
|
|
|
|
size_t displacement;
|
|
|
|
get_line_offset(&displacement, cursor_offset, buffer);
|
|
|
|
cursor_offset -= displacement;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GETCHAR_END: {
|
|
|
|
cursor_offset += get_line_length(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
case '\b':
|
|
|
|
if (cursor_offset) {
|
|
|
|
cursor_offset--;
|
|
|
|
case GETCHAR_DELETE:
|
|
|
|
for (size_t i = cursor_offset; ; i++) {
|
|
|
|
buffer[i] = buffer[i+1];
|
|
|
|
if (!buffer[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GETCHAR_F10:
|
|
|
|
disable_cursor();
|
|
|
|
return buffer;
|
2021-05-19 09:13:15 +03:00
|
|
|
case GETCHAR_ESCAPE:
|
2020-11-18 02:13:54 +03:00
|
|
|
disable_cursor();
|
2020-12-15 09:45:59 +03:00
|
|
|
return NULL;
|
2020-11-18 02:13:54 +03:00
|
|
|
default:
|
|
|
|
if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) {
|
|
|
|
for (size_t i = strlen(buffer); ; i--) {
|
|
|
|
buffer[i+1] = buffer[i];
|
|
|
|
if (i == cursor_offset)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer[cursor_offset++] = c;
|
2020-12-19 20:28:11 +03:00
|
|
|
} else {
|
|
|
|
display_overflow_error = true;
|
2020-11-18 02:13:54 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto refresh;
|
|
|
|
}
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
static size_t print_tree(const char *shift, size_t level, size_t base_index, size_t selected_entry,
|
2020-12-03 14:38:53 +03:00
|
|
|
struct menu_entry *current_entry,
|
|
|
|
struct menu_entry **selected_menu_entry) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t max_entries = 0;
|
2020-12-07 19:14:14 +03:00
|
|
|
|
2021-07-08 16:47:28 +03:00
|
|
|
bool no_print = false;
|
|
|
|
if (shift == NULL) {
|
|
|
|
no_print = true;
|
|
|
|
}
|
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
for (;;) {
|
|
|
|
if (current_entry == NULL)
|
|
|
|
break;
|
2021-07-08 16:47:28 +03:00
|
|
|
if (!no_print) print("%s", shift);
|
2020-11-17 02:50:26 +03:00
|
|
|
if (level) {
|
2021-08-17 01:00:23 +03:00
|
|
|
for (size_t i = level - 1; i > 0; i--) {
|
2020-11-17 02:50:26 +03:00
|
|
|
struct menu_entry *actual_parent = current_entry;
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t j = 0; j < i; j++)
|
2020-11-17 02:50:26 +03:00
|
|
|
actual_parent = actual_parent->parent;
|
2021-07-08 16:47:28 +03:00
|
|
|
if (actual_parent->next != NULL) {
|
|
|
|
if (!no_print) print(" \xb3");
|
|
|
|
} else {
|
|
|
|
if (!no_print) print(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current_entry->next == NULL) {
|
|
|
|
if (!no_print) print(" \xc0");
|
|
|
|
} else {
|
|
|
|
if (!no_print) print(" \xc3");
|
2020-11-17 02:50:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-08 16:47:28 +03:00
|
|
|
if (current_entry->sub) {
|
|
|
|
if (!no_print) print(current_entry->expanded ? "[-]" : "[+]");
|
|
|
|
} else if (level) {
|
|
|
|
if (!no_print) print("\xc4> ");
|
|
|
|
} else {
|
|
|
|
if (!no_print) print(" ");
|
|
|
|
}
|
2020-11-17 01:31:03 +03:00
|
|
|
if (base_index + max_entries == selected_entry) {
|
|
|
|
*selected_menu_entry = current_entry;
|
2021-07-08 16:47:28 +03:00
|
|
|
if (!no_print) print("\e[47m\e[30m");
|
2020-11-17 01:31:03 +03:00
|
|
|
}
|
2021-07-08 16:47:28 +03:00
|
|
|
if (!no_print) print(" %s \e[0m\n", current_entry->name);
|
2020-11-17 01:31:03 +03:00
|
|
|
if (current_entry->sub && current_entry->expanded) {
|
2021-07-07 05:23:34 +03:00
|
|
|
max_entries += print_tree(shift, level + 1, base_index + max_entries + 1,
|
2020-11-17 01:31:03 +03:00
|
|
|
selected_entry,
|
|
|
|
current_entry->sub,
|
|
|
|
selected_menu_entry);
|
|
|
|
}
|
|
|
|
max_entries++;
|
|
|
|
current_entry = current_entry->next;
|
|
|
|
}
|
|
|
|
return max_entries;
|
|
|
|
}
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2020-12-01 05:09:38 +03:00
|
|
|
char *menu(char **cmdline) {
|
2020-12-10 09:32:15 +03:00
|
|
|
menu_branding = config_get_value(NULL, 0, "MENU_BRANDING");
|
|
|
|
if (menu_branding == NULL)
|
|
|
|
menu_branding = "Limine " LIMINE_VERSION;
|
|
|
|
|
2021-09-06 08:31:57 +03:00
|
|
|
menu_branding_colour = config_get_value(NULL, 0, "MENU_BRANDING_COLOUR");
|
|
|
|
if (menu_branding_colour == NULL)
|
|
|
|
menu_branding_colour = config_get_value(NULL, 0, "MENU_BRANDING_COLOR");
|
|
|
|
if (menu_branding_colour == NULL)
|
|
|
|
menu_branding_colour = "6";
|
|
|
|
|
2020-12-02 21:16:38 +03:00
|
|
|
bool skip_timeout = false;
|
2020-12-03 14:38:53 +03:00
|
|
|
struct menu_entry *selected_menu_entry = NULL;
|
2020-11-17 01:31:03 +03:00
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t selected_entry = 0;
|
2020-11-27 21:33:34 +03:00
|
|
|
char *default_entry = config_get_value(NULL, 0, "DEFAULT_ENTRY");
|
|
|
|
if (default_entry != NULL) {
|
|
|
|
selected_entry = strtoui(default_entry, NULL, 10);
|
2020-12-03 14:38:53 +03:00
|
|
|
if (selected_entry)
|
|
|
|
selected_entry--;
|
2020-09-29 22:48:34 +03:00
|
|
|
}
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t timeout = 5;
|
2020-11-27 21:33:34 +03:00
|
|
|
char *timeout_config = config_get_value(NULL, 0, "TIMEOUT");
|
|
|
|
if (timeout_config != NULL) {
|
2020-12-03 14:38:53 +03:00
|
|
|
if (!strcmp(timeout_config, "no"))
|
|
|
|
skip_timeout = true;
|
|
|
|
else
|
|
|
|
timeout = strtoui(timeout_config, NULL, 10);
|
2020-09-29 22:48:34 +03:00
|
|
|
}
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
bool editor_enabled = true;
|
|
|
|
char *editor_enabled_config = config_get_value(NULL, 0, "EDITOR_ENABLED");
|
|
|
|
if (!strcmp(editor_enabled_config, "no")) editor_enabled = false;
|
2020-09-29 22:48:34 +03:00
|
|
|
|
2020-12-03 14:38:53 +03:00
|
|
|
if (!timeout) {
|
|
|
|
// Use print tree to load up selected_menu_entry and determine if the
|
|
|
|
// default entry is valid.
|
2021-07-08 16:47:28 +03:00
|
|
|
print_tree(NULL, 0, 0, selected_entry, menu_tree, &selected_menu_entry);
|
2020-12-03 14:38:53 +03:00
|
|
|
if (selected_menu_entry == NULL || selected_menu_entry->sub != NULL) {
|
|
|
|
print("Default entry is not valid or directory, booting to menu.\n");
|
|
|
|
skip_timeout = true;
|
|
|
|
} else {
|
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 22:48:34 +03:00
|
|
|
|
|
|
|
// If there is GRAPHICS config key and the value is "yes", enable graphics
|
2021-07-15 11:03:47 +03:00
|
|
|
#if bios == 1
|
2020-11-27 21:33:34 +03:00
|
|
|
char *graphics = config_get_value(NULL, 0, "GRAPHICS");
|
2021-07-15 11:03:47 +03:00
|
|
|
#elif uefi == 1
|
2021-03-10 07:08:37 +03:00
|
|
|
char *graphics = "yes";
|
|
|
|
#endif
|
2020-11-27 21:33:34 +03:00
|
|
|
if (graphics != NULL && !strcmp(graphics, "yes")) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t req_width = 0, req_height = 0, req_bpp = 0;
|
2020-09-02 11:31:39 +03:00
|
|
|
|
2021-04-11 03:30:48 +03:00
|
|
|
char *menu_resolution = config_get_value(NULL, 0, "MENU_RESOLUTION");
|
|
|
|
if (menu_resolution != NULL)
|
|
|
|
parse_resolution(&req_width, &req_height, &req_bpp, menu_resolution);
|
2021-04-09 02:26:39 +03:00
|
|
|
|
2021-04-11 03:30:48 +03:00
|
|
|
term_vbe(req_width, req_height);
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
|
|
|
|
2020-09-02 10:55:56 +03:00
|
|
|
disable_cursor();
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer(true);
|
|
|
|
|
2020-06-05 21:27:52 +03:00
|
|
|
refresh:
|
2020-09-02 10:55:56 +03:00
|
|
|
clear(true);
|
2021-07-07 05:23:34 +03:00
|
|
|
{
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n");
|
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
set_cursor_pos(term_cols / 2 - DIV_ROUNDUP(strlen(menu_branding), 2), y);
|
2021-09-06 08:31:57 +03:00
|
|
|
print("\e[3%sm%s\e[37m", menu_branding_colour, menu_branding);
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n\n\n\n");
|
|
|
|
}
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2021-04-20 07:45:03 +03:00
|
|
|
if (menu_tree == NULL) {
|
|
|
|
print("Config file %s.\n\n", config_ready ? "contains no valid entries" : "not found");
|
|
|
|
print("For information on the format of Limine config entries, consult CONFIG.md in\n");
|
|
|
|
print("the root of the Limine source repository.\n\n");
|
|
|
|
print("Press a key to enter an editor session and manually define a config entry...");
|
|
|
|
term_double_buffer_flush();
|
|
|
|
getchar();
|
|
|
|
char *new_body = NULL;
|
|
|
|
while (new_body == NULL)
|
2021-07-07 02:27:07 +03:00
|
|
|
new_body = config_entry_editor("New Entry", "");
|
2021-04-20 07:45:03 +03:00
|
|
|
selected_menu_entry = ext_mem_alloc(sizeof(struct menu_entry));
|
|
|
|
selected_menu_entry->body = new_body;
|
2021-04-20 11:49:49 +03:00
|
|
|
config_ready = true;
|
2021-04-20 07:45:03 +03:00
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
|
2021-07-08 17:07:33 +03:00
|
|
|
{ // Draw box around boot menu
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2021-07-08 17:07:33 +03:00
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
|
|
|
|
print("\xda");
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t i = 0; i < term_cols - 2; i++) {
|
2021-07-08 17:07:33 +03:00
|
|
|
print("\xc4");
|
|
|
|
}
|
|
|
|
print("\xbf");
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t i = y + 1; i < term_rows - 2; i++) {
|
2021-07-08 17:07:33 +03:00
|
|
|
set_cursor_pos(0, i);
|
|
|
|
print("\xb3");
|
|
|
|
set_cursor_pos(term_cols - 1, i);
|
|
|
|
print("\xb3");
|
|
|
|
}
|
|
|
|
|
|
|
|
print("\xc0");
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t i = 0; i < term_cols - 2; i++) {
|
2021-07-08 17:07:33 +03:00
|
|
|
print("\xc4");
|
|
|
|
}
|
|
|
|
print("\xd9");
|
|
|
|
|
|
|
|
set_cursor_pos(x, y + 2);
|
|
|
|
}
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t max_entries = print_tree("\xb3 ", 0, 0, selected_entry, menu_tree,
|
2020-11-17 01:31:03 +03:00
|
|
|
&selected_menu_entry);
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
{
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2021-07-07 02:27:07 +03:00
|
|
|
get_cursor_pos(&x, &y);
|
2021-07-07 05:23:34 +03:00
|
|
|
set_cursor_pos(0, 3);
|
2021-07-07 02:27:07 +03:00
|
|
|
if (editor_enabled && selected_menu_entry->sub == NULL) {
|
2021-07-07 05:23:34 +03:00
|
|
|
print(" \e[32mARROWS\e[0m Select \e[32mENTER\e[0m Boot \e[32mE\e[0m Edit");
|
2021-07-07 02:27:07 +03:00
|
|
|
} else {
|
2021-07-08 18:15:28 +03:00
|
|
|
print(" \e[32mARROWS\e[0m Select \e[32mENTER\e[0m %s",
|
|
|
|
selected_menu_entry->expanded ? "Collapse" : "Expand");
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
|
|
|
set_cursor_pos(x, y);
|
|
|
|
}
|
2020-12-02 21:16:38 +03:00
|
|
|
|
|
|
|
if (selected_menu_entry->sub != NULL)
|
|
|
|
skip_timeout = true;
|
2020-09-28 23:39:28 +03:00
|
|
|
|
|
|
|
int c;
|
2020-06-05 21:27:52 +03:00
|
|
|
|
|
|
|
if (skip_timeout == false) {
|
2020-09-28 23:39:28 +03:00
|
|
|
print("\n\n");
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t i = timeout; i; i--) {
|
2021-07-08 17:07:33 +03:00
|
|
|
set_cursor_pos(0, term_rows - 1);
|
2021-07-11 03:01:03 +03:00
|
|
|
scroll_disable();
|
2021-09-14 19:35:42 +03:00
|
|
|
print("\e[2K\e[32mBooting automatically in \e[92m%u\e[32m, press any key to stop the countdown...\e[0m", i);
|
2021-07-11 03:01:03 +03:00
|
|
|
scroll_enable();
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer_flush();
|
2021-03-04 15:48:31 +03:00
|
|
|
if ((c = pit_sleep_and_quit_on_keypress(1))) {
|
2020-06-05 21:27:52 +03:00
|
|
|
skip_timeout = true;
|
2021-07-08 17:57:40 +03:00
|
|
|
print("\e[2K");
|
2021-05-23 01:19:27 +03:00
|
|
|
term_double_buffer_flush();
|
2020-09-28 23:39:28 +03:00
|
|
|
goto timeout_aborted;
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
|
2021-07-08 17:57:40 +03:00
|
|
|
set_cursor_pos(0, term_rows - 1);
|
2021-07-11 03:01:03 +03:00
|
|
|
if (selected_menu_entry->comment != NULL) {
|
|
|
|
scroll_disable();
|
2021-07-08 19:05:17 +03:00
|
|
|
print("\e[36m%s\e[0m", selected_menu_entry->comment);
|
2021-07-11 03:01:03 +03:00
|
|
|
scroll_enable();
|
|
|
|
}
|
2021-07-08 17:57:40 +03:00
|
|
|
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer_flush();
|
|
|
|
|
2020-06-05 21:27:52 +03:00
|
|
|
for (;;) {
|
2020-09-28 23:39:28 +03:00
|
|
|
c = getchar();
|
|
|
|
timeout_aborted:
|
2020-06-05 21:27:52 +03:00
|
|
|
switch (c) {
|
|
|
|
case GETCHAR_CURSOR_UP:
|
2021-08-16 19:02:28 +03:00
|
|
|
if (selected_entry == 0)
|
2020-06-05 21:27:52 +03:00
|
|
|
selected_entry = max_entries - 1;
|
2021-08-16 19:02:28 +03:00
|
|
|
else
|
|
|
|
selected_entry--;
|
2020-06-05 21:27:52 +03:00
|
|
|
goto refresh;
|
|
|
|
case GETCHAR_CURSOR_DOWN:
|
|
|
|
if (++selected_entry == max_entries)
|
|
|
|
selected_entry = 0;
|
|
|
|
goto refresh;
|
2021-03-26 15:56:09 +03:00
|
|
|
case GETCHAR_CURSOR_RIGHT:
|
2020-12-15 09:45:59 +03:00
|
|
|
case '\n':
|
2020-06-05 21:27:52 +03:00
|
|
|
autoboot:
|
2020-11-17 01:31:03 +03:00
|
|
|
if (selected_menu_entry->sub != NULL) {
|
|
|
|
selected_menu_entry->expanded = !selected_menu_entry->expanded;
|
|
|
|
goto refresh;
|
|
|
|
}
|
2020-09-02 10:55:56 +03:00
|
|
|
enable_cursor();
|
2020-12-01 05:09:38 +03:00
|
|
|
*cmdline = config_get_value(selected_menu_entry->body, 0, "KERNEL_CMDLINE");
|
|
|
|
if (!*cmdline) {
|
|
|
|
*cmdline = config_get_value(selected_menu_entry->body, 0, "CMDLINE");
|
2020-11-27 21:33:34 +03:00
|
|
|
}
|
2020-12-01 05:09:38 +03:00
|
|
|
if (!*cmdline) {
|
|
|
|
*cmdline = "";
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
2020-09-02 10:55:56 +03:00
|
|
|
clear(true);
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer(false);
|
2020-11-17 01:31:03 +03:00
|
|
|
return selected_menu_entry->body;
|
2020-11-18 02:13:54 +03:00
|
|
|
case 'e': {
|
2021-07-07 02:27:07 +03:00
|
|
|
if (editor_enabled) {
|
|
|
|
if (selected_menu_entry->sub != NULL)
|
|
|
|
goto refresh;
|
|
|
|
enable_cursor();
|
|
|
|
char *new_body = config_entry_editor(selected_menu_entry->name, selected_menu_entry->body);
|
|
|
|
if (new_body == NULL)
|
|
|
|
goto refresh;
|
|
|
|
selected_menu_entry->body = new_body;
|
|
|
|
goto autoboot;
|
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
}
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|