source: src-sh/pc-extractoverlay/ports-overlay/usr/local/bin/xdg-open @ 192923c

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 192923c was 192923c, checked in by Ken Moore <ken@…>, 5 months ago

Move the new xdg-open to the ports overlay instead of the desktop overlay

  • Property mode set to 100755
File size: 14.1 KB
Line 
1#!/bin/sh
2#---------------------------------------------
3#   xdg-open
4#
5#   Utility script to open a URL in the registered default application.
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#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
13#
14#   LICENSE:
15#
16#   Permission is hereby granted, free of charge, to any person obtaining a
17#   copy of this software and associated documentation files (the "Software"),
18#   to deal in the Software without restriction, including without limitation
19#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
20#   and/or sell copies of the Software, and to permit persons to whom the
21#   Software is furnished to do so, subject to the following conditions:
22#
23#   The above copyright notice and this permission notice shall be included
24#   in all copies or substantial portions of the Software.
25#
26#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30#   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31#   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32#   OTHER DEALINGS IN THE SOFTWARE.
33#
34#---------------------------------------------
35
36# If we are started from a Linux app with LD_PRELOAD set unset that
37# so native apps (like browers) won't fail to start.
38if [ "x$(uname)" = "xLinux" ]; then
39        unset LD_PRELOAD
40fi
41
42manualpage()
43{
44cat << _MANUALPAGE
45Name
46
47xdg-open - opens a file or URL in the user's preferred application
48
49Synopsis
50
51xdg-open { file | URL }
52
53xdg-open { --help | --manual | --version }
54
55Description
56
57xdg-open opens a file or URL in the user's preferred application. If a URL is
58provided the URL will be opened in the user's preferred web browser. If a file
59is provided the file will be opened in the preferred application for files of
60that type. xdg-open supports file, ftp, http and https URLs.
61
62xdg-open is for use inside a desktop session only. It is not recommended to use
63xdg-open as root.
64
65Options
66
67--help
68    Show command synopsis.
69--manual
70    Show this manualpage.
71--version
72    Show the xdg-utils version information.
73
74Exit Codes
75
76An exit code of 0 indicates success while a non-zero exit code indicates
77failure. The following failure codes can be returned:
78
791
80    Error in command line syntax.
812
82    One of the files passed on the command line did not exist.
833
84    A required tool could not be found.
854
86    The action failed.
87
88Examples
89
90xdg-open 'http://www.freedesktop.org/'
91
92Opens the Freedesktop.org website in the user's default browser
93
94xdg-open /tmp/foobar.png
95
96Opens the PNG image file /tmp/foobar.png in the user's default image viewing
97application.
98
99_MANUALPAGE
100}
101
102usage()
103{
104cat << _USAGE
105xdg-open - opens a file or URL in the user's preferred application
106
107Synopsis
108
109xdg-open { file | URL }
110
111xdg-open { --help | --manual | --version }
112
113_USAGE
114}
115
116#@xdg-utils-common@
117
118#----------------------------------------------------------------------------
119#   Common utility functions included in all XDG wrapper scripts
120#----------------------------------------------------------------------------
121
122DEBUG()
123{
124  [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
125  [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
126  shift
127  echo "$@" >&2
128}
129
130#-------------------------------------------------------------
131# Exit script on successfully completing the desired operation
132
133exit_success()
134{
135    if [ $# -gt 0 ]; then
136        echo "$@"
137        echo
138    fi
139
140    exit 0
141}
142
143
144#-----------------------------------------
145# Exit script on malformed arguments, not enough arguments
146# or missing required option.
147# prints usage information
148
149exit_failure_syntax()
150{
151    if [ $# -gt 0 ]; then
152        echo "xdg-open: $@" >&2
153        echo "Try 'xdg-open --help' for more information." >&2
154    else
155        usage
156        echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
157    fi
158
159    exit 1
160}
161
162#-------------------------------------------------------------
163# Exit script on missing file specified on command line
164
165exit_failure_file_missing()
166{
167    if [ $# -gt 0 ]; then
168        echo "xdg-open: $@" >&2
169    fi
170
171    exit 2
172}
173
174#-------------------------------------------------------------
175# Exit script on failure to locate necessary tool applications
176
177exit_failure_operation_impossible()
178{
179    if [ $# -gt 0 ]; then
180        echo "xdg-open: $@" >&2
181    fi
182
183    exit 3
184}
185
186#-------------------------------------------------------------
187# Exit script on failure returned by a tool application
188
189exit_failure_operation_failed()
190{
191    if [ $# -gt 0 ]; then
192        echo "xdg-open: $@" >&2
193    fi
194
195    exit 4
196}
197
198#------------------------------------------------------------
199# Exit script on insufficient permission to read a specified file
200
201exit_failure_file_permission_read()
202{
203    if [ $# -gt 0 ]; then
204        echo "xdg-open: $@" >&2
205    fi
206
207    exit 5
208}
209
210#------------------------------------------------------------
211# Exit script on insufficient permission to write a specified file
212
213exit_failure_file_permission_write()
214{
215    if [ $# -gt 0 ]; then
216        echo "xdg-open: $@" >&2
217    fi
218
219    exit 6
220}
221
222check_input_file()
223{
224    if [ ! -e "$1" ]; then
225        exit_failure_file_missing "file '$1' does not exist"
226    fi
227    if [ ! -r "$1" ]; then
228        exit_failure_file_permission_read "no permission to read file '$1'"
229    fi
230}
231
232check_vendor_prefix()
233{
234    file_label="$2"
235    [ -n "$file_label" ] || file_label="filename"
236    file=`basename "$1"`
237    case "$file" in
238       [a-zA-Z]*-*)
239         return
240         ;;
241    esac
242
243    echo "xdg-open: $file_label '$file' does not have a proper vendor prefix" >&2
244    echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
245    echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
246    echo "Use --novendor to override or 'xdg-open --manual' for additional info." >&2
247    exit 1
248}
249
250check_output_file()
251{
252    # if the file exists, check if it is writeable
253    # if it does not exists, check if we are allowed to write on the directory
254    if [ -e "$1" ]; then
255        if [ ! -w "$1" ]; then
256            exit_failure_file_permission_write "no permission to write to file '$1'"
257        fi
258    else
259        DIR=`dirname "$1"`
260        if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
261            exit_failure_file_permission_write "no permission to create file '$1'"
262        fi
263    fi
264}
265
266#----------------------------------------
267# Checks for shared commands, e.g. --help
268
269check_common_commands()
270{
271    while [ $# -gt 0 ] ; do
272        parm="$1"
273        shift
274
275        case "$parm" in
276            --help)
277            usage
278            echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
279            exit_success
280            ;;
281
282            --manual)
283            manualpage
284            exit_success
285            ;;
286
287            --version)
288            echo "xdg-open 1.1.0 rc1"
289            exit_success
290            ;;
291        esac
292    done
293}
294
295check_common_commands "$@"
296
297[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
298if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
299    # Be silent
300    xdg_redirect_output=" > /dev/null 2> /dev/null"
301else
302    # All output to stderr
303    xdg_redirect_output=" >&2"
304fi
305
306#--------------------------------------
307# Checks for known desktop environments
308# set variable DE to the desktop environments name, lowercase
309
310detectDE()
311{
312  UR_PROCS=`ps -ax -o comm` #Get the current running processes
313  #Now scan the running procs for DE-specific indicators
314  if [ -n "`echo "${UR_PROCS}" | grep startxfce`" ]; then DE=xfce;
315  elif [ -n "`echo "${UR_PROCS}" | grep gnome-session`" ]; then DE=gnome;
316  elif [ -n "`echo "${UR_PROCS}" | grep lxsession`" ]; then DE=lxde;
317  elif [ -n "`echo "${UR_PROCS}" | grep kuiserver`" ]; then DE=kde;
318  #Otherwise use the classic methods for detection
319  elif [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
320  elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
321  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;
322  elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
323  elif [ x"$DESKTOP_SESSION" == x"LXDE" ]; then DE=lxde;
324  #Check for kdeinit4 last, can cause false positives, but dolphin usually comes with it
325  elif [ -n "`echo "${UR_PROCS}" | grep kdeinit4`" ]; then DE=kde; #(better than nothing)
326  else DE=""
327  fi
328  unset UR_PROCS
329}
330
331#----------------------------------------------------------------------------
332# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
333# It also always returns 1 in KDE 3.4 and earlier
334# Simply return 0 in such case
335
336kfmclient_fix_exit_code()
337{
338    [ x"$KDE_SESSION_VERSION" = x"4" ] && return 0;
339    version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
340    major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
341    minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
342    release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
343    test "$major" -gt 3 && return $1
344    test "$minor" -gt 5 && return $1
345    test "$release" -gt 4 && return $1
346    return 0
347}
348
349# This handles backslashes but not quote marks.
350first_word()
351{
352    read first rest
353    echo "$first"
354}
355
356open_kde()
357{
358    if kde-open -v 2>/dev/null 1>&2; then
359        kde-open "$1"
360    else
361        if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
362            kfmclient openURL "$1"
363        else
364            kfmclient exec "$1"
365            kfmclient_fix_exit_code $?
366        fi
367    fi
368
369    if [ $? -eq 0 ]; then
370        exit_success
371    else
372        exit_failure_operation_failed
373    fi
374}
375
376open_gnome()
377{
378    if gvfs-open --help 2>/dev/null 1>&2; then
379        gvfs-open "$1"
380    else
381        gnome-open "$1"
382    fi
383
384    if [ $? -eq 0 ]; then
385        exit_success
386    else
387        exit_failure_operation_failed
388    fi
389}
390
391open_xfce()
392{
393    exo-open "$1"
394
395    if [ $? -eq 0 ]; then
396        exit_success
397    else
398        exit_failure_operation_failed
399    fi
400}
401
402open_generic_xdg_mime()
403{
404    filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
405    default=`xdg-mime query default "$filetype"`
406    if [ -n "$default" ] ; then
407        xdg_user_dir="$XDG_DATA_HOME"
408        [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
409
410        xdg_system_dirs="$XDG_DATA_DIRS"
411        [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
412
413        for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
414            local file="$x/applications/$default"
415            if [ -r "$file" ] ; then
416                command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
417                command_exec=`which $command 2>/dev/null`
418                if [ -x "$command_exec" ] ; then
419                    $command_exec "$1"
420                    if [ $? -eq 0 ]; then
421                        exit_success
422                    fi
423                fi
424            fi
425        done
426    fi
427}
428
429open_generic()
430{
431    # Paths or file:// URLs
432    if (echo "$1" | grep -q '^file://' ||
433        ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then
434
435        local file="$1"
436
437        # Decode URLs
438        if echo "$file" | grep -q '^file:///'; then
439            file=${file#file://}
440            file="$(printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
441        fi
442        check_input_file "$file"
443
444        open_generic_xdg_mime "$file"
445
446        if [ -f /etc/debian_version ] &&
447            which run-mailcap 2>/dev/null 1>&2; then
448            run-mailcap --action=view "$file"
449            if [ $? -eq 0 ]; then
450                exit_success
451            fi
452        fi
453
454        if mimeopen -v 2>/dev/null 1>&2; then
455            mimeopen -L -n "$file"
456            if [ $? -eq 0 ]; then
457                exit_success
458            fi
459        fi
460    fi
461
462    IFS=":"
463    for browser in $BROWSER; do
464        if [ x"$browser" != x"" ]; then
465
466            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
467            if [ $? -ne 0 ]; then
468                browser_with_arg=$browser;
469            fi
470
471            if [ x"$browser_with_arg" = x"$browser" ]; then
472                "$browser" "$1";
473            else eval '$browser_with_arg'$xdg_redirect_output;
474            fi
475
476            if [ $? -eq 0 ]; then
477                exit_success;
478            fi
479        fi
480    done
481
482    exit_failure_operation_impossible "no method available for opening '$1'"
483}
484
485open_lxde()
486{
487    # pcmanfm only knows how to handle file:// urls and filepaths, it seems.
488    if (echo "$1" | grep -q '^file://' ||
489        ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:')
490    then
491        local file="$(echo "$1" | sed 's%^file://%%')"
492
493        # handle relative paths
494        if ! echo "$file" | grep -q '^/'; then
495            file="$(pwd)/$file"
496        fi
497
498        pcmanfm "$file"
499
500    else
501        open_generic "$1"
502    fi
503
504    if [ $? -eq 0 ]; then
505        exit_success
506    else
507        exit_failure_operation_failed
508    fi
509}
510
511[ x"$1" != x"" ] || exit_failure_syntax
512
513url=
514while [ $# -gt 0 ] ; do
515    parm="$1"
516    shift
517
518    case "$parm" in
519      -*)
520        exit_failure_syntax "unexpected option '$parm'"
521        ;;
522
523      *)
524        if [ -n "$url" ] ; then
525            exit_failure_syntax "unexpected argument '$parm'"
526        fi
527        url="$parm"
528        ;;
529    esac
530done
531
532if [ -z "${url}" ] ; then
533    exit_failure_syntax "file or URL argument missing"
534fi
535
536detectDE
537
538if [ x"$DE" = x"" ]; then
539    DE=generic
540fi
541
542# if BROWSER variable is not set, check some well known browsers instead
543if [ x"$BROWSER" = x"" ]; then
544    BROWSER=links2:links:lynx:w3m
545    if [ -n "$DISPLAY" ]; then
546        BROWSER=firefox:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
547    fi
548fi
549
550case "$DE" in
551    kde)
552    open_kde "$url"
553    ;;
554
555    gnome)
556    open_gnome "$url"
557    ;;
558
559    xfce)
560    open_xfce "$url"
561    ;;
562
563    lxde)
564    open_lxde "$url"
565    ;;
566
567    generic)
568    open_generic "$url"
569    ;;
570
571    *)
572    exit_failure_operation_impossible "no method available for opening '$url'"
573    ;;
574esac
Note: See TracBrowser for help on using the repository browser.