2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that this notice is preserved and that due credit is given
11 * to the University of Michigan at Ann Arbor. The name of the University
12 * may not be used to endorse or promote products derived from this
13 * software without specific prior written permission. This software
14 * is provided ``as is'' without express or implied warranty.
16 * dsparse.c: parsing routines used by display template and search
17 * preference file library routines for LDAP clients.
19 * 7 March 1994 by Mark C Smith
28 #include <ac/string.h>
31 #ifdef HAVE_SYS_FILE_H
37 static int next_line LDAP_P(( char **bufp, long *blenp, char **linep ));
38 static char *next_token LDAP_P(( char ** sp ));
43 next_line_tokens( char **bufp, long *blenp, char ***toksp )
45 char *p, *line, *token, **toks;
50 if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
54 if (( toks = (char **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
61 while (( token = next_token( &p )) != NULL ) {
62 if (( toks = (char **)LDAP_REALLOC( toks, ( tokcnt + 2 ) *
63 sizeof( char * ))) == NULL ) {
64 LBER_FREE( (char *)toks );
68 toks[ tokcnt ] = token;
69 toks[ ++tokcnt ] = NULL;
72 if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
74 free_strarray( toks );
82 LBER_FREE( (char *)toks );
93 next_line( char **bufp, long *blenp, char **linep )
95 char *linestart, *line, *p;
103 for ( linestart = p; plen > 0; ++p, --plen ) {
105 if ( plen > 1 && *(p+1) == '\n' ) {
113 if ( plen > 1 && *(p+1) == '\r' ) {
122 } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
131 return( 0 ); /* end of file */
134 if (( line = LDAP_MALLOC( p - linestart )) == NULL ) {
136 return( -1 ); /* fatal error */
139 (void) memcpy( line, linestart, p - linestart );
140 line[ p - linestart - 1 ] = '\0';
142 return( strlen( line ));
147 next_token( char **sp )
150 char *p, *tokstart, *t;
152 if ( **sp == '\0' ) {
158 while ( isspace( (unsigned char) *p )) { /* skip leading white space */
173 if ( *p == '\0' || ( isspace( (unsigned char) *p ) && !in_quote )) {
177 *t++ = '\0'; /* end of token */
182 in_quote = !in_quote;
191 if ( t == tokstart ) {
195 return( LDAP_STRDUP( tokstart ));
200 free_strarray( char **sap )
205 for ( i = 0; sap[ i ] != NULL; ++i ) {
206 LBER_FREE( sap[ i ] );
208 LBER_FREE( (char *)sap );