uxterm revision d522f475
1#!/bin/sh
2# $XTermId: uxterm,v 1.25 2007/12/30 16:33:36 tom Exp $
3#
4# wrapper script to setup xterm with UTF-8 locale
5
6whoami=uxterm
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		*.utf8|*.UTF8|*.utf-8|*.UTF-8)
35			found=yes
36			;;
37		*.utf8@*|*.UTF8@*|*.utf-8@*|*.UTF-8@*)
38			found=yes
39			;;
40		C|POSIX)
41			# Yes, I know this is not the same - but why are you
42			# here then?
43			value=en_US
44			;;
45		esac
46		break
47	fi
48done
49
50# If we didn't find one that used UTF-8, modify the safest one.  Not everyone
51# has a UTF-8 locale installed (and there appears to be no trivial/portable way
52# 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/[.@].*//'`.UTF-8
57	else
58		name="LC_CTYPE"
59		value="en_US.UTF-8"
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 UXTerm -title $whoami -u8 "$@"
85