mozilla.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * ) url="leo:$url" ;;
  94. esac
  95. ;;
  96. esac
  97. case "$url" in
  98. http://* | https://* | ftp://* )
  99. : # URL is already fully qualified
  100. ;;
  101. google:* )
  102. # expand Google query
  103. url=`echo "$url" | sed -e 's;^google:;;' | tr ' ' '+'`
  104. url="http://www.google.com/search?q=$url"
  105. ;;
  106. leo:* )
  107. # expand Leo Dictionary query
  108. url=`echo "$url" | sed -e 's;^leo:;;' | tr ' ' '+'`
  109. url="http://dict.leo.org/?search=$url"
  110. ;;
  111. ftpsearch:* )
  112. # expand FTPSearch query
  113. url=`echo "$url" | sed -e 's;^ftpsearch:;;' | sed -e 's; ;%20;g'`
  114. url="http://www.alltheweb.com/search?advanced=1&cat=ftp&q=$url"
  115. ;;
  116. /* )
  117. # expand absolute filename into file URL
  118. url="file://$url"
  119. ;;
  120. * )
  121. # expand relative filename into file URL
  122. url="file://`pwd`/$url"
  123. ;;
  124. esac
  125. fi
  126. if [ ".$first" = .yes -a ".$restart" = .yes ]; then
  127. # handling of first URL if restarting is necessary
  128. verbose "running new process"
  129. if [ ".$url" = ".about:blank" ]; then
  130. verbose "exec: $mozilla_bin >/dev/null 2>&1 &"
  131. $mozilla_bin >/dev/null 2>&1 &
  132. else
  133. verbose "exec: $mozilla_bin \"$url\" >/dev/null 2>&1 &"
  134. $mozilla_bin "$url" >/dev/null 2>&1 &
  135. fi
  136. else
  137. # delayed waiting for process to be finally remotely available
  138. # if it was started from scratch for the handling of the first
  139. # URL. This way we wait only if necessary, i.e., if more than
  140. # one URL was given.
  141. if [ ".$first" = .no -a ".$restart" = .yes ]; then
  142. verbose "waiting for new process to be available remotely"
  143. sleep 4 # give it a little bit of time to fully startup
  144. i=0
  145. while [ $i -lt 10 ]; do
  146. $mozilla_bin -remote "ping()" >/dev/null 2>&1
  147. if [ $? -eq 0 ]; then
  148. break
  149. fi
  150. sleep 1 # give it a little bit more time to startup
  151. i=`expr $i + 1`
  152. done
  153. fi
  154. # determine remote command
  155. if [ ".$opt_w" = .yes ]; then
  156. if [ ".$url" = ".about:blank" ]; then
  157. cmd="xfeDoCommand(openBrowser)"
  158. else
  159. cmd="openURL($url, new-window)"
  160. fi
  161. elif [ ".$opt_t" = .yes ]; then
  162. cmd="openURL($url, new-tab)"
  163. else
  164. cmd="openURL($url)"
  165. fi
  166. # perform remote command
  167. verbose "sending remote command to running process"
  168. verbose "exec: $mozilla_bin -remote \"$cmd\" >/dev/null 2>&1"
  169. $mozilla_bin -remote "$cmd" >/dev/null 2>&1
  170. fi
  171. first=no
  172. done