]> mj.ucw.cz Git - eval.git/blob - bin/lib
BOX_EXTRAS works again.
[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
177         a="ALIAS_EXT_$SRCEXT"
178         if [ -n "${!a}" ] ; then
179                 SRCEXT="${!a}"
180                 echo "Normalized file extension: $SRCEXT"
181         fi
182         override-vars "EXT_$SRCEXT"
183
184         # Beware, the original SRCN can be a strange user-supplied name
185         SRC=$PROBLEM.$SRCEXT
186         cp "$SDIR/$SRCN" $TDIR/$SRC
187         if [ -n "$COMP_EXTRAS" ] ; then
188                 echo "Extras: $COMP_EXTRAS"
189                 for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
190         fi
191
192         box-clean
193         for a in $SRC $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
194         EXE=$PROBLEM
195         CCMD=$(expand-var COMP)
196         COMP_SANDBOX_OPTS=$(expand-var COMP_SANDBOX_OPTS)
197         echo "Compiler command: $CCMD"
198         echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
199         eval $COMP_SANDBOX_INIT
200
201         echo "Compiler input files:"
202         ls -Al $BOXDIR
203         echo "Compiler output:"
204         if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
205                 COMPILE_MSG="`cat $TDIR/compile.out`"
206                 pend "FAILED: $COMPILE_MSG"
207                 echo "$COMPILE_MSG"
208                 return 1
209         fi
210         cat $TDIR/compile.out
211         rm $TDIR/compile.out
212         echo "Compiler output files:"
213         ls -Al $BOXDIR
214         if [ ! -f $BOXDIR/$PROBLEM ] ; then
215                 pend "FAILED: Missing executable file"
216                 echo "Missing executable file"
217                 return 1
218         fi
219         EXE=$TDIR/$PROBLEM
220         cp -a $BOXDIR/$PROBLEM $EXE
221         echo "Compiled OK, result copied to $EXE"
222         pend "OK"
223 }
224
225 # Running of test program according to current task type (returns exit code and TEST_MSG)
226
227 function test-config
228 {
229         [ -f $PDIR/$TEST.config ] && . $PDIR/$TEST.config
230         override-vars "TEST_$TEST"
231 }
232
233 function test-run
234 {
235         test-run-$TASK_TYPE
236 }
237
238 function test-result
239 {
240         P=$1
241         M=$2
242         if [ -s $TDIR/$TEST.pts ] ; then
243                 P=`cat $TDIR/$TEST.pts`
244                 rm $TDIR/$TEST.pts
245         fi
246
247         # Translate signal numbers to readable strings
248         SG=${M#Caught fatal signal }
249         SG=${SG#Committed suicide by signal }
250         if [ "$SG" != "$M" ] ; then
251                 SG=`perl -MConfig -e '@s=split / /,$Config{sig_name}; print $s[$ARGV[0]]' $SG`
252                 [ -z "$SG" ] || M="$M (SIG$SG)"
253         fi
254
255         # Translate runtime errors to readable strings
256         RE=${M#Exited with error status }
257         if [ -n "$EXIT_CODE_HOOK" -a "$RE" != "$M" ] ; then
258                 NEWMSG=`$EXIT_CODE_HOOK $RE`
259                 if [ -n "$NEWMSG" ] ; then
260                         M="Runtime error $RE: $NEWMSG"
261                 fi
262         fi
263
264         echo "Verdict: $M"
265         echo "Points: $P"
266         test-verdict $P "$M"
267 }
268
269 function test-prolog
270 {
271         pcont "<init> "
272         box-clean
273         echo "Executable file: $TDIR/$PROBLEM"
274         if [ ! -x $TDIR/$PROBLEM ] ; then
275                 test-result 0 "Compile error"
276         fi
277         cp $TDIR/$PROBLEM $BOXDIR/
278         IN_TYPE=${IN_TYPE:-$IO_TYPE}
279         OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
280         case $IN_TYPE in
281                 file)   echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
282                         try-ln $PDIR/$TEST.in $TDIR/$TEST.in
283                         cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
284                         [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
285                         ;;
286                 stdio)  echo "Input file: <stdin> (from $PDIR/$TEST.in)"
287                         try-ln $PDIR/$TEST.in $TDIR/$TEST.in
288                         cp $PDIR/$TEST.in $BOXDIR/.stdin
289                         BOX_EXTRAS="$BOX_EXTRAS -i.stdin"
290                         ;;
291                 none)   echo "Input file: <none>"
292                         ;;
293                 *)      die "Unknown IN_TYPE $IN_TYPE"
294                         ;;
295         esac
296         if [ -n "$EV_PEDANT" -a $IN_TYPE != none ] ; then
297                 pcont "<pedant> "
298                 if [ "$EV_PEDANT" = 1 ] ; then
299                         EV_PEDANT=" "
300                 fi
301                 bin/pedant <$TDIR/$TEST.in >$TDIR/$TEST.pedant $EV_PEDANT
302                 if [ -s $TDIR/$TEST.pedant ] ; then
303                         pend
304                         sed 's/^/\t/' <$TDIR/$TEST.pedant >&2
305                         pstart -e '\t'
306                 fi
307         fi
308         case $OUT_TYPE in
309                 file)   echo "Output file: $PROBLEM.out"
310                         [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
311                         ;;
312                 stdio)  echo "Output file: <stdout>"
313                         BOX_EXTRAS="$BOX_EXTRAS -o.stdout"
314                         ;;
315                 none)   echo "Output file: <none>"
316                         ;;
317                 *)      die "Unknown OUT_TYPE $OUT_TYPE"
318                         ;;
319         esac
320         echo "Timeout: $TIME_LIMIT s"
321         echo "Memory: $MEM_LIMIT KB"
322         eval $SANDBOX_INIT
323         echo "Sandbox contents before start:"
324         ls -Al $BOXDIR
325 }
326
327 function test-epilog
328 {
329         echo "Sandbox contents after exit:"
330         ls -Al $BOXDIR
331         case ${OUT_TYPE:-$IO_TYPE} in
332                 file)   [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file"
333                         cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
334                         ;;
335                 stdio)  [ -f $BOXDIR/.stdout ] || test-result 0 "No output file"
336                         cp $BOXDIR/.stdout $TDIR/$TEST.out
337                         ;;
338         esac
339
340         if [ -n "$OUTPUT_FILTER" -a "$OUT_TYPE" != none -a -z "$EV_NOFILTER" ] ; then
341                 pcont "<filter> "
342                 FILTER=$(expand-var OUTPUT_FILTER)
343                 echo "Output filter command: $FILTER"
344                 mv $TDIR/$TEST.out $TDIR/$TEST.raw
345                 if ! eval $FILTER 2>$TMPDIR/exec.out ; then
346                         cat $TMPDIR/exec.out
347                         MSG=`tail -1 $TMPDIR/exec.out`
348                         if [ -z "$MSG" ] ; then MSG="Filter failed" ; fi
349                         test-result 0 "$MSG"
350                 fi
351                 cat $TMPDIR/exec.out
352         fi
353 }
354
355 # Running of test program with file input/output
356
357 function test-run-file
358 {
359         test-prolog
360         pcont "<run> "
361         BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
362         echo "Sandbox options: $BOXOPTS"
363         if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
364                 cat $TMPDIR/exec.out
365                 MSG=`tail -1 $TMPDIR/exec.out`
366                 test-result 0 "$MSG"
367         fi
368         cat $TMPDIR/exec.out
369         test-epilog
370 }
371
372 # Running of interactive test programs
373
374 function test-run-interactive
375 {
376         test-prolog
377         pcont "<run> "
378         BOXOPTS=$(expand-var TEST_SANDBOX_OPTS)
379         echo "Sandbox options: $BOXOPTS"
380         ICCMD=$(expand-var IA_CHECK)
381         echo "Interactive checker: $ICCMD"
382         if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
383                 cat $TMPDIR/exec.out
384                 MSG="`head -1 $TMPDIR/exec.out`"
385                 test-result 0 "$MSG"
386         fi
387         cat $TMPDIR/exec.out
388         test-epilog
389 }
390
391 # "Running" of open-data problems
392
393 function test-run-open-data
394 {
395         [ -f $SDIR/$TEST.out ] || test-result 0 "No solution"
396         ln $SDIR/$TEST.out $TDIR/$TEST.out
397 }
398
399 # Syntax checks
400
401 function syntax-check
402 {
403         [ -n "$SYNTAX_CHECK" ] || return 0
404         [ -z "$EV_NOCHECK" ] || return 0
405         pcont "<syntax> "
406         SCHECK=$(expand-var SYNTAX_CHECK)
407         echo "Syntax check command: $SCHECK"
408         if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
409                 cat $TMPDIR/exec.out
410                 MSG=`tail -1 $TMPDIR/exec.out`
411                 if [ -z "$MSG" ] ; then MSG="Wrong syntax" ; fi
412                 test-result 0 "$MSG"
413         fi
414         cat $TMPDIR/exec.out
415 }
416
417 # Output checks
418
419 function output-check
420 {
421         MSG=
422         if [ -n "$OUTPUT_CHECK" -a "$OUT_TYPE" != none -a -z "$EV_NOCHECK" ] ; then
423                 pcont "<check> "
424                 [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
425                 OCHECK=$(expand-var OUTPUT_CHECK)
426                 echo "Output check command: $OCHECK"
427                 if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
428                         cat $TMPDIR/exec.out
429                         MSG=`tail -1 $TMPDIR/exec.out`
430                         if [ -z "$MSG" ] ; then MSG="Wrong answer" ; fi
431                         test-result 0 "$MSG"
432                 fi
433                 cat $TMPDIR/exec.out
434                 MSG=`tail -1 $TMPDIR/exec.out`
435         fi
436         if [ -z "$MSG" ] ; then MSG="OK" ; fi
437         test-result $POINTS_PER_TEST "$MSG"
438 }
439
440 # Setup of public commands
441
442 function public-setup
443 {
444         HDIR=$MO_ROOT
445         PDIR=$MO_ROOT/problems/$PROBLEM
446         SDIR=.
447         TDIR=~/.test
448         TMPDIR=~/.test
449         [ -d $PDIR ] || die "Unknown problem $PROBLEM"
450
451         pstart "Initializing... "
452         mkdir -p $TDIR
453         rm -rf $TDIR/*
454         BOXDIR=~/.box
455         mkdir -p $BOXDIR
456         rm -rf $BOXDIR/*
457         BOXCMD="$MO_ROOT/bin/box -c$BOXDIR"
458         exec >check-log
459         pend "OK  (see 'check-log' for details)"
460 }
461
462 # Locate output of open data problem, test case TEST
463 # Beware, SDIR and SRCN can contain spaces and other strange user-supplied characters.
464
465 function open-locate
466 {
467         [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
468         if [ -n "$1" ] ; then
469                 SDIR=`dirname "$1"`
470                 SRCN=`basename "$1"`
471         else
472                 SRCN=$SDIR/$PROBLEM$TEST.out
473         fi
474         [ -f "$SDIR/$SRCN" ] || fatal "Output file $SRCN not found"
475 }
476
477 # Translation of runtime error codes for various compilers
478
479 function fpc-exit-code
480 {
481         case "$1" in
482                 200)    echo -n "Division by zero" ;;
483                 201)    echo -n "Range check error" ;;
484                 202)    echo -n "Stack overflow" ;;
485                 203)    echo -n "Heap overflow" ;;
486                 205)    echo -n "Floating point overflow" ;;
487                 215)    echo -n "Arithmetic overflow" ;;
488                 216)    echo -n "Segmentation fault" ;;
489         esac
490 }