summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2010-01-05 12:20:48 +0000
committerMarc Espie <espie@cvs.openbsd.org>2010-01-05 12:20:48 +0000
commit1a02a5c913ffadbcacaebb4713b0ea255c42f568 (patch)
tree45aea285b876558925baabfca342e411f25f26dd /usr.sbin/pkg_add
parent723d25dd71d2787a0ec8a189ee7330c5374f729b (diff)
better spec handling: instead of dying, create badspec objects that never
match anything. Add method is_valid (and propagators) to know whether a spec is valid (to be used in pkg_create in a systematic way).
Diffstat (limited to 'usr.sbin/pkg_add')
-rw-r--r--usr.sbin/pkg_add/OpenBSD/PkgSpec.pm87
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Search.pm8
-rw-r--r--usr.sbin/pkg_add/pkg_info12
3 files changed, 83 insertions, 24 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/PkgSpec.pm b/usr.sbin/pkg_add/OpenBSD/PkgSpec.pm
index 0862d0ec617..7182e4c823f 100644
--- a/usr.sbin/pkg_add/OpenBSD/PkgSpec.pm
+++ b/usr.sbin/pkg_add/OpenBSD/PkgSpec.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: PkgSpec.pm,v 1.25 2009/12/30 19:04:06 espie Exp $
+# $OpenBSD: PkgSpec.pm,v 1.26 2010/01/05 12:20:47 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
@@ -84,6 +84,27 @@ sub match
return $$self->match($name->{version});
}
+package OpenBSD::PkgSpec::badspec;
+sub new
+{
+ my $class = shift;
+ bless {}, $class;
+}
+
+sub match_ref
+{
+ return ();
+}
+
+sub match_locations
+{
+ return ();
+}
+
+sub is_valid
+{
+ return 0;
+}
package OpenBSD::PkgSpec::SubPattern;
use OpenBSD::PackageName;
@@ -143,22 +164,25 @@ sub parse
{
my ($class, $p) = @_;
+ my $r = {};
+
if (defined $exception->{$p}) {
$p = $exception->{$p};
+ $r->{e} = 1;
}
# let's try really hard to find the stem and the flavors
unless ($p =~ m/^(.*?)\-((?:(?:\>|\>\=|\<\=|\<|\=)?\d|\*)[^-]*)(.*)$/) {
- die "Invalid spec $p";
+ return undef;
}
- my ($stemspec, $vspec, $flavorspec) = ($1, $2, $3);
-
- $stemspec =~ s/\./\\\./go;
- $stemspec =~ s/\+/\\\+/go;
- $stemspec =~ s/\*/\.\*/go;
- $stemspec =~ s/\?/\./go;
- $stemspec =~ s/^(\\\.libs)\-/$1\\d*\-/go;
- return ($stemspec, $vspec, $flavorspec);
+ ($r->{stemspec}, $r->{vspec}, $r->{flavorspec}) = ($1, $2, $3);
+
+ $r->{stemspec} =~ s/\./\\\./go;
+ $r->{stemspec} =~ s/\+/\\\+/go;
+ $r->{stemspec} =~ s/\*/\.\*/go;
+ $r->{stemspec} =~ s/\?/\./go;
+ $r->{stemspec} =~ s/^(\\\.libs)\-/$1\\d*\-/go;
+ return $r;
}
sub add_version_constraints
@@ -192,16 +216,25 @@ sub new
{
my ($class, $p) = @_;
- my ($stemspec, $vspec, $flavorspec) = $class->parse($p);
- my $constraints = [];
- $class->add_version_constraints($constraints, $vspec);
- $class->add_flavor_constraints($constraints, $flavorspec);
-
- bless {
- exactstem => qr{^$stemspec$},
- fuzzystem => qr{^$stemspec\-\d.*$},
- constraints => $constraints,
- }, $class;
+ my $r = $class->parse($p);
+ if (defined $r) {
+ my $stemspec = $r->{stemspec};
+ my $constraints = [];
+ $class->add_version_constraints($constraints, $r->{vspec});
+ $class->add_flavor_constraints($constraints, $r->{flavorspec});
+
+ my $o = bless {
+ exactstem => qr{^$stemspec$},
+ fuzzystem => qr{^$stemspec\-\d.*$},
+ constraints => $constraints,
+ }, $class;
+ if (defined $r->{e}) {
+ $o->{e} = 1;
+ }
+ return $o;
+ } else {
+ return OpenBSD::PkgSpec::badspec->new;
+ }
}
sub match_ref
@@ -240,6 +273,11 @@ LOOP2:
return $result;
}
+sub is_valid
+{
+ return !defined shift->{e};
+}
+
package OpenBSD::PkgSpec;
sub subpattern_class
{ "OpenBSD::PkgSpec::SubPattern" }
@@ -275,6 +313,15 @@ sub match_locations
return $l;
}
+sub is_valid
+{
+ my $self = @_;
+ for my $subpattern (@$self) {
+ return 0 unless $subpattern->is_valid;
+ }
+ return 1;
+}
+
package OpenBSD::PkgSpec::SubPattern::Exact;
our @ISA = qw(OpenBSD::PkgSpec::SubPattern);
diff --git a/usr.sbin/pkg_add/OpenBSD/Search.pm b/usr.sbin/pkg_add/OpenBSD/Search.pm
index 7568564ea37..278a53f4908 100644
--- a/usr.sbin/pkg_add/OpenBSD/Search.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Search.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: Search.pm,v 1.20 2010/01/05 11:31:07 espie Exp $
+# $OpenBSD: Search.pm,v 1.21 2010/01/05 12:20:47 espie Exp $
#
# Copyright (c) 2007 Marc Espie <espie@openbsd.org>
#
@@ -67,6 +67,12 @@ sub add_pkgpath_hint
sub spec_class
{ "OpenBSD::PkgSpec" }
+sub is_valid
+{
+ my $self = shift;
+ return $self->{spec}->is_valid;
+}
+
package OpenBSD::Search::Exact;
our @ISA=(qw(OpenBSD::Search::PkgSpec));
sub spec_class
diff --git a/usr.sbin/pkg_add/pkg_info b/usr.sbin/pkg_add/pkg_info
index 9cabcb4a973..a6be189d756 100644
--- a/usr.sbin/pkg_add/pkg_info
+++ b/usr.sbin/pkg_add/pkg_info
@@ -1,6 +1,6 @@
#! /usr/bin/perl
# ex:ts=8 sw=4:
-# $OpenBSD: pkg_info,v 1.83 2010/01/02 12:52:18 espie Exp $
+# $OpenBSD: pkg_info,v 1.84 2010/01/05 12:20:47 espie Exp $
#
# Copyright (c) 2003-2010 Marc Espie <espie@openbsd.org>
#
@@ -163,9 +163,15 @@ sub find_by_spec
require OpenBSD::Search;
require OpenBSD::PackageRepository::Installed;
- my $r = OpenBSD::PackageRepository::Installed->new->match_locations(OpenBSD::Search::PkgSpec->new($pat));
+ my $s = OpenBSD::Search::PkgSpec->new($pat);
+ if (!$s->is_valid) {
+ print STDERR "Invalid spec: $pat\n";
+ return ();
+ } else {
+ my $r = OpenBSD::PackageRepository::Installed->new->match_locations($s);
- return sort (map {$_->name} @$r);
+ return sort (map {$_->name} @$r);
+ }
}
sub filter_files