xref: /freebsd/usr.sbin/bsdconfig/console/ttys (revision fb7d723e6bb1b81ac1e34e86be13e9806247a2c3)
1641a6cfbSDevin Teske#!/bin/sh
2641a6cfbSDevin Teske#-
3641a6cfbSDevin Teske# Copyright (c) 2012 Devin Teske
4641a6cfbSDevin Teske# All Rights Reserved.
5641a6cfbSDevin Teske#
6641a6cfbSDevin Teske# Redistribution and use in source and binary forms, with or without
7641a6cfbSDevin Teske# modification, are permitted provided that the following conditions
8641a6cfbSDevin Teske# are met:
9641a6cfbSDevin Teske# 1. Redistributions of source code must retain the above copyright
10641a6cfbSDevin Teske#    notice, this list of conditions and the following disclaimer.
11641a6cfbSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12641a6cfbSDevin Teske#    notice, this list of conditions and the following disclaimer in the
13641a6cfbSDevin Teske#    documentation and/or other materials provided with the distribution.
14641a6cfbSDevin Teske#
15641a6cfbSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16641a6cfbSDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17641a6cfbSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18641a6cfbSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19641a6cfbSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20641a6cfbSDevin Teske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21641a6cfbSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22641a6cfbSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23641a6cfbSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24641a6cfbSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25641a6cfbSDevin Teske# SUCH DAMAGE.
26641a6cfbSDevin Teske#
27641a6cfbSDevin Teske# $FreeBSD$
28641a6cfbSDevin Teske#
29641a6cfbSDevin Teske############################################################ INCLUDES
30641a6cfbSDevin Teske
31ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
32ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/mustberoot.subr
35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr
36641a6cfbSDevin Teske
37ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
38641a6cfbSDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
39641a6cfbSDevin Teske
40641a6cfbSDevin Teskeipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
41641a6cfbSDevin Teske[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
42641a6cfbSDevin Teske
43641a6cfbSDevin Teske############################################################ CONFIGURATION
44641a6cfbSDevin Teske
45641a6cfbSDevin Teske#
46641a6cfbSDevin Teske# Location of ttys(5)
47641a6cfbSDevin Teske#
48641a6cfbSDevin TeskeETC_TTYS=/etc/ttys
49641a6cfbSDevin Teske
50641a6cfbSDevin Teske############################################################ GLOBALS
51641a6cfbSDevin Teske
52641a6cfbSDevin Teske#
53641a6cfbSDevin Teske# Terminal-type map/menu-item list
54641a6cfbSDevin Teske#
55641a6cfbSDevin TeskeTTY_MENU_LIST="
56641a6cfbSDevin Teske	'1 $msg_none'                '$msg_none_ttys_desc'
57641a6cfbSDevin Teske	'2 $msg_ibm_437_vga_default' 'cons25'
58641a6cfbSDevin Teske	'3 $msg_iso_8859_1'          'cons25l1'
59641a6cfbSDevin Teske	'4 $msg_iso_8859_2'          'cons25l2'
60641a6cfbSDevin Teske	'5 $msg_iso_8859_7'          'cons25l7'
61641a6cfbSDevin Teske	'6 $msg_koi8_r'              'cons25r'
62641a6cfbSDevin Teske	'7 $msg_koi8_u'              'cons25u'
63641a6cfbSDevin Teske	'8 $msg_us_ascii'            'cons25w'
64641a6cfbSDevin Teske" # END-QUOTE
65641a6cfbSDevin Teske
66641a6cfbSDevin Teske############################################################ FUNCTIONS
67641a6cfbSDevin Teske
68641a6cfbSDevin Teske# dialog_menu_main
69641a6cfbSDevin Teske#
70641a6cfbSDevin Teske# Display the dialog(1)-based application main menu.
71641a6cfbSDevin Teske#
72641a6cfbSDevin Teskedialog_menu_main()
73641a6cfbSDevin Teske{
74641a6cfbSDevin Teske	local size
75641a6cfbSDevin Teske	local hline="$hline_choose_a_terminal_type"
76641a6cfbSDevin Teske	local prompt="$msg_ttys_menu_text"
77641a6cfbSDevin Teske
78641a6cfbSDevin Teske	size=$( eval f_dialog_menu_size \
79641a6cfbSDevin Teske	        	\"\$DIALOG_TITLE\"     \
80641a6cfbSDevin Teske	        	\"\$DIALOG_BACKTITLE\" \
81641a6cfbSDevin Teske	                \"\$prompt\"           \
82641a6cfbSDevin Teske	        	\"\$hline\"            \
83641a6cfbSDevin Teske	        	$TTY_MENU_LIST         )
84641a6cfbSDevin Teske
8589498fdfSDevin Teske	local dialog_menu
8689498fdfSDevin Teske	dialog_menu=$( eval $DIALOG \
87641a6cfbSDevin Teske		--clear --title \"\$DIALOG_TITLE\" \
88641a6cfbSDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\" \
89641a6cfbSDevin Teske		--hline \"\$hline\"                \
90641a6cfbSDevin Teske		--ok-label \"\$msg_ok\"            \
91641a6cfbSDevin Teske		--cancel-label \"\$msg_cancel\"    \
92641a6cfbSDevin Teske		--menu \"\$prompt\" $size          \
93641a6cfbSDevin Teske		$TTY_MENU_LIST                     \
9489498fdfSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
9589498fdfSDevin Teske	)
9689498fdfSDevin Teske	local retval=$?
9789498fdfSDevin Teske	setvar DIALOG_MENU_$$ "$dialog_menu"
9889498fdfSDevin Teske	return $retval
99641a6cfbSDevin Teske}
100641a6cfbSDevin Teske
101641a6cfbSDevin Teske# ttys_set_type $consterm
102641a6cfbSDevin Teske#
103641a6cfbSDevin Teske# Set terminal type of `ttyv*' and `cons[0-9]' entries in ttys(5) to $consterm.
104641a6cfbSDevin Teske#
105641a6cfbSDevin Teskettys_set_type()
106641a6cfbSDevin Teske{
107641a6cfbSDevin Teske	local consterm="$1"
108641a6cfbSDevin Teske
109641a6cfbSDevin Teske	#
110641a6cfbSDevin Teske	# Create new temporary file to write our ttys(5) update with new types.
111641a6cfbSDevin Teske	#
112641a6cfbSDevin Teske	local tmpfile="$( mktemp -t "pgm" )"
113641a6cfbSDevin Teske	[ "$tmpfile" ] || return $FAILURE
114641a6cfbSDevin Teske
115641a6cfbSDevin Teske	#
116641a6cfbSDevin Teske	# Fixup permissions and ownership (mktemp(1) creates the temporary file
117641a6cfbSDevin Teske	# with 0600 permissions -- change the permissions and ownership to
118641a6cfbSDevin Teske	# match ttys(5) before we write it out and mv(1) it into place).
119641a6cfbSDevin Teske	#
120*fb7d723eSDevin Teske	local mode="$( stat -f '%#Lp' "$ETC_TTYS" 2> /dev/null )"
121*fb7d723eSDevin Teske	local owner="$( stat -f '%u:%g' "$ETC_TTYS" 2> /dev/null )"
122641a6cfbSDevin Teske	f_quietly chmod "${mode:-0644}" "$tmpfile"
123641a6cfbSDevin Teske	f_quietly chown "${owner:-root:wheel}" "$tmpfile"
124641a6cfbSDevin Teske
125641a6cfbSDevin Teske	#
126641a6cfbSDevin Teske	# Operate on ttys(5), replacing only the types of `ttyv*' and
127641a6cfbSDevin Teske	# `cons[0-9]' terminals with the new type.
128641a6cfbSDevin Teske	#
129641a6cfbSDevin Teske	awk -v consterm="$consterm" '
130641a6cfbSDevin Teske	BEGIN {
131641a6cfbSDevin Teske	}
132641a6cfbSDevin Teske	{
133641a6cfbSDevin Teske		# "Skip" blank-lines, lines containing only whitespace, and
134641a6cfbSDevin Teske		# lines containing only a comment or whitespace-then-comment.
135641a6cfbSDevin Teske		#
136641a6cfbSDevin Teske		if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
137641a6cfbSDevin Teske
138641a6cfbSDevin Teske		# "Skip" terminal types other than those supported
139641a6cfbSDevin Teske		#
140641a6cfbSDevin Teske		if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
141641a6cfbSDevin Teske
142641a6cfbSDevin Teske		# Change the terminal type to the new value
143641a6cfbSDevin Teske		#
144641a6cfbSDevin Teske		match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
145641a6cfbSDevin Teske		if ( ! RSTART ) { print; next }
146641a6cfbSDevin Teske		left = substr($0, 0, RSTART - 1)
147641a6cfbSDevin Teske		match($0, /[[:space:]]+(on|off).*$/)
148641a6cfbSDevin Teske		right = substr($0, RSTART)
149641a6cfbSDevin Teske		printf "%s%s%s\n", left, consterm, right
150641a6cfbSDevin Teske	}
151641a6cfbSDevin Teske	' "$ETC_TTYS" > "$tmpfile" || return $FAILURE
152641a6cfbSDevin Teske	f_quietly mv "$tmpfile" "$ETC_TTYS" || return $FAILURE
153641a6cfbSDevin Teske
154641a6cfbSDevin Teske	return $SUCCESS
155641a6cfbSDevin Teske}
156641a6cfbSDevin Teske
157641a6cfbSDevin Teske############################################################ MAIN
158641a6cfbSDevin Teske
159641a6cfbSDevin Teske# Incorporate rc-file if it exists
160641a6cfbSDevin Teske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
161641a6cfbSDevin Teske
162641a6cfbSDevin Teske#
163641a6cfbSDevin Teske# Process command-line arguments
164641a6cfbSDevin Teske#
165641a6cfbSDevin Teskewhile getopts hSX flag; do
166641a6cfbSDevin Teske	case "$flag" in
167641a6cfbSDevin Teske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
168641a6cfbSDevin Teske	esac
169641a6cfbSDevin Teskedone
170641a6cfbSDevin Teskeshift $(( $OPTIND - 1 ))
171641a6cfbSDevin Teske
172641a6cfbSDevin Teske#
173641a6cfbSDevin Teske# Initialize
174641a6cfbSDevin Teske#
175641a6cfbSDevin Teskef_dialog_init
176641a6cfbSDevin Teskef_dialog_title "$msg_system_console_terminal_type"
177641a6cfbSDevin Teskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
178641a6cfbSDevin Teskef_mustberoot_init
179641a6cfbSDevin Teske
180641a6cfbSDevin Teske#
181641a6cfbSDevin Teske# Launch application main menu
182641a6cfbSDevin Teske#
183641a6cfbSDevin Teskewhile :; do
184641a6cfbSDevin Teske	dialog_menu_main
185641a6cfbSDevin Teske	retval=$?
186641a6cfbSDevin Teske	mtag=$( f_dialog_menutag )
187641a6cfbSDevin Teske
188641a6cfbSDevin Teske	[ $retval -eq 0 ] || f_die
189641a6cfbSDevin Teske	[ "$mtag" = "1 $msg_none" ] && break
190641a6cfbSDevin Teske
191641a6cfbSDevin Teske	consterm=$( eval f_dialog_menutag2item \"\$mtag\" $TTY_MENU_LIST )
192641a6cfbSDevin Teske	err=$( ttys_set_type "$consterm" 2>&1 )
193641a6cfbSDevin Teske	[ "$err" ] || break
194641a6cfbSDevin Teske
195641a6cfbSDevin Teske	f_show_msg "%s" "$err"
196641a6cfbSDevin Teskedone
197641a6cfbSDevin Teske
198641a6cfbSDevin Teskeexit $SUCCESS
199641a6cfbSDevin Teske
200641a6cfbSDevin Teske################################################################################
201641a6cfbSDevin Teske# END
202641a6cfbSDevin Teske################################################################################
203