3 # vlock.sh -- start script for vlock, the VT locking program for linux
5 # This program is copyright (C) 2007 Frank Benkstein, and is free
6 # software which is freely distributable under the terms of the
7 # GNU General Public License version 2, included as the file COPYING in this
8 # distribution. It is NOT public domain software, and any
9 # redistribution not permitted by the GNU General Public License is
10 # expressly forbidden without prior written permission from
13 # Ignore some signals.
14 trap : HUP INT QUIT TSTP
19 # Magic characters to clear the terminal.
20 CLEAR_SCREEN
="`echo -e '\033[H\033[J'`"
22 # Enter message that is common to different the messages.
23 VLOCK_ENTER_PROMPT
="Please press [ENTER] to unlock."
25 # Message that is displayed when console switching is disabled.
26 VLOCK_ALL_MESSAGE
="${CLEAR_SCREEN}\
27 The entire console display is now completely locked.
28 You will not be able to switch to another virtual console.
30 ${VLOCK_ENTER_PROMPT}"
32 # Message that is displayed when only the current terminal is locked.
33 VLOCK_CURRENT_MESSAGE
="${CLEAR_SCREEN}\
34 This TTY is now locked.
36 ${VLOCK_ENTER_PROMPT}"
39 if [ -r "${HOME}/.vlockrc" ] ; then
43 # "Compile" time variables.
44 VLOCK_MAIN
="%PREFIX%/sbin/vlock-main"
45 VLOCK_VERSION
="%VLOCK_VERSION%"
46 # If set to "y" plugin support is enabled in vlock-main.
47 VLOCK_ENABLE_PLUGINS
="%VLOCK_ENABLE_PLUGINS%"
50 echo >&2 "vlock: locks virtual consoles, saving your current session."
51 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
52 echo >&2 "Usage: vlock [options] [plugins...]"
54 echo >&2 "Usage: vlock [options]"
56 echo >&2 " Where [options] are any of:"
57 echo >&2 "-c or --current: lock only this virtual console, allowing user to"
58 echo >&2 " switch to other virtual consoles."
59 echo >&2 "-a or --all: lock all virtual consoles by preventing other users"
60 echo >&2 " from switching virtual consoles."
61 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
62 echo >&2 "-n or --new: allocate a new virtual console before locking,"
63 echo >&2 " implies --all."
64 echo >&2 "-s or --disable-sysrq: disable SysRq while consoles are locked to"
65 echo >&2 " prevent killing vlock with SAK"
66 echo >&2 "-t <seconds> or --timeout <seconds>: run screen saver plugins"
67 echo >&2 " after the given amount of time."
69 echo >&2 "-v or --version: Print the version number of vlock and exit."
70 echo >&2 "-h or --help: Print this help message and exit."
73 # Export variables only if they are set. Some shells create an empty variable
74 # on export even if it was previously unset.
76 while [ $# -gt 0 ] ; do
77 if ( eval [ "\"\${$1+set}\"" = "set" ] ) ; then
85 short_options_with_arguments
="t"
86 long_options_with_arguments
="timeout"
88 # Parse command line arguments.
89 while [ $# -gt 0 ] ; do
92 # Strip "-" to get the list of option characters.
96 last_option_argument
="${options}"
99 # If an option character takes an argument all characters after it
100 # become the argument if it isn't already the last one. E.g. if "x"
101 # takes an argument "-fooxbar" becomes "-foo -x bar".
102 while [ -n "${last_option_argument}" ] ; do
103 # Get first option character.
104 option
="$(expr "${last_option_argument}" : '\(.\)')"
105 # Strip it from the list of option characters.
106 last_option_argument
="${last_option_argument#?}"
107 last_option_index
=$
((${last_option_index} + 1))
109 if expr "${short_options_with_arguments}" : "${option}" >/dev
/null
; then
110 # Prepend "-" plus option character and rest of option string to $@.
111 set -- "-${option}" "${last_option_argument}" "$@"
113 # Remove all characters after the option character.
114 if [ "${last_option_index}" -gt 1 ] ; then
115 options
="$(expr "${options}" : "\(.\{$((${last_option_index}-1))\}\
)")"
124 # Convert clashed arguments like "-foobar" to "-f -o -o -b -a -r".
125 while [ -n "${options}" ] ; do
126 # Get last option character.
127 option
="$(expr "${options}" : '.*\(.\)')"
128 # Strip it from the list of option characters.
129 options
="${options%?}"
130 # Prepend "-" plus option character to $@.
131 set -- "-${option}" "$@"
135 # Extract option name and argument.
136 option
="$(expr "x
$1" : 'x--\([^=]*\)=.*')"
137 option_argument
="$(expr "x
$1" : 'x--[^=]*=\(.*\)')"
140 compare_options
="${long_options_with_arguments}"
142 # Find the option in the list of options that take an argument.
143 while [ -n "${compare_options}" ] ; do
144 compare_option
="${compare_options%%,*}"
145 compare_options
="${compare_options#"${compare_option}"}"
146 compare_options
="${compare_options#,}"
148 if [ "${option}" = "${compare_option}" ] ; then
149 set -- "--${option}" "${option_argument}" "$@"
150 unset option option_argument
155 if [ -n "${option}" ] ; then
156 echo >&2 "$0: option '--${option}' does not allow an argument"
161 plugins
="${plugins} all"
169 plugins
="${plugins} new"
173 plugins
="${plugins} nosysrq"
179 echo >&2 "$0: option '$1' requires an argument"
188 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
189 echo >&2 "vlock version ${VLOCK_VERSION}"
191 echo >&2 "vlock version ${VLOCK_VERSION} (no plugin support)"
196 echo >&1 "$0: unknown option '$1'"
201 # End of option list.
207 if [ "${argument}" = "--" ] ; then
208 has_double_dash
="yes"
213 if [ -n "${has_double_dash}" ] ; then
224 # Export variables for vlock-main.
225 export_if_set VLOCK_TIMEOUT VLOCK_PROMPT_TIMEOUT
226 export_if_set VLOCK_MESSAGE VLOCK_ALL_MESSAGE VLOCK_CURRENT_MESSAGE
228 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
229 exec "${VLOCK_MAIN}" ${plugins} ${VLOCK_PLUGINS} "$@
"
231 exec "${VLOCK_MAIN}" ${plugins}