| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# 0. Check whether we're on a console |
| 4 |
TTY=$(/usr/bin/tty) |
| 5 |
case "$TTY" in |
| 6 |
/dev/console|/dev/vc*|/dev/tty[0-9]*) |
| 7 |
;; |
| 8 |
*) |
| 9 |
echo "unicode_start skipped on $TTY" >&2 |
| 10 |
exit 0 |
| 11 |
;; |
| 12 |
esac |
| 13 |
|
| 14 |
# Enables Unicode processing in the current console. |
| 15 |
# |
| •16 |
|
| 17 |
if [ ! -e /proc/self/fd/0 ] ; then |
| 18 |
echo "Won't set unicode mode: Can't determine console type;" >&2 |
| 19 |
echo " Please ensure that /proc is mounted." >&2 |
| 20 |
exit 1 |
| 21 |
fi |
| 22 |
|
| 23 |
readlink /proc/self/fd/0 | grep -q -e /dev/vc -e '/dev/tty[^p]' -e /dev/console |
| 24 |
if [ $? -eq 1 ]; then |
| 25 |
echo "Won't set unicode mode: not a VT." >&2 |
| 26 |
exit 1 |
| 27 |
fi |
| 28 |
|
| 29 |
# 1. The input side: the keyboard driver. |
| 30 |
|
| 31 |
# Set the keyboard driver in Unicode mode. (Default is ASCII mode.) |
| 32 |
# This really does nothing with the way normal keys are handled in |
| 33 |
# the kernel. All it does is: |
| 34 |
# - It is necessary for `dumpkeys' in order to not drop U+XXXX |
| 35 |
# entries from the keymaps. |
| 36 |
# - It is necessary for `loadkeys' in order to avoid warnings. |
| 37 |
# - Unicode characters typed as Alt-x1 ... Alt-xn (where x1,...,xn |
| 38 |
# are digits on the numeric keypad) will be emitted in UTF-8. |
| 39 |
|
| 40 |
kbd_mode -u |
| 41 |
|
| 42 |
# Change the keyboard mapping in such a way that the non-ASCII keys |
| 43 |
# produce UTF-8 encoded multibyte sequences, instead of single bytes |
| 44 |
# >= 0x80 in a legacy 8-bit encoding. |
| 45 |
|
| 46 |
# Non-root users are allowed to change the unicode mode of their console, but |
| 47 |
# not the global keymap. root will have to load the keymap in unicode mode |
| 48 |
# explicitly. |
| 49 |
|
| 50 |
uid="$(id -u 2>/dev/null)" ||: |
| 51 |
if [ "$uid" = 0 ]; then |
| 52 |
# There is no way of reverting the effect of "dumpkeys | loadkeys --unicode", |
| 53 |
# the memory of the earlier keymap is lost. Therefore, try |
| 54 |
# to save a copy of the original keymap to be able to reload it in unicode_stop. |
| 55 |
# (see also http://mail.nl.linux.org/linux-utf8/2003-08/msg00053.html): |
| 56 |
|
| 57 |
[ -n "$HOME" ] && [ "$HOME" != '/' ] || |
| 58 |
HOME='/root' |
| 59 |
|
| 60 |
if [ -d "$HOME" ] && [ -w "$HOME" ]; then |
| 61 |
[ -d "$HOME/.kbd" ] || |
| 62 |
mkdir -- "$HOME/.kbd" |
| 63 |
|
| 64 |
[ ! -w "$HOME/.kbd" ] || |
| 65 |
dumpkeys > "$HOME/.kbd/.keymap_sv" |
| 66 |
fi |
| 67 |
|
| 68 |
# redirect stderr and stdout of loadkeys to /dev/null to avoid the confusing |
| 69 |
# "plus before udiaeresis ignored" warnings. |
| 70 |
|
| 71 |
dumpkeys | loadkeys --unicode > /dev/null 2>&1 |
| 72 |
fi |
| 73 |
|
| 74 |
# 2. The output side: the console screen. |
| 75 |
|
| 76 |
# Tell the console output driver that the bytes arriving are UTF-8 |
| 77 |
# encoded multibyte sequences. |
| 78 |
if [ -t 1 ] && [ -t 2 ]; then |
| 79 |
printf '\033%%G' |
| 80 |
fi |
| 81 |
stty iutf8 |
| 82 |
|
| 83 |
# Tell the graphics card how to display Unicode characters not |
| 84 |
# contained in the IBM 437 character set (on PCs). The font should |
| 85 |
# have a Unicode map attached, or explicitly specified, e.g., |
| 86 |
# by giving `def.uni' as a second argument. |
| 87 |
|
| 88 |
case "$#" in |
| 89 |
2) |
| 90 |
setfont "$1" -u "$2" |
| 91 |
;; |
| 92 |
1) |
| 93 |
setfont "$1" |
| 94 |
;; |
| 95 |
0) |
| 96 |
;; |
| 97 |
*) |
| 98 |
echo "usage: unicode_start [font [unicode map]]" |
| 99 |
;; |
| 100 |
esac |