aboutsummaryrefslogtreecommitdiffstats
path: root/sbin/chksession
blob: 8afb6f95c6f5ac5b8f664b26f15f2a0506c6096a (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/perl
# (c) Mandriva, Chmouel Boudjnah <chmouel@mandriva.com>
# Copyright under GPL blah blah blah.
## For info, see "chksession --help" or  "man chksession"

# Modified by Bernard Lang on August 21, 2003.
# Modified by ns80 on February 12, 2016.


my (@lf, $dir, $first, $list, $list_order, %order, $test);

sub usage {
  my $e = shift @_;
  $0 =~ s|.*/||;
  print {  $e ? STDERR : STDOUT } << "EOF";
Usage: $0 [OPTION]...

	      -F --first:   Print only first available entry.
	      -t, --test:   Go in test mode.
	      -l, --list:   List window-managers.
	      -L:           List window-managers including the order number

	      -d=DIR, --dir=DIR:   Specifies a directory of w-m configuration files.
			 Default is /etc/X11/wmsession.d/

	      -h, --help:   Produce this help.

EOF
  exit($e);
}

sub cat {   # returns content of argument file as a single string
  my ($f) = @_;
  open my $F, $f or die "Can't open $f\n";
  local $/ = "";
  <$F>;
  }
  
sub parse_file {         # parse a session descriptor file
    my ($fn) = @_;
     my $n;
     local $_ = cat($fn);
     ($n = $1) =~ s| ||g if /^NAME=(.*)/m;
     $e = $1 if /^EXEC=(.*)/m;
     if (-x $e) { push @lf, $n; ($order{$n}) = $fn =~ m/(^[0-9][0-9])/; }
}

usage(1)
  if @ARGV < 1;

while ($ARGV[0] =~ /^--/ || $ARGV[0] =~ /^-/) {
  $_ = shift;
  if (/^--first/ || /^-F/) {
    $first++;
  } elsif (/^--list/ || /^-l/) {
    $list++;
  } elsif (/^-L/) {
    $list_order++;
  } elsif (/^--test/ || /^-t/) {
    $test++;
  } elsif (/^--dir=([^ ]+)/ || /^-d=([^ ]+)/) {
    $dir = $1;
  } elsif (/^--help/ || /^-h/ || /^-\?/) {
    usage(0);
  } else {
    print STDERR "Unrecognized switch: $_\n";
    usage(1);
  }
}
  
# Parse all relevant files in session directory $dir
$dir = $test ? './wmsession.d/' : '/etc/X11/wmsession.d/' unless $dir;
chdir $dir; 
for (<*>) {
    next if /.*~/;
    next if /.*\.rpm(save|old)/;
    parse_file($_);
}

my ($e) = eval { cat("/etc/sysconfig/desktop") } =~ /DESKTOP=(\S+)/;
# The first string (without spaces) in the file is copied to $e.

# If $e is one of the names in @lf, then it is placed first (leftmost).
# Order of names in @lf is otherwise unchanged.
@lf = sort { $b =~ /^$e$/i <=> $a =~ /^$e$/i } @lf;

@lf ? print shift @lf, "\n" : print "default\n"
  if $first;

if ($list) { 
  if (@lf) {
    print join(' ', @lf, 'default', 'failsafe'), "\n";
 } else {
    print "default\n";
  }
} elsif ($list_order) { 
    print join(' ', map { "$_=$order{$_}" } @lf), "\n";
}