settings-test.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2014-2018 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <getopt.h>
  20. #include <errno.h>
  21. #include <library.h>
  22. #include <settings/settings_types.h>
  23. /**
  24. * Defined in libstrongswan but not part of the public API
  25. */
  26. bool settings_parser_parse_file(void *this, char *name);
  27. /**
  28. * Produce indentation for the given level
  29. */
  30. static void get_indent(char indent[BUF_LEN], int level)
  31. {
  32. int i;
  33. for (i = 0; i < level * 2 && i < BUF_LEN - 2; i += 2)
  34. {
  35. indent[i ] = ' ';
  36. indent[i+1] = ' ';
  37. }
  38. indent[i] = '\0';
  39. }
  40. /**
  41. * Recursively print the section and all subsections/settings
  42. */
  43. static void print_section(section_t *section, int level)
  44. {
  45. section_t *sub;
  46. section_ref_t *ref;
  47. kv_t *kv;
  48. char indent[BUF_LEN];
  49. int i, j;
  50. get_indent(indent, level);
  51. for (i = 0; i < array_count(section->kv_order); i++)
  52. {
  53. array_get(section->kv_order, i, &kv);
  54. printf("%s%s = %s\n", indent, kv->key, kv->value);
  55. }
  56. for (i = 0; i < array_count(section->sections_order); i++)
  57. {
  58. array_get(section->sections_order, i, &sub);
  59. printf("%s%s", indent, sub->name);
  60. if (array_count(sub->references))
  61. {
  62. for (j = 0; j < array_count(sub->references); j++)
  63. {
  64. array_get(sub->references, j, &ref);
  65. printf("%s%s", j == 0 ? " : " : ", ", ref->name);
  66. }
  67. }
  68. printf(" {\n");
  69. print_section(sub, level + 1);
  70. printf("%s}\n", indent);
  71. }
  72. }
  73. /**
  74. * Recursively print a given section and all subsections/settings
  75. */
  76. static void print_settings_section(settings_t *settings, char *section,
  77. int level)
  78. {
  79. enumerator_t *enumerator;
  80. char indent[BUF_LEN], buf[BUF_LEN], *key, *value;
  81. get_indent(indent, level);
  82. enumerator = settings->create_key_value_enumerator(settings, section);
  83. while (enumerator->enumerate(enumerator, &key, &value))
  84. {
  85. printf("%s%s = %s\n", indent, key, value);
  86. }
  87. enumerator->destroy(enumerator);
  88. enumerator = settings->create_section_enumerator(settings, section);
  89. while (enumerator->enumerate(enumerator, &key))
  90. {
  91. printf("%s%s {\n", indent, key);
  92. snprintf(buf, sizeof(buf), "%s%s%s", section,
  93. strlen(section) ? "." : "", key);
  94. print_settings_section(settings, buf, level + 1);
  95. printf("%s}\n", indent);
  96. }
  97. enumerator->destroy(enumerator);
  98. }
  99. static void usage(FILE *out, char *name)
  100. {
  101. fprintf(out, "Test strongswan.conf parser\n\n");
  102. fprintf(out, "%s [OPTIONS]\n\n", name);
  103. fprintf(out, "Options:\n");
  104. fprintf(out, " -h, --help print this help.\n");
  105. fprintf(out, " -d, --debug enables debugging of the parser.\n");
  106. fprintf(out, " -r, --resolve displays the settings with references/redefines resolved.\n");
  107. fprintf(out, " -f, --file=FILE config file to load (default STDIN).\n");
  108. fprintf(out, "\n");
  109. }
  110. int main(int argc, char *argv[])
  111. {
  112. char *file = NULL;
  113. bool resolve = FALSE;
  114. while (true)
  115. {
  116. struct option long_opts[] = {
  117. {"help", no_argument, NULL, 'h' },
  118. {"debug", no_argument, NULL, 'd' },
  119. {"file", required_argument, NULL, 'f' },
  120. {"resolve", no_argument, NULL, 'r' },
  121. {0,0,0,0 },
  122. };
  123. switch (getopt_long(argc, argv, "hdf:r", long_opts, NULL))
  124. {
  125. case EOF:
  126. break;
  127. case 'h':
  128. usage(stdout, argv[0]);
  129. return 0;
  130. case 'd':
  131. setenv("DEBUG_SETTINGS_PARSER", "1", TRUE);
  132. continue;
  133. case 'f':
  134. file = optarg;
  135. continue;
  136. case 'r':
  137. resolve = TRUE;
  138. continue;
  139. default:
  140. usage(stderr, argv[0]);
  141. return 1;
  142. }
  143. break;
  144. }
  145. /* don't load strongswan.conf */
  146. library_init("", "settings-test");
  147. atexit(library_deinit);
  148. dbg_default_set_level(3);
  149. if (file)
  150. {
  151. if (resolve)
  152. {
  153. settings_t *settings = settings_create(file);
  154. print_settings_section(settings, "", 0);
  155. settings->destroy(settings);
  156. }
  157. else
  158. {
  159. section_t *root = settings_section_create(strdup("root"));
  160. settings_parser_parse_file(root, file);
  161. print_section(root, 0);
  162. settings_section_destroy(root, NULL);
  163. }
  164. }
  165. else
  166. {
  167. usage(stderr, argv[0]);
  168. }
  169. return 0;
  170. }