]> mj.ucw.cz Git - eval.git/blobdiff - bin/lib
Cosmetic changes.
[eval.git] / bin / lib
diff --git a/bin/lib b/bin/lib
index c83367f5e2d4fc070b273450a9c9449c59a1616d..0be042f80b88efde5fc5f39b3d3e9694bbf655d6 100644 (file)
--- a/bin/lib
+++ b/bin/lib
@@ -1,5 +1,5 @@
 # The Evaluator -- Shell Function Library
-# (c) 2001--2004 Martin Mares <mj@ucw.cz>
+# (c) 2001--2007 Martin Mares <mj@ucw.cz>
 
 # General settings
 shopt -s dotglob
@@ -44,6 +44,11 @@ function fatal
        exit 1
 }
 
+function try-ln
+{
+       ln $1 $2 2>/dev/null || cp $1 $2
+}
+
 # Sandbox subroutines
 
 function box-init
@@ -99,12 +104,37 @@ EOF
        pend "OK"
 }
 
-# Locating source file in SDIR, pass name in SRCN (without path) and extension in SRCEXT
+# Locate source file.
+# If no parameter is given, locate it in SDIR and return name as SRCN and extension as SRCEXT
+# Or a file name can be given and then SDIR, SRCN and SRCEXT are set.
 
 function locate-source
 {
        pstart "Finding source... "
-       SBASE=${1:-$PROBLEM}
+       if [ -n "$1" ] ; then
+               SRCBASE=$(echo $1 | sed 's/\.\([^.]\+\)//')
+               SRCEXT=$(echo $1 | sed 's/.*\.\([^.]\+\)/\1/')
+               if [ -n "$SRCEXT" ] ; then
+                       # Full name given, so just check the extension and existence
+                       SRCN=$1
+                       if [ ${SRCN:0:1} == / ] ; then
+                               SRCN=${SRCN:1}
+                               SDIR=
+                       fi
+                       [ -f "$SDIR/$SRCN" ] || die "Cannot find source file $SDIR/$SRCN"
+                       SRCEXT_OK=
+                       for a in $EXTENSIONS ; do
+                               if [ $a == $SRCEXT ] ; then
+                                       pend $SDIR/$SRCN
+                                       echo "Explicitly set source file: $SDIR/$SRCN"
+                                       return 0
+                               fi
+                       done
+                       die "Unknown extension .$SRCEXT"
+               fi
+       else
+               SBASE=$PROBLEM
+       fi
        for a in $EXTENSIONS ; do
                if [ -f $SDIR/$SBASE.$a ] ; then
                        [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
@@ -122,14 +152,15 @@ function locate-source
 function compile
 {
        pstart "Compiling... "
-       cp -a $SDIR/$SRCN $TDIR/$SRCN
+       # Beware, the original SRCN can be a strange user-supplied name
+       SRC=$PROBLEM.$SRCEXT
+       cp -a $SDIR/$SRCN $TDIR/$SRC
        if [ -n "$COMP_EXTRAS" ] ; then
                echo "Extras: $COMP_EXTRAS"
                for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
        fi
        box-clean
-       for a in $SRCN $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
-       SRC=$SRCN
+       for a in $SRC $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
        EXE=$PROBLEM
        CCMD=COMP_$SRCEXT
        CCMD=`eval echo ${!CCMD}`
@@ -196,13 +227,14 @@ function test-prolog
        OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
        case $IN_TYPE in
                file)   echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
-                       ln $PDIR/$TEST.in $TDIR/$TEST.in
+                       try-ln $PDIR/$TEST.in $TDIR/$TEST.in
                        cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
-                       BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
+                       [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
                        ;;
                stdio)  echo "Input file: <stdin> (from $PDIR/$TEST.in)"
-                       ln $PDIR/$TEST.in $TDIR/$TEST.in
-                       BOX_EXTRAS="$BOX_EXTRAS -i$TDIR/$TEST.in"
+                       try-ln $PDIR/$TEST.in $TDIR/$TEST.in
+                       cp $PDIR/$TEST.in $BOXDIR/.stdin
+                       BOX_EXTRAS="$BOX_EXTRAS -i.stdin"
                        ;;
                none)   echo "Input file: <none>"
                        ;;
@@ -211,10 +243,10 @@ function test-prolog
        esac
        case $OUT_TYPE in
                file)   echo "Output file: $PROBLEM.out"
-                       BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
+                       [ $TASK_TYPE == interactive ] || BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
                        ;;
                stdio)  echo "Output file: <stdout>"
