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