You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conf.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code 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.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * --
  19. * conf.c:
  20. * Configuration file loader and parser. All the accepted option. which are
  21. * listed in conf.h, are put in the environment for later retrievement.
  22. */
  23. #include "includes.h"
  24. #include <ctype.h>
  25. #include "common.h"
  26. #include "conf.h"
  27. /*
  28. * clear_config_env
  29. *
  30. * do not make the environment dirty
  31. */
  32. void
  33. clear_config_env(void)
  34. {
  35. int i;
  36. for (i = 0; config_str[i][0]; i++)
  37. if (getenv(config_str[i]))
  38. unsetenv(config_str[i]);
  39. }
  40. /*
  41. * parse_config_line: it reads the `line' string and sees if it has a valid
  42. * option assignment that is in the form of "option = value".
  43. * On success it stores the option name with its value in the environment.
  44. * On failure fatal() is called, so it will never return ;)
  45. * `file' and `pos' are used by fatal() to tell where the corrupted `line' was.
  46. */
  47. void
  48. parse_config_line(char *file, int pos, char *line)
  49. {
  50. int i, e = 0;
  51. char *value;
  52. if (!(value = strchr(line, '=')))
  53. fatal("The line %s:%d is invalid, it does not contain the '=' "
  54. "character. Aborting.", file, pos);
  55. for (i = 0; config_str[i][0]; i++)
  56. if (strstr(line, config_str[i])) {
  57. e = 1;
  58. break;
  59. }
  60. if (!e)
  61. fatal("The line %s:%d does not contain a valid option. Aborting.",
  62. file, pos);
  63. value++;
  64. while (isspace(*value))
  65. value++;
  66. if (setenv(config_str[i], value, 1))
  67. fatal("Error in line %s:%d: %s. Aborting.", file, pos,
  68. strerror(errno));
  69. }
  70. /*
  71. * load_config_file: loads from `file' all the options that are written in it
  72. * and stores them in the environment. See parse_config_line() above.
  73. * If `file' cannot be opened -1 is returned, but if it is read and
  74. * parse_config_line() detects a corrupted line, fatal() is directly called.
  75. * On success 0 is returned.
  76. */
  77. int
  78. load_config_file(char *file)
  79. {
  80. FILE *fd;
  81. char buf[PATH_MAX + 1], *p, *str;
  82. size_t slen;
  83. int i = 0, e = 0;
  84. if (!(fd = fopen(file, "r"))) {
  85. fatal("Cannot load the configuration file from %s: %s\n"
  86. " Maybe you want to use the -c option ?",
  87. file, strerror(errno));
  88. return -1;
  89. }
  90. while (!feof(fd) && i < CONF_MAX_LINES) {
  91. setzero(buf, PATH_MAX + 1);
  92. fgets(buf, PATH_MAX, fd);
  93. e++;
  94. if (feof(fd))
  95. break;
  96. str = buf;
  97. while (isspace(*str))
  98. str++;
  99. if (*str == '#' || !*str) {
  100. /* Strip off any comment or null lines */
  101. continue;
  102. } else {
  103. /* Remove the last part of the string where a side
  104. * comment starts, #a comment like this.
  105. */
  106. if ((p = strrchr(str, '#')))
  107. *p = '\0';
  108. /* Don't include the newline and spaces of the end of
  109. * the string */
  110. slen = strlen(str);
  111. for (p = &str[slen - 1]; isspace(*p); p--)
  112. *p = '\0';
  113. parse_config_line(file, e, str);
  114. i++;
  115. }
  116. }
  117. fclose(fd);
  118. return 0;
  119. }