How ODL %config is parsed: from text to parser->config
Grow through practice
No need to finish reading — test nowOne line:
%configis not “the data model”; it is the parser/runtime configuration table. It becomes key/value entries inparser->config;${}reads from it,$()reads from environment variables.
What it becomes
amxrt -N -dconfig -O '%config { name = "hello"; }' prints the merged config (excerpt):
name = "hello"
prefix = ""
cfg-dir = "/etc/amx"
rw_data_path = "${prefix}/etc/config"
storage-path = "${rw_data_path}/${name}/"
include-dirs = [".", "${prefix}${cfg-dir}/${name}", ...]
import-dirs = [".", "${prefix}${plugin-dir}/${name}", ...]
backends = ["/usr/bin/mods/amxb/mod-amxb-ubus.so"]
uris = []
daemon = false
priority = 0
Note: storage-path is still a template (${...}), not an expanded path. %config only stores strings/booleans/numbers/nested htables; expansion happens when the value is used.
What exactly is parser->config?
It is not a file or a database — it is a field in the parser context:
struct _amxo_parser {
...
amxc_var_t config; /* htable: key -> variant */
...
};
amxo_parser_init()sets it toAMXC_VAR_ID_HTABLEand adds the defaultinclude-dirs=["."];amxrt_get_config()returns&rt.parser.config;- it is an in-memory hash table:
string key → amxc_var_t; amxc_var_tis a typed value: string / bool / int / double / list / htable;- so it looks like JSON:
{
name: "amx-scheduler",
daemon: false,
priority: 0,
include-dirs: [".", "/etc/amx/amx-scheduler"],
odl: { dm-save: true, dm-save-delay: 1000 },
...
}
| Value | amxc_var_t type |
|---|---|
"hello" | cstring |
true / false | bool |
10 | int32/uint32 |
[ ... ] | list |
{ ... } | htable |
amxo_parser_get_config(parser, "odl.dm-save")reads by path;amxo_parser_set_config()usesamxc_var_set_path(..., AUTO_ADD), soa.b.ccreates nested htables;amxo_parser_clean()ends withamxc_var_clean(&parser->config);amxrt -dconfigsimply dumps this table.
In C:
amxc_var_t* cfg = amxrt_get_config();
amxc_var_dump(cfg, STDOUT_FILENO);
amxc_var_t* v = amxo_parser_get_config(parser, "storage-path");
/* v is borrowed; do not free */
Note:
parser->configis not the data model. The data model isamxd_dm_t(objects/parameters);%configonly affects the parser/runtime.
The parse path (code level)
- Lexer/grammar:
amxo_parser.lrecognizes%configasCONFIG;amxo_parser.y:
CONFIG '{' config_options '}'
config_option : path ASSIGN data ';'
- Action:
amxo_parser_update_config(parser, path, data, op, global);
- Write:
=→amxo_parser_set_config()→amxc_var_set_path(&parser->config, path, value, UPDATE|COPY|AUTO_ADD);+=→amxo_parser_add_config()(append);%global→ also recorded inparser->global_config;
- Read:
amxo_parser_get_config(parser, "storage-path")→amxc_var_get_path(&parser->config, ...).
- Nested paths like
odl.dm-savework too.
A concrete example: amx-scheduler
%config {
storage-path = "${rw_data_path}/${name}/";
definition_file = "${name}_definition.odl";
defaults_dir = "${cfg-dir}/${name}/defaults/";
}
With name=amx-scheduler, rw_data_path=/etc/config, cfg-dir=/etc/amx, when used they expand to:
| Key | Expanded |
|---|---|
storage-path | /etc/config/amx-scheduler/ |
definition_file | amx-scheduler_definition.odl |
defaults_dir | /etc/amx/amx-scheduler/defaults/ |
But -dconfig still shows the raw templates — the table stores templates; expansion happens when include/import/amxrt consumes them.
${} vs $()
| Syntax | Source | If undefined |
|---|---|---|
${name} | parser->config (htable) | empty string |
$(HOME) | environment (getenv) | empty string |
Implementation:
amxc_string_resolve_env()handles$();amxc_string_resolve_var()handles${};amxc_string_resolve()loops over both until nothing changes.
So ${prefix}${cfg-dir}/${name} works; but order matters: the referenced key must already be in the table, otherwise it is empty.
Precedence
Low to high:
amxrtdefaults;
-o name=value;
- ODL
%config(can override-o);
-F name=value(forced; ODL cannot override).
Measured:
amxrt -N -dconfig -o name=from-o -O '%config { name = "from-odl"; }'
# name = "from-odl"
amxrt -N -dconfig -F name=from-F -O '%config { name = "from-odl"; }'
# name = "from-F"
How include / import use config
include "x.odl": relative paths are searched ininclude-dirs;import "x.so": searched inimport-dirs;- both directories are themselves in
%config;amxrt -I/-Lappend to them; includeparses recursively into the same parser, so%configin included files updates the same table.
Common pitfalls
${name}is not automatic: runningamxrtdirectly givesname=amxrt; use-o name=xxxor install axxx -> amxrtsymlink (ascpu_info/amx-schedulerdo).
- Undefined = empty string:
${typo}collapses paths to/...//..., hard to debug.
%configcreates no objects: it only affects the parser/runtime; objects live in%define.
-ois not forced: ODL may override it; use-Fto lock a value.
storage-pathmay be a template:-dconfigshows the raw string, not the expanded path.
Debugging
# see the merged config
timeout 3 amxrt -N -dconfig -O '%config { name = "hello"; }'
# see include/import resolution
# add: import-dbg = true; in the ODL
Read it in C:
amxc_var_t* v = amxo_parser_get_config(parser, "storage-path");
You get the template; expand it yourself with amxc_string_set_resolved().
One sentence
%config= aparser->configtable;${}reads from the table,$()from the environment, and expansion happens when used. Once you see this table, ODL paths/includes/imports stop being mysterious.
Related: How ODL is parsed · 02 amx-scheduler Series: AMX Official Examples.
Comments 0
No comments yet — be the first!