Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Sep 2000 07:51:31 +0900
From:      "Akinori -Aki- MUSHA" <knu@idaemons.org>
To:        freebsd-ports@FreeBSD.ORG
Cc:        bmah@FreeBSD.ORG
Subject:   Re: Enhancement of pkg_version's version comparison routine
Message-ID:  <86hf726a30.wl@archon.local.idaemons.org>
In-Reply-To: In your message of "Wed, 27 Sep 2000 06:15:39 %2B0900" <86k8by6eis.wl@archon.local.idaemons.org>
References:  <86k8by6eis.wl@archon.local.idaemons.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Argh, I sent the wrong (old) diff for pkg_version.pl.  This one was
the latest and correct.

-- 
                           /
                          /__  __       
                         / )  )  ) )  /
Akinori -Aki- MUSHA aka / (_ /  ( (__(  @ idaemons.org / FreeBSD.org

"We're only at home when we're on the run, on the wing, on the fly"

Index: pkg_version.pl
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pkg_install/version/pkg_version.pl,v
retrieving revision 1.10
diff -u -r1.10 pkg_version.pl
--- pkg_version.pl	2000/09/15 04:16:20	1.10
+++ pkg_version.pl	2000/09/26 20:42:20
@@ -57,38 +57,87 @@
 # This function returns -1, 0, or 1, in the same manner as <=> or cmp.
 #
 sub CompareNumbers {
-    local($v1, $v2);
-    $v1 = $_[0];
-    $v2 = $_[1];
+    my($v1, $v2) = @_;
 
     # Short-cut in case of equality
     if ($v1 eq $v2) {
 	return 0;
     }
 
-    # Loop over different components (the parts separated by dots).
-    # If any component differs, we have the basis for an inequality.
-    while (1) {
-	($p1, $v1) = split(/\./, $v1, 2);
-	($p2, $v2) = split(/\./, $v2, 2);
-
-	# If we\'re out of components, they\'re equal (this probably won\'t
-	# happen, since the short-cut case above should get this).
-	if (($p1 eq "") && ($p2 eq "")) {
-	    return 0;
+    # Split into subnumbers
+    my @s1 = split(/\./, $v1);
+    my @s2 = split(/\./, $v2);
+
+    # Subnumbers
+    my($s1, $s2);
+
+    # Seek for the difference
+    do {
+	last unless @s1 || @s2;
+
+	$s1 = shift @s1;
+	$s2 = shift @s2;
+    } while ($s1 eq $s2);
+
+    # Short-cut in case of equality
+    if ($s1 eq $s2) {
+	return 0;
+    }
+
+    # Split into sub-subnumbers
+    my @x1 = split(/(\D+)/, $s1);
+    my @x2 = split(/(\D+)/, $s2);
+
+    shift @x1 if ($s1 =~ /^\D/);
+    shift @x2 if ($s2 =~ /^\D/);
+
+    # Sub-subnumbers
+    my $x1 = shift @x1;
+    my $x2 = shift @x2;
+
+    # Check for alpha, beta, or pre
+    if ($x1 =~ /^[abp]$/) {
+	if ($x2 !~ /^[abp]$/) {
+	    return -1;		# A non-abp (including null) wins over an abp
 	}
-	# Check for numeric inequality.  We assume here that (for example)
-	# 3.09 < 3.10.
-	elsif ($p1 != $p2) {
-	    return $p1 <=> $p2;
+
+	if ($x1 ne $x2) {
+	    return $x1 cmp $x2;	# Accidentally, 'a' < 'b' < 'p' :)
 	}
-	# Check for string inequality, given numeric equality.  This
-	# handles version numbers of the form 3.4j < 3.4k.
-	elsif ($p1 ne $p2) {
-	    return $p1 cmp $p2;
+    } elsif ($x2 =~ /^[abp]$/) {
+	return 1;		# A non-abp (including null) wins over an abp
+    }
+
+    # Seek for the difference
+    while ($x1 eq $x2) {
+	last unless @x1 || @x2;
+
+	$x1 = shift @x1;
+	$x2 = shift @x2;
+    }
+
+    # Short-cut in case of equality
+    if ($x1 eq $x2) {
+	return 0;
+    }
+
+    if ($x1 =~ /^\d/) {
+	if ($x2 =~ /^\d/) {
+	    # Both numbers: compare numerically
+	    return $x1 <=> $x2;
 	}
+
+	# A number wins over non-numbers
+	return 1;
     }
 
+    if ($x2 =~ /^\d/) {
+	# A number wins over non-numbers
+	return -1;
+    }
+
+    # Both non-numbers: compare as strings
+    return $x1 cmp $x2;
 }
 
 #
@@ -197,6 +246,7 @@
 -d debug	Debugging output (debug controls level of output)
 -h		Help (this message)
 -l limchar	Limit output
+-t expr		Test expression
 -v		Verbose output
 index		URL or filename of index file
 		(Default is $IndexFile)
@@ -206,7 +256,7 @@
 #
 # Parse command-line arguments, deal with them
 #
-if (!getopts('cdhl:v') || ($opt_h)) {
+if (!getopts('cdhl:t:v') || ($opt_h)) {
     &PrintHelp();
     exit;
 }
@@ -218,6 +268,34 @@
 }
 if ($opt_l) {
     $LimitFlag = $opt_l;
+}
+if ($opt_t) {
+    my $expr = $opt_t;
+
+    $expr =~ s/\s+//g;
+
+    my($v1, $op, $v2) = split(/([<>]=?|!?=)/, $expr, 2);
+
+    if ($v2 eq '') {
+	print "Invalid expression: $expr\n";
+	exit -1;
+    }
+
+    my $cmp = CompareVersions($v1, $v2);
+
+    if ($op =~ /</) {
+	exit($cmp < 0 ? 0 : 1);
+    }
+
+    if ($op =~ />/) {
+	exit($cmp > 0 ? 0 : 1);
+    }
+
+    if ($op =~ /!/) {
+	exit($cmp != 0 ? 0 : 1);
+    }
+
+    exit($cmp == 0 ? 0 : 1);
 }
 if ($opt_v) {
     $VerboseFlag = 1;


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-ports" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?86hf726a30.wl>