uci.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Provided by fon.com
  17. * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
  18. */
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include "include/log.h"
  22. #include <uci.h>
  23. struct uci_package *p = NULL;
  24. struct uci_context* uci_init(char *config_file)
  25. {
  26. struct uci_context *ctx = uci_alloc_context();
  27. uci_add_delta_path(ctx, "/var/state");
  28. if(uci_load(ctx, config_file, &p) != UCI_OK)
  29. {
  30. log_printf("/etc/config/%s is missing or corrupt\n", config_file);
  31. exit(-1);
  32. }
  33. return ctx;
  34. }
  35. void uci_cleanup(struct uci_context *ctx)
  36. {
  37. uci_unload(ctx, p);
  38. uci_free_context(ctx);
  39. }
  40. void uci_save_state(struct uci_context *ctx)
  41. {
  42. uci_save(ctx, p);
  43. }
  44. char* uci_get_option(struct uci_context *ctx, char *section, char *option)
  45. {
  46. struct uci_element *e = NULL;
  47. char *value = NULL;
  48. struct uci_ptr ptr;
  49. if (!p)
  50. return NULL;
  51. memset(&ptr, 0, sizeof(ptr));
  52. ptr.package = p->e.name;
  53. ptr.section = section;
  54. ptr.option = option;
  55. if (uci_lookup_ptr(ctx, &ptr, NULL, true) != UCI_OK)
  56. return NULL;
  57. if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
  58. return NULL;
  59. e = ptr.last;
  60. switch (e->type)
  61. {
  62. case UCI_TYPE_SECTION:
  63. value = uci_to_section(e)->type;
  64. break;
  65. case UCI_TYPE_OPTION:
  66. switch(ptr.o->type) {
  67. case UCI_TYPE_STRING:
  68. value = ptr.o->v.string;
  69. break;
  70. default:
  71. value = NULL;
  72. break;
  73. }
  74. break;
  75. default:
  76. return 0;
  77. }
  78. return value;
  79. }
  80. int uci_get_option_int(struct uci_context *ctx, char *section, char *option, int def)
  81. {
  82. char *tmp = uci_get_option(ctx, section, option);
  83. int ret = def;
  84. if (tmp)
  85. ret = atoi(tmp);
  86. return ret;
  87. }
  88. void uci_for_each_section_type(char *type, void (*cb)(char*, void*), void *priv)
  89. {
  90. struct uci_element *e;
  91. if (!p)
  92. return;
  93. uci_foreach_element(&p->sections, e)
  94. if (!strcmp(type, uci_to_section(e)->type))
  95. cb(e->name, priv);
  96. }