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>
26 extern char *strdup (const char *);
28 #ifdef HAVE_SYS_FILE_H
34 static int next_line LDAP_P(( char **bufp, long *blenp, char **linep ));
35 static char *next_token LDAP_P(( char ** sp ));
40 next_line_tokens( char **bufp, long *blenp, char ***toksp )
42 char *p, *line, *token, **toks;
47 if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
51 if (( toks = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
58 while (( token = next_token( &p )) != NULL ) {
59 if (( toks = (char **)realloc( toks, ( tokcnt + 2 ) *
60 sizeof( char * ))) == NULL ) {
65 toks[ tokcnt ] = token;
66 toks[ ++tokcnt ] = NULL;
69 if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
71 free_strarray( toks );
90 next_line( char **bufp, long *blenp, char **linep )
92 char *linestart, *line, *p;
100 for ( linestart = p; plen > 0; ++p, --plen ) {
102 if ( plen > 1 && *(p+1) == '\n' ) {
110 if ( plen > 1 && *(p+1) == '\r' ) {
119 } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
128 return( 0 ); /* end of file */
131 if (( line = malloc( p - linestart )) == NULL ) {
133 return( -1 ); /* fatal error */
136 (void) memcpy( line, linestart, p - linestart );
137 line[ p - linestart - 1 ] = '\0';
139 return( strlen( line ));
144 next_token( char **sp )
147 char *p, *tokstart, *t;
149 if ( **sp == '\0' ) {
155 while ( isspace( *p )) { /* skip leading white space */
170 if ( *p == '\0' || ( isspace( *p ) && !in_quote )) {
174 *t++ = '\0'; /* end of token */
179 in_quote = !in_quote;
188 if ( t == tokstart ) {
192 return( strdup( tokstart ));
197 free_strarray( char **sap )
202 for ( i = 0; sap[ i ] != NULL; ++i ) {