config.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #include "collate.h"
  13. #include "config.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "slap.h"
  17. #define MAXARGS 16
  18. static char *
  19. strtok_quote(char *line, char *sep)
  20. {
  21. int inquote;
  22. char *tmp, *d;
  23. static char *next;
  24. if (line != NULL) {
  25. next = line;
  26. }
  27. while (*next && strchr(sep, *next)) {
  28. next++;
  29. }
  30. if (*next == '\0') {
  31. next = NULL;
  32. return (NULL);
  33. }
  34. d = tmp = next;
  35. for (inquote = 0; *next; next++) {
  36. switch (*next) {
  37. case '"':
  38. if (inquote) {
  39. inquote = 0;
  40. } else {
  41. inquote = 1;
  42. }
  43. break;
  44. case '\\':
  45. *d++ = *++next;
  46. break;
  47. default:
  48. if (!inquote) {
  49. if (strchr(sep, *next) != NULL) {
  50. *d++ = '\0';
  51. next++;
  52. return (tmp);
  53. }
  54. }
  55. *d++ = *next;
  56. break;
  57. }
  58. }
  59. *d = '\0';
  60. return (tmp);
  61. }
  62. static void
  63. fp_parse_line(
  64. char *line,
  65. int *argcp,
  66. char **argv)
  67. {
  68. char *token;
  69. *argcp = 0;
  70. for (token = strtok_quote(line, " \t"); token != NULL;
  71. token = strtok_quote(NULL, " \t")) {
  72. if (*argcp == MAXARGS) {
  73. slapi_log_err(SLAPI_LOG_ERR, COLLATE_PLUGIN_SUBSYSTEM, "fp_parse_line - Too many tokens (max %d)\n",
  74. MAXARGS);
  75. exit(1);
  76. }
  77. argv[(*argcp)++] = token;
  78. }
  79. argv[*argcp] = NULL;
  80. }
  81. static char buf[BUFSIZ];
  82. static char *line;
  83. static int lmax, lcur;
  84. static void
  85. fp_getline_init(int *lineno)
  86. {
  87. *lineno = -1;
  88. buf[0] = '\0';
  89. }
  90. #define CATLINE(buf) \
  91. { \
  92. int len; \
  93. len = strlen(buf); \
  94. while (lcur + len + 1 > lmax) { \
  95. lmax += BUFSIZ; \
  96. line = (char *)slapi_ch_realloc(line, lmax); \
  97. } \
  98. strcpy(line + lcur, buf); \
  99. lcur += len; \
  100. }
  101. static char *
  102. fp_getline(FILE *fp, int *lineno)
  103. {
  104. char *p;
  105. lcur = 0;
  106. CATLINE(buf);
  107. (*lineno)++;
  108. /* hack attack - keeps us from having to keep a stack of bufs... */
  109. if (strncasecmp(line, "include", 7) == 0) {
  110. buf[0] = '\0';
  111. return (line);
  112. }
  113. while (fgets(buf, sizeof(buf), fp) != NULL) {
  114. if ((p = strchr(buf, '\n')) != NULL) {
  115. *p = '\0';
  116. }
  117. if (!isspace(buf[0])) {
  118. return (line);
  119. }
  120. CATLINE(buf);
  121. (*lineno)++;
  122. }
  123. buf[0] = '\0';
  124. return (line[0] ? line : NULL);
  125. }
  126. void
  127. collation_read_config(char *fname)
  128. {
  129. FILE *fp;
  130. char *line;
  131. int cargc;
  132. char *cargv[MAXARGS];
  133. int lineno;
  134. fp = fopen(fname, "r");
  135. if (fp == NULL) {
  136. slapi_log_err(SLAPI_LOG_ERR, COLLATE_PLUGIN_SUBSYSTEM,
  137. "collation_read_config - Could not open config file \"%s\" - absolute path?\n",
  138. fname);
  139. return; /* Do not exit */
  140. }
  141. slapi_log_err(SLAPI_LOG_CONFIG, "collation_read_config", "Reading config file %s\n", fname);
  142. fp_getline_init(&lineno);
  143. while ((line = fp_getline(fp, &lineno)) != NULL) {
  144. /* skip comments and blank lines */
  145. if (line[0] == '#' || line[0] == '\0') {
  146. continue;
  147. }
  148. slapi_log_err(SLAPI_LOG_CONFIG, COLLATE_PLUGIN_SUBSYSTEM,
  149. "collation_read_config - line %d: %s\n", lineno, line);
  150. fp_parse_line(line, &cargc, cargv);
  151. if (cargc < 1) {
  152. slapi_log_err(SLAPI_LOG_ERR, COLLATE_PLUGIN_SUBSYSTEM,
  153. "collation_read_config - %s: line %d: bad config line (ignored)\n",
  154. fname, lineno);
  155. continue;
  156. }
  157. collation_config(cargc, cargv, fname, lineno);
  158. }
  159. fclose(fp);
  160. }