Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / bin / setupcon
1 #!/bin/sh
2
3 #     setupcon -- setup the font and keyboard on the Linux console
4 #     Copyright © 1999,2000,2001,2002,2003,2006 Anton Zinoviev
5
6 #     This program is free software; you can redistribute it and/or modify
7 #     it under the terms of the GNU General Public License as published by
8 #     the Free Software Foundation; either version 2 of the License, or
9 #     (at your option) any later version.
10
11 #     This program is distributed in the hope that it will be useful,
12 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #     GNU General Public License for more details.
15
16 #     If you have not received a copy of the GNU General Public License
17 #     along with this program, write to the Free Software Foundation, Inc.,
18 #     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ###########################################################################
21
22
23 # The same as /usr/bin/which - in order to make "which" available before
24 # /usr is mounted
25 which () {
26     local IFS
27     IFS=:
28     for i in $PATH; do
29         if [ -x "$i/$1" ]; then
30             echo "$i/$1"
31             return 0
32         fi
33     done
34     return 1
35 }
36
37 while [ "$1" ]; do
38     case "$1" in
39         -k|--keyboard-only)
40             keyboard_only=yes
41             ;;
42         -f|--font-only)
43             font_only=yes
44             ;;
45         -v|--verbose)
46             verbose_option=yes
47             ;;
48         --force)
49             force=yes
50             ;;
51         --save)
52             save=yes
53             ;;
54         --save-only)
55             force=yes
56             save=yes
57             save_only=yes
58             ;;
59         -h|--help)
60             cat >&2 <<EOF
61 Usage: setupcon [OPTION] [VARIANT]
62 Sets up the font and the keyboard on Linux console.
63
64   -k, --keyboard-only  setup the keyboard only, do not setup the font
65   -f, --font-only      setup the font only, do not setup the keyboard
66       --force          do not check whether we are on the console
67   -v, --verbose        explain what is being doing, try it if s.t. goes wrong
68       --save           copy the font and the console map in /etc/console-setup,
69                          update /etc/console-setup/cached.kmap.gz
70       --save-only      only save; don't setup keyboard/font immediately
71                          (implies --force)
72   -h, --help           display this help and exit
73
74 If VARIANT is not specified setupcon looks for the configuration files
75 (in this order) ~/.console-setup and if this doesn't exist then the
76 combination/etc/default/keyboard + /etc/default/console-setup.  When
77 a VARIANT is specified then setupcon looks for the configuration files
78 ~/.console-setup.VARIANT and /etc/default/console-setup.VARIANT.
79 EOF
80             exit 0
81             ;;
82         -*)
83             echo "setupcon: Unrecognised option $1" >&2
84             exit 1
85             ;;
86         *)
87             if [ -z "$VARIANT" ]; then
88                 VARIANT="$1"
89             else
90                 echo "setupcon: Two variants specified: $VARIANT and $1" >&2
91                 exit 1
92             fi
93             ;;
94     esac
95     shift
96 done
97
98 if [ "$VARIANT" ]; then
99     USER_CONFIG=${HOME}/.console-setup."$VARIANT"
100     MAIN_CONFIG=/etc/default/keyboard."$VARIANT"
101     MAIN_CONFIG2=/etc/default/console-setup."$VARIANT"
102 else
103     USER_CONFIG=${HOME}/.console-setup
104     MAIN_CONFIG=/etc/default/keyboard
105     MAIN_CONFIG2=/etc/default/console-setup
106 fi
107
108 if [ -f "$USER_CONFIG" ]; then
109     CONFIG="$USER_CONFIG"
110     CONFIG2=''
111     save=
112 elif [ -f "$MAIN_CONFIG" ]; then
113     CONFIG="$MAIN_CONFIG"
114     CONFIG2="$MAIN_CONFIG2"
115 else
116     echo "setupcon: None of $MAIN_CONFIG nor $USER_CONFIG exists." >&2
117     exit 1
118 fi
119
120 if [ "$CONFIG2" -a -f "$CONFIG2" ]; then
121     . "$CONFIG2"
122 fi
123 . "$CONFIG"
124
125 if [ "$verbose_option" = yes ]; then
126     VERBOSE_OUTPUT=yes
127 fi
128 if [ "$VERBOSE_OUTPUT" = yes ]; then
129     verbose=''
130 else
131     verbose='>/dev/null 2>&1'
132 fi
133
134 if which ckbcomp-mini >/dev/null; then
135     CHARMAP=UTF-8
136     if [ "$VERBOSE_OUTPUT" = yes -a "$CHARMAP" != UTF-8 ]; then
137         echo Only UTF-8 is supported by console-setup-mini
138     fi
139 fi
140
141 if [ "$force" != yes ]; then
142     case `readlink /proc/self/fd/2` in
143         /dev/tty[0-9]*|/dev/vc/[0-9]*|/dev/console)
144             ;;
145         *)
146             echo We are not on the Linux console, the console is left unconfigured.
147             exit 0 
148             ;;
149     esac
150 fi
151
152 ACTIVE_CONSOLES=${ACTIVE_CONSOLES:-/dev/tty[1-6]}
153 CHARMAP=${CHARMAP:-UTF-8}
154 CODESET=${CODESET:-Uni2}
155 CONSOLE_MAP=${CONSOLE_MAP:-$ACM}
156
157 #-----------------------#
158 #       OUTPUT          #
159 #-----------------------#
160
161 if [ "$keyboard_only" != yes ] && [ "$ACTIVE_CONSOLES" ]; then
162     # Setup unicode/non-unicode mode
163     if [ "$save_only" != yes ]; then
164         for console in $ACTIVE_CONSOLES; do
165             [ -w $console ] || continue
166             if \
167                 [ "$CHARMAP" = UTF-8 ] || [ -z "$CONSOLE_MAP$CHARMAP" ]
168             then
169                 printf '\033%%G' >$console
170             else
171                 printf '\033%%@' >$console
172             fi
173         done
174     fi
175     
176     # Load the font
177     if [ -z "$FONT" ]; then
178         FONT_MAP=''
179         if which ckbcomp-mini >/dev/null; then
180             FONT=$(echo `ls /usr/share/consolefonts/$CODESET-*.psf* \
181                             /etc/console-setup/$CODESET-*.psf* 2>/dev/null`)
182             FONT=${FONT%% *}
183             if [ "$FONTFACE" ] || [ "$FONTSIZE" ]; then
184                 eval echo setupcon: Warning: ignoring the FONTFACE and FONTSIZE specifications! $verbose
185             fi
186         elif [ "$FONTFACE" ] && [ "$FONTSIZE" ]; then
187             FONT="$CODESET-$FONTFACE$FONTSIZE.psf.gz"
188             case "$FONTSIZE" in
189                 *x*)
190                     bigfont=yes
191                     ;;
192                 *)
193                     bigfont=no
194                     ;;
195             esac
196         fi
197     fi
198     LOADFONTS=''
199     for f in $FONT; do
200         if [ -f "$f" ]; then
201             LOADFONTS="$LOADFONTS $f"
202         else
203             fdec="${f%.gz}"
204             for dir in \
205                 /usr/local/share/consolefonts \
206                 /usr/share/consolefonts \
207                 /etc/console-setup \
208                 FONT_IS_MISSING
209             do
210                 if [ -f "$dir/${f##*/}" ]; then
211                     LOADFONTS="$LOADFONTS $dir/${f##*/}"
212                     break
213                 elif [ -f "$dir/${fdec##*/}" ]; then
214                     LOADFONTS="$LOADFONTS $dir/${fdec##*/}"
215                     break
216                 fi
217                 if [ "$dir" = FONT_IS_MISSING ]; then
218                     echo setupcon: Warning: the font ${f##*/} does not exist! >&2
219                 fi
220             done
221         fi
222     done
223     LOADFONT_MAP=''
224     for f in $FONT_MAP; do
225         [ -z "$LOADFONT_MAP" ] || break # only one unicode map
226         if [ -f "$f" ]; then
227             LOADFONT_MAP="$LOADFONT_MAP $f"
228         else
229             for dir in \
230                 /usr/local/share/consoletrans \
231                 /usr/share/consoletrans \
232                 /etc/console-setup \
233                 FONT_MAP_IS_MISSING
234             do
235                 if [ -f "$dir/${f##*/}" ]; then
236                     LOADFONT_MAP="$LOADFONT_MAP $dir/${f##*/}"
237                     break
238                 fi
239                 if [ "$dir" = FONT_MAP_IS_MISSING ]; then
240                     echo setupcon: Warning: the unicode map ${f##*/} does not exist! >&2
241                 fi
242             done
243         fi
244     done
245     
246     for f in $LOADFONTS; do
247         if \
248             [ "$save" = yes ] \
249             && [ "${f%/*}" != /etc/console-setup ]
250         then
251             fdec="${f%.gz}"
252             if [ "$fdec" = "$f" ]; then
253                 cp "$f" /etc/console-setup/
254             else
255                 gunzip -c "$f" >"/etc/console-setup/${fdec##*/}"
256             fi
257             rm -f "/etc/console-setup/${fdec##*/}.gz"
258         fi
259     done
260     for f in $LOADFONT_MAP; do
261         if \
262             [ "$save" = yes ] \
263             && [ "${f%/*}" != /etc/console-setup ]
264         then
265             cp "$f" /etc/console-setup/
266         fi
267     done
268     
269     # Due to bug in splashy and usplash: do not load fonts (#540314)
270     if \
271         pidof splashy > /dev/null \
272         || pidof usplash > /dev/null
273     then
274         LOADFONTS=''
275     fi
276
277     if [ "$save_only" != yes ] && [ "$LOADFONTS" ]; then
278         for console in $ACTIVE_CONSOLES; do
279             [ -w $console ] || continue
280             if which consolechars >/dev/null; then
281                 if [ "$bigfont" = yes ]; then
282                     echo "setupcon: The consolechars utility from the console-setup font can load only fonts with 8 pixel width matrix.  Please install the setfont utility from the kbd package." >&2
283                 fi
284                 if [ "$LOADFONT_MAP" ]; then
285                     eval consolechars -v --tty=$console -f $LOADFONTS -u "$LOADFONT_MAP" $verbose
286                 else
287                     eval consolechars -v --tty=$console -f $LOADFONTS $verbose
288                 fi
289             elif which setfont >/dev/null; then
290                 if [ "$LOADFONT_MAP" ]; then
291                     eval setfont -v -C $console $LOADFONTS -u "$LOADFONT_MAP" $verbose
292                 else
293                     eval setfont -v -C $console $LOADFONTS $verbose
294                 fi
295             fi
296         done
297     fi
298     
299     # Load the ACM
300     if [ ! -f "$CONSOLE_MAP" ]; then
301         for dir in \
302             /usr/local/share/consoletrans \
303             /usr/share/consoletrans \
304             /etc/console-setup 
305         do
306             if [ -f "$dir/$CHARMAP.acm.gz" ]; then
307                 CONSOLE_MAP="$dir/$CHARMAP.acm.gz"
308                 break
309             elif [ -f "$dir/$CHARMAP.acm" ]; then
310                 ACM="$dir/$CHARMAP.acm"
311                 break
312             fi
313         done
314     fi
315     if [ -f "$CONSOLE_MAP" ]; then
316         if \
317             [ "$save" = yes ] \
318             && [ "${CONSOLE_MAP%/*}" != /etc/console-setup ]
319         then
320             console_map_dec="${CONSOLE_MAP%.gz}"
321             if [ "$console_map_dec" = "$CONSOLE_MAP" ]; then
322                 cp "$CONSOLE_MAP" /etc/console-setup/
323             else
324                 gunzip -c "$CONSOLE_MAP" >"/etc/console-setup/${console_map_dec##*/}"
325             fi
326             rm -f "/etc/console-setup/${console_map_dec##*/}.gz"
327         fi
328     else
329         CONSOLE_MAP="$CHARMAP.acm.gz"
330     fi
331     if \
332         [ "$save_only" != yes ] && [ "$CHARMAP" != UTF-8 ] && [ "$LOADFONTS" ]
333     then
334         for console in $ACTIVE_CONSOLES; do
335             [ -w $console ] || continue
336             if which consolechars >/dev/null; then
337                 eval consolechars -v --tty=$console --acm "$CONSOLE_MAP" $verbose
338             elif which setfont >/dev/null; then
339                 eval setfont -v -C $console -m "$CONSOLE_MAP" $verbose
340             fi      
341         done
342     fi
343
344     STTY=""
345     [ -z "$SCREEN_WIDTH"  ] || STTY="$STTY cols $SCREEN_WIDTH"
346     [ -z "$SCREEN_HEIGHT" ] || STTY="$STTY rows $SCREEN_HEIGHT"
347
348     if [ -n "$STTY" ]
349     then
350         for console in $ACTIVE_CONSOLES; do
351             [ -r $console ] || continue
352             stty $STTY < $console
353         done
354     fi
355 fi
356
357 #-----------------------#
358 #        INPUT          #
359 #-----------------------#
360
361 if [ "$font_only" != yes ] && [ "$XKBMODEL" != unknown ] && \
362    [ "$ACTIVE_CONSOLES" ]; then
363     # On Mac PPC machines, we may need to set kernel vars first.  We need
364     # to mount /proc to do that, but we need it set up before sulogin may
365     # be run in checkroot, which will need the keyboard to log in...
366     # This code was borrowed from the keymap.sh script of console-common
367     # Copyright © 2001 Yann Dirson
368     # Copyright © 2001 Alcove http://www.alcove.fr/
369     if [ "$save_only" != yes ] && \
370        [ -x /sbin/sysctl ] && [ -r /etc/sysctl.conf ]; then
371         if grep -v '^\#' /etc/sysctl.conf | grep -q keycodes ; then
372             grep keycodes /etc/sysctl.conf | grep -v "^#" \
373                 | while read d ; do
374                       /sbin/sysctl -w $d 2> /dev/null || true
375                   done
376         fi
377     fi
378     
379     if [ "$save_only" != yes ]; then
380         for console in $ACTIVE_CONSOLES; do
381             [ -w $console ] || continue
382             if which kbd_mode >/dev/null; then
383                 if [ "$CHARMAP" = UTF-8 ] || [ -z "$CONSOLE_MAP" ]; then
384                     kbd_mode -u <$console
385                 else
386                     kbd_mode -a <$console
387                 fi
388             fi
389         done
390     fi
391     
392     if which loadkeys >/dev/null && [ "$XKBMODEL" != SKIP ]; then
393         if [ "$CHARMAP" != UTF-8 ]; then
394             acm_option="-charmap $CHARMAP"
395         else
396             acm_option=''
397         fi
398         
399         if [ "$XKBRULES" ]; then
400             rules_option="-rules $XKBRULES"
401         else
402             rules_option=''
403         fi
404         
405         if \
406             [ "$VARIANT" = '' ] \
407             && which gzip >/dev/null \
408             && [ -d /usr/share ] # /usr is mounted
409         then
410             if \
411                 [ ! -f /etc/console-setup/cached.kmap.gz ] \
412                 || [ /etc/console-setup/cached.kmap.gz \
413                      -ot /etc/default/keyboard ] \
414                 || [ "$save" = yes ]
415             then
416                 ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
417                     "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
418                     | gzip -9 2>/dev/null >/etc/console-setup/cached.kmap.gz
419                     # avoid warnings when /etc is mounted read-only
420             fi
421         fi
422         
423         if [ "$save_only" != yes ]; then
424             if [ "$KMAP" -a -f "$KMAP" ]; then
425                 eval loadkeys "$KMAP" $verbose
426             else                    
427                 if \
428                     [ "$VARIANT" = '' ] && [ "$CONFIG" != "$USER_CONFIG" ] \
429                     && [ -f /etc/console-setup/cached.kmap.gz ] \
430                     && [ ! /etc/console-setup/cached.kmap.gz \
431                            -ot /etc/default/keyboard ]
432                 then
433                     eval loadkeys /etc/console-setup/cached.kmap.gz $verbose
434                 else
435                     if [ -d /usr/share ]; then
436                         ckbcomp $acm_option $rules_option \
437                             -model "$XKBMODEL" \
438                             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
439                             | eval loadkeys $verbose
440                     elif [ -f /etc/console-setup/cached.kmap.gz ]; then
441                         eval loadkeys /etc/console-setup/cached.kmap.gz $verbose
442                     fi
443                 fi
444             fi
445         fi
446     fi
447 fi
448
449 exit 0