summaryrefslogtreecommitdiffstats
path: root/debian/gnome-terminal.wrapper
blob: cf64a624c755c08bd0729c35dc3a4d7bf84d8baf (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /usr/bin/perl -w

sub exists_in_path {
    my $command = shift;
    my @path = split ':', $ENV{PATH};

    foreach my $dir (@path) {
	if (-x "$dir/$command") {
	    return 1;
	}
    }

    return 0;
}

# GNOME Terminal now uses a dbus-activated service, so calling it would exit
# immediately, while we're supposed to mimic xterm behavior and so to wait
# until the terminal is explicitly closed, so always use the wait argument
push(@args, '--wait');

while ($opt = shift(@ARGV))
{
    if ($opt eq '-display')
    {
	$ENV{'DISPLAY'} = shift(@ARGV);
    }
    elsif ($opt eq '-name')
    {
	$arg = shift(@ARGV);
	push(@args, "--window-with-profile=$arg");
    }
    elsif ($opt eq '-n')
    {
	# Accept but ignore
	print STDERR "$0: to set an icon, please use -name <profile> and set a profile icon\n"
    }
    elsif ($opt eq '-T' || $opt eq '-title')
    {
	push(@args, '-t', shift(@ARGV));
    }
    elsif ($opt eq '-ls')
    {
	die "$0: Requested to be used as a login shell, but it's not supported\n"
    }
    elsif ($opt eq '+ls')
    {
	print STDERR "$0: gnome-terminal is always a non-login shell\n"
    }
    elsif ($opt eq '-geometry')
    {
	$arg = shift(@ARGV);
	push(@args, "--geometry=$arg");
    }
    elsif ($opt eq '-fn')
    {
	$arg = shift(@ARGV);
	push(@args, "--font=$arg");
    }
    elsif ($opt eq '-fg')
    {
	$arg = shift(@ARGV);
	push(@args, "--foreground=$arg");
    }
    elsif ($opt eq '-bg')
    {
	$arg = shift(@ARGV);
	push(@args, "--background=$arg");
    }
    elsif ($opt eq '-tn')
    {
	$arg = shift(@ARGV);
	push(@args, "--termname=$arg");
    }
    elsif ($opt eq '-e')
    {
	# Debian Policy says we would be free to translate this into:
	# push(@args, '--', @ARGV);
	# but let's be nice to people with local scripts that rely on
	# xterm -e having a special case for a singular argument.
	$arg = shift(@ARGV);
	if (@ARGV)
	{
	    push(@args, '--', $arg, @ARGV);
	    last;
	}
	else
	{
	    # Emulate xterm's special case for a single argument: if it
	    # isn't something we can execute directly, pass it to a shell
	    my $executable;

	    if ($arg =~ m{/})
	    {
		$executable = -x $arg;
	    }
	    else
	    {
		$executable = exists_in_path($arg);
	    }

	    if ($executable)
	    {
		push(@args, '--', $arg);
	    }
	    else
	    {
		# Historically, we used the now-deprecated gnome-terminal -e
		# for this case, which would split the command line up as if
		# by a shell, but didn't support shell constructs like "if"
		# and pipes. But this is easier, and matches what xterm does.
		push(@args, '--', 'sh', '-c', $arg);
	    }
	}
	last;
    }
    elsif ($opt eq '-h' || $opt eq '--help')
    {
	push(@args, '--help');
    }
}

exec('gnome-terminal',@args);