4 // Copyright (c) 2000-2005 Per Liden
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
26 #include <sys/types.h>
29 void pkginfo::run(int argc
, char** argv
)
32 // Check command line options
34 int o_footprint_mode
= 0;
35 int o_installed_mode
= 0;
41 for (int i
= 1; i
< argc
; ++i
) {
42 string
option(argv
[i
]);
43 if (option
== "-r" || option
== "--root") {
44 assert_argument(argv
, argc
, i
);
47 } else if (option
== "-i" || option
== "--installed") {
48 o_installed_mode
+= 1;
49 } else if (option
== "-l" || option
== "--list") {
50 assert_argument(argv
, argc
, i
);
54 } else if (option
== "-o" || option
== "--owner") {
55 assert_argument(argv
, argc
, i
);
59 } else if (option
== "-f" || option
== "--footprint") {
60 assert_argument(argv
, argc
, i
);
61 o_footprint_mode
+= 1;
65 throw runtime_error("invalid option " + option
);
69 if (o_footprint_mode
+ o_installed_mode
+ o_list_mode
+ o_owner_mode
== 0)
70 throw runtime_error("option missing");
72 if (o_footprint_mode
+ o_installed_mode
+ o_list_mode
+ o_owner_mode
> 1)
73 throw runtime_error("too many options");
75 if (o_footprint_mode
) {
82 // Modes that require the database to be opened
85 db_lock
lock(o_root
, false);
89 if (o_installed_mode
) {
91 // List installed packages
93 for (packages_t::const_iterator i
= packages
.begin(); i
!= packages
.end(); ++i
)
94 cout
<< i
->first
<< ' ' << i
->second
.version
<< endl
;
95 } else if (o_list_mode
) {
97 // List package or file contents
99 if (db_find_pkg(o_arg
)) {
100 copy(packages
[o_arg
].files
.begin(), packages
[o_arg
].files
.end(), ostream_iterator
<string
>(cout
, "\n"));
101 } else if (file_exists(o_arg
)) {
102 pair
<string
, pkginfo_t
> package
= pkg_open(o_arg
);
103 copy(package
.second
.files
.begin(), package
.second
.files
.end(), ostream_iterator
<string
>(cout
, "\n"));
105 throw runtime_error(o_arg
+ " is neither an installed package nor a package file");
109 // List owner(s) of file or directory
112 if (regcomp(&preg
, o_arg
.c_str(), REG_EXTENDED
| REG_NOSUB
))
113 throw runtime_error("error compiling regular expression '" + o_arg
+ "', aborting");
115 vector
<pair
<string
, string
> > result
;
116 result
.push_back(pair
<string
, string
>("Package", "File"));
117 unsigned int width
= result
.begin()->first
.length(); // Width of "Package"
119 for (packages_t::const_iterator i
= packages
.begin(); i
!= packages
.end(); ++i
) {
120 for (set
<string
>::const_iterator j
= i
->second
.files
.begin(); j
!= i
->second
.files
.end(); ++j
) {
121 const string
file('/' + *j
);
122 if (!regexec(&preg
, file
.c_str(), 0, 0, 0)) {
123 result
.push_back(pair
<string
, string
>(i
->first
, *j
));
124 if (i
->first
.length() > width
)
125 width
= i
->first
.length();
132 if (result
.size() > 1) {
133 for (vector
<pair
<string
, string
> >::const_iterator i
= result
.begin(); i
!= result
.end(); ++i
) {
134 cout
<< left
<< setw(width
+ 2) << i
->first
<< i
->second
<< endl
;
137 cout
<< utilname
<< ": no owner(s) found" << endl
;
143 void pkginfo::print_help() const
145 cout
<< "usage: " << utilname
<< " [options]" << endl
146 << "options:" << endl
147 << " -i, --installed list installed packages" << endl
148 << " -l, --list <package|file> list files in <package> or <file>" << endl
149 << " -o, --owner <pattern> list owner(s) of file(s) matching <pattern>" << endl
150 << " -f, --footprint <file> print footprint for <file>" << endl
151 << " -r, --root <path> specify alternative installation root" << endl
152 << " -v, --version print version and exit" << endl
153 << " -h, --help print help and exit" << endl
;