"Smart router" C code calls the UCI API read OpenWrt configuration file guide

Source: Internet
Author: User
Tags ocaml

"Smart Router" series article connection
http://blog.csdn.net/u012819339/article/category/5803489

The previous blog explains the use of UCI under the command line, this blog Arvik will be a simple analysis of the UCI part of the source code, leading you to use the C language to call the UCI API to read the configuration file.

Actual combat background

What if we wrote an application ourselves and would like to use UCI to centralize and manage configuration files for the application?
After reading Arvik's last blog, I believe that the novice can quickly use the UCI to configure a configuration file, just how to let our application read the configuration file content, this article Arvik will answer this question.

Simple diagram of basic relationships

Drawing a diagram here gives you an overview of the configuration file's content and the correspondence between several of the UCI's basic structures. (for example, a configuration file with the uhttpd file)

Several structural bodies

struct Uci_package: package structure. It corresponds to a configuration file content

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;};

struct uci_section: a section structure that corresponds to a section in a configuration file

struct uci_section{    struct uci_element e;    struct uci_list options;    struct uci_package *package;    bool anonymous;    char *type;};

struct uci_option: option struct, which corresponds to option or list in the section of the configuration file

struct uci_option{    struct uci_element e;    struct uci_section *section;    type;    union {        structlist;        char *string;    } v;};

struct Uci_ptr: element position pointer structure, used to query and save the corresponding position element

structuci_ptr{enumUci_type Target;enum{Uci_lookup_done = (1<<0), Uci_lookup_complete = (1<<1), uci_lookup_extended = (1<<2),} flags;structUci_package *p;structUci_section *s;structUci_option *o;structUci_element *last;Const Char*package;Const Char*section;Const Char*option;Const Char*value;};

struct Uci_context: UCI context structure, throughout the query, change the configuration file throughout the process.

structuci_context{/ * Configuration file Package list * /    structUci_list Root;/ * Parse context, only for error handling * /    structUci_parse_context *pctx;/ * Back-end import/export * /    structUci_backend *backend;structUci_list backends;/ * UCI run identity * /    enumUci_flags flags;Char*confdir;Char*savedir;/ * Search path for delta files * /    structUci_list Delta_path;/ * Private Data * /    intErrConst Char*func; JMP_BUF Trap;BOOL Internal, nested;Char*buf;intBufsz;};
Several basic API functions

Uci_alloc_context: Dynamic Request for a UCI context structure

struct uci_context *uci_alloc_context(void);

Uci_free_contex: Frees the UCI context structure requested by Uci_alloc_context and includes all of its data

void uci_free_context(struct uci_context *ctx);

Uci_lookup_ptr: Finding elements by a given tuple

/** * uci_lookup_ptr: Detach an UCI tuple string and find the corresponding element tree *  @ctx : UCI context struct pointer *  @ptr : struct pointer holding element query result *  @str : The UCI tuple string to find *  @extended : Allow extended Syntax query * * If extended is set to Ture, Uci_lookup_ PTR supports the following extended syntax: * * Example: * network.  @interface  [0].ifname (' ifname ' option of the first interface section) * Network.  @interface        [-1] (Last interface section) * Note: UCI_LOOKUP_PTR will automatically load the config package *  Cannot be a const type pointer, it will be changed during use and used to fill the string into  @ptr , so * it is as long as  @ptr  is still in use, it must be available * * This function returns Uci_err_notfound if a string of the specified package tuple is not found, otherwise returns UCI_OK * * Remember that when looking for other parts of the failure, if they are also specified, including section and option, the same will return UCI_OK, * but the ptr->flags * uci_lookup_complete flag bit will not be set */int uci_lookup_ptr (struct uci_context *ctx, struct uci_ptr *ptr, char *str, BOOL extended);

A single UCI standard profile can be read simply by the above 3 APIs.

Code Combat

Let's try to write an example code.

/***********************************author:arvikemail:[email protected]csdn:http://blog.csdn.net/u012819339 ************************************/#include <stdio.h>#include <string.h>#include <stdlib.h>#include "uci.h"intMain () {structUci_context *c;structUci_ptr p;Char*a = StrDup ("Arvik_testconfig.main.home"); c = Uci_alloc_context ();if(Uci_ok! = Uci_lookup_ptr (c, &p, A,true) {Uci_perror (c,"No found!\n");return-1; }printf("%s\n", P.o->v.string); Uci_free_context (c); Free(a);return(0);}

Effect:

root@OpenWrt:/# arvik_uci_test /www

Run:

"Smart Router" series article connection
http://blog.csdn.net/u012819339/article/category/5803489

"Smart router" C code calls the UCI API read OpenWrt configuration file guide

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.