ttys revision 249746
1160814Ssimon#!/bin/sh
2160814Ssimon#-
3160814Ssimon# Copyright (c) 2012-2013 Devin Teske
4160814Ssimon# All Rights Reserved.
5160814Ssimon#
6160814Ssimon# Redistribution and use in source and binary forms, with or without
7160814Ssimon# modification, are permitted provided that the following conditions
8160814Ssimon# are met:
9160814Ssimon# 1. Redistributions of source code must retain the above copyright
10160814Ssimon#    notice, this list of conditions and the following disclaimer.
11160814Ssimon# 2. Redistributions in binary form must reproduce the above copyright
12160814Ssimon#    notice, this list of conditions and the following disclaimer in the
13160814Ssimon#    documentation and/or other materials provided with the distribution.
14160814Ssimon#
15160814Ssimon# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16160814Ssimon# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17160814Ssimon# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18160814Ssimon# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19160814Ssimon# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20160814Ssimon# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21160814Ssimon# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22160814Ssimon# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23160814Ssimon# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24160814Ssimon# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25160814Ssimon# SUCH DAMAGE.
26160814Ssimon#
27160814Ssimon# $FreeBSD: head/usr.sbin/bsdconfig/console/ttys 249746 2013-04-22 05:02:34Z dteske $
28160814Ssimon#
29160814Ssimon############################################################ INCLUDES
30160814Ssimon
31160814SsimonBSDCFG_SHARE="/usr/share/bsdconfig"
32160814Ssimon. $BSDCFG_SHARE/common.subr || exit 1
33160814Ssimonf_dprintf "%s: loading includes..." "$0"
34160814Ssimonf_include $BSDCFG_SHARE/dialog.subr
35160814Ssimonf_include $BSDCFG_SHARE/mustberoot.subr
36160814Ssimonf_include $BSDCFG_SHARE/sysrc.subr
37160814Ssimon
38160814SsimonBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
39160814Ssimonf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40160814Ssimon
41160814Ssimonipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
42160814Ssimon[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43160814Ssimon
44160814Ssimon############################################################ CONFIGURATION
45160814Ssimon
46160814Ssimon#
47160814Ssimon# Location of ttys(5)
48160814Ssimon#
49160814SsimonETC_TTYS=/etc/ttys
50160814Ssimon
51160814Ssimon############################################################ GLOBALS
52160814Ssimon
53160814Ssimon#
54160814Ssimon# Terminal-type map/menu-item list
55160814Ssimon#
56160814SsimonTTY_MENU_LIST="
57160814Ssimon	'1 $msg_none'                '$msg_none_ttys_desc'
58160814Ssimon	'2 $msg_ibm_437_vga_default' 'cons25'
59160814Ssimon	'3 $msg_iso_8859_1'          'cons25l1'
60160814Ssimon	'4 $msg_iso_8859_2'          'cons25l2'
61160814Ssimon	'5 $msg_iso_8859_7'          'cons25l7'
62160814Ssimon	'6 $msg_koi8_r'              'cons25r'
63160814Ssimon	'7 $msg_koi8_u'              'cons25u'
64160814Ssimon	'8 $msg_us_ascii'            'cons25w'
65160814Ssimon" # END-QUOTE
66160814Ssimon
67160814Ssimon############################################################ FUNCTIONS
68160814Ssimon
69160814Ssimon# dialog_menu_main
70160814Ssimon#
71160814Ssimon# Display the dialog(1)-based application main menu.
72160814Ssimon#
73160814Ssimondialog_menu_main()
74160814Ssimon{
75160814Ssimon	local size
76160814Ssimon	local hline="$hline_choose_a_terminal_type"
77160814Ssimon	local prompt="$msg_ttys_menu_text"
78160814Ssimon
79160814Ssimon	size=$( eval f_dialog_menu_size \
80160814Ssimon	        	\"\$DIALOG_TITLE\"     \
81160814Ssimon	        	\"\$DIALOG_BACKTITLE\" \
82160814Ssimon	                \"\$prompt\"           \
83160814Ssimon	        	\"\$hline\"            \
84160814Ssimon	        	$TTY_MENU_LIST         )
85160814Ssimon
86160814Ssimon	local dialog_menu
87160814Ssimon	dialog_menu=$( eval $DIALOG \
88160814Ssimon		--clear --title \"\$DIALOG_TITLE\" \
89160814Ssimon		--backtitle \"\$DIALOG_BACKTITLE\" \
90160814Ssimon		--hline \"\$hline\"                \
91160814Ssimon		--ok-label \"\$msg_ok\"            \
92160814Ssimon		--cancel-label \"\$msg_cancel\"    \
93160814Ssimon		--menu \"\$prompt\" $size          \
94160814Ssimon		$TTY_MENU_LIST                     \
95160814Ssimon		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
96160814Ssimon	)
97160814Ssimon	local retval=$?
98160814Ssimon	setvar DIALOG_MENU_$$ "$dialog_menu"
99	return $retval
100}
101
102# ttys_set_type $consterm
103#
104# Set terminal type of `ttyv*' and `cons[0-9]' entries in ttys(5) to $consterm.
105#
106ttys_set_type()
107{
108	local consterm="$1"
109
110	#
111	# Create new temporary file to write our ttys(5) update with new types.
112	#
113	local tmpfile="$( mktemp -t "pgm" )"
114	[ "$tmpfile" ] || return $FAILURE
115
116	#
117	# Fixup permissions and ownership (mktemp(1) creates the temporary file
118	# with 0600 permissions -- change the permissions and ownership to
119	# match ttys(5) before we write it out and mv(1) it into place).
120	#
121	local mode="$( stat -f '%#Lp' "$ETC_TTYS" 2> /dev/null )"
122	local owner="$( stat -f '%u:%g' "$ETC_TTYS" 2> /dev/null )"
123	f_quietly chmod "${mode:-0644}" "$tmpfile"
124	f_quietly chown "${owner:-root:wheel}" "$tmpfile"
125
126	#
127	# Operate on ttys(5), replacing only the types of `ttyv*' and
128	# `cons[0-9]' terminals with the new type.
129	#
130	awk -v consterm="$consterm" '
131	BEGIN {
132	}
133	{
134		# "Skip" blank-lines, lines containing only whitespace, and
135		# lines containing only a comment or whitespace-then-comment.
136		#
137		if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
138
139		# "Skip" terminal types other than those supported
140		#
141		if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
142
143		# Change the terminal type to the new value
144		#
145		match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
146		if ( ! RSTART ) { print; next }
147		left = substr($0, 0, RSTART - 1)
148		match($0, /[[:space:]]+(on|off).*$/)
149		right = substr($0, RSTART)
150		printf "%s%s%s\n", left, consterm, right
151	}
152	' "$ETC_TTYS" > "$tmpfile" || return $FAILURE
153	f_quietly mv "$tmpfile" "$ETC_TTYS" || return $FAILURE
154
155	return $SUCCESS
156}
157
158############################################################ MAIN
159
160# Incorporate rc-file if it exists
161[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
162
163#
164# Process command-line arguments
165#
166while getopts dD:hSX flag; do
167	case "$flag" in
168	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
169	esac
170done
171shift $(( $OPTIND - 1 ))
172
173#
174# Initialize
175#
176f_dialog_title "$msg_system_console_terminal_type"
177f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
178f_mustberoot_init
179
180#
181# Launch application main menu
182#
183while :; do
184	dialog_menu_main
185	retval=$?
186	mtag=$( f_dialog_menutag )
187
188	[ $retval -eq 0 ] || f_die
189	[ "$mtag" = "1 $msg_none" ] && break
190
191	consterm=$( eval f_dialog_menutag2item \"\$mtag\" $TTY_MENU_LIST )
192	err=$( ttys_set_type "$consterm" 2>&1 )
193	[ "$err" ] || break
194
195	f_dialog_msgbox "$err"
196done
197
198exit $SUCCESS
199
200################################################################################
201# END
202################################################################################
203