Back to index

lightning-sunbird  0.9+nobinonly
crtfilt.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 /*
00039  * Demonstrate use of the create filter function.
00040  */
00041 
00042 #include "examples.h"
00043 
00044 #define FILT_BUFSIZ  4096
00045 
00046 struct filt_words {
00047     char      **f_words;
00048     char      *f_internal;
00049 };
00050 
00051 static int get_string_input( const char *prompt, const char *defaultval,
00052     char *resultbuf );
00053 static struct filt_words *val2words( const char *value, const char *delims );
00054 static void freewords( struct filt_words *words );
00055 
00056 
00057 int
00058 main( int argc, char **argv )
00059 {
00060     int                     rc;
00061     char             patbuf[ FILT_BUFSIZ ];
00062     char             attrbuf[ FILT_BUFSIZ ], valuebuf[ FILT_BUFSIZ ];
00063     char             filtbuf[ FILT_BUFSIZ ];
00064     struct filt_words       *fwords;
00065 
00066     patbuf[0] = attrbuf[0] = valuebuf[0] = '\0';
00067 
00068     while ( 1 ) {
00069        if ( get_string_input( "Enter a filter pattern: [%s] ", patbuf,
00070               patbuf ) != 0 ) {
00071            break;
00072        }
00073 
00074        if ( get_string_input( "Enter an attribute type: [%s] ", attrbuf,
00075               attrbuf ) != 0 ) {
00076            break;
00077        }
00078 
00079        if ( get_string_input( "Enter a value: [%s] ", valuebuf,
00080               valuebuf ) != 0 ) {
00081            break;
00082        }
00083 
00084        fwords = val2words( valuebuf, " " );
00085        rc = ldap_create_filter( filtbuf, sizeof( filtbuf ), patbuf,
00086               NULL, NULL, attrbuf, valuebuf, fwords->f_words );
00087        freewords( fwords );
00088        
00089        if ( rc != LDAP_SUCCESS ) {
00090            fprintf( stderr, "ldap_create_filter: failed (%d - %s)\n", rc,
00091                   ldap_err2string( rc ));
00092        } else {
00093            printf( "Resulting filter: %s\n", filtbuf );
00094        }
00095        putchar( '\n' );
00096     }
00097 
00098     return( 0 );
00099 }
00100 
00101 
00102 /*
00103  * Prompt the user for a string.  The entered string is placed in resultbuf.
00104  * If a zero-length string is entered, i.e., if they just hit return, the
00105  * contents of defaultval are copied to resultbuf.
00106  * Returns 0 if all goes well and -1 if error or end of file.
00107  */
00108 static int
00109 get_string_input( const char *prompt, const char *defaultval, char *resultbuf )
00110 {
00111     char inbuf[ FILT_BUFSIZ ];
00112 
00113     inbuf[0] = '\0';
00114     printf( prompt, defaultval );
00115     if ( fgets( inbuf, sizeof( inbuf ), stdin ) == NULL ) {
00116        return( -1 );
00117     }
00118     inbuf[ strlen( inbuf ) - 1 ] = '\0';  /* strip trailing newline */
00119     if ( inbuf[ 0 ] == '\0' ) {                  /* use default value */
00120        if ( defaultval != resultbuf ) {
00121            strcpy( resultbuf, defaultval );
00122        }
00123     } else {                              /* use newly entered value */
00124        strcpy( resultbuf, inbuf );
00125     }
00126 
00127     return( 0 );
00128 }
00129 
00130 
00131 static struct filt_words *
00132 val2words( const char *value, const char *delims )
00133 {
00134     struct filt_words       *fw;
00135     char             *word;
00136     int                     i;
00137 
00138     if (( fw = calloc( 1, sizeof( struct filt_words ))) == NULL ||
00139            ( fw->f_internal = strdup( value )) == NULL ) {
00140        perror( "calloc OR strdup" );
00141        exit( 1 );
00142     }
00143 
00144     word = strtok( fw->f_internal, delims );
00145     i = 0;
00146 
00147     while ( word != NULL ) {
00148        if ( fw->f_words == NULL ) {
00149            fw->f_words = (char **)malloc( (i + 2 ) * sizeof( char * ));
00150        } else {
00151            fw->f_words = (char **)realloc( fw->f_words,
00152                   (i + 2 ) * sizeof( char * ));
00153        }
00154 
00155        if ( fw->f_words == NULL ) {
00156            perror( "malloc OR realloc" );
00157            exit( 1 );
00158        }
00159 
00160        fw->f_words[ i ] = word;
00161        fw->f_words[ ++i ] = NULL;
00162        word = strtok( NULL, delims );
00163     }
00164 
00165     if ( i > 0 ) {
00166        fw->f_words[ i ] = NULL;
00167     }
00168 
00169     return( fw );
00170 }
00171 
00172 
00173 static void
00174 freewords( struct filt_words *words )
00175 {
00176     if ( words != NULL ) {
00177        if ( words->f_words != NULL ) {
00178            free( words->f_words );
00179        }
00180        if ( words->f_internal != NULL ) {
00181            free( words->f_internal );
00182        }
00183        free( words );
00184     }
00185 }