]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/bsdconfig/console/ttys
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / bsdconfig / console / ttys
1 #!/bin/sh
2 #-
3 # Copyright (c) 2012-2013 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 # $FreeBSD$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." "$0"
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/mustberoot.subr
36 f_include $BSDCFG_SHARE/sysrc.subr
37
38 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
39 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40
41 ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
42 [ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43
44 ############################################################ CONFIGURATION
45
46 #
47 # Location of ttys(5)
48 #
49 ETC_TTYS=/etc/ttys
50
51 ############################################################ FUNCTIONS
52
53 # dialog_menu_main
54 #
55 # Display the dialog(1)-based application main menu.
56 #
57 dialog_menu_main()
58 {
59         local prompt="$msg_ttys_menu_text"
60         local menu_list="
61                 '1 $msg_none'                '$msg_none_ttys_desc'
62                 '2 $msg_ibm_437_vga_default' 'cons25'
63                 '3 $msg_iso_8859_1'          'cons25l1'
64                 '4 $msg_iso_8859_2'          'cons25l2'
65                 '5 $msg_iso_8859_7'          'cons25l7'
66                 '6 $msg_koi8_r'              'cons25r'
67                 '7 $msg_koi8_u'              'cons25u'
68                 '8 $msg_us_ascii'            'cons25w'
69         " # END-QUOTE
70         local hline="$hline_choose_a_terminal_type"
71
72         local height width rows
73         eval f_dialog_menu_size height width rows \
74                                 \"\$DIALOG_TITLE\"     \
75                                 \"\$DIALOG_BACKTITLE\" \
76                                 \"\$prompt\"           \
77                                 \"\$hline\"            \
78                                 $menu_list
79
80         local menu_choice
81         menu_choice=$( eval $DIALOG \
82                 --title \"\$DIALOG_TITLE\"         \
83                 --backtitle \"\$DIALOG_BACKTITLE\" \
84                 --hline \"\$hline\"                \
85                 --ok-label \"\$msg_ok\"            \
86                 --cancel-label \"\$msg_cancel\"    \
87                 --menu \"\$prompt\"                \
88                 $height $width $rows               \
89                 $menu_list                         \
90                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
91         )
92         local retval=$?
93         f_dialog_menutag_store -s "$menu_choice"
94
95         if [ $retval -eq $SUCCESS ]; then
96                 local item
97                 item=$( eval f_dialog_menutag2item \
98                                 \"\$menu_choice\" $menu_list )
99                 f_dialog_menuitem_store "$item"
100         fi
101                 
102         return $retval
103 }
104
105 # ttys_set_type $consterm
106 #
107 # Set terminal type of `ttyv*' and `cons[0-9]' entries in ttys(5) to $consterm.
108 #
109 ttys_set_type()
110 {
111         local consterm="$1" err
112
113         #
114         # Create new temporary file to write our ttys(5) update with new types.
115         #
116         local tmpfile="$( mktemp -t "pgm" )"
117         [ "$tmpfile" ] || return $FAILURE
118
119         #
120         # Fixup permissions and ownership (mktemp(1) creates the temporary file
121         # with 0600 permissions -- change the permissions and ownership to
122         # match ttys(5) before we write it out and mv(1) it into place).
123         #
124         local mode="$( stat -f '%#Lp' "$ETC_TTYS" 2> /dev/null )"
125         local owner="$( stat -f '%u:%g' "$ETC_TTYS" 2> /dev/null )"
126         f_quietly chmod "${mode:-0644}" "$tmpfile"
127         f_quietly chown "${owner:-root:wheel}" "$tmpfile"
128
129         #
130         # Operate on ttys(5), replacing only the types of `ttyv*' and
131         # `cons[0-9]' terminals with the new type.
132         #
133         if ! err=$( awk -v consterm="$consterm" '
134         BEGIN {
135         }
136         {
137                 # "Skip" blank-lines, lines containing only whitespace, and
138                 # lines containing only a comment or whitespace-then-comment.
139                 #
140                 if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
141
142                 # "Skip" terminal types other than those supported
143                 #
144                 if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
145
146                 # Change the terminal type to the new value
147                 #
148                 match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
149                 if ( ! RSTART ) { print; next }
150                 left = substr($0, 0, RSTART - 1)
151                 match($0, /[[:space:]]+(on|off).*$/)
152                 right = substr($0, RSTART)
153                 printf "%s%s%s\n", left, consterm, right
154         }
155         ' "$ETC_TTYS" > "$tmpfile" 2>&1 ); then
156                 f_dialog_msgbox "$err"
157                 return $FAILURE
158         fi
159         if ! err=$( mv -f "$tmpfile" "$ETC_TTYS" 2>&1 ); then
160                 f_dialog_msgbox "$err"
161                 return $FAILURE
162         fi
163
164         return $SUCCESS
165 }
166
167 ############################################################ MAIN
168
169 # Incorporate rc-file if it exists
170 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
171
172 #
173 # Process command-line arguments
174 #
175 while getopts h$GETOPTS_STDARGS flag; do
176         case "$flag" in
177         h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
178         esac
179 done
180 shift $(( $OPTIND - 1 ))
181
182 #
183 # Initialize
184 #
185 f_dialog_title "$msg_system_console_terminal_type"
186 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
187 f_mustberoot_init
188
189 #
190 # Launch application main menu
191 #
192 dialog_menu_main || f_die
193 f_dialog_menutag_fetch mtag
194
195 [ "$mtag" = "1 $msg_none" ] && exit $SUCCESS
196
197 f_dialog_menuitem_fetch consterm
198 ttys_set_type "$consterm" || f_die
199
200 exit $SUCCESS
201
202 ################################################################################
203 # END
204 ################################################################################