1238438Sdteske#!/bin/sh
2238438Sdteske#-
3249746Sdteske# Copyright (c) 2012-2013 Devin Teske
4252980Sdteske# All rights reserved.
5238438Sdteske#
6238438Sdteske# Redistribution and use in source and binary forms, with or without
7238438Sdteske# modification, are permitted provided that the following conditions
8238438Sdteske# are met:
9238438Sdteske# 1. Redistributions of source code must retain the above copyright
10238438Sdteske#    notice, this list of conditions and the following disclaimer.
11238438Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12238438Sdteske#    notice, this list of conditions and the following disclaimer in the
13238438Sdteske#    documentation and/or other materials provided with the distribution.
14238438Sdteske#
15238438Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16252987Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17238438Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18238438Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19238438Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20252987Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21238438Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22238438Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23238438Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24238438Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25238438Sdteske# SUCH DAMAGE.
26238438Sdteske#
27238438Sdteske# $FreeBSD$
28238438Sdteske#
29238438Sdteske############################################################ INCLUDES
30238438Sdteske
31240684SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
32240684Sdteske. $BSDCFG_SHARE/common.subr || exit 1
33244675Sdteskef_dprintf "%s: loading includes..." "$0"
34240684Sdteskef_include $BSDCFG_SHARE/dialog.subr
35240684Sdteskef_include $BSDCFG_SHARE/mustberoot.subr
36240684Sdteskef_include $BSDCFG_SHARE/sysrc.subr
37238438Sdteske
38240684SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
39238438Sdteskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40238438Sdteske
41243112Sdteskeipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
42238438Sdteske[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43238438Sdteske
44238438Sdteske############################################################ CONFIGURATION
45238438Sdteske
46238438Sdteske#
47238438Sdteske# Location of ttys(5)
48238438Sdteske#
49238438SdteskeETC_TTYS=/etc/ttys
50238438Sdteske
51238438Sdteske############################################################ FUNCTIONS
52238438Sdteske
53238438Sdteske# dialog_menu_main
54238438Sdteske#
55238438Sdteske# Display the dialog(1)-based application main menu.
56238438Sdteske#
57238438Sdteskedialog_menu_main()
58238438Sdteske{
59251264Sdteske	local prompt="$msg_ttys_menu_text"
60251361Sdteske	local menu_list="
61251361Sdteske		'1 $msg_none'                '$msg_none_ttys_desc'
62251361Sdteske		'2 $msg_ibm_437_vga_default' 'cons25'
63251361Sdteske		'3 $msg_iso_8859_1'          'cons25l1'
64251361Sdteske		'4 $msg_iso_8859_2'          'cons25l2'
65251361Sdteske		'5 $msg_iso_8859_7'          'cons25l7'
66251361Sdteske		'6 $msg_koi8_r'              'cons25r'
67251361Sdteske		'7 $msg_koi8_u'              'cons25u'
68251361Sdteske		'8 $msg_us_ascii'            'cons25w'
69251361Sdteske	" # END-QUOTE
70238438Sdteske	local hline="$hline_choose_a_terminal_type"
71238438Sdteske
72251190Sdteske	local height width rows
73251190Sdteske	eval f_dialog_menu_size height width rows \
74251190Sdteske	                        \"\$DIALOG_TITLE\"     \
75251190Sdteske	                        \"\$DIALOG_BACKTITLE\" \
76251190Sdteske	                        \"\$prompt\"           \
77251190Sdteske	                        \"\$hline\"            \
78251361Sdteske	                        $menu_list
79238438Sdteske
80251236Sdteske	local menu_choice
81251236Sdteske	menu_choice=$( eval $DIALOG \
82249751Sdteske		--title \"\$DIALOG_TITLE\"         \
83238438Sdteske		--backtitle \"\$DIALOG_BACKTITLE\" \
84238438Sdteske		--hline \"\$hline\"                \
85238438Sdteske		--ok-label \"\$msg_ok\"            \
86238438Sdteske		--cancel-label \"\$msg_cancel\"    \
87251190Sdteske		--menu \"\$prompt\"                \
88251190Sdteske		$height $width $rows               \
89251552Sdteske		$menu_list                         \
90240768Sdteske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
91240768Sdteske	)
92240768Sdteske	local retval=$?
93251236Sdteske	f_dialog_menutag_store -s "$menu_choice"
94251361Sdteske
95256181Sdteske	if [ $retval -eq $DIALOG_OK ]; then
96251361Sdteske		local item
97251361Sdteske		item=$( eval f_dialog_menutag2item \
98251361Sdteske		        	\"\$menu_choice\" $menu_list )
99251361Sdteske		f_dialog_menuitem_store "$item"
100251361Sdteske	fi
101251361Sdteske		
102240768Sdteske	return $retval
103238438Sdteske}
104238438Sdteske
105238438Sdteske# ttys_set_type $consterm
106238438Sdteske#
107238438Sdteske# Set terminal type of `ttyv*' and `cons[0-9]' entries in ttys(5) to $consterm.
108238438Sdteske#
109238438Sdteskettys_set_type()
110238438Sdteske{
111251554Sdteske	local consterm="$1" err
112238438Sdteske
113238438Sdteske	#
114238438Sdteske	# Create new temporary file to write our ttys(5) update with new types.
115238438Sdteske	#
116238438Sdteske	local tmpfile="$( mktemp -t "pgm" )"
117238438Sdteske	[ "$tmpfile" ] || return $FAILURE
118238438Sdteske
119238438Sdteske	#
120238438Sdteske	# Fixup permissions and ownership (mktemp(1) creates the temporary file
121238438Sdteske	# with 0600 permissions -- change the permissions and ownership to
122238438Sdteske	# match ttys(5) before we write it out and mv(1) it into place).
123238438Sdteske	#
124240783Sdteske	local mode="$( stat -f '%#Lp' "$ETC_TTYS" 2> /dev/null )"
125240783Sdteske	local owner="$( stat -f '%u:%g' "$ETC_TTYS" 2> /dev/null )"
126238438Sdteske	f_quietly chmod "${mode:-0644}" "$tmpfile"
127238438Sdteske	f_quietly chown "${owner:-root:wheel}" "$tmpfile"
128238438Sdteske
129238438Sdteske	#
130238438Sdteske	# Operate on ttys(5), replacing only the types of `ttyv*' and
131238438Sdteske	# `cons[0-9]' terminals with the new type.
132238438Sdteske	#
133251554Sdteske	if ! err=$( awk -v consterm="$consterm" '
134238438Sdteske	BEGIN {
135238438Sdteske	}
136238438Sdteske	{
137238438Sdteske		# "Skip" blank-lines, lines containing only whitespace, and
138238438Sdteske		# lines containing only a comment or whitespace-then-comment.
139238438Sdteske		#
140238438Sdteske		if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
141238438Sdteske
142238438Sdteske		# "Skip" terminal types other than those supported
143238438Sdteske		#
144238438Sdteske		if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
145238438Sdteske
146238438Sdteske		# Change the terminal type to the new value
147238438Sdteske		#
148238438Sdteske		match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
149238438Sdteske		if ( ! RSTART ) { print; next }
150238438Sdteske		left = substr($0, 0, RSTART - 1)
151238438Sdteske		match($0, /[[:space:]]+(on|off).*$/)
152238438Sdteske		right = substr($0, RSTART)
153238438Sdteske		printf "%s%s%s\n", left, consterm, right
154238438Sdteske	}
155251554Sdteske	' "$ETC_TTYS" > "$tmpfile" 2>&1 ); then
156251554Sdteske		f_dialog_msgbox "$err"
157251554Sdteske		return $FAILURE
158251554Sdteske	fi
159251554Sdteske	if ! err=$( mv -f "$tmpfile" "$ETC_TTYS" 2>&1 ); then
160251554Sdteske		f_dialog_msgbox "$err"
161251554Sdteske		return $FAILURE
162251554Sdteske	fi
163238438Sdteske
164238438Sdteske	return $SUCCESS
165238438Sdteske}
166238438Sdteske
167238438Sdteske############################################################ MAIN
168238438Sdteske
169238438Sdteske# Incorporate rc-file if it exists
170238438Sdteske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
171238438Sdteske
172238438Sdteske#
173238438Sdteske# Process command-line arguments
174238438Sdteske#
175250633Sdteskewhile getopts h$GETOPTS_STDARGS flag; do
176238438Sdteske	case "$flag" in
177252178Sdteske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
178238438Sdteske	esac
179238438Sdteskedone
180238438Sdteskeshift $(( $OPTIND - 1 ))
181238438Sdteske
182238438Sdteske#
183238438Sdteske# Initialize
184238438Sdteske#
185238438Sdteskef_dialog_title "$msg_system_console_terminal_type"
186238438Sdteskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
187238438Sdteskef_mustberoot_init
188238438Sdteske
189238438Sdteske#
190238438Sdteske# Launch application main menu
191238438Sdteske#
192251905Sdteskedialog_menu_main || f_die
193251905Sdteskef_dialog_menutag_fetch mtag
194238438Sdteske
195251905Sdteske[ "$mtag" = "1 $msg_none" ] && exit $SUCCESS
196238438Sdteske
197251905Sdteskef_dialog_menuitem_fetch consterm
198251905Sdteskettys_set_type "$consterm" || f_die
199238438Sdteske
200238438Sdteskeexit $SUCCESS
201238438Sdteske
202238438Sdteske################################################################################
203238438Sdteske# END
204238438Sdteske################################################################################
205