config.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "collate.h"
  39. #include "config.h"
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include "slap.h"
  43. #define MAXARGS 16
  44. static char *
  45. strtok_quote( char *line, char *sep )
  46. {
  47. int inquote;
  48. char *tmp, *d;
  49. static char *next;
  50. if ( line != NULL ) {
  51. next = line;
  52. }
  53. while ( *next && strchr( sep, *next ) ) {
  54. next++;
  55. }
  56. if ( *next == '\0' ) {
  57. next = NULL;
  58. return( NULL );
  59. }
  60. d = tmp = next;
  61. for ( inquote = 0; *next; next++ ) {
  62. switch ( *next ) {
  63. case '"':
  64. if ( inquote ) {
  65. inquote = 0;
  66. } else {
  67. inquote = 1;
  68. }
  69. break;
  70. #ifndef _WIN32
  71. case '\\':
  72. *d++ = *++next;
  73. break;
  74. #endif
  75. default:
  76. if ( ! inquote ) {
  77. if ( strchr( sep, *next ) != NULL ) {
  78. *d++ = '\0';
  79. next++;
  80. return( tmp );
  81. }
  82. }
  83. *d++ = *next;
  84. break;
  85. }
  86. }
  87. *d = '\0';
  88. return( tmp );
  89. }
  90. static void
  91. fp_parse_line(
  92. char *line,
  93. int *argcp,
  94. char **argv
  95. )
  96. {
  97. char * token;
  98. *argcp = 0;
  99. for ( token = strtok_quote( line, " \t" ); token != NULL;
  100. token = strtok_quote( NULL, " \t" ) ) {
  101. if ( *argcp == MAXARGS ) {
  102. LDAPDebug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
  103. MAXARGS, 0, 0 );
  104. exit( 1 );
  105. }
  106. argv[(*argcp)++] = token;
  107. }
  108. argv[*argcp] = NULL;
  109. }
  110. static char buf[BUFSIZ];
  111. static char *line;
  112. static int lmax, lcur;
  113. static void
  114. fp_getline_init( int *lineno )
  115. {
  116. *lineno = -1;
  117. buf[0] = '\0';
  118. }
  119. #define CATLINE( buf ) { \
  120. int len; \
  121. len = strlen( buf ); \
  122. while ( lcur + len + 1 > lmax ) { \
  123. lmax += BUFSIZ; \
  124. line = (char *) slapi_ch_realloc( line, lmax ); \
  125. } \
  126. strcpy( line + lcur, buf ); \
  127. lcur += len; \
  128. }
  129. static char *
  130. fp_getline( FILE *fp, int *lineno )
  131. {
  132. char *p;
  133. lcur = 0;
  134. CATLINE( buf );
  135. (*lineno)++;
  136. /* hack attack - keeps us from having to keep a stack of bufs... */
  137. if ( strncasecmp( line, "include", 7 ) == 0 ) {
  138. buf[0] = '\0';
  139. return( line );
  140. }
  141. while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
  142. if ( (p = strchr( buf, '\n' )) != NULL ) {
  143. *p = '\0';
  144. }
  145. if ( ! isspace( buf[0] ) ) {
  146. return( line );
  147. }
  148. CATLINE( buf );
  149. (*lineno)++;
  150. }
  151. buf[0] = '\0';
  152. return( line[0] ? line : NULL );
  153. }
  154. void
  155. collation_read_config( char *fname )
  156. {
  157. FILE* fp;
  158. char* line;
  159. int cargc;
  160. char* cargv[MAXARGS];
  161. int lineno;
  162. fp = fopen( fname, "r" );
  163. if ( fp == NULL ) {
  164. LDAPDebug( LDAP_DEBUG_ANY,
  165. "could not open config file \"%s\" - absolute path?\n",
  166. fname, 0, 0 );
  167. return; /* Do not exit */
  168. }
  169. LDAPDebug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
  170. fp_getline_init( &lineno );
  171. while ( (line = fp_getline( fp, &lineno )) != NULL ) {
  172. /* skip comments and blank lines */
  173. if ( line[0] == '#' || line[0] == '\0' ) {
  174. continue;
  175. }
  176. LDAPDebug( LDAP_DEBUG_CONFIG, "line %d: %s\n", lineno, line, 0 );
  177. fp_parse_line( line, &cargc, cargv );
  178. if ( cargc < 1 ) {
  179. LDAPDebug( LDAP_DEBUG_ANY,
  180. "%s: line %d: bad config line (ignored)\n",
  181. fname, lineno, 0 );
  182. continue;
  183. }
  184. collation_config (cargc, cargv, fname, lineno);
  185. }
  186. fclose(fp);
  187. }