xdg-open 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. manualpage()
  36. {
  37. cat << _MANUALPAGE
  38. Name
  39. xdg-open - opens a file or URL in the user's preferred application
  40. Synopsis
  41. xdg-open { file | URL }
  42. xdg-open { --help | --manual | --version }
  43. Description
  44. xdg-open opens a file or URL in the user's preferred application. If a URL is
  45. provided the URL will be opened in the user's preferred web browser. If a file
  46. is provided the file will be opened in the preferred application for files of
  47. that type. xdg-open supports file, ftp, http and https URLs.
  48. xdg-open is for use inside a desktop session only. It is not recommended to use
  49. xdg-open as root.
  50. Options
  51. --help
  52. Show command synopsis.
  53. --manual
  54. Show this manual page.
  55. --version
  56. Show the xdg-utils version information.
  57. Exit Codes
  58. An exit code of 0 indicates success while a non-zero exit code indicates
  59. failure. The following failure codes can be returned:
  60. 1
  61. Error in command line syntax.
  62. 2
  63. One of the files passed on the command line did not exist.
  64. 3
  65. A required tool could not be found.
  66. 4
  67. The action failed.
  68. Examples
  69. xdg-open 'http://www.freedesktop.org/'
  70. Opens the freedesktop.org website in the user's default browser.
  71. xdg-open /tmp/foobar.png
  72. Opens the PNG image file /tmp/foobar.png in the user's default image viewing
  73. application.
  74. _MANUALPAGE
  75. }
  76. usage()
  77. {
  78. cat << _USAGE
  79. xdg-open - opens a file or URL in the user's preferred application
  80. Synopsis
  81. xdg-open { file | URL }
  82. xdg-open { --help | --manual | --version }
  83. _USAGE
  84. }
  85. #@xdg-utils-common@
  86. #----------------------------------------------------------------------------
  87. # Common utility functions included in all XDG wrapper scripts
  88. #----------------------------------------------------------------------------
  89. DEBUG()
  90. {
  91. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  92. [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  93. shift
  94. echo "$@" >&2
  95. }
  96. # This handles backslashes but not quote marks.
  97. first_word()
  98. {
  99. read first rest
  100. echo "$first"
  101. }
  102. #-------------------------------------------------------------
  103. # map a binary to a .desktop file
  104. binary_to_desktop_file()
  105. {
  106. search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  107. binary="`which "$1"`"
  108. binary="`readlink -f "$binary"`"
  109. base="`basename "$binary"`"
  110. IFS=:
  111. for dir in $search; do
  112. unset IFS
  113. [ "$dir" ] || continue
  114. [ -d "$dir/applications" ] || [ -d "$dir/applnk" ] || continue
  115. for file in "$dir"/applications/*.desktop "$dir"/applications/*/*.desktop "$dir"/applnk/*.desktop "$dir"/applnk/*/*.desktop; do
  116. [ -r "$file" ] || continue
  117. # Check to make sure it's worth the processing.
  118. grep -q "^Exec.*$base" "$file" || continue
  119. # Make sure it's a visible desktop file (e.g. not "preferred-web-browser.desktop").
  120. grep -Eq "^(NoDisplay|Hidden)=true" "$file" && continue
  121. command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  122. command="`which "$command"`"
  123. if [ x"`readlink -f "$command"`" = x"$binary" ]; then
  124. # Fix any double slashes that got added path composition
  125. echo "$file" | sed -e 's,//*,/,g'
  126. return
  127. fi
  128. done
  129. done
  130. }
  131. #-------------------------------------------------------------
  132. # map a .desktop file to a binary
  133. ## FIXME: handle vendor dir case
  134. desktop_file_to_binary()
  135. {
  136. search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  137. desktop="`basename "$1"`"
  138. IFS=:
  139. for dir in $search; do
  140. unset IFS
  141. [ "$dir" ] && [ -d "$dir/applications" ] || continue
  142. file="$dir/applications/$desktop"
  143. [ -r "$file" ] || continue
  144. # Remove any arguments (%F, %f, %U, %u, etc.).
  145. command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  146. command="`which "$command"`"
  147. readlink -f "$command"
  148. return
  149. done
  150. }
  151. #-------------------------------------------------------------
  152. # Exit script on successfully completing the desired operation
  153. exit_success()
  154. {
  155. if [ $# -gt 0 ]; then
  156. echo "$@"
  157. echo
  158. fi
  159. exit 0
  160. }
  161. #-----------------------------------------
  162. # Exit script on malformed arguments, not enough arguments
  163. # or missing required option.
  164. # prints usage information
  165. exit_failure_syntax()
  166. {
  167. if [ $# -gt 0 ]; then
  168. echo "xdg-open: $@" >&2
  169. echo "Try 'xdg-open --help' for more information." >&2
  170. else
  171. usage
  172. echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
  173. fi
  174. exit 1
  175. }
  176. #-------------------------------------------------------------
  177. # Exit script on missing file specified on command line
  178. exit_failure_file_missing()
  179. {
  180. if [ $# -gt 0 ]; then
  181. echo "xdg-open: $@" >&2
  182. fi
  183. exit 2
  184. }
  185. #-------------------------------------------------------------
  186. # Exit script on failure to locate necessary tool applications
  187. exit_failure_operation_impossible()
  188. {
  189. if [ $# -gt 0 ]; then
  190. echo "xdg-open: $@" >&2
  191. fi
  192. exit 3
  193. }
  194. #-------------------------------------------------------------
  195. # Exit script on failure returned by a tool application
  196. exit_failure_operation_failed()
  197. {
  198. if [ $# -gt 0 ]; then
  199. echo "xdg-open: $@" >&2
  200. fi
  201. exit 4
  202. }
  203. #------------------------------------------------------------
  204. # Exit script on insufficient permission to read a specified file
  205. exit_failure_file_permission_read()
  206. {
  207. if [ $# -gt 0 ]; then
  208. echo "xdg-open: $@" >&2
  209. fi
  210. exit 5
  211. }
  212. #------------------------------------------------------------
  213. # Exit script on insufficient permission to write a specified file
  214. exit_failure_file_permission_write()
  215. {
  216. if [ $# -gt 0 ]; then
  217. echo "xdg-open: $@" >&2
  218. fi
  219. exit 6
  220. }
  221. check_input_file()
  222. {
  223. if [ ! -e "$1" ]; then
  224. exit_failure_file_missing "file '$1' does not exist"
  225. fi
  226. if [ ! -r "$1" ]; then
  227. exit_failure_file_permission_read "no permission to read file '$1'"
  228. fi
  229. }
  230. check_vendor_prefix()
  231. {
  232. file_label="$2"
  233. [ -n "$file_label" ] || file_label="filename"
  234. file=`basename "$1"`
  235. case "$file" in
  236. [[:alpha:]]*-*)
  237. return
  238. ;;
  239. esac
  240. echo "xdg-open: $file_label '$file' does not have a proper vendor prefix" >&2
  241. echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
  242. echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
  243. echo "Use --novendor to override or 'xdg-open --manual' for additional info." >&2
  244. exit 1
  245. }
  246. check_output_file()
  247. {
  248. # if the file exists, check if it is writeable
  249. # if it does not exists, check if we are allowed to write on the directory
  250. if [ -e "$1" ]; then
  251. if [ ! -w "$1" ]; then
  252. exit_failure_file_permission_write "no permission to write to file '$1'"
  253. fi
  254. else
  255. DIR=`dirname "$1"`
  256. if [ ! -w "$DIR" ] || [ ! -x "$DIR" ]; then
  257. exit_failure_file_permission_write "no permission to create file '$1'"
  258. fi
  259. fi
  260. }
  261. #----------------------------------------
  262. # Checks for shared commands, e.g. --help
  263. check_common_commands()
  264. {
  265. while [ $# -gt 0 ] ; do
  266. parm="$1"
  267. shift
  268. case "$parm" in
  269. --help)
  270. usage
  271. echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
  272. exit_success
  273. ;;
  274. --manual)
  275. manualpage
  276. exit_success
  277. ;;
  278. --version)
  279. echo "xdg-open 1.1.0 rc1"
  280. exit_success
  281. ;;
  282. esac
  283. done
  284. }
  285. check_common_commands "$@"
  286. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
  287. if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
  288. # Be silent
  289. xdg_redirect_output=" > /dev/null 2> /dev/null"
  290. else
  291. # All output to stderr
  292. xdg_redirect_output=" >&2"
  293. fi
  294. #--------------------------------------
  295. # Checks for known desktop environments
  296. # set variable DE to the desktop environments name, lowercase
  297. detectDE()
  298. {
  299. # see https://bugs.freedesktop.org/show_bug.cgi?id=34164
  300. unset GREP_OPTIONS
  301. if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
  302. case "${XDG_CURRENT_DESKTOP}" in
  303. ENLIGHTENMENT)
  304. DE=enlightenment;
  305. ;;
  306. GNOME)
  307. DE=gnome;
  308. ;;
  309. KDE)
  310. DE=kde;
  311. ;;
  312. LXDE)
  313. DE=lxde;
  314. ;;
  315. MATE)
  316. DE=mate;
  317. ;;
  318. XFCE)
  319. DE=xfce
  320. ;;
  321. esac
  322. fi
  323. if [ x"$DE" = x"" ]; then
  324. # classic fallbacks
  325. if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
  326. elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
  327. elif [ x"$MATE_DESKTOP_SESSION_ID" != x"" ]; then DE=mate;
  328. 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;
  329. elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
  330. elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
  331. elif echo $DESKTOP | grep -q '^Enlightenment'; then DE=enlightenment;
  332. fi
  333. fi
  334. if [ x"$DE" = x"" ]; then
  335. # fallback to checking $DESKTOP_SESSION
  336. case "$DESKTOP_SESSION" in
  337. gnome)
  338. DE=gnome;
  339. ;;
  340. LXDE|Lubuntu)
  341. DE=lxde;
  342. ;;
  343. MATE)
  344. DE=mate;
  345. ;;
  346. xfce|xfce4|'Xfce Session')
  347. DE=xfce;
  348. ;;
  349. esac
  350. fi
  351. if [ x"$DE" = x"" ]; then
  352. # fallback to uname output for other platforms
  353. case "$(uname 2>/dev/null)" in
  354. Darwin)
  355. DE=darwin;
  356. ;;
  357. esac
  358. fi
  359. if [ x"$DE" = x"gnome" ]; then
  360. # gnome-default-applications-properties is only available in GNOME 2.x
  361. # but not in GNOME 3.x
  362. which gnome-default-applications-properties > /dev/null 2>&1 || DE="gnome3"
  363. fi
  364. }
  365. #----------------------------------------------------------------------------
  366. # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
  367. # It also always returns 1 in KDE 3.4 and earlier
  368. # Simply return 0 in such case
  369. kfmclient_fix_exit_code()
  370. {
  371. version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
  372. major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
  373. minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
  374. release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
  375. test "$major" -gt 3 && return $1
  376. test "$minor" -gt 5 && return $1
  377. test "$release" -gt 4 && return $1
  378. return 0
  379. }
  380. # This handles backslashes but not quote marks.
  381. first_word()
  382. {
  383. read first rest
  384. echo "$first"
  385. }
  386. last_word()
  387. {
  388. read first rest
  389. echo "$rest"
  390. }
  391. open_darwin()
  392. {
  393. open "$1"
  394. if [ $? -eq 0 ]; then
  395. exit_success
  396. else
  397. exit_failure_operation_failed
  398. fi
  399. }
  400. open_kde()
  401. {
  402. if kde-open -v 2>/dev/null 1>&2; then
  403. kde-open "$1"
  404. else
  405. if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  406. kfmclient openURL "$1"
  407. else
  408. kfmclient exec "$1"
  409. kfmclient_fix_exit_code $?
  410. fi
  411. fi
  412. if [ $? -eq 0 ]; then
  413. exit_success
  414. else
  415. exit_failure_operation_failed
  416. fi
  417. }
  418. open_gnome()
  419. {
  420. if gvfs-open --help 2>/dev/null 1>&2; then
  421. gvfs-open "$1"
  422. else
  423. gnome-open "$1"
  424. fi
  425. if [ $? -eq 0 ]; then
  426. exit_success
  427. else
  428. exit_failure_operation_failed
  429. fi
  430. }
  431. open_mate()
  432. {
  433. if gvfs-open --help 2>/dev/null 1>&2; then
  434. gvfs-open "$1"
  435. else
  436. mate-open "$1"
  437. fi
  438. if [ $? -eq 0 ]; then
  439. exit_success
  440. else
  441. exit_failure_operation_failed
  442. fi
  443. }
  444. open_xfce()
  445. {
  446. exo-open "$1"
  447. if [ $? -eq 0 ]; then
  448. exit_success
  449. else
  450. exit_failure_operation_failed
  451. fi
  452. }
  453. open_enlightenment()
  454. {
  455. enlightenment_open "$1"
  456. if [ $? -eq 0 ]; then
  457. exit_success
  458. else
  459. exit_failure_operation_failed
  460. fi
  461. }
  462. #-----------------------------------------
  463. # Recursively search .desktop file
  464. search_desktop_file()
  465. {
  466. local default="$1"
  467. local dir="$2"
  468. local arg="$3"
  469. local file=""
  470. # look for both vendor-app.desktop, vendor/app.desktop
  471. if [ -r "$dir/$default" ]; then
  472. file="$dir/$default"
  473. elif [ -r "$dir/`echo $default | sed -e 's|-|/|'`" ]; then
  474. file="$dir/`echo $default | sed -e 's|-|/|'`"
  475. fi
  476. if [ -r "$file" ] ; then
  477. command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  478. command_exec=`which $command 2>/dev/null`
  479. arguments="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | last_word`"
  480. arg_one="`echo "$arg" | sed 's/[&*\\]/\\\\&/g'`"
  481. arguments_exec="`echo "$arguments" | sed -e 's*%[fFuU]*"'"$arg_one"'"*g'`"
  482. if [ -x "$command_exec" ] ; then
  483. if echo "$arguments" | grep -iq '%[fFuU]' ; then
  484. echo START "$command_exec" "$arguments_exec"
  485. eval "$command_exec" "$arguments_exec"
  486. else
  487. echo START "$command_exec" "$arguments_exec" "$arg"
  488. eval "$command_exec" "$arguments_exec" "$arg"
  489. fi
  490. if [ $? -eq 0 ]; then
  491. exit_success
  492. fi
  493. fi
  494. fi
  495. for d in $dir/*/; do
  496. [ -d "$d" ] && search_desktop_file "$default" "$d" "$arg"
  497. done
  498. }
  499. open_generic_xdg_mime()
  500. {
  501. filetype="$2"
  502. default=`xdg-mime query default "$filetype"`
  503. if [ -n "$default" ] ; then
  504. xdg_user_dir="$XDG_DATA_HOME"
  505. [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  506. xdg_system_dirs="$XDG_DATA_DIRS"
  507. [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  508. DEBUG 3 "$xdg_user_dir:$xdg_system_dirs"
  509. for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
  510. search_desktop_file "$default" "$x/applications/" "$1"
  511. done
  512. fi
  513. }
  514. open_generic_xdg_file_mime()
  515. {
  516. filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
  517. open_generic_xdg_mime "$1" "$filetype"
  518. }
  519. open_generic_xdg_x_scheme_handler()
  520. {
  521. scheme="`echo $1 | sed -n 's/\(^[[:alnum:]+\.-]*\):.*$/\1/p'`"
  522. if [ -n $scheme ]; then
  523. filetype="x-scheme-handler/$scheme"
  524. open_generic_xdg_mime "$1" "$filetype"
  525. fi
  526. }
  527. open_generic()
  528. {
  529. # Paths or file:// URLs
  530. if (echo "$1" | grep -q '^file://' ||
  531. ! echo "$1" | egrep -q '^[[:alpha:]+\.\-]+:'); then
  532. local file="$1"
  533. # Decode URLs
  534. if echo "$file" | grep -q '^file:///'; then
  535. file=${file#file://}
  536. file="$(printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
  537. fi
  538. check_input_file "$file"
  539. open_generic_xdg_file_mime "$file"
  540. if which run-mailcap 2>/dev/null 1>&2; then
  541. run-mailcap --action=view "$file"
  542. if [ $? -eq 0 ]; then
  543. exit_success
  544. fi
  545. fi
  546. if mimeopen -v 2>/dev/null 1>&2; then
  547. mimeopen -L -n "$file"
  548. if [ $? -eq 0 ]; then
  549. exit_success
  550. fi
  551. fi
  552. fi
  553. open_generic_xdg_x_scheme_handler "$1"
  554. IFS=":"
  555. for browser in $BROWSER; do
  556. if [ x"$browser" != x"" ]; then
  557. browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
  558. if [ $? -ne 0 ]; then
  559. browser_with_arg=$browser;
  560. fi
  561. if [ x"$browser_with_arg" = x"$browser" ]; then
  562. eval '$browser "$1"'$xdg_redirect_output;
  563. else eval '$browser_with_arg'$xdg_redirect_output;
  564. fi
  565. if [ $? -eq 0 ]; then
  566. exit_success;
  567. fi
  568. fi
  569. done
  570. exit_failure_operation_impossible "no method available for opening '$1'"
  571. }
  572. open_lxde()
  573. {
  574. # pcmanfm only knows how to handle file:// urls and filepaths, it seems.
  575. if (echo "$1" | grep -q '^file://' ||
  576. ! echo "$1" | egrep -q '^[[:alpha:]+\.\-]+:')
  577. then
  578. local file="$(echo "$1" | sed 's%^file://%%')"
  579. # handle relative paths
  580. if ! echo "$file" | grep -q '^/'; then
  581. file="$(pwd)/$file"
  582. fi
  583. pcmanfm "$file"
  584. else
  585. open_generic "$1"
  586. fi
  587. if [ $? -eq 0 ]; then
  588. exit_success
  589. else
  590. exit_failure_operation_failed
  591. fi
  592. }
  593. [ x"$1" != x"" ] || exit_failure_syntax
  594. url=
  595. while [ $# -gt 0 ] ; do
  596. parm="$1"
  597. shift
  598. case "$parm" in
  599. -*)
  600. exit_failure_syntax "unexpected option '$parm'"
  601. ;;
  602. *)
  603. if [ -n "$url" ] ; then
  604. exit_failure_syntax "unexpected argument '$parm'"
  605. fi
  606. url="$parm"
  607. ;;
  608. esac
  609. done
  610. if [ -z "${url}" ] ; then
  611. exit_failure_syntax "file or URL argument missing"
  612. fi
  613. detectDE
  614. if [ x"$DE" = x"" ]; then
  615. DE=generic
  616. fi
  617. DEBUG 2 "Selected DE $DE"
  618. # sanitize BROWSER (avoid caling ourselves in particular)
  619. case "${BROWSER}" in
  620. *:"xdg-open"|"xdg-open":*)
  621. BROWSER=$(echo $BROWSER | sed -e 's|:xdg-open||g' -e 's|xdg-open:||g')
  622. ;;
  623. "xdg-open")
  624. BROWSER=
  625. ;;
  626. esac
  627. # if BROWSER variable is not set, check some well known browsers instead
  628. if [ x"$BROWSER" = x"" ]; then
  629. BROWSER=links2:elinks:links:lynx:w3m
  630. if [ -n "$DISPLAY" ]; then
  631. BROWSER=x-www-browser:firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
  632. fi
  633. fi
  634. case "$DE" in
  635. kde)
  636. open_kde "$url"
  637. ;;
  638. gnome*)
  639. open_gnome "$url"
  640. ;;
  641. mate)
  642. open_mate "$url"
  643. ;;
  644. xfce)
  645. open_xfce "$url"
  646. ;;
  647. lxde)
  648. open_lxde "$url"
  649. ;;
  650. enlightenment)
  651. open_enlightenment "$url"
  652. ;;
  653. generic)
  654. open_generic "$url"
  655. ;;
  656. *)
  657. exit_failure_operation_impossible "no method available for opening '$url'"
  658. ;;
  659. esac