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