2 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
12 * dsparse.c: parsing routines used by display template and search
13 * preference file library routines for LDAP clients.
15 * 7 March 1994 by Mark C Smith
24 #include <ac/string.h>
27 #ifdef HAVE_SYS_FILE_H
33 static int next_line LDAP_P(( char **bufp, long *blenp, char **linep ));
34 static char *next_token LDAP_P(( char ** sp ));
39 next_line_tokens( char **bufp, long *blenp, char ***toksp )
41 char *p, *line, *token, **toks;
46 if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
50 if (( toks = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
57 while (( token = next_token( &p )) != NULL ) {
58 if (( toks = (char **)realloc( toks, ( tokcnt + 2 ) *
59 sizeof( char * ))) == NULL ) {
64 toks[ tokcnt ] = token;
65 toks[ ++tokcnt ] = NULL;
68 if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
70 free_strarray( toks );
89 next_line( char **bufp, long *blenp, char **linep )
91 char *linestart, *line, *p;
99 for ( linestart = p; plen > 0; ++p, --plen ) {
101 if ( plen > 1 && *(p+1) == '\n' ) {
109 if ( plen > 1 && *(p+1) == '\r' ) {
118 } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
127 return( 0 ); /* end of file */
130 if (( line = malloc( p - linestart )) == NULL ) {
132 return( -1 ); /* fatal error */
135 (void) memcpy( line, linestart, p - linestart );
136 line[ p - linestart - 1 ] = '\0';
138 return( strlen( line ));
143 next_token( char **sp )
146 char *p, *tokstart, *t;
148 if ( **sp == '\0' ) {
154 while ( isspace( *p )) { /* skip leading white space */
169 if ( *p == '\0' || ( isspace( *p ) && !in_quote )) {
173 *t++ = '\0'; /* end of token */
178 in_quote = !in_quote;
187 if ( t == tokstart ) {
191 return( ldap_strdup( tokstart ));
196 free_strarray( char **sap )
201 for ( i = 0; sap[ i ] != NULL; ++i ) {