document the change in prompt timeout handling
[vlock.git] / src / vlock.sh
blob75f13691c26ffac7e655b5b6b9cf6c7a29499c54
1 #!%BOURNE_SHELL%
3 # vlock.sh -- start script for vlock, the VT locking program for linux
4 #
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
11 # the author.
13 # Ignore some signals.
14 trap : HUP INT QUIT TSTP
16 # Exit on error.
17 set -e
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}"
38 # Read user settings.
39 if [ -r "${HOME}/.vlockrc" ] ; then
40 . "${HOME}/.vlockrc"
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%"
49 print_help() {
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...]"
53 else
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.
75 export_if_set() {
76 while [ $# -gt 0 ] ; do
77 if ( eval [ "\"\${$1+set}\"" = "set" ] ) ; then
78 eval export $1
80 shift
81 done
84 main() {
85 short_options_with_arguments="t"
86 long_options_with_arguments="timeout"
88 # Parse command line arguments.
89 while [ $# -gt 0 ] ; do
90 case "$1" in
91 -[!-]?*)
92 # Strip "-" to get the list of option characters.
93 options="${1#-}"
94 shift
96 last_option_argument="${options}"
97 last_option_index=0
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))\}\)")"
116 else
117 options=""
120 break
122 done
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}" "$@"
132 done
134 --?*=?*)
135 # Extract option name and argument.
136 option="$(expr "x$1" : 'x--\([^=]*\)=.*')"
137 option_argument="$(expr "x$1" : 'x--[^=]*=\(.*\)')"
138 shift
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
151 break
153 done
155 if [ -n "${option}" ] ; then
156 echo >&2 "$0: option '--${option}' does not allow an argument"
157 exit 1
160 -a|--all)
161 plugins="${plugins} all"
162 shift
164 -c|--current)
165 unset plugins
166 shift
168 -n|--new)
169 plugins="${plugins} new"
170 shift
172 -s|--disable-sysrq)
173 plugins="${plugins} nosysrq"
174 shift
176 -t|--timeout)
177 VLOCK_TIMEOUT="$2"
178 if ! shift 2 ; then
179 echo >&2 "$0: option '$1' requires an argument"
180 exit 1
183 -h|--help)
184 print_help
185 exit
187 -v|--version)
188 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
189 echo >&2 "vlock version ${VLOCK_VERSION}"
190 else
191 echo >&2 "vlock version ${VLOCK_VERSION} (no plugin support)"
193 exit
195 -[!-]|--?*)
196 echo >&1 "$0: unknown option '$1'"
197 print_help
198 exit 1
201 # End of option list.
202 shift
203 break
206 for argument ; do
207 if [ "${argument}" = "--" ] ; then
208 has_double_dash="yes"
209 break
211 done
213 if [ -n "${has_double_dash}" ] ; then
214 set -- "$@" "$1"
215 else
216 set -- "$@" -- "$1"
219 shift
221 esac
222 done
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} "$@"
230 else
231 exec "${VLOCK_MAIN}" ${plugins}
235 main "$@"