-                       BOX_EXTRAS="$BOX_EXTRAS -o$TDIR/$TEST.out"
+                       BOX_EXTRAS="$BOX_EXTRAS -o.stdout"
                        ;;
                none)   echo "Output file: <none>"
                        ;;
@@ -236,7 +268,8 @@ function test-epilog
                file)   [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file."
                        cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
                        ;;
-               stdio)  [ -f $TDIR/$TEST.out ] || test-result 0 "No output file."
+               stdio)  [ -f $BOXDIR/.stdout ] || test-result 0 "No output file."
+                       cp $BOXDIR/.stdout $TDIR/$TEST.out
                        ;;
        esac
 }
@@ -250,7 +283,8 @@ function test-run-file
        BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`$BOX_EXTRAS"
        echo "Sandbox options: $BOXOPTS"
        if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
-               MSG=`head -1 $TMPDIR/exec.out`
+               cat $TMPDIR/exec.out
+               MSG=`tail -1 $TMPDIR/exec.out`
                test-result 0 "$MSG"
        fi
        cat $TMPDIR/exec.out
@@ -268,6 +302,7 @@ function test-run-interactive
        ICCMD=`eval echo $IA_CHECK`
        echo "Interactive checker: $ICCMD"
        if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
+               cat $TMPDIR/exec.out
                MSG="`head -1 $TMPDIR/exec.out`"
                test-result 0 "$MSG"
        fi
@@ -284,7 +319,8 @@ function syntax-check
        SCHECK=`eval echo $SYNTAX_CHECK`
        echo "Syntax check command: $SCHECK"
        if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
-               MSG=`head -1 $TMPDIR/exec.out`
+               cat $TMPDIR/exec.out
+               MSG=`tail -1 $TMPDIR/exec.out`
                if [ -z "$MSG" ] ; then MSG="Wrong syntax." ; fi
                test-result 0 "$MSG"
        fi
@@ -295,17 +331,21 @@ function syntax-check
 
 function output-check
 {
-       [ -n "$OUTPUT_CHECK" ] || return 0
-       pcont "<check> "
-       [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
-       OCHECK=`eval echo $OUTPUT_CHECK`
-       echo "Output check command: $OCHECK"
-       if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
-               MSG=`head -1 $TMPDIR/exec.out`
-               if [ -z "$MSG" ] ; then MSG="Wrong answer." ; fi
-               test-result 0 "$MSG"
+       MSG=
+       if [ -n "$OUTPUT_CHECK" -a "$OUT_TYPE" != none ] ; then
+               pcont "<check> "
+               [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
+               OCHECK=`eval echo $OUTPUT_CHECK`
+               echo "Output check command: $OCHECK"
+               if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
+                       cat $TMPDIR/exec.out
+                       MSG=`tail -1 $TMPDIR/exec.out`
+                       if [ -z "$MSG" ] ; then MSG="Wrong answer." ; fi
+                       test-result 0 "$MSG"
+               fi
+               cat $TMPDIR/exec.out
+               MSG=`tail -1 $TMPDIR/exec.out`
        fi
-       MSG=`head -1 $TMPDIR/exec.out`
        if [ -z "$MSG" ] ; then MSG="OK" ; fi
        test-result $POINTS_PER_TEST "$MSG"
 }
@@ -314,8 +354,8 @@ function output-check
 
 function public-setup
 {
-       HDIR=$MO_PUBLIC
-       PDIR=$MO_PUBLIC/problems/$PROBLEM
+       HDIR=$MO_ROOT
+       PDIR=$MO_ROOT/problems/$PROBLEM
        SDIR=.
        TDIR=~/.test
        TMPDIR=~/.test
@@ -327,7 +367,7 @@ function public-setup
        BOXDIR=~/.box
        mkdir -p $BOXDIR
        rm -rf $BOXDIR/*
-       BOXCMD="$MO_PUBLIC/bin/box -c$BOXDIR"
+       BOXCMD="$MO_ROOT/bin/box -c$BOXDIR"
        exec >log
        pend "OK  (see 'log' for details)"
 }
@@ -337,6 +377,10 @@ function public-setup
 function open-locate
 {
        [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
-       SRCN=$SDIR/$PROBLEM$TEST.out
+       if [ -n "$1" ] ; then
+               SRCN=$1
+       else
+               SRCN=$SDIR/$PROBLEM$TEST.out
+       fi
        [ -f $SRCN ] || fatal "Output file $SRCN not found"
 }