xref: /freebsd/usr.sbin/bsdconfig/share/keymap.subr (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1if [ ! "$_KEYMAP_SUBR" ]; then _KEYMAP_SUBR=1
2#
3# Copyright (c) 2013-2015 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." keymap.subr
33f_include $BSDCFG_SHARE/struct.subr
34
35############################################################ CONFIGURATION
36
37#
38# Defaults taken from usr.sbin/kbdmap/kbdmap.h
39#
40: ${DEFAULT_LANG:=en}
41case "$( sysctl -n kern.vty )" in
42vt)	: ${DEFAULT_KEYMAP_DIR:=/usr/share/vt/keymaps}		;;
43*)	: ${DEFAULT_KEYMAP_DIR:=/usr/share/syscons/keymaps}	;;
44esac
45
46############################################################ GLOBALS
47
48KEYMAPS=
49NKEYMAPS=0
50
51# A "keymap" from kbdmap's point of view
52f_struct_define KEYMAP \
53	desc \
54	keym \
55	mark
56
57#
58# Default behavior is to call f_keymap_get_all() automatically when loaded.
59#
60: ${KEYMAP_SELF_SCAN_ALL=1}
61
62############################################################ FUNCTIONS
63
64# f_keymap_register $name $desc $keym $mark
65#
66# Register a keymap. A `structure' (see struct.subr) is created with the name
67# keymap_$name (so make sure $name contains only alpha-numeric characters or
68# the underscore, `_'). The remaining arguments after $name correspond to the
69# propertise of the `KEYMAP' structure-type (defined above).
70#
71# If not already registered, the keymap is then appended to the KEYMAPS
72# environment variable, a space-separated list of all registered keymaps.
73#
74f_keymap_register()
75{
76	local name="$1" desc="$2" keym="$3" mark="$4"
77
78	f_struct_new KEYMAP "keymap_$name" || return $FAILURE
79	keymap_$name set desc "$desc"
80	keymap_$name set keym "$keym"
81	keymap_$name set mark "$mark"
82
83	# Scan our global register to see if needs ammending
84	local k found=
85	for k in $KEYMAPS; do
86		[ "$k" = "$name" ] || continue
87		found=1 && break
88	done
89	[ "$found" ] || KEYMAPS="$KEYMAPS $name"
90
91	return $SUCCESS
92}
93
94# f_keymap_checkfile $keymap
95#
96# Check that $keymap is a readable kbdmap(5) file. Returns success if $keymap
97# is a file, is readable, and exists in $DEFAULT_KEYMAP_DIR; otherwise failure.
98# If debugging is enabled, an appropriate debug error message is printed if
99# $keymap is not available.
100#
101f_keymap_checkfile()
102{
103	local keym="$1"
104
105	# Fixup keymap if it doesn't already contain at least one `/'
106	[ "${keym#*/}" = "$keym" ] && keym="$DEFAULT_KEYMAP_DIR/$keym"
107
108	# Short-cuts
109	[ -f "$keym" -a -r "$keym" ] && return $SUCCESS
110	f_debugging || return $FAILURE
111
112	# Print an appropriate debug error message
113	if [ ! -e "$keym" ]; then
114		f_dprintf "%s: No such file or directory" "$keym"
115	elif [ ! -f "$keym" ]; then
116		f_dprintf "%s: Not a file!" "$keym"
117	elif [ ! -r "$keym" ]; then
118		f_dprintf "%s: Permission denied" "$keym"
119	fi
120
121	return $FAILURE
122}
123
124# f_keymap_get_all
125#
126# Get all keymap information for kbdmap(5) entries both in the database and
127# loosely existing in $DEFAULT_KEYMAP_DIR.
128#
129f_keymap_get_all()
130{
131	local fname=f_keymap_get_all
132	local lang="${LC_ALL:-${LC_CTYPE:-${LANG:-$DEFAULT_LANG}}}"
133	[ "$lang" = "C" ] && lang="$DEFAULT_LANG"
134
135	f_dprintf "%s: Looking for keymap files..." $fname
136	f_dialog_info "$msg_looking_for_keymap_files"
137	f_dprintf "DEFAULT_LANG=[%s]" "$DEFAULT_LANG"
138
139	eval "$( awk -F: -v lang="$lang" -v lang_default="$DEFAULT_LANG" '
140		BEGIN {
141			# en_US.ISO8859-1 -> en_..\.ISO8859-1
142			dialect = lang
143			if (length(dialect) >= 6 &&
144			    substr(dialect, 3, 1) == "_")
145				dialect = substr(dialect, 1, 3) ".." \
146				          substr(dialect, 6)
147			printf "f_dprintf \"dialect=[%%s]\" \"%s\";\n", dialect
148
149			# en_US.ISO8859-1 -> en
150			lang_abk = lang
151			if (length(lang_abk) >= 3 &&
152			    substr(lang_abk, 3, 1) == "_")
153				lang_abk = substr(lang_abk, 1, 2)
154			printf "f_dprintf \"lang_abk=[%%s]\" \"%s\";\n",
155			       lang_abk
156		}
157		function find_token(buffer, token)
158		{
159			if (split(buffer, tokens, /,/) == 0) return 0
160			found = 0
161			for (t in tokens)
162				if (token == tokens[t]) { found = 1; break }
163			return found
164		}
165		function add_keymap(desc,mark,keym)
166		{
167			marks[keym] = mark
168			name = keym
169			gsub(/[^[:alnum:]_]/, "_", name)
170			gsub(/'\''/, "'\''\\'\'\''", desc);
171			printf "f_keymap_checkfile %s && " \
172			       "f_keymap_register %s '\'%s\'' %s %u\n",
173			       keym, name, desc, keym, mark
174		}
175		!/^[[:space:]]*(#|$)/ {
176			sub(/^[[:space:]]*/, "", $0)
177			keym = $1
178			if (keym ~ /^(MENU|FONT)$/) next
179			lg = ($2 == "" ? lang_default : $2)
180
181			# Match the entry and store the type of match we made
182			# as the mark value (so that if we make a better match
183			# later on with a higher mark, it overwrites previous)
184
185			mark = marks[keym];
186			if (find_token(lg, lang))
187				add_keymap($3, 4, keym) # Best match
188			else if (mark <= 3 && find_token(lg, dialect))
189				add_keymap($3, 3, keym)
190			else if (mark <= 2 && find_token(lg, lang_abk))
191				add_keymap($3, 2, keym)
192			else if (mark <= 1 && find_token(lg, lang_default))
193				add_keymap($3, 1, keym)
194			else if (mark <= 0)
195				add_keymap($3, 0, keym)
196		}
197	' "$DEFAULT_KEYMAP_DIR/INDEX.${DEFAULT_KEYMAP_DIR##*/}" )"
198
199
200	#
201	# Look for keymaps not in database
202	#
203	local direntry keym name
204	set +f # glob
205	for direntry in "$DEFAULT_KEYMAP_DIR"/*; do
206		[ "${direntry##*.}" = ".kbd" ] || continue
207		keym="${direntry##*/}"
208		f_str2varname "$keym" name
209		f_struct keymap_$name && continue
210		f_keymap_checkfile "$keym" &&
211			f_keymap_register $name "${keym%.*}" "$keym" 0
212		f_dprintf "%s: not in kbdmap(5) database" "$keym"
213	done
214
215	#
216	# Sort the items by their descriptions
217	#
218	f_dprintf "%s: Sorting keymap entries by description..." $fname
219	KEYMAPS=$(
220		for k in $KEYMAPS; do
221			echo -n "$k "
222			# NOTE: Translate '8x8' to '8x08' before sending to
223			# sort(1) so that things work out as we might expect.
224			debug= keymap_$k get desc | awk 'gsub(/8x8/,"8x08")||1'
225		done | sort -k2 | awk '{
226			printf "%s%s", (started ? " " : ""), $1; started = 1
227		}'
228	)
229
230	return $SUCCESS
231}
232
233# f_keymap_kbdcontrol $keymap
234#
235# Install keyboard map file from $keymap.
236#
237f_keymap_kbdcontrol()
238{
239	local keymap="$1"
240
241	[ "$keymap" ] || return $SUCCESS
242
243	# Fixup keymap if it doesn't already contain at least one `/'
244	[ "${keymap#*/}" = "$keymap" ] && keymap="$DEFAULT_KEYMAP_DIR/$keymap"
245
246	[ "$USE_XDIALOG" ] || kbdcontrol -l "$keymap"
247}
248
249############################################################ MAIN
250
251#
252# Scan for keymaps unless requeted otherwise
253#
254f_dprintf "%s: KEYMAP_SELF_SCAN_ALL=[%s]" keymap.subr "$KEYMAP_SELF_SCAN_ALL"
255case "$KEYMAP_SELF_SCAN_ALL" in
256""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
257*) f_keymap_get_all
258esac
259
260f_count NKEYMAPS $KEYMAPS
261f_dprintf "%s: Found %u keymap file(s)." keymap.subr $NKEYMAPS
262
263f_dprintf "%s: Successfully loaded." keymap.subr
264
265fi # ! $_KEYMAP_SUBR
266