source: git/xvfb-run @ a4cd3ac0

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since a4cd3ac0 was a4cd3ac0, checked in by Olly Betts <olly@…>, 19 years ago

Add xvfb-run script since there was a bug rendering it unusable in some
older versions, and it's a Debian thing so not everyone will have it.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2858 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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