2 #---------------------------------------------
5 # Utility script to open a URL in the registered default application.
6 # This script is modified fdopenres, the difference is that it waits for
7 # application termination, and thus - to use only own start way without
8 # DE-specific file opening tools like kde-open, gvfs-open or exo-open. It
9 # uses only xdg-mime for files.
11 # Refer to the usage() function below for usage.
13 # Copyright 2013, Zlobin Nikita <nick87720z@gmail.com>
14 # Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
15 # Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
16 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
17 # Copyright 2006, Jeremy White <jwhite@codeweavers.com>
21 # Permission is hereby granted, free of charge, to any person obtaining a
22 # copy of this software and associated documentation files (the "Software"),
23 # to deal in the Software without restriction, including without limitation
24 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
25 # and/or sell copies of the Software, and to permit persons to whom the
26 # Software is furnished to do so, subject to the following conditions:
28 # The above copyright notice and this permission notice shall be included
29 # in all copies or substantial portions of the Software.
31 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
35 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
37 # OTHER DEALINGS IN THE SOFTWARE.
39 #---------------------------------------------
46 fdopenres - opens a file or URL in the user's preferred application
50 fdopenres { file | URL }
52 fdopenres { --help | --manual | --version }
56 fdopenres opens a file or URL in the user's preferred application. If a URL is
57 provided the URL will be opened in the user's preferred web browser. If a file
58 is provided the file will be opened in the preferred application for files of
59 that type. fdopenres supports file, ftp, http and https URLs.
61 fdopenres is for use inside a desktop session only. It is not recommended to use
67 Show command synopsis.
71 Show the xdg-utils version information.
75 An exit code of 0 indicates success while a non-zero exit code indicates
76 failure. The following failure codes can be returned:
79 Error in command line syntax.
81 One of the files passed on the command line did not exist.
83 A required tool could not be found.
89 fdopenres 'http://www.freedesktop.org/'
91 Opens the Freedesktop.org website in the user's default browser
93 fdopenres /tmp/foobar.png
95 Opens the PNG image file /tmp/foobar.png in the user's default image viewing
104 fdopenres - opens a file or URL in the user's preferred application
108 fdopenres { file | URL }
110 fdopenres { --help | --manual | --version }
117 #----------------------------------------------------------------------------
118 # Common utility functions included in all XDG wrapper scripts
119 #----------------------------------------------------------------------------
123 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
124 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
129 #-------------------------------------------------------------
130 # Exit script on successfully completing the desired operation
134 if [ $# -gt 0 ]; then
143 #-----------------------------------------
144 # Exit script on malformed arguments, not enough arguments
145 # or missing required option.
146 # prints usage information
148 exit_failure_syntax
()
150 if [ $# -gt 0 ]; then
151 echo "fdopenres: $@" >&2
152 echo "Try 'fdopenres --help' for more information." >&2
155 echo "Use 'man fdopenres' or 'fdopenres --manual' for additional info."
161 #-------------------------------------------------------------
162 # Exit script on missing file specified on command line
164 exit_failure_file_missing
()
166 if [ $# -gt 0 ]; then
167 echo "fdopenres: $@" >&2
173 #-------------------------------------------------------------
174 # Exit script on failure to locate necessary tool applications
176 exit_failure_operation_impossible
()
178 if [ $# -gt 0 ]; then
179 echo "fdopenres: $@" >&2
185 #-------------------------------------------------------------
186 # Exit script on failure returned by a tool application
188 exit_failure_operation_failed
()
190 if [ $# -gt 0 ]; then
191 echo "fdopenres: $@" >&2
197 #------------------------------------------------------------
198 # Exit script on insufficient permission to read a specified file
200 exit_failure_file_permission_read
()
202 if [ $# -gt 0 ]; then
203 echo "fdopenres: $@" >&2
209 #------------------------------------------------------------
210 # Exit script on insufficient permission to write a specified file
212 exit_failure_file_permission_write
()
214 if [ $# -gt 0 ]; then
215 echo "fdopenres: $@" >&2
223 if [ ! -e "$1" ]; then
224 exit_failure_file_missing
"file '$1' does not exist"
226 if [ ! -r "$1" ]; then
227 exit_failure_file_permission_read
"no permission to read file '$1'"
231 check_vendor_prefix
()
234 [ -n "$file_label" ] || file_label
="filename"
242 echo "fdopenres: $file_label '$file' does not have a proper vendor prefix" >&2
243 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
244 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
245 echo "Use --novendor to override or 'fdopenres --manual' for additional info." >&2
251 # if the file exists, check if it is writeable
252 # if it does not exists, check if we are allowed to write on the directory
254 if [ ! -w "$1" ]; then
255 exit_failure_file_permission_write
"no permission to write to file '$1'"
259 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
260 exit_failure_file_permission_write
"no permission to create file '$1'"
265 #----------------------------------------
266 # Checks for shared commands, e.g. --help
268 check_common_commands
()
270 while [ $# -gt 0 ] ; do
277 echo "Use 'man fdopenres' or 'fdopenres --manual' for additional info."
287 echo "fdopenres 1.0.2"
294 check_common_commands
"$@"
296 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL
;
297 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
299 xdg_redirect_output
=" > /dev/null 2> /dev/null"
301 # All output to stderr
302 xdg_redirect_output
=" >&2"
305 #--------------------------------------
306 # Checks for known desktop environments
307 # set variable DE to the desktop environments name, lowercase
309 #----------------------------------------------------------------------------
310 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
311 # It also always returns 1 in KDE 3.4 and earlier
312 # Simply return 0 in such case
314 # This handles backslashes but not quote marks.
321 open_generic_xdg_mime
()
323 echo "entered to open_generic_xdg_mime()"
324 filetype
=`xdg-mime query filetype "$1" | sed "s/;.*//"`
325 default
=`xdg-mime query default "$filetype"`
326 if [ -n "$default" ] ; then
327 xdg_user_dir
="$XDG_DATA_HOME"
328 [ -n "$xdg_user_dir" ] || xdg_user_dir
="$HOME/.local/share"
330 xdg_system_dirs
="$XDG_DATA_DIRS"
331 [ -n "$xdg_system_dirs" ] || xdg_system_dirs
=/usr
/local
/share
/:/usr
/share
/
333 for x
in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
334 local dir
="$x/applications"
335 if [ -r "$dir" ] ; then
336 local file="`find \"$dir\" -name \"$default\" | head -n 1`"
338 if [ -r "$file" ] ; then
339 command="`grep -E "^Exec
(\
[[^
]=]*])?
=" "$file" | cut -d= -f 2- | first_word`"
340 command_exec
=`which $command 2>/dev/null`
341 if [ -x "$command_exec" ] ; then
343 if [ $?
-eq 0 ]; then
351 echo "canceled open_generic_xdg_mime()"
354 [ x
"$1" != x
"" ] || exit_failure_syntax
357 while [ $# -gt 0 ] ; do
363 exit_failure_syntax
"unexpected option '$parm'"
367 if [ -n "$url" ] ; then
368 exit_failure_syntax
"unexpected argument '$parm'"
375 if [ -z "${url}" ] ; then
376 exit_failure_syntax
"file or URL argument missing"
379 # if BROWSER variable is not set, check some well known browsers instead
380 if [ x
"$BROWSER" = x
"" ]; then
381 BROWSER
=links2
:links
:lynx
:w3m
382 if [ -n "$DISPLAY" ]; then
383 BROWSER
=x-www-browser
:firefox
:mozilla
:epiphany
:konqueror
:chromium-browser
:google-chrome
:$BROWSER
392 # Paths or file:// URLs
393 if (echo "$url" |
grep -q '^file://' ||
394 ! echo "$url" |
egrep -q '^[a-zA-Z+\.\-]+:'); then
399 if echo "$file" |
grep -q '^file:///'; then
402 if [ -x /usr
/bin
/printf ]; then
403 printf=/usr
/bin
/printf
405 file="$($printf "$
(echo "$file" |
sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
407 check_input_file
"$file"
409 echo "trying xdg-mime"
410 open_generic_xdg_mime
"$file"
412 echo "trying run-mailcap"
413 if [ -f /etc
/debian_version
] &&
414 which run-mailcap
2>/dev
/null
1>&2; then
415 run-mailcap
--action=view
"$file"
416 if [ $?
-eq 0 ]; then
421 echo "trying mimeopen"
422 if mimeopen
-v 2>/dev
/null
1>&2; then
423 mimeopen
-L -n "$file"
424 if [ $?
-eq 0 ]; then
432 for browser
in $BROWSER; do
434 if [ x
"$browser" != x
"" ]; then
436 browser_with_arg
=`printf "$browser" "$url" 2>/dev/null`
437 if [ $?
-ne 0 ]; then
438 browser_with_arg
=$browser;
441 if [ x
"$browser_with_arg" = x
"$browser" ]; then
443 else eval '$browser_with_arg'$xdg_redirect_output;
446 if [ $?
-eq 0 ]; then
452 exit_failure_operation_impossible
"no method available for opening '$url'"