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 / false | bool |
10 | int32/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只影响解析器/运行时。
解析路径(代码级)
- 词法/语法:
amxo_parser.l把%config识别成CONFIG;amxo_parser.y:
CONFIG '{' config_options '}'
config_option : path ASSIGN data ';'
- 动作:
amxo_parser_update_config(parser, path, data, op, global);
- 写入:
=→amxo_parser_set_config()→amxc_var_set_path(&parser->config, path, value, UPDATE|COPY|AUTO_ADD);+=→amxo_parser_add_config()(追加);%global→ 额外记到parser->global_config;
- 读取:
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_file | amx-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} 这种拼接也能工作;但顺序重要:被引用的键必须已经在表里,否则就是空串。
覆盖顺序
从低到高:
amxrt的默认值;
-o name=value;
- ODL 的
%config(可以覆盖-o);
-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也会更新同一张表。
常见坑
${name}不是自动的:直接跑amxrt时,name默认是amxrt;要用-o name=xxx或安装成xxx -> amxrt的软链(cpu_info/amx-scheduler都这么做)。
- 未定义 = 空串:
${typ o}会把路径变成/...//...,很难查。
%config不创建对象:它只影响解析器/运行时;对象在%define里。
-o不是强制:ODL 可以覆盖;要锁死用-F。
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。
评论 0
还没有评论,来抢沙发~