3 # Copyright (c) 2012 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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 (INLUDING, 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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_include $BSDCFG_SHARE/dialog.subr
34 f_include $BSDCFG_SHARE/mustberoot.subr
35 f_include $BSDCFG_SHARE/sysrc.subr
37 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
38 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40 ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
41 [ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43 ############################################################ CONFIGURATION
50 ############################################################ GLOBALS
53 # Terminal-type map/menu-item list
56 '1 $msg_none' '$msg_none_ttys_desc'
57 '2 $msg_ibm_437_vga_default' 'cons25'
58 '3 $msg_iso_8859_1' 'cons25l1'
59 '4 $msg_iso_8859_2' 'cons25l2'
60 '5 $msg_iso_8859_7' 'cons25l7'
61 '6 $msg_koi8_r' 'cons25r'
62 '7 $msg_koi8_u' 'cons25u'
63 '8 $msg_us_ascii' 'cons25w'
66 ############################################################ FUNCTIONS
70 # Display the dialog(1)-based application main menu.
75 local hline="$hline_choose_a_terminal_type"
76 local prompt="$msg_ttys_menu_text"
78 size=$( eval f_dialog_menu_size \
80 \"\$DIALOG_BACKTITLE\" \
86 dialog_menu=$( eval $DIALOG \
87 --clear --title \"\$DIALOG_TITLE\" \
88 --backtitle \"\$DIALOG_BACKTITLE\" \
90 --ok-label \"\$msg_ok\" \
91 --cancel-label \"\$msg_cancel\" \
92 --menu \"\$prompt\" $size \
94 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
97 setvar DIALOG_MENU_$$ "$dialog_menu"
101 # ttys_set_type $consterm
103 # Set terminal type of `ttyv*' and `cons[0-9]' entries in ttys(5) to $consterm.
110 # Create new temporary file to write our ttys(5) update with new types.
112 local tmpfile="$( mktemp -t "pgm" )"
113 [ "$tmpfile" ] || return $FAILURE
116 # Fixup permissions and ownership (mktemp(1) creates the temporary file
117 # with 0600 permissions -- change the permissions and ownership to
118 # match ttys(5) before we write it out and mv(1) it into place).
120 local mode="$( stat -f '%#Lp' "$ETC_TTYS" 2> /dev/null )"
121 local owner="$( stat -f '%u:%g' "$ETC_TTYS" 2> /dev/null )"
122 f_quietly chmod "${mode:-0644}" "$tmpfile"
123 f_quietly chown "${owner:-root:wheel}" "$tmpfile"
126 # Operate on ttys(5), replacing only the types of `ttyv*' and
127 # `cons[0-9]' terminals with the new type.
129 awk -v consterm="$consterm" '
133 # "Skip" blank-lines, lines containing only whitespace, and
134 # lines containing only a comment or whitespace-then-comment.
136 if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
138 # "Skip" terminal types other than those supported
140 if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
142 # Change the terminal type to the new value
144 match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
145 if ( ! RSTART ) { print; next }
146 left = substr($0, 0, RSTART - 1)
147 match($0, /[[:space:]]+(on|off).*$/)
148 right = substr($0, RSTART)
149 printf "%s%s%s\n", left, consterm, right
151 ' "$ETC_TTYS" > "$tmpfile" || return $FAILURE
152 f_quietly mv "$tmpfile" "$ETC_TTYS" || return $FAILURE
157 ############################################################ MAIN
159 # Incorporate rc-file if it exists
160 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
163 # Process command-line arguments
165 while getopts hSX flag; do
167 h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
170 shift $(( $OPTIND - 1 ))
176 f_dialog_title "$msg_system_console_terminal_type"
177 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
181 # Launch application main menu
186 mtag=$( f_dialog_menutag )
188 [ $retval -eq 0 ] || f_die
189 [ "$mtag" = "1 $msg_none" ] && break
191 consterm=$( eval f_dialog_menutag2item \"\$mtag\" $TTY_MENU_LIST )
192 err=$( ttys_set_type "$consterm" 2>&1 )
195 f_show_msg "%s" "$err"
200 ################################################################################
202 ################################################################################