config: Implement and use config_get_tuple()

This commit is contained in:
mintsuki 2021-11-09 12:01:53 +01:00
parent 1e1a0f2508
commit 9d4bf41ed9
5 changed files with 54 additions and 12 deletions

View File

@ -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;
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -308,12 +308,14 @@ 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");
char *module_path = conf_tuple.value1;
char *module_string = conf_tuple.value2;
struct stivale2_module *m = &tag->modules[i];
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.
@ -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);
}

View File

@ -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