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