dragora-keymap 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #! /bin/sh -
  2. #
  3. # A script to configure the keyboard map on console
  4. #
  5. # Copyright (c) 2019-2020 Matias Fonzo, <selk@dragora.org>.
  6. #
  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
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  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.
  18. # Exit immediately on any error
  19. set -e
  20. PROGRAM="${0##*/}"
  21. TMPDIR="${TMPDIR:-$HOME}"
  22. TMPFILE="${TMPDIR}/.${PROGRAM}.${RANDOM-0}$$"
  23. OUTPUT="${TMPFILE}.data"
  24. LOCKFILE=/var/lock/dragora-keymap.lockfile
  25. KEYMAPS="${KEYMAPS:-/usr/share/keymaps/i386}"
  26. chkstatus_or_exit()
  27. {
  28. status=$?
  29. # Clean up temporary files
  30. rm -f -- "$TMPFILE" "$LOCKFILE"
  31. if test $status -ne 0
  32. then
  33. printf '%s\n' "" "Return status = $status" 1>&2
  34. exit 2;
  35. fi
  36. unset status
  37. }
  38. # Sanity checks
  39. if test ! -d "$TMPDIR"
  40. then
  41. echo "${PROGRAM}: \`${TMPDIR}' is not a qualified temporary directory" 1>&2
  42. exit 1;
  43. fi
  44. if test ! -w "$TMPDIR"
  45. then
  46. echo "${PROGRAM}: \`${TMPDIR}' is not a writable temporary directory" 1>&2
  47. exit 1;
  48. fi
  49. if test ! -d "$KEYMAPS"
  50. then
  51. echo "${PROGRAM}: Directory \`${KEYMAPS}' for keyboard maps does not exist as such" 1>&2
  52. exit 1;
  53. fi
  54. trap 'chkstatus_or_exit' EXIT HUP INT QUIT ABRT TERM
  55. umask 077; # Remove access for all but user.
  56. # Unfortunately, loadkeys(1) is limited to the superuser;
  57. # we set a lock to allow only one instance of the script.
  58. if ( set -C ; echo ": $PROGRAM - locked" > $LOCKFILE ) 2> /dev/null
  59. then
  60. true
  61. else
  62. if test -e "$LOCKFILE"
  63. then
  64. echo "Only one instance of \`${PROGRAM}' is allowed." 1>&2
  65. exit 1;
  66. else
  67. echo "${PROGRAM}: \`${LOCKFILE}' lock failed." 1>&2
  68. exit 2;
  69. fi
  70. fi
  71. ### Search for keyboard map files
  72. search="$(find "$KEYMAPS" \( -type l -o -type f \) -name '*.map*' -print | LANG=C sort 2> /dev/tty)"
  73. if test -z "$search"
  74. then
  75. echo "No keyboard maps were found in \`${KEYMAPS}'." 1>&2
  76. exit 1;
  77. fi
  78. ### Compose output file to be shown
  79. # Header
  80. printf '%s\n' \
  81. 'dialog --colors \' \
  82. ' --backtitle "\\Zb${PROGRAM} - Keyboard map selection" \' \
  83. ' --title "MAIN MENU" --default-item "qwerty/us" --menu \' \
  84. '"Welcome to \\Z3${PROGRAM}\\Zn!\\n\\n\' \
  85. 'The following menu list is organized as keyboard \\n\' \
  86. '\"layout/language\". \\n\\n\' \
  87. 'Select a keyboard map for the console:" 20 56 6 \' \
  88. > "$OUTPUT"
  89. # Append 'search' to the body
  90. for item in $search
  91. do
  92. level="${item%/*}" # Remove all except the last slash.
  93. level="${level##*/}" # Starting-point under which it was found.
  94. item="${item%.map*}" # Remove extension(s).
  95. item="${item##*/}" # Get the base name.
  96. # Ignore unwanted map directories
  97. case $level in
  98. include | fgGIod)
  99. continue
  100. ;;
  101. *)
  102. echo "\"${level}/$item\" \"\" \\" >> "$OUTPUT"
  103. ;;
  104. esac
  105. done
  106. unset item level search
  107. # End of
  108. printf "2> \$TMPFILE\\n\\n" >> "$OUTPUT"
  109. # Import menu
  110. . "$OUTPUT"
  111. # Read answer from 'TMPFILE'
  112. echo "" >> "$TMPFILE"; # Append new line for reading.
  113. IFS= read -r REPLY < "$TMPFILE" || exit 2;
  114. # Compose values by layout/map to be set
  115. layout="${REPLY%/*}"
  116. map="${REPLY##*/}"
  117. unset REPLY
  118. # Temporary files are not needed anymore
  119. rm -f -- "$TMPFILE" "$OUTPUT"
  120. umask 022; # Remove write permission for group and other.
  121. # Set keyboard map
  122. loadkeys "${layout}/$map"
  123. while true
  124. do
  125. testkeys="$(
  126. dialog --colors \
  127. --no-mouse --no-cancel --no-ok \
  128. --backtitle "\\Zb${PROGRAM} - Keyboard map check" \
  129. --title "SELECTED KEYMAP: ${layout}/$map" \
  130. --inputbox \
  131. "Try the chosen keyboard map by typing something. Once satisfied\\n\
  132. type \\Zb\\Z71\\Zn (or \\Zb\\Z7y\\Zn) then hit \\Zb\\Z7ENTER\\Zn \
  133. to continue. To choose a different keyboard map, type \\Zb\\Z72\\Zn (or \
  134. \\Zb\\Z7n\\Zn) plus \\Zb\\Z7ENTER\\Zn." 10 68 2>&1 > /dev/tty
  135. )"
  136. case $testkeys in
  137. 1 | Y | y)
  138. break
  139. ;;
  140. 2 | N | n)
  141. rm -f -- "$LOCKFILE"
  142. exec "$0"
  143. ;;
  144. esac
  145. done
  146. unset testkeys
  147. dialog --colors \
  148. --backtitle "\\Zb${PROGRAM} - Configuration file" \
  149. --title "/etc/rc.conf" --yesno \
  150. "Would do you like to save this change for the next reboot?" 5 62
  151. echo ""
  152. echo "Setting 'RC_KEYMAP=${layout}/$map' on /etc/rc.conf ..."
  153. echo ",s/^\\(RC_KEYMAP\\)=.*/\\1=${layout}\\/${map}/"$'\nw' | \
  154. ed /etc/rc.conf && echo Done.