File: ALTERNATIVES

package info (click to toggle)
cruft 0.9.16
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,308 kB
  • sloc: ansic: 803; perl: 315; sh: 178; makefile: 61
file content (64 lines) | stat: -rwxr-xr-x 2,155 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl

open( NEED, ">&=3" ) || die( "Couldn't open output for needed files!?!" );

while( defined($alternative_file = glob("/var/lib/dpkg/alternatives/*")) )
{
    $name = "$1" if $alternative_file =~ m{^.*/([^/]*)$};

    open( FILE, $alternative_file ) || die( "Couldn't open $alternative_file\n" );
    
    my $mode = <FILE>;
    my $winner_name = readlink '/etc/alternatives/'.$name or die "Unable to read symlink $name";
    
    # 
    # parse the first stanza: the names of the symlinks
    #
    # @link holds pairs: name of the file in alternatives dir; path of the alternative
    # e.g.: editor, /usr/bin/editor
    #       editor.1.gz, /usr/share/man/man1/editor.1.gz
    #       ...
    # entry number 0 is the master, the rest are slaves
    my @link;
    $link[0][0] = $name;
    $_ = <FILE>; chomp($_);
    $link[0][1] = $_;

    my $num_files = 1;
    while (1) {
	$_ = <FILE>; chomp($_);
	last unless $_;
	$link[$num_files][0] = $_;

	$_ = <FILE>; chomp($_);
	$link[$num_files][1] = $_;

	$num_files++;
    }
    
    # 
    # parse the stanzas with files provided by packages
    #
    my %alternative;
    my $winner = undef;
    ALT: while( $master = <FILE> ) {
	chomp($master);
	last ALT if $master eq "";
	die "internal error: duplicate master [$master] in alternatives file $name" if exists $alternative{$master};
	my $pri = <FILE>; chomp($pri);
	my $this = $alternative{$master} = { prio => $pri, files => [ $master ] };
	$winner = $this if $master eq $winner_name;
	
	SLAVE: for ( $i = 1; $i < $num_files; $i++ ) {
		$slave = <FILE>; chomp($slave);
		$alternative{$master}->{files}->[$i] = $slave;
	}
    }
    warn("Master link \"/etc/alternatives/$name\" points to alternative \"${winner_name}\", but no such alternative is registered, according to \"/var/lib/dpkg/alternatives/$name\". See \"update-alternatives --display $name\"\n"), next unless defined $winner;

    # list the symlinks for files the winning alternative provides
    for ($i = 0; $i < $num_files; $i++) {
    	print '/etc/alternatives/', $link[$i][0], "\n", $link[$i][1], "\n" or die $! if $winner->{files}->[$i];
    }
}
close STDOUT or die $!;