Procházet zdrojové kódy

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.
Pierre Habouzit před 18 roky
rodič
revize
5b54cfc995
6 změnil soubory, kde provedl 129 přidání a 5 odebrání
  1. 7 0
      ChangeLog
  2. 1 0
      THANKS
  3. 0 2
      TODO
  4. 3 0
      debian/changelog
  5. 76 0
      man/update-alternatives.8
  6. 42 3
      scripts/update-alternatives.pl

+ 7 - 0
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>
 2008-05-05  Raphael Hertzog  <hertzog@debian.org>
 
 
 	* scripts/dpkg-gensymbols.pl: Do not accept empty parameters for
 	* scripts/dpkg-gensymbols.pl: Do not accept empty parameters for

+ 1 - 0
THANKS

@@ -132,6 +132,7 @@ Peter Mann <Peter.Mann@tuke.sk>
 Peter van Dijk <peter@dataloss.nl>
 Peter van Dijk <peter@dataloss.nl>
 Petr Cech <cech@atrey.karlin.mff.cuni.cz>
 Petr Cech <cech@atrey.karlin.mff.cuni.cz>
 Philippe Batailler <pbatailler@teaser.fr>
 Philippe Batailler <pbatailler@teaser.fr>
+Pierre Habouzit <madcoder@debian.org>
 Piotr Engelking <inkerman42@gmail.com>
 Piotr Engelking <inkerman42@gmail.com>
 Piotr Roszatycki <dexter@fnet.pl>
 Piotr Roszatycki <dexter@fnet.pl>
 Rakesh 'arky' Ambati <rakesh_ambati@yahoo.com>
 Rakesh 'arky' Ambati <rakesh_ambati@yahoo.com>

+ 0 - 2
TODO

