1d522f475Smrg#!/bin/sh
204b94745Smrg# $XTermId: uxterm,v 1.30 2023/05/03 23:47:34 tom Exp $
320d2c4d2Smrg# -----------------------------------------------------------------------------
420d2c4d2Smrg# this file is part of xterm
520d2c4d2Smrg#
65307cd1aSmrg# Copyright 2001-2020,2023 by Thomas E. Dickey
720d2c4d2Smrg# 
820d2c4d2Smrg#                         All Rights Reserved
920d2c4d2Smrg# 
1020d2c4d2Smrg# Permission is hereby granted, free of charge, to any person obtaining a
1120d2c4d2Smrg# copy of this software and associated documentation files (the
1220d2c4d2Smrg# "Software"), to deal in the Software without restriction, including
1320d2c4d2Smrg# without limitation the rights to use, copy, modify, merge, publish,
1420d2c4d2Smrg# distribute, sublicense, and/or sell copies of the Software, and to
1520d2c4d2Smrg# permit persons to whom the Software is furnished to do so, subject to
1620d2c4d2Smrg# the following conditions:
1720d2c4d2Smrg# 
1820d2c4d2Smrg# The above copyright notice and this permission notice shall be included
1920d2c4d2Smrg# in all copies or substantial portions of the Software.
2020d2c4d2Smrg# 
2120d2c4d2Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
2220d2c4d2Smrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2320d2c4d2Smrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2420d2c4d2Smrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2520d2c4d2Smrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2620d2c4d2Smrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2720d2c4d2Smrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2820d2c4d2Smrg# 
2920d2c4d2Smrg# Except as contained in this notice, the name(s) of the above copyright
3020d2c4d2Smrg# holders shall not be used in advertising or otherwise to promote the
3120d2c4d2Smrg# sale, use or other dealings in this Software without prior written
3220d2c4d2Smrg# authorization.
3320d2c4d2Smrg# -----------------------------------------------------------------------------
34d522f475Smrg#
35d522f475Smrg# wrapper script to setup xterm with UTF-8 locale
36d522f475Smrg
37d522f475Smrgwhoami=uxterm
38d522f475Smrg
39f2e35a3aSmrg: "${XTERM_PROGRAM=xterm}"
40d522f475Smrg
41d522f475Smrg# Check if there is a workable locale program.  If there is not, we will read
42d522f475Smrg# something via the standard error.  Ignore whatever is written to the
43d522f475Smrg# standard output.
44d522f475Smrglocale=`sh -c "LC_ALL=C LC_CTYPE=C LANG=C locale >/dev/null" 2>&1`
45d522f475Smrgfound=no
46d522f475Smrg
47d522f475Smrg# Check for -version and -help options, to provide a simple return without
48d522f475Smrg# requiring the program to create a window:
49d522f475Smrgif test $# = 1
50d522f475Smrgthen
51d522f475Smrg	case $1 in
52d522f475Smrg	-v|-ver*|-h|-he*)
53d522f475Smrg		$XTERM_PROGRAM "$@"
54d522f475Smrg		exit $?
55d522f475Smrg		;;
56d522f475Smrg	esac
57d522f475Smrgfi
58d522f475Smrg
59f2e35a3aSmrg# Check environment variables that might be used for encoding:
60d522f475Smrgfor name in LC_ALL LC_CTYPE LANG
61d522f475Smrgdo
62d522f475Smrg	eval 'value=$'$name
63d522f475Smrg	if test -n "$value" ; then
64d522f475Smrg		case $value in
65d522f475Smrg		*.utf8|*.UTF8|*.utf-8|*.UTF-8)
66d522f475Smrg			found=yes
67d522f475Smrg			;;
68d522f475Smrg		*.utf8@*|*.UTF8@*|*.utf-8@*|*.UTF-8@*)
69d522f475Smrg			found=yes
70d522f475Smrg			;;
71f2e35a3aSmrg		*)
72f2e35a3aSmrg			value=""	# ignore
73f2e35a3aSmrg			continue	# keep trying
74d522f475Smrg			;;
75d522f475Smrg		esac
76d522f475Smrg		break
77d522f475Smrg	fi
78d522f475Smrgdone
79d522f475Smrg
80d522f475Smrg# If we didn't find one that used UTF-8, modify the safest one.  Not everyone
81d522f475Smrg# has a UTF-8 locale installed (and there appears to be no trivial/portable way
82d522f475Smrg# to determine whether it is, from a shell script).  We could check if the
83d522f475Smrg# user's shell does not reset unknown locale specifiers, but not all shells do.
84d522f475Smrgif test $found != yes ; then
85d522f475Smrg	if test -n "$value" ; then
865307cd1aSmrg		value=`echo "${value}" |sed -e 's/[.@].*//'`.UTF-8
87d522f475Smrg	else
88f2e35a3aSmrg		expect=
89f2e35a3aSmrg		for name in LC_ALL LC_CTYPE LANG
90f2e35a3aSmrg		do
91f2e35a3aSmrg			eval 'check=$'$name
92f2e35a3aSmrg			if test -n "$check"
93f2e35a3aSmrg			then
94f2e35a3aSmrg				expect=`echo "$check" | sed -e 's/[.@].*$//'`
95f2e35a3aSmrg				test -n "$expect" && break
96f2e35a3aSmrg			fi
97f2e35a3aSmrg		done
98f2e35a3aSmrg		if test -z "$expect" ; then
99f2e35a3aSmrg			name="LC_CTYPE"
100f2e35a3aSmrg			expect="en_US"
101f2e35a3aSmrg		fi
102f2e35a3aSmrg		value=`locale -a | awk -v "expect=$expect" 'BEGIN {
103f2e35a3aSmrg			exact=""
104f2e35a3aSmrg			maybe=""
105f2e35a3aSmrg		}
106f2e35a3aSmrg		/[.](utf|UTF)[-]?8(@.*)?$/ {
107f2e35a3aSmrg			if (index($0, "C.") == 1) {
108f2e35a3aSmrg				maybe=$0;
109f2e35a3aSmrg			}
110f2e35a3aSmrg			if (index($0, expect ".") == 1) {
111f2e35a3aSmrg				exact=$0;
112f2e35a3aSmrg			}
113f2e35a3aSmrg		}
114f2e35a3aSmrg		END {
115f2e35a3aSmrg			if ( exact != "" ) {
116f2e35a3aSmrg				print exact;
117f2e35a3aSmrg			} else if ( maybe != "" ) {
118f2e35a3aSmrg				print maybe;
119f2e35a3aSmrg			} else {
120f2e35a3aSmrg				print "";
121f2e35a3aSmrg			}
122f2e35a3aSmrg		}
123f2e35a3aSmrg		'`
124f2e35a3aSmrg		test -z "$value" && value="en_US.UTF-8"
125d522f475Smrg	fi
126d522f475Smrg	eval save=\$${name}
127d522f475Smrg	eval ${name}=${value}
128d522f475Smrg	eval export ${name}
129d522f475Smrg	if test -z "$locale" ; then
130d522f475Smrg		# The 'locale' program tries to do a sanity check.
131d522f475Smrg		check=`sh -c "locale >/dev/null" 2>&1`
132d522f475Smrg		if test -n "$check" ; then
133f2e35a3aSmrg			eval ${name}="${save}"
134d522f475Smrg			eval export ${name}
135d522f475Smrg
136d522f475Smrg			echo "$whoami tried to use locale $value by setting \$$name" >&2
137d522f475Smrg			xmessage -file - <<EOF
138d522f475Smrg$whoami tried unsuccessfully to use locale $value
139d522f475Smrgby setting \$$name to "${value}".
140d522f475SmrgEOF
141d522f475Smrg			exit 1
142d522f475Smrg		fi
143d522f475Smrg	fi
144d522f475Smrgfi
145d522f475Smrg
146d522f475Smrg# for testing:
147d522f475Smrg#test -f ./xterm && XTERM_PROGRAM=./xterm
148d522f475Smrg
1495307cd1aSmrgexec "$XTERM_PROGRAM" -class UXTerm -u8 "$@"
150