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
41260678Sdteskef_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
42260678Sdteske	pgm="${ipgm:-$pgm}"
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{
111260678Sdteske	local funcname=ttys_set_type
112251554Sdteske	local consterm="$1" err
113238438Sdteske
114238438Sdteske	#
115238438Sdteske	# Create new temporary file to write our ttys(5) update with new types.
116238438Sdteske	#
117260678Sdteske	local tmpfile
118260678Sdteske	f_eval_catch -k tmpfile $funcname mktemp 'mktemp -t "%s"' "$pgm" ||
119260678Sdteske		return $FAILURE
120238438Sdteske
121238438Sdteske	#
122238438Sdteske	# Fixup permissions and ownership (mktemp(1) creates the temporary file
123238438Sdteske	# with 0600 permissions -- change the permissions and ownership to
124238438Sdteske	# match ttys(5) before we write it out and mv(1) it into place).
125238438Sdteske	#
126260678Sdteske	local mode owner
127260678Sdteske	f_eval_catch -dk mode $funcname stat \
128260678Sdteske		'stat -f "%%#Lp" "%s"' "$ETC_TTYS" || mode=0644
129260678Sdteske	f_eval_catch -dk owner $funcname stat \
130260678Sdteske		'stat -f "%%u:%%g" "%s"' "$ETC_TTYS" || owner="root:wheel"
131260678Sdteske	f_eval_catch -d $funcname chmod 'chmod "%s" "%s"' "$mode" "$tmpfile"
132260678Sdteske	f_eval_catch -d $funcname chown 'chown "%s" "%s"' "$owner" "$tmpfile"
133238438Sdteske
134238438Sdteske	#
135238438Sdteske	# Operate on ttys(5), replacing only the types of `ttyv*' and
136238438Sdteske	# `cons[0-9]' terminals with the new type.
137238438Sdteske	#
138251554Sdteske	if ! err=$( awk -v consterm="$consterm" '
139238438Sdteske	BEGIN {
140238438Sdteske	}
141238438Sdteske	{
142238438Sdteske		# "Skip" blank-lines, lines containing only whitespace, and
143238438Sdteske		# lines containing only a comment or whitespace-then-comment.
144238438Sdteske		#
145238438Sdteske		if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
146238438Sdteske
147238438Sdteske		# "Skip" terminal types other than those supported
148238438Sdteske		#
149238438Sdteske		if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
150238438Sdteske
151238438Sdteske		# Change the terminal type to the new value
152238438Sdteske		#
153238438Sdteske		match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
154238438Sdteske		if ( ! RSTART ) { print; next }
155238438Sdteske		left = substr($0, 0, RSTART - 1)
156238438Sdteske		match($0, /[[:space:]]+(on|off).*$/)
157238438Sdteske		right = substr($0, RSTART)
158238438Sdteske		printf "%s%s%s\n", left, consterm, right
159238438Sdteske	}
160251554Sdteske	' "$ETC_TTYS" > "$tmpfile" 2>&1 ); then
161251554Sdteske		f_dialog_msgbox "$err"
162251554Sdteske		return $FAILURE
163251554Sdteske	fi
164260678Sdteske	f_eval_catch $funcname mv 'mv -f "%s" "%s"' "$tmpfile" "$ETC_TTYS" ||
165251554Sdteske		return $FAILURE
166238438Sdteske
167238438Sdteske	return $SUCCESS
168238438Sdteske}
169238438Sdteske
170238438Sdteske############################################################ MAIN
171238438Sdteske
172238438Sdteske# Incorporate rc-file if it exists
173238438Sdteske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
174238438Sdteske
175238438Sdteske#
176238438Sdteske# Process command-line arguments
177238438Sdteske#
178250633Sdteskewhile getopts h$GETOPTS_STDARGS flag; do
179238438Sdteske	case "$flag" in
180252178Sdteske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
181238438Sdteske	esac
182238438Sdteskedone
183238438Sdteskeshift $(( $OPTIND - 1 ))
184238438Sdteske
185238438Sdteske#
186238438Sdteske# Initialize
187238438Sdteske#
188238438Sdteskef_dialog_title "$msg_system_console_terminal_type"
189238438Sdteskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
190238438Sdteskef_mustberoot_init
191238438Sdteske
192238438Sdteske#
193238438Sdteske# Launch application main menu
194238438Sdteske#
195251905Sdteskedialog_menu_main || f_die
196251905Sdteskef_dialog_menutag_fetch mtag
197238438Sdteske
198251905Sdteske[ "$mtag" = "1 $msg_none" ] && exit $SUCCESS
199238438Sdteske
200251905Sdteskef_dialog_menuitem_fetch consterm
201251905Sdteskettys_set_type "$consterm" || f_die
202238438Sdteske
203238438Sdteskeexit $SUCCESS
204238438Sdteske
205238438Sdteske################################################################################
206238438Sdteske# END
207238438Sdteske################################################################################
208