mozilla.sh 6.7 KB

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