home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / bin / setupcon < prev    next >
Encoding:
Text File  |  2013-01-05  |  29.6 KB  |  1,038 lines

  1. #!/bin/sh
  2.  
  3. #     setupcon -- setup the font and keyboard on the Linux console
  4. #     Copyright (C) 2011 Anton Zinoviev <anton@lml.bas.bg>
  5.  
  6. #     Permission is hereby granted, free of charge, to any person
  7. #     obtaining a copy of this file (the "Program"), to deal in the
  8. #     Program without restriction, including without limitation the
  9. #     rights to use, copy, modify, merge, publish, distribute,
  10. #     sublicense, and/or sell copies of the Program, and to permit
  11. #     persons to whom the Program is furnished to do so, subject to
  12. #     the following conditions: The above copyright notice and this
  13. #     permission notice shall be included in all copies or substantial
  14. #     portions of the Program.
  15.  
  16. #     THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. #     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. #     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. #     NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. #     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. #     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. #     FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE OR
  23. #     OTHER DEALINGS IN THE PROGRAM.
  24.  
  25. ###########################################################################
  26.  
  27. do_font=yes     # configure font
  28. do_kbd=yes      # configure keyboard
  29. do_check=yes    # test whether we are on the console
  30. do_verbose=''   # explain what is being doing
  31. do_save=''      # save the required files in /etc/console-setup
  32. savekbdfile=''  # save the keyboard map in $savekbdfile
  33. setupdir=''     # directory for --setup-dir
  34. SETUP=''
  35.  
  36. # The same as /usr/bin/which - in order to make "which" available before
  37. # /usr is mounted
  38. which () {
  39.     local IFS
  40.     IFS=:
  41.     for i in $PATH; do
  42.     if [ -f "$i/$1" -a -x "$i/$1" ]; then
  43.         echo "$i/$1"
  44.         return 0
  45.     fi
  46.     done
  47.     return 1
  48. }
  49.  
  50. # Create a temporary file name and set TMPFILE to its name.  Early in
  51. # the boot process /tmp is mounted read-only, so lets have some other
  52. # options...  I am not sure all non-GNU versions of mktemp understand
  53. # the -q option so redirections of stderr are used instead.
  54. TMPFILE=''
  55. tempfile () {
  56.     local tmp
  57.     tmp="$TMPFILE"
  58.     if \
  59.         TMPFILE=`mktemp /tmp/tmpkbd.XXXXXX 2>/dev/null` \
  60.             || TMPFILE=`mktemp /run/tmpkbd.XXXXXX 2>/dev/null` \
  61.             || TMPFILE=`mktemp /dev/.tmpkbd.XXXXXX 2>/dev/null` \
  62.             || TMPFILE=`mktemp /lib/init/rw/tmpkbd.XXXXXX 2>/dev/null` \
  63.             || TMPFILE=`mktemp 2>/dev/null`
  64.     then
  65.         if [ -z "$tmp" ]; then
  66.             trap 'rm -f "$TMPFILE" >/dev/null 2>&1' 0
  67.             trap "exit 2" 1 2 3 13 15
  68.         fi
  69.         return 0
  70.     else
  71.         TMPFILE=''
  72.         return 1
  73.     fi
  74. }
  75.  
  76. # Print the arguments to stderr if $do_verbose is yes
  77. report () {
  78.     if [ "$do_verbose" ]; then
  79.         echo "$@" >&2
  80.     fi
  81. }
  82.  
  83. # Execute a command on every console terminal screen specified in
  84. # $ACTIVE_CONSOLES.
  85. #
  86. # 1st argument: plain=execute only once with no tty change,
  87. # out=execute with standard output redirected to the tty, in=the same
  88. # but the standard input is being redirected, 'other argument'=do not
  89. # redirect the standard input or output but use this as a tty option.
  90. #
  91. # 2rd argument: option(s) for verbose output; 3nd argument: command to
  92. # run; other arguments: other options
  93. #
  94. # Example: run '-C ' -v setfont font.psf
  95. # If ACTIVE_CONSOLES='/dev/tty1 /dev/tty2 /dev/tty3', then this results in
  96. #
  97. # setfont -C /dev/tty1 font.psf -v
  98. # setfont -C /dev/tty2 font.psf -v
  99. # setfont -C /dev/tty3 font.psf -v
  100. #
  101. # or
  102. #
  103. # setfont -C /dev/tty1 font.psf >/dev/null 2>&1
  104. # setfont -C /dev/tty2 font.psf >/dev/null 2>&1
  105. # setfont -C /dev/tty3 font.psf >/dev/null 2>&1
  106. #
  107. # depending on the value of $do_verbose
  108.  
  109. run () {
  110.     local ttyarg cmd verbose tty x
  111.     ttyarg="$1"
  112.     verbose="$2"
  113.     cmd="$3"
  114.     shift; shift; shift
  115.     if [ -z "$ACTIVE_CONSOLES" ]; then
  116.         ttyarg=plain
  117.     fi
  118.     case "$ttyarg" in
  119.         plain)
  120.             if [ "$setupdir" ]; then
  121.                 SETUP="$SETUP$cmd $@
  122. "
  123.             elif [ "$do_verbose" ]; then
  124.         case "$verbose" in
  125.             ECHO)
  126.             report Executing $cmd "$@".
  127.             $cmd "$@"
  128.             ;;
  129.             *)
  130.             $cmd "$@" $verbose
  131.             ;;
  132.         esac
  133.             else
  134.                 $cmd "$@" >/dev/null 2>&1
  135.             fi
  136.             ;;
  137.         in)
  138.             for tty in $ACTIVE_CONSOLES; do
  139.                 if [ -r $tty ]; then
  140.                     if [ "$setupdir" ]; then
  141.                         # keep the space between < and $tty
  142.                         SETUP="$SETUP$cmd $@ < $tty
  143. "
  144.                     else
  145.                         report For $tty:
  146.                         run plain "$verbose" "$cmd" "$@" <$tty
  147.                     fi
  148.                 else
  149.                     report No read access from $tty. Can not execute $cmd.
  150.                 fi
  151.             done
  152.             ;;
  153.         *)
  154.             for tty in $ACTIVE_CONSOLES; do
  155.                 x="${ttyarg}$tty"
  156.                 run plain "$verbose" "$cmd" $x "$@"
  157.             done
  158.             ;;
  159.     esac
  160. }
  161.  
  162. # Search a file and return the full file name if found.
  163. # The filename may include wildcards and may not include space.
  164. # Example: findfile share/consolefonts Uni3-*.psf.gz
  165. # Result: /usr/share/consolefonts/Uni3-Fixed13.psf.gz
  166. findfile () {
  167.     local x
  168.     case "$2" in
  169.         /*)
  170.             if [ -f "$2" ]; then
  171.                 echo "$2"
  172.                 return 0
  173.             fi
  174.             ;;
  175.     esac
  176.     x=`(ls "$installdir"/$1/$2 /usr/local/$1/$2 \
  177.        /usr/$1/$2 /etc/console-setup/$2 \
  178.        "$installdir"/etc/console-setup/$2) 2>/dev/null`
  179.     x=`echo $x`
  180.     [ "${x%% *}" ] || report Unable to find "$2".
  181.     echo "${x%% *}"
  182. }
  183.  
  184. # Usage: utf_start /dev/tty5
  185. # Configure a console for unicode output
  186. utf_start () {
  187.     if [ -w "$1" ]; then
  188.         report Configuring "$1" in Unicode mode.
  189.         printf '\033%%G' >"$1"
  190.     else
  191.         report No write access to $tty. Can not configure Unicode mode.
  192.     fi
  193. }
  194.  
  195. # Usage: utf_stop /dev/tty5
  196. # Configure a console for non-unicode output
  197. utf_stop () {
  198.     if [ -w "$1" ]; then
  199.         report Configuring "$1" in non-Unicode mode.
  200.         printf '\033%%@' >"$1"
  201.     else
  202.         report No write access to $tty. Can not configure non-Unicode mode.
  203.     fi
  204. }
  205.  
  206. # Return code 0: we are on the console; 1: we are not on the console
  207. test_console () {
  208.     local ok
  209.     ok=0
  210.     if which tty >/dev/null; then
  211.         case "`tty`" in
  212.         /dev/tty[1-9]*|/dev/vc/[0-9]*|/dev/console|/dev/ttyv[0-9]*)
  213.                 return 0
  214.                 ;;
  215.         esac
  216.         ok=1
  217.     fi
  218.  
  219.     if which kbd_mode >/dev/null; then
  220.         mode="`(LC_ALL=C; export LC_ALL; kbd_mode) 2>&1`"
  221.         mode=${mode#The keyboard is in }
  222.         case "$mode" in
  223.             Unicode*|default*|xlate*) return 0 ;;
  224.         esac
  225.         ok=1
  226.     fi
  227.  
  228.     if which vidcontrol >/dev/null; then
  229.         if vidcontrol -i adapter >&- 2>&-; then
  230.             return 0
  231.         fi
  232.         ok=1
  233.     fi
  234.  
  235.     return $ok
  236. }
  237.  
  238. ###########################################################################
  239. ### PROCESS THE COMMAND LINE ARGUMENTS
  240. ###########################################################################
  241.  
  242. while [ "$1" ]; do
  243.     case "$1" in
  244.     -k|--keyboard-only)
  245.         do_font=''
  246.         ;;
  247.     -f|--font-only)
  248.         do_kbd=''
  249.         ;;
  250.     -v|--verbose)
  251.         do_verbose=yes
  252.         ;;
  253.     --force)
  254.         do_check=''
  255.         ;;
  256.     --save)
  257.         do_save=yes
  258.         ;;
  259.     --save-only)
  260.         do_save=yes
  261.             do_kbd=''
  262.             do_font=''
  263.             do_check=''
  264.         ;;
  265.         --save-keyboard)
  266.             shift
  267.             savekbdfile="$1"
  268.             do_kbd=''
  269.             do_font=''
  270.             do_check=''
  271.             ;;
  272.         --setup-dir)
  273.             shift
  274.             setupdir="$1"
  275.             do_check=''
  276.             ;;
  277.     -h|--help)
  278.         cat >&2 <<EOF
  279. Usage: setupcon [OPTION] [VARIANT]
  280. Sets up the font and the keyboard on Linux console.
  281.  
  282.   -k, --keyboard-only  setup the keyboard only, do not setup the font
  283.   -f, --font-only      setup the font only, do not setup the keyboard
  284.       --force          do not check whether we are on the console
  285.   -v, --verbose        explain what is being doing, try it if s.t. goes wrong
  286.       --save           copy the font and the console map in /etc/console-setup,
  287.                          update /etc/console-setup/cached.*
  288.       --save-only      only save; don't setup keyboard/font immediately
  289.                          (implies --force)
  290.   -h, --help           display this help and exit
  291.  
  292. If VARIANT is not specified setupcon looks for the configuration files
  293. (in this order) ~/.console-setup and if this doesn't exist then the
  294. combination/etc/default/keyboard + /etc/default/console-setup.  When
  295. a VARIANT is specified then setupcon looks for the configuration files
  296. ~/.console-setup.VARIANT and /etc/default/console-setup.VARIANT.
  297. EOF
  298.         exit 0
  299.         ;;
  300.     -*)
  301.         echo "setupcon: Unrecognised option $1" >&2
  302.         exit 1
  303.         ;;
  304.     *)
  305.         if [ -z "$VARIANT" ]; then
  306.         VARIANT="$1"
  307.         else
  308.         echo "setupcon: Two variants specified: $VARIANT and $1" >&2
  309.         exit 1
  310.         fi
  311.         ;;
  312.     esac
  313.     shift
  314. done
  315.  
  316. # installdir
  317. installdir=${0%/*}
  318. case "$installdir" in
  319.     */bin) installdir=${installdir%/bin} ;;
  320.     *) installdir=$installdir/.. ;;
  321. esac
  322. [ -n "$installdir" -a -d "$installdir"/bin ] || installdir=/usr
  323.  
  324.  
  325. ###########################################################################
  326. ### READ THE CONFIGURATION FILES
  327. ###########################################################################
  328.  
  329. if [ "$VARIANT" ]; then
  330.     VARIANT=".$VARIANT"
  331. fi
  332.  
  333. USER_CONFIG=${HOME}/.console-setup"$VARIANT"
  334. USER_CONFIG2=${HOME}/.keyboard"$VARIANT"
  335. MAIN_CONFIG=/etc/default/keyboard"$VARIANT"
  336. [ -f "$MAIN_CONFIG" ] \
  337.     || MAIN_CONFIG="$installdir"/etc/default/keyboard"$VARIANT"
  338. MAIN_CONFIG2=/etc/default/console-setup"$VARIANT"
  339. [ -f "$MAIN_CONFIG2" ] \
  340.     || MAIN_CONFIG2="$installdir"/etc/default/console-setup"$VARIANT"
  341.  
  342. if [ -f "$USER_CONFIG" ]; then
  343.     CONFIG="$USER_CONFIG"
  344.     CONFIG2="$USER_CONFIG2"
  345. elif [ -f "$MAIN_CONFIG" ]; then
  346.     CONFIG="$MAIN_CONFIG"
  347.     CONFIG2="$MAIN_CONFIG2"
  348. else
  349.     echo "setupcon: None of $MAIN_CONFIG nor $USER_CONFIG exists." >&2
  350.     exit 1
  351. fi
  352.  
  353. if [ -f "$CONFIG2" ]; then
  354.     . "$CONFIG2"
  355. else
  356.     # in order to permit "if [ cached.kmap.gz -ot $CONFIG2 ]; then ... fi"
  357.     CONFIG2="$CONFIG"
  358. fi
  359. . "$CONFIG"
  360.  
  361. ###########################################################################
  362. ### INITIALIZATION AND DEFAULT VALUES
  363. ###########################################################################
  364.  
  365. # do_verbose
  366. # The variable VERBOSE_OUTPUT is obsoleted in favour of the option --verbose
  367. if [ "$VERBOSE_OUTPUT" = yes ]; then
  368.     do_verbose=yes
  369. fi
  370.  
  371. # kernel
  372. kernel=unknown
  373. if which uname >/dev/null; then
  374.     case "`uname`" in
  375.         *Linux*) kernel=linux ;;
  376.         *FreeBSD*) kernel=freebsd ;;
  377.         *)
  378.             echo 'Unknown kernel (only Linux and FreeBSD are supported).' >&2
  379.             exit 1
  380.             ;;
  381.     esac
  382. fi
  383.  
  384. # do_save
  385. if [ -n "$do_save" ]; then
  386.     if [ ! -d /usr/share ]; then
  387.         echo It seems /usr is not mounted.  Will not save files in /etc. >&2
  388.         do_save=''
  389.     elif [ ! -w /etc/console-setup/ ]; then
  390.         echo /etc/console-setup is not writable.  No files will be saved there. >&2
  391.         do_save=''
  392.     fi
  393. fi
  394.  
  395. # ACTIVE_CONSOLES
  396. # When ACTIVE_CONSOLES=guess the following will result in ACTIVE_CONSOLES=''
  397. ACTIVE_CONSOLES=$(
  398.     for tty in $ACTIVE_CONSOLES; do
  399.         if [ -e $tty ]; then
  400.             echo $tty
  401.         fi
  402.     done
  403. )
  404. if [ -z "$ACTIVE_CONSOLES" ]; then
  405.     # Some crude guess
  406.     #  Conf. files:
  407.     #    BSD:        /etc/ttys
  408.     #    Sys V init: /etc/inittab
  409.     #    Upstart:    /etc/init/*
  410.     #  Devices:
  411.     #    Linux:   /dev/tty[1-9][0-9]*
  412.     #    FreeBSD: /dev/ttyv[0-9a-f]
  413.     for tty in \
  414.         $(cat /etc/inittab /etc/init/* /etc/ttys 2>/dev/null \
  415.     | grep getty \
  416.         | egrep '([[:blank:]]|^)tty([1-9][0-9]*|v[0-9a-f])([[:blank:]]|$)' \
  417.         | sed -e '/^ *#/d' \
  418.               -e 's/.*[[:blank:]]\(tty[1-9][0-9]*\).*/\1/' \
  419.               -e 's/.*[[:blank:]]\(ttyv[0-9a-f]\).*/\1/')
  420.     do
  421.         if [ -e /dev/$tty ]; then
  422.             ACTIVE_CONSOLES="$ACTIVE_CONSOLES /dev/$tty"
  423.         fi
  424.     done
  425. fi
  426. if [ -z "$ACTIVE_CONSOLES" ]; then
  427.     case "$kernel" in
  428.         linux) ACTIVE_CONSOLES=$(ls /dev/tty[1-6] 2>/dev/null) ;;
  429.         freebsd) ACTIVE_CONSOLES=$(ls /dev/ttyv[0-3] 2>/dev/null) ;;
  430.     esac
  431.     report Can not find the active virtual consoles, \
  432.         assuming ACTIVE_CONSOLES=\"$ACTIVE_CONSOLES\" >&2
  433. else
  434.     report Configuring $ACTIVE_CONSOLES
  435. fi
  436.  
  437. # CHARMAP
  438. if [ "$CHARMAP" = guess -o -z "$CHARMAP" ]; then
  439.     CHARMAP=''
  440.     if which locale >/dev/null; then
  441.         CHARMAP=`locale charmap`
  442.     fi
  443. fi
  444. CHARMAP=${CHARMAP:-UTF-8}
  445. # FreeBSD uses ISO8859-1, GNU uses ISO-8859-1, we use the GNU names
  446. case "$CHARMAP" in
  447.     ISO8859-*) CHARMAP="ISO-8859-${CHARMAP#ISO8859-}" ;;
  448.     US-ASCII|ANSI*) CHARMAP=ISO-8859-1 ;;
  449. esac
  450. report The charmap is $CHARMAP
  451.  
  452. # unicode
  453. if \
  454.     [ "$CHARMAP" = UTF-8 ]
  455. then
  456.     unicode=yes
  457. else
  458.     unicode=''
  459. fi
  460.  
  461. # do_font
  462. if [ "$do_font" ]; then
  463.     case "$kernel" in
  464.         linux)
  465.             if which consolechars >/dev/null ; then
  466.                 do_font=linuxct
  467.             elif which setfont >/dev/null ; then
  468.                 do_font=linuxkbd
  469.             else
  470.                 echo "Neither setfont nor consolechars is accessible. No font will be configured." >&2
  471.                 do_font=''
  472.             fi
  473.             ;;
  474.         freebsd)
  475.             if which vidcontrol >/dev/null ; then
  476.                 do_font=freebsd
  477.             else
  478.                 echo "vidcontrol is not accessible. No font will be configured." >&2
  479.                 do_font=''
  480.             fi
  481.             ;;
  482.     esac
  483. fi
  484. # Due to bug in splashy and usplash: do not load fonts (#540314)
  485. if which pidof >/dev/null; then
  486.     if pidof splashy > /dev/null || pidof usplash > /dev/null; then
  487.         do_font=''
  488.     fi
  489. fi
  490.  
  491. # CODESET
  492. [ "$CODESET" != guess ] || CODESET=''
  493. if [ -z "$CODESET" ]; then
  494.     case "$CHARMAP" in
  495.         UTF-8)            CODESET=Uni2;;
  496.         ARMSCII-8)        CODESET=Armenian ;;
  497.         CP1251)           CODESET=CyrSlav ;;
  498.         CP1255)           CODESET=Hebrew ;;
  499.         CP1256)           CODESET=Arabic ;;
  500.         GEORGIAN-ACADEMY) CODESET=Georgian ;;
  501.         GEORGIAN-PS)      CODESET=Georgian ;;
  502.         IBM1133)          CODESET=Lao ;;
  503.         ISIRI-3342)       CODESET=Arabic ;;
  504.         ISO-8859-1)       CODESET=Lat15 ;;
  505.         ISO-8859-2)       CODESET=Lat2 ;;
  506.         ISO-8859-3)       CODESET=Lat38 ;;
  507.         ISO-8859-4)       CODESET=Lat7 ;; # sometimes Lat15
  508.         ISO-8859-5)       CODESET=CyrSlav ;;
  509.         ISO-8859-6)       CODESET=Arabic ;;
  510.         ISO-8859-7)       CODESET=Greek ;;
  511.         ISO-8859-8)       CODESET=Hebrew ;;
  512.         ISO-8859-9)       CODESET=Lat15 ;;
  513.         ISO-8859-10)      CODESET=Lat15 ;;
  514.         ISO-8859-11)      CODESET=Thai ;;
  515.         ISO-8859-13)      CODESET=Lat7 ;;
  516.         ISO-8859-14)      CODESET=Lat38 ;;
  517.         ISO-8859-15)      CODESET=Lat15 ;;
  518.         ISO-8859-16)      CODESET=Lat2 ;;
  519.         KOI8-R)           CODESET=CyrKoi ;;
  520.         KOI8-U)           CODESET=CyrKoi ;;
  521.         TIS-620)          CODESET=Thai ;;
  522.         VISCII)           CODESET=Vietnamese ;;
  523.         *)
  524.             if [ "$do_font" ]; then
  525.                 echo Unsupported charmap $CHARMAP >&2
  526.                 exit 1
  527.             fi
  528.             ;;
  529.     esac
  530.     if [ "$kernel" = freebsd ]; then
  531.         # 512 character fonts are not supported on FreeBSD
  532.         case "$CODESET" in
  533.             Uni*|Vietnamese|Arabic|Ethiopian) CODESET=Lat15 ;;
  534.         esac
  535.     fi
  536. fi
  537. if [ "$CHARMAP" != UTF-8 -a "$kernel" = freebsd ]; then
  538.     if \
  539.         [ -z "`findfile share/syscons/scrnmaps ${CHARMAP}_${CODESET}.scm`" ]
  540.     then
  541.         report "Ignoring the CODESET specification ($CODESET)."
  542.         CODESET=`findfile share/syscons/scrnmaps ${CHARMAP}_*.scm`
  543.         if [ -n "$do_font" -a -z "$CODESET" ]; then
  544.             echo Unsupported charmap $CHARMAP >&2
  545.             exit 1
  546.         fi
  547.         CODESET=${CODESET%%*/}
  548.         CODESET=${CODESET#.scm*}
  549.         CODESET=${CODESET%*_}
  550.         report Using $CODESET instead.
  551.     fi
  552. fi
  553.  
  554. # FONTSIZE
  555. if [ -z "$FONTSIZE" -o "$FONTSIZE" = guess ]; then
  556.     FONTSIZE=16
  557. fi
  558. case "$FONTSIZE" in
  559.     8x*)
  560.         FONTSIZE=${FONTSIZE#*x}
  561.         ;;
  562.     *x8)
  563.         FONTSIZE=${FONTSIZE%x*}
  564.         ;;
  565.     *x*)
  566.         a=${FONTSIZE%x*}
  567.         b=${FONTSIZE#*x}
  568.         if [ "$a" -lt "$b" ]; then
  569.             FONTSIZE=${b}x${a}
  570.         fi
  571.         ;;
  572. esac
  573.  
  574. # mapdir, fontdir, stdfont, stdfontfallback
  575. case "$kernel" in
  576.     linux)
  577.         mapdir=share/consoletrans
  578.         stdmap=$CHARMAP.acm.gz
  579.         fontdir=share/consolefonts
  580.         stdfont=$CODESET-$FONTFACE$FONTSIZE.psf.gz
  581.         # [A-WXYZa-wyz] is a funny way to say [A-Za-wyz].  In some locales 
  582.         # [A-Z] includes x and we don't want this.
  583.         stdfontfallback=$CODESET-*[A-WXYZa-wyz]$FONTSIZE.psf.gz
  584.         ;;
  585.     freebsd)
  586.         mapdir=share/syscons/scrnmaps
  587.         stdmap=${CHARMAP}_${CODESET}.scm
  588.         fontdir=share/syscons/fonts
  589.         stdfont16=$CODESET-${FONTFACE}16.fnt
  590.         stdfont14=$CODESET-${FONTFACE}14.fnt
  591.         stdfont8=$CODESET-${FONTFACE}8.fnt
  592.         stdfontfallback16=$CODESET-*[A-WXYZa-wyz]16.fnt
  593.         stdfontfallback14=$CODESET-*[A-WXYZa-wyz]14.fnt
  594.         stdfontfallback8=$CODESET-*[A-WXYZa-wyz]8.fnt
  595.         ;;
  596. esac
  597.  
  598. # CONSOLE_MAP
  599. CONSOLE_MAP=${CONSOLE_MAP:-$ACM}
  600. [ -z "$CONSOLE_MAP" ] || CONSOLE_MAP=`findfile $mapdir "$CONSOLE_MAP"`
  601. [ -n "$CONSOLE_MAP" -o "$CHARMAP" = UTF-8 ] || CONSOLE_MAP=`findfile $mapdir $stdmap`
  602.  
  603. # FONTFILES
  604. FONTFILES=''
  605. if [ "$FONT" ]; then
  606.     for f in $FONT; do
  607.         FONTFILES="$FONTFILES `findfile $fontdir $f`"
  608.     done
  609. fi
  610. FONTFILES=`echo $FONTFILES` # remove extra spaces
  611. if [ -n "$FONTFACE" -a -z "$FONTFILES" ]; then
  612.     case "$kernel" in
  613.         linux)
  614.             # the following will fail if FONTFACE=guess ($stdfont will
  615.             # match nothing)
  616.             FONTFILES=`findfile $fontdir $stdfont`
  617.             [ "$FONTFILES" ] || FONTFILES=`findfile $fontdir $stdfontfallback`
  618.             case "$FONTFILES" in
  619.                 *[0-9]x[1-9]*.psf.gz)
  620.                     if which consolechars >/dev/null; then
  621.                 echo "\
  622. The consolechars utility from the \"console-tools\" package can load only fonts
  623. with 8 pixel width matrix.  Please install the setfont utility from the package
  624. \"kbd\" or reconfigure the font size." >&2
  625.                     fi
  626.                     ;;
  627.             esac
  628.             ;;
  629.         freebsd)
  630.             FONTFILES=`findfile $fontdir $stdfont16`
  631.             [ "$FONTFILES" ] || FONTFILES=`findfile $fontdir $stdfontfallback16`
  632.             font=`findfile $fontdir $stdfont14`
  633.             [ "$font" ] || font=`findfile $fontdir $stdfontfallback14`
  634.             [ -z "$font" ] || FONTFILES="$FONTFILES $font"
  635.             font=`findfile $fontdir $stdfont8`
  636.             [ "$font" ] || font=`findfile $fontdir $stdfontfallback8`
  637.             [ -z "$font" ] || FONTFILES="$FONTFILES $font"
  638.             ;;
  639.     esac
  640.     if [ -n "$do_font" -a -z "$FONTFILES" ]; then
  641.         echo Unable to find the required font.  No font will be configured. >&2
  642.         do_font=''
  643.     fi
  644. fi
  645.  
  646. # FONTMAPFILE
  647. FONTMAPFILE=''
  648. if [ "$kernel" = linux -a -n "$FONT_MAP" ]; then
  649.     FONTMAPFILE=`findfile share/consoletrans "$FONT_MAP"`
  650. fi
  651.  
  652. # XKBMODEL
  653. if [ -n "$do_kbd$do_save$savekbdfile$setupdir" -a "$XKBMODEL" = unknown ]; then
  654.     echo The keyboard model is unknown.  Keyboard will not be configured. >&2
  655.     XKBMODEL=''
  656. fi
  657. [ -n "$XKBMODEL" -o -z "$savekbdfile" ] || exit 1
  658.  
  659. # do_kbd
  660. [ "$XKBMODEL" ] || do_kbd=''
  661. if [ "$do_kbd" ]; then
  662.     case "$kernel" in
  663.         linux)
  664.             if which loadkeys >/dev/null; then
  665.                 do_kbd=linux
  666.             else
  667.                 echo loadkeys is not accessible. Keyboard will not be configured.>&2
  668.                 do_kbd=''
  669.             fi
  670.             ;;
  671.         freebsd)
  672.             if which kbdcontrol >/dev/null; then
  673.                 do_kbd=freebsd
  674.             else
  675.                 echo kbdcontrol is not accessible. Keyboard will not be configured.>&2
  676.                 do_kbd=''
  677.             fi
  678.             ;;
  679.     esac
  680. fi
  681.  
  682. # acm_option
  683. if [ "$CHARMAP" != UTF-8 ]; then
  684.     acm_option="-charmap $CHARMAP"
  685. elif [ "$kernel" = freebsd ]; then
  686.     acm_option='-charmap ISO-8859-1'
  687. else
  688.     acm_option=''
  689. fi
  690.  
  691. # rules_option
  692. if [ "$XKBRULES" ]; then
  693.     rules_option="-rules $XKBRULES"
  694. else
  695.     rules_option=''
  696. fi
  697.  
  698. # backspace
  699. case "$kernel" in
  700.     linux) backspace='del' ;;
  701.     freebsd) backspace='bs' ;;
  702. esac
  703. case \
  704.     "`(stty -a \
  705.           | egrep '(^| )erase *=' \
  706.           | sed -e 's/.* erase *= *//' -e 's/^erase *= *//' -e 's/[; ].*//') \
  707.       2>/dev/null`"
  708. in
  709.     ^\?) backspace='del' ;;
  710.     ^h|^H) backspace='bs' ;;
  711. esac
  712. case "$BACKSPACE" in
  713.     del) backspace='del' ;;
  714.     bs) backspace='bs' ;;
  715. esac
  716. case "$backspace" in
  717.     del) report BackSpace is ^? ;;
  718.     bs) report BackSpace is ^h ;;
  719.     *) echo Wrong BackSpace option >&2 ;;
  720. esac
  721.  
  722. # cached
  723. case "$kernel" in
  724.     linux)
  725.         cached=/etc/console-setup/cached_${CHARMAP}_$backspace$VARIANT.kmap.gz
  726.         ;;
  727.     freebsd)
  728.         cached=/etc/console-setup/cached_${CHARMAP}_$backspace$VARIANT.kbd
  729.         ;;
  730. esac
  731.  
  732. # savekbdfile
  733. if \
  734.     [ -z "$savekbdfile" -a -n "$do_save" ] \
  735.     && [ ! -f "$cached" \
  736.          -o ! "$CONFIG" -ot "$cached" \
  737.          -o ! "$CONFIG2" -ot "$cached" ]
  738. then
  739.     savekbdfile="$cached"
  740. fi
  741. if [ -z "$savekbdfile" -a -n "$setupdir" ]; then
  742.     mkdir -p "$setupdir"/etc/console-setup
  743.     case "$kernel" in
  744.         linux) savekbdfile="$setupdir"/etc/console-setup/cached.kmap.gz ;;
  745.         freebsd) savekbdfile="$setupdir"/etc/console-setup/cached.kbd ;;
  746.     esac
  747. fi
  748. [ "$XKBMODEL" ] || savekbdfile=''
  749. if [ "$kernel" = linux ] && ! which gzip >/dev/null; then
  750.     savekbdfile=''
  751.     echo gzip is not accessible.  Will not save cached keyboard map. >&2
  752. fi
  753.  
  754. # KMAP
  755. if [ -n "$KMAP" -a ! -f "$KMAP" ]; then
  756.     echo $KMAP does not exist. >&2
  757.     KMAP=''
  758. fi
  759.  
  760.  
  761. ###########################################################################
  762. ### SAVE THE FILES IN /etc
  763. ###########################################################################
  764.  
  765. if [ -n "$do_save" ]; then
  766.     case "$CONSOLE_MAP" in
  767.         /etc/console-setup/*) ;;
  768.         ?*) cp "$CONSOLE_MAP" /etc/console-setup/ ;;
  769.     esac
  770.     for font in $FONTFILES; do
  771.         case "$font" in
  772.             /etc/console-setup/*) ;;
  773.             ?*) cp "$font" /etc/console-setup/ ;;
  774.         esac
  775.     done
  776.     case "$FONTMAPFILE" in
  777.         /etc/console-setup/*) ;;
  778.         ?*) cp "$FONTMAPFILE" /etc/console-setup/ ;;
  779.     esac
  780. fi
  781.  
  782. if [ "$savekbdfile" ]; then
  783.     case "$kernel" in
  784.         linux)
  785.             tempfile || { echo Can not create temporary file >&2; exit 1; }
  786.             {
  787.             $installdir/bin/ckbcomp -backspace "$backspace" $acm_option \
  788.                     $rules_option -model "$XKBMODEL" \
  789.                 "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" >$TMPFILE \
  790.                     && gzip -9 <$TMPFILE >"$savekbdfile"
  791.             } || exit 1
  792.             rm $TMPFILE
  793.             ;;
  794.         freebsd)
  795.         $installdir/bin/ckbcomp -freebsd -backspace "$backspace" \
  796.                 $acm_option $rules_option -model "$XKBMODEL" \
  797.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" >"$savekbdfile" \
  798.                 || exit 1
  799.             ;;
  800.     esac
  801. fi
  802.  
  803.  
  804. ###########################################################################
  805. ### ARE WE ON THE CONSOLE?
  806. ###########################################################################
  807.  
  808. if [ "$do_check" ]; then
  809.     if ! test_console; then
  810.     echo We are not on the console, the console is left unconfigured. >&2
  811.     exit 0
  812.     fi
  813. fi
  814.  
  815.  
  816. ###########################################################################
  817. ### OUTPUT
  818. ###########################################################################
  819.  
  820. # Video mode
  821. if [ "$VIDEOMODE" ]; then
  822.     case "$do_font" in
  823.         freebsd)
  824.             run in ECHO vidcontrol "$VIDEOMODE"
  825.             ;;
  826.         linux*)
  827.             # this is a bit pointless as vesafb doesn't support changing mode
  828.             if which fbset >/dev/null; then
  829.                 run plain ECHO fbset -a "$VIDEOMODE"
  830.             else
  831.                 report fbset is not installed
  832.             fi
  833.             ;;
  834.     esac
  835. fi
  836.  
  837. # Setup unicode/non-unicode mode
  838. case "$do_font" in
  839.     # So far the FreeBSD kernel doesn't support changes of the mode from
  840.     # utf to 8-bit and vice versa (its a compile time option).
  841.     linux*)
  842.         if [ "$unicode" ]; then
  843.             run '' ECHO utf_start
  844.         else
  845.             run '' ECHO utf_stop
  846.         fi
  847.         ;;
  848. esac
  849.  
  850. # Load the font(s)
  851. if [ "$FONTFILES" ]; then
  852.     case "$do_font" in
  853.         freebsd)
  854.             if [ -z "$unicode" ]; then
  855.                 for font in $FONTFILES; do
  856.                     run plain ECHO vidcontrol -f $font
  857.                 done
  858.             fi
  859.             ;;
  860.         linuxkbd)
  861.             if [ "$FONTMAPFILE" ]; then
  862.             run '-C ' -v setfont $FONTFILES -u "$FONTMAPFILE"
  863.             else
  864.             run '-C ' -v setfont $FONTFILES
  865.             fi
  866.             ;;
  867.         linuxct)
  868.             if [ "$FONTMAPFILE" ]; then
  869.             run --tty= -v consolechars -f ${FONTFILES%% *} -u "$FONTMAPFILE"
  870.             else
  871.             run --tty= -v consolechars -f ${FONTFILES%% *}
  872.             fi
  873.             ;;
  874.     esac
  875. fi
  876.  
  877. # Load the console map
  878. if [ "$CONSOLE_MAP" ]; then
  879.     case "$do_font" in
  880.         freebsd)
  881.             if [ -z "$unicode" ]; then
  882.                 run plain ECHO vidcontrol -l "$CONSOLE_MAP"
  883.             fi
  884.             ;;
  885.         linuxkbd)
  886.         run '-C ' -v setfont -m "$CONSOLE_MAP"
  887.             ;;
  888.         linuxct)
  889.             run --tty= -v consolechars --acm "$CONSOLE_MAP"
  890.             ;;
  891.     esac
  892. fi
  893.  
  894. # Setup the terminal width and height
  895. if [ "$do_font" ]; then
  896.     STTY=''
  897.     [ -z "$SCREEN_WIDTH"  ] || STTY="$STTY cols $SCREEN_WIDTH"
  898.     [ -z "$SCREEN_HEIGHT" ] || STTY="$STTY rows $SCREEN_HEIGHT"
  899.  
  900.     if [ "$STTY" ]; then
  901.         run in ECHO stty $STTY
  902.     fi
  903. fi
  904.  
  905.  
  906. ###########################################################################
  907. ### INPUT
  908. ###########################################################################
  909.  
  910. # On Mac PPC machines, we may need to set kernel vars first.  We need
  911. # to mount /proc to do that, but we need it set up before sulogin may
  912. # be run in checkroot, which will need the keyboard to log in...
  913. # This code was borrowed from the keymap.sh script of console-common
  914. # Copyright ┬⌐ 2001 Yann Dirson
  915. # Copyright ┬⌐ 2001 Alcove http://www.alcove.fr/
  916. if [ "$do_kbd" = linux ]; then
  917.     if [ -x /sbin/sysctl -a -r /etc/sysctl.conf ]; then
  918.     if grep -v '^\#' /etc/sysctl.conf | grep -q keycodes ; then
  919.         grep keycodes /etc/sysctl.conf | grep -v "^#" \
  920.         | while read d ; do
  921.                 /sbin/sysctl -w $d 2> /dev/null || true
  922.             done
  923.     fi
  924.     fi
  925. fi
  926.  
  927. # Setup unicode/non-unicode mode
  928. if [ "$do_kbd" = linux ]; then
  929.     if which kbd_mode >/dev/null; then
  930.         if [ "$unicode" ]; then
  931.             run in ECHO kbd_mode -u
  932.         else
  933.             run in ECHO kbd_mode -a
  934.         fi
  935.     else
  936.         report kbd_mode is not accessible.  Unable to setup unicode/non-unicode keyboard mode.
  937.     fi
  938. fi
  939.  
  940. if \
  941.     [ -z "$KMAP" -a -f "$cached" ] \
  942.     && [ "$CONFIG" -ot "$cached" -a "$CONFIG2" -ot "$cached" ]
  943. then
  944.     KMAP="$cached"
  945. fi
  946.  
  947. if [ "$KMAP" ]; then
  948.     case "$do_kbd" in
  949.         linux) run plain '' loadkeys "$KMAP" ;;
  950.         freebsd) run in ECHO kbdcontrol -l "$KMAP" ;;
  951.     esac
  952. else
  953.     tempfile || { echo Can not create temporary file >&2; exit 1; }
  954.     case "$do_kbd" in
  955.         linux)
  956.         $installdir/bin/ckbcomp -backspace "$backspace" $acm_option \
  957.                 $rules_option -model "$XKBMODEL" \
  958.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" >$TMPFILE
  959.             run plain ECHO loadkeys $TMPFILE
  960.             ;;
  961.         freebsd)
  962.         $installdir/bin/ckbcomp -freebsd -backspace "$backspace" \
  963.                 $acm_option $rules_option -model "$XKBMODEL" \
  964.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" >$TMPFILE
  965.             run in ECHO kbdcontrol -l $TMPFILE
  966.             run in ECHO kbdcontrol -f 70 "`printf '\033[3~'`"
  967.             ;;
  968.     esac
  969.     rm $TMPFILE
  970. fi
  971.  
  972.  
  973. ###########################################################################
  974. ### SETUPDIR
  975. ###########################################################################
  976.  
  977.  
  978.  
  979. if [ "$setupdir" ]; then
  980.  
  981.     filearg () {
  982.         case "$1" in
  983.             *.kmap.gz|*.kbd|*/tmpkbd.*)
  984.                 echo -n "/etc/console-setup/${savekbdfile##*/} "
  985.                 ;;
  986.             /*)
  987.                 if [ -e "$1" ]; then
  988.                     if [ ! -e "$setupdir/etc/console-setup/${1##*/}" ]; then
  989.                         cp -a "$1" "$setupdir/etc/console-setup/${1##*/}"
  990.                     fi
  991.                     echo -n "/etc/console-setup/${1##*/} "
  992.                 else
  993.                     echo -n "$1 "
  994.                 fi
  995.                 ;;
  996.             *)
  997.                 echo -n "$1 "
  998.                 ;;
  999.         esac
  1000.     }
  1001.  
  1002.     mkdir -p "$setupdir"/bin
  1003.  
  1004.     echo '#!/bin/sh' >"$setupdir"/bin/setupcon
  1005.     echo '# A micro-version of setupcon with static configuration.' >>"$setupdir"/bin/setupcon
  1006.     chmod +x "$setupdir"/bin/setupcon
  1007.     tempfile || { echo Can not create temporary file >&2; exit 1; }
  1008.     echo "$SETUP" |
  1009.     while read cmd args; do
  1010.         case "$cmd $args" in
  1011.             utf_start*)
  1012.                 echo -n printf \'\\\\'033%%G'\'' >' # not for FreeBSD
  1013.                 filearg "${args%% *}"
  1014.                 echo
  1015.                 ;;
  1016.             utf_stop*)
  1017.                 echo -n printf \'\\\\'033%%@'\'' >' # not for FreeBSD
  1018.                 filearg "${args%% *}"
  1019.                 echo
  1020.                 ;;
  1021.             kbdcontrol?-f?70*)
  1022.                 echo kbdcontrol -f 70 \"\`printf \'\\\\033[3~\'\`\"
  1023.                 ;;
  1024.             *)
  1025.                 which "$cmd" >>$TMPFILE || true
  1026.                 echo -n "$cmd "
  1027.                 for arg in $args; do
  1028.                     filearg "$arg"
  1029.                 done
  1030.                 echo
  1031.                 ;;
  1032.         esac
  1033.     done >>"$setupdir"/bin/setupcon
  1034.     echo exit 0 >>"$setupdir"/bin/setupcon
  1035.     sort $TMPFILE | uniq >"$setupdir"/morefiles
  1036.     rm $TMPFILE
  1037. fi
  1038.