]> mj.ucw.cz Git - moe.git/blob - eval/libeval.sh
HDIR should default to `.'.
[moe.git] / eval / libeval.sh
1 # The Evaluator -- Shell Function Library
2 # (c) 2001--2008 Martin Mares <mj@ucw.cz>
3
4 # General settings
5 shopt -s dotglob
6
7 # Logging functions.
8 # File handles used: fd1=log, fd2=progress
9
10 function log-init
11 {
12         exec >>$TDIR/log
13         HAVE_LOG=1
14 }
15
16 function pstart
17 {
18         echo >&2 -n "$@"
19 }
20
21 function pcont
22 {
23         echo >&2 -n "$@"
24 }
25
26 function pend
27 {
28         echo >&2 "$@"
29 }
30
31 function die
32 {
33         # Report an internal error
34         echo >&2 "$@"
35         [ -n "$HAVE_LOG" ] && echo "Fatal error: $@"
36         exit 2
37 }
38
39 function fatal
40 {
41         # Report a fatal error in the program being tested
42         echo >&2 "$@"
43         [ -n "$HAVE_LOG" ] && echo "Fatal error: $@"
44         exit 1
45 }
46
47 function try-ln
48 {
49         ln $1 $2 2>/dev/null || cp $1 $2
50 }
51
52 # Expand occurrences of `$var' in a given variable
53
54 function expand-var
55 {
56         eval echo \"${!1}\"
57 }
58
59 # Given a <prefix>, override each variable <x> by <prefix>_<x>
60
61 function override-vars
62 {
63         local OR V W
64         declare -a OR
65         # `${!${1}_@}' does not work, so we have to use eval
66         OR=($(eval echo '${!'$1'_@}'))
67         for V in "${OR[@]}" ; do
68                 W=${V##$1_}
69                 eval $W='"$'$V'"'
70         done
71 }
72
73 # Sandbox subroutines
74
75 function box-init
76 {
77         pstart "Preparing sandbox... "
78
79         # Default values for user/group
80         EVAL_USER=${EVAL_USER:-$USER}
81         EVAL_GROUP=${EVAL_GROUP:-$GROUP}
82         TEST_USER=${TEST_USER:-$EVAL_USER}
83
84         if [ -z "$TEST_USER" -o "$TEST_USER" == "$EVAL_USER" ] ; then
85                 pcont "running locally (INSECURE), "
86                 TEST_USER="$EVAL_USER"
87                 BOXDIR=$HDIR/box
88                 BOXCMD=$HDIR/bin/box
89                 mkdir -p $BOXDIR
90         else
91                 pcont "used account $TEST_USER, "
92                 BOXDIR=$HDIR/box
93                 BOXCMD=$HDIR/bin/box-$TEST_USER
94         fi
95         [ -d $BOXDIR -a -f $BOXCMD ] || die "Sandbox set up incorrectly"
96         BOXCMD="$BOXCMD -c$BOXDIR"
97         echo "Sandbox directory: $BOXDIR"
98         echo "Sandbox command: $BOXCMD"
99         box-clean
100         pend "OK"
101 }
102
103 function box-clean
104 {
105         [ -n "$BOXCMD" ] || die "box-init not called"
106         rm -rf $BOXDIR/*
107 }
108
109 # Initialization of testing directories
110
111 function dir-init
112 {
113         pstart "Initializing... "
114         [ -z "$HDIR" ] && HDIR=.
115         PDIR=$HDIR/problems/$PROBLEM
116         SDIR=$HDIR/solutions/$CONTESTANT/$PROBLEM
117         TDIR=$HDIR/testing/$CONTESTANT/$PROBLEM
118         TMPDIR=$HDIR/tmp/
119
120         [ -d $PDIR ] || die "Problem $PROBLEM not known"
121         [ -d $SDIR ] || fatal "Solution of $PROBLEM not found"
122         mkdir -p $TDIR $TMPDIR
123         rm -rf $TDIR $TMPDIR
124         mkdir -p $TDIR $TMPDIR
125         cat >$TDIR/log <<EOF
126 Testing solution of $PROBLEM by $CONTESTANT
127 Test started at `date`
128 Eval base directory: $HDIR
129 Contestant's solution directory: $SDIR
130 Problem directory: $PDIR
131 Testing directory: $TDIR
132 EOF
133         pend "OK"
134 }
135
136 # Locate source file.
137 # If no parameter is given, locate it in SDIR and return name as SRCN and extension as SRCEXT
138 # Or a file name can be given and then SDIR, SRCN and SRCEXT are set.
139 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
140
141 function locate-source
142 {
143         pstart "Finding source... "
144         local SBASE
145         if [ -n "$1" ] ; then
146                 SDIR=`dirname "$1"`
147                 local S=`basename "$1"`
148                 SBASE=$(echo "$S" | sed 's/\.\([^.]\+\)//')
149                 SRCEXT=$(echo "$S" | sed '/\./!d; s/.*\.\([^.]\+\)/\1/')
150                 if [ -n "$SRCEXT" ] ; then
151                         # Full name given, so just check the extension and existence
152                         SRCN="$S"
153                         [ -f "$SDIR/$SRCN" ] || die "Cannot find source file $SDIR/$SRCN"
154                         SRCEXT_OK=
155                         for a in $EXTENSIONS ; do
156                                 if [ $a == $SRCEXT ] ; then
157                                         pend $SDIR/$SRCN
158                                         echo "Explicitly set source file: $SDIR/$SRCN"
159                                         return 0
160                                 fi
161                         done
162                         die "Unknown extension .$SRCEXT"
163                 fi
164         else
165                 SBASE=$PROBLEM
166         fi
167         for a in $EXTENSIONS ; do
168                 if [ -f "$SDIR/$SBASE.$a" ] ; then
169                         [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
170                         SRCN="$SBASE.$a"
171                         SRCEXT=$a
172                 fi
173         done
174         [ -n "$SRCN" ] || fatal "NOT FOUND"
175         pend $SRCN
176         echo "Found source file: $SDIR/$SRCN"
177 }
178
179 # Compilation (compile SDIR/SRCN with PDIR/COMP_EXTRAS to EXE=TDIR/PROBLEM)
180
181 function compile
182 {
183         pstart "Compiling... "
184
185         a="ALIAS_EXT_$SRCEXT"
186         if [ -n "${!a}" ] ; then
187                 SRCEXT="${!a}"
188                 echo "Normalized file extension: $SRCEXT"
189         fi
190         override-vars "EXT_$SRCEXT"
191
192         # Beware, the original SRCN can be a strange user-supplied name
193         SRC=$PROBLEM.$SRCEXT
194         cp "$SDIR/$SRCN" $TDIR/$SRC
195         if [ -n "$COMP_EXTRAS" ] ; then
196                 echo "Extras: $COMP_EXTRAS"
197                 for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
198         fi
199
200         box-clean
201         for a in $SRC $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
202         EXE=$PROBLEM
203         CCMD=$(expand-var COMP)
204         COMP_SANDBOX_OPTS=$(expand-var COMP_SANDBOX_OPTS)
205         echo "Compiler command: $CCMD"
206         echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
207         if [ -n "$PRE_COMPILE_HOOK" ] ; then
208                 echo "Pre-compile hook: $PRE_COMPILE_HOOK"
209                 eval $PRE_COMPILE_HOOK
210         fi
211
212         echo "Compiler input files:"
213         ls -Al $BOXDIR
214         echo "Compiler output:"
215         if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
216                 COMPILE_MSG="`cat $TDIR/compile.out`"
217                 pend "FAILED: $COMPILE_MSG"
218                 echo "$COMPILE_MSG"
219                 return 1
220         fi
221         cat $TDIR/compile.out
222         rm $TDIR/compile.out
223         if [ -n "$POST_COMPILE_HOOK" ] ; then
224                 echo "Post-compile hook: $POST_COMPILE_HOOK"
225                 eval $POST_COMPILE_HOOK
226         fi
227         echo "Compiler output files:"
228         ls -Al $BOXDIR
229         if [ ! -f $BOXDIR/$PROBLEM ] ; then
230                 pend "FAILED: Missing executable file"
231                 echo "Missing executable file"
232                 return 1
233         fi
234         EXE=$TDIR/$PROBLEM
235         cp -a $BOXDIR/$PROBLEM $EXE
236         echo "Compiled OK, result copied to $EXE"
237         pend "OK"
238 }
239
240 # Running of test program according to current task type (returns exit code and TEST_MSG)
241
242 function test-config
243 {
244         [ -f $PDIR/$TEST.config ] && . $PDIR/$TEST.config
245         override-vars "TEST_$TEST"
246 }
247
248 function test-run
249 {
250         test-run-$TASK_TYPE
251 }
252
253 function test-result
254 {
255         P=$1
256         M=$2
257         if [ -s $TDIR/$TEST.pts ] ; then
258                 P=`cat $TDIR/$TEST.pts`
259                 rm $TDIR/$TEST.pts
260         fi
261
262         # Translate signal numbers to readable strings
263         SG=${M#Caught fatal signal }
264         SG=${SG#Committed suicide by signal }
265         if [ "$SG" != "$M" ] ; then
266                 SG=`perl -MConfig -e '@s=split / /,$Config{sig_name}; print $s[$ARGV[0]]' $SG`
267                 [ -z "$SG" ] || M="$M (SIG$SG)"
268         fi
269
270         # Translate runtime errors to readable strings
271         RE=${M#Exited with error status }
272         if [ -n "$EXIT_CODE_HOOK" -a "$RE" != "$M" ] ; then
273                 NEWMSG=`$EXIT_CODE_HOOK $RE`
274                 if [ -n "$NEWMSG" ] ; then
275                         M="Runtime error $RE: $NEWMSG"
276                 fi
277         fi
278
279         echo "Verdict: $M"
280         echo "Points: $P"
281         test-verdict $P "$M"
282 }
283
284 function test-prolog
285 {
286         pcont "<init> "
287         box-clean
288         echo "Executable file: $TDIR/$PROBLEM"
289         if [ ! -x $TDIR/$PROBLEM ] ; then
290                 test-result 0 "Compile error"
291         fi
292         cp $TDIR/$PROBLEM $BOXDIR/
293         IN_TYPE=${IN_TYPE:-$IO_TYPE}
294         OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
295         case $IN_TYPE in
296                 file)   echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
297                         try-ln $PDIR/$TEST.in $TDIR/$TEST.in
298                         cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
299                         [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
300                         ;;
301                 stdio)  echo "Input file: <stdin> (from $PDIR/$TEST.in)"
302                         try-ln $PDIR/$TEST.in $TDIR/$TEST.in
303                         cp $PDIR/$TEST.in $BOXDIR/.stdin
304                         BOX_EXTRAS="$BOX_EXTRAS -i.stdin"
305                         ;;
306                 none)   echo "Input file: <none>"
307                         ;;
308                 *)      die "Unknown IN_TYPE $IN_TYPE"
309                         ;;
310         esac
311         if [ -n "$EV_PEDANT" -a $IN_TYPE != none ] ; then
312                 pcont "<pedant> "
313                 if [ "$EV_PEDANT" = 1 ] ; then
314                         EV_PEDANT=" "
315                 fi
316                 bin/pedant <$TDIR/$TEST.in >$TDIR/$TEST.pedant $EV_PEDANT
317                 if [ -s $TDIR/$TEST.pedant ] ; then
318                         pend
319                         sed 's/^/\t/' <$TDIR/$TEST.pedant >&2
320                         pstart -e '\t'
321                 fi
322         fi
323         case $OUT_TYPE in
324                 file)   echo "Output file: $PROBLEM.out"
325                         [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
326                         ;;
327                 stdio)  echo "Output file: <stdout>"
328                         BOX_EXTRAS="$BOX_EXTRAS -o.stdout"
329                         ;;
330                 none)   echo "Output file: <none>"
331                         ;;
332                 *)      die "Unknown OUT_TYPE $OUT_TYPE"
333                         ;;
334         esac
335         echo "Timeout: $TIME_LIMIT s"
336         echo "Memory: $MEM_LIMIT KB"
337         if [ -n "$PRE_RUN_HOOK" ] ; then
338                 echo "Pre-run hook: $PRE_RUN_HOOK"
339                 eval $PRE_RUN_HOOK
340         fi
341         echo "Sandbox contents before start:"
342         ls -Al $BOXDIR
343 }
344
345 function test-epilog
346 {
347         if [ -n "$POST_RUN_HOOK" ] ; then
348                 echo "Post-run hook: $POST_RUN_HOOK"
349                 eval $POST_RUN_HOOK
350         fi
351         echo "Sandbox contents after exit:"
352         ls -Al $BOXDIR
353         case ${OUT_TYPE:-$IO_TYPE} in
354                 file)   [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file"
355                         cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
356                         ;;
357                 stdio)  [ -f $BOXDIR/.stdout ] || test-result 0 "No output file"
358                         cp $BOXDIR/.stdout $TDIR/$TEST.out
359                         ;;
360         esac
361
362         if [ -n "$OUTPUT_FILTER" -a "$OUT_TYPE" != none -a -z "$EV_NOFILTER" ] ; then
363                 pcont "<filter> "
364                 FILTER=$(expand-var OUTPUT_FILTER)
365                 echo "Output filter command: $FILTER"
366                 mv $TDIR/$TEST.out $TDIR/$TEST.raw
367                 if ! eval $FILTER 2>$TMPDIR/exec.out ; then
368                         cat $TMPDIR/exec.out
369                         MSG=`tail -1 $TMPDIR/exec.out`
370                         if [ -z "$MSG" ] ; then MSG="Filter failed" ; fi
371                         test-result 0 "$MSG"
372                 fi
373                 cat $TMPDIR/exec.out
374         fi
375 }
376
377 # Running of test program with file input/output
378
379 function test-run-file
380 {
381         test-prolog
382         pcont "<run> "
383         BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
384         echo "Sandbox options: $BOXOPTS"
385         if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
386                 cat $TMPDIR/exec.out
387                 MSG=`tail -1 $TMPDIR/exec.out`
388                 test-result 0 "$MSG"
389         fi
390         cat $TMPDIR/exec.out
391         test-epilog
392 }
393
394 # Running of interactive test programs
395
396 function test-run-interactive
397 {
398         test-prolog
399         pcont "<run> "
400         BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
401         echo "Sandbox options: $BOXOPTS"
402         ICCMD=$(expand-var IA_CHECK)
403         echo "Interactive checker: $ICCMD"
404         if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
405                 cat $TMPDIR/exec.out
406                 MSG="`head -1 $TMPDIR/exec.out`"
407                 test-result 0 "$MSG"
408         fi
409         cat $TMPDIR/exec.out
410         test-epilog
411 }
412
413 # "Running" of open-data problems
414
415 function test-run-open-data
416 {
417         [ -f $SDIR/$TEST.out ] || test-result 0 "No solution"
418         ln $SDIR/$TEST.out $TDIR/$TEST.out
419 }
420
421 # Syntax checks
422
423 function syntax-check
424 {
425         [ -n "$SYNTAX_CHECK" ] || return 0
426         [ -z "$EV_NOCHECK" ] || return 0
427         pcont "<syntax> "
428         SCHECK=$(expand-var SYNTAX_CHECK)
429         echo "Syntax check command: $SCHECK"
430         if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
431                 cat $TMPDIR/exec.out
432                 MSG=`tail -1 $TMPDIR/exec.out`
433                 if [ -z "$MSG" ] ; then MSG="Wrong syntax" ; fi
434                 test-result 0 "$MSG"
435         fi
436         cat $TMPDIR/exec.out
437 }
438
439 # Output checks
440
441 function output-check
442 {
443         MSG=
444         if [ -n "$OUTPUT_CHECK" -a "$OUT_TYPE" != none -a -z "$EV_NOCHECK" ] ; then
445                 pcont "<check> "
446                 [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
447                 OCHECK=$(expand-var OUTPUT_CHECK)
448                 echo "Output check command: $OCHECK"
449                 if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
450                         cat $TMPDIR/exec.out
451                         MSG=`tail -1 $TMPDIR/exec.out`
452                         if [ -z "$MSG" ] ; then MSG="Wrong answer" ; fi
453                         test-result 0 "$MSG"
454                 fi
455                 cat $TMPDIR/exec.out
456                 MSG=`tail -1 $TMPDIR/exec.out`
457         fi
458         if [ -z "$MSG" ] ; then MSG="OK" ; fi
459         test-result $POINTS_PER_TEST "$MSG"
460 }
461
462 # Setup of public commands
463
464 function public-setup
465 {
466         HDIR=$MO_ROOT
467         PDIR=$MO_ROOT/problems/$PROBLEM
468         SDIR=.
469         TDIR=~/.test
470         TMPDIR=~/.test
471         [ -d $PDIR ] || die "Unknown problem $PROBLEM"
472
473         pstart "Initializing... "
474         mkdir -p $TDIR
475         rm -rf $TDIR/*
476         BOXDIR=~/.box
477         mkdir -p $BOXDIR
478         rm -rf $BOXDIR/*
479         BOXCMD="$MO_ROOT/bin/box -c$BOXDIR"
480         exec >check-log
481         pend "OK  (see 'check-log' for details)"
482 }
483
484 # Locate output of open data problem, test case TEST
485 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
486
487 function open-locate
488 {
489         [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
490         if [ -n "$1" ] ; then
491                 SDIR=`dirname "$1"`
492                 SRCN=`basename "$1"`
493         else
494                 SRCN=$SDIR/$PROBLEM$TEST.out
495         fi
496         [ -f "$SDIR/$SRCN" ] || fatal "Output file $SRCN not found"
497 }
498
499 # Translation of runtime error codes for various compilers
500
501 function fpc-exit-code
502 {
503         case "$1" in
504                 200)    echo -n "Division by zero" ;;
505                 201)    echo -n "Range check error" ;;
506                 202)    echo -n "Stack overflow" ;;
507                 203)    echo -n "Heap overflow" ;;
508                 205)    echo -n "Floating point overflow" ;;
509                 215)    echo -n "Arithmetic overflow" ;;
510                 216)    echo -n "Segmentation fault" ;;
511         esac
512 }
513
514 # A helper function for parsing of command-line overrides of variables
515
516 function parse-cmdline
517 {
518         if [ "${1#*=}" != "$1" ] ; then
519                 local var=${1%%=*}
520                 local val=${1#*=}
521                 eval $var="'$val'"
522                 return 0
523         else
524                 return 1
525         fi
526 }