lib/scripts/run-smlnj
author wenzelm
Thu, 24 Apr 1997 19:41:00 +0200
changeset 3046 6b7935317538
parent 3010 4be22c300966
child 3051 30490aa41356
permissions -rwxr-xr-x
adapted to SML/NJ 1.09.27; minor cleanup;

#!/bin/bash
#
# $Id$
#
# SML/NJ startup script (for 1.06 or later).
#
# Global vars: INFILE OUTFILE COPYDB MLTEXT TERMINATE,
# and from settings


## diagnostics

function fail_out()
{
  echo "Unable to create output heap file: \"$OUTFILE\"" >&2
  exit 2
}


## version specific stuff

EXIT=""
COMMIT=""
FIXNAME=""

case "$ML_SYSTEM" in
  smlnj-1.0[678]*)
    EXIT="val exit = System.Unix.exit;"
    COMMIT="fun commit () = not (exportML\"$OUTFILE\");"
    COMMIT_RO='fun commit () = (output (std_err, "Error - Database is not opened for writing.\\n"); false);'
    ;;
  smlnj-1.09*)
    EXIT="fun exit 0 : unit = OS.Process.exit OS.Process.success | exit _ = OS.Process.exit OS.Process.failure;"
    COMMIT="fun commit () = not (SMLofNJ.exportML\"$OUTFILE\");"
    COMMIT_RO='fun commit () = (TextIO.output (TextIO.stdErr, "Error - Database is not opened for writing.\\n"); false);'
    FIXNAME=true
    ;;
esac


## prepare databases

DB="$INFILE"

if [ -n "$DB" ]; then
  DB="@SMLload=$INFILE"
  EXIT=""
fi

if [ -z "$OUTFILE" ]; then
  COMMIT="$COMMIT_RO"
elif [ -n "$INFILE" -a ! "$INFILE" -ef "$OUTFILE" ]; then
  [ -f "$OUTFILE" ] && { rm -f "$OUTFILE" || fail_out; }
  cp "$INFILE" "$OUTFILE" || fail_out
fi

[ -n "$OUTFILE" -a -f "$OUTFILE" ] && { chmod +w "$OUTFILE" || fail_out; }

MLTEXT="$EXIT $COMMIT $MLTEXT"
MLEXIT="commit ();"


## run it!

START_SML="$ML_HOME/sml $ML_OPTIONS $DB"

if [ -n "$TERMINATE" ]; then
  echo "$MLTEXT" "$MLEXIT" | $START_SML
  RC=$?
elif [ -z "$MLTEXT" ]; then
  sh -c "{ $ISABELLE_HOME/lib/scripts/ucat; echo '$MLEXIT'; } | $START_SML"
  RC=$?
else
  sh -c "{ echo '$MLTEXT'; $ISABELLE_HOME/lib/scripts/ucat; echo '$MLEXIT'; } | $START_SML"
  RC=$?
fi

#fix heap file name
if [ -n "$OUTFILE" -a -n "$FIXNAME" ]; then
  eval $($ML_HOME/.arch-n-opsys)
  SUFFIX=$ARCH-$OPSYS
  [ -f $OUTFILE.$SUFFIX ] && mv $OUTFILE.$SUFFIX $OUTFILE
fi

exit $RC