1#!/bin/sh
2# $XTermId: koi8rxterm,v 1.7 2023/05/03 23:59:22 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 2007-2021,2023 by Thomas E. Dickey
7# 
8#                         All Rights Reserved
9# 
10# Permission is hereby granted, free of charge, to any person obtaining a
11# copy of this software and associated documentation files (the
12# "Software"), to deal in the Software without restriction, including
13# without limitation the rights to use, copy, modify, merge, publish,
14# distribute, sublicense, and/or sell copies of the Software, and to
15# permit persons to whom the Software is furnished to do so, subject to
16# the following conditions:
17# 
18# The above copyright notice and this permission notice shall be included
19# in all copies or substantial portions of the Software.
20# 
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28# 
29# Except as contained in this notice, the name(s) of the above copyright
30# holders shall not be used in advertising or otherwise to promote the
31# sale, use or other dealings in this Software without prior written
32# authorization.
33# -----------------------------------------------------------------------------
34# This is a wrapper script to set up xterm with a KOI8-R locale; based on
35# uxterm by Branden Robinson.
36
37whoami=koi8rxterm
38
39: "${XTERM_PROGRAM=xterm}"
40
41# Check if there is a workable locale program.  If there is not, we will read
42# something via the standard error.  Ignore whatever is written to the
43# standard output.
44locale=`sh -c "LC_ALL=C LC_CTYPE=C LANG=C locale >/dev/null" 2>&1`
45found=no
46
47# Check for -version and -help options, to provide a simple return without
48# requiring the program to create a window:
49if test $# = 1
50then
51	case $1 in
52	-v|-ver*|-h|-he*)
53		$XTERM_PROGRAM "$@"
54		exit $?
55		;;
56	esac
57fi
58
59# Check environment variables that xterm does, in the same order:
60for name in LC_ALL LC_CTYPE LANG
61do
62	eval 'value=$'$name
63	if test -n "$value" ; then
64		case $value in
65		*.koi8r|*.KOI8R|*.koi8-r|*.KOI8-R)
66			found=yes
67			;;
68		*.koi8r@*|*.KOI8R@*|*.koi8-r@*|*.KOI8-R*)
69			found=yes
70			;;
71		*)
72			# The user may not have configured his or her
73			# locale; try to muddle through anyway.
74			value=ru_RU.KOI8-R
75			;;
76		esac
77		break
78	fi
79done
80
81# If we didn't find one that uses KOI8-R, modify the safest one.  Not everyone
82# has a KOI8-R locale installed (and there appears to be no trivial/portable
83# way to determine whether it is, from a shell script).  We could check if the
84# user's shell does not reset unknown locale specifiers, but not all shells do.
85if test $found != yes ; then
86	if test -n "$value" ; then
87		value=`echo ${value} |sed -e 's/[.@].*//'`.KOI8-R
88	else
89		name="LC_CTYPE"
90		value="ru_RU.KOI8-R"
91	fi
92	eval save=\$${name}
93	eval ${name}=${value}
94	eval export ${name}
95	if test -z "$locale" ; then
96		# The 'locale' program tries to do a sanity check.
97		check=`sh -c "locale >/dev/null" 2>&1`
98		if test -n "$check" ; then
99			eval ${name}="${save}"
100			eval export ${name}
101
102			echo "$whoami tried to use locale $value by setting \$$name" >&2
103			xmessage -file - <<EOF
104$whoami tried unsuccessfully to use locale $value
105by setting \$$name to "${value}".
106EOF
107			exit 1
108		fi
109	fi
110fi
111
112# for testing:
113#test -f ./xterm && XTERM_PROGRAM=./xterm
114
115exec "$XTERM_PROGRAM" -class KOI8RXTerm -k8 "$@"
116