1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A/*
1N/A * Copyright (c) 1993, 1994 Regents of the University of Michigan.
1N/A * All rights reserved.
1N/A *
1N/A * Redistribution and use in source and binary forms are permitted
1N/A * provided that this notice is preserved and that due credit is given
1N/A * to the University of Michigan at Ann Arbor. The name of the University
1N/A * may not be used to endorse or promote products derived from this
1N/A * software without specific prior written permission. This software
1N/A * is provided ``as is'' without express or implied warranty.
1N/A */
1N/A/*
1N/A * dsparse.c: parsing routines used by display template and search
1N/A * preference file library routines for LDAP clients.
1N/A *
1N/A */
1N/A
1N/A#include "ldap-int.h"
1N/A
1N/Astatic int next_line( char **bufp, long *blenp, char **linep );
1N/Astatic char *next_token( char ** sp );
1N/A
1N/Aint
1N/Aldap_next_line_tokens( char **bufp, long *blenp, char ***toksp )
1N/A{
1N/A char *p, *line, *token, **toks;
1N/A int rc, tokcnt;
1N/A
1N/A *toksp = NULL;
1N/A
1N/A if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
1N/A return( rc );
1N/A }
1N/A
1N/A if (( toks = (char **)NSLDAPI_CALLOC( 1, sizeof( char * ))) == NULL ) {
1N/A NSLDAPI_FREE( line );
1N/A return( -1 );
1N/A }
1N/A tokcnt = 0;
1N/A
1N/A p = line;
1N/A while (( token = next_token( &p )) != NULL ) {
1N/A if (( toks = (char **)NSLDAPI_REALLOC( toks, ( tokcnt + 2 ) *
1N/A sizeof( char * ))) == NULL ) {
1N/A NSLDAPI_FREE( (char *)toks );
1N/A NSLDAPI_FREE( line );
1N/A return( -1 );
1N/A }
1N/A toks[ tokcnt ] = token;
1N/A toks[ ++tokcnt ] = NULL;
1N/A }
1N/A
1N/A if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
1N/A tokcnt = 0;
1N/A ldap_free_strarray( toks );
1N/A toks = NULL;
1N/A }
1N/A
1N/A NSLDAPI_FREE( line );
1N/A
1N/A if ( tokcnt == 0 ) {
1N/A if ( toks != NULL ) {
1N/A NSLDAPI_FREE( (char *)toks );
1N/A }
1N/A } else {
1N/A *toksp = toks;
1N/A }
1N/A
1N/A return( tokcnt );
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Anext_line( char **bufp, long *blenp, char **linep )
1N/A{
1N/A char *linestart, *line, *p;
1N/A long plen;
1N/A
1N/A linestart = *bufp;
1N/A p = *bufp;
1N/A plen = *blenp;
1N/A
1N/A do {
1N/A for ( linestart = p; plen > 0; ++p, --plen ) {
1N/A if ( *p == '\r' ) {
1N/A if ( plen > 1 && *(p+1) == '\n' ) {
1N/A ++p;
1N/A --plen;
1N/A }
1N/A break;
1N/A }
1N/A
1N/A if ( *p == '\n' ) {
1N/A if ( plen > 1 && *(p+1) == '\r' ) {
1N/A ++p;
1N/A --plen;
1N/A }
1N/A break;
1N/A }
1N/A }
1N/A ++p;
1N/A --plen;
1N/A } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
1N/A
1N/A
1N/A *bufp = p;
1N/A *blenp = plen;
1N/A
1N/A
1N/A if ( plen <= 0 ) {
1N/A *linep = NULL;
1N/A return( 0 ); /* end of file */
1N/A }
1N/A
1N/A if (( line = NSLDAPI_MALLOC( p - linestart )) == NULL ) {
1N/A *linep = NULL;
1N/A return( -1 ); /* fatal error */
1N/A }
1N/A
1N/A SAFEMEMCPY( line, linestart, p - linestart );
1N/A line[ p - linestart - 1 ] = '\0';
1N/A *linep = line;
1N/A return( strlen( line ));
1N/A}
1N/A
1N/A
1N/Astatic char *
1N/Anext_token( char **sp )
1N/A{
1N/A int in_quote = 0;
1N/A char *p, *tokstart, *t;
1N/A
1N/A if ( **sp == '\0' ) {
1N/A return( NULL );
1N/A }
1N/A
1N/A p = *sp;
1N/A
1N/A while ( ldap_utf8isspace( p )) { /* skip leading white space */
1N/A ++p;
1N/A }
1N/A
1N/A if ( *p == '\0' ) {
1N/A return( NULL );
1N/A }
1N/A
1N/A if ( *p == '\"' ) {
1N/A in_quote = 1;
1N/A ++p;
1N/A }
1N/A t = tokstart = p;
1N/A
1N/A for ( ;; ) {
1N/A if ( *p == '\0' || ( ldap_utf8isspace( p ) && !in_quote )) {
1N/A if ( *p != '\0' ) {
1N/A ++p;
1N/A }
1N/A *t++ = '\0'; /* end of token */
1N/A break;
1N/A }
1N/A
1N/A if ( *p == '\"' ) {
1N/A in_quote = !in_quote;
1N/A ++p;
1N/A } else {
1N/A *t++ = *p++;
1N/A }
1N/A }
1N/A
1N/A *sp = p;
1N/A
1N/A if ( t == tokstart ) {
1N/A return( NULL );
1N/A }
1N/A
1N/A return( nsldapi_strdup( tokstart ));
1N/A}
1N/A
1N/A
1N/Avoid
1N/Aldap_free_strarray( char **sap )
1N/A{
1N/A int i;
1N/A
1N/A if ( sap != NULL ) {
1N/A for ( i = 0; sap[ i ] != NULL; ++i ) {
1N/A NSLDAPI_FREE( sap[ i ] );
1N/A }
1N/A NSLDAPI_FREE( (char *)sap );
1N/A }
1N/A}