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
34 int next_line_tokens LDAP_P(( char **bufp, long *blenp, char ***toksp ));
35 void free_strarray LDAP_P(( char **sap ));
36 static int next_line LDAP_P(( char **bufp, long *blenp, char **linep ));
37 static char *next_token LDAP_P(( char ** sp ));
42 next_line_tokens( char **bufp, long *blenp, char ***toksp )
44 char *p, *line, *token, **toks;
49 if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
53 if (( toks = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
60 while (( token = next_token( &p )) != NULL ) {
61 if (( toks = (char **)realloc( toks, ( tokcnt + 2 ) *
62 sizeof( char * ))) == NULL ) {
67 toks[ tokcnt ] = token;
68 toks[ ++tokcnt ] = NULL;
71 if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
73 free_strarray( toks );
92 next_line( char **bufp, long *blenp, char **linep )
94 char *linestart, *line, *p;
102 for ( linestart = p; plen > 0; ++p, --plen ) {
104 if ( plen > 1 && *(p+1) == '\n' ) {
112 if ( plen > 1 && *(p+1) == '\r' ) {
121 } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
130 return( 0 ); /* end of file */
133 if (( line = malloc( p - linestart )) == NULL ) {
135 return( -1 ); /* fatal error */
138 (void) memcpy( line, linestart, p - linestart );
139 line[ p - linestart - 1 ] = '\0';
141 return( strlen( line ));
146 next_token( char **sp )
149 char *p, *tokstart, *t;
151 if ( **sp == '\0' ) {
157 while ( isspace( *p )) { /* skip leading white space */
172 if ( *p == '\0' || ( isspace( *p ) && !in_quote )) {
176 *t++ = '\0'; /* end of token */
181 in_quote = !in_quote;
190 if ( t == tokstart ) {
194 return( strdup( tokstart ));
199 free_strarray( char **sap )
204 for ( i = 0; sap[ i ] != NULL; ++i ) {