Add config module: INI loader with schema-driven defaults
Config_Def schema tables declare section/key/type/default per module. Typed getters: config_get_str, _u16, _u32, _flags. FLAGS type parses space/comma-separated tokens via a Config_Flag_Def table. config_defaults() gives schema defaults without a file. config_dump() prints effective values for diagnostics. config_cli: load a file or --defaults and dump effective config. dev/example.cfg: sample config covering node, discovery, transport. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
73
dev/cli/config_cli.c
Normal file
73
dev/cli/config_cli.c
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "discovery.h"
|
||||
#include "transport.h"
|
||||
#include "error.h"
|
||||
|
||||
/*
|
||||
* Example schema covering node identity, discovery, and transport.
|
||||
* Each module would normally provide its own schema table; they are
|
||||
* combined here for the CLI demo.
|
||||
*/
|
||||
|
||||
static const struct Config_Flag_Def function_flag_defs[] = {
|
||||
{ "source", DISCOVERY_FLAG_SOURCE },
|
||||
{ "relay", DISCOVERY_FLAG_RELAY },
|
||||
{ "sink", DISCOVERY_FLAG_SINK },
|
||||
{ "controller", DISCOVERY_FLAG_CONTROLLER },
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static const struct Config_Def schema[] = {
|
||||
/* section key type default */
|
||||
{ "node", "name", CONFIG_STRING, "unnamed:0", NULL },
|
||||
{ "node", "site_id", CONFIG_UINT16, "0", NULL },
|
||||
{ "node", "tcp_port", CONFIG_UINT16, "8000", NULL },
|
||||
{ "node", "function", CONFIG_FLAGS, "source", function_flag_defs },
|
||||
|
||||
{ "discovery", "interval_ms", CONFIG_UINT32, "5000", NULL },
|
||||
{ "discovery", "timeout_intervals", CONFIG_UINT32, "3", NULL },
|
||||
{ "discovery", "connect_mask", CONFIG_FLAGS, "", function_flag_defs },
|
||||
|
||||
{ "transport", "max_connections", CONFIG_UINT32, "16", NULL },
|
||||
{ "transport", "max_payload", CONFIG_UINT32, "16777216", NULL },
|
||||
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static void usage(void) {
|
||||
fprintf(stderr,
|
||||
"usage: config_cli <file>\n"
|
||||
" config_cli --defaults\n"
|
||||
"\n"
|
||||
" <file> load config file and print effective values\n"
|
||||
" --defaults print schema defaults without a file\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct Config *cfg;
|
||||
struct App_Error err;
|
||||
|
||||
if (argv[1][0] == '-') {
|
||||
err = config_defaults(&cfg, schema);
|
||||
} else {
|
||||
err = config_load(&cfg, argv[1], schema);
|
||||
}
|
||||
|
||||
if (!APP_IS_OK(err)) {
|
||||
fprintf(stderr, "config_load: errno %d\n", err.detail.syscall.err_no);
|
||||
return 1;
|
||||
}
|
||||
|
||||
config_dump(cfg);
|
||||
|
||||
config_free(cfg);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user