ttys revision 251264
1145256Sjkoshy#!/bin/sh
2183266Sjkoshy#-
3174395Sjkoshy# Copyright (c) 2012-2013 Devin Teske
4145256Sjkoshy# All Rights Reserved.
5145256Sjkoshy#
6174395Sjkoshy# Redistribution and use in source and binary forms, with or without
7174395Sjkoshy# modification, are permitted provided that the following conditions
8174395Sjkoshy# are met:
9145256Sjkoshy# 1. Redistributions of source code must retain the above copyright
10145256Sjkoshy#    notice, this list of conditions and the following disclaimer.
11145256Sjkoshy# 2. Redistributions in binary form must reproduce the above copyright
12145256Sjkoshy#    notice, this list of conditions and the following disclaimer in the
13145256Sjkoshy#    documentation and/or other materials provided with the distribution.
14145256Sjkoshy#
15145256Sjkoshy# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16145256Sjkoshy# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17145256Sjkoshy# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18145256Sjkoshy# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19145256Sjkoshy# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20145256Sjkoshy# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21145256Sjkoshy# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22145256Sjkoshy# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23145256Sjkoshy# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24145256Sjkoshy# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25145256Sjkoshy# SUCH DAMAGE.
26145256Sjkoshy#
27145256Sjkoshy# $FreeBSD: head/usr.sbin/bsdconfig/console/ttys 251264 2013-06-02 20:02:50Z dteske $
28145256Sjkoshy#
29145256Sjkoshy############################################################ INCLUDES
30145256Sjkoshy
31145256SjkoshyBSDCFG_SHARE="/usr/share/bsdconfig"
32145256Sjkoshy. $BSDCFG_SHARE/common.subr || exit 1
33145256Sjkoshyf_dprintf "%s: loading includes..." "$0"
34145256Sjkoshyf_include $BSDCFG_SHARE/dialog.subr
35145256Sjkoshyf_include $BSDCFG_SHARE/mustberoot.subr
36145256Sjkoshyf_include $BSDCFG_SHARE/sysrc.subr
37145256Sjkoshy
38145256SjkoshyBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
39147191Sjkoshyf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40145256Sjkoshy
41145256Sjkoshyipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
42145256Sjkoshy[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43145256Sjkoshy
44201151Sjkoshy############################################################ CONFIGURATION
45145256Sjkoshy
46145256Sjkoshy#
47145256Sjkoshy# Location of ttys(5)
48147191Sjkoshy#
49164033SrwatsonETC_TTYS=/etc/ttys
50145256Sjkoshy
51145256Sjkoshy############################################################ GLOBALS
52147191Sjkoshy
53248084Sattilio#
54145256Sjkoshy# Terminal-type map/menu-item list
55145256Sjkoshy#
56145256SjkoshyTTY_MENU_LIST="
57145256Sjkoshy	'1 $msg_none'                '$msg_none_ttys_desc'
58145256Sjkoshy	'2 $msg_ibm_437_vga_default' 'cons25'
59145256Sjkoshy	'3 $msg_iso_8859_1'          'cons25l1'
60145256Sjkoshy	'4 $msg_iso_8859_2'          'cons25l2'
61147191Sjkoshy	'5 $msg_iso_8859_7'          'cons25l7'
62145256Sjkoshy	'6 $msg_koi8_r'              'cons25r'
63157144Sjkoshy	'7 $msg_koi8_u'              'cons25u'
64157144Sjkoshy	'8 $msg_us_ascii'            'cons25w'
65147191Sjkoshy" # END-QUOTE
66145256Sjkoshy
67145256Sjkoshy############################################################ FUNCTIONS
68201021Sjkoshy
69201021Sjkoshy# dialog_menu_main
70201021Sjkoshy#
71201021Sjkoshy# Display the dialog(1)-based application main menu.
72201021Sjkoshy#
73201021Sjkoshydialog_menu_main()
74233628Sfabient{
75233628Sfabient	local prompt="$msg_ttys_menu_text"
76145256Sjkoshy	local hline="$hline_choose_a_terminal_type"
77145256Sjkoshy
78145256Sjkoshy	local height width rows
79145256Sjkoshy	eval f_dialog_menu_size height width rows \
80145256Sjkoshy	                        \"\$DIALOG_TITLE\"     \
81145256Sjkoshy	                        \"\$DIALOG_BACKTITLE\" \
82145256Sjkoshy	                        \"\$prompt\"           \
83145256Sjkoshy	                        \"\$hline\"            \
84145256Sjkoshy	                        $TTY_MENU_LIST
85145256Sjkoshy
86145256Sjkoshy	local menu_choice
87145256Sjkoshy	menu_choice=$( eval $DIALOG \
88145256Sjkoshy		--title \"\$DIALOG_TITLE\"         \
89145256Sjkoshy		--backtitle \"\$DIALOG_BACKTITLE\" \
90145256Sjkoshy		--hline \"\$hline\"                \
91145256Sjkoshy		--ok-label \"\$msg_ok\"            \
92145256Sjkoshy		--cancel-label \"\$msg_cancel\"    \
93145256Sjkoshy		--menu \"\$prompt\"                \
94145256Sjkoshy		$height $width $rows               \
95145256Sjkoshy		$TTY_MENU_LIST                     \
96145256Sjkoshy		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
97145256Sjkoshy	)
98145256Sjkoshy	local retval=$?
99145256Sjkoshy	f_dialog_menutag_store -s "$menu_choice"
100145256Sjkoshy	return $retval
101145256Sjkoshy}
102145256Sjkoshy
103145256Sjkoshy# ttys_set_type $consterm
104145256Sjkoshy#
105145256Sjkoshy# Set terminal type of `ttyv*' and `cons[0-9]' entries in ttys(5) to $consterm.
106145256Sjkoshy#
107145256Sjkoshyttys_set_type()
108145256Sjkoshy{
109145256Sjkoshy	local consterm="$1"
110145256Sjkoshy
111183266Sjkoshy	#
112183266Sjkoshy	# Create new temporary file to write our ttys(5) update with new types.
113145256Sjkoshy	#
114145256Sjkoshy	local tmpfile="$( mktemp -t "pgm" )"
115145256Sjkoshy	[ "$tmpfile" ] || return $FAILURE
116145256Sjkoshy
117145256Sjkoshy	#
118145256Sjkoshy	# Fixup permissions and ownership (mktemp(1) creates the temporary file
119145256Sjkoshy	# with 0600 permissions -- change the permissions and ownership to
120145256Sjkoshy	# match ttys(5) before we write it out and mv(1) it into place).
121145256Sjkoshy	#
122145256Sjkoshy	local mode="$( stat -f '%#Lp' "$ETC_TTYS" 2> /dev/null )"
123145256Sjkoshy	local owner="$( stat -f '%u:%g' "$ETC_TTYS" 2> /dev/null )"
124145256Sjkoshy	f_quietly chmod "${mode:-0644}" "$tmpfile"
125145256Sjkoshy	f_quietly chown "${owner:-root:wheel}" "$tmpfile"
126145256Sjkoshy
127145256Sjkoshy	#
128145256Sjkoshy	# Operate on ttys(5), replacing only the types of `ttyv*' and
129145256Sjkoshy	# `cons[0-9]' terminals with the new type.
130145256Sjkoshy	#
131145256Sjkoshy	awk -v consterm="$consterm" '
132145256Sjkoshy	BEGIN {
133145256Sjkoshy	}
134145256Sjkoshy	{
135254813Smarkj		# "Skip" blank-lines, lines containing only whitespace, and
136254813Smarkj		# lines containing only a comment or whitespace-then-comment.
137145256Sjkoshy		#
138145256Sjkoshy		if ( $0 ~ /^[[:space:]]*(#|$)/ ) { print; next }
139145256Sjkoshy
140145256Sjkoshy		# "Skip" terminal types other than those supported
141145256Sjkoshy		#
142185363Sjkoshy		if ( $1 !~ /^(ttyv.*|cons[0-9])$/ ) { print; next }
143145256Sjkoshy
144145256Sjkoshy		# Change the terminal type to the new value
145145256Sjkoshy		#
146145256Sjkoshy		match($0, /[[:alnum:]\.\+-_]+[[:space:]]+(on|off).*$/)
147145256Sjkoshy		if ( ! RSTART ) { print; next }
148145256Sjkoshy		left = substr($0, 0, RSTART - 1)
149145256Sjkoshy		match($0, /[[:space:]]+(on|off).*$/)
150145256Sjkoshy		right = substr($0, RSTART)
151145256Sjkoshy		printf "%s%s%s\n", left, consterm, right
152145256Sjkoshy	}
153145256Sjkoshy	' "$ETC_TTYS" > "$tmpfile" || return $FAILURE
154145256Sjkoshy	f_quietly mv "$tmpfile" "$ETC_TTYS" || return $FAILURE
155145256Sjkoshy
156145256Sjkoshy	return $SUCCESS
157145256Sjkoshy}
158145256Sjkoshy
159145256Sjkoshy############################################################ MAIN
160145256Sjkoshy
161147191Sjkoshy# Incorporate rc-file if it exists
162147191Sjkoshy[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
163147191Sjkoshy
164147191Sjkoshy#
165147191Sjkoshy# Process command-line arguments
166147191Sjkoshy#
167147191Sjkoshywhile getopts h$GETOPTS_STDARGS flag; do
168184802Sjkoshy	case "$flag" in
169184802Sjkoshy	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
170184802Sjkoshy	esac
171184802Sjkoshydone
172184802Sjkoshyshift $(( $OPTIND - 1 ))
173145256Sjkoshy
174145256Sjkoshy#
175145256Sjkoshy# Initialize
176153110Sru#
177145256Sjkoshyf_dialog_title "$msg_system_console_terminal_type"
178145256Sjkoshyf_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
179145256Sjkoshyf_mustberoot_init
180145256Sjkoshy
181145256Sjkoshy#
182147191Sjkoshy# Launch application main menu
183145256Sjkoshy#
184147191Sjkoshywhile :; do
185147191Sjkoshy	dialog_menu_main || f_die
186147191Sjkoshy	f_dialog_menutag_fetch mtag
187147191Sjkoshy
188147191Sjkoshy	[ "$mtag" = "1 $msg_none" ] && break
189233628Sfabient
190147191Sjkoshy	consterm=$( eval f_dialog_menutag2item \"\$mtag\" $TTY_MENU_LIST )
191147191Sjkoshy	err=$( ttys_set_type "$consterm" 2>&1 )
192147191Sjkoshy	[ "$err" ] || break
193147191Sjkoshy
194147191Sjkoshy	f_dialog_msgbox "$err"
195147191Sjkoshydone
196147191Sjkoshy
197145256Sjkoshyexit $SUCCESS
198145256Sjkoshy
199145256Sjkoshy################################################################################
200145256Sjkoshy# END
201145774Sjkoshy################################################################################
202145256Sjkoshy