summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/collation/config.c
blob: 3bfd35e43555bc87984d35373da9cf344d4ce881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
#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;

#ifndef _WIN32
	  case '\\':
	    *d++ = *++next;
	    break;
#endif

	  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_ANY, "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_ANY,
		  "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_ANY,
		      "%s: line %d: bad config line (ignored)\n",
		      fname, lineno, 0 );
	    continue;
	}
	collation_config (cargc, cargv, fname, lineno);
    }
    fclose(fp);
}