[PATCH] Add a --query option to update-alternatives. Closes: #336091, #441904
* scripts/update-alternatives.pl: add a --query option that works like
--display but outputs a machine parseable rfc822-like output.
* man/update-alternatives.8: document that new option, and the
associated format.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
ChangeLog | 7 ++++
debian/changelog | 3 ++
man/update-alternatives.8 | 73 ++++++++++++++++++++++++++++++++++++++++
scripts/update-alternatives.pl | 45 +++++++++++++++++++++++--
4 files changed, 125 insertions(+), 3 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 52fb487..699d4b1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-05-11 Pierre Habouzit <madcoder@debian.org>
+
+ * scripts/update-alternatives.pl: add a --query option that works like
+ --display but outputs a machine parseable rfc822-like output.
+ * man/update-alternatives.8: document that new option, and the
+ associated format.
+
2008-05-05 Raphael Hertzog <hertzog@debian.org>
* scripts/dpkg-gensymbols.pl: Do not accept empty parameters for
diff --git a/debian/changelog b/debian/changelog
index 77542e6..86eeb29 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -13,6 +13,9 @@ dpkg (1.15.0) UNRELEASED; urgency=low
[ Updated dpkg translations ]
* Portuguese (Miguel Figueiredo).
+ [ Pierre Habouzit ]
+ * Add a --query option to update-alternatives. Closes: #336091, #441904
+
-- Guillem Jover <guillem@debian.org> Tue, 29 Apr 2008 06:01:40 +0300
dpkg (1.14.19) UNRELEASED; urgency=low
diff --git a/man/update-alternatives.8 b/man/update-alternatives.8
index 6029c0a..14f4b88 100644
--- a/man/update-alternatives.8
+++ b/man/update-alternatives.8
@@ -283,6 +283,12 @@ what other alternatives are available
(and their corresponding slave alternatives),
and the highest priority alternative currently installed.
.TP
+\fB\-\-query\fR \fIlink\fR
+Display information about the link group of which
+.I link
+is the master link like \-\-display does, but in a machine parseable way.
+(see section \fBQUERY FORMAT\fR below).
+.TP
\fB\-\-list\fR \fIlink\fR
Display all targets of the link group.
.TP
@@ -339,6 +345,73 @@ The requested action was successfully performed.
Problems were encountered whilst parsing the command line
or performing the action.
.
+.SH "QUERY FORMAT"
+The update\-alternatives \-\-query format is using an RFC822-like flat format,
+qite usual in Debian tools. It's made of n + 1 blocks. The first block is
+made of:
+.TP
+.BR Link: " <link>"
+The name of the alternative, what you passed as a \-\-query argument.
+.BR Status: " <status>"
+The status of the alternative (\fBauto\fR or \fBmanual\fR).
+.TP
+.BR Best: " <best choice>"
+The path to the best choice available for this alternative yet. Not present if
+there is no possible choice available.
+.TP
+.BR Value: " <value of the alternative> "
+The path to the current value of the alternative. It can also take the magic
+values \fBnone\fR or \fBerror\fR. The former is used if the link doesn't
+exist, the latter case, if an error occured.
+.TP
+.BR Error: " <error details>"
+When \fBValue\fR is \fBerror\fR, this is present and contains a human readable
+string explaning the error.
+.
+.TP
+The other blocks details the possible values that exist for this alternative:
+.TP
+.BR Alternative: " <path the this alternative>"
+Path to this block's alternative.
+.TP
+.BR Priority: " <priority value>"
+Value of the priority of this alternative.
+.TP
+.BR Slaves: " <list of slaves>"
+When this header is present, the \fBnext\fR lines hold all slave alternatives
+to this master one. There is one slave per line, with one space, the slave
+name, another space, and the path to the slave link value.
+.
+.TP
+.BR Example
+.nf
+$ update\-alternatives \-\-query editor
+Link: editor
+Status: auto
+Best: /usr/bin/vim.gtk
+Value: /usr/bin/vim.gtk
+
+Alternative: /bin/ed
+Priority: -100
+Slaves:
+ editor.1.gz /usr/share/man/man1/ed.1.gz
+
+Alternative: /usr/bin/vim.gtk
+Priority: 50
+Slaves:
+ editor.1.gz /usr/share/man/man1/vim.1.gz
+ editor.ru.1.gz /usr/share/man/ru/man1/vim.1.gz
+ editor.pl.ISO8859-2.1.gz /usr/share/man/pl.ISO8859-2/man1/vim.1.gz
+ editor.it.ISO8859-1.1.gz /usr/share/man/it.ISO8859-1/man1/vim.1.gz
+ editor.pl.UTF-8.1.gz /usr/share/man/pl.UTF-8/man1/vim.1.gz
+ editor.it.1.gz /usr/share/man/it/man1/vim.1.gz
+ editor.fr.UTF-8.1.gz /usr/share/man/fr.UTF-8/man1/vim.1.gz
+ editor.fr.1.gz /usr/share/man/fr/man1/vim.1.gz
+ editor.it.UTF-8.1.gz /usr/share/man/it.UTF-8/man1/vim.1.gz
+ editor.pl.1.gz /usr/share/man/pl/man1/vim.1.gz
+ editor.fr.ISO8859-1.1.gz /usr/share/man/fr.ISO8859-1/man1/vim.1.gz
+.fi
+.
.SH DIAGNOSTICS
.B update\-alternatives
chatters incessantly about its activities on its standard output channel.
diff --git a/scripts/update-alternatives.pl b/scripts/update-alternatives.pl
index 8b9b193..2ff1069 100755
--- a/scripts/update-alternatives.pl
+++ b/scripts/update-alternatives.pl
@@ -17,7 +17,7 @@ $admindir = $admindir . '/alternatives';
my $verbosemode = 0;
-my $action = ''; # Action to perform (display / install / remove / display / auto / config)
+my $action = ''; # Action to perform (display / query / install / remove / auto / config)
my $mode = 'auto'; # Update mode for alternative (manual / auto)
my $state; # State of alternative:
# expected: alternative with highest priority is the active alternative
@@ -78,6 +78,7 @@ Commands:
--remove-all <name> remove <name> group from the alternatives system.
--auto <name> switch the master link <name> to automatic mode.
--display <name> display information about the <name> group.
+ --query <name> machine parseable version of --display <name>.
--list <name> display all targets of the <name> group.
--config <name> show alternatives for the <name> group and ask the
user to select which one to use.
@@ -212,6 +213,38 @@ sub display_link_group
}
}
+sub query_link_group
+{
+ pr(sprintf("Link: %s", $name));
+ pr(sprintf("Status: %s", $mode));
+ if ($best ne '') {
+ pr(sprintf("Best: %s", $best));
+ }
+ $linkname = readlink("$altdir/$name");
+
+ if (defined($linkname)) {
+ pr(sprintf("Value: %s", $linkname));
+ } elsif ($! == ENOENT) {
+ pr("Value: none");
+ } else {
+ pr("Value: error");
+ pr(sprintf("Error: %s", $!));
+ }
+
+ for (my $i = 0; $i <= $#versions; $i++) {
+ pr("");
+ pr(sprintf("Alternative: %s", $versions[$i]));
+ pr(sprintf("Priority: %s", $priorities[$i]));
+ next unless ($#slavenames >= 0);
+ pr("Slaves:");
+ for (my $j = 0; $j <= $#slavenames; $j++) {
+ my $tspath = $slavepath{$i, $j};
+ next unless length($tspath);
+ pr(sprintf(" %s %s", $slavenames[$j], $tspath));
+ }
+ }
+}
+
sub list_link_group
{
for (my $i = 0; $i <= $#versions; $i++) {
@@ -314,7 +347,7 @@ while (@ARGV) {
@ARGV >= 2 || &badusage(sprintf(_g("--%s needs <name> <path>"), $1));
($name,$apath,@ARGV) = @ARGV;
$action = $1;
- } elsif (m/^--(display|auto|config|list|remove-all)$/) {
+ } elsif (m/^--(display|query|auto|config|list|remove-all)$/) {
check_many_actions();
@ARGV || &badusage(sprintf(_g("--%s needs <name>"), $1));
$action = $1;
@@ -345,7 +378,7 @@ defined($alink) && $aslavelinkcount{$alink} &&
badusage(sprintf(_g("link %s is both primary and slave"), $alink));
$action ||
- badusage(_g("need --display, --config, --set, --install, --remove, --all, --remove-all or --auto"));
+ badusage(_g("need --display, --query, --config, --set, --install, --remove, --all, --remove-all or --auto"));
$action eq 'install' || !%aslavelink ||
badusage(_g("--slave only allowed with --install"));
@@ -371,6 +404,12 @@ if ($action eq 'display') {
exit 0;
}
+if ($action eq 'query') {
+ find_best_version();
+ query_link_group();
+ exit 0;
+}
+
if ($action eq 'list') {
list_link_group();
exit 0;
--
1.5.5.1.295.gbeb1
Reply to: