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