home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / orca < prev    next >
Encoding:
Text File  |  2006-08-21  |  5.0 KB  |  191 lines

  1. #!/bin/sh
  2. #
  3. # Orca
  4. #
  5. # Copyright 2006 Sun Microsystems Inc.
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Library General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. # Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Library General Public
  18. # License along with this library; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. # Boston, MA 02111-1307, USA.
  21.  
  22. # This script performs some clean up and will run Orca.  It will also
  23. # rerun Orca if it detects that Orca died an unnatural death.
  24.  
  25. # __id__        = "$Id: orca.in,v 1.14 2006/08/18 22:19:46 wwalker Exp $"
  26. # __version__   = "$Revision: 1.14 $"
  27. # __date__      = "$Date: 2006/08/18 22:19:46 $"
  28. # __copyright__ = "Copyright (c) 2005-2006 Sun Microsystems Inc."
  29. # __license__   = "LGPL"
  30.  
  31. # If you set RUNONCE to "true", then this will just run Orca once and quit.
  32. #
  33. RUNONCE="false"
  34.  
  35. # A value of 1 means to run Orca.  A value of 0 means quit.
  36. #
  37. RUN=1
  38.  
  39. # Initially there will be no watchdog process id.
  40. #
  41. watchdog_pid=0
  42.  
  43. # Save the arguments away.
  44. #
  45. ARGS="$*"
  46.  
  47. # The watchdog will periodically ping Orca to see if it is responding.
  48. # If orca isn't responding, the watchdog will kill the Orca process.
  49. # The watchdog logic requires 'wget', so we won't do it if we can't
  50. # find wget in the path.  Note also that you can force WATCHDOG=0 if you
  51. # do not want a background process that periodically pings Orca to see
  52. # if it is responding.
  53. #
  54. IFS=:
  55. WGETCMD=
  56. WATCHDOG=0
  57. for dir in $PATH:/usr/sfw/bin:/usr/local/bin; do
  58.     test -x "$dir/wget" && {
  59.         WGETCMD="$dir/wget"
  60.         WATCHDOG=1
  61.         break
  62.     }
  63. done
  64.  
  65. # Cleans up any orca-related processes that might be running,
  66. # restricting it to those processes owned by the user. These include
  67. # orca itself, any gnome-speech synthesis drivers, and festival
  68. # processes running in server mode.
  69. #
  70. cleanup()
  71. {
  72.     USERID=`id | cut -f2 -d= | cut -f1 -d\(`
  73.     PIDS=`ps -eo pid,ruid,args | grep $USERID | egrep "orca[.]orca|synthesis-driver|festival [-][-]server" | grep -v grep | awk '{ print $1 }'`
  74.  
  75.     IFS='
  76.     '
  77.     PIDS=`echo $PIDS`
  78.  
  79.     if [ "x$PIDS" != "x" ]
  80.     then
  81.         kill -9 $PIDS > /dev/null
  82.     fi
  83. }
  84.  
  85. # Runs orca.
  86. #
  87. runOrca()
  88. {
  89.     cleanup
  90.     exec_prefix=/usr
  91.     PYTHONPATH=${exec_prefix}/lib/python2.4/site-packages
  92.     export PYTHONPATH
  93.     /usr/bin/python -c "import orca.orca; orca.orca.main()" "$ARGS"
  94. }
  95.  
  96. # Runs a watchdog process in the background.  It merely attempts to
  97. # get to Orca via some other means than the AT-SPI.  Here we use
  98. # Orca's http server at port 20433.  If it doesn't respond, then
  99. # we assume Orca is dead.
  100. #
  101. watchdog()
  102. {
  103.     (
  104.         sleep 30 # Give orca a chance to start.
  105.         while [ "$WATCHDOG" -gt 0 ]
  106.         do
  107.             sleep 5
  108.             USERID=`id | cut -f2 -d= | cut -f1 -d\(`
  109.             PID=`ps -eo pid,ruid,args | grep $USERID | egrep "orca[.]orca" | grep -v grep | awk '{ print $1 }'`
  110.  
  111.             if [ "x$PID" = "x" ]
  112.             then
  113.                 exit
  114.             else
  115.                 $WGETCMD -q -t 0 -O /dev/null -w 2 "http://localhost:20433" || {
  116.                     echo Orca watchdog detected something bad.  Cleaning up.
  117.                     cleanup
  118.                 }
  119.             fi
  120.         done
  121.     ) &
  122. }
  123.  
  124. kill_orca()
  125. {
  126.     cleanup
  127.     if [ "x$watchdog_pid" != x0 ]
  128.     then
  129.         kill -9 $watchdog_pid > /dev/null
  130.     fi
  131.     exit
  132. }
  133.  
  134. hup_orca()
  135. {
  136.     cleanup
  137. }
  138.  
  139. main()
  140. {
  141.     if [ "$WATCHDOG" -gt 0 ]
  142.     then
  143.         watchdog
  144.         watchdog_pid=$!
  145.     fi
  146.     while [ "$RUN" -gt 0 ]
  147.     do
  148.         runOrca &
  149.     orca_pid=$!
  150.     wait $orca_pid
  151.  
  152.         RUN=$?  # quit on a normal exit status from Orca
  153.  
  154.     # We will stop re-running Orca on SEGV's (139 = SEGV + 128).
  155.     # The reason for this is that there are cases where Python
  156.     # will SEGV when Orca attempts to exit normally.  This happens
  157.     # because of something going on in pyorbit.  This should be
  158.     # fixed in pyorbit 2.14.1, but not everyone has that.
  159.     # So...we'll check for it.  
  160.         #
  161.     if [ "$RUN" -eq 139 ]
  162.     then
  163.         RUN=0
  164.         fi
  165.     done
  166.     kill_orca
  167. }
  168.  
  169. trap kill_orca QUIT TERM INT ABRT
  170. trap hup_orca HUP
  171.  
  172. # Orca will fall into a text-based question and answer session if the
  173. # user has not configured orca and/or accessibility yet.  We will
  174. # force that to happen in the foreground (i.e., RUNONCE=true).  In
  175. # addition, if the user passes any command line arguments to orca, we
  176. # will run it in the foreground as well to avoid a situation where
  177. # orca dumps itself into the text-based setup utility.
  178. #
  179. ACCESSIBILITY_ENABLED=`gconftool-2 --get /desktop/gnome/interface/accessibility`
  180. if [ "x$ACCESSIBILITY_ENABLED" != "xtrue" ]
  181. then
  182.     RUNONCE="true"
  183. fi
  184.  
  185. if [ "x$RUNONCE" = "xfalse" -a "x$ARGS" = "x" ]
  186. then
  187.     main
  188. else
  189.     runOrca
  190. fi
  191.