xref: /onnv-gate/usr/src/lib/libldap4/common/dsparse.c (revision 3857:21b9b714e4ab)
1 /*
2  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * Copyright (c) 1993, 1994 Regents of the University of Michigan.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms are permitted
13  * provided that this notice is preserved and that due credit is given
14  * to the University of Michigan at Ann Arbor. The name of the University
15  * may not be used to endorse or promote products derived from this
16  * software without specific prior written permission. This software
17  * is provided ``as is'' without express or implied warranty.
18  *
19  * dsparse.c:  parsing routines used by display template and search
20  * preference file library routines for LDAP clients.
21  *
22  * 7 March 1994 by Mark C Smith
23  */
24 
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28 #ifdef MACOS
29 #include <stdlib.h>
30 #include "macos.h"
31 #else /* MACOS */
32 #ifdef DOS
33 #include <malloc.h>
34 #include "msdos.h"
35 #else /* DOS */
36 #include <sys/types.h>
37 #include <sys/file.h>
38 #include <stdlib.h>
39 #endif /* DOS */
40 #endif /* MACOS */
41 
42 #include "lber.h"
43 #include "ldap.h"
44 #include "ldap-private.h"
45 #include "ldap-int.h"
46 
47 #ifndef NEEDPROTOS
48 int next_line_tokens();
49 static ssize_t next_line();
50 static char *next_token();
51 #else /* !NEEDPROTOS */
52 int next_line_tokens( char **bufp, ssize_t *blenp, char ***toksp );
53 static ssize_t next_line( char **bufp, ssize_t *blenp, char **linep );
54 static char *next_token( char ** sp );
55 #endif /* !NEEDPROTOS */
56 
57 
58 
59 int
next_line_tokens(char ** bufp,ssize_t * blenp,char *** toksp)60 next_line_tokens( char **bufp, ssize_t *blenp, char ***toksp )
61 {
62     char	*p, *line, *token, **toks;
63     ssize_t rc;
64     int		tokcnt;
65 
66     *toksp = NULL;
67 
68     if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
69 	return( (int)rc );
70     }
71 
72     if (( toks = (char **)calloc( (size_t) 1, sizeof( char * ))) == NULL ) {
73 	free( line );
74 	return( -1 );
75     }
76     tokcnt = 0;
77 
78     p = line;
79     while (( token = next_token( &p )) != NULL ) {
80 	if (( toks = (char **)realloc( toks, ( tokcnt + 2 ) *
81 		sizeof( char * ))) == NULL ) {
82 	    free( (char *)toks );
83 	    free( line );
84 	    return( -1 );
85 	}
86 	toks[ tokcnt ] = token;
87 	toks[ ++tokcnt ] = NULL;
88     }
89 
90     if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
91 	tokcnt = 0;
92 	free_strarray( toks );
93 	toks = NULL;
94     }
95 
96     free( line );
97 
98     if ( tokcnt == 0 ) {
99 	if ( toks != NULL ) {
100 	    free( (char *)toks );
101 	}
102     } else {
103 	*toksp = toks;
104     }
105 
106     return( tokcnt );
107 }
108 
109 
110 static ssize_t
next_line(char ** bufp,ssize_t * blenp,char ** linep)111 next_line( char **bufp, ssize_t *blenp, char **linep )
112 {
113     char	*linestart, *line, *p;
114     ssize_t	plen;
115 
116     linestart = *bufp;
117     p = *bufp;
118     plen = *blenp;
119 
120     do {
121 	for ( linestart = p; plen > 0; ++p, --plen ) {
122 	    if ( *p == '\r' ) {
123 		if ( plen > 1 && *(p+1) == '\n' ) {
124 		    ++p;
125 		    --plen;
126 		}
127 		break;
128 	    }
129 
130 	    if ( *p == '\n' ) {
131 		if ( plen > 1 && *(p+1) == '\r' ) {
132 		    ++p;
133 		    --plen;
134 		}
135 		break;
136 	    }
137 	}
138 	++p;
139 	--plen;
140     } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
141 
142 
143     *bufp = p;
144     *blenp = plen;
145 
146 
147     if ( plen <= 0 ) {
148 	*linep = NULL;
149 	return( 0 );	/* end of file */
150     }
151 
152     if (( line = malloc( p - linestart )) == NULL ) {
153 	*linep = NULL;
154 	return( -1 );	/* fatal error */
155     }
156 
157     (void) memcpy( line, linestart, p - linestart );
158     line[ p - linestart - 1 ] = '\0';
159     *linep = line;
160     return( strlen( line ));
161 }
162 
163 
164 static char *
next_token(char ** sp)165 next_token( char **sp )
166 {
167     int		in_quote = 0;
168     char	*p, *tokstart, *t;
169 
170     if ( **sp == '\0' ) {
171 	return( NULL );
172     }
173 
174     p = *sp;
175 
176     while ( isspace( *p )) {		/* skip leading white space */
177 	++p;
178     }
179 
180     if ( *p == '\0' ) {
181 	return( NULL );
182     }
183 
184     if ( *p == '\"' ) {
185 	in_quote = 1;
186 	++p;
187     }
188     t = tokstart = p;
189 
190     for ( ;; ) {
191 	if ( *p == '\0' || ( isspace( *p ) && !in_quote )) {
192 	    if ( *p != '\0' ) {
193 		++p;
194 	    }
195 	    *t++ = '\0';		/* end of token */
196 	    break;
197 	}
198 
199 	if ( *p == '\"' ) {
200 	    in_quote = !in_quote;
201 	    ++p;
202 	} else {
203 	    *t++ = *p++;
204 	}
205     }
206 
207     *sp = p;
208 
209     if ( t == tokstart ) {
210 	return( NULL );
211     }
212 
213     return( strdup( tokstart ));
214 }
215