ucimap.c 18 KB

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