uci.h 14 KB

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