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

enter/10releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2
Last change on this file since 2c7fba6 was 2c7fba6, checked in by Ken Moore <ken@…>, 16 months ago

Merge branch 'master' of github.com:pcbsd/pcbsd

  • Property mode set to 100755
File size: 14.8 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  #Check for the PCDM_SESSION environment variable first
314  if [ "${PCDM_SESSION}" = "KDE" ]; then DE=kde;
315  elif [ "${PCDM_SESSION}" = "LXDE" ]; then DE=lxde;
316  elif [ "${PCDM_SESSION}" = "MATE" ]; then DE=mate;
317  elif [ "${PCDM_SESSION}" = "GNOME" ]; then DE=gnome;
318  elif [ "${PCDM_SESSION}" = "CINNAMON" ]; then DE=gnome;
319  elif [ "${PCDM_SESSION}" = "XFCE" ]; then DE=xfce;
320  #otherwise scan the running procs for DE-specific indicators
321  elif [ -n "`echo "${UR_PROCS}" | grep startxfce`" ]; then DE=xfce;
322  elif [ -n "`echo "${UR_PROCS}" | grep gnome-session`" ]; then DE=gnome;
323  elif [ -n "`echo "${UR_PROCS}" | grep mate-session`" ]; then DE=mate;
324  elif [ -n "`echo "${UR_PROCS}" | grep lxsession`" ]; then DE=lxde;
325  elif [ -n "`echo "${UR_PROCS}" | grep kuiserver`" ]; then DE=kde;
326  #Otherwise use the classic methods for detection
327  elif [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
328  elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
329  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;
330  elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
331  elif [ x"$DESKTOP_SESSION" == x"LXDE" ]; then DE=lxde;
332  #Check for kdeinit4 last, can cause false positives, but dolphin usually comes with it
333  elif [ -n "`echo "${UR_PROCS}" | grep kdeinit4`" ]; then DE=kde; #(better than nothing)
334  else DE=""
335  fi
336  unset UR_PROCS
337}
338
339#----------------------------------------------------------------------------
340# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
341# It also always returns 1 in KDE 3.4 and earlier
342# Simply return 0 in such case
343
344kfmclient_fix_exit_code()
345{
346    [ x"$KDE_SESSION_VERSION" = x"4" ] && return 0;
347    version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
348    major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
349    minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
350    release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
351    test "$major" -gt 3 && return $1
352    test "$minor" -gt 5 && return $1
353    test "$release" -gt 4 && return $1
354    return 0
355}
356
357# This handles backslashes but not quote marks.
358first_word()
359{
360    read first rest
361    echo "$first"
362}
363
364open_kde()
365{
366    if kioclient -v 2>/dev/null 1>&2; then
367        kioclient exec "$1"
368    else
369        if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
370            kfmclient openURL "$1"
371        else
372            kfmclient exec "$1"
373            kfmclient_fix_exit_code $?
374        fi
375    fi
376
377    if [ $? -eq 0 ]; then
378        exit_success
379    else
380        exit_failure_operation_failed
381    fi
382}
383
384open_gnome()
385{
386    if gvfs-open --help 2>/dev/null 1>&2; then
387        gvfs-open "$1"
388    else
389        gnome-open "$1"
390    fi
391
392    if [ $? -eq 0 ]; then
393        exit_success
394    else
395        exit_failure_operation_failed
396    fi
397}
398
399open_xfce()
400{
401    exo-open "$1"
402
403    if [ $? -eq 0 ]; then
404        exit_success
405    else
406        exit_failure_operation_failed
407    fi
408}
409
410open_mate()
411{
412  gvfs-open "$1"
413
414  if [ $? -eq 0 ]; then
415       exit_success
416  else
417       exit_failure_operation_failed
418  fi
419}
420
421open_generic_xdg_mime()
422{
423    filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
424    default=`xdg-mime query default "$filetype"`
425    if [ -n "$default" ] ; then
426        xdg_user_dir="$XDG_DATA_HOME"
427        [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
428
429        xdg_system_dirs="$XDG_DATA_DIRS"
430        [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
431
432        for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
433            local file="$x/applications/$default"
434            if [ -r "$file" ] ; then
435                command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
436                command_exec=`which $command 2>/dev/null`
437                if [ -x "$command_exec" ] ; then
438                    $command_exec "$1"
439                    if [ $? -eq 0 ]; then
440                        exit_success
441                    fi
442                fi
443            fi
444        done
445    fi
446}
447
448open_generic()
449{
450    # Paths or file:// URLs
451    if (echo "$1" | grep -q '^file://' ||
452        ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then
453
454        local file="$1"
455
456        # Decode URLs
457        if echo "$file" | grep -q '^file:///'; then
458            file=${file#file://}
459            file="$(printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
460        fi
461        check_input_file "$file"
462
463        open_generic_xdg_mime "$file"
464
465        if [ -f /etc/debian_version ] &&
466            which run-mailcap 2>/dev/null 1>&2; then
467            run-mailcap --action=view "$file"
468            if [ $? -eq 0 ]; then
469                exit_success
470            fi
471        fi
472
473        if mimeopen -v 2>/dev/null 1>&2; then
474            mimeopen -L -n "$file"
475            if [ $? -eq 0 ]; then
476                exit_success
477            fi
478        fi
479    fi
480
481    IFS=":"
482    for browser in $BROWSER; do
483        if [ x"$browser" != x"" ]; then
484
485            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
486            if [ $? -ne 0 ]; then
487                browser_with_arg=$browser;
488            fi
489
490            if [ x"$browser_with_arg" = x"$browser" ]; then
491                "$browser" "$1";
492            else eval '$browser_with_arg'$xdg_redirect_output;
493            fi
494
495            if [ $? -eq 0 ]; then
496                exit_success;
497            fi
498        fi
499    done
500
501    exit_failure_operation_impossible "no method available for opening '$1'"
502}
503
504open_lxde()
505{
506    # pcmanfm only knows how to handle file:// urls and filepaths, it seems.
507    if (echo "$1" | grep -q '^file://' ||
508        ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:')
509    then
510        local file="$(echo "$1" | sed 's%^file://%%')"
511
512        # handle relative paths
513        if ! echo "$file" | grep -q '^/'; then
514            file="$(pwd)/$file"
515        fi
516
517        pcmanfm "$file"
518
519    else
520        open_generic "$1"
521    fi
522
523    if [ $? -eq 0 ]; then
524        exit_success
525    else
526        exit_failure_operation_failed
527    fi
528}
529
530[ x"$1" != x"" ] || exit_failure_syntax
531
532url=
533while [ $# -gt 0 ] ; do
534    parm="$1"
535    shift
536
537    case "$parm" in
538      -*)
539        exit_failure_syntax "unexpected option '$parm'"
540        ;;
541
542      *)
543        if [ -n "$url" ] ; then
544            exit_failure_syntax "unexpected argument '$parm'"
545        fi
546        url="$parm"
547        ;;
548    esac
549done
550
551if [ -z "${url}" ] ; then
552    exit_failure_syntax "file or URL argument missing"
553fi
554
555detectDE
556
557if [ x"$DE" = x"" ]; then
558    DE=generic
559fi
560
561# if BROWSER variable is not set, check some well known browsers instead
562if [ x"$BROWSER" = x"" ]; then
563    BROWSER=links2:links:lynx:w3m
564    if [ -n "$DISPLAY" ]; then
565        BROWSER=firefox:mozilla:epiphany:konqueror:chrome:chromium-browser:google-chrome:$BROWSER
566    fi
567fi
568
569case "$DE" in
570    kde)
571    open_kde "$url"
572    ;;
573
574    gnome)
575    open_gnome "$url"
576    ;;
577
578    mate)
579    open_mate "$url"
580    ;;
581
582    xfce)
583    open_xfce "$url"
584    ;;
585
586    lxde)
587    open_lxde "$url"
588    ;;
589
590    generic)
591    open_generic "$url"
592    ;;
593
594    *)
595    exit_failure_operation_impossible "no method available for opening '$url'"
596    ;;
597esac
Note: See TracBrowser for help on using the repository browser.