config.c 3.2 KB

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