uci.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. #include "uci_config.h"
  17. /*
  18. * you can use these defines to enable debugging behavior for
  19. * apps compiled against libuci:
  20. *
  21. * #define UCI_DEBUG_TYPECAST:
  22. * enable uci_element typecast checking at run time
  23. *
  24. */
  25. #include <stdbool.h>
  26. #include <setjmp.h>
  27. #include <stdio.h>
  28. #define UCI_CONFDIR "/etc/config"
  29. #define UCI_SAVEDIR "/tmp/.uci"
  30. #define UCI_FILEMODE 0600
  31. enum
  32. {
  33. UCI_OK = 0,
  34. UCI_ERR_MEM,
  35. UCI_ERR_INVAL,
  36. UCI_ERR_NOTFOUND,
  37. UCI_ERR_IO,
  38. UCI_ERR_PARSE,
  39. UCI_ERR_DUPLICATE,
  40. UCI_ERR_UNKNOWN,
  41. UCI_ERR_LAST
  42. };
  43. struct uci_list;
  44. struct uci_list
  45. {
  46. struct uci_list *next;
  47. struct uci_list *prev;
  48. };
  49. struct uci_element;
  50. struct uci_package;
  51. struct uci_section;
  52. struct uci_option;
  53. struct uci_history;
  54. struct uci_context;
  55. struct uci_backend;
  56. struct uci_parse_context;
  57. /**
  58. * uci_parse_tuple: Parse an uci tuple
  59. * @ctx: uci context
  60. * @str: input string
  61. * @package: output package pointer
  62. * @section: output section pointer
  63. * @option: output option pointer
  64. * @value: output value pointer
  65. *
  66. * format: <package>[.<section>[.<option>]][=<value>]
  67. */
  68. extern int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value);
  69. /**
  70. * uci_alloc_context: Allocate a new uci context
  71. */
  72. extern struct uci_context *uci_alloc_context(void);
  73. /**
  74. * uci_free_context: Free the uci context including all of its data
  75. */
  76. extern void uci_free_context(struct uci_context *ctx);
  77. /**
  78. * uci_perror: Print the last uci error that occured
  79. * @ctx: uci context
  80. * @str: string to print before the error message
  81. */
  82. extern void uci_perror(struct uci_context *ctx, const char *str);
  83. /**
  84. * uci_import: Import uci config data from a stream
  85. * @ctx: uci context
  86. * @stream: file stream to import from
  87. * @name: (optional) assume the config has the given name
  88. * @package: (optional) store the last parsed config package in this variable
  89. * @single: ignore the 'package' keyword and parse everything into a single package
  90. *
  91. * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
  92. * if 'package' points to a non-null struct pointer, enable history tracking and merge
  93. */
  94. extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single);
  95. /**
  96. * uci_export: Export one or all uci config packages
  97. * @ctx: uci context
  98. * @stream: output stream
  99. * @package: (optional) uci config package to export
  100. * @header: include the package header
  101. */
  102. extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header);
  103. /**
  104. * uci_load: Parse an uci config file and store it in the uci context
  105. *
  106. * @ctx: uci context
  107. * @name: name of the config file (relative to the config directory)
  108. * @package: store the loaded config package in this variable
  109. */
  110. extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
  111. /**
  112. * uci_unload: Unload a config file from the uci context
  113. *
  114. * @ctx: uci context
  115. * @package: pointer to the uci_package struct
  116. */
  117. extern int uci_unload(struct uci_context *ctx, struct uci_package *p);
  118. /**
  119. * uci_lookup: Look up an uci element
  120. *
  121. * @ctx: uci context
  122. * @res: where to store the result
  123. * @package: uci_package struct
  124. * @section: config section (optional)
  125. * @option: option to search for (optional)
  126. *
  127. * If section is omitted, then a pointer to the config package is returned
  128. * If option is omitted, then a pointer to the config section is returned
  129. */
  130. extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *package, char *section, char *option);
  131. /**
  132. * uci_add_section: Add an unnamed section
  133. * @ctx: uci context
  134. * @p: package to add the section to
  135. * @type: section type
  136. * @res: pointer to store a reference to the new section in
  137. */
  138. extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, char *type, struct uci_section **res);
  139. /**
  140. * uci_set_element_value: Replace an element's value with a new one
  141. * @ctx: uci context
  142. * @element: pointer to an uci_element struct pointer
  143. * @value: new value
  144. *
  145. * Only valid for uci_option and uci_section. Will replace the type string
  146. * when used with an uci_section
  147. */
  148. extern int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value);
  149. /**
  150. * uci_set: Set an element's value; create the element if necessary
  151. * @ctx: uci context
  152. * @package: package name
  153. * @section: section name
  154. * @option: option name
  155. * @value: value (option) or type (section)
  156. * @result: store the updated element in this variable (optional)
  157. */
  158. extern int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value, struct uci_element **result);
  159. /**
  160. * uci_rename: Rename an element
  161. * @ctx: uci context
  162. * @package: package name
  163. * @section: section name
  164. * @option: option name
  165. * @name: new name
  166. */
  167. extern int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name);
  168. /**
  169. * uci_delete_element: Delete a section or option
  170. * @ctx: uci context
  171. * @e: element (section or option)
  172. */
  173. extern int uci_delete_element(struct uci_context *ctx, struct uci_element *e);
  174. /**
  175. * uci_delete: Delete a section or option
  176. * @ctx: uci context
  177. * @p: uci package
  178. * @section: section name
  179. * @option: option name (optional)
  180. */
  181. extern int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option);
  182. /**
  183. * uci_save: save change history for a package
  184. * @ctx: uci context
  185. * @p: uci_package struct
  186. */
  187. extern int uci_save(struct uci_context *ctx, struct uci_package *p);
  188. /**
  189. * uci_commit: commit changes to a package
  190. * @ctx: uci context
  191. * @p: uci_package struct pointer
  192. * @overwrite: overwrite existing config data and flush history
  193. *
  194. * committing may reload the whole uci_package data,
  195. * the supplied pointer is updated accordingly
  196. */
  197. extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
  198. /**
  199. * uci_list_configs: List available uci config files
  200. * @ctx: uci context
  201. *
  202. * caller is responsible for freeing the allocated memory behind list
  203. */
  204. extern int uci_list_configs(struct uci_context *ctx, char ***list);
  205. /**
  206. * uci_set_savedir: override the default history save directory
  207. * @ctx: uci context
  208. * @dir: directory name
  209. */
  210. extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
  211. /**
  212. * uci_set_savedir: override the default config storage directory
  213. * @ctx: uci context
  214. * @dir: directory name
  215. */
  216. extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
  217. /**
  218. * uci_add_history_path: add a directory to the search path for change history files
  219. * @ctx: uci context
  220. * @dir: directory name
  221. *
  222. * This function allows you to add directories, which contain 'overlays'
  223. * for the active config, that will never be committed.
  224. */
  225. extern int uci_add_history_path(struct uci_context *ctx, const char *dir);
  226. /**
  227. * uci_revert: revert all changes to a config item
  228. * @ctx: uci context
  229. * @p: pointer to a uci_package struct ptr (will be replaced by the revert)
  230. * @section: section name (optional)
  231. * @option option name (optional)
  232. */
  233. extern int uci_revert(struct uci_context *ctx, struct uci_package **p, char *section, char *option);
  234. /**
  235. * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
  236. * @ctx: uci context
  237. * @stream: input stream
  238. * @str: pointer to the current line (use NULL for parsing the next line)
  239. * @result: pointer for the result
  240. */
  241. extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
  242. /**
  243. * uci_set_backend: change the default backend
  244. * @ctx: uci context
  245. * @name: name of the backend
  246. *
  247. * The default backend is "file", which uses /etc/config for config storage
  248. */
  249. extern int uci_set_backend(struct uci_context *ctx, const char *name);
  250. /* UCI data structures */
  251. enum uci_type {
  252. UCI_TYPE_HISTORY = 0,
  253. UCI_TYPE_PACKAGE = 1,
  254. UCI_TYPE_SECTION = 2,
  255. UCI_TYPE_OPTION = 3,
  256. UCI_TYPE_PATH = 4,
  257. UCI_TYPE_BACKEND = 5,
  258. };
  259. enum uci_flags {
  260. UCI_FLAG_STRICT = (1 << 0), /* strict mode for the parser */
  261. UCI_FLAG_PERROR = (1 << 1), /* print parser error messages */
  262. UCI_FLAG_EXPORT_NAME = (1 << 2), /* when exporting, name unnamed sections */
  263. UCI_FLAG_SAVED_HISTORY = (1 << 3), /* store the saved history in memory as well */
  264. };
  265. struct uci_element
  266. {
  267. struct uci_list list;
  268. enum uci_type type;
  269. char *name;
  270. };
  271. struct uci_backend
  272. {
  273. struct uci_element e;
  274. char **(*list_configs)(struct uci_context *ctx);
  275. struct uci_package *(*load)(struct uci_context *ctx, const char *name);
  276. void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);
  277. /* private: */
  278. const void *ptr;
  279. void *priv;
  280. };
  281. struct uci_context
  282. {
  283. /* list of config packages */
  284. struct uci_list root;
  285. /* parser context, use for error handling only */
  286. struct uci_parse_context *pctx;
  287. /* backend for import and export */
  288. struct uci_backend *backend;
  289. struct uci_list backends;
  290. /* uci runtime flags */
  291. enum uci_flags flags;
  292. char *confdir;
  293. char *savedir;
  294. /* search path for history files */
  295. struct uci_list history_path;
  296. /* private: */
  297. int errno;
  298. const char *func;
  299. jmp_buf trap;
  300. bool internal;
  301. char *buf;
  302. int bufsz;
  303. };
  304. struct uci_package
  305. {
  306. struct uci_element e;
  307. struct uci_list sections;
  308. struct uci_context *ctx;
  309. bool has_history;
  310. char *path;
  311. /* private: */
  312. struct uci_backend *backend;
  313. void *priv;
  314. int n_section;
  315. struct uci_list history;
  316. struct uci_list saved_history;
  317. };
  318. struct uci_section
  319. {
  320. struct uci_element e;
  321. struct uci_list options;
  322. struct uci_package *package;
  323. bool anonymous;
  324. char *type;
  325. };
  326. struct uci_option
  327. {
  328. struct uci_element e;
  329. struct uci_section *section;
  330. char *value;
  331. };
  332. enum uci_command {
  333. UCI_CMD_ADD,
  334. UCI_CMD_REMOVE,
  335. UCI_CMD_CHANGE,
  336. UCI_CMD_RENAME
  337. };
  338. struct uci_history
  339. {
  340. struct uci_element e;
  341. enum uci_command cmd;
  342. char *section;
  343. char *value;
  344. };
  345. /* linked list handling */
  346. #ifndef offsetof
  347. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  348. #endif
  349. /**
  350. * container_of - cast a member of a structure out to the containing structure
  351. * @ptr: the pointer to the member.
  352. * @type: the type of the container struct this is embedded in.
  353. * @member: the name of the member within the struct.
  354. */
  355. #define container_of(ptr, type, member) \
  356. ((type *) ((char *)ptr - offsetof(type,member)))
  357. /**
  358. * uci_list_entry: casts an uci_list pointer to the containing struct.
  359. * @_type: config, section or option
  360. * @_ptr: pointer to the uci_list struct
  361. */
  362. #define list_to_element(ptr) \
  363. container_of(ptr, struct uci_element, list)
  364. /**
  365. * uci_foreach_entry: loop through a list of uci elements
  366. * @_list: pointer to the uci_list struct
  367. * @_ptr: iteration variable, struct uci_element
  368. *
  369. * use like a for loop, e.g:
  370. * uci_foreach(&list, p) {
  371. * ...
  372. * }
  373. */
  374. #define uci_foreach_element(_list, _ptr) \
  375. for(_ptr = list_to_element((_list)->next); \
  376. &_ptr->list != (_list); \
  377. _ptr = list_to_element(_ptr->list.next))
  378. /**
  379. * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
  380. * @_list: pointer to the uci_list struct
  381. * @_tmp: temporary variable, struct uci_element *
  382. * @_ptr: iteration variable, struct uci_element *
  383. *
  384. * use like a for loop, e.g:
  385. * uci_foreach(&list, p) {
  386. * ...
  387. * }
  388. */
  389. #define uci_foreach_element_safe(_list, _tmp, _ptr) \
  390. for(_ptr = list_to_element((_list)->next), \
  391. _tmp = list_to_element(_ptr->list.next); \
  392. &_ptr->list != (_list); \
  393. _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
  394. /**
  395. * uci_list_empty: returns true if a list is empty
  396. * @list: list head
  397. */
  398. #define uci_list_empty(list) ((list)->next == (list))
  399. /* wrappers for dynamic type handling */
  400. #define uci_type_backend UCI_TYPE_BACKEND
  401. #define uci_type_history UCI_TYPE_HISTORY
  402. #define uci_type_package UCI_TYPE_PACKAGE
  403. #define uci_type_section UCI_TYPE_SECTION
  404. #define uci_type_option UCI_TYPE_OPTION
  405. /* element typecasting */
  406. #ifdef UCI_DEBUG_TYPECAST
  407. static const char *uci_typestr[] = {
  408. [uci_type_backend] = "backend",
  409. [uci_type_history] = "history",
  410. [uci_type_package] = "package",
  411. [uci_type_section] = "section",
  412. [uci_type_option] = "option",
  413. };
  414. static void uci_typecast_error(int from, int to)
  415. {
  416. fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
  417. }
  418. #define BUILD_CAST(_type) \
  419. static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
  420. { \
  421. if (e->type != uci_type_ ## _type) { \
  422. uci_typecast_error(e->type, uci_type_ ## _type); \
  423. } \
  424. return (struct uci_ ## _type *) e; \
  425. }
  426. BUILD_CAST(backend)
  427. BUILD_CAST(history)
  428. BUILD_CAST(package)
  429. BUILD_CAST(section)
  430. BUILD_CAST(option)
  431. #else
  432. #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
  433. #define uci_to_history(ptr) container_of(ptr, struct uci_history, e)
  434. #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
  435. #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
  436. #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
  437. #endif
  438. /**
  439. * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
  440. * @ctx: uci context
  441. * @type: {package,section,option}
  442. * @name: string containing the name of the element
  443. * @datasize: additional buffer size to reserve at the end of the struct
  444. */
  445. #define uci_alloc_element(ctx, type, name, datasize) \
  446. uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
  447. #define uci_dataptr(ptr) \
  448. (((char *) ptr) + sizeof(*ptr))
  449. #endif