3 # A script to configure the keyboard map on console
5 # Copyright (c) 2019 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
=/tmp
/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 # Compose output file to be shown
86 # Use a portable find(1) in terms of options, plus sort(1),
87 # redirects the standard error to see any possible error
88 search
="$(find "$KEYMAPS" -name '*.map*' -print | sort 2> /dev/tty)"
92 echo "No keyboard maps were found in \`${KEYMAPS}'." 1>&2
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:" 24 56 12 \' \
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
130 printf "2> \$TMPFILE\n\n" >> $OUTPUT
135 # Read answer from 'TMPFILE'
137 echo "" >> $TMPFILE; # Append new line for reading.
138 IFS
= read -r REPLY
< $TMPFILE ||
exit 2;
140 # Compose values by layout/map to be set
147 # The temporary files are not needed anymore
148 rm -f -- "$TMPFILE" "$OUTPUT"
150 umask 022; # Remove write permission for group and other.
153 loadkeys
"${layout}/$map"
159 --no-mouse --no-cancel --no-ok \
160 --backtitle "\Zb
${PROGRAM} - Keyboard map check
" \
161 --title "SELECTED KEYMAP
: ${layout}/$map" \
163 "Try the chosen keyboard map by typing something. Once satisfied
\n\
164 type \Zb\Z71\Zn
(or \Zb\Z7y\Zn
) then hit \Zb\Z7ENTER\Zn \
165 to
continue. To choose a different keyboard map
, type \Zb\Z72\Zn
(or \
166 \Zb\Z7n\Zn
) plus \Zb\Z7ENTER\Zn.
" 10 68 2>&1 > /dev/tty
181 --backtitle "\Zb${PROGRAM} - Configuration file" \
182 --title "/etc/rc.conf" --yesno \
183 "Would do you like to save this change for the next reboot?" 5 62
188 echo "Setting 'RC_KEYMAP=${layout}/$map' on /etc/rc.conf ..."
189 echo ",s/^\(RC_KEYMAP\)=.*/\1=${layout}\/${map}/"$
'\nw' | \
190 ed
-s /etc
/rc.conf
&& echo Done.