1
0

config.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. {
  69. char * token;
  70. *argcp = 0;
  71. for ( token = strtok_quote( line, " \t" ); token != NULL;
  72. token = strtok_quote( NULL, " \t" ) ) {
  73. if ( *argcp == MAXARGS ) {
  74. LDAPDebug(LDAP_DEBUG_ERR, "fp_parse_line - Too many tokens (max %d)\n",
  75. MAXARGS, 0, 0 );
  76. exit( 1 );
  77. }
  78. argv[(*argcp)++] = token;
  79. }
  80. argv[*argcp] = NULL;
  81. }
  82. static char buf[BUFSIZ];
  83. static char *line;
  84. static int lmax, lcur;
  85. static void
  86. fp_getline_init( int *lineno )
  87. {
  88. *lineno = -1;
  89. buf[0] = '\0';
  90. }
  91. #define CATLINE( buf ) { \
  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. LDAPDebug(LDAP_DEBUG_ERR,
  137. "collation_read_config - Could not open config file \"%s\" - absolute path?\n",
  138. fname, 0, 0 );
  139. return; /* Do not exit */
  140. }
  141. LDAPDebug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
  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. LDAPDebug(LDAP_DEBUG_CONFIG, "line %d: %s\n", lineno, line, 0 );
  149. fp_parse_line( line, &cargc, cargv );
  150. if ( cargc < 1 ) {
  151. LDAPDebug(LDAP_DEBUG_ERR,
  152. "collation_read_config - %s: line %d: bad config line (ignored)\n",
  153. fname, lineno, 0 );
  154. continue;
  155. }
  156. collation_config (cargc, cargv, fname, lineno);
  157. }
  158. fclose(fp);
  159. }