← 全部文章

ODL 的 %config 到底怎么解析:从文本到 parser->config

在练习中成长

不用等读完,先测一下

一句话:%config 不是“数据模型”,是“解析器/运行时的配置表”。 它最终变成 parser->config 里的键值;${} 从这里取值,$() 从环境变量取值。

先看它变成了什么

amxrt -N -dconfig -O '%config { name = "hello"; }' 会打印合并后的配置表(节选):

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

注意:storage-path 在表里还是模板(${...}),不是展开后的路径。 %config 只负责存字符串/布尔/数字/嵌套 htable;展开发生在“使用它”的时候。

parser->config 到底是什么

它不是文件、不是数据库,是解析器上下文里的一个字段:

struct _amxo_parser {
    ...
    amxc_var_t config;   /* htable: key -> variant */
    ...
};
  • amxo_parser_init() 把它设成 AMXC_VAR_ID_HTABLE,并放入默认 include-dirs=["."];
  • amxrt_get_config() 返回 &rt.parser.config;
  • 它是一张内存哈希表:string key → amxc_var_t;
  • amxc_var_t 是带类型的值:string / bool / int / double / list / htable;
  • 所以它长得像 JSON:
{
  name: "amx-scheduler",
  daemon: false,
  priority: 0,
  include-dirs: [".", "/etc/amx/amx-scheduler"],
  odl: { dm-save: true, dm-save-delay: 1000 },
  ...
}
值amxc_var_t 类型
"hello"cstring
true / falsebool
10int32/uint32
[ ... ]list
{ ... }htable
  • amxo_parser_get_config(parser, "odl.dm-save") 用路径取;
  • amxo_parser_set_config() 用 amxc_var_set_path(..., AUTO_ADD),所以 a.b.c 会自动建嵌套 htable;
  • amxo_parser_clean() 最后 amxc_var_clean(&parser->config);
  • amxrt -dconfig 就是把这张表 dump 出来。

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 是表里的借用指针,不要 free */

注意:parser->config 不是数据模型。数据模型是 amxd_dm_t(对象/参数);%config 只影响解析器/运行时。

解析路径(代码级)

  1. 词法/语法:amxo_parser.l 把 %config 识别成 CONFIG;amxo_parser.y:
   CONFIG '{' config_options '}'
   config_option : path ASSIGN data ';'
  1. 动作:amxo_parser_update_config(parser, path, data, op, global);
  1. 写入:
  • = → amxo_parser_set_config() → amxc_var_set_path(&parser->config, path, value, UPDATE|COPY|AUTO_ADD);
  • += → amxo_parser_add_config()(追加);
  • %global → 额外记到 parser->global_config;
  1. 读取:amxo_parser_get_config(parser, "storage-path") → amxc_var_get_path(&parser->config, ...)。
  • 支持嵌套:odl.dm-save 这种路径也能取。

一个具体例子:amx-scheduler

%config {
    storage-path = "${rw_data_path}/${name}/";
    definition_file = "${name}_definition.odl";
    defaults_dir = "${cfg-dir}/${name}/defaults/";
}

假设 name=amx-scheduler、rw_data_path=/etc/config、cfg-dir=/etc/amx,使用时展开为:

键展开后
storage-path/etc/config/amx-scheduler/
definition_fileamx-scheduler_definition.odl
defaults_dir/etc/amx/amx-scheduler/defaults/

但 -dconfig 里仍然显示原始模板——因为表里存的就是模板,展开发生在 include/import/amxrt 使用它时。

${} 和 $() 的区别

写法来源未定义时
${name}parser->config(htable)空串
$(HOME)环境变量(getenv)空串

实现:

  • amxc_string_resolve_env() 处理 $();
  • amxc_string_resolve_var() 处理 ${};
  • amxc_string_resolve() 循环两者,直到没有变化。

所以 ${prefix}${cfg-dir}/${name} 这种拼接也能工作;但顺序重要:被引用的键必须已经在表里,否则就是空串。

覆盖顺序

从低到高:

  1. amxrt 的默认值;
  1. -o name=value;
  1. ODL 的 %config(可以覆盖 -o);
  1. -F name=value(强制,ODL 覆盖不了)。

实测:

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"

include / import 怎么用 config

  • include "x.odl":相对路径在 include-dirs 里找;
  • import "x.so":在 import-dirs 里找;
  • 这两个目录本身也在 %config 里,amxrt -I/-L 会往列表里加。
  • include 是递归解析到同一个 parser,所以被 include 文件里的 %config 也会更新同一张表。

常见坑

  1. ${name} 不是自动的:直接跑 amxrt 时,name 默认是 amxrt;要用 -o name=xxx 或安装成 xxx -> amxrt 的软链(cpu_info/amx-scheduler 都这么做)。
  1. 未定义 = 空串:${typ o} 会把路径变成 /...//...,很难查。
  1. %config 不创建对象:它只影响解析器/运行时;对象在 %define 里。
  1. -o 不是强制:ODL 可以覆盖;要锁死用 -F。
  1. storage-path 等值可能是模板:-dconfig 看到的是原始字符串,不是展开后的路径。

调试

# 看合并后的配置
timeout 3 amxrt -N -dconfig -O '%config { name = "hello"; }'

# 看 include/import 解析
# 在 ODL 里加:import-dbg = true;

C 里读:

amxc_var_t* v = amxo_parser_get_config(parser, "storage-path");

拿到的是模板;需要展开就自己用 amxc_string_set_resolved()。

一句话

%config = 一张 parser->config 表;${} 从表里取,$() 从环境取,使用时才展开。 看懂这张表,ODL 的路径/包含/导入就都不神秘了。


相关:ODL 是怎么解析的? · 02 amx-scheduler 系列:prplOS 与 AMX。

本页练习

觉得有用?

我持续记录 AI Agent × 嵌入式 / 边缘 AI 的实践。订阅更新,或来邮件交流一个具体问题。

去留言板 →订阅 RSS赞助

评论 0

还没有评论,来抢沙发~

无需登录即可评论;昵称可选。