list.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. #include <glob.h>
  15. /* initialize a list head/item */
  16. static inline void uci_list_init(struct uci_list *ptr)
  17. {
  18. ptr->prev = ptr;
  19. ptr->next = ptr;
  20. }
  21. /* inserts a new list entry after a given entry */
  22. static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
  23. {
  24. list->next->prev = ptr;
  25. ptr->prev = list;
  26. ptr->next = list->next;
  27. list->next = ptr;
  28. }
  29. /* inserts a new list entry at the tail of the list */
  30. static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
  31. {
  32. /* NB: head->prev points at the tail */
  33. uci_list_insert(head->prev, ptr);
  34. }
  35. static inline void uci_list_del(struct uci_list *ptr)
  36. {
  37. struct uci_list *next, *prev;
  38. next = ptr->next;
  39. prev = ptr->prev;
  40. prev->next = next;
  41. next->prev = prev;
  42. uci_list_init(ptr);
  43. }
  44. /*
  45. * uci_alloc_generic allocates a new uci_element with payload
  46. * payload is appended to the struct to save memory and reduce fragmentation
  47. */
  48. static struct uci_element *
  49. uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
  50. {
  51. struct uci_element *e;
  52. int datalen = size;
  53. void *ptr;
  54. ptr = uci_malloc(ctx, datalen);
  55. e = (struct uci_element *) ptr;
  56. e->type = type;
  57. if (name) {
  58. UCI_TRAP_SAVE(ctx, error);
  59. e->name = uci_strdup(ctx, name);
  60. UCI_TRAP_RESTORE(ctx);
  61. }
  62. uci_list_init(&e->list);
  63. goto done;
  64. error:
  65. free(ptr);
  66. UCI_THROW(ctx, ctx->errno);
  67. done:
  68. return e;
  69. }
  70. static void
  71. uci_free_element(struct uci_element *e)
  72. {
  73. if (e->name)
  74. free(e->name);
  75. if (!uci_list_empty(&e->list))
  76. uci_list_del(&e->list);
  77. free(e);
  78. }
  79. static struct uci_option *
  80. uci_alloc_option(struct uci_section *s, const char *name, const char *value)
  81. {
  82. struct uci_package *p = s->package;
  83. struct uci_context *ctx = p->ctx;
  84. struct uci_option *o;
  85. o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
  86. o->value = uci_dataptr(o);
  87. o->section = s;
  88. strcpy(o->value, value);
  89. uci_list_add(&s->options, &o->e.list);
  90. return o;
  91. }
  92. static inline void
  93. uci_free_option(struct uci_option *o)
  94. {
  95. if ((o->value != uci_dataptr(o)) &&
  96. (o->value != NULL))
  97. free(o->value);
  98. uci_free_element(&o->e);
  99. }
  100. static struct uci_section *
  101. uci_alloc_section(struct uci_package *p, const char *type, const char *name)
  102. {
  103. struct uci_context *ctx = p->ctx;
  104. struct uci_section *s;
  105. if (name && !name[0])
  106. name = NULL;
  107. s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
  108. uci_list_init(&s->options);
  109. s->type = uci_dataptr(s);
  110. s->package = p;
  111. strcpy(s->type, type);
  112. if (name == NULL)
  113. s->anonymous = true;
  114. uci_list_add(&p->sections, &s->e.list);
  115. return s;
  116. }
  117. static void
  118. uci_free_section(struct uci_section *s)
  119. {
  120. struct uci_element *o, *tmp;
  121. uci_foreach_element_safe(&s->options, tmp, o) {
  122. uci_free_option(uci_to_option(o));
  123. }
  124. if ((s->type != uci_dataptr(s)) &&
  125. (s->type != NULL))
  126. free(s->type);
  127. uci_free_element(&s->e);
  128. }
  129. /* record a change that was done to a package */
  130. static void
  131. uci_add_history(struct uci_context *ctx, struct uci_package *p, int cmd, char *section, char *option, char *value)
  132. {
  133. struct uci_history *h;
  134. int size = strlen(section) + 1;
  135. char *ptr;
  136. if (!p->confdir)
  137. return;
  138. if (value)
  139. size += strlen(section) + 1;
  140. h = uci_alloc_element(ctx, history, option, size);
  141. ptr = uci_dataptr(h);
  142. h->cmd = cmd;
  143. h->section = strcpy(ptr, section);
  144. if (value) {
  145. ptr += strlen(ptr) + 1;
  146. h->value = strcpy(ptr, value);
  147. }
  148. uci_list_add(&p->history, &h->e.list);
  149. }
  150. static void
  151. uci_free_history(struct uci_history *h)
  152. {
  153. if (!h)
  154. return;
  155. if ((h->section != NULL) &&
  156. (h->section != uci_dataptr(h))) {
  157. free(h->section);
  158. free(h->value);
  159. }
  160. uci_free_element(&h->e);
  161. }
  162. static struct uci_package *
  163. uci_alloc_package(struct uci_context *ctx, const char *name)
  164. {
  165. struct uci_package *p;
  166. p = uci_alloc_element(ctx, package, name, 0);
  167. p->ctx = ctx;
  168. uci_list_init(&p->sections);
  169. uci_list_init(&p->history);
  170. return p;
  171. }
  172. static void
  173. uci_free_package(struct uci_package **package)
  174. {
  175. struct uci_element *e, *tmp;
  176. struct uci_package *p = *package;
  177. if(!p)
  178. return;
  179. if (p->path)
  180. free(p->path);
  181. uci_foreach_element_safe(&p->sections, tmp, e) {
  182. uci_free_section(uci_to_section(e));
  183. }
  184. uci_foreach_element_safe(&p->history, tmp, e) {
  185. uci_free_history(uci_to_history(e));
  186. }
  187. uci_free_element(&p->e);
  188. *package = NULL;
  189. }
  190. static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
  191. {
  192. struct uci_element *e;
  193. uci_foreach_element(list, e) {
  194. if (!strcmp(e->name, name))
  195. return e;
  196. }
  197. return NULL;
  198. }
  199. int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
  200. {
  201. struct uci_element *e;
  202. struct uci_section *s;
  203. struct uci_option *o;
  204. UCI_HANDLE_ERR(ctx);
  205. UCI_ASSERT(ctx, res != NULL);
  206. UCI_ASSERT(ctx, p != NULL);
  207. UCI_ASSERT(ctx, section != NULL);
  208. e = uci_lookup_list(ctx, &p->sections, section);
  209. if (!e)
  210. goto notfound;
  211. if (option) {
  212. s = uci_to_section(e);
  213. e = uci_lookup_list(ctx, &s->options, option);
  214. }
  215. *res = e;
  216. return 0;
  217. notfound:
  218. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  219. return 0;
  220. }
  221. int uci_del_element(struct uci_context *ctx, struct uci_element *e)
  222. {
  223. /* NB: UCI_INTERNAL use means without history tracking */
  224. bool internal = ctx->internal;
  225. struct uci_package *p = NULL;
  226. struct uci_section *s = NULL;
  227. struct uci_option *o = NULL;
  228. struct uci_element *i, *tmp;
  229. char *option = NULL;
  230. UCI_HANDLE_ERR(ctx);
  231. UCI_ASSERT(ctx, e != NULL);
  232. switch(e->type) {
  233. case UCI_TYPE_SECTION:
  234. s = uci_to_section(e);
  235. uci_foreach_element_safe(&s->options, tmp, i) {
  236. uci_del_element(ctx, i);
  237. }
  238. break;
  239. case UCI_TYPE_OPTION:
  240. o = uci_to_option(e);
  241. s = o->section;
  242. p = s->package;
  243. option = e->name;
  244. break;
  245. default:
  246. UCI_THROW(ctx, UCI_ERR_INVAL);
  247. break;
  248. }
  249. p = s->package;
  250. if (!internal)
  251. uci_add_history(ctx, p, UCI_CMD_REMOVE, s->e.name, option, NULL);
  252. switch(e->type) {
  253. case UCI_TYPE_SECTION:
  254. uci_free_section(s);
  255. break;
  256. case UCI_TYPE_OPTION:
  257. uci_free_option(o);
  258. break;
  259. default:
  260. break;
  261. }
  262. return 0;
  263. }
  264. int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
  265. {
  266. /* NB: UCI_INTERNAL use means without history tracking */
  267. bool internal = ctx->internal;
  268. struct uci_list *list;
  269. struct uci_element *e;
  270. struct uci_package *p;
  271. struct uci_section *s;
  272. char *section;
  273. char *option;
  274. char *str;
  275. int size;
  276. UCI_HANDLE_ERR(ctx);
  277. UCI_ASSERT(ctx, value != NULL);
  278. UCI_ASSERT(ctx, element != NULL);
  279. UCI_ASSERT(ctx, *element != NULL);
  280. /* what the 'value' of an element means depends on the type
  281. * for a section, the 'value' means its type
  282. * for an option, the 'value' means its value string
  283. * when changing the value, shrink the element to its actual size
  284. * (it may have been allocated with a bigger size, to include
  285. * its buffer)
  286. * then duplicate the string passed on the command line and
  287. * insert it into the structure.
  288. */
  289. e = *element;
  290. list = e->list.prev;
  291. switch(e->type) {
  292. case UCI_TYPE_SECTION:
  293. size = sizeof(struct uci_section);
  294. s = uci_to_section(e);
  295. section = e->name;
  296. option = NULL;
  297. break;
  298. case UCI_TYPE_OPTION:
  299. size = sizeof(struct uci_option);
  300. s = uci_to_option(e)->section;
  301. section = s->e.name;
  302. option = e->name;
  303. break;
  304. default:
  305. UCI_THROW(ctx, UCI_ERR_INVAL);
  306. return 0;
  307. }
  308. p = s->package;
  309. if (!internal)
  310. uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
  311. uci_list_del(&e->list);
  312. e = uci_realloc(ctx, e, size);
  313. str = uci_strdup(ctx, value);
  314. uci_list_insert(list, &e->list);
  315. *element = e;
  316. switch(e->type) {
  317. case UCI_TYPE_SECTION:
  318. uci_to_section(e)->type = str;
  319. break;
  320. case UCI_TYPE_OPTION:
  321. uci_to_option(e)->value = str;
  322. break;
  323. default:
  324. break;
  325. }
  326. return 0;
  327. }
  328. int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
  329. {
  330. /* NB: UCI_INTERNAL use means without history tracking */
  331. bool internal = ctx->internal;
  332. struct uci_element *e;
  333. struct uci_section *s = NULL;
  334. struct uci_option *o = NULL;
  335. UCI_HANDLE_ERR(ctx);
  336. UCI_ASSERT(ctx, p != NULL);
  337. UCI_ASSERT(ctx, section != NULL);
  338. UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
  339. if (!internal)
  340. uci_add_history(ctx, p, UCI_CMD_RENAME, section, option, name);
  341. name = uci_strdup(ctx, name);
  342. if (e->name)
  343. free(e->name);
  344. e->name = name;
  345. return 0;
  346. }
  347. int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option)
  348. {
  349. /* NB: pass on internal flag to uci_del_element */
  350. bool internal = ctx->internal;
  351. struct uci_element *e;
  352. struct uci_section *s = NULL;
  353. struct uci_option *o = NULL;
  354. UCI_HANDLE_ERR(ctx);
  355. UCI_ASSERT(ctx, p != NULL);
  356. UCI_ASSERT(ctx, section != NULL);
  357. UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
  358. if (!internal)
  359. return uci_del_element(ctx, e);
  360. UCI_INTERNAL(uci_del_element, ctx, e);
  361. return 0;
  362. }
  363. int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value)
  364. {
  365. /* NB: UCI_INTERNAL use means without history tracking */
  366. bool internal = ctx->internal;
  367. struct uci_element *e = NULL;
  368. struct uci_section *s = NULL;
  369. struct uci_option *o = NULL;
  370. struct uci_history *h;
  371. UCI_HANDLE_ERR(ctx);
  372. UCI_ASSERT(ctx, p != NULL);
  373. UCI_ASSERT(ctx, section != NULL);
  374. UCI_ASSERT(ctx, value != NULL);
  375. /*
  376. * look up the package, section and option (if set)
  377. * if the section/option is to be modified and it is not found
  378. * create a new element in the appropriate list
  379. */
  380. e = uci_lookup_list(ctx, &p->sections, section);
  381. if (!e)
  382. goto notfound;
  383. s = uci_to_section(e);
  384. if (option) {
  385. e = uci_lookup_list(ctx, &s->options, option);
  386. if (!e)
  387. goto notfound;
  388. o = uci_to_option(e);
  389. }
  390. /*
  391. * no unknown element was supplied, assume that we can just update
  392. * an existing entry
  393. */
  394. if (o)
  395. e = &o->e;
  396. else
  397. e = &s->e;
  398. if (!internal)
  399. return uci_set_element_value(ctx, &e, value);
  400. UCI_INTERNAL(uci_set_element_value, ctx, &e, value);
  401. return 0;
  402. notfound:
  403. /*
  404. * the entry that we need to update was not found,
  405. * check if the search failed prematurely.
  406. * this can happen if the package was not found, or if
  407. * an option was supplied, but the section wasn't found
  408. */
  409. if (!p || (!s && option))
  410. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  411. /* now add the missing entry */
  412. if (!internal)
  413. uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
  414. if (s)
  415. uci_alloc_option(s, option, value);
  416. else
  417. uci_alloc_section(p, value, section);
  418. return 0;
  419. }
  420. int uci_unload(struct uci_context *ctx, struct uci_package *p)
  421. {
  422. struct uci_element *e;
  423. UCI_HANDLE_ERR(ctx);
  424. UCI_ASSERT(ctx, p != NULL);
  425. uci_free_package(&p);
  426. return 0;
  427. }