uci.h 17 KB

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