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