ucimap.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <stdbool.h>
  15. #include "uci_list.h"
  16. #include "uci.h"
  17. #ifndef ARRAY_SIZE
  18. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  19. #endif
  20. #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
  21. #define CLR_BIT(_name, _bit) do { \
  22. _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
  23. } while (0)
  24. #define SET_BIT(_name, _bit) do { \
  25. _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
  26. } while (0)
  27. #define TEST_BIT(_name, _bit) \
  28. (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
  29. #define UCIMAP_OPTION(_type, _field) \
  30. .type = UCIMAP_CUSTOM, \
  31. .name = #_field, \
  32. .offset = offsetof(_type, _field)
  33. struct uci_sectionmap;
  34. struct uci_optmap;
  35. struct ucimap_list;
  36. struct uci_map {
  37. struct uci_sectionmap **sections;
  38. unsigned int n_sections;
  39. struct list_head sdata;
  40. struct list_head fixup;
  41. void *priv; /* user data */
  42. };
  43. enum ucimap_type {
  44. /* types */
  45. UCIMAP_SIMPLE = 0x00,
  46. UCIMAP_LIST = 0x10,
  47. UCIMAP_TYPE = 0xf0, /* type mask */
  48. /* subtypes */
  49. UCIMAP_STRING = 0x0,
  50. UCIMAP_BOOL = 0x1,
  51. UCIMAP_INT = 0x2,
  52. UCIMAP_SECTION = 0x3,
  53. UCIMAP_CUSTOM = 0x4,
  54. UCIMAP_SUBTYPE = 0xf, /* subtype mask */
  55. };
  56. union ucimap_data {
  57. int i;
  58. bool b;
  59. char *s;
  60. void *section;
  61. struct ucimap_list *list;
  62. };
  63. struct uci_listmap {
  64. struct list_head list;
  65. union ucimap_data data;
  66. };
  67. struct uci_sectionmap {
  68. /* type string for the uci section */
  69. const char *type;
  70. /* length of the struct to map into */
  71. unsigned int alloc_len;
  72. /* give the caller time to initialize the preallocated struct */
  73. int (*init)(struct uci_map *map, void *section, struct uci_section *s);
  74. /* pass the fully processed struct to the callback after the section end */
  75. int (*add)(struct uci_map *map, void *section);
  76. /* let the callback clean up its own stuff in the section */
  77. int (*free)(struct uci_map *map, void *section);
  78. /* list of option mappings for this section */
  79. struct uci_optmap *options;
  80. unsigned int n_options;
  81. unsigned int options_size;
  82. };
  83. struct uci_optmap {
  84. unsigned int offset;
  85. const char *name;
  86. enum ucimap_type type;
  87. int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
  88. union {
  89. struct {
  90. int base;
  91. int min;
  92. int max;
  93. } i;
  94. struct {
  95. int maxlen;
  96. } s;
  97. struct uci_sectionmap *sm;
  98. } data;
  99. };
  100. struct ucimap_list {
  101. int n_items;
  102. union ucimap_data item[];
  103. };
  104. extern int ucimap_init(struct uci_map *map);
  105. extern void ucimap_cleanup(struct uci_map *map);
  106. extern void ucimap_set_changed(void *section, void *field);
  107. extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section);
  108. extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);