title.sh revision d522f475
1#!/bin/sh
2# $XFree86: xc/programs/xterm/vttests/title.sh,v 1.6 2003/05/19 00:52:30 dickey Exp $
3#
4# -- Thomas Dickey (1999/3/27)
5# Obtain the current title of the window, set up a simple clock which runs
6# until this script is interrupted, then restore the title.
7
8ESC=""
9CMD='echo'
10OPT='-n'
11SUF=''
12TMP=/tmp/xterm$$
13eval '$CMD $OPT >$TMP || echo fail >$TMP' 2>/dev/null
14( test ! -f $TMP || test -s $TMP ) &&
15for verb in printf print ; do
16    rm -f $TMP
17    eval '$verb "\c" >$TMP || echo fail >$TMP' 2>/dev/null
18    if test -f $TMP ; then
19	if test ! -s $TMP ; then
20	    CMD="$verb"
21	    OPT=
22	    SUF='\c'
23	    break
24	fi
25    fi
26done
27rm -f $TMP
28
29exec </dev/tty
30old=`stty -g`
31stty raw -echo min 0  time 5
32
33$CMD $OPT "${ESC}[21t${SUF}" > /dev/tty
34read original
35
36stty $old
37
38# We actually get this terminated by an <esc>backslash, but the backslash
39# is lost.  We may lose doublequote characters when restoring the title,
40# depending on the shell.
41original=`echo "$original" |sed -e 's/^...//' -e 's/.$//'`
42original=${ESC}]2\;"${original}"${SUF}
43
44if ( trap "echo exit" EXIT 2>/dev/null ) >/dev/null
45then
46    trap '$CMD $OPT "$original" >/dev/tty; exit' EXIT HUP INT TRAP TERM
47else
48    trap '$CMD $OPT "$original" >/dev/tty; exit' 0    1   2   5    15
49fi
50
51while true
52do
53	sleep 1
54	$CMD $OPT "${ESC}]2;`date`" >/dev/tty
55done
56