ttys revision 259054
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: head/usr.sbin/bsdconfig/console/ttys 259054 2013-12-07 00:31:01Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/mustberoot.subr
36f_include $BSDCFG_SHARE/sysrc.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
39f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40
41f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
42	pgm="${ipgm:-$pgm}"
43
44############################################################ CONFIGURATION
45
46#
47# Location of ttys(5)
48#
49ETC_TTYS=/etc/ttys
50
51############################################################ FUNCTIONS
52
53# dialog_menu_main
54#
55# Display the dialog(1)-based application main menu.
56#
57dialog_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 $DIALOG_OK ]; 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#
109ttys_set_type()
110{
111	local funcname=ttys_set_type
112	local consterm="$1" err
113
114	#
115	# Create new temporary file to write our ttys(5) update with new types.
116	#
117	local tmpfile
118	f_eval_catch -k tmpfile $funcname mktemp 'mktemp -t "%s"' "$pgm" ||
119		return $FAILURE
120
121	#
122	# Fixup permissions and ownership (mktemp(1) creates the temporary file
123	# with 0600 permissions -- change the permissions and ownership to
124	# match ttys(5) before we write it out and mv(1) it into place).
125	#
126	local mode owner
127	f_eval_catch -dk mode $funcname stat \
128		'stat -f "%%#Lp" "%s"' "$ETC_TTYS" || mode=0644
129	f_eval_catch -dk owner $funcname stat \
130		'stat -f "%%u:%%g" "%s"' "$ETC_TTYS" || owner="root:wheel"
131	f_eval_catch -d $funcname chmod 'chmod "%s" "%s"' "$mode" "$tmpfile"
132	f_eval_catch -d $funcname chown 'chown "%s" "%s"' "$owner" "$tmpfile"
133
134	#
135	# Operate on ttys(5), replacing only the types of `ttyv*' and
136	# `cons[0-9]' terminals with the new type.
137	#
138	if ! err=$( awk -v consterm="$consterm" '
139	BEGIN {
140	}
141	{
142		# "Skip" blank-lines, lines containing only whitespace, and
143		# lines containing only a comment or whitespace-then-comment.
144		#
145		if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
146
147		# "Skip" terminal types other than those supported
148		#
149		if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
150
151		# Change the terminal type to the new value
152		#
153		match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
154		if ( ! RSTART ) { print; next }
155		left = substr($0, 0, RSTART - 1)
156		match($0, /[[:space:]]+(on|off).*$/)
157		right = substr($0, RSTART)
158		printf "%s%s%s\n", left, consterm, right
159	}
160	' "$ETC_TTYS" > "$tmpfile" 2>&1 ); then
161		f_dialog_msgbox "$err"
162		return $FAILURE
163	fi
164	f_eval_catch $funcname mv 'mv -f "%s" "%s"' "$tmpfile" "$ETC_TTYS" ||
165		return $FAILURE
166
167	return $SUCCESS
168}
169
170############################################################ MAIN
171
172# Incorporate rc-file if it exists
173[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
174
175#
176# Process command-line arguments
177#
178while getopts h$GETOPTS_STDARGS flag; do
179	case "$flag" in
180	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
181	esac
182done
183shift $(( $OPTIND - 1 ))
184
185#
186# Initialize
187#
188f_dialog_title "$msg_system_console_terminal_type"
189f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
190f_mustberoot_init
191
192#
193# Launch application main menu
194#
195dialog_menu_main || f_die
196f_dialog_menutag_fetch mtag
197
198[ "$mtag" = "1 $msg_none" ] && exit $SUCCESS
199
200f_dialog_menuitem_fetch consterm
201ttys_set_type "$consterm" || f_die
202
203exit $SUCCESS
204
205################################################################################
206# END
207################################################################################
208