#!/bin/sh #--------------------------------------------- # xdg-browser # manualpage() { cat << _MANUALPAGE Name xdg-browser - opens the user's preferred browser application Synopsis xdg-browser [URL] xdg-browser { --help | --manual | --version } Description xdg-browser opens the user's preferred browser application. If a URL is provided the URL will be opened in the user's preferred web browser. xdg-browser is for use inside a desktop session only. It is not recommended to use xdg-browser as root. Options --help Show command synopsis. --manual Show this manualpage. --version Show the xdg-utils version information. Exit Codes An exit code of 0 indicates success while a non-zero exit code indicates failure. The following failure codes can be returned: 1 Error in command line syntax. 3 A required tool could not be found. 4 The action failed. Examples xdg-browser Opens the user's default browser application xdg-browser 'http://www.freedesktop.org/' Opens the Freedesktop.org website in the user's default browser _MANUALPAGE } usage() { cat << _USAGE xdg-browser - opens the user's preferred browser application Synopsis xdg-browser [URL] xdg-browser { --help | --manual | --version } _USAGE } #@xdg-utils-common@ #---------------------------------------------------------------------------- # Common utility functions included in all XDG wrapper scripts #---------------------------------------------------------------------------- DEBUG() { [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0; [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0; shift echo "$@" >&2 } #------------------------------------------------------------- # Exit script on successfully completing the desired operation exit_success() { if [ $# -gt 0 ]; then echo "$@" echo fi exit 0 } #----------------------------------------- # Exit script on malformed arguments, not enough arguments # or missing required option. # prints usage information exit_failure_syntax() { if [ $# -gt 0 ]; then echo "xdg-browser: $@" >&2 echo "Try 'xdg-browser --help' for more information." >&2 else usage echo "Use 'man xdg-browser' or 'xdg-browser --manual' for additional info." fi exit 1 } #------------------------------------------------------------- # Exit script on missing file specified on command line exit_failure_file_missing() { if [ $# -gt 0 ]; then echo "xdg-browser: $@" >&2 fi exit 2 } #------------------------------------------------------------- # Exit script on failure to locate necessary tool applications exit_failure_operation_impossible() { if [ $# -gt 0 ]; then echo "xdg-browser: $@" >&2 fi exit 3 } #------------------------------------------------------------- # Exit script on failure returned by a tool application exit_failure_operation_failed() { if [ $# -gt 0 ]; then echo "xdg-browser: $@" >&2 fi exit 4 } #------------------------------------------------------------ # Exit script on insufficient permission to read a specified file exit_failure_file_permission_read() { if [ $# -gt 0 ]; then echo "xdg-browser: $@" >&2 fi exit 5 } #------------------------------------------------------------ # Exit script on insufficient permission to read a specified file exit_failure_file_permission_write() { if [ $# -gt 0 ]; then echo "xdg-browser: $@" >&2 fi exit 6 } check_input_file() { if [ ! -e "$1" ]; then exit_failure_file_missing "file '$1' does not exist" fi if [ ! -r "$1" ]; then exit_failure_file_permission_read "no permission to read file '$1'" fi } check_vendor_prefix() { file_label="$2" [ -n "$file_label" ] || file_label="filename" file=`basename "$1"` case "$file" in [a-zA-Z]*-*) return ;; esac echo "xdg-browser: $file_label '$file' does not have a proper vendor prefix" >&2 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2 echo "Use --novendor to override or 'xdg-browser --manual' for additional info." >&2 exit 1 } check_output_file() { # if the file exists, check if it is writeable # if it does not exists, check if we are allowed to write on the directory if [ -e "$1" ]; then if [ ! -w "$1" ]; then exit_failure_file_permission_write "no permission to write to file '$1'" fi else DIR=`dirname "$1"` if [ ! -w "$DIR" -o ! -x "$DIR" ]; then exit_failure_file_permission_write "no permission to create file '$1'" fi fi } #---------------------------------------- # Checks for shared commands, e.g. --help check_common_commands() { while [ $# -gt 0 ] ; do parm="$1" shift case "$parm" in --help) usage echo "Use 'man xdg-browser' or 'xdg-browser --manual' for additional info." exit_success ;; --manual) manualpage exit_success ;; --version) echo "xdg-browser 0.0.1" exit_success ;; esac done } check_common_commands "$@" [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL; if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then # Be silent xdg_redirect_output=" > /dev/null 2> /dev/null" else # All output to stderr xdg_redirect_output=" >&2" fi #-------------------------------------- # Checks for known desktop environments # set variable DE to the desktop environments name, lowercase detectDE() { if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; 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; elif xprop -root | grep -qw XFCE_DESKTOP_WINDOW 2>&1; then DE=xfce; elif [ x"$DESKTOP_SESSION" == x"LXDE" ]; then DE=lxde; else DE="" fi echo $DE } ## Utility functions # This handles backslashes but not quote marks. first_word() { read first rest echo "$first" } desktop_file_to_binary() { search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}" desktop="`basename "$1"`" IFS=: for dir in $search; do unset IFS [ "$dir" -a -d "$dir/applications" ] || continue file="$dir/applications/$desktop" [ -r "$file" ] || continue # Remove any arguments (%F, %f, %U, %u, etc.). command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`" command="`which "$command"`" readlink -f "$command" return done } ### Browser functions browser_kde() { browser="`kreadconfig --file kdeglobals --group General --key BrowserApplication`" if [ x"$browser" = x ]; then # kreadconfig in KDE 4 may not notice Key[$*]=... localized settings, so # check by hand if it didn't find anything (oddly kwriteconfig works # fine though). kdeglobals_dir=`kde${KDE_SESSION_VERSION}-config --path config | cut -d ':' -f 1` kdeglobals="$kdeglobals_dir/kdeglobals" [ ! -f "$kdeglobals" ] && return # This will only take the first value if there is more than one. browser="`grep '^BrowserApplication\[$[^]=]*\]=' "$kdeglobals" | head -n 1 | cut -d= -f 2-`" fi # Kde returns in two ways. One with a prefix (i.g !firefox) # The other one with a desktop suffix (i.g chromium-browser.desktop) case "$browser" in !*) browser="${browser#!}" browser_exec=`type -p $browser` ;; *) browser_exec="`desktop_file_to_binary "$browser"`" ;; esac if [ -x "$browser_exec" ]; then if [ x"$1" == x"" ]; then $browser_exec else $browser_exec "$1" fi if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi else exit_failure_operation_impossible "configured browser program '$browser' not found or not executable" fi } browser_gnome() { browser="`gconftool-2 --get /desktop/gnome/applications/browser/exec | first_word`" browser_exec=`type -p $browser` if [ -x "$browser_exec" ]; then if [ x"$1" == x"" ]; then $browser_exec else $browser_exec "$1" fi if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi else exit_failure_operation_impossible "configured browser program '$browser' not found or not executable" fi } browser_xfce() { browser_exec="`exo-open --launch WebBrowser`" if [ -x "$browser_exec" ]; then if [ x"$1" == x"" ]; then $browser_exec else $browser_exec "$1" fi if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi else exit_failure_operation_impossible "configured browser program '$browser' not found or not executable" fi } browser_lxde() { search="${XDG_CONFIG_HOME:-$HOME/.config}" for dir in $search; do file="$dir/libfm/pref-apps.conf" [ -r "$file" ] || continue grep -q "^CustomWebBrowsers=" "$file" || continue desktop="`grep "^CustomWebBrowsers=" "$file" | cut -d= -f 2-`" #Lxde append a ; characters to the lines desktop="${desktop%;}" browser_exec="`desktop_file_to_binary "$desktop"`" done if [ -x "$browser_exec" ]; then if [ x"$1" == x"" ]; then $browser_exec else $browser_exec "$1" fi if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi else exit_failure_operation_impossible "configured browser program '$browser' not found or not executable" fi } browser_generic() { # if browser variable is not set, check some well known browsers instead if [ x"$browser" = x"" ]; then browser=links2:links:lynx:w3m if [ -n "$DISPLAY" ]; then browser=firefox:mozilla:epiphany:konqueror:chromium-browser:google-chrome:opera:midori:$browser fi fi browser_exec=`type -p $browser` if [ -x "$browser_exec" ]; then if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi else exit_failure_operation_impossible "configured browser program '$browser' not found or not executable" fi } #[ x"$1" != x"" ] || exit_failure_syntax command= while [ $# -gt 0 ] ; do parm="$1" shift case "$parm" in -*) exit_failure_syntax "unexpected option '$parm'" ;; *) if [ -n "$command" ] ; then exit_failure_syntax "unexpected argument '$parm'" fi command="$parm" ;; esac done detectDE if [ x"$DE" = x"" ]; then DE=generic fi case "$DE" in kde) browser_kde "$command" ;; gnome) browser_gnome "$command" ;; xfce) browser_xfce "$command" ;; lxde) browser_lxde "$command" ;; generic) browser_generic "$command" ;; *) exit_failure_operation_impossible "no browser application available" ;; esac