OpenWrt上UCI库的使用(C语言调用)

本文介绍了LUCI配置文件的结构,包括uci_package、uci_section和uci_option等结构体,并提供了uci_lookup_ptr、uci_alloc_context和uci_free_context等API的说明。通过代码实例展示了如何遍历配置文件、获取和设置option的值。内容适用于OpenWRT等系统中对LUCI配置文件的操作。

1 LUCI 配置文件简介

LUCI 的配置文件一般存储在 /etc/config 目录下。比如网络配置文件则是 /etc/config/network,无线的配置文件是 /etc/config/wireless。更多配置文件的含义参考官方 WIKI.

2 简单的基本关系

这里画一个图让大家大致了解配置文件的内容和 uci 的几个基本结构之间的对应关系。(例举文件为 uhttpd 的配置文件)
在这里插入图片描述

3 结构体介绍

  1. struct uci_package: 包结构体,它对应一个配置文件内容。

    struct uci_package
    {
         
         
        struct uci_element e;
        struct uci_list sections;
        struct uci_context *ctx;
        bool has_delta;
        char *path;
    
        /* private: */
        struct uci_backend *backend;
        void *priv;
        int n_section;
        struct uci_list delta;
        struct uci_list saved_delta;
    };
    
  2. struct uci_section:节结构体,它对应配置文件中的节。

    struct uci_section
    {
         
         
        struct uci_element e;
        struct uci_list options;
        struct uci_package *package;
        bool anonymous;
        char *type;
    };
    
  3. struct uci_option:选项结构体,它对应配置文件里节中的 option 或者 list。

    struct uci_option
    {
         
         
        struct uci_element e;
        struct uci_section *section;
        enum uci_option_type type;
        union {
         
         
            struct uci_list list;
            char *string;
        } v;
    };
    
  4. struct uci_ptr:元素位置指针结构,用来查询并保存对应位置元素。

    struct uci_ptr
    {
         
         
        enum uci_type target;
        enum {
         
         
            UCI_LOOKUP_DONE =    (1 << 0),
            UCI_LOOKUP_COMPLETE = (1 << 1),
            UCI_LOOKUP_EXTENDED = (1 << 2),
        } flags;
    
        struct uci_package *p;
        struct uci_section *s;
        struct uci_option *o;
        struct uci_element *last;
    
        const char *package;
        const char *section;
        const char *option;
        const char *value;
    };
    
  5. struct uci_context: uci 上下文结构,贯穿查询、更改配置文件全过程。

    struct uci_context
    {
         
         
        /* 配置文件包列表 */
        struct uci_list root;
    
        /* 解析上下文,只用于错误处理 */
        struct uci_parse_context *
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半砖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值