koi8rxterm revision d522f475
1#!/bin/sh
2# $XTermId: koi8rxterm,v 1.3 2007/12/30 16:33:06 tom Exp $
3# This is a wrapper script to set up xterm with a KOI8-R locale; based on
4# uxterm.
5
6whoami=koi8rxterm
7
8: ${XTERM_PROGRAM=xterm}
9
10# Check if there is a workable locale program.  If there is not, we will read
11# something via the standard error.  Ignore whatever is written to the
12# standard output.
13locale=`sh -c "LC_ALL=C LC_CTYPE=C LANG=C locale >/dev/null" 2>&1`
14found=no
15
16# Check for -version and -help options, to provide a simple return without
17# requiring the program to create a window:
18if test $# = 1
19then
20	case $1 in
21	-v|-ver*|-h|-he*)
22		$XTERM_PROGRAM "$@"
23		exit $?
24		;;
25	esac
26fi
27
28# Check environment variables that xterm does, in the same order:
29for name in LC_ALL LC_CTYPE LANG
30do
31	eval 'value=$'$name
32	if test -n "$value" ; then
33		case $value in
34		*.koi8r|*.KOI8R|*.koi8-r|*.KOI8-R)
35			found=yes
36			;;
37		*.koi8r@*|*.KOI8R@*|*.koi8-r@*|*.KOI8-R*)
38			found=yes
39			;;
40		*)
41			# The user may not have configured his or her
42			# locale; try to muddle through anyway.
43			value=ru_RU.KOI8-R
44			;;
45		esac
46		break
47	fi
48done
49
50# If we didn't find one that uses KOI8-R, modify the safest one.  Not everyone
51# has a KOI8-R locale installed (and there appears to be no trivial/portable
52# way to determine whether it is, from a shell script).  We could check if the
53# user's shell does not reset unknown locale specifiers, but not all shells do.
54if test $found != yes ; then
55	if test -n "$value" ; then
56		value=`echo ${value} |sed -e 's/[.@].*//'`.KOI8-R
57	else
58		name="LC_CTYPE"
59		value="ru_RU.KOI8-R"
60	fi
61	eval save=\$${name}
62	eval ${name}=${value}
63	eval export ${name}
64	if test -z "$locale" ; then
65		# The 'locale' program tries to do a sanity check.
66		check=`sh -c "locale >/dev/null" 2>&1`
67		if test -n "$check" ; then
68			eval ${name}=${save}
69			eval export ${name}
70
71			echo "$whoami tried to use locale $value by setting \$$name" >&2
72			xmessage -file - <<EOF
73$whoami tried unsuccessfully to use locale $value
74by setting \$$name to "${value}".
75EOF
76			exit 1
77		fi
78	fi
79fi
80
81# for testing:
82#test -f ./xterm && XTERM_PROGRAM=./xterm
83
84exec $XTERM_PROGRAM -class KOI8RXTerm -title $whoami -k8 "$@"
85