summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/OpenBSD/PkgSpec.pm
blob: 250ba48931bc46ad1e02d842d09d2bdcba2e2ffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# ex:ts=8 sw=4:
# $OpenBSD: PkgSpec.pm,v 1.27 2010/01/07 13:11:21 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use strict;
use warnings;

package OpenBSD::PkgSpec::flavorspec;
sub new
{
	my ($class, $spec) = @_;

	$spec =~ s/^-//o;

	bless \$spec, $class;
}

sub check_1flavor
{
	my ($f, $spec) = @_;

	for my $_ (split /\-/o, $spec) {
		# must not be here
		if (m/^\!(.*)$/o) {
			return 0 if $f->{$1};
		# must be here
		} else {
			return 0 unless $f->{$_};
		}
	}
	return 1;
}

sub match
{
	my ($self, $h) = @_;

	# check each flavor constraint
	for my $_ (split /\,/o, $$self) {
		if (check_1flavor($h->{flavors}, $_)) {
			return 1;
		}
	}
	return 0;
}

package OpenBSD::PkgSpec::exactflavor;
our @ISA = qw(OpenBSD::PkgSpec::flavorspec);
sub match
{
	my ($self, $h) = @_;
	if ($$self eq $h->flavor_string) {
		return 1;
	} else {
		return 0;
	}
}

package OpenBSD::PkgSpec::versionspec;
sub new
{
	my ($class, $s) = @_;
	my $spec = OpenBSD::PackageName::versionspec->from_string($s);
	bless \$spec, $class;
}

