OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.9 KiB

  1. #!/bin/sh
  2. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # Filename: package/.../xorg-server/xvfb-run.sh
  6. # Copyright (C) 2008 The OpenSDE Project
  7. # Copyright (C) 2005 - 2006 The T2 SDE Project
  8. # Copyright (C) XXXX - 2005 Debian
  9. #
  10. # More information can be found in the files COPYING and README.
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; version 2 of the License. A copy of the
  15. # GNU General Public License can be found in the file COPYING.
  16. # --- SDE-COPYRIGHT-NOTE-END ---
  17. # $Id: xvfb-run 2166 2005-01-27 07:54:19Z branden $
  18. # from: http://necrotic.deadbeast.net/xsf/XFree86/trunk/debian/local/xvfb-run
  19. # This script starts an instance of Xvfb, the "fake" X server, runs a command
  20. # with that server available, and kills the X server when done. The return
  21. # value of the command becomes the return value of this script.
  22. #
  23. # If anyone is using this to build a Debian package, make sure the package
  24. # Build-Depends on xvfb, xbase-clients, and xfonts-base.
  25. set -e
  26. PROGNAME=xvfb-run
  27. SERVERNUM=99
  28. AUTHFILE=
  29. ERRORFILE=/dev/null
  30. STARTWAIT=3
  31. XVFBARGS="-screen 0 640x480x8"
  32. LISTENTCP="-nolisten tcp"
  33. XAUTHPROTO=.
  34. # Query the terminal to establish a default number of columns to use for
  35. # displaying messages to the user. This is used only as a fallback in the event
  36. # the COLUMNS variable is not set. ($COLUMNS can react to SIGWINCH while the
  37. # script is running, and this cannot, only being calculated once.)
  38. DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
  39. if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
  40. DEFCOLUMNS=80
  41. fi
  42. # Display a message, wrapping lines at the terminal width.
  43. message () {
  44. echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
  45. }
  46. # Display an error message.
  47. error () {
  48. message "error: $*" >&2
  49. }
  50. # Display a usage message.
  51. usage () {
  52. if [ -n "$*" ]; then
  53. message "usage error: $*"
  54. fi
  55. cat <<EOF
  56. Usage: $PROGNAME [OPTION ...] COMMAND
  57. Run COMMAND (usually an X client) in a virtual X server environment.
  58. Options:
  59. -a --auto-servernum try to get a free server number, starting at
  60. --server-num
  61. -e FILE --error-file=FILE file used to store xauth errors and Xvfb
  62. output (default: $ERRORFILE)
  63. -f FILE --auth-file=FILE file used to store auth cookie
  64. (default: ./.Xauthority)
  65. -h --help display this usage message and exit
  66. -n NUM --server-num=NUM server number to use (default: $SERVERNUM)
  67. -l --listen-tcp enable TCP port listening in the X server
  68. -p PROTO --xauth-protocol=PROTO X authority protocol name to use
  69. (default: xauth command's default)
  70. -s ARGS --server-args=ARGS arguments (other than server number and
  71. "-nolisten tcp") to pass to the Xvfb server
  72. (default: "$XVFBARGS")
  73. -w DELAY --wait=DELAY delay in seconds to wait for Xvfb to start
  74. before running COMMAND (default: $STARTWAIT)
  75. EOF
  76. }
  77. # Find a free server number by looking at .X*-lock files in /tmp.
  78. find_free_servernum() {
  79. # Sadly, the "local" keyword is not POSIX. Leave the next line commented in
  80. # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
  81. # anyway.
  82. #local i
  83. i=$SERVERNUM
  84. while [ -f /tmp/.X$i-lock ]; do
  85. i=$(($i + 1))
  86. done
  87. echo $i
  88. }
  89. # Parse the command line.
  90. ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
  91. --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
  92. --name "$PROGNAME" -- "$@")
  93. GETOPT_STATUS=$?
  94. if [ $GETOPT_STATUS -ne 0 ]; then
  95. error "internal error; getopt exited with status $GETOPT_STATUS"
  96. exit 6
  97. fi
  98. eval set -- "$ARGS"
  99. while :; do
  100. case "$1" in
  101. -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
  102. -e|--error-file) ERRORFILE="$2"; shift ;;
  103. -f|--auth-file) AUTHFILE="$2"; shift ;;
  104. -h|--help) SHOWHELP="yes" ;;
  105. -n|--server-num) SERVERNUM="$2"; shift ;;
  106. -l|--listen-tcp) LISTENTCP= ;;
  107. -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
  108. -s|--server-args) XVFBARGS="$2"; shift ;;
  109. -w|--wait) STARTWAIT="$2"; shift ;;
  110. --) shift; break ;;
  111. *) error "internal error; getopt permitted \"$1\" unexpectedly"
  112. exit 6
  113. ;;
  114. esac
  115. shift
  116. done
  117. if [ "$SHOWHELP" ]; then
  118. usage
  119. exit 0
  120. fi
  121. if [ -z "$*" ]; then
  122. usage "need a command to run" >&2
  123. exit 2
  124. fi
  125. if ! which xauth >/dev/null; then
  126. error "xauth command not found"
  127. exit 3
  128. fi
  129. # If the user did not specify an X authorization file to use, set up a temporary
  130. # directory to house one.
  131. if [ -z "$AUTHFILE" ]; then
  132. XVFB_RUN_TMPDIR="${TMPDIR:-/tmp}/$PROGNAME.$$"
  133. if ! mkdir -p -m 700 "$XVFB_RUN_TMPDIR"; then
  134. error "temporary directory $XVFB_RUN_TMPDIR already exists"
  135. exit 4
  136. fi
  137. AUTHFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" Xauthority)
  138. fi
  139. # Start Xvfb.
  140. MCOOKIE=$(mcookie)
  141. XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \
  142. >"$ERRORFILE" 2>&1
  143. XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \
  144. 2>&1 &
  145. XVFBPID=$!
  146. sleep "$STARTWAIT"
  147. # Start the command and save its exit status.
  148. set +e
  149. DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
  150. RETVAL=$?
  151. set -e
  152. # Kill Xvfb now that the command has exited.
  153. kill $XVFBPID
  154. # Clean up.
  155. XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
  156. if [ -n "$XVFB_RUN_TMPDIR" ]; then
  157. if ! rm -r "$XVFB_RUN_TMPDIR"; then
  158. error "problem while cleaning up temporary directory"
  159. exit 5
  160. fi
  161. fi
  162. # Return the executed command's exit status.
  163. exit $RETVAL
  164. # vim:set ai et sts=4 sw=4 tw=80: