3 # A script to configure the keyboard map on console
5 # Copyright (c) 2019-2021 Matias Fonzo, <selk@dragora.org>.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # Exit immediately on any error
23 TMPDIR
="${TMPDIR:-$HOME}"
24 TMPFILE
="${TMPDIR}/.${PROGRAM}.${RANDOM-0}$$"
25 OUTPUT
="${TMPFILE}.data"
26 LOCKFILE
=/var
/lock
/dragora-keymap.lockfile
27 KEYMAPS
="${KEYMAPS:-/usr/share/keymaps/i386}"
33 # Clean up temporary files
34 rm -f -- "$TMPFILE" "$LOCKFILE"
38 printf '%s\n' "" "Return status = $status" 1>&2
47 if test ! -d "$TMPDIR"
49 echo "${PROGRAM}: \`${TMPDIR}' is not a qualified temporary directory" 1>&2
52 if test ! -w "$TMPDIR"
54 echo "${PROGRAM}: \`${TMPDIR}' is not a writable temporary directory" 1>&2
57 if test ! -d "$KEYMAPS"
59 echo "${PROGRAM}: Directory \`${KEYMAPS}' for keyboard maps does not exist as such" 1>&2
63 trap 'chkstatus_or_exit' EXIT HUP INT QUIT ABRT TERM
65 umask 077; # Remove access for all but user.
67 # Unfortunately, loadkeys(1) is limited to the superuser;
68 # we set a lock to allow only one instance of the script.
70 if ( set -C ; echo ": $PROGRAM - locked" > $LOCKFILE ) 2> /dev
/null
74 if test -e "$LOCKFILE"
76 echo "Only one instance of \`${PROGRAM}' is allowed." 1>&2
79 echo "${PROGRAM}: \`${LOCKFILE}' lock failed." 1>&2
84 ### Search for keyboard map files
86 search
="$(find "$KEYMAPS" \( -type l -o -type f \) -name '*.map*' -print | LC_ALL=C sort 2> /dev/tty)"
90 echo "No keyboard maps were found in \`${KEYMAPS}'." 1>&2
94 ### Compose output file to be shown
99 ' --backtitle "\\Zb${PROGRAM} - Keyboard map selection" \' \
100 ' --title "MAIN MENU" --default-item "qwerty/us" --menu \' \
101 '"Welcome to \\Z3${PROGRAM}\\Zn!\\n\\n\' \
102 'The following menu list is organized as keyboard \\n\' \
103 '\"layout/language\". \\n\\n\' \
104 'Select a keyboard map for the console:" 20 56 6 \' \
107 # Append 'search' to the body
111 level
="${item%/*}" # Remove all except the last slash.
112 level
="${level##*/}" # Starting-point under which it was found.
113 item
="${item%.map*}" # Remove extension(s).
114 item
="${item##*/}" # Get the base name.
116 # Ignore unwanted map directories
122 echo "\"${level}/$item\" \"\" \\" >> "$OUTPUT"
126 unset -v item level search
129 printf "2> \$TMPFILE\\n\\n" >> "$OUTPUT"
134 # Read answer from 'TMPFILE'
136 echo "" >> "$TMPFILE"; # Append new line for reading.
137 IFS
= read -r REPLY
< "$TMPFILE" ||
exit 2;
139 # Compose values by layout/map to be set
146 # Temporary files are not needed anymore
147 rm -f -- "$TMPFILE" "$OUTPUT"
149 umask 022; # Remove write permission for group and other.
152 loadkeys
"${layout}/$map"
158 --no-mouse --no-cancel --no-ok \
159 --backtitle "\\Zb
${PROGRAM} - Keyboard map check
" \
160 --title "SELECTED KEYMAP
: ${layout}/$map" \
162 "Try the chosen keyboard map by typing something. Once satisfied
\\n\
163 type \\Zb
\\Z71
\\Zn
(or
\\Zb
\\Z7y
\\Zn
) then hit
\\Zb
\\Z7ENTER
\\Zn \
164 to
continue. To choose a different keyboard map
, type \\Zb
\\Z72
\\Zn
(or \
165 \\Zb
\\Z7n
\\Zn
) plus
\\Zb
\\Z7ENTER
\\Zn.
" 10 68 2>&1 > /dev/tty
180 --backtitle "\\Zb${PROGRAM} - Configuration file" \
181 --title "/etc/rc.conf" --yesno \
182 "Would do you like to save this change for the next reboot?" 5 62
184 echo "Setting 'RC_KEYMAP=${layout}/$map' on /etc/rc.conf ..."
185 echo ",s/^\\(RC_KEYMAP\\)=.*/\\1=${layout}\\/${map}/"$
'\nw' | \
186 ed
/etc
/rc.conf
&& echo Done.