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.

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.