xref: /openbsd/usr.bin/tic/capconvert (revision c7ef0cfc)
1*c7ef0cfcSnicm#!/bin/sh
2*c7ef0cfcSnicm##############################################################################
3*c7ef0cfcSnicm# Copyright 2019-2021,2022 Thomas E. Dickey                                  #
4*c7ef0cfcSnicm# Copyright 1998-2011,2017 Free Software Foundation, Inc.                    #
5*c7ef0cfcSnicm#                                                                            #
6*c7ef0cfcSnicm# Permission is hereby granted, free of charge, to any person obtaining a    #
7*c7ef0cfcSnicm# copy of this software and associated documentation files (the "Software"), #
8*c7ef0cfcSnicm# to deal in the Software without restriction, including without limitation  #
9*c7ef0cfcSnicm# the rights to use, copy, modify, merge, publish, distribute, distribute    #
10*c7ef0cfcSnicm# with modifications, sublicense, and/or sell copies of the Software, and to #
11*c7ef0cfcSnicm# permit persons to whom the Software is furnished to do so, subject to the  #
12*c7ef0cfcSnicm# following conditions:                                                      #
13*c7ef0cfcSnicm#                                                                            #
14*c7ef0cfcSnicm# The above copyright notice and this permission notice shall be included in #
15*c7ef0cfcSnicm# all copies or substantial portions of the Software.                        #
16*c7ef0cfcSnicm#                                                                            #
17*c7ef0cfcSnicm# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18*c7ef0cfcSnicm# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
19*c7ef0cfcSnicm# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
20*c7ef0cfcSnicm# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
21*c7ef0cfcSnicm# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
22*c7ef0cfcSnicm# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
23*c7ef0cfcSnicm# DEALINGS IN THE SOFTWARE.                                                  #
24*c7ef0cfcSnicm#                                                                            #
25*c7ef0cfcSnicm# Except as contained in this notice, the name(s) of the above copyright     #
26*c7ef0cfcSnicm# holders shall not be used in advertising or otherwise to promote the sale, #
27*c7ef0cfcSnicm# use or other dealings in this Software without prior written               #
28*c7ef0cfcSnicm# authorization.                                                             #
29*c7ef0cfcSnicm##############################################################################
30*c7ef0cfcSnicm# $Id: capconvert,v 1.1 2023/10/17 09:52:10 nicm Exp $
31*c7ef0cfcSnicm#
32*c7ef0cfcSnicm# capconvert -- automated conversion from termcap to terminfo
33*c7ef0cfcSnicm#
34*c7ef0cfcSnicm
35*c7ef0cfcSnicmecho "This script tries to automatically set you up so that your applications"
36*c7ef0cfcSnicmecho "that now use termcap can use terminfo and the ncurses library."
37*c7ef0cfcSnicmecho ""
38*c7ef0cfcSnicm
39*c7ef0cfcSnicm# Note, except for telling if we're running under xterm we don't use TERM at
40*c7ef0cfcSnicm# all.  This is because BSD users not infrequently have multiple termtypes
41*c7ef0cfcSnicm# selected by conditionals in tset -- unless they're xterm users, in which
42*c7ef0cfcSnicm# case they're on a workstation and probably don't.
43*c7ef0cfcSnicm
44*c7ef0cfcSnicm# Check to make sure TERMINFO is not already defined
45*c7ef0cfcSnicmif test -n "$TERMINFO"
46*c7ef0cfcSnicmthen
47*c7ef0cfcSnicm	echo "TERMINFO is already defined in your environment.  This means"
48*c7ef0cfcSnicm	echo "you already have a local terminfo tree, so you do not need any"
49*c7ef0cfcSnicm	echo "conversion."
50*c7ef0cfcSnicm	if test ! -d "$TERMINFO" ; then
51*c7ef0cfcSnicm		echo "Caution: TERMINFO does not point to a directory!"
52*c7ef0cfcSnicm	fi
53*c7ef0cfcSnicm	exit;
54*c7ef0cfcSnicmfi
55*c7ef0cfcSnicm
56*c7ef0cfcSnicm# Check to see if terminfo is present in one of the standard locations.
57*c7ef0cfcSnicmterminfo=no
58*c7ef0cfcSnicmfor p in $TERMINFO \
59*c7ef0cfcSnicm	/usr/lib/terminfo \
60*c7ef0cfcSnicm	/usr/share/lib/terminfo \
61*c7ef0cfcSnicm	/usr/share/terminfo \
62*c7ef0cfcSnicm	/usr/local/lib/terminfo \
63*c7ef0cfcSnicm	/usr/local/share/terminfo
64*c7ef0cfcSnicmdo
65*c7ef0cfcSnicm	if test -d "$p" ; then
66*c7ef0cfcSnicm		terminfo=yes
67*c7ef0cfcSnicm		break
68*c7ef0cfcSnicm	fi
69*c7ef0cfcSnicmdone
70*c7ef0cfcSnicm
71*c7ef0cfcSnicmif test $terminfo = yes
72*c7ef0cfcSnicmthen
73*c7ef0cfcSnicm	echo "Your system already has a system-wide terminfo tree."
74*c7ef0cfcSnicm	echo ""
75*c7ef0cfcSnicm	if test -z "$TERMCAP"
76*c7ef0cfcSnicm	then
77*c7ef0cfcSnicm		echo "You have no TERMCAP variable set, so we are done."
78*c7ef0cfcSnicm		# Assumes the terminfo master covers all canned terminal types
79*c7ef0cfcSnicm		exit;
80*c7ef0cfcSnicm	fi
81*c7ef0cfcSnicm	case $TERM in
82*c7ef0cfcSnicm	xterm | xterm-*)
83*c7ef0cfcSnicm		echo "You are running xterm, which usually sets TERMCAP itself."
84*c7ef0cfcSnicm		echo "We can ignore this, because terminfo knows about xterm."
85*c7ef0cfcSnicm		echo "So you will just use the system-wide terminfo tree."
86*c7ef0cfcSnicm		exit
87*c7ef0cfcSnicm		;;
88*c7ef0cfcSnicm	*)
89*c7ef0cfcSnicm		echo "We will have to make a local one for you anyway, to capture the effect"
90*c7ef0cfcSnicm		echo "of your TERMCAP variable."
91*c7ef0cfcSnicm		;;
92*c7ef0cfcSnicm	esac
93*c7ef0cfcSnicmelse
94*c7ef0cfcSnicm	echo "No system-wide terminfo tree.  We will make you a local one."
95*c7ef0cfcSnicmfi
96*c7ef0cfcSnicmecho "";
97*c7ef0cfcSnicm
98*c7ef0cfcSnicm# Check if test -x works (it is not portable, but useful)
99*c7ef0cfcSnicmOPT="-x"
100*c7ef0cfcSnicmTMP=test$$; touch $TMP && chmod 755 $TMP
101*c7ef0cfcSnicmif test $OPT $TMP ; then
102*c7ef0cfcSnicm	chmod 644 $TMP
103*c7ef0cfcSnicm	test $OPT $TMP && OPT="-f"
104*c7ef0cfcSnicmelse
105*c7ef0cfcSnicm	OPT="-f"
106*c7ef0cfcSnicmfi
107*c7ef0cfcSnicmrm -f $TMP
108*c7ef0cfcSnicm
109*c7ef0cfcSnicm# First step -- go find tic
110*c7ef0cfcSnicmTIC=
111*c7ef0cfcSnicmIFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
112*c7ef0cfcSnicmfor x in $PATH .
113*c7ef0cfcSnicmdo
114*c7ef0cfcSnicm	if test "$OPT" "$x"/tic
115*c7ef0cfcSnicm	then
116*c7ef0cfcSnicm		TIC=$x/tic
117*c7ef0cfcSnicm		break
118*c7ef0cfcSnicm	fi
119*c7ef0cfcSnicmdone
120*c7ef0cfcSnicmIFS="$save_ifs"
121*c7ef0cfcSnicm
122*c7ef0cfcSnicmif test -n "$TIC"
123*c7ef0cfcSnicmthen
124*c7ef0cfcSnicm	echo "I see tic at $TIC."
125*c7ef0cfcSnicm	case $TIC in # (vi
126*c7ef0cfcSnicm	./tic)
127*c7ef0cfcSnicm		if test $OPT ../misc/shlib ; then
128*c7ef0cfcSnicm			TIC="../misc/shlib $TIC"
129*c7ef0cfcSnicm		fi
130*c7ef0cfcSnicm		;;
131*c7ef0cfcSnicm	esac
132*c7ef0cfcSnicmelse
133*c7ef0cfcSnicm	echo "You do not have tic installed anywhere I can see, please fix that."
134*c7ef0cfcSnicm	exit;
135*c7ef0cfcSnicmfi
136*c7ef0cfcSnicmecho "";
137*c7ef0cfcSnicm
138*c7ef0cfcSnicm# We have tic.  Either there's no system terminfo tree or there is one but
139*c7ef0cfcSnicm# the user has a TERMCAP variable that may modify a stock description.
140*c7ef0cfcSnicm#
141*c7ef0cfcSnicm
142*c7ef0cfcSnicm# Make the user a terminfo directory
143*c7ef0cfcSnicmif test -d "$HOME"/.terminfo
144*c7ef0cfcSnicmthen
145*c7ef0cfcSnicm	echo "It appears you already have a private terminfo directory"
146*c7ef0cfcSnicm	echo "at $HOME/.terminfo; this seems odd, because TERMINFO"
147*c7ef0cfcSnicm	echo "is not defined.  I am not going to second-guess this -- if you"
148*c7ef0cfcSnicm	echo "really want me to try auto-configuring for you, remove or"
149*c7ef0cfcSnicm	echo "rename $HOME/terminfo and run me again."
150*c7ef0cfcSnicm	exit;
151*c7ef0cfcSnicmelse
152*c7ef0cfcSnicm	echo "I am creating your private terminfo directory at $HOME/.terminfo"
153*c7ef0cfcSnicm	mkdir "$HOME"/.terminfo
154*c7ef0cfcSnicm	# Ensure that that's where tic's compilation results.
155*c7ef0cfcSnicm	# This isn't strictly necessary with a 1.9.7 or later tic.
156*c7ef0cfcSnicm	TERMINFO="$HOME/.terminfo"; export TERMINFO
157*c7ef0cfcSnicmfi
158*c7ef0cfcSnicmecho "";
159*c7ef0cfcSnicm
160*c7ef0cfcSnicm# Find a terminfo source to work from
161*c7ef0cfcSnicmif test -f ../misc/terminfo.src
162*c7ef0cfcSnicmthen
163*c7ef0cfcSnicm	echo "I see the terminfo master source is handy; I will use that."
164*c7ef0cfcSnicm	master=../misc/terminfo.src
165*c7ef0cfcSnicmelse
166*c7ef0cfcSnicm	# Ooops...looks like we're running from somewhere other than the
167*c7ef0cfcSnicm	# progs directory of an ncurses source tree.
168*c7ef0cfcSnicm	master=`find "$HOME" -name "*terminfo.src" -print`
169*c7ef0cfcSnicm	mcount=`find "$HOME" -name "*terminfo.src" | wc -l`
170*c7ef0cfcSnicm	case $mcount in
171*c7ef0cfcSnicm	0)
172*c7ef0cfcSnicm		echo "I can not find a terminfo source file anywhere under your home directory."
173*c7ef0cfcSnicm		echo "There should be a file called terminfo.src somewhere in your"
174*c7ef0cfcSnicm		echo "ncurses distribution; please put it in your home directotry"
175*c7ef0cfcSnicm		echo "and run me again (it does not have to live there permanently)."
176*c7ef0cfcSnicm		exit;
177*c7ef0cfcSnicm	;;
178*c7ef0cfcSnicm	1)
179*c7ef0cfcSnicm		echo "I see a file called $master."
180*c7ef0cfcSnicm		echo "I am going to assume this is the terminfo source included with"
181*c7ef0cfcSnicm		echo "the ncurses distribution.  If this assumption is wrong, please"
182*c7ef0cfcSnicm		echo "interrupt me now!  OK to continue?"
183*c7ef0cfcSnicm		read answer;
184*c7ef0cfcSnicm	;;
185*c7ef0cfcSnicm	2)
186*c7ef0cfcSnicm		echo "I see more than one possible terminfo source.  Here they are:"
187*c7ef0cfcSnicm		echo "$master" | sed "/^/s//	/";
188*c7ef0cfcSnicm		while :
189*c7ef0cfcSnicm		do
190*c7ef0cfcSnicm			echo "Please tell me which one to use:"
191*c7ef0cfcSnicm			read master;
192*c7ef0cfcSnicm			if test -f "$master"
193*c7ef0cfcSnicm			then
194*c7ef0cfcSnicm				break
195*c7ef0cfcSnicm			else
196*c7ef0cfcSnicm				echo "That file does not exist. Try again?";
197*c7ef0cfcSnicm			fi
198*c7ef0cfcSnicm		done
199*c7ef0cfcSnicm	;;
200*c7ef0cfcSnicm	esac
201*c7ef0cfcSnicmfi
202*c7ef0cfcSnicmecho "";
203*c7ef0cfcSnicm
204*c7ef0cfcSnicm# Now that we have a master, compile it into the local tree
205*c7ef0cfcSnicmecho "OK, now I will make your private terminfo tree.  This may take a bit..."
206*c7ef0cfcSnicm#
207*c7ef0cfcSnicm# Kluge alert: we compile terminfo.src in two pieces because a lot of machines
208*c7ef0cfcSnicm# with < 16MB RAM choke on tic's core-hog habits.
209*c7ef0cfcSnicmtrap 'rm -f tsplit$$.*; exit 1' 1 2 3 15
210*c7ef0cfcSnicmtrap 'rm -f tsplit$$.*' 0
211*c7ef0cfcSnicmsed -n "$master" \
212*c7ef0cfcSnicm	-e '1,/SPLIT HERE/w 'tsplit$$.01 \
213*c7ef0cfcSnicm	-e '/SPLIT HERE/,$w 'tsplit$$.02 \
214*c7ef0cfcSnicm	2>/dev/null
215*c7ef0cfcSnicmfor x in tsplit$$.*; do eval $TIC "$x"; done
216*c7ef0cfcSnicmrm tsplit$$.*
217*c7ef0cfcSnicmtrap EXIT INT QUIT TERM HUP
218*c7ef0cfcSnicm#
219*c7ef0cfcSnicmecho "You now have a private tree under $HOME/.terminfo;"
220*c7ef0cfcSnicmecho "the ncurses library will automatically read from it,"
221*c7ef0cfcSnicmecho "and ncurses tic will automatically compile entries to it."
222*c7ef0cfcSnicm
223*c7ef0cfcSnicm# We're done unless user has a .termcap file or equivalent named by TERMCAP
224*c7ef0cfcSnicmif test -z "$TERMCAP"
225*c7ef0cfcSnicmthen
226*c7ef0cfcSnicm	echo "You have no TERMCAP set, so we are done."
227*c7ef0cfcSnicmfi
228*c7ef0cfcSnicm
229*c7ef0cfcSnicm# OK, here comes the nasty case...user has a TERMCAP.  Instead of
230*c7ef0cfcSnicm# trying to follow all the convolutions of the relationship between
231*c7ef0cfcSnicm# TERM and TERMCAP (partly because it is too painful, and partly because
232*c7ef0cfcSnicm# we don't actually know what TERM will be nor even if it always has
233*c7ef0cfcSnicm# the same value for this user) we do the following three steps...
234*c7ef0cfcSnicm
235*c7ef0cfcSnicmif test -f "$HOME"/.termcap
236*c7ef0cfcSnicmthen
237*c7ef0cfcSnicm	echo "I see you have a \$HOME/.termcap file.  I will compile that."
238*c7ef0cfcSnicm	eval $TIC "$HOME"/.termcap
239*c7ef0cfcSnicm	echo "Done."
240*c7ef0cfcSnicm	echo "Note that editing $HOME/.termcap will no longer change the data curses sees."
241*c7ef0cfcSnicmelif test -f "$TERMCAP"
242*c7ef0cfcSnicmthen
243*c7ef0cfcSnicm	echo "Your TERMCAP names the file $TERMCAP.  I will compile that."
244*c7ef0cfcSnicm	eval $TIC "$TERMCAP"
245*c7ef0cfcSnicm	echo "Done."
246*c7ef0cfcSnicm	echo "Note that editing $TERMCAP will no longer change the data curses sees."
247*c7ef0cfcSnicmelse
248*c7ef0cfcSnicm	echo "Your TERMCAP value appears to be an entry in termcap format."
249*c7ef0cfcSnicm	echo "I will compile it."
250*c7ef0cfcSnicm	echo "$TERMCAP" >myterm$$
251*c7ef0cfcSnicm	eval $TIC myterm$$
252*c7ef0cfcSnicm	rm myterm$$
253*c7ef0cfcSnicm	echo "Done."
254*c7ef0cfcSnicm	echo "Note that editing TERMCAP will no longer change the data curses sees."
255*c7ef0cfcSnicmfi
256*c7ef0cfcSnicmecho "To do that, decompile the terminal description you want with infocmp(1),"
257*c7ef0cfcSnicmecho "edit to taste, and recompile using tic(1)."
258*c7ef0cfcSnicm
259*c7ef0cfcSnicm# capconvert ends here
260*c7ef0cfcSnicm
261