:: commit 51f3638bdd7b6e534c0e76da15479fcf33ebc504

mintsuki <mintsuki@protonmail.com> — 2021-11-09 11:01

parents: 10091dc74d

config: Implement and use config_get_tuple()

diff --git a/stage23/lib/config.c b/stage23/lib/config.c
index 080a77ba..130bac71 100644
--- a/stage23/lib/config.c
+++ b/stage23/lib/config.c
@@ -219,6 +219,34 @@ cont:
     return ret;
 }
 
+static const char *lastkey;
+
+struct conf_tuple config_get_tuple(const char *config, size_t index,
+                                   const char *key1, const char *key2) {
+    struct conf_tuple conf_tuple;
+
+    conf_tuple.value1 = config_get_value(config, index, key1);
+    if (conf_tuple.value1 == NULL) {
+        return (struct conf_tuple){0};
+    }
+
+    conf_tuple.value2 = config_get_value(lastkey, 0, key2);
+
+    const char *lk1 = lastkey;
+
+    const char *next_value1 = config_get_value(config, index + 1, key1);
+
+    const char *lk2 = lastkey;
+
+    if (conf_tuple.value2 != NULL && next_value1 != NULL) {
+        if ((uintptr_t)lk1 > (uintptr_t)lk2) {
+            conf_tuple.value2 = NULL;
+        }
+    }
+
+    return conf_tuple;
+}
+
 char *config_get_value(const char *config, size_t index, const char *key) {
     if (!key || !config_ready)
         return NULL;
@@ -241,6 +269,7 @@ char *config_get_value(const char *config, size_t index, const char *key) {
                  value_len++);
             char *buf = ext_mem_alloc(value_len + 1);
             memcpy(buf, config + i, value_len);
+            lastkey = config + i;
             return buf;
         }
     }
diff --git a/stage23/lib/config.h b/stage23/lib/config.h
index f9cea928..7f0703d0 100644
--- a/stage23/lib/config.h
+++ b/stage23/lib/config.h
@@ -17,13 +17,21 @@ struct menu_entry {
     struct menu_entry *next;
 };
 
+struct conf_tuple {
+    char *value1;
+    char *value2;
+};
+
 extern struct menu_entry *menu_tree;
 
 int init_config_disk(struct volume *part);
 int init_config_pxe(void);
 int init_config(size_t config_size);
+
 bool config_get_entry_name(char *ret, size_t index, size_t limit);
 char *config_get_entry(size_t *size, size_t index);
 char *config_get_value(const char *config, size_t index, const char *key);
+struct conf_tuple config_get_tuple(const char *config, size_t index,
+                                   const char *key1, const char *key2);
 
 #endif
diff --git a/stage23/protos/stivale.c b/stage23/protos/stivale.c
index bc8c2113..e2913349 100644
--- a/stage23/protos/stivale.c
+++ b/stage23/protos/stivale.c
@@ -193,7 +193,12 @@ void stivale_load(char *config, char *cmdline) {
     stivale_struct.module_count = 0;
     uint64_t *prev_mod_ptr = &stivale_struct.modules;
     for (int i = 0; ; i++) {
-        char *module_path = config_get_value(config, i, "MODULE_PATH");
+        struct conf_tuple conf_tuple =
+                config_get_tuple(config, i, "MODULE_PATH", "MODULE_STRING");
+
+        char *module_path = conf_tuple.value1;
+        char *module_string = conf_tuple.value2;
+
         if (module_path == NULL)
             break;
 
@@ -201,8 +206,6 @@ void stivale_load(char *config, char *cmdline) {
 
         struct stivale_module *m = ext_mem_alloc(sizeof(struct stivale_module));
 
-        char *module_string = config_get_value(config, i, "MODULE_STRING");
-
         // TODO: perhaps change the module string to to be a pointer.
         //
         // NOTE: By default, the module string is the file name.
@@ -216,10 +219,10 @@ void stivale_load(char *config, char *cmdline) {
         } else {
             // TODO perhaps change this to be a pointer
             size_t str_len = strlen(module_string);
-            
+
             if (str_len > 127)
                 str_len = 127;
-            
+
             memcpy(m->string, module_string, str_len);
         }
 
diff --git a/stage23/protos/stivale2.c b/stage23/protos/stivale2.c
index 21cd7fba..c89b0ef6 100644
--- a/stage23/protos/stivale2.c
+++ b/stage23/protos/stivale2.c
@@ -308,11 +308,13 @@ failed_to_load_header_section:
     tag->module_count   = module_count;
 
     for (size_t i = 0; i < module_count; i++) {
-        char *module_path = config_get_value(config, i, "MODULE_PATH");
+        struct conf_tuple conf_tuple =
+                config_get_tuple(config, i, "MODULE_PATH", "MODULE_STRING");
 
-        struct stivale2_module *m = &tag->modules[i];
+        char *module_path = conf_tuple.value1;
+        char *module_string = conf_tuple.value2;
 
-        char *module_string = config_get_value(config, i, "MODULE_STRING");
+        struct stivale2_module *m = &tag->modules[i];
 
         // TODO: perhaps change the module string to to be a pointer.
         //
@@ -326,10 +328,10 @@ failed_to_load_header_section:
             memcpy(m->string, module_path, str_len);
         } else {
             size_t str_len = strlen(module_string);
-            
+
             if (str_len > 127)
                 str_len = 127;
-            
+
             memcpy(m->string, module_string, str_len);
         }
 
diff --git a/test/limine.cfg b/test/limine.cfg
index 1cf676f0..8ba0940a 100644
--- a/test/limine.cfg
+++ b/test/limine.cfg
@@ -21,8 +21,8 @@ KERNEL_CMDLINE=Woah! Another example!
 MODULE_PATH=boot:///boot/bg.bmp
 MODULE_STRING=yooooo
 
-# Test that the module string provided to the kernel will be 
-# the module path since a module string is not specified. 
+# Test that the module string provided to the kernel will be
+# the module path since a module string is not specified.
 # (cc CONFIG.md stivale2.`MODULE_STRING` section)
 MODULE_PATH=boot:///boot/bg.bmp
 
tab: 248 wrap: offon