]> mj.ucw.cz Git - eval.git/blob - bin/lib
16afaebe428b02feec61afb9f0c831d989e57bdb
[eval.git] / bin / lib
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         if [ -z "$TEST_USER" -o "$TEST_USER" == $EVAL_USER ] ; then
79                 pcont "running locally (INSECURE), "
80                 TEST_USER=$EVAL_USER
81                 BOXDIR=`pwd`/box
82                 BOXCMD=bin/box
83                 mkdir -p box
84         else
85                 pcont "used account $TEST_USER, "
86                 BOXDIR=$MO_ROOT/eval/$TEST_USER
87                 BOXCMD=bin/box-$TEST_USER
88         fi
89         [ -d $BOXDIR -a -f $BOXCMD ] || die "Sandbox set up incorrectly"
90         BOXCMD="$BOXCMD -c$BOXDIR"
91         echo "Sandbox directory: $BOXDIR"
92         echo "Sandbox command: $BOXCMD"
93         box-clean
94         pend "OK"
95 }
96
97 function box-clean
98 {
99         [ -n "$BOXCMD" ] || die "box-init not called"
100         rm -rf $BOXDIR/*
101 }
102
103 # Initialization of testing directories
104
105 function dir-init
106 {
107         pstart "Initializing... "
108         HDIR=.
109         PDIR=problems/$PROBLEM
110         SDIR=solutions/$CONTESTANT/$PROBLEM
111         TDIR=testing/$CONTESTANT/$PROBLEM
112         TMPDIR=tmp
113         [ -d $PDIR ] || die "Problem $PROBLEM not known"
114         [ -d $SDIR ] || fatal "Solution of $PROBLEM not found"
115         mkdir -p $TDIR $TMPDIR
116         rm -rf $TDIR $TMPDIR
117         mkdir -p $TDIR $TMPDIR
118         cat >$TDIR/log <<EOF
119 Testing solution of $PROBLEM by $CONTESTANT
120 Test started at `date`
121 Contestant's solution directory: $SDIR
122 Problem directory: $PDIR
123 Testing directory: $TDIR
124 EOF
125         pend "OK"
126 }
127
128 # Locate source file.
129 # If no parameter is given, locate it in SDIR and return name as SRCN and extension as SRCEXT
130 # Or a file name can be given and then SDIR, SRCN and SRCEXT are set.
131 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
132
133 function locate-source
134 {
135         pstart "Finding source... "
136         local SBASE
137         if [ -n "$1" ] ; then
138                 SDIR=`dirname "$1"`
139                 local S=`basename "$1"`
140                 SBASE=$(echo "$S" | sed 's/\.\([^.]\+\)//')
141                 SRCEXT=$(echo "$S" | sed '/\./!d; s/.*\.\([^.]\+\)/\1/')
142                 if [ -n "$SRCEXT" ] ; then
143                         # Full name given, so just check the extension and existence
144                         SRCN="$S"
145                         [ -f "$SDIR/$SRCN" ] || die "Cannot find source file $SDIR/$SRCN"
146                         SRCEXT_OK=
147                         for a in $EXTENSIONS ; do
148                                 if [ $a == $SRCEXT ] ; then
149                                         pend $SDIR/$SRCN
150                                         echo "Explicitly set source file: $SDIR/$SRCN"
151                                         return 0
152                                 fi
153                         done
154                         die "Unknown extension .$SRCEXT"
155                 fi
156         else
157                 SBASE=$PROBLEM
158         fi
159         for a in $EXTENSIONS ; do
160                 if [ -f "$SDIR/$SBASE.$a" ] ; then
161                         [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
162                         SRCN="$SBASE.$a"
163                         SRCEXT=$a
164                 fi
165         done
166         [ -n "$SRCN" ] || fatal "NOT FOUND"
167         pend $SRCN
168         echo "Found source file: $SDIR/$SRCN"
169 }
170
171 # Compilation (compile SDIR/SRCN with PDIR/COMP_EXTRAS to EXE=TDIR/PROBLEM)
172
173 function compile
174 {
175         pstart "Compiling... "
176         override-vars "EXT_$SRCEXT"
177         # Beware, the original SRCN can be a strange user-supplied name
178         SRC=$PROBLEM.$SRCEXT
179         cp "$SDIR/$SRCN" $TDIR/$SRC
180         if [ -n "$COMP_EXTRAS" ] ; then
181                 echo "Extras: $COMP_EXTRAS"
182                 for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
183         fi
184         box-clean
185         for a in $SRC $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
186         EXE=$PROBLEM
187         CCMD=$(expand-var COMP)
188         COMP_SANDBOX_OPTS=$(expand-var COMP_SANDBOX_OPTS)
189         echo "Compiler command: $CCMD"
190         echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
191         eval $COMP_SANDBOX_INIT
192
193         echo "Compiler input files:"
194         ls -Al $BOXDIR
195         echo "Compiler output:"
196         if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
197                 COMPILE_MSG="`cat $TDIR/compile.out`"
198                 pend "FAILED: $COMPILE_MSG"
199                 echo "$COMPILE_MSG"
200                 return 1
201         fi
202         cat $TDIR/compile.out
203         rm $TDIR/compile.out
204         echo "Compiler output files:"
205         ls -Al $BOXDIR
206         if [ ! -f $BOXDIR/$PROBLEM ] ; then
207                 pend "FAILED: Missing executable file"
208                 echo "Missing executable file"
209                 return 1
210         fi
211         EXE=$TDIR/$PROBLEM
212         cp -a $BOXDIR/$PROBLEM $EXE
213         echo "Compiled OK, result copied to $EXE"
214         pend "OK"
215 }
216
217 # Running of test program according to current task type (returns exit code and TEST_MSG)
218
219 function test-config
220 {
221         [ -f $PDIR/$TEST.config ] && . $PDIR/$TEST.config
222         override-vars "TEST_$TEST"
223 }
224
225 function test-run
226 {
227         test-run-$TASK_TYPE
228 }
229
230 function test-result
231 {
232         P=$1
233         M=$2
234         if [ -s $TDIR/$TEST.pts ] ; then
235                 P=`cat $TDIR/$TEST.pts`
236                 rm $TDIR/$TEST.pts
237         fi
238
239         # Translate signal numbers to readable strings
240         SG=${M#Caught fatal signal }
241         SG=${SG#Committed suicide by signal }
242         if [ "$SG" != "$M" ] ; then
243                 SG=`perl -MConfig -e '@s=split / /,$Config{sig_name}; print $s[$ARGV[0]]' $SG`
244                 [ -z "$SG" ] || M="$M (SIG$SG)"
245         fi
246
247         # Translate runtime errors to readable strings
248         RE=${M#Exited with error status }
249         if [ -n "$EXIT_CODE_HOOK" -a "$RE" != "$M" ] ; then
250                 NEWMSG=`$EXIT_CODE_HOOK $RE`
251                 if [ -n "$NEWMSG" ] ; then
252                         M="Runtime error $RE: $NEWMSG"
253                 fi
254         fi
255
256         echo "Verdict: $M"
257         echo "Points: $P"
258         test-verdict $P "$M"
259 }
260
261 function test-prolog
262 {
263         pcont "<init> "
264         box-clean
265         echo "Executable file: $TDIR/$PROBLEM"
266         if [ ! -x $TDIR/$PROBLEM ] ; then
267                 test-result 0 "Compile error"
268         fi
269         cp $TDIR/$PROBLEM $BOXDIR/
270         BOX_EXTRAS=
271         IN_TYPE=${IN_TYPE:-$IO_TYPE}
272         OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
273         case $IN_TYPE in
274                 file)   echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
275                         try-ln $PDIR/$TEST.in $TDIR/$TEST.in
276                         cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
277                         [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
278                         ;;
279                 stdio)  echo "Input file: <stdin> (from $PDIR/$TEST.in)"
280                         try-ln $PDIR/$TEST.in $TDIR/$TEST.in
281                         cp $PDIR/$TEST.in $BOXDIR/.stdin
282                         BOX_EXTRAS="$BOX_EXTRAS -i.stdin"
283                         ;;
284                 none)   echo "Input file: <none>"
285                         ;;
286                 *)      die "Unknown IN_TYPE $IN_TYPE"
287                         ;;
288         esac
289         if [ -n "$EV_PEDANT" -a $IN_TYPE != none ] ; then
290                 pcont "<pedant> "
291                 if [ "$EV_PEDANT" = 1 ] ; then
292                         EV_PEDANT=" "
293                 fi
294                 bin/pedant <$TDIR/$TEST.in >$TDIR/$TEST.pedant $EV_PEDANT
295                 if [ -s $TDIR/$TEST.pedant ] ; then
296                         pend
297                         sed 's/^/\t/' <$TDIR/$TEST.pedant >&2
298                         pstart -e '\t'
299                 fi
300         fi
301         case $OUT_TYPE in
302                 file)   echo "Output file: $PROBLEM.out"
303                         [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
304                         ;;
305                 stdio)  echo "Output file: <stdout>"
306                         BOX_EXTRAS="$BOX_EXTRAS -o.stdout"
307                         ;;
308                 none)   echo "Output file: <none>"
309                         ;;
310                 *)      die "Unknown OUT_TYPE $OUT_TYPE"
311                         ;;
312         esac
313         echo "Timeout: $TIME_LIMIT s"
314         echo "Memory: $MEM_LIMIT KB"
315         eval $SANDBOX_INIT
316         echo "Sandbox contents before start:"
317         ls -Al $BOXDIR
318 }
319
320 function test-epilog
321 {
322         echo "Sandbox contents after exit:"
323         ls -Al $BOXDIR
324         case ${OUT_TYPE:-$IO_TYPE} in
325                 file)   [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file"
326                         cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
327                         ;;
328                 stdio)  [ -f $BOXDIR/.stdout ] || test-result 0 "No output file"
329                         cp $BOXDIR/.stdout $TDIR/$TEST.out
330                         ;;
331         esac
332
333         if [ -n "$OUTPUT_FILTER" -a "$OUT_TYPE" != none -a -z "$EV_NOFILTER" ] ; then
334                 pcont "<filter> "
335                 FILTER=$(expand-var OUTPUT_FILTER)
336                 echo "Output filter command: $FILTER"
337                 mv $TDIR/$TEST.out $TDIR/$TEST.raw
338                 if ! eval $FILTER 2>$TMPDIR/exec.out ; then
339                         cat $TMPDIR/exec.out
340                         MSG=`tail -1 $TMPDIR/exec.out`
341                         if [ -z "$MSG" ] ; then MSG="Filter failed" ; fi
342                         test-result 0 "$MSG"
343                 fi
344                 cat $TMPDIR/exec.out
345         fi
346 }
347
348 # Running of test program with file input/output
349
350 function test-run-file
351 {
352         test-prolog
353         pcont "<run> "
354         BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
355         echo "Sandbox options: $BOXOPTS"
356         if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
357                 cat $TMPDIR/exec.out
358                 MSG=`tail -1 $TMPDIR/exec.out`
359                 test-result 0 "$MSG"
360         fi
361         cat $TMPDIR/exec.out
362         test-epilog
363 }
364
365 # Running of interactive test programs
366
367 function test-run-interactive
368 {
369         test-prolog
370         pcont "<run> "
371         BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
372         echo "Sandbox options: $BOXOPTS"
373         ICCMD=$(expand-var IA_CHECK)
374         echo "Interactive checker: $ICCMD"
375         if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
376                 cat $TMPDIR/exec.out
377                 MSG="`head -1 $TMPDIR/exec.out`"
378                 test-result 0 "$MSG"
379         fi
380         cat $TMPDIR/exec.out
381         test-epilog
382 }
383
384 # "Running" of open-data problems
385
386 function test-run-open-data
387 {
388         [ -f $SDIR/$TEST.out ] || test-result 0 "No solution"
389         ln $SDIR/$TEST.out $TDIR/$TEST.out
390 }
391
392 # Syntax checks
393
394 function syntax-check
395 {
396         [ -n "$SYNTAX_CHECK" ] || return 0
397         [ -z "$EV_NOCHECK" ] || return 0
398         pcont "<syntax> "
399         SCHECK=$(expand-var SYNTAX_CHECK)
400         echo "Syntax check command: $SCHECK"
401         if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
402                 cat $TMPDIR/exec.out
403                 MSG=`tail -1 $TMPDIR/exec.out`
404                 if [ -z "$MSG" ] ; then MSG="Wrong syntax" ; fi
405                 test-result 0 "$MSG"
406         fi
407         cat $TMPDIR/exec.out
408 }
409
410 # Output checks
411
412 function output-check
413 {
414         MSG=
415         if [ -n "$OUTPUT_CHECK" -a "$OUT_TYPE" != none -a -z "$EV_NOCHECK" ] ; then
416                 pcont "<check> "
417                 [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
418                 OCHECK=$(expand-var OUTPUT_CHECK)
419                 echo "Output check command: $OCHECK"
420                 if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
421                         cat $TMPDIR/exec.out
422                         MSG=`tail -1 $TMPDIR/exec.out`
423                         if [ -z "$MSG" ] ; then MSG="Wrong answer" ; fi
424                         test-result 0 "$MSG"
425                 fi
426                 cat $TMPDIR/exec.out
427                 MSG=`tail -1 $TMPDIR/exec.out`
428         fi
429         if [ -z "$MSG" ] ; then MSG="OK" ; fi
430         test-result $POINTS_PER_TEST "$MSG"
431 }
432
433 # Setup of public commands
434
435 function public-setup
436 {
437         HDIR=$MO_ROOT
438         PDIR=$MO_ROOT/problems/$PROBLEM
439         SDIR=.
440         TDIR=~/.test
441         TMPDIR=~/.test
442         [ -d $PDIR ] || die "Unknown problem $PROBLEM"
443
444         pstart "Initializing... "
445         mkdir -p $TDIR
446         rm -rf $TDIR/*
447         BOXDIR=~/.box
448         mkdir -p $BOXDIR
449         rm -rf $BOXDIR/*
450         BOXCMD="$MO_ROOT/bin/box -c$BOXDIR"
451         exec >check-log
452         pend "OK  (see 'check-log' for details)"
453 }
454
455 # Locate output of open data problem, test case TEST
456 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
457
458 function open-locate
459 {
460         [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
461         if [ -n "$1" ] ; then
462                 SDIR=`dirname "$1"`
463                 SRCN=`basename "$1"`
464         else
465                 SRCN=$SDIR/$PROBLEM$TEST.out
466         fi
467         [ -f "$SDIR/$SRCN" ] || fatal "Output file $SRCN not found"
468 }
469
470 # Translation of runtime error codes for various compilers
471
472 function fpc-exit-code
473 {
474         case "$1" in
475                 200)    echo -n "Division by zero" ;;
476                 201)    echo -n "Range check error" ;;
477                 202)    echo -n "Stack overflow" ;;
478                 203)    echo -n "Heap overflow" ;;
479                 205)    echo -n "Floating point overflow" ;;
480                 215)    echo -n "Arithmetic overflow" ;;
481                 216)    echo -n "Segmentation fault" ;;
482         esac
483 }