Blame SOURCES/rpmsort

7689d7
#! /usr/bin/perl -w
7689d7
7689d7
# This program is free software; you can redistribute it and/or
7689d7
# modify it under the terms of the GNU General Public License
7689d7
# as published by the Free Software Foundation; either version 2
7689d7
# of the License, or (at your option) any later version.
7689d7
#
7689d7
# This program is distributed in the hope that it will be useful,
7689d7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
7689d7
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7689d7
# GNU General Public License for more details.
7689d7
#
7689d7
# You should have received a copy of the GNU General Public License
7689d7
# along with this program; if not, write to the Free Software
7689d7
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
7689d7
# USA.
7689d7
7689d7
use Getopt::Long qw(:config gnu_getopt);
7689d7
7689d7
sub rpm_cmp_versions {
7689d7
    my ($evr1, $evr2) = @_;
7689d7
7689d7
    sub _rpm_cmp {
7689d7
	my ($s1, $s2) = @_;
7689d7
7689d7
	return defined $s1 <=> defined $s2
7689d7
	    unless defined $s1 && defined $s2;
7689d7
7689d7
	my ($r, $x1, $x2);
7689d7
	do {
7689d7
	    $s1 =~ s/^[^a-zA-Z0-9]+//;
7689d7
	    $s2 =~ s/^[^a-zA-Z0-9]+//;
7689d7
	    if ($s1 =~ /^\d/ || $s2 =~ /^\d/) {
7689d7
		$s1 =~ s/^0*(\d*)//;  $x1 = $1;
7689d7
		$s2 =~ s/^0*(\d*)//;  $x2 = $1;
7689d7
		$r = length $x1 <=> length $x2 || $x1 cmp $x2;
7689d7
	    } else {
7689d7
		$s1 =~ s/^([a-zA-Z]*)//;  $x1 = $1;
7689d7
		$s2 =~ s/^([a-zA-Z]*)//;  $x2 = $1;
7689d7
		return 0
7689d7
		    if $x1 eq '' && $x2 eq '';
7689d7
		$r = $x1 cmp $x2;
7689d7
	    }
7689d7
	} until $r;
7689d7
	return $r;
7689d7
    }
7689d7
7689d7
    my ($e1, $v1, $r1) = $evr1 =~ /^(?:(\d*):)?(.*?)(?:-([^-]*))?$/;
7689d7
    my ($e2, $v2, $r2) = $evr2 =~ /^(?:(\d*):)?(.*?)(?:-([^-]*))?$/;
7689d7
    my $r = _rpm_cmp($e1 || 0, $e2 || 0);
7689d7
    $r = _rpm_cmp($v1, $v2)
7689d7
	unless $r;
7689d7
    $r = _rpm_cmp($r1, $r2)
7689d7
	unless $r;
7689d7
    return $r;
7689d7
}
7689d7
7689d7
my $reorder = sub { return @_ };
7689d7
my $key = 0;
7689d7
7689d7
GetOptions ("r|reverse"	    => sub { $reorder = sub { return reverse @_ } },
7689d7
	    "k|key=i"	    => \$key)
7689d7
or do {
7689d7
    print STDERR "Usage\n";
7689d7
    exit 1;
7689d7
};
7689d7
7689d7
if ($key == 0) {
7689d7
    # Sort by entire lines
7689d7
    map { print } &$reorder(sort { rpm_cmp_versions($a, $b) } <>);
7689d7
} else {
7689d7
    # Sort by field $key
7689d7
    my @data = map { [(split)[$key-1], $_] } <>;
7689d7
    map { print } &$reorder(map { $_->[1] }
7689d7
        sort { rpm_cmp_versions($a->[0], $b->[0]) } @data);
7689d7
}