src/util.{c,h}: get rid of ensure_* functions
[vlock.git] / src / vlock.sh
blobf138926919f52d6f731bedcee21969845da7ea05
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, implies --all."
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 local options long_options short_options plugins
87 short_options="acvh"
88 long_options="all,current,version,help"
90 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
91 short_options="${short_options}nst:"
92 long_options="${long_options},new,disable-sysrq,timeout:"
95 # Test for GNU getopt.
96 ( getopt -T >/dev/null )
98 if [ $? -eq 4 ] ; then
99 # GNU getopt.
100 options=`getopt -o "${short_options}" --long "${long_options}" -n vlock -- "$@"` || getopt_error=1
101 else
102 # Other getopt, e.g. BSD.
103 options=`getopt "${short_options}" "$@"` || getopt_error=1
106 if [ -n "${getopt_error}" ] ; then
107 print_help
108 exit 1
111 eval set -- "${options}"
113 while : ; do
114 case "$1" in
115 -a|--all)
116 plugins="${plugins} all"
117 shift
119 -c|--current)
120 unset plugins
121 shift
123 -n|--new)
124 plugins="${plugins} new"
125 shift
127 -s|--disable-sysrq)
128 plugins="${plugins} nosysrq"
129 shift
131 -t|--timeout)
132 shift
133 VLOCK_TIMEOUT="$1"
134 shift
136 -h|--help)
137 print_help
138 exit
140 -v|--version)
141 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
142 echo >&2 "vlock version ${VLOCK_VERSION}"
143 else
144 echo >&2 "vlock version ${VLOCK_VERSION} (no plugin support)"
146 exit
149 # End of option list.
150 shift
151 break
154 echo >&2 "getopt error: $1"
155 exit 1
157 esac
158 done
160 # Export variables for vlock-main.
161 export_if_set VLOCK_TIMEOUT VLOCK_PROMPT_TIMEOUT
162 export_if_set VLOCK_MESSAGE VLOCK_ALL_MESSAGE VLOCK_CURRENT_MESSAGE
164 if [ "${VLOCK_ENABLE_PLUGINS}" = "yes" ] ; then
165 exec "${VLOCK_MAIN}" ${plugins} ${VLOCK_PLUGINS} "$@"
166 else
167 exec "${VLOCK_MAIN}" ${plugins}
171 main "$@"