1d522f475Smrg#!/bin/sh
204b94745Smrg# $XTermId: title.sh,v 1.26 2022/04/25 08:19:38 tom Exp $
320d2c4d2Smrg# -----------------------------------------------------------------------------
420d2c4d2Smrg# this file is part of xterm
5d522f475Smrg#
64419d26bSmrg# Copyright 1999-2021,2022 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=''
425307cd1aSmrg: "${TMPDIR=/tmp}"
435307cd1aSmrgTMP=`(mktemp "$TMPDIR/xterm.XXXXXXXX") 2>/dev/null` || TMP="$TMPDIR/xterm$$"
44d522f475Smrgeval '$CMD $OPT >$TMP || echo fail >$TMP' 2>/dev/null
455307cd1aSmrg{ test ! -f "$TMP" || test -s "$TMP"; } &&
46ad37e533Smrgfor verb in "printf" "print" ; do
475307cd1aSmrg    rm -f "$TMP"
48d522f475Smrg    eval '$verb "\c" >$TMP || echo fail >$TMP' 2>/dev/null
495307cd1aSmrg    if test -f "$TMP" ; then
505307cd1aSmrg	if test ! -s "$TMP" ; then
51d522f475Smrg	    CMD="$verb"
52d522f475Smrg	    OPT=
53d522f475Smrg	    SUF='\c'
54d522f475Smrg	    break
55d522f475Smrg	fi
56d522f475Smrg    fi
57d522f475Smrgdone
585307cd1aSmrgrm -f "$TMP"
59d522f475Smrg
60d522f475Smrgexec </dev/tty
61d522f475Smrgold=`stty -g`
62d522f475Smrgstty raw -echo min 0  time 5
63d522f475Smrg
64ad37e533Smrg$CMD $OPT "${CSI}21t${SUF}" > /dev/tty
655307cd1aSmrgread -r original
66d522f475Smrg
67d522f475Smrgstty $old
68d522f475Smrg
69d522f475Smrg# We actually get this terminated by an <esc>backslash, but the backslash
70d522f475Smrg# is lost.  We may lose doublequote characters when restoring the title,
71d522f475Smrg# depending on the shell.
72d522f475Smrgoriginal=`echo "$original" |sed -e 's/^...//' -e 's/.$//'`
73d522f475Smrgoriginal=${ESC}]2\;"${original}"${SUF}
74d522f475Smrg
754419d26bSmrgtrap '$CMD $OPT "$original" >/dev/tty; exit 1' 1 2 3 15
764419d26bSmrgtrap '$CMD $OPT "$original" >/dev/tty' 0
77d522f475Smrg
78d522f475Smrgwhile true
79d522f475Smrgdo
80d522f475Smrg	sleep 1
81d522f475Smrg	$CMD $OPT "${ESC}]2;`date`" >/dev/tty
82d522f475Smrgdone
83