1#!/bin/sh
2#---------------------------------------------
3#   xdg-terminal
4#
5#   Utility script to open the registered terminal emulator
6#
7#   Refer to the usage() function below for usage.
8#
9#   Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
10#   Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
11#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
12#
13#   LICENSE:
14#
15#   Permission is hereby granted, free of charge, to any person obtaining a
16#   copy of this software and associated documentation files (the "Software"),
17#   to deal in the Software without restriction, including without limitation
18#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
19#   and/or sell copies of the Software, and to permit persons to whom the
20#   Software is furnished to do so, subject to the following conditions:
21#
22#   The above copyright notice and this permission notice shall be included
23#   in all copies or substantial portions of the Software.
24#
25#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29#   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30#   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31#   OTHER DEALINGS IN THE SOFTWARE.
32#
33#---------------------------------------------
34
35manualpage()
36{
37cat << _MANUALPAGE
38Name
39
40xdg-terminal - opens the user's preferred terminal emulator application
41
42Synopsis
43
44xdg-terminal [command]
45
46xdg-terminal { --help | --manual | --version }
47
48Description
49
50xdg-terminal opens the user's preferred terminal emulator application. If a
51command is provided the command will be executed by the shell within the newly
52opened terminal window.
53
54xdg-terminal is for use inside a desktop session only. It is not recommended to
55use xdg-terminal as root.
56
57Options
58
59--help
60    Show command synopsis.
61--manual
62    Show this manualpage.
63--version
64    Show the xdg-utils version information.
65
66Exit Codes
67
68An exit code of 0 indicates success while a non-zero exit code indicates
69failure. The following failure codes can be returned:
70
711
72    Error in command line syntax.
733
74    A required tool could not be found.
754
76    The action failed.
77
78Examples
79
80xdg-terminal
81
82Opens the user's default terminal emulator, just starting an interactive shell.
83
84xdg-terminal top
85
86Opens the user's default terminal emulator and lets it run the top executable.
87
88_MANUALPAGE
89}
90
91usage()
92{
93cat << _USAGE
94xdg-terminal - opens the user's preferred terminal emulator application
95
96Synopsis
97
98xdg-terminal [command]
99
100xdg-terminal { --help | --manual | --version }
101
102_USAGE
103}
104
105#@xdg-utils-common@
106
107#----------------------------------------------------------------------------
108#   Common utility functions included in all XDG wrapper scripts
109#----------------------------------------------------------------------------
110
111DEBUG()
112{
113  [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
114  [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
115  shift
116  echo "$@" >&2
117}
118
119#-------------------------------------------------------------
120# Exit script on successfully completing the desired operation
121
122exit_success()
123{
124    if [ $# -gt 0 ]; then
125        echo "$@"
126        echo
127    fi
128
129    exit 0
130}
131
132
133#-----------------------------------------
134# Exit script on malformed arguments, not enough arguments
135# or missing required option.
136# prints usage information
137
138exit_failure_syntax()
139{
140    if [ $# -gt 0 ]; then
141        echo "xdg-terminal: $@" >&2
142        echo "Try 'xdg-terminal --help' for more information." >&2
143    else
144        usage
145        echo "Use 'man xdg-terminal' or 'xdg-terminal --manual' for additional info."
146    fi
147
148    exit 1
149}
150
151#-------------------------------------------------------------
152# Exit script on missing file specified on command line
153
154exit_failure_file_missing()
155{
156    if [ $# -gt 0 ]; then
157        echo "xdg-terminal: $@" >&2
158    fi
159
160    exit 2
161}
162
163#-------------------------------------------------------------
164# Exit script on failure to locate necessary tool applications
165
166exit_failure_operation_impossible()
167{
168    if [ $# -gt 0 ]; then
169        echo "xdg-terminal: $@" >&2
170    fi
171
172    exit 3
173}
174
175#-------------------------------------------------------------
176# Exit script on failure returned by a tool application
177
178exit_failure_operation_failed()
179{
180    if [ $# -gt 0 ]; then
181        echo "xdg-terminal: $@" >&2
182    fi
183
184    exit 4
185}
186
187#------------------------------------------------------------
188# Exit script on insufficient permission to read a specified file
189
190exit_failure_file_permission_read()
191{
192    if [ $# -gt 0 ]; then
193        echo "xdg-terminal: $@" >&2
194    fi
195
196    exit 5
197}
198
199#------------------------------------------------------------
200# Exit script on insufficient permission to write a specified file
201
202exit_failure_file_permission_write()
203{
204    if [ $# -gt 0 ]; then
205        echo "xdg-terminal: $@" >&2
206    fi
207
208    exit 6
209}
210
211check_input_file()
212{
213    if [ ! -e "$1" ]; then
214        exit_failure_file_missing "file '$1' does not exist"
215    fi
216    if [ ! -r "$1" ]; then
217        exit_failure_file_permission_read "no permission to read file '$1'"
218    fi
219}
220
221check_vendor_prefix()
222{
223    file_label="$2"
224    [ -n "$file_label" ] || file_label="filename"
225    file=`basename "$1"`
226    case "$file" in
227       [a-zA-Z]*-*)
228         return
229         ;;
230    esac
231
232    echo "xdg-terminal: $file_label '$file' does not have a proper vendor prefix" >&2
233    echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
234    echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
235    echo "Use --novendor to override or 'xdg-terminal --manual' for additional info." >&2
236    exit 1
237}
238
239check_output_file()
240{
241    # if the file exists, check if it is writeable
242    # if it does not exists, check if we are allowed to write on the directory
243    if [ -e "$1" ]; then
244        if [ ! -w "$1" ]; then
245            exit_failure_file_permission_write "no permission to write to file '$1'"
246        fi
247    else
248        DIR=`dirname "$1"`
249        if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
250            exit_failure_file_permission_write "no permission to create file '$1'"
251        fi
252    fi
253}
254
255#----------------------------------------
256# Checks for shared commands, e.g. --help
257
258check_common_commands()
259{
260    while [ $# -gt 0 ] ; do
261        parm="$1"
262        shift
263
264        case "$parm" in
265            --help)
266            usage
267            echo "Use 'man xdg-terminal' or 'xdg-terminal --manual' for additional info."
268            exit_success
269            ;;
270
271            --manual)
272            manualpage
273            exit_success
274            ;;
275
276            --version)
277            echo "xdg-terminal 1.0.2"
278            exit_success
279            ;;
280        esac
281    done
282}
283
284check_common_commands "$@"
285
286[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
287if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
288    # Be silent
289    xdg_redirect_output=" > /dev/null 2> /dev/null"
290else
291    # All output to stderr
292    xdg_redirect_output=" >&2"
293fi
294
295#--------------------------------------
296# Checks for known desktop environments
297# set variable DE to the desktop environments name, lowercase
298
299detectDE()
300{
301    if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
302    elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
303    elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
304    elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
305    elif [ x"$DESKTOP_SESSION" == x"LXDE" ]; then DE=lxde;
306    else DE=""
307    fi
308}
309
310#----------------------------------------------------------------------------
311# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
312# It also always returns 1 in KDE 3.4 and earlier
313# Simply return 0 in such case
314
315kfmclient_fix_exit_code()
316{
317    version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep '^KDE'`
318    major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
319    minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
320    release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
321    test "$major" -gt 3 && return $1
322    test "$minor" -gt 5 && return $1
323    test "$release" -gt 4 && return $1
324    return 0
325}
326
327terminal_kde()
328{
329    terminal=`kreadconfig --file kdeglobals --group General --key TerminalApplication --default konsole`
330
331    terminal_exec=`which $terminal 2>/dev/null`
332
333    if [ -x "$terminal_exec" ]; then
334        if [ x"$1" == x"" ]; then
335            $terminal_exec
336        else
337            $terminal_exec -e "$1"
338        fi
339
340        if [ $? -eq 0 ]; then
341            exit_success
342        else
343            exit_failure_operation_failed
344        fi
345    else
346        exit_failure_operation_impossible "configured terminal program '$terminal' not found or not executable"
347    fi
348}
349
350terminal_gnome()
351{
352    term_exec_key="/desktop/gnome/applications/terminal/exec"
353    term_exec_arg_key="/desktop/gnome/applications/terminal/exec_arg"
354
355    term_exec=`gconftool-2 --get ${term_exec_key}`
356    term_exec_arg=`gconftool-2 --get ${term_exec_arg_key}`
357
358    terminal_exec=`which $term_exec 2>/dev/null`
359
360    if [ -x "$terminal_exec" ]; then
361        if [ x"$1" == x"" ]; then
362            $terminal_exec
363        else
364            if [ x"$term_exec_arg" == x"" ]; then
365                $terminal_exec "$1"
366            else
367                $terminal_exec "$term_exec_arg" "$1"
368            fi
369        fi
370
371        if [ $? -eq 0 ]; then
372            exit_success
373        else
374            exit_failure_operation_failed
375        fi
376    else
377        exit_failure_operation_impossible "configured terminal program '$term_exec' not found or not executable"
378    fi
379}
380
381terminal_xfce()
382{
383    if [ x"$1" == x"" ]; then
384        exo-open --launch TerminalEmulator
385    else
386        exo-open --launch TerminalEmulator "$1"
387    fi
388
389    if [ $? -eq 0 ]; then
390        exit_success
391    else
392        exit_failure_operation_failed
393    fi
394}
395
396terminal_generic()
397{
398    # if $TERM is not set, try xterm
399    if [ x"$TERM" == x"" ]; then
400        TERM=xterm
401    fi
402
403    terminal_exec=`which $TERM >/dev/null 2>/dev/null`
404
405    if [ -x "$terminal_exec" ]; then
406        if [ $? -eq 0 ]; then
407            exit_success
408        else
409            exit_failure_operation_failed
410        fi
411    else
412        exit_failure_operation_impossible "configured terminal program '$TERM' not found or not executable"
413    fi
414}
415
416terminal_lxde()
417{
418    if which lxterminal &>/dev/null; then
419        if [ x"$1" == x"" ]; then
420            lxterminal
421        else
422            lxterminal -e "$1"
423        fi
424    else
425        terminal_generic "$1"
426    fi
427}
428
429#[ x"$1" != x"" ] || exit_failure_syntax
430
431command=
432while [ $# -gt 0 ] ; do
433    parm="$1"
434    shift
435
436    case "$parm" in
437      -*)
438        exit_failure_syntax "unexpected option '$parm'"
439        ;;
440
441      *)
442        if [ -n "$command" ] ; then
443            exit_failure_syntax "unexpected argument '$parm'"
444        fi
445        command="$parm"
446        ;;
447    esac
448done
449
450detectDE
451
452if [ x"$DE" = x"" ]; then
453    # if TERM variable is not set, try xterm
454    if [ x"$TERM" = x"" ]; then
455        TERM=xterm
456    fi
457    DE=generic
458fi
459
460case "$DE" in
461    kde)
462    terminal_kde "$command"
463    ;;
464
465    gnome)
466    terminal_gnome "$command"
467    ;;
468
469    xfce)
470    terminal_xfce "$command"
471    ;;
472
473    lxde)
474    terminal_lxde "$command"
475    ;;
476
477    generic)
478    terminal_generic "$command"
479    ;;
480
481    *)
482    exit_failure_operation_impossible "no terminal emulator available"
483    ;;
484esac
485