sub match
{
	my ($self, $name) = @_;

	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;

my $exception = {
	"db-3.*" => "db->=3,<4",
	"db-4.*" => "db->=4,<5",
	"db-java-4.*" => "db-java->=4,<5",
	"emacs-21.*" => "emacs->=21,<22",
	"emacs-21.4*" => "emacs->=21.4,<21.5",
	"emacs-22.2*" => "emacs->=22.2,<22.3",
	"enlightenment-0.16*" => "enlightenment->=0.16,<0.17",
	"gimp-2.*" => "gimp->=2,<3",
	"gnupg->=1.4.*" => "gnupg->=1.4",
	"gstreamer-0.10.*" => "gstreamer->=0.10,<0.11",
	"gtksourceview-2.*" => "gtksourceview->=2,<3",
	"hydra-5.4*" => "hydra->=5.4,<5.5",
	"jdk->=1.5.0.*" => "jdk->=1.5.0",
	"jdk->=1.6.0.*" => "jdk->=1.6.0",
	"jre->=1.5.0.*" => "jre->=1.5.0",
	"libggi->=0.9*" => "libggi->=0.9",
	"libnet-1.0*" => "libnet->=1.0,<1.1",
	"libnet-1.0.*" => "libnet->=1.0,<1.1",
	"libnet-1.1*" => "libnet->=1.1,<1.2",
	"libsigc++-1.*" => "libsigc++->=1,<2",
	"libsigc++-2.*" => "libsigc++->=2,<3",
	"mysql-client-5.0.*" => "mysql-client->=5.0,<5.1",
	"ocaml-3.09.3*" => "ocaml->=3.09.3,<3.09.4",
	"openldap-client-2.*" => "openldap-client->=2,<3",
	"pgp-5.*" => "pgp->=5,<6",
	"postgresql-client-8.3.*" => "postgresql-client->=8.3,<8.4",
	"python-2.4*" => "python->=2.4,<2.5",
	"python-2.4.*" => "python->=2.4,<2.5",
	"python-2.5*" => "python->=2.5,<2.6",
	"python-2.5.*" => "python->=2.5,<2.6",
	"python-2.6.*" => "python->=2.6,<2.7",
	"python-bsddb-2.5*" => "python-bsddb->=2.5,<2.6",
	"python-tkinter-2.4*" => "python-tkinter->=2.4,<2.5",
	"python-tkinter-2.5*" => "python-tkinter->=2.5,<2.6",
	"rrdtool-1.2.*" => "rrdtool->=1.2,<1.3",
	"swt-3.2.2*" => "swt->=3.2.2,<3.2.3",
	"swt-browser-3.2.2*" => "swt-browser->=3.2.2,<3.2.3",
	"tcl-8.4.*" => "tcl->=8.4,<8.5",
	"tcl-8.5.*" => "tcl->=8.5,<8.6",
	"tk-8.4*" => "tk->=8.4,<8.5",
	"tk-8.4.*" => "tk->=8.4,<8.5",
	"tk-8.5*" => "tk->=8.5,<8.6",
	"tomcat-4.*" => "tomcat->=4,<5",
	"tomcat-5.*" => "tomcat->=5,<6",
	"tomcat-6.*" => "tomcat->=6,<7",
	"tomcat-admin-4.*" => "tomcat-admin->=4,<5",
	"tomcat-admin-5.*" => "tomcat-admin->=5,<6",
	"xmms-1.2.11*" => "xmms->=1.2.11,<1.2.12"
};

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|\*)[^-]*)(.*)$/) {
		return undef;
	}
	($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
{
	my ($class, $constraints, $vspec) = @_;

	# turn the vspec into a list of constraints.
	if ($vspec eq '*') {
		# non constraint
	} else {
		for my $c (split /\,/, $vspec) {
			push(@$constraints, 
			    OpenBSD::PkgSpec::versionspec->new($c));
		}
	}
}

sub add_flavor_constraints
{
	my ($class, $constraints, $flavorspec) = @_;
	# and likewise for flavors
	if ($flavorspec eq '') {
		# non constraint
	} else {
		push(@$constraints, 
		    OpenBSD::PkgSpec::flavorspec->new($flavorspec));
	}
}

sub new
{
	my ($class, $p) = @_;

	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
{
	my ($o, $list) = @_;
	my @result = ();
	# Now, have to extract the version number, and the flavor...
LOOP1:
	for my $s (grep(/$o->{fuzzystem}/, @$list)) {
		my $name = OpenBSD::PackageName->from_string($s);
		next unless $name->{stem} =~ m/^$o->{exactstem}$/;
		for my $c (@{$o->{constraints}}) {
			next LOOP1 unless $c->match($name);
		}
		push(@result, $s);
	}
		
	return @result;
}

sub match_locations
{
	my ($o, $list) = @_;
	my $result = [];
	# Now, have to extract the version number, and the flavor...
LOOP2:
	for my $s (grep { $_->name =~ m/$o->{fuzzystem}/} @$list) {
		my $name = $s->pkgname;
		next unless $name->{stem} =~ m/^$o->{exactstem}$/;
		for my $c (@{$o->{constraints}}) {
			next LOOP2 unless $c->match($name);
		}
		push(@$result, $s);
	}
		
	return $result;
}

sub is_valid
{
	return !defined shift->{e};
}

package OpenBSD::PkgSpec;
sub subpattern_class
{ "OpenBSD::PkgSpec::SubPattern" }
sub new
{
	my ($class, $pattern) = @_;
	my @l = map { $class->subpattern_class->new($_) } 
		(split /\|/o, $pattern);
	if (@l == 1) {
		return $l[0];
	} else {
		return bless \@l, $class;
	}
}

sub match_ref
{
	my ($self, $r) = @_;
	my @l = ();
	for my $subpattern (@$self) {
		push(@l, $subpattern->match_ref($r));
	}
	return @l;
}

sub match_locations
{
	my ($self, $r) = @_;
	my $l = [];
	for my $subpattern (@$self) {
		push(@$l, @{$subpattern->match_locations($r)});
	}
	return $l;
}

sub is_valid
{
	my $self = shift;
	for my $subpattern (@$self) {
		return 0 unless $subpattern->is_valid;
	}
	return 1;
}

package OpenBSD::PkgSpec::SubPattern::Exact;
our @ISA = qw(OpenBSD::PkgSpec::SubPattern);

sub add_version_constraints
{
	my ($class, $constraints, $vspec) = @_;
	return if $vspec eq '*'; # XXX
	my $v = OpenBSD::PkgSpec::versionspec->new($vspec);
	die "not a good exact spec" if !$$v->is_exact;
	delete $$v->{p};
	push(@$constraints, $v);
}

sub add_flavor_constraints
{
	my ($class, $constraints, $flavorspec) = @_;
	push(@$constraints, OpenBSD::PkgSpec::exactflavor->new($flavorspec));
}

package OpenBSD::PkgSpec::Exact;
our @ISA = qw(OpenBSD::PkgSpec);

sub subpattern_class
{ "OpenBSD::PkgSpec::SubPattern::Exact" }

1;