Blame SOURCES/xvfb-run.sh

877270
#!/bin/sh
877270
# --- T2-COPYRIGHT-NOTE-BEGIN ---
877270
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
877270
# 
877270
# T2 SDE: package/.../xorg-server/xvfb-run.sh
877270
# Copyright (C) 2005 The T2 SDE Project
877270
# Copyright (C) XXXX - 2005 Debian
877270
# 
877270
# More information can be found in the files COPYING and README.
877270
# 
877270
# This program is free software; you can redistribute it and/or modify
877270
# it under the terms of the GNU General Public License as published by
877270
# the Free Software Foundation; version 2 of the License. A copy of the
877270
# GNU General Public License can be found in the file COPYING.
877270
# --- T2-COPYRIGHT-NOTE-END ---
877270
877270
# $Id$
877270
# from: http://necrotic.deadbeast.net/xsf/XFree86/trunk/debian/local/xvfb-run
877270
877270
# This script starts an instance of Xvfb, the "fake" X server, runs a command
877270
# with that server available, and kills the X server when done.  The return
877270
# value of the command becomes the return value of this script.
877270
#
877270
# If anyone is using this to build a Debian package, make sure the package
877270
# Build-Depends on xvfb, xbase-clients, and xfonts-base.
877270
877270
set -e
877270
877270
PROGNAME=xvfb-run
877270
SERVERNUM=99
877270
AUTHFILE=
877270
ERRORFILE=/dev/null
877270
STARTWAIT=3
877270
XVFBARGS="-screen 0 640x480x24"
877270
LISTENTCP="-nolisten tcp"
877270
XAUTHPROTO=.
877270
877270
# Query the terminal to establish a default number of columns to use for
877270
# displaying messages to the user.  This is used only as a fallback in the event
877270
# the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while the
877270
# script is running, and this cannot, only being calculated once.)
877270
DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
877270
if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
877270
    DEFCOLUMNS=80
877270
fi
877270
877270
# Display a message, wrapping lines at the terminal width.
877270
message () {
877270
    echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
877270
}
877270
877270
# Display an error message.
877270
error () {
877270
    message "error: $*" >&2
877270
}
877270
877270
# Display a usage message.
877270
usage () {
877270
    if [ -n "$*" ]; then
877270
        message "usage error: $*"
877270
    fi
877270
    cat <
877270
Usage: $PROGNAME [OPTION ...] COMMAND
877270
Run COMMAND (usually an X client) in a virtual X server environment.
877270
Options:
877270
-a        --auto-servernum          try to get a free server number, starting at
877270
                                    --server-num (deprecated, use --auto-display
877270
                                    instead)
877270
-d        --auto-display            use the X server to find a display number
877270
                                    automatically
877270
-e FILE   --error-file=FILE         file used to store xauth errors and Xvfb
877270
                                    output (default: $ERRORFILE)
877270
-f FILE   --auth-file=FILE          file used to store auth cookie
877270
                                    (default: ./.Xauthority)
877270
-h        --help                    display this usage message and exit
877270
-n NUM    --server-num=NUM          server number to use (default: $SERVERNUM)
877270
-l        --listen-tcp              enable TCP port listening in the X server
877270
-p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
877270
                                    (default: xauth command's default)
877270
-s ARGS   --server-args=ARGS        arguments (other than server number and
877270
                                    "-nolisten tcp") to pass to the Xvfb server
877270
                                    (default: "$XVFBARGS")
877270
-w DELAY  --wait=DELAY              delay in seconds to wait for Xvfb to start
877270
                                    before running COMMAND (default: $STARTWAIT)
877270
EOF
877270
}
877270
877270
# Find a free server number by looking at .X*-lock files in /tmp.
877270
find_free_servernum() {
877270
    # Sadly, the "local" keyword is not POSIX.  Leave the next line commented in
877270
    # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
877270
    # anyway.
877270
    #local i
877270
877270
    i=$SERVERNUM
877270
    while [ -f /tmp/.X$i-lock ]; do
877270
        i=$(($i + 1))
877270
    done
877270
    echo $i
877270
}
877270
877270
# Parse the command line.
877270
ARGS=$(getopt --options +ade:f:hn:lp:s:w: \
877270
       --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
877270
       --name "$PROGNAME" -- "$@")
877270
GETOPT_STATUS=$?
877270
877270
if [ $GETOPT_STATUS -ne 0 ]; then
877270
    error "internal error; getopt exited with status $GETOPT_STATUS"
877270
    exit 6
877270
fi
877270
877270
eval set -- "$ARGS"
877270
877270
while :; do
877270
    case "$1" in
877270
        -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
877270
        -d|--auto-display) AUTO_DISPLAY=1 ;;
