3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
17 * dsparse.c: parsing routines used by display template and search
18 * preference file library routines for LDAP clients.
20 * 7 March 1994 by Mark C Smith
26 #include <ac/stdlib.h>
29 #include <ac/string.h>
32 #ifdef HAVE_SYS_FILE_H
38 static int next_line LDAP_P(( char **bufp, ber_len_t *blenp, char **linep ));
39 static char *next_token LDAP_P(( char ** sp ));
44 next_line_tokens( char **bufp, ber_len_t *blenp, char ***toksp )
46 char *p, *line, *token, **toks;
51 if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
55 if (( toks = (char **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
62 while (( token = next_token( &p )) != NULL ) {
63 if (( toks = (char **)LDAP_REALLOC( toks, ( tokcnt + 2 ) *
64 sizeof( char * ))) == NULL ) {
65 LBER_FREE( (char *)toks );
69 toks[ tokcnt ] = token;
70 toks[ ++tokcnt ] = NULL;
73 if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
75 free_strarray( toks );
83 LBER_FREE( (char *)toks );
94 next_line( char **bufp, ber_len_t *blenp, char **linep )
96 char *linestart, *line, *p;
104 for ( linestart = p; plen > 0; ++p, --plen ) {
106 if ( plen > 1 && *(p+1) == '\n' ) {
114 if ( plen > 1 && *(p+1) == '\r' ) {
123 } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
132 return( 0 ); /* end of file */
135 if (( line = LDAP_MALLOC( p - linestart )) == NULL ) {
137 return( -1 ); /* fatal error */
140 (void) memcpy( line, linestart, p - linestart );
141 line[ p - linestart - 1 ] = '\0';
143 return( strlen( line ));
148 next_token( char **sp )
151 char *p, *tokstart, *t;
153 if ( **sp == '\0' ) {
159 while ( isspace( (unsigned char) *p )) { /* skip leading white space */
174 if ( *p == '\0' || ( isspace( (unsigned char) *p ) && !in_quote )) {
178 *t++ = '\0'; /* end of token */
183 in_quote = !in_quote;
192 if ( t == tokstart ) {
196 return( LDAP_STRDUP( tokstart ));
201 free_strarray( char **sap )
206 for ( i = 0; sap[ i ] != NULL; ++i ) {
207 LBER_FREE( sap[ i ] );
209 LBER_FREE( (char *)sap );