firefox.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_bin="@l_prefix@/lib/firefox/firefox"
  28. xsel_bin="@l_prefix@/bin/xsel"
  29. # option parsing
  30. opt_v=no
  31. opt_w=no
  32. opt_t=no
  33. opt_r=""
  34. while [ ".$1" != . ]; do
  35. case $1 in
  36. -v|--verbose ) opt_v=yes; shift ;;
  37. -w|--window ) opt_w=yes; shift ;;
  38. -t|--tab ) opt_t=yes; shift ;;
  39. -r|-remote|--remote ) opt_r="$2"; shift; shift ;;
  40. -* ) echo "$0:ERROR: invalid option \"$1\"" 1>&2; exit 1 ;;
  41. * ) break ;;
  42. esac
  43. done
  44. if [ $# -eq 0 ]; then
  45. set -- ""
  46. fi
  47. verbose () {
  48. if [ ".$opt_v" = .yes ]; then
  49. echo "firefox: $*" 1>&2
  50. fi
  51. }
  52. # establish Mozilla environment
  53. MOZILLA_FIVE_HOME="$firefox_dir"
  54. export MOZILLA_FIVE_HOME
  55. LANG=en_US.ISO8859-1
  56. export LANG
  57. # special case of direct remote option
  58. if [ ".$opt_r" != . ]; then
  59. verbose "exec: $firefox_bin -remote \"$opt_r\""
  60. exec $firefox_bin -remote "$opt_r"
  61. fi
  62. # determine Mozilla run-time status
  63. $firefox_bin -remote "ping()" >/dev/null 2>&1
  64. if [ $? -eq 0 ]; then
  65. verbose "process already running (available remotely)"
  66. restart=no
  67. else
  68. verbose "process still not running (not available remotely)"
  69. restart=yes
  70. fi
  71. # open one or more URLs
  72. first=yes
  73. for url in "$@"; do
  74. # determine URL to open
  75. if [ ".`echo .$url | grep '@'`" != . ]; then
  76. # expand at-sign into X11 selection buffer
  77. xsel=`($xsel_bin --paste) 2>/dev/null`
  78. url=`echo "$url" | sed -e "s;@;$xsel;"`
  79. fi
  80. if [ ".$url" = . ]; then
  81. # expand an empty URL into the internal blank page
  82. url="about:blank"
  83. else
  84. # expand special URL constructs
  85. case "$url" in
  86. auto:* )
  87. url=`echo "$url" | sed -e 's;^auto:;;'`
  88. case "$url" in
  89. http://* | https://* | ftp://* ) ;;
  90. www.* ) url="http://$url" ;;
  91. ftp.* ) url="ftp://$url" ;;
  92. *.tar.gz | *.tgz ) url="ftpsearch:$url" ;;
  93. *" "* ) url="google:$url" ;;
  94. [0-9]* ) url="rfc:$url" ;;
  95. * ) url="leo:$url" ;;
  96. esac
  97. ;;
  98. esac
  99. case "$url" in
  100. http://* | https://* | ftp://* )
  101. : # URL is already fully qualified
  102. ;;
  103. google:* )
  104. # expand Google query
  105. url=`echo "$url" | sed -e 's;^google:;;' | tr ' ' '+'`
  106. url="http://www.google.com/search?q=$url"
  107. ;;
  108. leo:* )
  109. # expand Leo Dictionary query
  110. url=`echo "$url" | sed -e 's;^leo:;;' | tr ' ' '+'`
  111. url="http://dict.leo.org/?search=$url"
  112. ;;
  113. ftpsearch:* )
  114. # expand FTPSearch query
  115. url=`echo "$url" | sed -e 's;^ftpsearch:;;' | sed -e 's; ;%20;g'`
  116. url="http://www.alltheweb.com/search?advanced=1&cat=ftp&q=$url"
  117. ;;
  118. rfc:* )
  119. # expand Zvon.org RFC query
  120. url=`echo "$url" | sed -e 's;^rfc:;;'`
  121. url="http://zvon.org/tmRFC/RFC$url/Output/index.html"
  122. ;;
  123. whois:* )
  124. # expand Geektools.com WHOIS query
  125. url=`echo "$url" | sed -e 's;^whois:;;'`
  126. url="http://www.whois-search.com/whois/$url"
  127. ;;
  128. jargon:* )
  129. # expand UMEC Jargon File query
  130. url=`echo "$url" | sed -e 's;^jargon:;;' | tr ' ' '+'`
  131. url="http://www.tf.hut.fi/cgi-bin/jargon?search=$url"
  132. ;;
  133. /* )
  134. # expand absolute filename into file URL
  135. url="file://$url"
  136. ;;
  137. * )
  138. # expand relative filename into file URL
  139. url="file://`pwd`/$url"
  140. ;;
  141. esac
  142. fi
  143. if [ ".$first" = .yes -a ".$restart" = .yes ]; then
  144. # handling of first URL if restarting is necessary
  145. verbose "running new process"
  146. if [ ".$url" = ".about:blank" ]; then
  147. verbose "exec: $firefox_bin >/dev/null 2>&1 &"
  148. $firefox_bin >/dev/null 2>&1 &
  149. else
  150. verbose "exec: $firefox_bin \"$url\" >/dev/null 2>&1 &"
  151. $firefox_bin "$url" >/dev/null 2>&1 &
  152. fi
  153. else
  154. # delayed waiting for process to be finally remotely available
  155. # if it was started from scratch for the handling of the first
  156. # URL. This way we wait only if necessary, i.e., if more than
  157. # one URL was given.
  158. if [ ".$first" = .no -a ".$restart" = .yes ]; then
  159. verbose "waiting for new process to be available remotely"
  160. sleep 4 # give it a little bit of time to fully startup
  161. i=0
  162. while [ $i -lt 10 ]; do
  163. $firefox_bin -remote "ping()" >/dev/null 2>&1
  164. if [ $? -eq 0 ]; then
  165. break
  166. fi
  167. sleep 1 # give it a little bit more time to startup
  168. i=`expr $i + 1`
  169. done
  170. fi
  171. # determine remote command
  172. if [ ".$opt_w" = .yes ]; then
  173. if [ ".$url" = ".about:blank" ]; then
  174. cmd="xfeDoCommand(openBrowser)"
  175. else
  176. cmd="openURL($url, new-window)"
  177. fi
  178. elif [ ".$opt_t" = .yes ]; then
  179. cmd="openURL($url, new-tab)"
  180. else
  181. cmd="openURL($url)"
  182. fi
  183. # perform remote command
  184. verbose "sending remote command to running process"
  185. verbose "exec: $firefox_bin -remote \"$cmd\" >/dev/null 2>&1"
  186. $firefox_bin -remote "$cmd" >/dev/null 2>&1
  187. fi
  188. first=no
  189. done