rulimine/stage2/lib/config.c

124 lines
2.6 KiB
C
Raw Normal View History

2020-01-22 09:13:19 +03:00
#include <stddef.h>
2020-10-17 06:37:39 +03:00
#include <stdbool.h>
2020-01-22 09:13:19 +03:00
#include <lib/config.h>
#include <lib/libc.h>
2020-03-30 22:24:36 +03:00
#include <lib/blib.h>
2020-09-20 13:03:44 +03:00
#include <mm/pmm.h>
2020-04-14 06:20:55 +03:00
#include <fs/file.h>
2020-01-22 09:13:19 +03:00
#define SEPARATOR '\n'
2020-10-20 03:38:44 +03:00
bool config_ready = false;
2020-03-30 22:24:36 +03:00
static char *config_addr;
2020-11-02 12:17:20 +03:00
int init_config(struct part *part) {
2020-04-14 06:20:55 +03:00
struct file_handle f;
2020-03-30 22:24:36 +03:00
2020-11-02 12:17:20 +03:00
if (fopen(&f, part, "/limine.cfg")) {
if (fopen(&f, part, "/boot/limine.cfg")) {
2020-04-23 23:23:38 +03:00
return -1;
}
2020-03-30 22:24:36 +03:00
}
2020-07-02 17:20:18 +03:00
size_t config_size = f.size + 1;
2020-09-20 13:03:44 +03:00
config_addr = conv_mem_alloc(config_size);
2020-03-30 22:24:36 +03:00
2020-04-14 06:20:55 +03:00
fread(&f, config_addr, 0, f.size);
2020-03-30 22:24:36 +03:00
// remove windows carriage returns, if any
2020-07-02 17:14:09 +03:00
for (size_t i = 0; i < config_size; i++) {
if (config_addr[i] == '\r') {
2020-07-02 17:14:09 +03:00
for (size_t j = i; j < config_size - 1; j++)
config_addr[j] = config_addr[j+1];
2020-07-02 17:14:09 +03:00
config_size--;
}
}
2020-10-20 03:38:44 +03:00
config_ready = true;
2020-03-30 22:24:36 +03:00
return 0;
}
2020-04-21 17:23:30 +03:00
int config_get_entry_name(char *ret, size_t index, size_t limit) {
char *p = config_addr;
for (size_t i = 0; i <= index; i++) {
while (*p != ':') {
if (!*p)
return -1;
p++;
}
p++;
2020-05-27 10:24:59 +03:00
if ((p - 1) != config_addr && *(p - 2) != '\n')
i--;
2020-04-21 17:23:30 +03:00
}
size_t i;
for (i = 0; i < (limit - 1); i++) {
if (p[i] == SEPARATOR)
break;
ret[i] = p[i];
}
ret[i] = 0;
return 0;
}
int config_set_entry(size_t index) {
char *p = config_addr;
for (size_t i = 0; i <= index; i++) {
while (*p != ':') {
if (!*p)
return -1;
p++;
}
2020-04-21 19:27:05 +03:00
p++;
2020-05-27 10:24:59 +03:00
if ((p - 1) != config_addr && *(p - 2) != '\n')
i--;
2020-04-21 17:23:30 +03:00
}
config_addr = p;
cont:
2020-04-21 19:27:05 +03:00
while (*p != ':' && *p)
p++;
2020-04-21 17:23:30 +03:00
if (*p && *(p - 1) != '\n') {
p++;
goto cont;
}
2020-04-21 19:27:05 +03:00
*p = 0;
2020-04-21 17:23:30 +03:00
return 0;
}
2020-03-30 23:27:15 +03:00
char *config_get_value(char *buf, size_t index, size_t limit, const char *key) {
2020-03-30 22:24:36 +03:00
if (!limit || !buf || !key)
2020-01-22 09:13:19 +03:00
return NULL;
size_t key_len = strlen(key);
2020-03-30 22:24:36 +03:00
for (size_t i = 0; config_addr[i]; i++) {
if (!strncmp(&config_addr[i], key, key_len) && config_addr[i + key_len] == '=') {
if (i && config_addr[i - 1] != SEPARATOR)
2020-01-22 09:13:19 +03:00
continue;
2020-03-30 23:27:15 +03:00
if (index--)
continue;
2020-01-22 09:13:19 +03:00
i += key_len + 1;
size_t j;
2020-03-30 22:24:36 +03:00
for (j = 0; config_addr[i + j] != SEPARATOR && config_addr[i + j]; j++) {
2020-01-22 09:13:19 +03:00
if (j == limit - 1)
break;
2020-03-30 22:24:36 +03:00
buf[j] = config_addr[i + j];
2020-01-22 09:13:19 +03:00
}
buf[j] = 0;
return buf;
}
}
return NULL;
}