[crux-devel] [PATCH] Add optional pattern matching for -i option

Thomas Penteker tek at serverop.de
Thu Aug 4 15:11:34 UTC 2011


Hello list,

I rolled a little patch adding pattern matching for pkginfo's -i option.
Suggestions/criticism, anyone?

---
 pkginfo.8.in |    5 +++--
 pkginfo.cc   |   22 +++++++++++++++++++++-
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/pkginfo.8.in b/pkginfo.8.in
index ee0da6c..bdd84c0 100644
--- a/pkginfo.8.in
+++ b/pkginfo.8.in
@@ -9,8 +9,9 @@ information about software packages that are installed on the system
 or that reside in a particular directory.
 .SH OPTIONS
 .TP
-.B "\-i, \-\-installed"
-List installed packages and their version.
+.B "\-i, \-\-installed [<pattern>]"
+List installed packages and their version. Optionally, list only packages
+matching pattern.
 .TP
 .B "\-l, \-\-list <package|file>"
 List files owned by the specified <package> or contained in <file>.
diff --git a/pkginfo.cc b/pkginfo.cc
index fb4d993..b20e78f 100644
--- a/pkginfo.cc
+++ b/pkginfo.cc
@@ -36,6 +36,7 @@ void pkginfo::run(int argc, char** argv)
 	int o_installed_mode = 0;
 	int o_list_mode = 0;
 	int o_owner_mode = 0;
+	int o_grep_mode = 0;
 	string o_root;
 	string o_arg;
 
@@ -47,6 +48,16 @@ void pkginfo::run(int argc, char** argv)
 			i++;
 		} else if (option == "-i" || option == "--installed") {
 			o_installed_mode += 1;
+			try {
+				assert_argument(argv, argc, i);
+				o_grep_mode = 1;
+				o_arg = argv[i + 1];
+				i++;
+			} catch (exception &e) {
+				//
+				// argument is optional
+				//
+			}
 		} else if (option == "-l" || option == "--list") {
 			assert_argument(argv, argc, i);
 			o_list_mode += 1;
@@ -91,8 +102,17 @@ void pkginfo::run(int argc, char** argv)
 			//
 			// List installed packages
 			//
+			regex_t preg;
+			if (o_grep_mode)
+				if (regcomp(&preg, o_arg.c_str(), REG_EXTENDED | REG_NOSUB))
+					throw runtime_error("error compiling regular expression '" + o_arg + "', aborting");
+
 			for (packages_t::const_iterator i = packages.begin(); i != packages.end(); ++i)
-				cout << i->first << ' ' << i->second.version << endl;
+				if (!o_grep_mode || !regexec(&preg, i->first.c_str(), 0, 0, 0)) 
+					cout << i->first << ' ' << i->second.version << endl;
+
+			if (o_grep_mode)
+				regfree(&preg);
 		} else if (o_list_mode) {
 			//
 			// List package or file contents
-- 
1.7.4.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.crux.nu/pipermail/crux-devel/attachments/20110804/4228487e/attachment.asc>


More information about the crux-devel mailing list