]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/versionmatch.cc
* Swedish translation update. Closes: #592366
[apt.git] / apt-pkg / versionmatch.cc
index 1abb7e4addcfc7e5ae87524d4d5f941dcf84adaa..093180f9b78e2f9d9397eed1e39b3e191fa52626 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: versionmatch.cc,v 1.8 2003/04/24 03:16:58 doogie Exp $
+// $Id: versionmatch.cc,v 1.9 2003/05/19 17:58:26 doogie Exp $
 /* ######################################################################
 
    Version Matching 
 /* ######################################################################
 
    Version Matching 
@@ -11,9 +11,6 @@
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/versionmatch.h"
-#endif
 #include <apt-pkg/versionmatch.h>
 
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/versionmatch.h>
 
 #include <apt-pkg/strutl.h>
 
 #include <stdio.h>
 #include <ctype.h>
 
 #include <stdio.h>
 #include <ctype.h>
+#include <fnmatch.h>
+#include <sys/types.h>
+#include <regex.h>
+
                                                                        /*}}}*/
 
 // VersionMatch::pkgVersionMatch - Constructor                         /*{{{*/
                                                                        /*}}}*/
 
 // VersionMatch::pkgVersionMatch - Constructor                         /*{{{*/
@@ -66,8 +67,8 @@ pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
         if (isdigit(Data[0]))
            RelVerStr = Data;
         else
         if (isdigit(Data[0]))
            RelVerStr = Data;
         else
-           RelArchive = Data;
-        
+           RelRelease = Data;
+
         if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
         {
            RelVerPrefixMatch = true;
         if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
         {
            RelVerPrefixMatch = true;
@@ -90,19 +91,23 @@ pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
       {
         if (strlen(Fragments[J]) < 3)
            continue;
       {
         if (strlen(Fragments[J]) < 3)
            continue;
-           
+
         if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0)
            RelVerStr = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0)
            RelOrigin = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0)
            RelArchive = Fragments[J]+2;
         if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0)
            RelVerStr = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0)
            RelOrigin = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0)
            RelArchive = Fragments[J]+2;
+        else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0)
+           RelCodename = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
            RelLabel = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
            RelComponent = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
            RelLabel = Fragments[J]+2;
         else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
            RelComponent = Fragments[J]+2;
+        else if (stringcasecmp(Fragments[J],Fragments[J]+2,"b=") == 0)
+           RelArchitecture = Fragments[J]+2;
       }
       }
-      
+
       if (RelVerStr.end()[-1] == '*')
       {
         RelVerPrefixMatch = true;
       if (RelVerStr.end()[-1] == '*')
       {
         RelVerPrefixMatch = true;
@@ -127,7 +132,7 @@ bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
    const char *Ae = Ab + strlen(A);
    
    // Strings are not a compatible size.
    const char *Ae = Ab + strlen(A);
    
    // Strings are not a compatible size.
-   if ((unsigned)(Ae - Ab) != B.length() && Prefix == false ||
+   if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
        (unsigned)(Ae - Ab) < B.length())
       return false;
    
        (unsigned)(Ae - Ab) < B.length())
       return false;
    
@@ -150,6 +155,8 @@ pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
       {
         if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
            return Ver;
       {
         if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
            return Ver;
+        if (ExpressionMatches(VerStr, Ver.VerStr()) == true)
+           return Ver;
         continue;
       }
       
         continue;
       }
       
@@ -161,6 +168,36 @@ pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
    // This will be Ended by now.
    return Ver;
 }
    // This will be Ended by now.
    return Ver;
 }
