| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /** BEGIN COPYRIGHT BLOCK
- * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
- * Copyright (C) 2005 Red Hat, Inc.
- * All rights reserved.
- *
- * License: GPL (version 3 or any later version).
- * See LICENSE for details.
- * END COPYRIGHT BLOCK **/
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include "collate.h"
- #include "config.h"
- #include <stdio.h>
- #include <string.h>
- #include "slap.h"
- #define MAXARGS 16
- static char *
- strtok_quote( char *line, char *sep )
- {
- int inquote;
- char *tmp, *d;
- static char *next;
- if ( line != NULL ) {
- next = line;
- }
- while ( *next && strchr( sep, *next ) ) {
- next++;
- }
- if ( *next == '\0' ) {
- next = NULL;
- return( NULL );
- }
- d = tmp = next;
- for ( inquote = 0; *next; next++ ) {
- switch ( *next ) {
- case '"':
- if ( inquote ) {
- inquote = 0;
- } else {
- inquote = 1;
- }
- break;
- case '\\':
- *d++ = *++next;
- break;
- default:
- if ( ! inquote ) {
- if ( strchr( sep, *next ) != NULL ) {
- *d++ = '\0';
- next++;
- return( tmp );
- }
- }
- *d++ = *next;
- break;
- }
- }
- *d = '\0';
- return( tmp );
- }
- static void
- fp_parse_line(
- char *line,
- int *argcp,
- char **argv
- )
- {
- char * token;
- *argcp = 0;
- for ( token = strtok_quote( line, " \t" ); token != NULL;
- token = strtok_quote( NULL, " \t" ) ) {
- if ( *argcp == MAXARGS ) {
- LDAPDebug(LDAP_DEBUG_ERR, "fp_parse_line - Too many tokens (max %d)\n",
- MAXARGS, 0, 0 );
- exit( 1 );
- }
- argv[(*argcp)++] = token;
- }
- argv[*argcp] = NULL;
- }
- static char buf[BUFSIZ];
- static char *line;
- static int lmax, lcur;
- static void
- fp_getline_init( int *lineno )
- {
- *lineno = -1;
- buf[0] = '\0';
- }
- #define CATLINE( buf ) { \
- int len; \
- len = strlen( buf ); \
- while ( lcur + len + 1 > lmax ) { \
- lmax += BUFSIZ; \
- line = (char *) slapi_ch_realloc( line, lmax ); \
- } \
- strcpy( line + lcur, buf ); \
- lcur += len; \
- }
- static char *
- fp_getline( FILE *fp, int *lineno )
- {
- char *p;
- lcur = 0;
- CATLINE( buf );
- (*lineno)++;
- /* hack attack - keeps us from having to keep a stack of bufs... */
- if ( strncasecmp( line, "include", 7 ) == 0 ) {
- buf[0] = '\0';
- return( line );
- }
- while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
- if ( (p = strchr( buf, '\n' )) != NULL ) {
- *p = '\0';
- }
- if ( ! isspace( buf[0] ) ) {
- return( line );
- }
- CATLINE( buf );
- (*lineno)++;
- }
- buf[0] = '\0';
- return( line[0] ? line : NULL );
- }
- void
- collation_read_config( char *fname )
- {
- FILE* fp;
- char* line;
- int cargc;
- char* cargv[MAXARGS];
- int lineno;
- fp = fopen( fname, "r" );
- if ( fp == NULL ) {
- LDAPDebug(LDAP_DEBUG_ERR,
- "collation_read_config - Could not open config file \"%s\" - absolute path?\n",
- fname, 0, 0 );
- return; /* Do not exit */
- }
- LDAPDebug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
- fp_getline_init( &lineno );
- while ( (line = fp_getline( fp, &lineno )) != NULL ) {
- /* skip comments and blank lines */
- if ( line[0] == '#' || line[0] == '\0' ) {
- continue;
- }
- LDAPDebug(LDAP_DEBUG_CONFIG, "line %d: %s\n", lineno, line, 0 );
- fp_parse_line( line, &cargc, cargv );
- if ( cargc < 1 ) {
- LDAPDebug(LDAP_DEBUG_ERR,
- "collation_read_config - %s: line %d: bad config line (ignored)\n",
- fname, lineno, 0 );
- continue;
- }
- collation_config (cargc, cargv, fname, lineno);
- }
- fclose(fp);
- }
|