summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/pkg_info
blob: d7bce4a0b43b3e0b62a21ce8841fe1094c0d6142 (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
#! /usr/bin/perl
# ex:ts=8 sw=4:
# $OpenBSD: pkg_info,v 1.3 2003/10/23 17:48:07 espie Exp $
#
# Copyright (c) 2003 Marc Espie.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
# PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use warnings;
use OpenBSD::PackageInfo;
use OpenBSD::PackageName;
use Getopt::Std;
use File::Copy;
# XXX we don't want to load these packages all the time
package OpenBSD::PackageLocator;
our $AUTOLOAD;
sub AUTOLOAD {
	eval { require OpenBSD::PackageLocator;
	};
	goto &$AUTOLOAD;
}

package OpenBSD::PackingList;
our $AUTOLOAD;
sub AUTOLOAD {
	eval { require OpenBSD::PackingList;
	};
	goto &$AUTOLOAD;
}

package main;

sub get_line
{
	open my $fh, '<', shift or return "";
	my $c = <$fh>;
	chomp($c);
	close $fh;
	return $c;
}

our ($opt_c, $opt_D, $opt_d, $opt_f, $opt_I, $opt_i, $opt_k, $opt_L, $opt_m,
    $opt_p, $opt_q, $opt_R, $opt_r, $opt_v, $opt_h, $opt_e, $opt_l, $opt_a);

sub print_info
{
	my ($pkg, $dir) = @_;
	unless (-d $dir) {
		print STDERR "Error printing info for $pkg: no info ?\n";
	}
	if ($opt_I) {
		my $l = 20 - length($pkg);
		$l = 1 if $l <= 0;
		print $pkg, " "x$l, get_line($dir.COMMENT), "\n";
	} else {
		print $opt_p, "Information for ", $pkg, "\n\n" unless $opt_q;
		if ($opt_c) {
			print $opt_p, "Comment:\n" unless $opt_q;
			copy($dir.COMMENT, \*STDOUT);
			print "\n";
		}
		if ($opt_R && -f $dir.REQUIRED_BY) {
			print $opt_p, "Required by:\n" unless $opt_q;
			copy($dir.REQUIRED_BY, \*STDOUT);
			print "\n";
		}
		if ($opt_d) {
			print $opt_p, "Description:\n" unless $opt_q;
			copy($dir.DESC, \*STDOUT);
			print "\n";
		}
		if ($opt_D && -f $dir.DISPLAY) {
			print $opt_p, "Install notice:\n" unless $opt_q;
			copy($dir.DISPLAY, \*STDOUT);
			print "\n";
		}
		if ($opt_i && -f $dir.INSTALL) {
			print $opt_p, "Install script:\n" unless $opt_q;
			copy($dir.INSTALL, \*STDOUT);
			print "\n";
		}
		if ($opt_k && -f $dir.DEINSTALL) {
			print $opt_p, "De-Install script:\n" unless $opt_q;
			copy($dir.DEINSTALL, \*STDOUT);
			print "\n";
		}
		if ($opt_r && -f $dir.REQUIRE) {
			print $opt_p, "Require script:\n" unless $opt_q;
			copy($dir.REQUIRE, \*STDOUT);
			print "\n";
		}
		my $plist;
		if ($opt_f || $opt_L) {
			if ($opt_f) {
				$plist = OpenBSD::PackingList->fromfile($dir.CONTENTS);
			} else {
				$plist = OpenBSD::PackingList->fromfile($dir.CONTENTS, \&OpenBSD::PackingList::FilesOnly);
			}
			die "Bad packing list" unless defined $plist;
		}
		if ($opt_L) {
			print $opt_p, "Files:\n" unless $opt_q;
			for my $item (@{$plist->{items}}) {
				next unless $item->IsFile();
				print $item->fullname(), "\n";
			}
			print "\n";
		}
		if ($opt_f) {
			print $opt_p, "Packing list:\n" unless $opt_q;
			$plist->write(\*STDOUT);
			print "\n";
		}
		print $opt_p, "\n" unless $opt_q;
	}
}

getopts('cDdfIikLmpqRrvhe:l:a');

if (defined $opt_e) {
	die "Error: -e is not yet supported";
}

unless ($opt_c || $opt_D || $opt_d || $opt_f || $opt_I || $opt_i ||
	$opt_k || $opt_L || $opt_m || $opt_p || $opt_R || $opt_r) {
	if (@ARGV == 0) {
		$opt_I = $opt_a = 1;
	} else {
		$opt_c = $opt_d = $opt_R = 1;
	}
}

if ($opt_v) {
	$opt_c = $opt_d = $opt_f = $opt_i = $opt_k = $opt_r = $opt_D =
	    $opt_m = $opt_R = 1;
}

if (!defined $opt_p) {
	$opt_p = "";
}

if (@ARGV == 0 && !$opt_a) {
	die "Missing package name(s)";
}

if (@ARGV > 0 && $opt_a) {
	die "Can't specify package name(s) with -a";
}

if (@ARGV == 0) {
	@ARGV = sort(installed_packages());
}

for my $pkg (@ARGV) {
	if (is_installed($pkg)) {
		print_info($pkg, installed_info($pkg));
		next;
	}
	if (OpenBSD::PackageName::is_stem($pkg)) {
		my @l = sort (OpenBSD::PackageName::findstem($pkg, installed_packages()));
		if (@l != 0) {
			for my $p (@l) {
				print_info($p, installed_info($p));
			}
			next;
		}
	}
				
	my $true_package = OpenBSD::PackageLocator->find($pkg);
	next unless $true_package;
	my $dir = $true_package->info();
	$true_package->close();

	print_info($pkg, $dir);
}