xref: /freebsd/contrib/ncurses/progs/capconvert (revision 21817992b3314c908ab50f0bb88d2ee750b9c4ac)
10e3d5408SPeter Wemm#!/bin/sh
24a1a9510SRong-En Fan##############################################################################
3*21817992SBaptiste Daroussin# Copyright 2019-2021,2022 Thomas E. Dickey                                  #
4e1865124SBaptiste Daroussin# Copyright 1998-2011,2017 Free Software Foundation, Inc.                    #
54a1a9510SRong-En Fan#                                                                            #
64a1a9510SRong-En Fan# Permission is hereby granted, free of charge, to any person obtaining a    #
74a1a9510SRong-En Fan# copy of this software and associated documentation files (the "Software"), #
84a1a9510SRong-En Fan# to deal in the Software without restriction, including without limitation  #
94a1a9510SRong-En Fan# the rights to use, copy, modify, merge, publish, distribute, distribute    #
104a1a9510SRong-En Fan# with modifications, sublicense, and/or sell copies of the Software, and to #
114a1a9510SRong-En Fan# permit persons to whom the Software is furnished to do so, subject to the  #
124a1a9510SRong-En Fan# following conditions:                                                      #
134a1a9510SRong-En Fan#                                                                            #
144a1a9510SRong-En Fan# The above copyright notice and this permission notice shall be included in #
154a1a9510SRong-En Fan# all copies or substantial portions of the Software.                        #
164a1a9510SRong-En Fan#                                                                            #
174a1a9510SRong-En Fan# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
184a1a9510SRong-En Fan# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
194a1a9510SRong-En Fan# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
204a1a9510SRong-En Fan# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
214a1a9510SRong-En Fan# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
224a1a9510SRong-En Fan# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
234a1a9510SRong-En Fan# DEALINGS IN THE SOFTWARE.                                                  #
244a1a9510SRong-En Fan#                                                                            #
254a1a9510SRong-En Fan# Except as contained in this notice, the name(s) of the above copyright     #
264a1a9510SRong-En Fan# holders shall not be used in advertising or otherwise to promote the sale, #
274a1a9510SRong-En Fan# use or other dealings in this Software without prior written               #
284a1a9510SRong-En Fan# authorization.                                                             #
294a1a9510SRong-En Fan##############################################################################
30*21817992SBaptiste Daroussin# $Id: capconvert,v 1.12 2022/07/16 21:00:27 tom Exp $
310e3d5408SPeter Wemm#
320e3d5408SPeter Wemm# capconvert -- automated conversion from termcap to terminfo
330e3d5408SPeter Wemm#
340e3d5408SPeter Wemm
350e3d5408SPeter Wemmecho "This script tries to automatically set you up so that your applications"
360e3d5408SPeter Wemmecho "that now use termcap can use terminfo and the ncurses library."
370e3d5408SPeter Wemmecho ""
380e3d5408SPeter Wemm
390e3d5408SPeter Wemm# Note, except for telling if we're running under xterm we don't use TERM at
400e3d5408SPeter Wemm# all.  This is because BSD users not infrequently have multiple termtypes
410e3d5408SPeter Wemm# selected by conditionals in tset -- unless they're xterm users, in which
420e3d5408SPeter Wemm# case they're on a workstation and probably don't.
430e3d5408SPeter Wemm
440e3d5408SPeter Wemm# Check to make sure TERMINFO is not already defined
450e3d5408SPeter Wemmif test -n "$TERMINFO"
460e3d5408SPeter Wemmthen
470e3d5408SPeter Wemm	echo "TERMINFO is already defined in your environment.  This means"
480e3d5408SPeter Wemm	echo "you already have a local terminfo tree, so you do not need any"
490e3d5408SPeter Wemm	echo "conversion."
50*21817992SBaptiste Daroussin	if test ! -d "$TERMINFO" ; then
510e3d5408SPeter Wemm		echo "Caution: TERMINFO does not point to a directory!"
520e3d5408SPeter Wemm	fi
530e3d5408SPeter Wemm	exit;
540e3d5408SPeter Wemmfi
550e3d5408SPeter Wemm
560e3d5408SPeter Wemm# Check to see if terminfo is present in one of the standard locations.
570e3d5408SPeter Wemmterminfo=no
580e3d5408SPeter Wemmfor p in $TERMINFO \
590e3d5408SPeter Wemm	/usr/lib/terminfo \
600e3d5408SPeter Wemm	/usr/share/lib/terminfo \
610e3d5408SPeter Wemm	/usr/share/terminfo \
620e3d5408SPeter Wemm	/usr/local/lib/terminfo \
630e3d5408SPeter Wemm	/usr/local/share/terminfo
640e3d5408SPeter Wemmdo
65*21817992SBaptiste Daroussin	if test -d "$p" ; then
660e3d5408SPeter Wemm		terminfo=yes
670e3d5408SPeter Wemm		break
680e3d5408SPeter Wemm	fi
690e3d5408SPeter Wemmdone
700e3d5408SPeter Wemm
710e3d5408SPeter Wemmif test $terminfo = yes
720e3d5408SPeter Wemmthen
730e3d5408SPeter Wemm	echo "Your system already has a system-wide terminfo tree."
740e3d5408SPeter Wemm	echo ""
750e3d5408SPeter Wemm	if test -z "$TERMCAP"
760e3d5408SPeter Wemm	then
770e3d5408SPeter Wemm		echo "You have no TERMCAP variable set, so we are done."
780e3d5408SPeter Wemm		# Assumes the terminfo master covers all canned terminal types
790e3d5408SPeter Wemm		exit;
800e3d5408SPeter Wemm	fi
8173f0a83dSXin LI	case $TERM in
8273f0a83dSXin LI	xterm | xterm-*)
830e3d5408SPeter Wemm		echo "You are running xterm, which usually sets TERMCAP itself."
840e3d5408SPeter Wemm		echo "We can ignore this, because terminfo knows about xterm."
850e3d5408SPeter Wemm		echo "So you will just use the system-wide terminfo tree."
8673f0a83dSXin LI		exit
8773f0a83dSXin LI		;;
8873f0a83dSXin LI	*)
890e3d5408SPeter Wemm		echo "We will have to make a local one for you anyway, to capture the effect"
900e3d5408SPeter Wemm		echo "of your TERMCAP variable."
9173f0a83dSXin LI		;;
9273f0a83dSXin LI	esac
930e3d5408SPeter Wemmelse
940e3d5408SPeter Wemm	echo "No system-wide terminfo tree.  We will make you a local one."
950e3d5408SPeter Wemmfi
960e3d5408SPeter Wemmecho "";
970e3d5408SPeter Wemm
98*21817992SBaptiste Daroussin# Check if test -x works (it is not portable, but useful)
990e3d5408SPeter WemmOPT="-x"
1000e3d5408SPeter WemmTMP=test$$; touch $TMP && chmod 755 $TMP
1010e3d5408SPeter Wemmif test $OPT $TMP ; then
1020e3d5408SPeter Wemm	chmod 644 $TMP
1030e3d5408SPeter Wemm	test $OPT $TMP && OPT="-f"
1040e3d5408SPeter Wemmelse
1050e3d5408SPeter Wemm	OPT="-f"
1060e3d5408SPeter Wemmfi
1070e3d5408SPeter Wemmrm -f $TMP
1080e3d5408SPeter Wemm
1090e3d5408SPeter Wemm# First step -- go find tic
1100e3d5408SPeter WemmTIC=
1110e3d5408SPeter WemmIFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
1120e3d5408SPeter Wemmfor x in $PATH .
1130e3d5408SPeter Wemmdo
114*21817992SBaptiste Daroussin	if test "$OPT" "$x"/tic
1150e3d5408SPeter Wemm	then
1160e3d5408SPeter Wemm		TIC=$x/tic
1170e3d5408SPeter Wemm		break
1180e3d5408SPeter Wemm	fi
1190e3d5408SPeter Wemmdone
120*21817992SBaptiste DaroussinIFS="$save_ifs"
1210e3d5408SPeter Wemm
1220e3d5408SPeter Wemmif test -n "$TIC"
1230e3d5408SPeter Wemmthen
1240e3d5408SPeter Wemm	echo "I see tic at $TIC."
1250e3d5408SPeter Wemm	case $TIC in # (vi
1260e3d5408SPeter Wemm	./tic)
1270e3d5408SPeter Wemm		if test $OPT ../misc/shlib ; then
1280e3d5408SPeter Wemm			TIC="../misc/shlib $TIC"
1290e3d5408SPeter Wemm		fi
1300e3d5408SPeter Wemm		;;
1310e3d5408SPeter Wemm	esac
1320e3d5408SPeter Wemmelse
1330e3d5408SPeter Wemm	echo "You do not have tic installed anywhere I can see, please fix that."
1340e3d5408SPeter Wemm	exit;
1350e3d5408SPeter Wemmfi
1360e3d5408SPeter Wemmecho "";
1370e3d5408SPeter Wemm
1380e3d5408SPeter Wemm# We have tic.  Either there's no system terminfo tree or there is one but
1390e3d5408SPeter Wemm# the user has a TERMCAP variable that may modify a stock description.
1400e3d5408SPeter Wemm#
1410e3d5408SPeter Wemm
1420e3d5408SPeter Wemm# Make the user a terminfo directory
143*21817992SBaptiste Daroussinif test -d "$HOME"/.terminfo
1440e3d5408SPeter Wemmthen
1450e3d5408SPeter Wemm	echo "It appears you already have a private terminfo directory"
1460e3d5408SPeter Wemm	echo "at $HOME/.terminfo; this seems odd, because TERMINFO"
1470e3d5408SPeter Wemm	echo "is not defined.  I am not going to second-guess this -- if you"
1480e3d5408SPeter Wemm	echo "really want me to try auto-configuring for you, remove or"
1490e3d5408SPeter Wemm	echo "rename $HOME/terminfo and run me again."
1500e3d5408SPeter Wemm	exit;
1510e3d5408SPeter Wemmelse
1520e3d5408SPeter Wemm	echo "I am creating your private terminfo directory at $HOME/.terminfo"
153*21817992SBaptiste Daroussin	mkdir "$HOME"/.terminfo
1540e3d5408SPeter Wemm	# Ensure that that's where tic's compilation results.
1550e3d5408SPeter Wemm	# This isn't strictly necessary with a 1.9.7 or later tic.
1560e3d5408SPeter Wemm	TERMINFO="$HOME/.terminfo"; export TERMINFO
1570e3d5408SPeter Wemmfi
1580e3d5408SPeter Wemmecho "";
1590e3d5408SPeter Wemm
1600e3d5408SPeter Wemm# Find a terminfo source to work from
1610e3d5408SPeter Wemmif test -f ../misc/terminfo.src
1620e3d5408SPeter Wemmthen
1630e3d5408SPeter Wemm	echo "I see the terminfo master source is handy; I will use that."
1640e3d5408SPeter Wemm	master=../misc/terminfo.src
1650e3d5408SPeter Wemmelse
1660e3d5408SPeter Wemm	# Ooops...looks like we're running from somewhere other than the
1670e3d5408SPeter Wemm	# progs directory of an ncurses source tree.
168*21817992SBaptiste Daroussin	master=`find "$HOME" -name "*terminfo.src" -print`
169*21817992SBaptiste Daroussin	mcount=`find "$HOME" -name "*terminfo.src" | wc -l`
1700e3d5408SPeter Wemm	case $mcount in
1710e3d5408SPeter Wemm	0)
1720e3d5408SPeter Wemm		echo "I can not find a terminfo source file anywhere under your home directory."
1730e3d5408SPeter Wemm		echo "There should be a file called terminfo.src somewhere in your"
1740e3d5408SPeter Wemm		echo "ncurses distribution; please put it in your home directotry"
1750e3d5408SPeter Wemm		echo "and run me again (it does not have to live there permanently)."
1760e3d5408SPeter Wemm		exit;
1770e3d5408SPeter Wemm	;;
1780e3d5408SPeter Wemm	1)
1790e3d5408SPeter Wemm		echo "I see a file called $master."
1800e3d5408SPeter Wemm		echo "I am going to assume this is the terminfo source included with"
1810e3d5408SPeter Wemm		echo "the ncurses distribution.  If this assumption is wrong, please"
1820e3d5408SPeter Wemm		echo "interrupt me now!  OK to continue?"
183aae38d10SBaptiste Daroussin		read answer;
1840e3d5408SPeter Wemm	;;
1850e3d5408SPeter Wemm	2)
1860e3d5408SPeter Wemm		echo "I see more than one possible terminfo source.  Here they are:"
187*21817992SBaptiste Daroussin		echo "$master" | sed "/^/s//	/";
1880e3d5408SPeter Wemm		while :
1890e3d5408SPeter Wemm		do
1900e3d5408SPeter Wemm			echo "Please tell me which one to use:"
1910e3d5408SPeter Wemm			read master;
192*21817992SBaptiste Daroussin			if test -f "$master"
1930e3d5408SPeter Wemm			then
1940e3d5408SPeter Wemm				break
1950e3d5408SPeter Wemm			else
1960e3d5408SPeter Wemm				echo "That file does not exist. Try again?";
1970e3d5408SPeter Wemm			fi
1980e3d5408SPeter Wemm		done
1990e3d5408SPeter Wemm	;;
2000e3d5408SPeter Wemm	esac
2010e3d5408SPeter Wemmfi
2020e3d5408SPeter Wemmecho "";
2030e3d5408SPeter Wemm
2040e3d5408SPeter Wemm# Now that we have a master, compile it into the local tree
2050e3d5408SPeter Wemmecho "OK, now I will make your private terminfo tree.  This may take a bit..."
2060e3d5408SPeter Wemm#
2070e3d5408SPeter Wemm# Kluge alert: we compile terminfo.src in two pieces because a lot of machines
2080e3d5408SPeter Wemm# with < 16MB RAM choke on tic's core-hog habits.
209*21817992SBaptiste Daroussintrap 'rm -f tsplit$$.*; exit 1' 1 2 3 15
210*21817992SBaptiste Daroussintrap 'rm -f tsplit$$.*' 0
211*21817992SBaptiste Daroussinsed -n "$master" \
2120e3d5408SPeter Wemm	-e '1,/SPLIT HERE/w 'tsplit$$.01 \
2130e3d5408SPeter Wemm	-e '/SPLIT HERE/,$w 'tsplit$$.02 \
2140e3d5408SPeter Wemm	2>/dev/null
215*21817992SBaptiste Daroussinfor x in tsplit$$.*; do eval $TIC "$x"; done
2160e3d5408SPeter Wemmrm tsplit$$.*
217aae38d10SBaptiste Daroussintrap EXIT INT QUIT TERM HUP
2180e3d5408SPeter Wemm#
2190e3d5408SPeter Wemmecho "You now have a private tree under $HOME/.terminfo;"
2200e3d5408SPeter Wemmecho "the ncurses library will automatically read from it,"
2210e3d5408SPeter Wemmecho "and ncurses tic will automatically compile entries to it."
2220e3d5408SPeter Wemm
2230e3d5408SPeter Wemm# We're done unless user has a .termcap file or equivalent named by TERMCAP
2240e3d5408SPeter Wemmif test -z "$TERMCAP"
2250e3d5408SPeter Wemmthen
2260e3d5408SPeter Wemm	echo "You have no TERMCAP set, so we are done."
2270e3d5408SPeter Wemmfi
2280e3d5408SPeter Wemm
2290e3d5408SPeter Wemm# OK, here comes the nasty case...user has a TERMCAP.  Instead of
2300e3d5408SPeter Wemm# trying to follow all the convolutions of the relationship between
231*21817992SBaptiste Daroussin# TERM and TERMCAP (partly because it is too painful, and partly because
2320e3d5408SPeter Wemm# we don't actually know what TERM will be nor even if it always has
2330e3d5408SPeter Wemm# the same value for this user) we do the following three steps...
2340e3d5408SPeter Wemm
235*21817992SBaptiste Daroussinif test -f "$HOME"/.termcap
2360e3d5408SPeter Wemmthen
237*21817992SBaptiste Daroussin	echo "I see you have a \$HOME/.termcap file.  I will compile that."
238*21817992SBaptiste Daroussin	eval $TIC "$HOME"/.termcap
2390e3d5408SPeter Wemm	echo "Done."
2400e3d5408SPeter Wemm	echo "Note that editing $HOME/.termcap will no longer change the data curses sees."
2410e3d5408SPeter Wemmelif test -f "$TERMCAP"
2420e3d5408SPeter Wemmthen
2430e3d5408SPeter Wemm	echo "Your TERMCAP names the file $TERMCAP.  I will compile that."
244*21817992SBaptiste Daroussin	eval $TIC "$TERMCAP"
2450e3d5408SPeter Wemm	echo "Done."
2460e3d5408SPeter Wemm	echo "Note that editing $TERMCAP will no longer change the data curses sees."
2470e3d5408SPeter Wemmelse
2480e3d5408SPeter Wemm	echo "Your TERMCAP value appears to be an entry in termcap format."
2490e3d5408SPeter Wemm	echo "I will compile it."
250*21817992SBaptiste Daroussin	echo "$TERMCAP" >myterm$$
2510e3d5408SPeter Wemm	eval $TIC myterm$$
2520e3d5408SPeter Wemm	rm myterm$$
2530e3d5408SPeter Wemm	echo "Done."
2540e3d5408SPeter Wemm	echo "Note that editing TERMCAP will no longer change the data curses sees."
2550e3d5408SPeter Wemmfi
256aae38d10SBaptiste Daroussinecho "To do that, decompile the terminal description you want with infocmp(1),"
2570e3d5408SPeter Wemmecho "edit to taste, and recompile using tic(1)."
2580e3d5408SPeter Wemm
2590e3d5408SPeter Wemm# capconvert ends here
2600e3d5408SPeter Wemm
261