Blame SOURCES/xvfb-run.sh

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