config.c 5.2 KB

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