+
+#ifndef FNM_CASEFOLD
+#define FNM_CASEFOLD 0
+#endif
+
+bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)
+{
+   if (pattern[0] == '/') {
+      bool res = false;
+      size_t length = strlen(pattern);
+      if (pattern[length - 1] == '/') {
+        regex_t preg;
+        char *regex = strdup(pattern + 1);
+        regex[length - 2] = '\0';
+        if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) {
+           _error->Warning("Invalid regular expression: %s", regex);
+        } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
+           res = true;
+        }
+        free(regex);
+        regfree(&preg);
+        return res;
+      }
+   }
+   return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
+}
+bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
+{
+    return ExpressionMatches(pattern.c_str(), string);
+}
                                                                        /*}}}*/
 // VersionMatch::FileMatch - Match against an index file               /*{{{*/
 // ---------------------------------------------------------------------
                                                                        /*}}}*/
 // VersionMatch::FileMatch - Match against an index file               /*{{{*/
 // ---------------------------------------------------------------------
@@ -172,47 +209,64 @@ bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
    {
       if (MatchAll == true)
         return true;
    {
       if (MatchAll == true)
         return true;
-      
+
 /*      cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
       cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
 /*      cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
       cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
-      
+
       if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
          RelArchive.empty() == true && RelLabel.empty() == true &&
       if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
          RelArchive.empty() == true && RelLabel.empty() == true &&
-         RelComponent.empty() == true)
+         RelRelease.empty() == true && RelCodename.empty() == true &&
+         RelComponent.empty() == true && RelArchitecture.empty() == true)
         return false;
         return false;
-      
+
       if (RelVerStr.empty() == false)
         if (File->Version == 0 ||
       if (RelVerStr.empty() == false)
         if (File->Version == 0 ||
-            MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false)
+            (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
+             ExpressionMatches(RelVerStr, File.Version()) == false))
            return false;
       if (RelOrigin.empty() == false)
            return false;
       if (RelOrigin.empty() == false)
-        if (File->Origin == 0 ||
-            stringcasecmp(RelOrigin,File.Origin()) != 0)
+        if (File->Origin == 0 || !ExpressionMatches(RelOrigin,File.Origin()))
            return false;
       if (RelArchive.empty() == false)
            return false;
       if (RelArchive.empty() == false)
-      {
-        if (File->Archive == 0 || 
-            stringcasecmp(RelArchive,File.Archive()) != 0)
-           return false;
-      }      
+        if (File->Archive == 0 ||
+            !ExpressionMatches(RelArchive,File.Archive()))
+            return false;
+      if (RelCodename.empty() == false)
+        if (File->Codename == 0 ||
+            !ExpressionMatches(RelCodename,File.Codename()))
+            return false;
+      if (RelRelease.empty() == false)
+        if ((File->Archive == 0 ||
+            !ExpressionMatches(RelRelease,File.Archive())) &&
+             (File->Codename == 0 ||
+             !ExpressionMatches(RelRelease,File.Codename())))
+              return false;
       if (RelLabel.empty() == false)
         if (File->Label == 0 ||
       if (RelLabel.empty() == false)
         if (File->Label == 0 ||
-            stringcasecmp(RelLabel,File.Label()) != 0)
+            !ExpressionMatches(RelLabel,File.Label()))
            return false;
       if (RelComponent.empty() == false)
         if (File->Component == 0 ||
            return false;
       if (RelComponent.empty() == false)
         if (File->Component == 0 ||
-            stringcasecmp(RelComponent,File.Component()) != 0)
+            !ExpressionMatches(RelComponent,File.Component()))
+           return false;
+      if (RelArchitecture.empty() == false)
+        if (File->Architecture == 0 ||
+            !ExpressionMatches(RelArchitecture,File.Architecture()))
            return false;
       return true;
    }
            return false;
       return true;
    }
-   
+
    if (Type == Origin)
    {
    if (Type == Origin)
    {
-      if (!strcmp(File.Archive(), "now"))      /* ignore local "status" file */
-        return false;
-      return (OrSite == File.Site());          /* both strings match */
+      if (OrSite.empty() == false) {
+        if (File->Site == 0 || !ExpressionMatches(OrSite, File.Site()))
+           return false;
+      } else // so we are talking about file:// or status file
+        if (strcmp(File.Site(),"") == 0 && File->Archive != 0) // skip the status file
+           return false;
+      return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
    }
    }
-   
+
    return false;
 }
                                                                        /*}}}*/
    return false;
 }
                                                                        /*}}}*/