ucimap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /*
  2. * ucimap.c - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008-2009 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. /*
  15. * This file contains ucimap, an API for mapping UCI to C data structures
  16. */
  17. #include <strings.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include "ucimap.h"
  27. #include "uci_internal.h"
  28. struct ucimap_alloc {
  29. void *ptr;
  30. };
  31. struct ucimap_alloc_custom {
  32. void *section;
  33. struct uci_optmap *om;
  34. void *ptr;
  35. };
  36. struct ucimap_fixup {
  37. struct ucimap_fixup *next;
  38. struct uci_sectionmap *sm;
  39. const char *name;
  40. enum ucimap_type type;
  41. union ucimap_data *data;
  42. };
  43. #define ucimap_foreach_option(_sm, _o) \
  44. if (!(_sm)->options_size) \
  45. (_sm)->options_size = sizeof(struct uci_optmap); \
  46. for (_o = &(_sm)->options[0]; \
  47. ((char *)(_o)) < ((char *) &(_sm)->options[0] + \
  48. (_sm)->options_size * (_sm)->n_options); \
  49. _o = (struct uci_optmap *) ((char *)(_o) + \
  50. (_sm)->options_size))
  51. static inline bool
  52. ucimap_is_alloc(enum ucimap_type type)
  53. {
  54. return (type & UCIMAP_SUBTYPE) == UCIMAP_STRING;
  55. }
  56. static inline bool
  57. ucimap_is_fixup(enum ucimap_type type)
  58. {
  59. return (type & UCIMAP_SUBTYPE) == UCIMAP_SECTION;
  60. }
  61. static inline bool
  62. ucimap_is_simple(enum ucimap_type type)
  63. {
  64. return ((type & UCIMAP_TYPE) == UCIMAP_SIMPLE);
  65. }
  66. static inline bool
  67. ucimap_is_list(enum ucimap_type type)
  68. {
  69. return ((type & UCIMAP_TYPE) == UCIMAP_LIST);
  70. }
  71. static inline bool
  72. ucimap_is_list_auto(enum ucimap_type type)
  73. {
  74. return ucimap_is_list(type) && !!(type & UCIMAP_LIST_AUTO);
  75. }
  76. static inline bool
  77. ucimap_is_custom(enum ucimap_type type)
  78. {
  79. return ((type & UCIMAP_SUBTYPE) == UCIMAP_CUSTOM);
  80. }
  81. static inline void *
  82. ucimap_section_ptr(struct ucimap_section_data *sd)
  83. {
  84. return ((char *) sd - sd->sm->smap_offset);
  85. }
  86. static inline struct ucimap_section_data *
  87. ucimap_ptr_section(struct uci_sectionmap *sm, void *ptr) {
  88. ptr = (char *) ptr + sm->smap_offset;
  89. return ptr;
  90. }
  91. static inline union ucimap_data *
  92. ucimap_get_data(struct ucimap_section_data *sd, struct uci_optmap *om)
  93. {
  94. void *data;
  95. data = (char *) ucimap_section_ptr(sd) + om->offset;
  96. return data;
  97. }
  98. int
  99. ucimap_init(struct uci_map *map)
  100. {
  101. map->fixup = NULL;
  102. map->sdata = NULL;
  103. map->fixup_tail = &map->fixup;
  104. map->sdata_tail = &map->sdata;
  105. return 0;
  106. }
  107. static void
  108. ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
  109. {
  110. struct ucimap_alloc *a = &sd->allocmap[sd->allocmap_len++];
  111. a->ptr = ptr;
  112. }
  113. void
  114. ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
  115. {
  116. void *section;
  117. unsigned int i;
  118. section = ucimap_section_ptr(sd);
  119. if (sd->ref)
  120. *sd->ref = sd->next;
  121. if (sd->sm->free)
  122. sd->sm->free(map, section);
  123. for (i = 0; i < sd->allocmap_len; i++) {
  124. free(sd->allocmap[i].ptr);
  125. }
  126. if (sd->alloc_custom) {
  127. for (i = 0; i < sd->alloc_custom_len; i++) {
  128. struct ucimap_alloc_custom *a = &sd->alloc_custom[i];
  129. a->om->free(a->section, a->om, a->ptr);
  130. }
  131. free(sd->alloc_custom);
  132. }
  133. free(sd->allocmap);
  134. free(sd);
  135. }
  136. void
  137. ucimap_cleanup(struct uci_map *map)
  138. {
  139. struct ucimap_section_data *sd, *sd_next;
  140. for (sd = map->sdata; sd; sd = sd_next) {
  141. sd_next = sd->next;
  142. ucimap_free_section(map, sd);
  143. }
  144. }
  145. static void *
  146. ucimap_find_section(struct uci_map *map, struct ucimap_fixup *f)
  147. {
  148. struct ucimap_section_data *sd;
  149. for (sd = map->sdata; sd; sd = sd->next) {
  150. if (sd->sm != f->sm)
  151. continue;
  152. if (strcmp(f->name, sd->section_name) != 0)
  153. continue;
  154. return ucimap_section_ptr(sd);
  155. }
  156. for (sd = map->pending; sd; sd = sd->next) {
  157. if (sd->sm != f->sm)
  158. continue;
  159. if (strcmp(f->name, sd->section_name) != 0)
  160. continue;
  161. return ucimap_section_ptr(sd);
  162. }
  163. return NULL;
  164. }
  165. static union ucimap_data *
  166. ucimap_list_append(struct ucimap_list *list)
  167. {
  168. if (unlikely(list->size <= list->n_items)) {
  169. /* should not happen */
  170. DPRINTF("ERROR: overflow while filling a list (size=%d)\n", list->size);
  171. return NULL;
  172. }
  173. return &list->item[list->n_items++];
  174. }
  175. static bool
  176. ucimap_handle_fixup(struct uci_map *map, struct ucimap_fixup *f)
  177. {
  178. void *ptr = ucimap_find_section(map, f);
  179. union ucimap_data *data;
  180. if (!ptr)
  181. return false;
  182. switch(f->type & UCIMAP_TYPE) {
  183. case UCIMAP_SIMPLE:
  184. f->data->ptr = ptr;
  185. break;
  186. case UCIMAP_LIST:
  187. data = ucimap_list_append(f->data->list);
  188. if (!data)
  189. return false;
  190. data->ptr = ptr;
  191. break;
  192. }
  193. return true;
  194. }
  195. void
  196. ucimap_free_item(struct ucimap_section_data *sd, void *item)
  197. {
  198. struct ucimap_alloc_custom *ac;
  199. struct ucimap_alloc *a;
  200. void *ptr = *((void **) item);
  201. unsigned int i;
  202. if (!ptr)
  203. return;
  204. *((void **)item) = NULL;
  205. for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
  206. if (a->ptr != ptr)
  207. continue;
  208. if (i != sd->allocmap_len - 1)
  209. a->ptr = sd->allocmap[sd->allocmap_len - 1].ptr;
  210. sd->allocmap_len--;
  211. return;
  212. }
  213. for (i = 0, ac = sd->alloc_custom; i < sd->alloc_custom_len; i++, ac++) {
  214. if (ac->ptr != ptr)
  215. continue;
  216. if (i != sd->alloc_custom_len - 1)
  217. memcpy(ac, &sd->alloc_custom[sd->alloc_custom_len - 1],
  218. sizeof(struct ucimap_alloc_custom));
  219. ac->om->free(ac->section, ac->om, ac->ptr);
  220. sd->alloc_custom_len--;
  221. return;
  222. }
  223. }
  224. int
  225. ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items)
  226. {
  227. struct ucimap_list *new;
  228. struct ucimap_alloc *a;
  229. unsigned int i;
  230. int offset = 0;
  231. int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
  232. if (!*list) {
  233. new = calloc(1, size);
  234. if (!new)
  235. return -ENOMEM;
  236. ucimap_add_alloc(sd, new);
  237. goto set;
  238. }
  239. for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
  240. if (a->ptr != *list)
  241. continue;
  242. goto realloc;
  243. }
  244. return -ENOENT;
  245. realloc:
  246. if (items > (*list)->size)
  247. offset = (items - (*list)->size) * sizeof(union ucimap_data);
  248. a->ptr = realloc(a->ptr, size);
  249. if (!a->ptr)
  250. return -ENOMEM;
  251. if (offset)
  252. memset((char *) a->ptr + offset, 0, size - offset);
  253. new = a->ptr;
  254. set:
  255. new->size = items;
  256. *list = new;
  257. return 0;
  258. }
  259. static void
  260. ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
  261. {
  262. struct ucimap_fixup *f, tmp;
  263. struct uci_map *map = sd->map;
  264. tmp.next = NULL;
  265. tmp.sm = om->data.sm;
  266. tmp.name = str;
  267. tmp.type = om->type;
  268. tmp.data = data;
  269. if (ucimap_handle_fixup(map, &tmp))
  270. return;
  271. f = malloc(sizeof(struct ucimap_fixup));
  272. if (!f)
  273. return;
  274. memcpy(f, &tmp, sizeof(tmp));
  275. f->next = NULL;
  276. *map->fixup_tail = f;
  277. map->fixup_tail = &f->next;
  278. }
  279. static void
  280. ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
  281. {
  282. struct ucimap_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
  283. a->section = ucimap_section_ptr(sd);
  284. a->om = om;
  285. a->ptr = ptr;
  286. }
  287. static void
  288. ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
  289. {
  290. union ucimap_data tdata = *data;
  291. char *eptr = NULL;
  292. long lval;
  293. char *s;
  294. int val;
  295. if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
  296. data = ucimap_list_append(data->list);
  297. if (!data)
  298. return;
  299. }
  300. switch(om->type & UCIMAP_SUBTYPE) {
  301. case UCIMAP_STRING:
  302. if ((om->data.s.maxlen > 0) &&
  303. (strlen(str) > (unsigned) om->data.s.maxlen))
  304. return;
  305. s = strdup(str);
  306. tdata.s = s;
  307. ucimap_add_alloc(sd, s);
  308. break;
  309. case UCIMAP_BOOL:
  310. if (!strcmp(str, "on"))
  311. val = true;
  312. else if (!strcmp(str, "1"))
  313. val = true;
  314. else if (!strcmp(str, "enabled"))
  315. val = true;
  316. else if (!strcmp(str, "off"))
  317. val = false;
  318. else if (!strcmp(str, "0"))
  319. val = false;
  320. else if (!strcmp(str, "disabled"))
  321. val = false;
  322. else
  323. return;
  324. tdata.b = val;
  325. break;
  326. case UCIMAP_INT:
  327. lval = strtol(str, &eptr, om->data.i.base);
  328. if (lval < INT_MIN || lval > INT_MAX)
  329. return;
  330. if (!eptr || *eptr == '\0')
  331. tdata.i = (int) lval;
  332. else
  333. return;
  334. break;
  335. case UCIMAP_SECTION:
  336. ucimap_add_fixup(sd, data, om, str);
  337. return;
  338. case UCIMAP_CUSTOM:
  339. break;
  340. }
  341. if (om->parse) {
  342. if (om->parse(ucimap_section_ptr(sd), om, data, str) < 0)
  343. return;
  344. if (ucimap_is_custom(om->type) && om->free) {
  345. if (tdata.ptr != data->ptr)
  346. ucimap_add_custom_alloc(sd, om, data->ptr);
  347. }
  348. }
  349. if (ucimap_is_custom(om->type))
  350. return;
  351. memcpy(data, &tdata, sizeof(union ucimap_data));
  352. }
  353. static void
  354. ucimap_convert_list(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
  355. {
  356. char *s, *p;
  357. s = strdup(str);
  358. if (!s)
  359. return;
  360. ucimap_add_alloc(sd, s);
  361. do {
  362. while (isspace(*s))
  363. s++;
  364. if (!*s)
  365. break;
  366. p = s;
  367. while (*s && !isspace(*s))
  368. s++;
  369. if (isspace(*s)) {
  370. *s = 0;
  371. s++;
  372. }
  373. ucimap_add_value(data, om, sd, p);
  374. } while (*s);
  375. }
  376. static int
  377. ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
  378. {
  379. struct uci_element *e, *l;
  380. struct uci_option *o;
  381. union ucimap_data *data;
  382. uci_foreach_element(&s->options, e) {
  383. struct uci_optmap *om = NULL, *tmp;
  384. ucimap_foreach_option(sm, tmp) {
  385. if (strcmp(e->name, tmp->name) == 0) {
  386. om = tmp;
  387. break;
  388. }
  389. }
  390. if (!om)
  391. continue;
  392. data = ucimap_get_data(sd, om);
  393. o = uci_to_option(e);
  394. if ((o->type == UCI_TYPE_STRING) && ucimap_is_simple(om->type)) {
  395. ucimap_add_value(data, om, sd, o->v.string);
  396. } else if ((o->type == UCI_TYPE_LIST) && ucimap_is_list(om->type)) {
  397. uci_foreach_element(&o->v.list, l) {
  398. ucimap_add_value(data, om, sd, l->name);
  399. }
  400. } else if ((o->type == UCI_TYPE_STRING) && ucimap_is_list_auto(om->type)) {
  401. ucimap_convert_list(data, om, sd, o->v.string);
  402. }
  403. }
  404. return 0;
  405. }
  406. static void
  407. ucimap_add_section_list(struct uci_map *map, struct ucimap_section_data *sd)
  408. {
  409. sd->ref = map->sdata_tail;
  410. *sd->ref = sd;
  411. map->sdata_tail = &sd->next;
  412. }
  413. static void
  414. ucimap_add_section(struct ucimap_section_data *sd)
  415. {
  416. struct uci_map *map = sd->map;
  417. sd->next = NULL;
  418. if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
  419. ucimap_free_section(map, sd);
  420. else
  421. ucimap_add_section_list(map, sd);
  422. }
  423. #ifdef UCI_DEBUG
  424. static const char *ucimap_type_names[] = {
  425. [UCIMAP_STRING] = "string",
  426. [UCIMAP_INT] = "integer",
  427. [UCIMAP_BOOL] = "boolean",
  428. [UCIMAP_SECTION] = "section",
  429. [UCIMAP_LIST] = "list",
  430. };
  431. static const char *
  432. ucimap_get_type_name(int type)
  433. {
  434. static char buf[32];
  435. const char *name;
  436. if (ucimap_is_list(type))
  437. return ucimap_type_names[UCIMAP_LIST];
  438. name = ucimap_type_names[type & UCIMAP_SUBTYPE];
  439. if (!name) {
  440. sprintf(buf, "Unknown (%d)", type & UCIMAP_SUBTYPE);
  441. name = buf;
  442. }
  443. return name;
  444. }
  445. #endif
  446. static bool
  447. ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
  448. {
  449. int type;
  450. if (unlikely(sm->type_name != om->type_name) &&
  451. unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
  452. DPRINTF("Option '%s' of section type '%s' refereces unknown "
  453. "section type '%s', should be '%s'.\n",
  454. om->name, sm->type, om->type_name, sm->type_name);
  455. return false;
  456. }
  457. if (om->detected_type < 0)
  458. return true;
  459. if (ucimap_is_custom(om->type))
  460. return true;
  461. if (ucimap_is_list(om->type) !=
  462. ucimap_is_list(om->detected_type))
  463. goto failed;
  464. if (ucimap_is_list(om->type))
  465. return true;
  466. type = om->type & UCIMAP_SUBTYPE;
  467. switch(type) {
  468. case UCIMAP_STRING:
  469. case UCIMAP_INT:
  470. case UCIMAP_BOOL:
  471. if (type != om->detected_type)
  472. goto failed;
  473. break;
  474. case UCIMAP_SECTION:
  475. goto failed;
  476. default:
  477. break;
  478. }
  479. return true;
  480. failed:
  481. DPRINTF("Invalid type in option '%s' of section type '%s', "
  482. "declared type is %s, detected type is %s\n",
  483. om->name, sm->type,
  484. ucimap_get_type_name(om->type),
  485. ucimap_get_type_name(om->detected_type));
  486. return false;
  487. }
  488. static void
  489. ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
  490. {
  491. if (ucimap_is_alloc(om->type))
  492. (*n_alloc)++;
  493. else if (ucimap_is_custom(om->type) && om->free)
  494. (*n_custom)++;
  495. }
  496. int
  497. ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
  498. {
  499. struct uci_optmap *om;
  500. char *section_name;
  501. void *section;
  502. int n_alloc = 2;
  503. int n_alloc_custom = 0;
  504. int err;
  505. sd->map = map;
  506. sd->sm = sm;
  507. ucimap_foreach_option(sm, om) {
  508. if (!ucimap_check_optmap_type(sm, om))
  509. continue;
  510. if (ucimap_is_list(om->type)) {
  511. union ucimap_data *data;
  512. struct uci_element *e;
  513. int n_elements = 0;
  514. int n_elements_alloc = 0;
  515. int n_elements_custom = 0;
  516. int size;
  517. data = ucimap_get_data(sd, om);
  518. uci_foreach_element(&s->options, e) {
  519. struct uci_option *o = uci_to_option(e);
  520. struct uci_element *tmp;
  521. if (strcmp(e->name, om->name) != 0)
  522. continue;
  523. if (o->type == UCI_TYPE_LIST) {
  524. uci_foreach_element(&o->v.list, tmp) {
  525. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  526. n_elements++;
  527. }
  528. } else if ((o->type == UCI_TYPE_STRING) &&
  529. ucimap_is_list_auto(om->type)) {
  530. const char *data = o->v.string;
  531. do {
  532. while (isspace(*data))
  533. data++;
  534. if (!*data)
  535. break;
  536. n_elements++;
  537. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  538. while (*data && !isspace(*data))
  539. data++;
  540. } while (*data);
  541. /* for the duplicated data string */
  542. if (n_elements)
  543. n_alloc++;
  544. }
  545. break;
  546. }
  547. /* add one more for the ucimap_list */
  548. n_alloc += n_elements_alloc + 1;
  549. n_alloc_custom += n_elements_custom;
  550. size = sizeof(struct ucimap_list) +
  551. n_elements * sizeof(union ucimap_data);
  552. data->list = malloc(size);
  553. if (!data->list)
  554. goto error_mem;
  555. memset(data->list, 0, size);
  556. data->list->size = n_elements;
  557. } else {
  558. ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
  559. }
  560. }
  561. sd->allocmap = calloc(n_alloc, sizeof(struct ucimap_alloc));
  562. if (!sd->allocmap)
  563. goto error_mem;
  564. if (n_alloc_custom > 0) {
  565. sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct ucimap_alloc_custom));
  566. if (!sd->alloc_custom)
  567. goto error_mem;
  568. }
  569. section_name = strdup(s->e.name);
  570. if (!section_name)
  571. goto error_mem;
  572. sd->section_name = section_name;
  573. sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
  574. if (!sd->cmap)
  575. goto error_mem;
  576. ucimap_add_alloc(sd, (void *)section_name);
  577. ucimap_add_alloc(sd, (void *)sd->cmap);
  578. ucimap_foreach_option(sm, om) {
  579. if (!ucimap_is_list(om->type))
  580. continue;
  581. ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
  582. }
  583. section = ucimap_section_ptr(sd);
  584. err = sm->init(map, section, s);
  585. if (err)
  586. goto error;
  587. if (map->parsed) {
  588. ucimap_add_section(sd);
  589. } else {
  590. ucimap_add_section_list(map, sd);
  591. }
  592. err = ucimap_parse_options(map, sm, sd, s);
  593. if (err)
  594. goto error;
  595. return 0;
  596. error_mem:
  597. free(sd->alloc_custom);
  598. free(sd->allocmap);
  599. free(sd);
  600. return UCI_ERR_MEM;
  601. error:
  602. ucimap_free_section(map, sd);
  603. return err;
  604. }
  605. static int
  606. ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
  607. {
  608. struct uci_package *p = s->package;
  609. memset(ptr, 0, sizeof(struct uci_ptr));
  610. ptr->package = p->e.name;
  611. ptr->p = p;
  612. ptr->section = s->e.name;
  613. ptr->s = s;
  614. ptr->option = option;
  615. return uci_lookup_ptr(p->ctx, ptr, NULL, false);
  616. }
  617. void
  618. ucimap_set_changed(struct ucimap_section_data *sd, void *field)
  619. {
  620. void *section = ucimap_section_ptr(sd);
  621. struct uci_sectionmap *sm = sd->sm;
  622. struct uci_optmap *om;
  623. unsigned int ofs = (char *)field - (char *)section;
  624. int i = 0;
  625. ucimap_foreach_option(sm, om) {
  626. if (om->offset == ofs) {
  627. SET_BIT(sd->cmap, i);
  628. break;
  629. }
  630. i++;
  631. }
  632. }
  633. static char *
  634. ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, union ucimap_data *data)
  635. {
  636. static char buf[32];
  637. char *str = NULL;
  638. switch(om->type & UCIMAP_SUBTYPE) {
  639. case UCIMAP_STRING:
  640. str = data->s;
  641. break;
  642. case UCIMAP_INT:
  643. sprintf(buf, "%d", data->i);
  644. str = buf;
  645. break;
  646. case UCIMAP_BOOL:
  647. sprintf(buf, "%d", !!data->b);
  648. str = buf;
  649. break;
  650. case UCIMAP_SECTION:
  651. if (data->ptr)
  652. str = (char *) ucimap_ptr_section(om->data.sm, data->ptr)->section_name;
  653. else
  654. str = "";
  655. break;
  656. case UCIMAP_CUSTOM:
  657. break;
  658. default:
  659. return NULL;
  660. }
  661. if (om->format) {
  662. if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
  663. return NULL;
  664. if (!str)
  665. str = "";
  666. }
  667. return str;
  668. }
  669. int
  670. ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
  671. {
  672. struct uci_sectionmap *sm = sd->sm;
  673. struct uci_section *s = NULL;
  674. struct uci_optmap *om;
  675. struct uci_element *e;
  676. struct uci_ptr ptr;
  677. int i = 0;
  678. int ret;
  679. uci_foreach_element(&p->sections, e) {
  680. if (!strcmp(e->name, sd->section_name)) {
  681. s = uci_to_section(e);
  682. break;
  683. }
  684. }
  685. if (!s)
  686. return UCI_ERR_NOTFOUND;
  687. ucimap_foreach_option(sm, om) {
  688. union ucimap_data *data;
  689. i++;
  690. data = ucimap_get_data(sd, om);
  691. if (!TEST_BIT(sd->cmap, i - 1))
  692. continue;
  693. ucimap_fill_ptr(&ptr, s, om->name);
  694. if (ucimap_is_list(om->type)) {
  695. struct ucimap_list *list = data->list;
  696. bool first = true;
  697. int j;
  698. for (j = 0; j < list->n_items; j++) {
  699. ptr.value = ucimap_data_to_string(sd, om, &list->item[j]);
  700. if (!ptr.value)
  701. continue;
  702. if (first) {
  703. ret = uci_set(s->package->ctx, &ptr);
  704. first = false;
  705. } else {
  706. ret = uci_add_list(s->package->ctx, &ptr);
  707. }
  708. if (ret)
  709. return ret;
  710. }
  711. } else {
  712. ptr.value = ucimap_data_to_string(sd, om, data);
  713. if (!ptr.value)
  714. continue;
  715. ret = uci_set(s->package->ctx, &ptr);
  716. if (ret)
  717. return ret;
  718. }
  719. CLR_BIT(sd->cmap, i - 1);
  720. }
  721. return 0;
  722. }
  723. void
  724. ucimap_parse(struct uci_map *map, struct uci_package *pkg)
  725. {
  726. struct uci_element *e;
  727. struct ucimap_section_data *sd, **sd_tail;
  728. struct ucimap_fixup *f;
  729. unsigned int i;
  730. sd_tail = map->sdata_tail;
  731. map->parsed = false;
  732. map->sdata_tail = &map->pending;
  733. uci_foreach_element(&pkg->sections, e) {
  734. struct uci_section *s = uci_to_section(e);
  735. for (i = 0; i < map->n_sections; i++) {
  736. struct uci_sectionmap *sm = map->sections[i];
  737. struct ucimap_section_data *sd;
  738. if (strcmp(s->type, map->sections[i]->type) != 0)
  739. continue;
  740. if (sm->alloc) {
  741. sd = sm->alloc(map, sm, s);
  742. memset(sd, 0, sizeof(struct ucimap_section_data));
  743. } else {
  744. sd = malloc(sm->alloc_len);
  745. memset(sd, 0, sm->alloc_len);
  746. sd = ucimap_ptr_section(sm, sd);
  747. }
  748. if (!sd)
  749. continue;
  750. ucimap_parse_section(map, sm, sd, s);
  751. }
  752. }
  753. if (!map->parsed) {
  754. map->parsed = true;
  755. map->sdata_tail = sd_tail;
  756. }
  757. f = map->fixup;
  758. while (f) {
  759. struct ucimap_fixup *next = f->next;
  760. ucimap_handle_fixup(map, f);
  761. free(f);
  762. f = next;
  763. }
  764. map->fixup_tail = &map->fixup;
  765. map->fixup = NULL;
  766. sd = map->pending;
  767. while (sd) {
  768. struct ucimap_section_data *next = sd->next;
  769. ucimap_add_section(sd);
  770. sd = next;
  771. }
  772. map->pending = NULL;
  773. }