title.sh revision 20d2c4d2
1d522f475Smrg#!/bin/sh
220d2c4d2Smrg# $XTermId: title.sh,v 1.17 2003/05/19 00:52:30 tom Exp $
320d2c4d2Smrg# -----------------------------------------------------------------------------
420d2c4d2Smrg# this file is part of xterm
5d522f475Smrg#
620d2c4d2Smrg# Copyright 1999-2002,2003 by Thomas E. Dickey
720d2c4d2Smrg# 
820d2c4d2Smrg#                         All Rights Reserved
920d2c4d2Smrg# 
1020d2c4d2Smrg# Permission is hereby granted, free of charge, to any person obtaining a
1120d2c4d2Smrg# copy of this software and associated documentation files (the
1220d2c4d2Smrg# "Software"), to deal in the Software without restriction, including
1320d2c4d2Smrg# without limitation the rights to use, copy, modify, merge, publish,
1420d2c4d2Smrg# distribute, sublicense, and/or sell copies of the Software, and to
1520d2c4d2Smrg# permit persons to whom the Software is furnished to do so, subject to
1620d2c4d2Smrg# the following conditions:
1720d2c4d2Smrg# 
1820d2c4d2Smrg# The above copyright notice and this permission notice shall be included
1920d2c4d2Smrg# in all copies or substantial portions of the Software.
2020d2c4d2Smrg# 
2120d2c4d2Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
2220d2c4d2Smrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2320d2c4d2Smrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2420d2c4d2Smrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2520d2c4d2Smrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2620d2c4d2Smrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2720d2c4d2Smrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2820d2c4d2Smrg# 
2920d2c4d2Smrg# Except as contained in this notice, the name(s) of the above copyright
3020d2c4d2Smrg# holders shall not be used in advertising or otherwise to promote the
3120d2c4d2Smrg# sale, use or other dealings in this Software without prior written
3220d2c4d2Smrg# authorization.
3320d2c4d2Smrg# -----------------------------------------------------------------------------
34d522f475Smrg# Obtain the current title of the window, set up a simple clock which runs
35d522f475Smrg# until this script is interrupted, then restore the title.
36d522f475Smrg
37d522f475SmrgESC=""
38d522f475SmrgCMD='echo'
39d522f475SmrgOPT='-n'
40d522f475SmrgSUF=''
41d522f475SmrgTMP=/tmp/xterm$$
42d522f475Smrgeval '$CMD $OPT >$TMP || echo fail >$TMP' 2>/dev/null
43d522f475Smrg( test ! -f $TMP || test -s $TMP ) &&
44d522f475Smrgfor verb in printf print ; do
45d522f475Smrg    rm -f $TMP
46d522f475Smrg    eval '$verb "\c" >$TMP || echo fail >$TMP' 2>/dev/null
47d522f475Smrg    if test -f $TMP ; then
48d522f475Smrg	if test ! -s $TMP ; then
49d522f475Smrg	    CMD="$verb"
50d522f475Smrg	    OPT=
51d522f475Smrg	    SUF='\c'
52d522f475Smrg	    break
53d522f475Smrg	fi
54d522f475Smrg    fi
55d522f475Smrgdone
56d522f475Smrgrm -f $TMP
57d522f475Smrg
58d522f475Smrgexec </dev/tty
59d522f475Smrgold=`stty -g`
60d522f475Smrgstty raw -echo min 0  time 5
61d522f475Smrg
62d522f475Smrg$CMD $OPT "${ESC}[21t${SUF}" > /dev/tty
63d522f475Smrgread original
64d522f475Smrg
65d522f475Smrgstty $old
66d522f475Smrg
67d522f475Smrg# We actually get this terminated by an <esc>backslash, but the backslash
68d522f475Smrg# is lost.  We may lose doublequote characters when restoring the title,
69d522f475Smrg# depending on the shell.
70d522f475Smrgoriginal=`echo "$original" |sed -e 's/^...//' -e 's/.$//'`
71d522f475Smrgoriginal=${ESC}]2\;"${original}"${SUF}
72d522f475Smrg
73d522f475Smrgif ( trap "echo exit" EXIT 2>/dev/null ) >/dev/null
74d522f475Smrgthen
75d522f475Smrg    trap '$CMD $OPT "$original" >/dev/tty; exit' EXIT HUP INT TRAP TERM
76d522f475Smrgelse
77d522f475Smrg    trap '$CMD $OPT "$original" >/dev/tty; exit' 0    1   2   5    15
78d522f475Smrgfi
79d522f475Smrg
80d522f475Smrgwhile true
81d522f475Smrgdo
82d522f475Smrg	sleep 1
83d522f475Smrg	$CMD $OPT "${ESC}]2;`date`" >/dev/tty
84d522f475Smrgdone
85