firefox.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/sh
  2. ##
  3. ## firefox -- Mozilla Firefox Startup Control Utility
  4. ## Copyright (c) 2000-2005 OpenPKG Foundation e.V. <http://openpkg.net/>
  5. ## Copyright (c) 2000-2005 Ralf S. Engelschall <http://engelschall.com/>
  6. ##
  7. ## Permission to use, copy, modify, and distribute this software for
  8. ## any purpose with or without fee is hereby granted, provided that
  9. ## the above copyright notice and this permission notice appear in all
  10. ## copies.
  11. ##
  12. ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  13. ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
  16. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. ## SUCH DAMAGE.
  24. ##
  25. # configuration
  26. firefox_dir="@l_prefix@/lib/firefox"
  27. firefox_browser_bin="$firefox_dir/firefox"
  28. firefox_remote_bin="$firefox_dir/run-mozilla.sh $firefox_dir/mozilla-xremote-client"
  29. xsel_bin="@l_prefix@/bin/xsel"
  30. wmctrl_bin="@l_prefix@/bin/wmctrl"
  31. # option parsing
  32. opt_v=no
  33. opt_w=no
  34. opt_t=no
  35. opt_r=""
  36. while [ ".$1" != . ]; do
  37. case $1 in
  38. -v|--verbose ) opt_v=yes; shift ;;
  39. -w|--window ) opt_w=yes; shift ;;
  40. -t|--tab ) opt_t=yes; shift ;;
  41. -r|-remote|--remote ) opt_r="$2"; shift; shift ;;
  42. -* ) echo "$0:ERROR: invalid option \"$1\"" 1>&2; exit 1 ;;
  43. * ) break ;;
  44. esac
  45. done
  46. if [ $# -eq 0 ]; then
  47. set -- ""
  48. fi
  49. verbose () {
  50. if [ ".$opt_v" = .yes ]; then
  51. echo "firefox: $*" 1>&2
  52. fi
  53. }
  54. # establish Mozilla environment
  55. MOZILLA_FIVE_HOME="$firefox_dir"
  56. export MOZILLA_FIVE_HOME
  57. LANG=en_US.ISO8859-1
  58. export LANG
  59. # special case of direct remote option
  60. if [ ".$opt_r" != . ]; then
  61. verbose "exec: $firefox_remote_bin \"$opt_r\""
  62. exec $firefox_remote_bin "$opt_r"
  63. fi
  64. # determine Mozilla run-time status
  65. $firefox_remote_bin "ping()" >/dev/null 2>&1
  66. if [ $? -eq 0 ]; then
  67. verbose "process already running (available remotely)"
  68. restart=no
  69. else
  70. verbose "process still not running (not available remotely)"
  71. restart=yes
  72. fi
  73. # open one or more URLs
  74. first=yes
  75. for url in "$@"; do
  76. # determine URL to open
  77. if [ ".`echo .$url | grep '@'`" != . ]; then
  78. # expand at-sign into X11 selection buffer
  79. xsel=`($xsel_bin --paste) 2>/dev/null`
  80. url=`echo "$url" | sed -e "s;@;$xsel;"`
  81. fi
  82. if [ ".$url" = . ]; then
  83. # expand an empty URL into the internal blank page
  84. url="about:blank"
  85. else
  86. # expand special URL constructs
  87. case "$url" in
  88. auto:* )
  89. url=`echo "$url" | sed -e 's;^auto:;;'`
  90. case "$url" in
  91. http://* | https://* | ftp://* | file://* ) ;;
  92. www.* ) url="http://$url" ;;
  93. ftp.* ) url="ftp://$url" ;;
  94. *.tar.gz | *.tgz ) url="ftpsearch:$url" ;;
  95. *" "* ) url="google:$url" ;;
  96. [0-9]* ) url="rfc:$url" ;;
  97. * ) url="leo:$url" ;;
  98. esac
  99. ;;
  100. esac
  101. case "$url" in
  102. http://* | https://* | ftp://* | file://* )
  103. : # URL is already fully qualified
  104. ;;
  105. google:* )
  106. # expand Google query
  107. url=`echo "$url" | sed -e 's;^google:;;' | tr ' ' '+'`
  108. url="http://www.google.com/search?q=$url"
  109. ;;
  110. leo:* )
  111. # expand Leo Dictionary query
  112. url=`echo "$url" | sed -e 's;^leo:;;' | tr ' ' '+'`
  113. url="http://dict.leo.org/?search=$url"
  114. ;;
  115. ftpsearch:* )
  116. # expand FTPSearch query
  117. url=`echo "$url" | sed -e 's;^ftpsearch:;;' | sed -e 's; ;%20;g'`
  118. url="http://www.alltheweb.com/search?advanced=1&cat=ftp&q=$url"
  119. ;;
  120. rfc:* )
  121. # expand Zvon.org RFC query
  122. url=`echo "$url" | sed -e 's;^rfc:;;'`
  123. url="http://zvon.org/tmRFC/RFC$url/Output/index.html"
  124. ;;
  125. whois:* )
  126. # expand Geektools.com WHOIS query
  127. url=`echo "$url" | sed -e 's;^whois:;;'`
  128. url="http://www.whois-search.com/whois/$url"
  129. ;;
  130. jargon:* )
  131. # expand UMEC Jargon File query
  132. url=`echo "$url" | sed -e 's;^jargon:;;' | tr ' ' '+'`
  133. url="http://www.tf.hut.fi/cgi-bin/jargon?search=$url"
  134. ;;
  135. /* )
  136. # expand absolute filename into file URL
  137. url="file://$url"
  138. ;;
  139. * )
  140. # expand relative filename into file URL
  141. url="file://`pwd`/$url"
  142. ;;
  143. esac
  144. fi
  145. if [ ".$first" = .yes -a ".$restart" = .yes ]; then
  146. # handling of first URL if restarting is necessary
  147. verbose "running new process"
  148. if [ ".$url" = ".about:blank" ]; then
  149. verbose "exec: $firefox_browser_bin >/dev/null 2>&1 &"
  150. $firefox_browser_bin >/dev/null 2>&1 &
  151. else
  152. verbose "exec: $firefox_browser_bin \"$url\" >/dev/null 2>&1 &"
  153. $firefox_browser_bin "$url" >/dev/null 2>&1 &
  154. fi
  155. else
  156. # delayed waiting for process to be finally remotely available
  157. # if it was started from scratch for the handling of the first
  158. # URL. This way we wait only if necessary, i.e., if more than
  159. # one URL was given.
  160. if [ ".$first" = .no -a ".$restart" = .yes ]; then
  161. verbose "waiting for new process to be available remotely"
  162. sleep 4 # give it a little bit of time to fully startup
  163. i=0
  164. while [ $i -lt 10 ]; do
  165. $firefox_remote_bin "ping()" >/dev/null 2>&1
  166. if [ $? -eq 0 ]; then
  167. break
  168. fi
  169. sleep 1 # give it a little bit more time to startup
  170. i=`expr $i + 1`
  171. done
  172. fi
  173. # try to activate (and bring to foreground) the Mozilla Firefox window
  174. $wmctrl_bin -a "Mozilla Firefox" >/dev/null 2>&1 || true
  175. # determine remote command
  176. if [ ".$opt_w" = .yes ]; then
  177. if [ ".$url" = ".about:blank" ]; then
  178. cmd="xfeDoCommand(openBrowser)"
  179. else
  180. cmd="openURL($url, new-window)"
  181. fi
  182. elif [ ".$opt_t" = .yes ]; then
  183. cmd="openURL($url, new-tab)"
  184. else
  185. cmd="openURL($url)"
  186. fi
  187. # perform remote command
  188. verbose "sending remote command to running process"
  189. verbose "exec: $firefox_remote_bin \"$cmd\" >/dev/null 2>&1"
  190. $firefox_remote_bin "$cmd" >/dev/null 2>&1
  191. fi
  192. first=no
  193. done