Initial commit
[zlobin-misc-tools.git] / fdopenres
blob1283a0b130d509764d2f5aeed9ffb71a2a5b4765
1 #!/bin/sh
2 #---------------------------------------------
3 # fdopenres
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>
19 # LICENSE:
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 #---------------------------------------------
41 manualpage()
43 cat << _MANUALPAGE
44 Name
46 fdopenres - opens a file or URL in the user's preferred application
48 Synopsis
50 fdopenres { file | URL }
52 fdopenres { --help | --manual | --version }
54 Description
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
62 fdopenres as root.
64 Options
66 --help
67 Show command synopsis.
68 --manual
69 Show this manualpage.
70 --version
71 Show the xdg-utils version information.
73 Exit Codes
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.
85 The action failed.
87 Examples
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
96 application.
98 _MANUALPAGE
101 usage()
103 cat << _USAGE
104 fdopenres - opens a file or URL in the user's preferred application
106 Synopsis
108 fdopenres { file | URL }
110 fdopenres { --help | --manual | --version }
112 _USAGE
115 #@xdg-utils-common@
117 #----------------------------------------------------------------------------
118 # Common utility functions included in all XDG wrapper scripts
119 #----------------------------------------------------------------------------
121 DEBUG()
123 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
124 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
125 shift
126 echo "$@" >&2
129 #-------------------------------------------------------------
130 # Exit script on successfully completing the desired operation
132 exit_success()
134 if [ $# -gt 0 ]; then
135 echo "$@"
136 echo
139 exit 0
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
153 else
154 usage
155 echo "Use 'man fdopenres' or 'fdopenres --manual' for additional info."
158 exit 1
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
170 exit 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
182 exit 3
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
194 exit 4
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
206 exit 5
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
218 exit 6
221 check_input_file()
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()
233 file_label="$2"
234 [ -n "$file_label" ] || file_label="filename"
235 file=`basename "$1"`
236 case "$file" in
237 [a-zA-Z]*-*)
238 return
240 esac
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
246 exit 1
249 check_output_file()
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
253 if [ -e "$1" ]; then
254 if [ ! -w "$1" ]; then
255 exit_failure_file_permission_write "no permission to write to file '$1'"
257 else
258 DIR=`dirname "$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
271 parm="$1"
272 shift
274 case "$parm" in
275 --help)
276 usage
277 echo "Use 'man fdopenres' or 'fdopenres --manual' for additional info."
278 exit_success
281 --manual)
282 manualpage
283 exit_success
286 --version)
287 echo "fdopenres 1.0.2"
288 exit_success
290 esac
291 done
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
298 # Be silent
299 xdg_redirect_output=" > /dev/null 2> /dev/null"
300 else
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.
315 first_word()
317 read first rest
318 echo "$first"
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
342 $command_exec "$1"
343 if [ $? -eq 0 ]; then
344 exit_success
349 done
351 echo "canceled open_generic_xdg_mime()"
354 [ x"$1" != x"" ] || exit_failure_syntax
356 url=
357 while [ $# -gt 0 ] ; do
358 parm="$1"
359 shift
361 case "$parm" in
363 exit_failure_syntax "unexpected option '$parm'"
367 if [ -n "$url" ] ; then
368 exit_failure_syntax "unexpected argument '$parm'"
370 url="$parm"
372 esac
373 done
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
387 # Opening URL
389 open_generic()
392 # Paths or file:// URLs
393 if (echo "$url" | grep -q '^file://' ||
394 ! echo "$url" | egrep -q '^[a-zA-Z+\.\-]+:'); then
396 local file="$url"
398 # Decode URLs
399 if echo "$file" | grep -q '^file:///'; then
400 file=${file#file://}
401 local printf=printf
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
417 exit_success
421 echo "trying mimeopen"
422 if mimeopen -v 2>/dev/null 1>&2; then
423 mimeopen -L -n "$file"
424 if [ $? -eq 0 ]; then
425 exit_success
430 OLDIFS="$IFS"
431 IFS=":"
432 for browser in $BROWSER; do
433 IFS="$OLDIFS"
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
442 $browser "$url";
443 else eval '$browser_with_arg'$xdg_redirect_output;
446 if [ $? -eq 0 ]; then
447 exit_success;
450 done
452 exit_failure_operation_impossible "no method available for opening '$url'"
455 open_generic