mirror of
https://github.com/djhackersdev/bemanitools.git
synced 2025-02-17 11:18:31 +01:00
chore: Remove cconfig
Finish deprecation. Everything uses the new config abstraction layer of the bt(6) core.
This commit is contained in:
parent
04603a9591
commit
7840864998
@ -100,7 +100,6 @@ include src/main/bio2emu/Module.mk
|
||||
include src/main/bsthook/Module.mk
|
||||
include src/main/bstio/Module.mk
|
||||
include src/main/camhook/Module.mk
|
||||
include src/main/cconfig/Module.mk
|
||||
include src/main/config/Module.mk
|
||||
include src/main/core/Module.mk
|
||||
include src/main/d3d9-util/Module.mk
|
||||
|
9
dist/test/run-tests.sh
vendored
9
dist/test/run-tests.sh
vendored
@ -8,15 +8,6 @@ cd $DIR
|
||||
|
||||
echo "Running tests..."
|
||||
|
||||
wine ./cconfig-test.exe
|
||||
wine ./cconfig-util-test.exe
|
||||
wine ./cconfig-cmd-test.exe
|
||||
wine ./iidxhook-util-config-eamuse-test.exe
|
||||
wine ./iidxhook-util-config-gfx-test.exe
|
||||
# wine ./iidxhook-config-iidxhook1-test.exe
|
||||
# wine ./iidxhook-config-iidxhook2-test.exe
|
||||
wine ./iidxhook-util-config-misc-test.exe
|
||||
wine ./iidxhook-util-config-sec-test.exe
|
||||
wine ./security-id-test.exe
|
||||
wine ./security-mcode-test.exe
|
||||
wine ./security-util-test.exe
|
||||
|
@ -1,12 +0,0 @@
|
||||
libs += cconfig
|
||||
|
||||
libs_cconfig := \
|
||||
util \
|
||||
|
||||
src_cconfig := \
|
||||
cconfig-main.c \
|
||||
cconfig-hook.c \
|
||||
cconfig-util.c \
|
||||
cconfig.c \
|
||||
cmd.c \
|
||||
conf.c \
|
@ -1,14 +0,0 @@
|
||||
#include "cconfig/cconfig-util.h"
|
||||
#include "cconfig/cmd.h"
|
||||
|
||||
#include "cconfig/cconfig-hook.h"
|
||||
#include "cconfig/cconfig-main.h"
|
||||
|
||||
bool cconfig_hook_config_init(
|
||||
struct cconfig *config,
|
||||
const char *usage_header,
|
||||
enum cconfig_cmd_usage_out cmd_usage_out)
|
||||
{
|
||||
return cconfig_main_config_init(
|
||||
config, "--config", NULL, "--help", "-h", usage_header, cmd_usage_out);
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
#ifndef CCONFIG_HOOK_H
|
||||
#define CCONFIG_HOOK_H
|
||||
|
||||
#include "cconfig/cconfig.h"
|
||||
#include "cconfig/cmd.h"
|
||||
|
||||
/**
|
||||
* Init function for cconfig for hooks
|
||||
*
|
||||
* calls cconfig_main_config_init with some defaults for hook dlls
|
||||
* see: cconfig_main_config_init for more details
|
||||
*/
|
||||
bool cconfig_hook_config_init(
|
||||
struct cconfig *config,
|
||||
const char *usage_header,
|
||||
enum cconfig_cmd_usage_out cmd_usage_out);
|
||||
|
||||
#endif
|
@ -1,114 +0,0 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "cconfig/cconfig-util.h"
|
||||
#include "cconfig/cmd.h"
|
||||
#include "cconfig/conf.h"
|
||||
|
||||
#include "cconfig/cconfig-main.h"
|
||||
|
||||
#include "iface-core/log.h"
|
||||
|
||||
#include "util/cmdline.h"
|
||||
|
||||
bool cconfig_main_config_init(
|
||||
struct cconfig *config,
|
||||
const char *config_parameter_name,
|
||||
const char *default_config_path,
|
||||
const char *help_parameter_name,
|
||||
const char *help_parameter_short_name,
|
||||
const char *usage_header,
|
||||
enum cconfig_cmd_usage_out cmd_usage_out)
|
||||
{
|
||||
bool success;
|
||||
int argc;
|
||||
char **argv;
|
||||
enum cconfig_conf_error conf_error;
|
||||
const char *config_path;
|
||||
|
||||
success = true;
|
||||
|
||||
args_recover(&argc, &argv);
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (!strcmp(argv[i], help_parameter_short_name) ||
|
||||
!strcmp(argv[i], help_parameter_name)) {
|
||||
goto failure_usage;
|
||||
}
|
||||
}
|
||||
|
||||
config_path = NULL;
|
||||
|
||||
if (config_parameter_name != NULL) {
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (!strcmp(argv[i], config_parameter_name)) {
|
||||
if (i + 1 >= argc) {
|
||||
log_fatal(
|
||||
"--config parameter not followed by a config file "
|
||||
"path param");
|
||||
goto failure;
|
||||
}
|
||||
|
||||
config_path = argv[i + 1];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (config_path == NULL && default_config_path != NULL) {
|
||||
log_misc("Using default config path: %s", default_config_path);
|
||||
config_path = default_config_path;
|
||||
}
|
||||
|
||||
if (config_path) {
|
||||
log_misc("Loading config file: %s", config_path);
|
||||
conf_error = cconfig_conf_load_from_file(config, config_path, false);
|
||||
|
||||
if (conf_error == CCONFIG_CONF_ERROR_NO_SUCH_FILE) {
|
||||
/* Create default config */
|
||||
if (cconfig_conf_save_to_file(config, config_path) !=
|
||||
CCONFIG_CONF_SUCCESS) {
|
||||
log_fatal(
|
||||
"Creating default config file '%s' failed", config_path);
|
||||
goto failure;
|
||||
} else {
|
||||
log_info(
|
||||
"Default configuration '%s' created. Restart "
|
||||
"application",
|
||||
config_path);
|
||||
goto failure;
|
||||
}
|
||||
} else if (conf_error != CCONFIG_CONF_SUCCESS) {
|
||||
log_fatal(
|
||||
"Error loading config file '%s': %d", config_path, conf_error);
|
||||
goto failure;
|
||||
}
|
||||
|
||||
log_misc("Config state after file loading:");
|
||||
cconfig_util_log(config, log_misc_func);
|
||||
}
|
||||
|
||||
log_misc("Parsing override config parameters from cmd");
|
||||
|
||||
/* Override defaults or values loaded from file with values from cmd */
|
||||
if (!cconfig_cmd_parse(config, "-p", argc, argv, false)) {
|
||||
log_fatal("Error parsing cmd args for config values");
|
||||
goto failure_usage;
|
||||
}
|
||||
|
||||
log_misc("Config state after cmd parameter overrides:");
|
||||
cconfig_util_log(config, log_misc_func);
|
||||
|
||||
goto success;
|
||||
|
||||
failure_usage:
|
||||
cconfig_cmd_print_usage(config, usage_header, cmd_usage_out);
|
||||
|
||||
failure:
|
||||
success = false;
|
||||
|
||||
success:
|
||||
args_free(argc, argv);
|
||||
|
||||
return success;
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
#ifndef CCONFIG_MAIN_H
|
||||
#define CCONFIG_MAIN_H
|
||||
|
||||
#include "cconfig/cconfig.h"
|
||||
#include "cconfig/cmd.h"
|
||||
|
||||
/**
|
||||
* Init function for cconfig after all cconfig_util_set's have been called
|
||||
*
|
||||
* will parse a config file first (if specified)
|
||||
* then uses override config parameters from cmd
|
||||
*/
|
||||
bool cconfig_main_config_init(
|
||||
struct cconfig *config,
|
||||
const char *config_cmd_param_name,
|
||||
const char *config_default_path,
|
||||
const char *help_cmd_param_name,
|
||||
const char *help_cmd_param_shortname,
|
||||
const char *usage_header,
|
||||
enum cconfig_cmd_usage_out cmd_usage_out);
|
||||
|
||||
#endif
|
@ -1,233 +0,0 @@
|
||||
#define LOG_MODULE "cconfig-util"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cconfig/cconfig-util.h"
|
||||
|
||||
#include "iface-core/log.h"
|
||||
|
||||
#include "util/hex.h"
|
||||
#include "util/mem.h"
|
||||
|
||||
bool cconfig_util_get_int(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
int32_t *ret,
|
||||
int32_t default_value)
|
||||
{
|
||||
struct cconfig_entry *entry;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (entry) {
|
||||
if (sscanf(entry->value, "%d", ret) == 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
*ret = default_value;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cconfig_util_get_float(
|
||||
struct cconfig *config, const char *key, float *ret, float default_value)
|
||||
{
|
||||
struct cconfig_entry *entry;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (entry) {
|
||||
if (sscanf(entry->value, "%f", ret) == 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
*ret = default_value;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cconfig_util_get_bool(
|
||||
struct cconfig *config, const char *key, bool *ret, bool default_value)
|
||||
{
|
||||
struct cconfig_entry *entry;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (entry) {
|
||||
if (!strcmp(entry->value, "true")) {
|
||||
*ret = true;
|
||||
return true;
|
||||
} else if (!strcmp(entry->value, "false")) {
|
||||
*ret = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
*ret = default_value;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cconfig_util_get_str(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
char *buffer,
|
||||
size_t len,
|
||||
const char *default_value)
|
||||
{
|
||||
struct cconfig_entry *entry;
|
||||
size_t str_len;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (entry) {
|
||||
str_len = strlen(entry->value);
|
||||
|
||||
if (str_len <= len) {
|
||||
strcpy(buffer, entry->value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
strcpy(buffer, default_value);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cconfig_util_get_data(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
uint8_t *buffer,
|
||||
size_t len,
|
||||
const uint8_t *default_value)
|
||||
{
|
||||
size_t res_len;
|
||||
struct cconfig_entry *entry;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(len);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (entry) {
|
||||
res_len = strlen(entry->value);
|
||||
res_len = res_len / 2 + res_len % 2;
|
||||
|
||||
if (len <= res_len) {
|
||||
if (hex_decode(buffer, len, entry->value, strlen(entry->value))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(buffer, default_value, len);
|
||||
return false;
|
||||
}
|
||||
|
||||
void cconfig_util_set_int(
|
||||
struct cconfig *config, const char *key, int32_t value, const char *desc)
|
||||
{
|
||||
char *str;
|
||||
size_t str_len;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(desc);
|
||||
|
||||
str_len = snprintf(NULL, 0, "%d", value) + 1;
|
||||
str = xmalloc(str_len);
|
||||
snprintf(str, str_len, "%d", value);
|
||||
|
||||
cconfig_set(config, key, str, desc);
|
||||
|
||||
free(str);
|
||||
}
|
||||
|
||||
void cconfig_util_set_float(
|
||||
struct cconfig *config, const char *key, float value, const char *desc)
|
||||
{
|
||||
char *str;
|
||||
size_t str_len;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(desc);
|
||||
|
||||
str_len = snprintf(NULL, 0, "%f", value) + 1;
|
||||
str = xmalloc(str_len);
|
||||
snprintf(str, str_len, "%f", value);
|
||||
|
||||
cconfig_set(config, key, str, desc);
|
||||
|
||||
free(str);
|
||||
}
|
||||
|
||||
void cconfig_util_set_bool(
|
||||
struct cconfig *config, const char *key, bool value, const char *desc)
|
||||
{
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(desc);
|
||||
|
||||
cconfig_set(config, key, value ? "true" : "false", desc);
|
||||
}
|
||||
|
||||
void cconfig_util_set_str(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
const char *value,
|
||||
const char *desc)
|
||||
{
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(desc);
|
||||
log_assert(value);
|
||||
|
||||
cconfig_set(config, key, value, desc);
|
||||
}
|
||||
|
||||
void cconfig_util_set_data(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
const uint8_t *value,
|
||||
size_t len,
|
||||
const char *desc)
|
||||
{
|
||||
char *str;
|
||||
size_t str_len;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(desc);
|
||||
|
||||
str_len = len * 2 + 1;
|
||||
str = xmalloc(str_len);
|
||||
hex_encode_uc(value, len, str, str_len);
|
||||
|
||||
cconfig_set(config, key, str, desc);
|
||||
|
||||
free(str);
|
||||
}
|
||||
|
||||
void cconfig_util_log(struct cconfig *config, bt_core_log_message_t log_message)
|
||||
{
|
||||
for (uint32_t i = 0; i < config->nentries; i++) {
|
||||
log_message(
|
||||
LOG_MODULE,
|
||||
"%s=%s",
|
||||
config->entries[i].key,
|
||||
config->entries[i].value);
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
#ifndef CCONFIG_UTIL_H
|
||||
#define CCONFIG_UTIL_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cconfig/cconfig.h"
|
||||
|
||||
#include "iface-core/log.h"
|
||||
|
||||
bool cconfig_util_get_int(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
int32_t *ret,
|
||||
int32_t default_value);
|
||||
|
||||
bool cconfig_util_get_float(
|
||||
struct cconfig *config, const char *key, float *ret, float default_value);
|
||||
|
||||
bool cconfig_util_get_bool(
|
||||
struct cconfig *config, const char *key, bool *ret, bool default_value);
|
||||
|
||||
bool cconfig_util_get_str(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
char *buffer,
|
||||
size_t len,
|
||||
const char *default_value);
|
||||
|
||||
bool cconfig_util_get_data(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
uint8_t *buffer,
|
||||
size_t len,
|
||||
const uint8_t *default_value);
|
||||
|
||||
void cconfig_util_set_int(
|
||||
struct cconfig *config, const char *key, int32_t value, const char *desc);
|
||||
|
||||
void cconfig_util_set_float(
|
||||
struct cconfig *config, const char *key, float value, const char *desc);
|
||||
|
||||
void cconfig_util_set_bool(
|
||||
struct cconfig *config, const char *key, bool value, const char *desc);
|
||||
|
||||
void cconfig_util_set_str(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
const char *value,
|
||||
const char *desc);
|
||||
|
||||
void cconfig_util_set_data(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
const uint8_t *value,
|
||||
size_t len,
|
||||
const char *desc);
|
||||
|
||||
void cconfig_util_log(
|
||||
struct cconfig *config, bt_core_log_message_t log_message);
|
||||
|
||||
#endif
|
@ -1,115 +0,0 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "cconfig/cconfig.h"
|
||||
|
||||
#include "iface-core/log.h"
|
||||
|
||||
#include "util/mem.h"
|
||||
#include "util/str.h"
|
||||
|
||||
static struct cconfig_entry *cconfig_extend_config(struct cconfig *config)
|
||||
{
|
||||
config->nentries++;
|
||||
|
||||
config->entries = xrealloc(
|
||||
config->entries, config->nentries * sizeof(struct cconfig_entry));
|
||||
memset(
|
||||
&config->entries[config->nentries - 1],
|
||||
0,
|
||||
sizeof(struct cconfig_entry));
|
||||
|
||||
return &config->entries[config->nentries - 1];
|
||||
}
|
||||
|
||||
struct cconfig *cconfig_init()
|
||||
{
|
||||
struct cconfig *config;
|
||||
|
||||
config = xmalloc(sizeof(struct cconfig));
|
||||
memset(config, 0, sizeof(struct cconfig));
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
struct cconfig_entry *cconfig_get(struct cconfig *config, const char *key)
|
||||
{
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
|
||||
for (uint32_t i = 0; i < config->nentries; i++) {
|
||||
if (!strcmp(config->entries[i].key, key)) {
|
||||
return &config->entries[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void cconfig_set(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
const char *value,
|
||||
const char *desc)
|
||||
{
|
||||
struct cconfig_entry *entry;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(value);
|
||||
log_assert(desc);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (!entry) {
|
||||
entry = cconfig_extend_config(config);
|
||||
} else {
|
||||
free(entry->key);
|
||||
free(entry->value);
|
||||
free(entry->desc);
|
||||
|
||||
memset(entry, 0, sizeof(struct cconfig_entry));
|
||||
}
|
||||
|
||||
entry->key = str_dup(key);
|
||||
entry->desc = str_dup(desc);
|
||||
entry->value = str_dup(value);
|
||||
}
|
||||
|
||||
void cconfig_set2(struct cconfig *config, const char *key, const char *value)
|
||||
{
|
||||
struct cconfig_entry *entry;
|
||||
|
||||
log_assert(config);
|
||||
log_assert(key);
|
||||
log_assert(value);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (!entry) {
|
||||
entry = cconfig_extend_config(config);
|
||||
} else {
|
||||
free(entry->key);
|
||||
free(entry->value);
|
||||
}
|
||||
|
||||
entry->key = str_dup(key);
|
||||
entry->value = str_dup(value);
|
||||
|
||||
/* Description optional, but do not wipe previous description if
|
||||
available */
|
||||
if (!entry->desc) {
|
||||
entry->desc = "";
|
||||
}
|
||||
}
|
||||
|
||||
void cconfig_finit(struct cconfig *config)
|
||||
{
|
||||
for (uint32_t i = 0; i < config->nentries; i++) {
|
||||
free(config->entries[i].key);
|
||||
free(config->entries[i].value);
|
||||
free(config->entries[i].desc);
|
||||
}
|
||||
|
||||
free(config->entries);
|
||||
free(config);
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
#ifndef CCONFIG_H
|
||||
#define CCONFIG_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct cconfig_entry {
|
||||
char *key;
|
||||
char *value;
|
||||
char *desc;
|
||||
};
|
||||
|
||||
struct cconfig {
|
||||
uint32_t nentries;
|
||||
struct cconfig_entry *entries;
|
||||
};
|
||||
|
||||
struct cconfig *cconfig_init();
|
||||
|
||||
struct cconfig_entry *cconfig_get(struct cconfig *config, const char *key);
|
||||
|
||||
void cconfig_set(
|
||||
struct cconfig *config,
|
||||
const char *key,
|
||||
const char *value,
|
||||
const char *desc);
|
||||
|
||||
void cconfig_set2(struct cconfig *config, const char *key, const char *value);
|
||||
|
||||
void cconfig_finit(struct cconfig *config);
|
||||
|
||||
#endif
|
@ -1,142 +0,0 @@
|
||||
#define LOG_MODULE "cconfig-cmd"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <windows.h>
|
||||
|
||||
#include "cconfig/cmd.h"
|
||||
|
||||
#include "iface-core/log.h"
|
||||
|
||||
#include "util/hex.h"
|
||||
#include "util/str.h"
|
||||
|
||||
static void
|
||||
cconfig_cmd_usage_print(enum cconfig_cmd_usage_out output, const char *fmt, ...)
|
||||
{
|
||||
char buffer[32768];
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
|
||||
switch (output) {
|
||||
case CCONFIG_CMD_USAGE_OUT_STDOUT:
|
||||
vfprintf(stdout, fmt, ap);
|
||||
break;
|
||||
|
||||
case CCONFIG_CMD_USAGE_OUT_STDERR:
|
||||
vfprintf(stderr, fmt, ap);
|
||||
break;
|
||||
|
||||
case CCONFIG_CMD_USAGE_OUT_DBG:
|
||||
_vsnprintf(buffer, sizeof(buffer), fmt, ap);
|
||||
OutputDebugString(buffer);
|
||||
break;
|
||||
|
||||
case CCONFIG_CMD_USAGE_OUT_LOG:
|
||||
_vsnprintf(buffer, sizeof(buffer), fmt, ap);
|
||||
log_info("%s", buffer);
|
||||
break;
|
||||
|
||||
default:
|
||||
log_assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
bool cconfig_cmd_parse(
|
||||
struct cconfig *config,
|
||||
const char *key_ident,
|
||||
int argc,
|
||||
char **argv,
|
||||
bool add_params_if_absent)
|
||||
{
|
||||
bool no_error;
|
||||
struct cconfig_entry *entry;
|
||||
char *tmp;
|
||||
char *cur_tok;
|
||||
int ntok;
|
||||
char *toks[2];
|
||||
|
||||
no_error = true;
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (!strcmp(argv[i], key_ident)) {
|
||||
if (i + 1 >= argc) {
|
||||
no_error = false;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Not another key ident is following */
|
||||
if (!strcmp(argv[i + 1], key_ident)) {
|
||||
no_error = false;
|
||||
break;
|
||||
}
|
||||
|
||||
++i;
|
||||
|
||||
tmp = str_dup(argv[i]);
|
||||
ntok = 0;
|
||||
|
||||
cur_tok = strtok(tmp, "=");
|
||||
|
||||
while (cur_tok != NULL) {
|
||||
toks[ntok] = cur_tok;
|
||||
ntok++;
|
||||
cur_tok = strtok(NULL, "=");
|
||||
|
||||
if (ntok == 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ntok != 2) {
|
||||
/* Tokenizing key=value parameter error */
|
||||
log_warning("Parsing parameter '%s' failed, ignore", argv[i]);
|
||||
|
||||
free(tmp);
|
||||
no_error = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
log_misc("Key: %s, Value: %s", toks[0], toks[1]);
|
||||
|
||||
entry = cconfig_get(config, toks[0]);
|
||||
|
||||
if (entry || add_params_if_absent) {
|
||||
cconfig_set2(config, toks[0], toks[1]);
|
||||
} else {
|
||||
/* Ignore cmd params that are not found in config */
|
||||
log_warning(
|
||||
"Could not find cmd parameter with key '%s' in "
|
||||
"config, ignored",
|
||||
toks[0]);
|
||||
}
|
||||
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
return no_error;
|
||||
}
|
||||
|
||||
void cconfig_cmd_print_usage(
|
||||
struct cconfig *config,
|
||||
const char *usage_header,
|
||||
enum cconfig_cmd_usage_out output)
|
||||
{
|
||||
cconfig_cmd_usage_print(output, "%s\n", usage_header);
|
||||
|
||||
for (uint32_t i = 0; i < config->nentries; i++) {
|
||||
cconfig_cmd_usage_print(
|
||||
output,
|
||||
" %s: %s\n"
|
||||
" default: %s\n",
|
||||
config->entries[i].key,
|
||||
config->entries[i].desc,
|
||||
config->entries[i].value);
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
#ifndef CCONFIG_CMD_H
|
||||
#define CCONFIG_CMD_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "cconfig/cconfig.h"
|
||||
|
||||
enum cconfig_cmd_usage_out {
|
||||
CCONFIG_CMD_USAGE_OUT_STDOUT,
|
||||
CCONFIG_CMD_USAGE_OUT_STDERR,
|
||||
CCONFIG_CMD_USAGE_OUT_DBG,
|
||||
CCONFIG_CMD_USAGE_OUT_LOG,
|
||||
};
|
||||
|
||||
bool cconfig_cmd_parse(
|
||||
struct cconfig *config,
|
||||
const char *key_ident,
|
||||
int argc,
|
||||
char **argv,
|
||||
bool add_params_if_absent);
|
||||
|
||||
void cconfig_cmd_print_usage(
|
||||
struct cconfig *config,
|
||||
const char *usage_header,
|
||||
enum cconfig_cmd_usage_out output);
|
||||
|
||||
#endif
|
@ -1,129 +0,0 @@
|
||||
#define LOG_MODULE "cconfig-conf"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cconfig/conf.h"
|
||||
|
||||
#include "iface-core/log.h"
|
||||
|
||||
#include "util/fs.h"
|
||||
#include "util/str.h"
|
||||
|
||||
enum cconfig_conf_error cconfig_conf_load_from_file(
|
||||
struct cconfig *config, const char *path, bool add_params_if_absent)
|
||||
{
|
||||
char *pos_lines;
|
||||
char *pos_key_val;
|
||||
char *ctx_lines;
|
||||
char *ctx_key_val;
|
||||
char *data;
|
||||
size_t len;
|
||||
bool use_crlf;
|
||||
|
||||
if (!file_load(path, (void **) &data, &len, true)) {
|
||||
/* If file does not exist, create one with default configuration
|
||||
values */
|
||||
if (path_exists(path)) {
|
||||
return CCONFIG_CONF_ERROR_FILE_CORRUPTED;
|
||||
} else {
|
||||
return CCONFIG_CONF_ERROR_NO_SUCH_FILE;
|
||||
}
|
||||
}
|
||||
|
||||
use_crlf = false;
|
||||
if (strstr(data, "\r\n")) {
|
||||
use_crlf = true;
|
||||
}
|
||||
pos_lines = strtok_r(data, use_crlf ? "\r\n" : "\n", &ctx_lines);
|
||||
|
||||
while (pos_lines != NULL) {
|
||||
char *pos_line_dup;
|
||||
char *key = NULL;
|
||||
char *val = NULL;
|
||||
int cnt = 0;
|
||||
struct cconfig_entry *entry;
|
||||
|
||||
/* ignore comments and empty lines */
|
||||
if (strlen(pos_lines) > 0 && pos_lines[0] != '#') {
|
||||
pos_line_dup = str_dup(pos_lines);
|
||||
pos_key_val = strtok_r(pos_line_dup, "=", &ctx_key_val);
|
||||
|
||||
log_misc("Line: %s", pos_lines);
|
||||
|
||||
while (pos_key_val != NULL) {
|
||||
if (cnt == 0) {
|
||||
key = pos_key_val;
|
||||
} else if (cnt == 1) {
|
||||
val = pos_key_val;
|
||||
}
|
||||
|
||||
pos_key_val = strtok_r(NULL, "=", &ctx_key_val);
|
||||
cnt++;
|
||||
}
|
||||
|
||||
/* Key requiured, value can be NULL */
|
||||
if (cnt != 1 && cnt != 2) {
|
||||
log_warning(
|
||||
"Invalid options line %s in options file %s",
|
||||
pos_lines,
|
||||
path);
|
||||
free(pos_line_dup);
|
||||
free(data);
|
||||
return CCONFIG_CONF_ERROR_PARSING;
|
||||
}
|
||||
|
||||
/* NULL not allowed but empty string */
|
||||
if (!val) {
|
||||
val = "";
|
||||
}
|
||||
|
||||
log_misc("Key: %s, Value: %s", key, val);
|
||||
|
||||
entry = cconfig_get(config, key);
|
||||
|
||||
if (entry || add_params_if_absent) {
|
||||
cconfig_set2(config, key, val);
|
||||
} else {
|
||||
/* Ignore cmd params that are not found in config */
|
||||
log_warning(
|
||||
"Could not find parameter with key '%s' in "
|
||||
"config, ignored",
|
||||
key);
|
||||
}
|
||||
|
||||
free(pos_line_dup);
|
||||
}
|
||||
|
||||
pos_lines = strtok_r(NULL, use_crlf ? "\r\n" : "\n", &ctx_lines);
|
||||
}
|
||||
|
||||
free(data);
|
||||
|
||||
return CCONFIG_CONF_SUCCESS;
|
||||
}
|
||||
|
||||
enum cconfig_conf_error
|
||||
cconfig_conf_save_to_file(struct cconfig *config, const char *path)
|
||||
{
|
||||
FILE *file;
|
||||
|
||||
file = fopen(path, "wb+");
|
||||
|
||||
if (file == NULL) {
|
||||
return CCONFIG_CONF_ERROR_NO_SUCH_FILE;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < config->nentries; i++) {
|
||||
fprintf(file, "# %s\n", config->entries[i].desc);
|
||||
fprintf(
|
||||
file,
|
||||
"%s=%s\n\n",
|
||||
config->entries[i].key,
|
||||
config->entries[i].value);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return CCONFIG_CONF_SUCCESS;
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "cconfig/cconfig.h"
|
||||
|
||||
enum cconfig_conf_error {
|
||||
CCONFIG_CONF_SUCCESS = 0,
|
||||
CCONFIG_CONF_ERROR_NO_SUCH_FILE = 1,
|
||||
CCONFIG_CONF_ERROR_FILE_CORRUPTED = 2,
|
||||
CCONFIG_CONF_ERROR_PARSING = 3,
|
||||
};
|
||||
|
||||
enum cconfig_conf_error cconfig_conf_load_from_file(
|
||||
struct cconfig *config, const char *path, bool add_params_if_absent);
|
||||
|
||||
enum cconfig_conf_error
|
||||
cconfig_conf_save_to_file(struct cconfig *config, const char *path);
|
Loading…
x
Reference in New Issue
Block a user