list.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. static bool uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
  15. {
  16. struct uci_list *old_head = ptr->prev;
  17. struct uci_list *new_head = head;
  18. struct uci_element *p = NULL;
  19. uci_list_del(ptr);
  20. uci_foreach_element(head, p) {
  21. if (pos-- <= 0)
  22. break;
  23. new_head = &p->list;
  24. }
  25. uci_list_add(new_head->next, ptr);
  26. return (old_head != new_head);
  27. }
  28. static inline void uci_list_fixup(struct uci_list *ptr)
  29. {
  30. ptr->prev->next = ptr;
  31. ptr->next->prev = ptr;
  32. }
  33. /*
  34. * uci_alloc_generic allocates a new uci_element with payload
  35. * payload is appended to the struct to save memory and reduce fragmentation
  36. */
  37. __private struct uci_element *
  38. uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
  39. {
  40. struct uci_element *e;
  41. int datalen = size;
  42. void *ptr;
  43. ptr = uci_malloc(ctx, datalen);
  44. e = (struct uci_element *) ptr;
  45. e->type = type;
  46. if (name) {
  47. UCI_TRAP_SAVE(ctx, error);
  48. e->name = uci_strdup(ctx, name);
  49. UCI_TRAP_RESTORE(ctx);
  50. }
  51. uci_list_init(&e->list);
  52. goto done;
  53. error:
  54. free(ptr);
  55. UCI_THROW(ctx, ctx->err);
  56. done:
  57. return e;
  58. }
  59. __private void
  60. uci_free_element(struct uci_element *e)
  61. {
  62. free(e->name);
  63. if (!uci_list_empty(&e->list))
  64. uci_list_del(&e->list);
  65. free(e);
  66. }
  67. static struct uci_option *
  68. uci_alloc_option(struct uci_section *s, const char *name, const char *value)
  69. {
  70. struct uci_package *p = s->package;
  71. struct uci_context *ctx = p->ctx;
  72. struct uci_option *o;
  73. o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
  74. o->type = UCI_TYPE_STRING;
  75. o->v.string = uci_dataptr(o);
  76. o->section = s;
  77. strcpy(o->v.string, value);
  78. uci_list_add(&s->options, &o->e.list);
  79. return o;
  80. }
  81. static inline void
  82. uci_free_option(struct uci_option *o)
  83. {
  84. struct uci_element *e, *tmp;
  85. switch(o->type) {
  86. case UCI_TYPE_STRING:
  87. if ((o->v.string != uci_dataptr(o)) &&
  88. (o->v.string != NULL))
  89. free(o->v.string);
  90. break;
  91. case UCI_TYPE_LIST:
  92. uci_foreach_element_safe(&o->v.list, tmp, e) {
  93. uci_free_element(e);
  94. }
  95. break;
  96. default:
  97. break;
  98. }
  99. uci_free_element(&o->e);
  100. }
  101. static struct uci_option *
  102. uci_alloc_list(struct uci_section *s, const char *name)
  103. {
  104. struct uci_package *p = s->package;
  105. struct uci_context *ctx = p->ctx;
  106. struct uci_option *o;
  107. o = uci_alloc_element(ctx, option, name, 0);
  108. o->type = UCI_TYPE_LIST;
  109. o->section = s;
  110. uci_list_init(&o->v.list);
  111. uci_list_add(&s->options, &o->e.list);
  112. return o;
  113. }
  114. /* Based on an efficient hash function published by D. J. Bernstein */
  115. static unsigned int djbhash(unsigned int hash, char *str)
  116. {
  117. int len = strlen(str);
  118. int i;
  119. /* initial value */
  120. if (hash == ~0U)
  121. hash = 5381;
  122. for(i = 0; i < len; i++) {
  123. hash = ((hash << 5) + hash) + str[i];
  124. }
  125. return (hash & 0x7FFFFFFF);
  126. }
  127. /* fix up an unnamed section, e.g. after adding options to it */
  128. static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
  129. {
  130. unsigned int hash = ~0U;
  131. struct uci_element *e;
  132. char buf[16];
  133. if (!s || s->e.name)
  134. return;
  135. /*
  136. * Generate a name for unnamed sections. This is used as reference
  137. * when locating or updating the section from apps/scripts.
  138. * To make multiple concurrent versions somewhat safe for updating,
  139. * the name is generated from a hash of its type and name/value
  140. * pairs of its option, and it is prefixed by a counter value.
  141. * If the order of the unnamed sections changes for some reason,
  142. * updates to them will be rejected.
  143. */
  144. hash = djbhash(hash, s->type);
  145. uci_foreach_element(&s->options, e) {
  146. struct uci_option *o;
  147. hash = djbhash(hash, e->name);
  148. o = uci_to_option(e);
  149. switch(o->type) {
  150. case UCI_TYPE_STRING:
  151. hash = djbhash(hash, o->v.string);
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. sprintf(buf, "cfg%02x%04x", s->package->n_section, hash % (1 << 16));
  158. s->e.name = uci_strdup(ctx, buf);
  159. }
  160. /* fix up option list HEAD pointers and pointer to section in options */
  161. static void uci_section_fixup_options(struct uci_section *s, bool no_options)
  162. {
  163. struct uci_element *e;
  164. if (no_options) {
  165. /*
  166. * enforce empty list pointer state (s->next == s) when original
  167. * section had no options in the first place
  168. */
  169. uci_list_init(&s->options);
  170. return;
  171. }
  172. /* fix pointers to HEAD at end/beginning of list */
  173. uci_list_fixup(&s->options);
  174. /* fix back pointer to section in options */
  175. uci_foreach_element(&s->options, e) {
  176. struct uci_option *o;
  177. o = uci_to_option(e);
  178. o->section = s;
  179. }
  180. }
  181. static struct uci_section *
  182. uci_alloc_section(struct uci_package *p, const char *type, const char *name)
  183. {
  184. struct uci_context *ctx = p->ctx;
  185. struct uci_section *s;
  186. if (name && !name[0])
  187. name = NULL;
  188. s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
  189. uci_list_init(&s->options);
  190. s->type = uci_dataptr(s);
  191. s->package = p;
  192. strcpy(s->type, type);
  193. if (name == NULL)
  194. s->anonymous = true;
  195. p->n_section++;
  196. uci_list_add(&p->sections, &s->e.list);
  197. return s;
  198. }
  199. static void
  200. uci_free_section(struct uci_section *s)
  201. {
  202. struct uci_element *o, *tmp;
  203. uci_foreach_element_safe(&s->options, tmp, o) {
  204. uci_free_option(uci_to_option(o));
  205. }
  206. if ((s->type != uci_dataptr(s)) &&
  207. (s->type != NULL))
  208. free(s->type);
  209. uci_free_element(&s->e);
  210. }
  211. __private struct uci_package *
  212. uci_alloc_package(struct uci_context *ctx, const char *name)
  213. {
  214. struct uci_package *p;
  215. p = uci_alloc_element(ctx, package, name, 0);
  216. p->ctx = ctx;
  217. uci_list_init(&p->sections);
  218. uci_list_init(&p->delta);
  219. uci_list_init(&p->saved_delta);
  220. return p;
  221. }
  222. __private void
  223. uci_free_package(struct uci_package **package)
  224. {
  225. struct uci_element *e, *tmp;
  226. struct uci_package *p = *package;
  227. if(!p)
  228. return;
  229. free(p->path);
  230. uci_foreach_element_safe(&p->sections, tmp, e) {
  231. uci_free_section(uci_to_section(e));
  232. }
  233. uci_foreach_element_safe(&p->delta, tmp, e) {
  234. uci_free_delta(uci_to_delta(e));
  235. }
  236. uci_foreach_element_safe(&p->saved_delta, tmp, e) {
  237. uci_free_delta(uci_to_delta(e));
  238. }
  239. uci_free_element(&p->e);
  240. *package = NULL;
  241. }
  242. static void
  243. uci_free_any(struct uci_element **e)
  244. {
  245. switch((*e)->type) {
  246. case UCI_TYPE_SECTION:
  247. uci_free_section(uci_to_section(*e));
  248. break;
  249. case UCI_TYPE_OPTION:
  250. uci_free_option(uci_to_option(*e));
  251. break;
  252. default:
  253. break;
  254. }
  255. *e = NULL;
  256. }
  257. __private struct uci_element *
  258. uci_lookup_list(struct uci_list *list, const char *name)
  259. {
  260. struct uci_element *e;
  261. uci_foreach_element(list, e) {
  262. if (!strcmp(e->name, name))
  263. return e;
  264. }
  265. return NULL;
  266. }
  267. static struct uci_element *
  268. uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
  269. {
  270. char *idxstr, *t, *section, *name;
  271. struct uci_element *e = NULL;
  272. struct uci_section *s;
  273. int idx, c;
  274. section = uci_strdup(ctx, ptr->section);
  275. name = idxstr = section + 1;
  276. if (section[0] != '@')
  277. goto error;
  278. /* parse the section index part */
  279. idxstr = strchr(idxstr, '[');
  280. if (!idxstr)
  281. goto error;
  282. *idxstr = 0;
  283. idxstr++;
  284. t = strchr(idxstr, ']');
  285. if (!t)
  286. goto error;
  287. if (t[1] != 0)
  288. goto error;
  289. *t = 0;
  290. t = NULL;
  291. idx = strtol(idxstr, &t, 10);
  292. if (t && *t)
  293. goto error;
  294. if (!*name)
  295. name = NULL;
  296. else if (!uci_validate_type(name))
  297. goto error;
  298. /* if the given index is negative, it specifies the section number from
  299. * the end of the list */
  300. if (idx < 0) {
  301. c = 0;
  302. uci_foreach_element(&ptr->p->sections, e) {
  303. s = uci_to_section(e);
  304. if (name && (strcmp(s->type, name) != 0))
  305. continue;
  306. c++;
  307. }
  308. idx += c;
  309. }
  310. c = 0;
  311. uci_foreach_element(&ptr->p->sections, e) {
  312. s = uci_to_section(e);
  313. if (name && (strcmp(s->type, name) != 0))
  314. continue;
  315. if (idx == c)
  316. goto done;
  317. c++;
  318. }
  319. e = NULL;
  320. goto done;
  321. error:
  322. free(section);
  323. memset(ptr, 0, sizeof(struct uci_ptr));
  324. UCI_THROW(ctx, UCI_ERR_INVAL);
  325. done:
  326. free(section);
  327. if (e)
  328. ptr->section = e->name;
  329. return e;
  330. }
  331. int
  332. uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
  333. {
  334. UCI_HANDLE_ERR(ctx);
  335. *e = uci_lookup_list(list, name);
  336. if (!*e)
  337. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  338. return 0;
  339. }
  340. int
  341. uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
  342. {
  343. struct uci_element *e;
  344. UCI_HANDLE_ERR(ctx);
  345. UCI_ASSERT(ctx, ptr != NULL);
  346. if (str)
  347. UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
  348. ptr->flags |= UCI_LOOKUP_DONE;
  349. /* look up the package first */
  350. if (ptr->p)
  351. e = &ptr->p->e;
  352. else
  353. e = uci_lookup_list(&ctx->root, ptr->package);
  354. if (!e) {
  355. UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
  356. if (!ptr->p)
  357. goto notfound;
  358. ptr->last = &ptr->p->e;
  359. } else {
  360. ptr->p = uci_to_package(e);
  361. ptr->last = e;
  362. }
  363. if (!ptr->section && !ptr->s)
  364. goto complete;
  365. /* if the section name validates as a regular name, pass through
  366. * to the regular uci_lookup function call */
  367. if (ptr->s) {
  368. e = &ptr->s->e;
  369. } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
  370. if (extended)
  371. e = uci_lookup_ext_section(ctx, ptr);
  372. else
  373. UCI_THROW(ctx, UCI_ERR_INVAL);
  374. } else {
  375. e = uci_lookup_list(&ptr->p->sections, ptr->section);
  376. }
  377. if (!e)
  378. goto abort;
  379. ptr->last = e;
  380. ptr->s = uci_to_section(e);
  381. if (ptr->option) {
  382. e = uci_lookup_list(&ptr->s->options, ptr->option);
  383. if (!e)
  384. goto abort;
  385. ptr->o = uci_to_option(e);
  386. ptr->last = e;
  387. }
  388. complete:
  389. ptr->flags |= UCI_LOOKUP_COMPLETE;
  390. abort:
  391. return UCI_OK;
  392. notfound:
  393. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  394. /* not a chance here */
  395. return UCI_ERR_NOTFOUND;
  396. }
  397. __private struct uci_element *
  398. uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
  399. {
  400. UCI_ASSERT(ctx, ptr != NULL);
  401. if (!(ptr->flags & UCI_LOOKUP_DONE))
  402. UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
  403. if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
  404. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  405. UCI_ASSERT(ctx, ptr->p != NULL);
  406. /* fill in missing string info */
  407. if (ptr->p && !ptr->package)
  408. ptr->package = ptr->p->e.name;
  409. if (ptr->s && !ptr->section)
  410. ptr->section = ptr->s->e.name;
  411. if (ptr->o && !ptr->option)
  412. ptr->option = ptr->o->e.name;
  413. if (ptr->o)
  414. return &ptr->o->e;
  415. if (ptr->s)
  416. return &ptr->s->e;
  417. if (ptr->p)
  418. return &ptr->p->e;
  419. else
  420. return NULL;
  421. }
  422. static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
  423. {
  424. struct uci_element *e;
  425. struct uci_package *p;
  426. p = ptr->p;
  427. if (!internal && p->has_delta)
  428. uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
  429. e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
  430. uci_list_add(&ptr->o->v.list, &e->list);
  431. }
  432. int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
  433. {
  434. /* NB: UCI_INTERNAL use means without delta tracking */
  435. bool internal = ctx && ctx->internal;
  436. struct uci_element *e;
  437. struct uci_package *p;
  438. char *n;
  439. UCI_HANDLE_ERR(ctx);
  440. e = uci_expand_ptr(ctx, ptr, true);
  441. p = ptr->p;
  442. UCI_ASSERT(ctx, ptr->s);
  443. UCI_ASSERT(ctx, ptr->value);
  444. if (!internal && p->has_delta)
  445. uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
  446. n = uci_strdup(ctx, ptr->value);
  447. free(e->name);
  448. e->name = n;
  449. if (e->type == UCI_TYPE_SECTION)
  450. uci_to_section(e)->anonymous = false;
  451. return 0;
  452. }
  453. int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
  454. {
  455. struct uci_package *p = s->package;
  456. bool internal = ctx && ctx->internal;
  457. bool changed = false;
  458. char order[32];
  459. UCI_HANDLE_ERR(ctx);
  460. changed = uci_list_set_pos(&s->package->sections, &s->e.list, pos);
  461. if (!internal && p->has_delta && changed) {
  462. sprintf(order, "%d", pos);
  463. uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
  464. }
  465. return 0;
  466. }
  467. int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
  468. {
  469. bool internal = ctx && ctx->internal;
  470. struct uci_section *s;
  471. UCI_HANDLE_ERR(ctx);
  472. UCI_ASSERT(ctx, p != NULL);
  473. s = uci_alloc_section(p, type, NULL);
  474. if (s && s->anonymous)
  475. uci_fixup_section(ctx, s);
  476. *res = s;
  477. if (!internal && p->has_delta)
  478. uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
  479. return 0;
  480. }
  481. int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
  482. {
  483. /* NB: pass on internal flag to uci_del_element */
  484. bool internal = ctx && ctx->internal;
  485. struct uci_package *p;
  486. struct uci_element *e1, *e2, *tmp;
  487. int index;
  488. UCI_HANDLE_ERR(ctx);
  489. e1 = uci_expand_ptr(ctx, ptr, true);
  490. p = ptr->p;
  491. UCI_ASSERT(ctx, ptr->s);
  492. if (ptr->o && ptr->o->type == UCI_TYPE_LIST && ptr->value && *ptr->value) {
  493. if (!sscanf(ptr->value, "%d", &index))
  494. return 1;
  495. uci_foreach_element_safe(&ptr->o->v.list, tmp, e2) {
  496. if (index == 0) {
  497. if (!internal && p->has_delta)
  498. uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, ptr->value);
  499. uci_free_option(uci_to_option(e2));
  500. return 0;
  501. }
  502. index--;
  503. }
  504. return 0;
  505. }
  506. if (!internal && p->has_delta)
  507. uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
  508. uci_free_any(&e1);
  509. if (ptr->option)
  510. ptr->o = NULL;
  511. else if (ptr->section)
  512. ptr->s = NULL;
  513. return 0;
  514. }
  515. int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
  516. {
  517. /* NB: UCI_INTERNAL use means without delta tracking */
  518. bool internal = ctx && ctx->internal;
  519. struct uci_option *volatile prev = NULL;
  520. const char *volatile value2 = NULL;
  521. UCI_HANDLE_ERR(ctx);
  522. uci_expand_ptr(ctx, ptr, false);
  523. UCI_ASSERT(ctx, ptr->s);
  524. UCI_ASSERT(ctx, ptr->value);
  525. if (ptr->o) {
  526. switch (ptr->o->type) {
  527. case UCI_TYPE_STRING:
  528. /* we already have a string value, convert that to a list */
  529. prev = ptr->o;
  530. value2 = ptr->value;
  531. ptr->value = ptr->o->v.string;
  532. break;
  533. case UCI_TYPE_LIST:
  534. uci_add_element_list(ctx, ptr, internal);
  535. return 0;
  536. default:
  537. UCI_THROW(ctx, UCI_ERR_INVAL);
  538. break;
  539. }
  540. }
  541. ptr->o = uci_alloc_list(ptr->s, ptr->option);
  542. if (prev) {
  543. uci_add_element_list(ctx, ptr, true);
  544. uci_free_option(prev);
  545. ptr->value = value2;
  546. }
  547. uci_add_element_list(ctx, ptr, internal);
  548. return 0;
  549. }
  550. int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr)
  551. {
  552. /* NB: pass on internal flag to uci_del_element */
  553. bool internal = ctx && ctx->internal;
  554. struct uci_element *e, *tmp;
  555. struct uci_package *p;
  556. UCI_HANDLE_ERR(ctx);
  557. uci_expand_ptr(ctx, ptr, false);
  558. UCI_ASSERT(ctx, ptr->s);
  559. UCI_ASSERT(ctx, ptr->value);
  560. if (!(ptr->o && ptr->option))
  561. return 0;
  562. if ((ptr->o->type != UCI_TYPE_LIST))
  563. return 0;
  564. p = ptr->p;
  565. if (!internal && p->has_delta)
  566. uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_DEL, ptr->section, ptr->option, ptr->value);
  567. uci_foreach_element_safe(&ptr->o->v.list, tmp, e) {
  568. if (!strcmp(ptr->value, uci_to_option(e)->e.name)) {
  569. uci_free_option(uci_to_option(e));
  570. }
  571. }
  572. return 0;
  573. }
  574. int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
  575. {
  576. /* NB: UCI_INTERNAL use means without delta tracking */
  577. bool internal = ctx && ctx->internal;
  578. UCI_HANDLE_ERR(ctx);
  579. uci_expand_ptr(ctx, ptr, false);
  580. UCI_ASSERT(ctx, ptr->value);
  581. UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
  582. if (!ptr->option && ptr->value[0]) {
  583. UCI_ASSERT(ctx, uci_validate_type(ptr->value));
  584. }
  585. if (!ptr->o && ptr->s && ptr->option) {
  586. struct uci_element *e;
  587. e = uci_lookup_list(&ptr->s->options, ptr->option);
  588. if (e)
  589. ptr->o = uci_to_option(e);
  590. }
  591. if (!ptr->value[0]) {
  592. /* if setting a nonexistant option/section to a nonexistant value,
  593. * exit without errors */
  594. if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
  595. return 0;
  596. return uci_delete(ctx, ptr);
  597. } else if (!ptr->o && ptr->option) { /* new option */
  598. ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
  599. ptr->last = &ptr->o->e;
  600. } else if (!ptr->s && ptr->section) { /* new section */
  601. ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
  602. ptr->last = &ptr->s->e;
  603. } else if (ptr->o && ptr->option) { /* update option */
  604. struct uci_option *o;
  605. if ((ptr->o->type == UCI_TYPE_STRING) &&
  606. !strcmp(ptr->o->v.string, ptr->value))
  607. return 0;
  608. o = ptr->o;
  609. ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
  610. uci_free_option(o);
  611. ptr->last = &ptr->o->e;
  612. } else if (ptr->s && ptr->section) { /* update section */
  613. char *s = uci_strdup(ctx, ptr->value);
  614. if (ptr->s->type == uci_dataptr(ptr->s)) {
  615. /* drop the in-section storage of type name */
  616. bool no_options;
  617. no_options = uci_list_empty(&ptr->s->options);
  618. ptr->last = NULL;
  619. ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
  620. ptr->s = uci_to_section(ptr->last);
  621. uci_list_fixup(&ptr->s->e.list);
  622. uci_section_fixup_options(ptr->s, no_options);
  623. } else {
  624. free(ptr->s->type);
  625. }
  626. ptr->s->type = s;
  627. } else {
  628. UCI_THROW(ctx, UCI_ERR_INVAL);
  629. }
  630. if (!internal && ptr->p->has_delta)
  631. uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
  632. return 0;
  633. }
  634. int uci_unload(struct uci_context *ctx, struct uci_package *p)
  635. {
  636. UCI_HANDLE_ERR(ctx);
  637. UCI_ASSERT(ctx, p != NULL);
  638. uci_free_package(&p);
  639. return 0;
  640. }