@@ -106,8 +106,6 @@ lenny
 
 
  * Make dpkg-deb --build check for duped conffiles. (#131633)
  * Make dpkg-deb --build check for duped conffiles. (#131633)
 
 
- * Make update-alternatives --display support --quiet. (#336091)
-
  * Propagate --admindir to programs run from maintainer scritpts. (#97076)
  * Propagate --admindir to programs run from maintainer scritpts. (#97076)
 
 
  * Fix quotes? <http://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html>
  * Fix quotes? <http://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html>

+ 3 - 0
debian/changelog

@@ -10,6 +10,9 @@ dpkg (1.15.0) UNRELEASED; urgency=low
   * dpkg-gensymbols now refuses empty values for the the -v -P and -e
   * dpkg-gensymbols now refuses empty values for the the -v -P and -e
     parameters.
     parameters.
 
 
+  [ Pierre Habouzit ]
+  * Add a --query option to update-alternatives. Closes: #336091, #441904
+
   [ Updated dpkg translations ]
   [ Updated dpkg translations ]
   * Portuguese (Miguel Figueiredo).
   * Portuguese (Miguel Figueiredo).
 
 

+ 76 - 0
man/update-alternatives.8

@@ -283,6 +283,12 @@ what other alternatives are available
 (and their corresponding slave alternatives),
 (and their corresponding slave alternatives),
 and the highest priority alternative currently installed.
 and the highest priority alternative currently installed.
 .TP
 .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
 \fB\-\-list\fR \fIlink\fR
 Display all targets of the link group.
 Display all targets of the link group.
 .TP
 .TP
@@ -339,6 +345,76 @@ The requested action was successfully performed.
 Problems were encountered whilst parsing the command line
 Problems were encountered whilst parsing the command line
 or performing the action.
 or performing the action.
 .
 .
+.SH "QUERY FORMAT"
+The \fBupdate\-alternatives\fP \fI\-\-query\fP format is using an
+RFC822-like flat format. It's made of \fIn\fP + 1 blocks where \fIn\fP is
+the number of alternatives available in the queried link group. The first
+block contains the following fields:
+.TP
+.BR Link: " <link>"
+The generic name of the alternative.
+.TP
+.BR Status: " <status>"
+The status of the alternative (\fBauto\fR or \fBmanual\fR).
+.TP
+.BR Best: " <best choice>"
+The path of the best alternative for this link group. Not present if
+there is no alternatives available.
+.TP
+.BR Value: " <currently selected alternative> "
+The path of the currently selected alternative. It can also take the magic
+values \fBnone\fR or \fBerror\fR. The former is used if the link doesn't
+exist, and the latter in case of error.
+.TP
+.BR Error: " <error details>"
+When \fBValue\fR is \fBerror\fR, this field is present and contains a human readable
+string explaining the error.
+.
+.TP
+The other blocks describe the available alternatives in the queried link group:
+.TP
+.BR Alternative: " <path of 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
+associated to the master link of the alternative. There is one slave per
+line. Each line contains one space, the generic name of the slave
+alternative, another space, and the path to the slave alternative.
+.
+.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
 .SH DIAGNOSTICS
 .B update\-alternatives
 .B update\-alternatives
 chatters incessantly about its activities on its standard output channel.
 chatters incessantly about its activities on its standard output channel.

+ 42 - 3
scripts/update-alternatives.pl

@@ -17,7 +17,7 @@ $admindir = $admindir . '/alternatives';
 
 
 my $verbosemode = 0;
 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 $mode = 'auto';    # Update mode for alternative (manual / auto)
 my $state;            # State of alternative:
 my $state;            # State of alternative:
                       #   expected: alternative with highest priority is the active 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.
   --remove-all <name>      remove <name> group from the alternatives system.
   --auto <name>            switch the master link <name> to automatic mode.
   --auto <name>            switch the master link <name> to automatic mode.
   --display <name>         display information about the <name> group.
   --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.
   --list <name>            display all targets of the <name> group.
   --config <name>          show alternatives for the <name> group and ask the
   --config <name>          show alternatives for the <name> group and ask the
                            user to select which one to use.
                            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
 sub list_link_group
 {
 {
     for (my $i = 0; $i <= $#versions; $i++) {
     for (my $i = 0; $i <= $#versions; $i++) {
@@ -314,7 +347,7 @@ while (@ARGV) {
         @ARGV >= 2 || &badusage(sprintf(_g("--%s needs <name> <path>"), $1));
         @ARGV >= 2 || &badusage(sprintf(_g("--%s needs <name> <path>"), $1));
         ($name,$apath,@ARGV) = @ARGV;
         ($name,$apath,@ARGV) = @ARGV;
 	$action = $1;
 	$action = $1;
-    } elsif (m/^--(display|auto|config|list|remove-all)$/) {
+    } elsif (m/^--(display|query|auto|config|list|remove-all)$/) {
 	check_many_actions();
 	check_many_actions();
         @ARGV || &badusage(sprintf(_g("--%s needs <name>"), $1));
         @ARGV || &badusage(sprintf(_g("--%s needs <name>"), $1));
 	$action = $1;
 	$action = $1;
@@ -345,7 +378,7 @@ defined($alink) && $aslavelinkcount{$alink} &&
   badusage(sprintf(_g("link %s is both primary and slave"), $alink));
   badusage(sprintf(_g("link %s is both primary and slave"), $alink));
 
 
 $action ||
 $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 ||
 $action eq 'install' || !%aslavelink ||
   badusage(_g("--slave only allowed with --install"));
   badusage(_g("--slave only allowed with --install"));
 
 
@@ -371,6 +404,12 @@ if ($action eq 'display') {
     exit 0;
     exit 0;
 }
 }
 
 
+if ($action eq 'query') {
+    find_best_version();
+    query_link_group();
+    exit 0;
+}
+
 if ($action eq 'list') {
 if ($action eq 'list') {
     list_link_group();
     list_link_group();
     exit 0;
     exit 0;