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