aboutsummaryrefslogtreecommitdiff
path: root/doomsh.bash
blob: eb4a1ac7162095d772d928ea4686d4ae24292d7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!bash
#
# Copyright (C) 2016, 2018, 2021 Tom Ryder <tom@sanctum.geek.nz>
#
# This file is part of doomsh.
#
# doomsh is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# doomsh is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# doomsh.  If not, see <https://www.gnu.org/licenses/>.

# doomsh: Play with low ulimits in Bash
self=doomsh

# Don't try to play doomsh if already playing doomsh
if [[ $DOOMSH ]] ; then
	printf '%s: Already playing doomsh!\n' \
		"$self" >&2
	exit 2
fi

# Show the coloured logo; thanks to Frans P. de Vries for the classic ASCII art
# <http://www.gamers.org/~fpv/doombann.html>
printf >&2 %s "$( {
	tput AF 3 || tput setaf 3
	tput md || tput bold
} 2>/dev/null )"
while IFS= read -r line ; do
	printf >&2 '%s\n' "$line"
done <<EOF
______ _____ _____ __   ___
| __  \  _  |  _  |  \ /  |
| | \ | | | | | | | . V . |
| | / | \_/ | \_/ | |\ /| |
| |/ / \   / \   /| | V | |
| ' /   \_/   \_/ \_|   | |
|__/                    \_|
EOF
printf >&2 %s "$( {
	tput me || tput sgr0
} 2>/dev/null )"

# Need to figure out a skill level
declare -i skill

# If we have an argument, that must be the skill level
if (($#)) ; then
	skill=$1
	shift

# Otherwise, we'll give the user a menu to choose from
else
	while IFS= read -r line ; do
		printf >&2 '%s\n' "$line"
	done <<EOF

1. I'm too young to die
2. Hey, not too rough
3. Hurt me plenty
4. Ultra-Violence
5. Nightmare!

EOF
	read -er -p '[1-5]: ' skill

	# If the user chose "Nightmare!" mode, get them to confirm, in the true
	# spirit of things; otherwise, quit
	if ((skill == 5)) ; then
		while IFS= read -r line ; do
			printf '%s\n' "$line" >&2
		done <<EOF

Are you sure? This skill level isn't even remotely fair.
EOF
		read -er -p '[y/N]: ' confirm
		[[ $confirm == [Yy]* ]] || exit 1
	fi
fi

# Don't accept any other skill level settings
case $skill in
	1|2|3|4|5)
		;;
	*)
		printf '%s: Unknown skill level\n' \
			"$self" >&2
		exit 2
esac

# Form a red prompt (hopefully) using terminal escapes
ps1=$(printf '%sdoomsh[%u]\$%s ' \
	"$( {
		tput AF 1 || tput setaf 1
	} 2>/dev/null )" \
	"$skill" \
	"$( {
		tput me || tput sgr0
	} 2>/dev/null )"
)

# Set the limits based on the skill level
declare -i filesize files stack memory
filesize=$((1 << (8 - skill)))
files=$((11 - skill))
stack=$((1 << (11 - skill)))
memory=$((1 << (19 - skill)))

# Display the limits
printf >&2 '\n'
printf >&2 'File size writing limit: %u blocks\n' \
	"$filesize"
printf >&2 'Open files limit: %u\n' \
	"$files"
printf >&2 'Stack size: %u kbytes\n' \
	"$stack"
printf >&2 'Memory size: %u kbytes\n' \
	"$memory"
printf >&2 '\n'

# Record the current seconds since shell start, so we can tell how long the
# user lasts before giving up or segfaulting
declare -i start
start=$SECONDS

# Open a subshell so that the ulimit settings don't persist after the fork
(
	# We only proceed if the ulimit call succeeds
	ulimit \
		-f "$filesize" \
		-n "$files" \
		-s "$stack" \
		-v "$memory" \
		|| exit 2
	DOOMSH=$skill PS1=$ps1 bash --norc
)

# Give the user some statistics about their performance
printf >&2 '%s: You lasted %u seconds at skill level %u!\n' \
	"$self" "$((SECONDS - start))" "$skill"