|
#!/bin/sh
|
|
|
|
# This script starts an instance of Xvfb, the "fake" X server, runs a command
|
|
# with that server available, and kills the X server when done. The return
|
|
# value of the command becomes the return value of this script.
|
|
|
|
# This is a stripped-down version of the 'xvfb-run' script provided by
|
|
# Debian's 'xvfb' package which itself is licensed under the GPL, just as
|
|
# Lx-Office is. It is unclear who wrote the original script, but the
|
|
# CVS Id tag mentioned 'branden'.
|
|
|
|
set -e
|
|
|
|
PROGNAME=xvfb-run
|
|
SERVERNUM=99
|
|
AUTHFILE=
|
|
ERRORFILE=/dev/null
|
|
STARTWAIT=3
|
|
XVFBARGS="-screen 0 640x480x8"
|
|
LISTENTCP="-nolisten tcp"
|
|
XAUTHPROTO=.
|
|
|
|
# Query the terminal to establish a default number of columns to use for
|
|
# displaying messages to the user. This is used only as a fallback in the event
|
|
# the COLUMNS variable is not set. ($COLUMNS can react to SIGWINCH while the
|
|
# script is running, and this cannot, only being calculated once.)
|
|
DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
|
|
if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
|
|
DEFCOLUMNS=80
|
|
fi
|
|
|
|
# Display a message, wrapping lines at the terminal width.
|
|
message () {
|
|
echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
|
|
}
|
|
|
|
# Display an error message.
|
|
error () {
|
|
message "error: $*" >&2
|
|
}
|
|
|
|
# Find a free server number by looking at .X*-lock files in /tmp.
|
|
find_free_servernum() {
|
|
# Sadly, the "local" keyword is not POSIX. Leave the next line commented in
|
|
# the hope Debian Policy eventually changes to allow it in /bin/sh scripts
|
|
# anyway.
|
|
#local i
|
|
|
|
i=$SERVERNUM
|
|
while [ -f /tmp/.X$i-lock ]; do
|
|
i=$(($i + 1))
|
|
done
|
|
echo $i
|
|
}
|
|
|
|
SERVERNUM=$(find_free_servernum)
|
|
|
|
PATH=$PATH:/usr/bin/x11:/usr/X11R6/bin
|
|
|
|
if ! which xauth >/dev/null; then
|
|
error "xauth command not found"
|
|
exit 3
|
|
fi
|
|
|
|
# If the user did not specify an X authorization file to use, set up a temporary
|
|
# directory to house one.
|
|
XVFB_RUN_TMPDIR="${TMPDIR:-/tmp}/$PROGNAME.$$"
|
|
if ! mkdir -p -m 700 "$XVFB_RUN_TMPDIR"; then
|
|
error "temporary directory $XVFB_RUN_TMPDIR already exists"
|
|
exit 4
|
|
fi
|
|
AUTHFILE=$(tempfile -n "$XVFB_RUN_TMPDIR/Xauthority")
|
|
|
|
# Start Xvfb.
|
|
MCOOKIE=$(mcookie)
|
|
XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \
|
|
>"$ERRORFILE" 2>&1
|
|
XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \
|
|
2>&1 &
|
|
XVFBPID=$!
|
|
sleep "$STARTWAIT"
|
|
|
|
# Start the command and save its exit status.
|
|
set +e
|
|
DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
|
|
RETVAL=$?
|
|
set -e
|
|
|
|
# Kill Xvfb now that the command has exited.
|
|
kill $XVFBPID
|
|
|
|
# Clean up.
|
|
XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
|
|
if [ -n "$XVFB_RUN_TMPDIR" ]; then
|
|
if ! rm -r "$XVFB_RUN_TMPDIR"; then
|
|
error "problem while cleaning up temporary directory"
|
|
exit 5
|
|
fi
|
|
fi
|
|
|
|
# Return the executed command's exit status.
|
|
exit $RETVAL
|