uci.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * libuci - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef __LIBUCI_H
  15. #define __LIBUCI_H
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #include "uci_config.h"
  20. /*
  21. * you can use these defines to enable debugging behavior for
  22. * apps compiled against libuci:
  23. *
  24. * #define UCI_DEBUG_TYPECAST:
  25. * enable uci_element typecast checking at run time
  26. *
  27. */
  28. #include <stdbool.h>
  29. #include <setjmp.h>
  30. #include <stdio.h>
  31. #include <stdint.h>
  32. #define UCI_CONFDIR "/etc/config"
  33. #define UCI_SAVEDIR "/tmp/.uci"
  34. #define UCI_DIRMODE 0700
  35. #define UCI_FILEMODE 0600
  36. enum
  37. {
  38. UCI_OK = 0,
  39. UCI_ERR_MEM,
  40. UCI_ERR_INVAL,
  41. UCI_ERR_NOTFOUND,
  42. UCI_ERR_IO,
  43. UCI_ERR_PARSE,
  44. UCI_ERR_DUPLICATE,
  45. UCI_ERR_UNKNOWN,
  46. UCI_ERR_LAST
  47. };
  48. struct uci_list;
  49. struct uci_list
  50. {
  51. struct uci_list *next;
  52. struct uci_list *prev;
  53. };
  54. struct uci_ptr;
  55. struct uci_plugin;
  56. struct uci_hook_ops;
  57. struct uci_element;
  58. struct uci_package;
  59. struct uci_section;
  60. struct uci_option;
  61. struct uci_delta;
  62. struct uci_context;
  63. struct uci_backend;
  64. struct uci_parse_option;
  65. struct uci_parse_context;
  66. /**
  67. * uci_alloc_context: Allocate a new uci context
  68. */
  69. extern struct uci_context *uci_alloc_context(void);
  70. /**
  71. * uci_free_context: Free the uci context including all of its data
  72. */
  73. extern void uci_free_context(struct uci_context *ctx);
  74. /**
  75. * uci_perror: Print the last uci error that occured
  76. * @ctx: uci context
  77. * @str: string to print before the error message
  78. */
  79. extern void uci_perror(struct uci_context *ctx, const char *str);
  80. /**
  81. * uci_geterror: Get an error string for the last uci error
  82. * @ctx: uci context
  83. * @dest: target pointer for the string
  84. * @str: prefix for the error message
  85. *
  86. * Note: string must be freed by the caller
  87. */
  88. extern void uci_get_errorstr(struct uci_context *ctx, char **dest, const char *str);
  89. /**
  90. * uci_import: Import uci config data from a stream
  91. * @ctx: uci context
  92. * @stream: file stream to import from
  93. * @name: (optional) assume the config has the given name
  94. * @package: (optional) store the last parsed config package in this variable
  95. * @single: ignore the 'package' keyword and parse everything into a single package
  96. *
  97. * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
  98. * if 'package' points to a non-null struct pointer, enable delta tracking and merge
  99. */
  100. extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single);
  101. /**
  102. * uci_export: Export one or all uci config packages
  103. * @ctx: uci context
  104. * @stream: output stream
  105. * @package: (optional) uci config package to export
  106. * @header: include the package header
  107. */
  108. extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header);
  109. /**
  110. * uci_load: Parse an uci config file and store it in the uci context
  111. *
  112. * @ctx: uci context
  113. * @name: name of the config file (relative to the config directory)
  114. * @package: store the loaded config package in this variable
  115. */
  116. extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
  117. /**
  118. * uci_unload: Unload a config file from the uci context
  119. *
  120. * @ctx: uci context
  121. * @package: pointer to the uci_package struct
  122. */
  123. extern int uci_unload(struct uci_context *ctx, struct uci_package *p);
  124. /**
  125. * uci_lookup_ptr: Split an uci tuple string and look up an element tree
  126. * @ctx: uci context
  127. * @ptr: lookup result struct
  128. * @str: uci tuple string to look up
  129. * @extended: allow extended syntax lookup
  130. *
  131. * if extended is set to true, uci_lookup_ptr supports the following
  132. * extended syntax:
  133. *
  134. * Examples:
  135. * network.@interface[0].ifname ('ifname' option of the first interface section)
  136. * network.@interface[-1] (last interface section)
  137. * Note: uci_lookup_ptr will automatically load a config package if necessary
  138. * @str must not be constant, as it will be modified and used for the strings inside @ptr,
  139. * thus it must also be available as long as @ptr is in use.
  140. */
  141. extern int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended);
  142. /**
  143. * uci_add_section: Add an unnamed section
  144. * @ctx: uci context
  145. * @p: package to add the section to
  146. * @type: section type
  147. * @res: pointer to store a reference to the new section in
  148. */
  149. extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res);
  150. /**
  151. * uci_set: Set an element's value; create the element if necessary
  152. * @ctx: uci context
  153. * @ptr: uci pointer
  154. *
  155. * The updated/created element is stored in ptr->last
  156. */
  157. extern int uci_set(struct uci_context *ctx, struct uci_ptr *ptr);
  158. /**
  159. * uci_add_list: Append a string to an element list
  160. * @ctx: uci context
  161. * @ptr: uci pointer (with value)
  162. *
  163. * Note: if the given option already contains a string value,
  164. * it will be converted to an 1-element-list before appending the next element
  165. */
  166. extern int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr);
  167. /**
  168. * uci_reorder: Reposition a section
  169. * @ctx: uci context
  170. * @s: uci section to reposition
  171. * @pos: new position in the section list
  172. */
  173. extern int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos);
  174. /**
  175. * uci_rename: Rename an element
  176. * @ctx: uci context
  177. * @ptr: uci pointer (with value)
  178. */
  179. extern int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr);
  180. /**
  181. * uci_delete: Delete a section or option
  182. * @ctx: uci context
  183. * @ptr: uci pointer
  184. */
  185. extern int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr);
  186. /**
  187. * uci_save: save change delta for a package
  188. * @ctx: uci context
  189. * @p: uci_package struct
  190. */
  191. extern int uci_save(struct uci_context *ctx, struct uci_package *p);
  192. /**
  193. * uci_commit: commit changes to a package
  194. * @ctx: uci context
  195. * @p: uci_package struct pointer
  196. * @overwrite: overwrite existing config data and flush delta
  197. *
  198. * committing may reload the whole uci_package data,
  199. * the supplied pointer is updated accordingly
  200. */
  201. extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
  202. /**
  203. * uci_list_configs: List available uci config files
  204. * @ctx: uci context
  205. *
  206. * caller is responsible for freeing the allocated memory behind list
  207. */
  208. extern int uci_list_configs(struct uci_context *ctx, char ***list);
  209. /**
  210. * uci_set_savedir: override the default delta save directory
  211. * @ctx: uci context
  212. * @dir: directory name
  213. */
  214. extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
  215. /**
  216. * uci_set_savedir: override the default config storage directory
  217. * @ctx: uci context
  218. * @dir: directory name
  219. */
  220. extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
  221. /**
  222. * uci_add_delta_path: add a directory to the search path for change delta files
  223. * @ctx: uci context
  224. * @dir: directory name
  225. *
  226. * This function allows you to add directories, which contain 'overlays'
  227. * for the active config, that will never be committed.
  228. */
  229. extern int uci_add_delta_path(struct uci_context *ctx, const char *dir);
  230. /**
  231. * uci_revert: revert all changes to a config item
  232. * @ctx: uci context
  233. * @ptr: uci pointer
  234. */
  235. extern int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr);
  236. /**
  237. * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
  238. * @ctx: uci context
  239. * @stream: input stream
  240. * @str: pointer to the current line (use NULL for parsing the next line)
  241. * @result: pointer for the result
  242. */
  243. extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
  244. /**
  245. * uci_set_backend: change the default backend
  246. * @ctx: uci context
  247. * @name: name of the backend
  248. *
  249. * The default backend is "file", which uses /etc/config for config storage
  250. */
  251. extern int uci_set_backend(struct uci_context *ctx, const char *name);
  252. /**
  253. * uci_validate_text: validate a value string for uci options
  254. * @str: value
  255. *
  256. * this function checks whether a given string is acceptable as value
  257. * for uci options
  258. */
  259. extern bool uci_validate_text(const char *str);
  260. /**
  261. * uci_add_hook: add a uci hook
  262. * @ctx: uci context
  263. * @ops: uci hook ops
  264. *
  265. * NB: allocated and freed by the caller
  266. */
  267. extern int uci_add_hook(struct uci_context *ctx, const struct uci_hook_ops *ops);
  268. /**
  269. * uci_remove_hook: remove a uci hook
  270. * @ctx: uci context
  271. * @ops: uci hook ops
  272. */
  273. extern int uci_remove_hook(struct uci_context *ctx, const struct uci_hook_ops *ops);
  274. /**
  275. * uci_load_plugin: load an uci plugin
  276. * @ctx: uci context
  277. * @filename: path to the uci plugin
  278. *
  279. * NB: plugin will be unloaded automatically when the context is freed
  280. */
  281. int uci_load_plugin(struct uci_context *ctx, const char *filename);
  282. /**
  283. * uci_load_plugins: load all uci plugins from a directory
  284. * @ctx: uci context
  285. * @pattern: pattern of uci plugin files (optional)
  286. *
  287. * if pattern is NULL, then uci_load_plugins will call uci_load_plugin
  288. * for uci_*.so in <prefix>/lib/
  289. */
  290. int uci_load_plugins(struct uci_context *ctx, const char *pattern);
  291. /**
  292. * uci_parse_ptr: parse a uci string into a uci_ptr
  293. * @ctx: uci context
  294. * @ptr: target data structure
  295. * @str: string to parse
  296. *
  297. * str is modified by this function
  298. */
  299. int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str);
  300. /**
  301. * uci_lookup_next: lookup a child element
  302. * @ctx: uci context
  303. * @e: target element pointer
  304. * @list: list of elements
  305. * @name: name of the child element
  306. *
  307. * if parent is NULL, the function looks up the package with the given name
  308. */
  309. int uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name);
  310. /**
  311. * uci_parse_section: look up a set of options
  312. * @s: uci section
  313. * @opts: list of options to look up
  314. * @n_opts: number of options to look up
  315. * @tb: array of pointers to found options
  316. */
  317. void uci_parse_section(struct uci_section *s, const struct uci_parse_option *opts,
  318. int n_opts, struct uci_option **tb);
  319. /**
  320. * uci_hash_options: build a hash over a list of options
  321. * @tb: list of option pointers
  322. * @n_opts: number of options
  323. */
  324. uint32_t uci_hash_options(struct uci_option **tb, int n_opts);
  325. /* UCI data structures */
  326. enum uci_type {
  327. UCI_TYPE_UNSPEC = 0,
  328. UCI_TYPE_DELTA = 1,
  329. UCI_TYPE_PACKAGE = 2,
  330. UCI_TYPE_SECTION = 3,
  331. UCI_TYPE_OPTION = 4,
  332. UCI_TYPE_PATH = 5,
  333. UCI_TYPE_BACKEND = 6,
  334. UCI_TYPE_ITEM = 7,
  335. UCI_TYPE_HOOK = 8,
  336. UCI_TYPE_PLUGIN = 9,
  337. };
  338. enum uci_option_type {
  339. UCI_TYPE_STRING = 0,
  340. UCI_TYPE_LIST = 1,
  341. };
  342. enum uci_flags {
  343. UCI_FLAG_STRICT = (1 << 0), /* strict mode for the parser */
  344. UCI_FLAG_PERROR = (1 << 1), /* print parser error messages */
  345. UCI_FLAG_EXPORT_NAME = (1 << 2), /* when exporting, name unnamed sections */
  346. UCI_FLAG_SAVED_DELTA = (1 << 3), /* store the saved delta in memory as well */
  347. };
  348. struct uci_element
  349. {
  350. struct uci_list list;
  351. enum uci_type type;
  352. char *name;
  353. };
  354. struct uci_backend
  355. {
  356. struct uci_element e;
  357. char **(*list_configs)(struct uci_context *ctx);
  358. struct uci_package *(*load)(struct uci_context *ctx, const char *name);
  359. void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);
  360. /* private: */
  361. const void *ptr;
  362. void *priv;
  363. };
  364. struct uci_context
  365. {
  366. /* list of config packages */
  367. struct uci_list root;
  368. /* parser context, use for error handling only */
  369. struct uci_parse_context *pctx;
  370. /* backend for import and export */
  371. struct uci_backend *backend;
  372. struct uci_list backends;
  373. /* uci runtime flags */
  374. enum uci_flags flags;
  375. char *confdir;
  376. char *savedir;
  377. /* search path for delta files */
  378. struct uci_list delta_path;
  379. /* private: */
  380. int err;
  381. const char *func;
  382. jmp_buf trap;
  383. bool internal, nested;
  384. char *buf;
  385. int bufsz;
  386. struct uci_list hooks;
  387. struct uci_list plugins;
  388. };
  389. struct uci_package
  390. {
  391. struct uci_element e;
  392. struct uci_list sections;
  393. struct uci_context *ctx;
  394. bool has_delta;
  395. char *path;
  396. /* private: */
  397. struct uci_backend *backend;
  398. void *priv;
  399. int n_section;
  400. struct uci_list delta;
  401. struct uci_list saved_delta;
  402. };
  403. struct uci_section
  404. {
  405. struct uci_element e;
  406. struct uci_list options;
  407. struct uci_package *package;
  408. bool anonymous;
  409. char *type;
  410. };
  411. struct uci_option
  412. {
  413. struct uci_element e;
  414. struct uci_section *section;
  415. enum uci_option_type type;
  416. union {
  417. struct uci_list list;
  418. char *string;
  419. } v;
  420. };
  421. enum uci_command {
  422. UCI_CMD_ADD,
  423. UCI_CMD_REMOVE,
  424. UCI_CMD_CHANGE,
  425. UCI_CMD_RENAME,
  426. UCI_CMD_REORDER,
  427. UCI_CMD_LIST_ADD,
  428. };
  429. struct uci_delta
  430. {
  431. struct uci_element e;
  432. enum uci_command cmd;
  433. char *section;
  434. char *value;
  435. };
  436. struct uci_ptr
  437. {
  438. enum uci_type target;
  439. enum {
  440. UCI_LOOKUP_DONE = (1 << 0),
  441. UCI_LOOKUP_COMPLETE = (1 << 1),
  442. UCI_LOOKUP_EXTENDED = (1 << 2),
  443. } flags;
  444. struct uci_package *p;
  445. struct uci_section *s;
  446. struct uci_option *o;
  447. struct uci_element *last;
  448. const char *package;
  449. const char *section;
  450. const char *option;
  451. const char *value;
  452. };
  453. struct uci_hook_ops
  454. {
  455. void (*load)(const struct uci_hook_ops *ops, struct uci_package *p);
  456. void (*set)(const struct uci_hook_ops *ops, struct uci_package *p, struct uci_delta *e);
  457. };
  458. struct uci_hook
  459. {
  460. struct uci_element e;
  461. const struct uci_hook_ops *ops;
  462. };
  463. struct uci_plugin_ops
  464. {
  465. int (*attach)(struct uci_context *ctx);
  466. void (*detach)(struct uci_context *ctx);
  467. };
  468. struct uci_plugin
  469. {
  470. struct uci_element e;
  471. const struct uci_plugin_ops *ops;
  472. void *dlh;
  473. };
  474. struct uci_parse_option {
  475. const char *name;
  476. enum uci_option_type type;
  477. };
  478. /* linked list handling */
  479. #ifndef offsetof
  480. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  481. #endif
  482. /**
  483. * container_of - cast a member of a structure out to the containing structure
  484. * @ptr: the pointer to the member.
  485. * @type: the type of the container struct this is embedded in.
  486. * @member: the name of the member within the struct.
  487. */
  488. #ifndef container_of
  489. #define container_of(ptr, type, member) \
  490. ((type *) ((char *)ptr - offsetof(type,member)))
  491. #endif
  492. /**
  493. * uci_list_entry: casts an uci_list pointer to the containing struct.
  494. * @_type: config, section or option
  495. * @_ptr: pointer to the uci_list struct
  496. */
  497. #define list_to_element(ptr) \
  498. container_of(ptr, struct uci_element, list)
  499. /**
  500. * uci_foreach_entry: loop through a list of uci elements
  501. * @_list: pointer to the uci_list struct
  502. * @_ptr: iteration variable, struct uci_element
  503. *
  504. * use like a for loop, e.g:
  505. * uci_foreach(&list, p) {
  506. * ...
  507. * }
  508. */
  509. #define uci_foreach_element(_list, _ptr) \
  510. for(_ptr = list_to_element((_list)->next); \
  511. &_ptr->list != (_list); \
  512. _ptr = list_to_element(_ptr->list.next))
  513. /**
  514. * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
  515. * @_list: pointer to the uci_list struct
  516. * @_tmp: temporary variable, struct uci_element *
  517. * @_ptr: iteration variable, struct uci_element *
  518. *
  519. * use like a for loop, e.g:
  520. * uci_foreach(&list, p) {
  521. * ...
  522. * }
  523. */
  524. #define uci_foreach_element_safe(_list, _tmp, _ptr) \
  525. for(_ptr = list_to_element((_list)->next), \
  526. _tmp = list_to_element(_ptr->list.next); \
  527. &_ptr->list != (_list); \
  528. _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
  529. /**
  530. * uci_list_empty: returns true if a list is empty
  531. * @list: list head
  532. */
  533. #define uci_list_empty(list) ((list)->next == (list))
  534. /* wrappers for dynamic type handling */
  535. #define uci_type_backend UCI_TYPE_BACKEND
  536. #define uci_type_delta UCI_TYPE_DELTA
  537. #define uci_type_package UCI_TYPE_PACKAGE
  538. #define uci_type_section UCI_TYPE_SECTION
  539. #define uci_type_option UCI_TYPE_OPTION
  540. #define uci_type_hook UCI_TYPE_HOOK
  541. #define uci_type_plugin UCI_TYPE_PLUGIN
  542. /* element typecasting */
  543. #ifdef UCI_DEBUG_TYPECAST
  544. static const char *uci_typestr[] = {
  545. [uci_type_backend] = "backend",
  546. [uci_type_delta] = "delta",
  547. [uci_type_package] = "package",
  548. [uci_type_section] = "section",
  549. [uci_type_option] = "option",
  550. [uci_type_hook] = "hook",
  551. [uci_type_plugin] = "plugin",
  552. };
  553. static void uci_typecast_error(int from, int to)
  554. {
  555. fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
  556. }
  557. #define BUILD_CAST(_type) \
  558. static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
  559. { \
  560. if (e->type != uci_type_ ## _type) { \
  561. uci_typecast_error(e->type, uci_type_ ## _type); \
  562. } \
  563. return (struct uci_ ## _type *) e; \
  564. }
  565. BUILD_CAST(backend)
  566. BUILD_CAST(delta)
  567. BUILD_CAST(package)
  568. BUILD_CAST(section)
  569. BUILD_CAST(option)
  570. BUILD_CAST(hook)
  571. BUILD_CAST(plugin)
  572. #else
  573. #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
  574. #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
  575. #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
  576. #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
  577. #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
  578. #define uci_to_hook(ptr) container_of(ptr, struct uci_hook, e)
  579. #define uci_to_plugin(ptr) container_of(ptr, struct uci_plugin, e)
  580. #endif
  581. /**
  582. * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
  583. * @ctx: uci context
  584. * @type: {package,section,option}
  585. * @name: string containing the name of the element
  586. * @datasize: additional buffer size to reserve at the end of the struct
  587. */
  588. #define uci_alloc_element(ctx, type, name, datasize) \
  589. uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
  590. #define uci_dataptr(ptr) \
  591. (((char *) ptr) + sizeof(*ptr))
  592. /**
  593. * uci_lookup_package: look up a package
  594. * @ctx: uci context
  595. * @name: name of the package
  596. */
  597. static inline struct uci_package *
  598. uci_lookup_package(struct uci_context *ctx, const char *name)
  599. {
  600. struct uci_element *e = NULL;
  601. if (uci_lookup_next(ctx, &e, &ctx->root, name) == 0)
  602. return uci_to_package(e);
  603. else
  604. return NULL;
  605. }
  606. /**
  607. * uci_lookup_section: look up a section
  608. * @ctx: uci context
  609. * @p: package that the section belongs to
  610. * @name: name of the section
  611. */
  612. static inline struct uci_section *
  613. uci_lookup_section(struct uci_context *ctx, struct uci_package *p, const char *name)
  614. {
  615. struct uci_element *e = NULL;
  616. if (uci_lookup_next(ctx, &e, &p->sections, name) == 0)
  617. return uci_to_section(e);
  618. else
  619. return NULL;
  620. }
  621. /**
  622. * uci_lookup_option: look up an option
  623. * @ctx: uci context
  624. * @section: section that the option belongs to
  625. * @name: name of the option
  626. */
  627. static inline struct uci_option *
  628. uci_lookup_option(struct uci_context *ctx, struct uci_section *s, const char *name)
  629. {
  630. struct uci_element *e = NULL;
  631. if (uci_lookup_next(ctx, &e, &s->options, name) == 0)
  632. return uci_to_option(e);
  633. else
  634. return NULL;
  635. }
  636. static inline const char *
  637. uci_lookup_option_string(struct uci_context *ctx, struct uci_section *s, const char *name)
  638. {
  639. struct uci_option *o;
  640. o = uci_lookup_option(ctx, s, name);
  641. if (!o || o->type != UCI_TYPE_STRING)
  642. return NULL;
  643. return o->v.string;
  644. }
  645. #ifdef __cplusplus
  646. }
  647. #endif
  648. #endif