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
29 #include <sys/types.h>
39 int next_line_tokens();
41 static int next_line();
42 static char *next_token();
43 #else /* !NEEDPROTOS */
44 int next_line_tokens( char **bufp, long *blenp, char ***toksp );
45 void free_strarray( char **sap );
46 static int next_line( char **bufp, long *blenp, char **linep );
47 static char *next_token( char ** sp );
48 #endif /* !NEEDPROTOS */
53 next_line_tokens( char **bufp, long *blenp, char ***toksp )
55 char *p, *line, *token, **toks;
60 if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
64 if (( toks = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
71 while (( token = next_token( &p )) != NULL ) {
72 if (( toks = (char **)realloc( toks, ( tokcnt + 2 ) *
73 sizeof( char * ))) == NULL ) {
78 toks[ tokcnt ] = token;
79 toks[ ++tokcnt ] = NULL;
82 if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
84 free_strarray( toks );
103 next_line( char **bufp, long *blenp, char **linep )
105 char *linestart, *line, *p;
113 for ( linestart = p; plen > 0; ++p, --plen ) {
115 if ( plen > 1 && *(p+1) == '\n' ) {
123 if ( plen > 1 && *(p+1) == '\r' ) {
132 } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
141 return( 0 ); /* end of file */
144 if (( line = malloc( p - linestart )) == NULL ) {
146 return( -1 ); /* fatal error */
149 (void) memcpy( line, linestart, p - linestart );
150 line[ p - linestart - 1 ] = '\0';
152 return( strlen( line ));
157 next_token( char **sp )
160 char *p, *tokstart, *t;
162 if ( **sp == '\0' ) {
168 while ( isspace( *p )) { /* skip leading white space */
183 if ( *p == '\0' || ( isspace( *p ) && !in_quote )) {
187 *t++ = '\0'; /* end of token */
192 in_quote = !in_quote;
201 if ( t == tokstart ) {
205 return( strdup( tokstart ));
210 free_strarray( char **sap )
215 for ( i = 0; sap[ i ] != NULL; ++i ) {