]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/getfilter.c
Updates for MSVC 5.0. Fix libraries names to be ol{ber,dap,..}32.lib.
[openldap] / libraries / libldap / getfilter.c
index e9ebc6aa294457ceed11058ff4df09140a697704..5be8726f6406daf7f4277191f510b2b1687148b8 100644 (file)
@@ -1,57 +1,34 @@
 /*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/*  Portions
  *  Copyright (c) 1993 Regents of the University of Michigan.
  *  All rights reserved.
  *
  *  getfilter.c -- optional add-on to libldap
  */
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
+#include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#if defined(NeXT)
-#include <regex.h>
-#endif
-#ifdef MACOS
 #include <stdlib.h>
-#include "macos.h"
-#else /* MACOS */
-#ifdef DOS
-#include <malloc.h>
-#include "msdos.h"
-#else /* DOS */
-#include <sys/types.h>
+
+#include <ac/ctype.h>
+#include <ac/errno.h>
+#include <ac/regex.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
-#include <stdlib.h>
-#include <sys/errno.h>
-#ifndef VMS
-#include <unistd.h>
-#endif /* VMS */
-#endif /* DOS */
-#endif /* MACOS */
-
-#include "lber.h"
-#include "ldap.h"
-#include "regex.h"
-
-#ifdef NEEDPROTOS
-static int break_into_words( char *str, char *delims, char ***wordsp );
-int next_line_tokens( char **bufp, long *blenp, char ***toksp );
-void free_strarray( char **sap );
-#else /* NEEDPROTOS */
-static int break_into_words();
-int next_line_tokens();
-void free_strarray();
-#endif /* NEEDPROTOS */
-
-#if !defined( MACOS ) && !defined( DOS )
-extern int     errno;
-extern char    *re_comp();
 #endif
 
+#include "ldap-int.h"
+
+static int break_into_words LDAP_P(( char *str, char *delims, char ***wordsp ));
+
 #define FILT_MAX_LINE_LEN      1024
 
 LDAPFiltDesc *
@@ -107,8 +84,10 @@ ldap_init_getfilter_buf( char *buf, long buflen )
     LDAPFiltDesc       *lfdp;
     LDAPFiltList       *flp, *nextflp;
     LDAPFiltInfo       *fip, *nextfip;
-    char               *tag, **tok;
-    int                        tokcnt, i;
+    char                       *tag, **tok;
+    int                                tokcnt, i;
+       int                             rc;
+       regex_t                 re;
 
     if (( lfdp = (LDAPFiltDesc *)calloc( 1, sizeof( LDAPFiltDesc))) == NULL ) {
        return( NULL );
@@ -138,18 +117,19 @@ ldap_init_getfilter_buf( char *buf, long buflen )
            }
            nextflp->lfl_tag = strdup( tag );
            nextflp->lfl_pattern = tok[ 0 ];
-           if ( re_comp( nextflp->lfl_pattern ) != NULL ) {
-#ifndef NO_USERINTERFACE
+           if ( (rc = regcomp( &re, nextflp->lfl_pattern, 0 )) != 0 ) {
+#ifdef LDAP_LIBUI
+               char error[512];
+               regerror(rc, &re, error, sizeof(error));
                ldap_getfilter_free( lfdp );
-               fprintf( stderr, "bad regular expresssion %s\n",
-                       nextflp->lfl_pattern );
-#if !defined( MACOS ) && !defined( DOS )
+               fprintf( stderr, "bad regular expression %s, %s\n",
+                       nextflp->lfl_pattern, error );
                errno = EINVAL;
-#endif
-#endif /* NO_USERINTERFACE */
+#endif /* LDAP_LIBUI */
                free_strarray( tok );
                return( NULL );
            }
+               regfree(&re);
                
            nextflp->lfl_delims = tok[ 1 ];
            nextflp->lfl_ilist = NULL;
@@ -194,9 +174,7 @@ ldap_init_getfilter_buf( char *buf, long buflen )
                    } else {
                        free_strarray( tok );
                        ldap_getfilter_free( lfdp );
-#if !defined( MACOS ) && !defined( DOS )
                        errno = EINVAL;
-#endif
                        return( NULL );
                    }
                    free( tok[ 2 ] );
