Back to index

lightning-sunbird  0.9+nobinonly
dsparse.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  * Copyright (c) 1993, 1994 Regents of the University of Michigan.
00039  * All rights reserved.
00040  *
00041  * Redistribution and use in source and binary forms are permitted
00042  * provided that this notice is preserved and that due credit is given
00043  * to the University of Michigan at Ann Arbor. The name of the University
00044  * may not be used to endorse or promote products derived from this
00045  * software without specific prior written permission. This software
00046  * is provided ``as is'' without express or implied warranty.
00047  */
00048 /*
00049  * dsparse.c:  parsing routines used by display template and search 
00050  * preference file library routines for LDAP clients.
00051  *
00052  */
00053 
00054 #include "ldap-int.h"
00055 
00056 static int next_line( char **bufp, long *blenp, char **linep );
00057 static char *next_token( char ** sp );
00058 
00059 int
00060 nsldapi_next_line_tokens( char **bufp, long *blenp, char ***toksp )
00061 {
00062     char      *p, *line, *token, **toks;
00063     int              rc, tokcnt;
00064 
00065     *toksp = NULL;
00066 
00067     if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
00068        return( rc );
00069     }
00070 
00071     if (( toks = (char **)NSLDAPI_CALLOC( 1, sizeof( char * ))) == NULL ) {
00072        NSLDAPI_FREE( line );
00073        return( -1 );
00074     }
00075     tokcnt = 0;
00076 
00077     p = line;
00078     while (( token = next_token( &p )) != NULL ) {
00079        if (( toks = (char **)NSLDAPI_REALLOC( toks, ( tokcnt + 2 ) *
00080               sizeof( char * ))) == NULL ) {
00081            NSLDAPI_FREE( (char *)toks );
00082            NSLDAPI_FREE( line );
00083            return( -1 );
00084        }
00085        toks[ tokcnt ] = token;
00086        toks[ ++tokcnt ] = NULL;
00087     }
00088 
00089     if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
00090        tokcnt = 0;
00091        nsldapi_free_strarray( toks );
00092        toks = NULL;
00093     }
00094 
00095     NSLDAPI_FREE( line );
00096 
00097     if ( tokcnt == 0 ) {
00098        if ( toks != NULL ) {
00099            NSLDAPI_FREE( (char *)toks );
00100        }
00101     } else {
00102        *toksp = toks;
00103     }
00104 
00105     return( tokcnt );
00106 }
00107 
00108 
00109 static int
00110 next_line( char **bufp, long *blenp, char **linep )
00111 {
00112     char      *linestart, *line, *p;
00113     long      plen;
00114 
00115     linestart = *bufp;
00116     p = *bufp;
00117     plen = *blenp;
00118 
00119     do {
00120        for ( linestart = p; plen > 0; ++p, --plen ) {
00121            if ( *p == '\r' ) {
00122               if ( plen > 1 && *(p+1) == '\n' ) {
00123                   ++p;
00124                   --plen;
00125               }
00126               break;
00127            }
00128 
00129            if ( *p == '\n' ) {
00130               if ( plen > 1 && *(p+1) == '\r' ) {
00131                   ++p;
00132                   --plen;
00133               }
00134               break;
00135            }
00136        }
00137        ++p;
00138        --plen;
00139     } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
00140 
00141 
00142     *bufp = p;
00143     *blenp = plen;
00144 
00145 
00146     if ( plen <= 0 ) {
00147        *linep = NULL;
00148        return( 0 );  /* end of file */
00149     }
00150 
00151     if (( line = NSLDAPI_MALLOC( p - linestart )) == NULL ) {
00152        *linep = NULL;
00153        return( -1 ); /* fatal error */
00154     }
00155 
00156     SAFEMEMCPY( line, linestart, p - linestart );
00157     line[ p - linestart - 1 ] = '\0';
00158     *linep = line;
00159     return( strlen( line ));
00160 }
00161 
00162 
00163 static char *
00164 next_token( char **sp )
00165 {
00166     int              in_quote = 0;
00167     char      *p, *tokstart, *t;
00168 
00169     if ( **sp == '\0' ) {
00170        return( NULL );
00171     }
00172 
00173     p = *sp;
00174 
00175     while ( ldap_utf8isspace( p )) {             /* skip leading white space */
00176        ++p;
00177     }
00178 
00179     if ( *p == '\0' ) {
00180        return( NULL );
00181     }
00182 
00183     if ( *p == '\"' ) {
00184        in_quote = 1;
00185        ++p;
00186     }
00187     t = tokstart = p;
00188 
00189     for ( ;; ) {
00190        if ( *p == '\0' || ( ldap_utf8isspace( p ) && !in_quote )) {
00191            if ( *p != '\0' ) {
00192               ++p;
00193            }
00194            *t++ = '\0';            /* end of token */
00195            break;
00196        }
00197 
00198        if ( *p == '\"' ) {
00199            in_quote = !in_quote;
00200            ++p;
00201        } else {
00202            *t++ = *p++;
00203        }
00204     }
00205 
00206     *sp = p;
00207 
00208     if ( t == tokstart ) {
00209        return( NULL );
00210     }
00211 
00212     return( nsldapi_strdup( tokstart ));
00213 }
00214 
00215 
00216 void
00217 nsldapi_free_strarray( char **sap )
00218 {
00219     int              i;
00220 
00221     if ( sap != NULL ) {
00222        for ( i = 0; sap[ i ] != NULL; ++i ) {
00223            NSLDAPI_FREE( sap[ i ] );
00224        }
00225        NSLDAPI_FREE( (char *)sap );
00226     }
00227 }