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