Bemanitools v5.26 release

This commit is contained in:
icex2
2019-09-27 22:36:50 +02:00
commit cbd7720349
718 changed files with 62731 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
libs += cconfig
libs_cconfig := \
util \
src_cconfig := \
cconfig-hook.c \
cconfig-util.c \
cconfig.c \
cmd.c \
conf.c \
+96
View File
@@ -0,0 +1,96 @@
#include <string.h>
#include "cconfig/cconfig-util.h"
#include "cconfig/cmd.h"
#include "cconfig/conf.h"
#include "cconfig/cconfig-hook.h"
#include "util/cmdline.h"
#include "util/log.h"
bool cconfig_hook_config_init(struct cconfig* config, const char* usage_header,
enum cconfig_cmd_usage_out cmd_usage_out)
{
bool success;
int argc;
char **argv;
enum cconfig_conf_error conf_error;
char* config_path;
success = true;
args_recover(&argc, &argv);
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
goto failure_usage;
}
}
config_path = NULL;
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "--config")) {
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) {
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_impl_misc);
}
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_impl_misc);
goto success;
failure_usage:
cconfig_cmd_print_usage(config, usage_header, cmd_usage_out);
failure:
success = false;
success:
args_free(argc, argv);
return success;
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef CCONFIG_HOOK_H
#define CCONFIG_HOOK_H
#include "cconfig/cconfig.h"
#include "cconfig/cmd.h"
bool cconfig_hook_config_init(struct cconfig* config, const char* usage_header,
enum cconfig_cmd_usage_out cmd_usage_out);
#endif
+211
View File
@@ -0,0 +1,211 @@
#define LOG_MODULE "cconfig-util"
#include <stdio.h>
#include <string.h>
#include "cconfig/cconfig-util.h"
#include "util/hex.h"
#include "util/log.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, log_formatter_t log_formatter)
{
for (uint32_t i = 0; i < config->nentries; i++) {
log_formatter(LOG_MODULE, "%s=%s", config->entries[i].key,
config->entries[i].value);
}
}
+44
View File
@@ -0,0 +1,44 @@
#ifndef CCONFIG_UTIL_H
#define CCONFIG_UTIL_H
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "cconfig/cconfig.h"
#include "util/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, log_formatter_t log_formatter);
#endif
+109
View File
@@ -0,0 +1,109 @@
#include <string.h>
#include "cconfig/cconfig.h"
#include "util/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);
}
+31
View File
@@ -0,0 +1,31 @@
#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
+132
View File
@@ -0,0 +1,132 @@
#define LOG_MODULE "cconfig-cmd"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "cconfig/cmd.h"
#include "util/hex.h"
#include "util/log.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);
}
}
+21
View File
@@ -0,0 +1,21 @@
#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
+117
View File
@@ -0,0 +1,117 @@
#define LOG_MODULE "cconfig-conf"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cconfig/conf.h"
#include "util/fs.h"
#include "util/log.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;
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;
}
}
pos_lines = strtok_r(data, "\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, "\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;
}
+16
View File
@@ -0,0 +1,16 @@
#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);