/
gsh.sh
611 lines (527 loc) · 15.3 KB
/
gsh.sh
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#!/bin/bash
# warning about "echo $(cmd)", used many times with echo "$(gettext ...)"
# shellcheck disable=SC2005
#
# warning about eval_gettext '$GSH_ROOT/...' about variable not expanding in single quotes
# shellcheck disable=SC2016
#
# warning about using printf "$(gettext ...)" because the string may contain
# escape characters (they don't)
# shellcheck disable=SC2059
#
# warning about declaring and initializing at the same time: local x=...
# shellcheck disable=SC2155
# shellcheck source=/dev/null
. gettext.sh
# shellcheck source=lib/mission_source.sh
. "$GSH_LIB/mission_source.sh"
trap "_gsh_exit EXIT" EXIT
trap "_gsh_exit TERM" SIGTERM
# trap "_gsh_exit INT" SIGINT
# log an action to the missions.log file
__log_action() {
local MISSION_NB action D S
MISSION_NB=$1
action=$2
D="$(date +%s)"
S="$(checksum "$GSH_UID#$MISSION_NB#$action#$D")"
printf "%s %s %s %s\n" "$MISSION_NB" "$action" "$D" "$S" >> "$GSH_CONFIG/missions.log"
}
_gsh_reset() {
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
if ! . mainshell.sh
then
echo "$(gettext "Error: the command 'gsh reset' shouldn't be run inside a subshell!")" >&2
return 1
fi
__gsh_start "$MISSION_NB"
}
# reset the bash configuration
_gsh_hard_reset() {
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
if ! . mainshell.sh
then
echo "$(gettext "Error: the command 'gsh hardreset' shouldn't be run inside a subshell!")" >&2
return 1
fi
# on relance bash, histoire de recharcher la config
__log_action "$MISSION_NB" "HARD_RESET"
exec bash --rcfile "$GSH_LIB/gshrc"
}
# called when gsh exits
_gsh_exit() {
local MISSION_NB=$(_gsh_pcm)
local signal=$1
if jobs | grep -iq stopped
then
while true
do
printf "$(gettext "There are stopped jobs in your session.
Those processes will be terminated.
You can get the list of those jobs with
\$ jobs -s
Do you still want to quit? [y/n]") "
local resp
read -r resp
if [ -z "$resp" ] || [ "$resp" = "$(gettext "n")" ] || [ "$resp" = "$(gettext "n")" ]
then
return
elif [ "$resp" = "$(gettext "y")" ] || [ "$resp" = "$(gettext "y")" ]
then
break
fi
done
#shellcheck disable=SC2046
kill $(jobs -ps)
fi
__log_action "$MISSION_NB" "$signal"
export GSH_LAST_ACTION='exit'
__gsh_clean "$MISSION_NB"
[ "$GSH_MODE" != "DEBUG" ] && ! [ -d "$GSH_ROOT/.git" ] && gsh unprotect
#shellcheck disable=SC2046
kill -sSIGHUP $(jobs -p) 2>/dev/null
}
# display the goal of a mission given by its number
_gsh_goal() {
local MISSION_NB
if [ "$#" -eq 0 ]
then
MISSION_NB="$(_gsh_pcm)"
else
MISSION_NB="$1"
shift
fi
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
local MISSION_DIR="$(missiondir "$MISSION_NB")"
if [ -f "$MISSION_DIR/goal.sh" ]
then
mission_source "$MISSION_DIR/goal.sh" | parchment | pager
elif [ -f "$MISSION_DIR/goal.txt" ]
then
FILE="$MISSION_DIR/goal.txt"
parchment "$FILE" | pager
else
FILE="$(TEXTDOMAIN="$(textdomainname "$MISSION_DIR")" eval_gettext '$MISSION_DIR/goal/en.txt')"
parchment "$FILE" | pager
fi
}
# start a mission given by its number
__gsh_start() {
local quiet=""
if [ "$1" = "--quiet" ]
then
quiet="--quiet"
shift
fi
local MISSION_NB D S
if [ -z "$1" ]
then
MISSION_NB=$(_gsh_pcm)
if [ "$?" -eq 1 ] && [ "$GSH_MODE" != "DEBUG" ]
then
gsh welcome
echo
printf "$(gettext "Press Enter to continue.")"
stty -echo 2>/dev/null # ignore errors, in case input comes from a redirection
read
stty echo 2>/dev/null # ignore errors, in case input comes from a redirection
clear
fi
else
MISSION_NB=$1
fi
[ -z "$MISSION_NB" ] && MISSION_NB=1
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
local MISSION_DIR="$(missiondir "$MISSION_NB")"
if [ -z "$MISSION_DIR" ]
then
echo
color_echo red "$(eval_gettext "Error: mission \$MISSION_NB doesn't exist!")" >&2
echo
__log_action "$MISSION_NB" "UNKNOWN_MISSION"
gsh reset
return 1
fi
# re-source static.sh, in case some important directory was removed by accident
[ -f "$MISSION_DIR/static.sh" ] && mission_source "$MISSION_DIR/static.sh"
if [ -f "$MISSION_DIR/init.sh" ]
then
# If init.sh is sourced in a subshell, variable definitions, changes of
# PWD, functions or aliases defined in init.sh will disappear.
# I save the environment before / after and display a warning when that's the case.
if ! . mainshell.sh
then
local env_before=$(mktemp)
local env_after=$(mktemp)
. save_environment.sh > "$env_before"
fi
mission_source "$MISSION_DIR/init.sh"
local exit_status=$?
if [ "$exit_status" -ne 0 ]
then
color_echo yellow "$(eval_gettext "Error: mission \$MISSION_NB is cancelled because some dependencies are not met.")" >&2
__log_action "$MISSION_NB" "CANCEL_DEP_PB"
__gsh_start "$((MISSION_NB + 1))"
return
fi
if ! . mainshell.sh
then
. save_environment.sh > "$env_after"
if ! cmp -s "$env_before" "$env_after"
then
color_echo yellow "$(gettext "Error: this mission was initialized in a subshell.
Run the command
\$ gsh reset
to make sure the mission is initialized properly.")" >&2
rm -f "$env_before" "$env_after"
fi
fi
fi
__log_action "$MISSION_NB" "START"
if [ -z "$GSH_QUIET_INTRO" ] && [ -z "$quiet" ]
then
if [ "$MISSION_NB" -eq 1 ]
then
parchment -B Inverted "$(eval_gettext '$GSH_ROOT/i18n/gameshell-init-msg/en.txt')"
else
parchment -B Inverted "$(eval_gettext '$GSH_ROOT/i18n/gameshell-init-msg-short/en.txt')"
fi
fi
}
# stop a mission given by its number
_gsh_skip() {
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
if ! admin_mode
then
__log_action "$MISSION_NB" "SKIP:AUTH_FAILURE"
return 1
fi
__log_action "$MISSION_NB" "SKIP"
export GSH_LAST_ACTION='skip'
__gsh_clean "$MISSION_NB"
color_echo yellow "$(eval_gettext 'Mission $MISSION_NB has been cancelled.')" >&2
__gsh_start $((10#$MISSION_NB + 1))
}
# applies auto.sh script, if it exists
_gsh_auto() {
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
local MISSION_DIR="$(missiondir "$MISSION_NB")"
if ! [ -f "$MISSION_DIR/auto.sh" ]
then
echo "$(eval_gettext "Error: mission \$MISSION_NB doesn't have an auto script.")" >&2
return 1
fi
if ! admin_mode
then
__log_action "$MISSION_NB" "AUTO:AUTH_FAILURE"
return 1
fi
__log_action "$MISSION_NB" "AUTO"
mission_source "$MISSION_DIR/auto.sh"
return 0
}
# check completion of a mission given by its number
_gsh_check() {
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
local MISSION_DIR="$(missiondir "$MISSION_NB")"
mission_source "$MISSION_DIR/check.sh"
local exit_status=$?
if [ "$exit_status" -eq 0 ]
then
echo
color_echo green "$(eval_gettext 'Congratulations, mission $MISSION_NB has been successfully completed!')"
echo
__log_action "$MISSION_NB" "CHECK_OK"
export GSH_LAST_ACTION='check_true'
__gsh_clean "$MISSION_NB"
if [ -f "$MISSION_DIR/treasure.sh" ]
then
# Record the treasure to be loaded by GameShell's gshrc.
TREASURE_FILE=$GSH_CONFIG/treasure_$(printf "%04d" "$MISSION_NB")_$(basename "$MISSION_DIR"/).sh
echo "export TEXTDOMAIN=\"$(textdomainname "$MISSION_DIR")\"" > "$TREASURE_FILE"
cat "$MISSION_DIR/treasure.sh" >> "$TREASURE_FILE"
echo "export TEXTDOMAIN=gsh" >> "$TREASURE_FILE"
unset TREASURE_FILE
# Display the text message (if it exists).
if [ -f "$MISSION_DIR/treasure-msg.sh" ]
then
local tmp_treasure_file=$(mktemp)
mission_source "$MISSION_DIR/treasure-msg.sh" > "$tmp_treasure_file"
if [ -s "$tmp_treasure_file" ]
then
echo
treasure_message "$tmp_treasure_file"
echo
fi
rm -rf "$tmp_treasure_file"
elif [ -f "$MISSION_DIR/treasure-msg.txt" ]
then
echo
treasure_message "$MISSION_DIR/treasure-msg.txt"
echo
else
local file_msg="$(TEXTDOMAIN="$(textdomainname "$MISSION_DIR")" eval_gettext '$MISSION_DIR/treasure-msg/en.txt')"
if [ -f "$file_msg" ]
then
echo
treasure_message "$file_msg"
echo
fi
fi
# Load the treasure in the current shell.
mission_source "$MISSION_DIR/treasure.sh"
#sourcing the file isn't very robust as the "gsh check" may happen in a subshell!
if ! . mainshell.sh
then
echo "$(gettext "Warning: the file 'treasure.sh' was sourced from a subshell.
You should use the command
\$ gsh reset")" >&2
fi
fi
__gsh_start $((10#$MISSION_NB + 1))
return 0
else
echo
color_echo red "$(eval_gettext "Sorry, mission \$MISSION_NB hasn't been completed.")"
echo
__log_action "$MISSION_NB" "CHECK_OOPS"
export GSH_LAST_ACTION='check_false'
__gsh_clean "$MISSION_NB"
__gsh_start "$MISSION_NB"
return 255
fi
}
__gsh_clean() {
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
local MISSION_DIR="$(missiondir "$MISSION_NB")"
if [ -f "$MISSION_DIR/clean.sh" ]
then
mission_source "$MISSION_DIR/clean.sh"
fi
unset GSH_LAST_ACTION
}
_gsh_assert_check() {
local MISSION_NB="$(_gsh_pcm)"
local expected=$1
if [ "$expected" != "true" ] && [ "$expected" != "false" ]
then
echo "$(eval_gettext "Error: _gsh_assert_check only accept 'true' and 'false' as argument.")" >&2
return 1
fi
local msg=$3
local MISSION_DIR="$(missiondir "$MISSION_NB")"
mission_source "$MISSION_DIR/check.sh"
local exit_status=$?
local nb_tests=$(cat "$GSH_VAR/nb_tests")
echo "$(( nb_tests + 1))" > "$GSH_VAR/nb_tests"
local nb_failed_tests=$(cat "$GSH_VAR/nb_failed_tests")
if [ "$expected" = "true" ] && [ "$exit_status" -ne 0 ]
then
echo "$(( nb_failed_tests + 1))" > "$GSH_VAR/nb_failed_tests"
color_echo red "$(eval_gettext 'test $nb_tests failed') (expected check 'true')"
[ -n "$msg" ] && echo "$msg"
elif [ "$expected" = "false" ] && [ "$exit_status" -eq 0 ]
then
echo "$(( nb_failed_tests + 1))" > "$GSH_VAR/nb_failed_tests"
color_echo red "$(eval_gettext 'test $nb_tests failed') (expected check 'false')"
[ -n "$msg" ] && echo "$msg"
fi
export GSH_LAST_ACTION="assert"
__gsh_clean "$MISSION_NB"
__gsh_start --quiet "$MISSION_NB"
}
_gsh_assert() {
local condition=$1
if [ "$condition" = "check" ]
then
shift
_gsh_assert_check "$@"
return
fi
local msg=$2
local nb_tests=$(cat "$GSH_VAR/nb_tests")
echo "$(( nb_tests + 1))" > "$GSH_VAR/nb_tests"
echo "INCR"
if ! eval "$condition"
then
echo "$(( nb_failed_tests + 1))" > "$GSH_VAR/nb_failed_tests"
color_echo red "$(eval_gettext 'test $nb_tests failed') (expected condition 'true')"
[ -n "$msg" ] && echo "$msg"
fi
}
_gsh_test() {
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
#shellcheck disable=SC2034
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
local MISSION_DIR="$(missiondir "$MISSION_NB")"
if ! [ -f "$MISSION_DIR/test.sh" ]
then
echo "$(eval_gettext "Error: mission \$MISSION_NB doesn't have a test script.")" >&2
return 2
fi
echo 0 > "$GSH_VAR/nb_tests"
echo 0 > "$GSH_VAR/nb_failed_tests"
mission_source "$MISSION_DIR/test.sh"
local ret
local nb_tests=$(cat "$GSH_VAR/nb_tests")
local nb_failed_tests=$(cat "$GSH_VAR/nb_failed_tests")
if [ "$nb_failed_tests" = 0 ]
then
echo
color_echo green "$(eval_gettext '$nb_tests successful tests')"
echo
ret=0
else
echo
color_echo red "$(eval_gettext '$nb_failed_tests failures out of $nb_tests tests')"
echo
ret=255
fi
rm -f "$GSH_VAR/nb_tests" "$GSH_VAR/nb_failed_tests"
return "$ret"
}
gsh() {
local _TEXTDOMAIN=$TEXTDOMAIN
export TEXTDOMAIN="gsh"
local cmd=$1
shift
# should the command abort GameShell on failure (gsh test / gsh auto)
if [ "$1" = "--abort" ]
then
local ABORT="true"
shift
fi
local ret=0
case $cmd in
"check")
_gsh_check
ret=$?
;;
"reset")
export GSH_LAST_ACTION='reset'
__gsh_clean
_gsh_reset
;;
"goal")
_gsh_goal "$@"
;;
"exit")
exit 0
;;
# admin stuff
# TODO: something to regenerate static world
"skip")
_gsh_skip
;;
"auto")
_gsh_auto
;;
"hardreset")
export GSH_LAST_ACTION='hardreset'
__gsh_clean
_gsh_hard_reset
;;
"goto")
if [ -z "$1" ]
then
echo "$(gettext "Error: the 'goto' command requires a mission number as argument.")" >&2
return 1
fi
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
#shellcheck disable=SC2034
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
if ! admin_mode
then
__log_action "$MISSION_NB" "GOTO:AUTH_FAILURE"
return 1
fi
__log_action "$MISSION_NB" "GOTO"
export GSH_LAST_ACTION='goto'
__gsh_clean
__gsh_start "$@"
;;
*)
if command -v "_gsh_$cmd" >/dev/null
then
local MISSION_NB="$(_gsh_pcm)"
if [ -z "$MISSION_NB" ]
then
#shellcheck disable=SC2034
local fn_name="${FUNCNAME[0]}"
echo "$(eval_gettext "Error: couldn't get mission number \$MISSION_NB (from \$fn_name)")" >&2
return 1
fi
local MISSION_DIR="$(missiondir "$MISSION_NB")"
MISSION_NB=$MISSION_NB MISSION_DIR=$MISSION_DIR _gsh_$cmd "$@"
ret=$?
else
echo "$(eval_gettext "Error: unknown gsh command '\$cmd'.
Use one of the following commands:") check, goal, help, reset" >&2
export TEXTDOMAIN=$_TEXTDOMAIN
unset _TEXTDOMAIN
return 1
fi
;;
esac
export TEXTDOMAIN=$_TEXTDOMAIN
unset _TEXTDOMAIN
[ -n "$ABORT" ] && [ "$ret" -eq 255 ] && exit 1
return "$ret"
}
# vim: shiftwidth=2 tabstop=2 softtabstop=2