877270
        -e|--error-file) ERRORFILE="$2"; shift ;;
877270
        -f|--auth-file) AUTHFILE="$2"; shift ;;
877270
        -h|--help) SHOWHELP="yes" ;;
877270
        -n|--server-num) SERVERNUM="$2"; shift ;;
877270
        -l|--listen-tcp) LISTENTCP="" ;;
877270
        -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
877270
        -s|--server-args) XVFBARGS="$2"; shift ;;
877270
        -w|--wait) STARTWAIT="$2"; shift ;;
877270
        --) shift; break ;;
877270
        *) error "internal error; getopt permitted \"$1\" unexpectedly"
877270
           exit 6
877270
           ;;
877270
    esac
877270
    shift
877270
done
877270
877270
if [ "$SHOWHELP" ]; then
877270
    usage
877270
    exit 0
877270
fi
877270
877270
if [ -z "$*" ]; then
877270
    usage "need a command to run" >&2
877270
    exit 2
877270
fi
877270
877270
if ! type xauth >/dev/null; then
877270
    error "xauth command not found"
877270
    exit 3
877270
fi
877270
877270
# Set up the temp dir for the pid and X authorization file
877270
XVFB_RUN_TMPDIR="$(mktemp --directory --tmpdir $PROGNAME.XXXXXX)"
877270
# If the user did not specify an X authorization file to use, set up a temporary
877270
# directory to house one.
877270
if [ -z "$AUTHFILE" ]; then
877270
    AUTHFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" Xauthority.XXXXXX)
877270
fi
877270
877270
# Start Xvfb.
877270
MCOOKIE=$(mcookie)
877270
877270
if [ -z "$AUTO_DISPLAY" ]; then
877270
  # Old style using a pre-computed SERVERNUM
877270
  XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" \
877270
  2>&1 &
877270
  XVFBPID=$!
877270
else
877270
  # New style using Xvfb to provide a free display
877270
  PIDFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" pid.XXXXXX)
877270
  SERVERNUM=$(XAUTHORITY=$AUTHFILE Xvfb -displayfd 1 $XVFBARGS $LISTENTCP \
877270
  2>"$ERRORFILE" & echo $! > $PIDFILE)
877270
  XVFBPID=$(cat $PIDFILE)
877270
fi
877270
sleep "$STARTWAIT"
877270
877270
XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
877270
add :$SERVERNUM $XAUTHPROTO $MCOOKIE
877270
EOF
877270
877270
# Start the command and save its exit status.
877270
set +e
877270
DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
877270
RETVAL=$?
877270
set -e
877270
877270
# Kill Xvfb now that the command has exited.
877270
kill $XVFBPID
877270
877270
# Clean up.
877270
XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
877270
if [ -n "$XVFB_RUN_TMPDIR" ]; then
877270
    if ! rm -r "$XVFB_RUN_TMPDIR"; then
877270
        error "problem while cleaning up temporary directory"
877270
        exit 5
877270
    fi
877270
fi
877270
877270
# Return the executed command's exit status.
877270
exit $RETVAL
877270
877270
# vim:set ai et sts=4 sw=4 tw=80: