list.c 17 KB

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