uxterm revision 5307cd1a
1#!/bin/sh
2# $XTermId: uxterm,v 1.29 2020/03/07 15:52:31 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 2001-2020,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#
35# wrapper script to setup xterm with UTF-8 locale
36
37whoami=uxterm
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 might be used for encoding:
60for name in LC_ALL LC_CTYPE LANG
61do
62	eval 'value=$'$name
63	if test -n "$value" ; then
64		case $value in
65		*.utf8|*.UTF8|*.utf-8|*.UTF-8)
66			found=yes
67			;;
68		*.utf8@*|*.UTF8@*|*.utf-8@*|*.UTF-8@*)
69			found=yes
70			;;
71		*)
72			value=""	# ignore
73			continue	# keep trying
74			;;
75		esac
76		break
77	fi
78done
79
80# If we didn't find one that used UTF-8, modify the safest one.  Not everyone
81# has a UTF-8 locale installed (and there appears to be no trivial/portable way
82# to determine whether it is, from a shell script).  We could check if the
83# user's shell does not reset unknown locale specifiers, but not all shells do.
84if test $found != yes ; then
85	if test -n "$value" ; then
86		value=`echo "${value}" |sed -e 's/[.@].*//'`.UTF-8
87	else
88		expect=
89		for name in LC_ALL LC_CTYPE LANG
90		do
91			eval 'check=$'$name
92			if test -n "$check"
93			then
94				expect=`echo "$check" | sed -e 's/[.@].*$//'`
95				test -n "$expect" && break
96			fi
97		done
98		if test -z "$expect" ; then
99			name="LC_CTYPE"
100			expect="en_US"
101		fi
102		value=`locale -a | awk -v "expect=$expect" 'BEGIN {
103			exact=""
104			maybe=""
105		}
106		/[.](utf|UTF)[-]?8(@.*)?$/ {
107			if (index($0, "C.") == 1) {
108				maybe=$0;
109			}
110			if (index($0, expect ".") == 1) {
111				exact=$0;
112			}
113		}
114		END {
115			if ( exact != "" ) {
116				print exact;
117			} else if ( maybe != "" ) {
118				print maybe;
119			} else {
120				print "";
121			}
122		}
123		'`
124		test -z "$value" && value="en_US.UTF-8"
125	fi
126	eval save=\$${name}
127	eval ${name}=${value}
128	eval export ${name}
129	if test -z "$locale" ; then
130		# The 'locale' program tries to do a sanity check.
131		check=`sh -c "locale >/dev/null" 2>&1`
132		if test -n "$check" ; then
133			eval ${name}="${save}"
134			eval export ${name}
135
136			echo "$whoami tried to use locale $value by setting \$$name" >&2
137			xmessage -file - <<EOF
138$whoami tried unsuccessfully to use locale $value
139by setting \$$name to "${value}".
140EOF
141			exit 1
142		fi
143	fi
144fi
145
146# for testing:
147#test -f ./xterm && XTERM_PROGRAM=./xterm
148
149exec "$XTERM_PROGRAM" -class UXTerm -u8 "$@"
150