@@ -213,9 +191,7 @@ ldap_init_getfilter_buf( char *buf, long buflen )
        default:
            free_strarray( tok );
            ldap_getfilter_free( lfdp );
-#if !defined( MACOS ) && !defined( DOS )
            errno = EINVAL;
-#endif
            return( NULL );
        }
     }
@@ -247,6 +223,8 @@ LDAPFiltInfo *
 ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
 {
     LDAPFiltList       *flp;
+       int                             rc;
+       regex_t                 re;
 
     if ( lfdp->lfd_curvalcopy != NULL ) {
        free( lfdp->lfd_curvalcopy );
@@ -256,13 +234,30 @@ ldap_getfirstfilter( LDAPFiltDesc *lfdp, char *tagpat, char *value )
     lfdp->lfd_curval = value;
     lfdp->lfd_curfip = NULL;
 
-    for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
-       if ( re_comp( tagpat ) == NULL && re_exec( flp->lfl_tag ) == 1
-               && re_comp( flp->lfl_pattern ) == NULL
-               && re_exec( lfdp->lfd_curval ) == 1 ) {
-           lfdp->lfd_curfip = flp->lfl_ilist;
-           break;
-       }
+       for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = flp->lfl_next ) {
+               /* compile tagpat, continue if we fail */
+               if (regcomp(&re, tagpat, 0) != 0)
+                       continue;
+
+               /* match tagpatern and tag, continue if we fail */
+               rc = regexec(&re, flp->lfl_tag, 0, NULL, 0);
+               regfree(&re);
+               if (rc != 0)
+                       continue;
+
+               /* compile flp->ifl_pattern, continue if we fail */
+               if (regcomp(&re, flp->lfl_pattern, 0) != 0)
+                       continue;
+
+               /* match ifl_pattern and lfd_curval, continue if we fail */
+               rc = regexec(&re, lfdp->lfd_curval, 0, NULL, 0);
+               regfree(&re);
+               if (rc != 0)
+                       continue;
+
+               /* we successfully compiled both patterns and matched both values */
+               lfdp->lfd_curfip = flp->lfl_ilist;
+               break;
     }
 
     if ( lfdp->lfd_curfip == NULL ) {
@@ -388,14 +383,16 @@ ldap_build_filter( char *filtbuf, unsigned long buflen, char *pattern,
                *f++ = *p;
            }
                
-           if ( f - filtbuf > buflen ) {
+           if ( (unsigned long) (f - filtbuf) > buflen ) {
                /* sanity check */
                --f;
                break;
            }
        }
 
-       if ( suffix != NULL && ( f - filtbuf ) < buflen ) {
+       if ( suffix != NULL && (
+               (unsigned long) ( f - filtbuf ) < buflen ) )
+       {
            strcpy( f, suffix );
        } else {
            *f = '\0';
@@ -408,14 +405,15 @@ break_into_words( char *str, char *delims, char ***wordsp )
 {
     char       *word, **words;
     int                count;
-       
+    char        *tok_r;        
+
     if (( words = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
        return( -1 );
     }
     count = 0;
     words[ count ] = NULL;
 
-    word = strtok( str, delims );
+    word = ldap_pvt_strtok( str, delims, &tok_r );
     while ( word != NULL ) {
        if (( words = (char **)realloc( words,
                ( count + 2 ) * sizeof( char * ))) == NULL ) {
@@ -424,7 +422,7 @@ break_into_words( char *str, char *delims, char ***wordsp )
 
        words[ count ] = word;
        words[ ++count ] = NULL;
-       word = strtok( NULL, delims );
+       word = ldap_pvt_strtok( NULL, delims, &tok_r );
     }
        
     *wordsp = words;