uci.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. };
  278. struct uci_context
  279. {
  280. /* list of config packages */
  281. struct uci_list root;
  282. /* parser context, use for error handling only */
  283. struct uci_parse_context *pctx;
  284. /* backend for import and export */
  285. struct uci_backend *backend;
  286. struct uci_list backends;
  287. /* uci runtime flags */
  288. enum uci_flags flags;
  289. char *confdir;
  290. char *savedir;
  291. /* search path for history files */
  292. struct uci_list history_path;
  293. /* private: */
  294. int errno;
  295. const char *func;
  296. jmp_buf trap;
  297. bool internal;
  298. char *buf;
  299. int bufsz;
  300. };
  301. struct uci_package
  302. {
  303. struct uci_element e;
  304. struct uci_list sections;
  305. struct uci_context *ctx;
  306. bool has_history;
  307. char *path;
  308. /* private: */
  309. struct uci_backend *backend;
  310. void *priv;
  311. int n_section;
  312. struct uci_list history;
  313. struct uci_list saved_history;
  314. };
  315. struct uci_section
  316. {
  317. struct uci_element e;
  318. struct uci_list options;
  319. struct uci_package *package;
  320. bool anonymous;
  321. char *type;
  322. };
  323. struct uci_option
  324. {
  325. struct uci_element e;
  326. struct uci_section *section;
  327. char *value;
  328. };
  329. enum uci_command {
  330. UCI_CMD_ADD,
  331. UCI_CMD_REMOVE,
  332. UCI_CMD_CHANGE,
  333. UCI_CMD_RENAME
  334. };
  335. struct uci_history
  336. {
  337. struct uci_element e;
  338. enum uci_command cmd;
  339. char *section;
  340. char *value;
  341. };
  342. #define UCI_BACKEND(_var, _name, ...) \
  343. struct uci_backend _var = { \
  344. .e.list = { \
  345. .next = &_var.e.list, \
  346. .prev = &_var.e.list, \
  347. }, \
  348. .e.name = _name, \
  349. .e.type = UCI_TYPE_BACKEND, \
  350. __VA_ARGS__ \
  351. }
  352. /* linked list handling */
  353. #ifndef offsetof
  354. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  355. #endif
  356. /**
  357. * container_of - cast a member of a structure out to the containing structure
  358. * @ptr: the pointer to the member.
  359. * @type: the type of the container struct this is embedded in.
  360. * @member: the name of the member within the struct.
  361. */
  362. #define container_of(ptr, type, member) \
  363. ((type *) ((char *)ptr - offsetof(type,member)))
  364. /**
  365. * uci_list_entry: casts an uci_list pointer to the containing struct.
  366. * @_type: config, section or option
  367. * @_ptr: pointer to the uci_list struct
  368. */
  369. #define list_to_element(ptr) \
  370. container_of(ptr, struct uci_element, list)
  371. /**
  372. * uci_foreach_entry: loop through a list of uci elements
  373. * @_list: pointer to the uci_list struct
  374. * @_ptr: iteration variable, struct uci_element
  375. *
  376. * use like a for loop, e.g:
  377. * uci_foreach(&list, p) {
  378. * ...
  379. * }
  380. */
  381. #define uci_foreach_element(_list, _ptr) \
  382. for(_ptr = list_to_element((_list)->next); \
  383. &_ptr->list != (_list); \
  384. _ptr = list_to_element(_ptr->list.next))
  385. /**
  386. * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
  387. * @_list: pointer to the uci_list struct
  388. * @_tmp: temporary variable, struct uci_element *
  389. * @_ptr: iteration variable, struct uci_element *
  390. *
  391. * use like a for loop, e.g:
  392. * uci_foreach(&list, p) {
  393. * ...
  394. * }
  395. */
  396. #define uci_foreach_element_safe(_list, _tmp, _ptr) \
  397. for(_ptr = list_to_element((_list)->next), \
  398. _tmp = list_to_element(_ptr->list.next); \
  399. &_ptr->list != (_list); \
  400. _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
  401. /**
  402. * uci_list_empty: returns true if a list is empty
  403. * @list: list head
  404. */
  405. #define uci_list_empty(list) ((list)->next == (list))
  406. /* wrappers for dynamic type handling */
  407. #define uci_type_backend UCI_TYPE_BACKEND
  408. #define uci_type_history UCI_TYPE_HISTORY
  409. #define uci_type_package UCI_TYPE_PACKAGE
  410. #define uci_type_section UCI_TYPE_SECTION
  411. #define uci_type_option UCI_TYPE_OPTION
  412. /* element typecasting */
  413. #ifdef UCI_DEBUG_TYPECAST
  414. static const char *uci_typestr[] = {
  415. [uci_type_backend] = "backend",
  416. [uci_type_history] = "history",
  417. [uci_type_package] = "package",
  418. [uci_type_section] = "section",
  419. [uci_type_option] = "option",
  420. };
  421. static void uci_typecast_error(int from, int to)
  422. {
  423. fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
  424. }
  425. #define BUILD_CAST(_type) \
  426. static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
  427. { \
  428. if (e->type != uci_type_ ## _type) { \
  429. uci_typecast_error(e->type, uci_type_ ## _type); \
  430. } \
  431. return (struct uci_ ## _type *) e; \
  432. }
  433. BUILD_CAST(backend)
  434. BUILD_CAST(history)
  435. BUILD_CAST(package)
  436. BUILD_CAST(section)
  437. BUILD_CAST(option)
  438. #else
  439. #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
  440. #define uci_to_history(ptr) container_of(ptr, struct uci_history, e)
  441. #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
  442. #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
  443. #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
  444. #endif
  445. /**
  446. * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
  447. * @ctx: uci context
  448. * @type: {package,section,option}
  449. * @name: string containing the name of the element
  450. * @datasize: additional buffer size to reserve at the end of the struct
  451. */
  452. #define uci_alloc_element(ctx, type, name, datasize) \
  453. uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
  454. #define uci_dataptr(ptr) \
  455. (((char *) ptr) + sizeof(*ptr))
  456. #endif