title.sh revision ad37e533
1d522f475Smrg#!/bin/sh
2ad37e533Smrg# $XTermId: title.sh,v 1.22 2021/03/03 01:16:53 tom Exp $
320d2c4d2Smrg# -----------------------------------------------------------------------------
420d2c4d2Smrg# this file is part of xterm
5d522f475Smrg#
6ad37e533Smrg# Copyright 1999-2021,2021 by Thomas E. Dickey
7ad37e533Smrg#
820d2c4d2Smrg#                         All Rights Reserved
9ad37e533Smrg#
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:
17ad37e533Smrg#
1820d2c4d2Smrg# The above copyright notice and this permission notice shall be included
1920d2c4d2Smrg# in all copies or substantial portions of the Software.
20ad37e533Smrg#
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.
28ad37e533Smrg#
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=""
38ad37e533SmrgCSI="${ESC}["
390bd37d32SmrgCMD='/bin/echo'
40d522f475SmrgOPT='-n'
41d522f475SmrgSUF=''
42ad37e533SmrgTMP=`(mktemp) 2>/dev/null` || TMP=/tmp/xterm$$
43d522f475Smrgeval '$CMD $OPT >$TMP || echo fail >$TMP' 2>/dev/null
44ad37e533Smrg{ test ! -f $TMP || test -s $TMP; } &&
45ad37e533Smrgfor verb in "printf" "print" ; do
46d522f475Smrg    rm -f $TMP
47d522f475Smrg    eval '$verb "\c" >$TMP || echo fail >$TMP' 2>/dev/null
48d522f475Smrg    if test -f $TMP ; then
49d522f475Smrg	if test ! -s $TMP ; then
50d522f475Smrg	    CMD="$verb"
51d522f475Smrg	    OPT=
52d522f475Smrg	    SUF='\c'
53d522f475Smrg	    break
54d522f475Smrg	fi
55d522f475Smrg    fi
56d522f475Smrgdone
57d522f475Smrgrm -f $TMP
58d522f475Smrg
59d522f475Smrgexec </dev/tty
60d522f475Smrgold=`stty -g`
61d522f475Smrgstty raw -echo min 0  time 5
62d522f475Smrg
63ad37e533Smrg$CMD $OPT "${CSI}21t${SUF}" > /dev/tty
64d522f475Smrgread original
65d522f475Smrg
66d522f475Smrgstty $old
67d522f475Smrg
68d522f475Smrg# We actually get this terminated by an <esc>backslash, but the backslash
69d522f475Smrg# is lost.  We may lose doublequote characters when restoring the title,
70d522f475Smrg# depending on the shell.
71d522f475Smrgoriginal=`echo "$original" |sed -e 's/^...//' -e 's/.$//'`
72d522f475Smrgoriginal=${ESC}]2\;"${original}"${SUF}
73d522f475Smrg
74d522f475Smrgif ( trap "echo exit" EXIT 2>/dev/null ) >/dev/null
75d522f475Smrgthen
76ad37e533Smrg    trap '$CMD $OPT "$original" >/dev/tty; exit' EXIT HUP INT QUIT TERM
77d522f475Smrgelse
78ad37e533Smrg    trap '$CMD $OPT "$original" >/dev/tty; exit' 0    1   2   3    15
79d522f475Smrgfi
80d522f475Smrg
81d522f475Smrgwhile true
82d522f475Smrgdo
83d522f475Smrg	sleep 1
84d522f475Smrg	$CMD $OPT "${ESC}]2;`date`" >/dev/tty
85d522f475Smrgdone
86