← All posts

How ODL %config is parsed: from text to parser->config

Grow through practice

No need to finish reading — test now

One line: %config is not “the data model”; it is the parser/runtime configuration table. It becomes key/value entries in parser->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 to AMXC_VAR_ID_HTABLE and adds the default include-dirs=["."];
  • amxrt_get_config() returns &rt.parser.config;
  • it is an in-memory hash table: string key → amxc_var_t;
  • amxc_var_t is 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 },
  ...
}
Valueamxc_var_t type
"hello"cstring
true / falsebool
10int32/uint32
[ ... ]list
{ ... }htable
  • amxo_parser_get_config(parser, "odl.dm-save") reads by path;
  • amxo_parser_set_config() uses amxc_var_set_path(..., AUTO_ADD), so a.b.c creates nested htables;
  • amxo_parser_clean() ends with amxc_var_clean(&parser->config);
  • amxrt -dconfig simply 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->config is not the data model. The data model is amxd_dm_t (objects/parameters); %config only affects the parser/runtime.

The parse path (code level)

  1. Lexer/grammar: amxo_parser.l recognizes %config as CONFIG; amxo_parser.y:
   CONFIG '{' config_options '}'
   config_option : path ASSIGN data ';'
  1. Action: amxo_parser_update_config(parser, path, data, op, global);
  1. 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 in parser->global_config;
  1. Read: amxo_parser_get_config(parser, "storage-path") → amxc_var_get_path(&parser->config, ...).
  • Nested paths like odl.dm-save work 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:

KeyExpanded
storage-path/etc/config/amx-scheduler/
definition_fileamx-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 $()

SyntaxSourceIf 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:

  1. amxrt defaults;
  1. -o name=value;
  1. ODL %config (can override -o);
  1. -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 in include-dirs;
  • import "x.so": searched in import-dirs;
  • both directories are themselves in %config; amxrt -I/-L append to them;
  • include parses recursively into the same parser, so %config in included files updates the same table.

Common pitfalls

  1. ${name} is not automatic: running amxrt directly gives name=amxrt; use -o name=xxx or install a xxx -> amxrt symlink (as cpu_info/amx-scheduler do).
  1. Undefined = empty string: ${typo} collapses paths to /...//..., hard to debug.
  1. %config creates no objects: it only affects the parser/runtime; objects live in %define.
  1. -o is not forced: ODL may override it; use -F to lock a value.
  1. storage-path may be a template: -dconfig shows 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 = a parser->config table; ${} 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.

Practice

Found this useful?

I write about AI agents × embedded / edge AI. Subscribe, or email me a concrete problem.

Guestbook →Subscribe (RSS)Support

Comments 0

No comments yet — be the first!

No login required; name is optional.