:: commit cce8a70eee6640920c5ffdad1313df5d03a0f09c

illis <170103872+iillis@users.noreply.github.com> — 2024-06-06 14:56

parents: f137a99b50

When booting from editor fails you return to the editor while keeping the text as you were editing it (#364)

* When booting from editor fails you return to the editor while keeping the failing text

* Fixing syntax and menu render, and adding title backup

* Adding the buffer end and fixing blank bug

* Using strcpy() instead of memcpy for the title and relocating booting_from_editor = true;
diff --git a/common/menu.c b/common/menu.c
index 3505e116..3c9a8d86 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -22,15 +22,13 @@
 #include <protos/multiboot2.h>
 #include <protos/limine.h>
 #include <sys/cpu.h>
+#include <lib/misc.h>
 
 #if defined (UEFI)
 EFI_GUID limine_efi_vendor_guid =
     { 0x513ee0d0, 0x6e43, 0xcb05, { 0xb2, 0x72, 0xf1, 0x46, 0xa2, 0xfc, 0xb8, 0x8a } };
 #endif
 
-static char *menu_branding = NULL;
-static char *menu_branding_colour = NULL;
-
 #define EDITOR_MAX_BUFFER_SIZE 4096
 #define TOK_KEY 0
 #define TOK_EQUALS 1
@@ -38,6 +36,12 @@ static char *menu_branding_colour = NULL;
 #define TOK_BADKEY 3
 #define TOK_COMMENT 4
 
+static char *menu_branding = NULL;
+static char *menu_branding_colour = NULL;
+static no_unwind bool booting_from_editor = false;
+static no_unwind char saved_orig_entry[EDITOR_MAX_BUFFER_SIZE];
+static no_unwind char saved_title[64];
+
 static size_t get_line_offset(size_t *displacement, size_t index, const char *buffer) {
     size_t offset = 0;
     size_t _index = index;
@@ -169,6 +173,11 @@ char *config_entry_editor(const char *title, const char *orig_entry) {
 
     print("\e[2J\e[H");
 
+    if (booting_from_editor) {
+        orig_entry = saved_orig_entry;
+        title = saved_title;
+    }
+
     size_t cursor_offset  = 0;
     size_t entry_size     = strlen(orig_entry);
     size_t _window_size   = terms[0]->rows - 8;
@@ -463,11 +472,16 @@ refresh:
             }
             break;
         case GETCHAR_F10:
+            memcpy(saved_orig_entry, buffer, buffer_len);
+            saved_orig_entry[buffer_len] = 0;
+            strcpy(saved_title, title);
             editor_no_term_reset ? editor_no_term_reset = false : reset_term();
+            booting_from_editor = true;
             return buffer;
         case GETCHAR_ESCAPE:
             pmm_free(buffer, EDITOR_MAX_BUFFER_SIZE);
             editor_no_term_reset ? editor_no_term_reset = false : reset_term();
+            booting_from_editor = false;
             return NULL;
         default:
             if (buffer_len < EDITOR_MAX_BUFFER_SIZE - 1) {
@@ -961,6 +975,10 @@ refresh:
         FOR_TERM(TERM->scroll_enabled = true);
     }
 
+    if (booting_from_editor) {
+        goto editor;
+    }
+
     FOR_TERM(TERM->double_buffer_flush(TERM));
 
     for (;;) {
@@ -1031,6 +1049,7 @@ timeout_aborted:
             case 'e':
             case 'E': {
                 if (editor_enabled) {
+                    editor:
                     if (selected_menu_entry == NULL || selected_menu_entry->sub != NULL) {
                         break;
                     }
tab: 248 wrap: offon