summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/portsearch
blob: 39ea41a21ff9f9ce00464e7cbc07cea9d0f367b0 (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
#!/usr/bin/perl
#-
# Copyright (c) 2000 Mark Ovens
# All rights reserved.
#
# 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
#    in this position and unchanged.
# 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 AUTHOR ``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 AUTHOR 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.
#
#      $Id$
#


# Each port record in INDEX contains 10 fields, delimited by ``|'', some
# of which may be empty. The fields are:
#
# distribution-name|port-path|installation-prefix|comment| \
# description-file|maintainer|categories|build deps|run deps|www site


use strict;
use Getopt::Std;
use vars qw/ $key @list %fields %list %opts /;

#
# Global variables
#

my $osrel = `/usr/bin/uname -r`;
$osrel =~ s/\..+$//s;

my $VERSION = "1.0";
my $file = "/usr/ports/INDEX" . ($osrel <= 4 ? "" : "-$osrel");
my $match = 1;
my $count = 0;

# We only need 7 of the 10 fields in a record; define which ones in a
# hash slice to ignore the un-needed ones. This also makes it easy to
# add or remove fields in the future.

@fields{qw(n p i m x b r)} = (0, 1, 3, 5, 6, 7, 8);

#
# Print a basic help message
#

sub usage() {
    print(STDERR "
Usage: portsearch [-h] [-n name] [-p path] [-i info] [-m maint] [-x index]
		[-b b_deps] [-r r_deps] [-d deps] [-f file]

");
} # usage()

#
# Print a more verbose help message
#

sub help() {
    print(STDERR "portsearch $VERSION - A utility for searching the ports tree.

Options:

  -n name	Search for \"name\" in name of ports
  -p path	Search for \"path\" in location of ports
  -i info	Search for \"info\" in ports COMMENT
  -m maint	Search for \"maint\" in ports Maintainer
  -x index	Search for \"index\" in ports categories
  -b b_deps	Search for \"b_deps\" in build depends of ports
  -r r_deps	Search for \"r_deps\" in run depends of ports
  -d deps	Search for \"deps\" in both build & run depends of ports
  -f file	Use \"file\" instead of /usr/ports/INDEX
  -h		Print this message and exit

Report bugs to <marko\@freebsd.org>.

");
} # help()

#
# The program proper
#

MAIN: {
				# No command-line args
    if ($#ARGV == -1) {
	usage();
	exit(1);
    }

    getopt('fnpimxbrd', \%opts);
				# Command-line args, but without options
    if (keys(%opts) == 0 ) {
	usage();
	exit(1);
    }
				# If ``-h'', ignore any other options
    if (defined($opts{"h"})) {
	help();
	exit(1);
    }
				# A different INDEX file
    if (defined($opts{"f"})) {
	$file = $opts{"f"};
    }
				# If ``-d'' used we don't want ``-b'' & ``-r''
    if (defined($opts{"d"})) {
	delete $opts{"b"};
	delete $opts{"r"};
    }
				# Finished with it now so remove it from hash
    delete $opts{"f"};
	
    open(INDEX, "$file") || die "Unable to open $file";

    while (<INDEX>) {
	chomp;
	@list = split(/\|/);

        $match = 1;
				# All searches are case-insensitive!
				# For ``-d'' search both build & run depends.
				# Only fail to match if not found in either.
	foreach $key (keys (%opts)) {
	    if ($key eq "d") {
		if ($list[$fields{"b"}] !~ m#$opts{$key}#i &&
				  $list[$fields{"r"}] !~ m#$opts{$key}#i) {
		    $match = 0;
		    last;
		    }
	    } else {
		if ($list[$fields{$key}] !~ m#$opts{$key}#i) {
		    $match = 0;
		    last;
		}
	    }
	} # foreach

	if ($match == 1) {
	    $count++;
	    write;
	}

    } # while

    close(INDEX);

    print ("Number of matching ports = $count\n\n");

} # MAIN


format STDOUT =

Port:	@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[0]
Path:	@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[1]
Info:	^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[3]
~~      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[3]
Maint:	@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[5]
Index:	@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[6]
B-deps:	^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[7]
~~      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[7]
R-deps:	^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[8]
~~      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$list[8]

.
OpenPOWER on IntegriCloud