1
0

config.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "collate.h"
  42. #include "config.h"
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include "slap.h"
  46. #define MAXARGS 16
  47. static char *
  48. strtok_quote( char *line, char *sep )
  49. {
  50. int inquote;
  51. char *tmp, *d;
  52. static char *next;
  53. if ( line != NULL ) {
  54. next = line;
  55. }
  56. while ( *next && strchr( sep, *next ) ) {
  57. next++;
  58. }
  59. if ( *next == '\0' ) {
  60. next = NULL;
  61. return( NULL );
  62. }
  63. d = tmp = next;
  64. for ( inquote = 0; *next; next++ ) {
  65. switch ( *next ) {
  66. case '"':
  67. if ( inquote ) {
  68. inquote = 0;
  69. } else {
  70. inquote = 1;
  71. }
  72. break;
  73. #ifndef _WIN32
  74. case '\\':
  75. *d++ = *++next;
  76. break;
  77. #endif
  78. default:
  79. if ( ! inquote ) {
  80. if ( strchr( sep, *next ) != NULL ) {
  81. *d++ = '\0';
  82. next++;
  83. return( tmp );
  84. }
  85. }
  86. *d++ = *next;
  87. break;
  88. }
  89. }
  90. *d = '\0';
  91. return( tmp );
  92. }
  93. static void
  94. fp_parse_line(
  95. char *line,
  96. int *argcp,
  97. char **argv
  98. )
  99. {
  100. char * token;
  101. *argcp = 0;
  102. for ( token = strtok_quote( line, " \t" ); token != NULL;
  103. token = strtok_quote( NULL, " \t" ) ) {
  104. if ( *argcp == MAXARGS ) {
  105. LDAPDebug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
  106. MAXARGS, 0, 0 );
  107. exit( 1 );
  108. }
  109. argv[(*argcp)++] = token;
  110. }
  111. argv[*argcp] = NULL;
  112. }
  113. static char buf[BUFSIZ];
  114. static char *line;
  115. static int lmax, lcur;
  116. static void
  117. fp_getline_init( int *lineno )
  118. {
  119. *lineno = -1;
  120. buf[0] = '\0';
  121. }
  122. #define CATLINE( buf ) { \
  123. int len; \
  124. len = strlen( buf ); \
  125. while ( lcur + len + 1 > lmax ) { \
  126. lmax += BUFSIZ; \
  127. line = (char *) slapi_ch_realloc( line, lmax ); \
  128. } \
  129. strcpy( line + lcur, buf ); \
  130. lcur += len; \
  131. }
  132. static char *
  133. fp_getline( FILE *fp, int *lineno )
  134. {
  135. char *p;
  136. lcur = 0;
  137. CATLINE( buf );
  138. (*lineno)++;
  139. /* hack attack - keeps us from having to keep a stack of bufs... */
  140. if ( strncasecmp( line, "include", 7 ) == 0 ) {
  141. buf[0] = '\0';
  142. return( line );
  143. }
  144. while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
  145. if ( (p = strchr( buf, '\n' )) != NULL ) {
  146. *p = '\0';
  147. }
  148. if ( ! isspace( buf[0] ) ) {
  149. return( line );
  150. }
  151. CATLINE( buf );
  152. (*lineno)++;
  153. }
  154. buf[0] = '\0';
  155. return( line[0] ? line : NULL );
  156. }
  157. void
  158. collation_read_config( char *fname )
  159. {
  160. FILE* fp;
  161. char* line;
  162. int cargc;
  163. char* cargv[MAXARGS];
  164. int lineno;
  165. fp = fopen( fname, "r" );
  166. if ( fp == NULL ) {
  167. LDAPDebug( LDAP_DEBUG_ANY,
  168. "could not open config file \"%s\" - absolute path?\n",
  169. fname, 0, 0 );
  170. return; /* Do not exit */
  171. }
  172. LDAPDebug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
  173. fp_getline_init( &lineno );
  174. while ( (line = fp_getline( fp, &lineno )) != NULL ) {
  175. /* skip comments and blank lines */
  176. if ( line[0] == '#' || line[0] == '\0' ) {
  177. continue;
  178. }
  179. LDAPDebug( LDAP_DEBUG_CONFIG, "line %d: %s\n", lineno, line, 0 );
  180. fp_parse_line( line, &cargc, cargv );
  181. if ( cargc < 1 ) {
  182. LDAPDebug( LDAP_DEBUG_ANY,
  183. "%s: line %d: bad config line (ignored)\n",
  184. fname, lineno, 0 );
  185. continue;
  186. }
  187. collation_config (cargc, cargv, fname, lineno);
  188. }
  189. fclose(fp);
  190. }