bin/isabelle-process
author wenzelm
Sun May 12 19:56:30 2013 +0200 (2013-05-12)
changeset 51948 cb5dbc9a06f9
parent 51938 cf9c8d8d8939
child 51952 4517ceb545c1
permissions -rwxr-xr-x
load options for regular isabelle-process, not just for Isar loop (relevant for numerous low-level tools) -- NB: Isabelle_Process manages options via protocol message;
     1 #!/usr/bin/env bash
     2 #
     3 # Author: Markus Wenzel, TU Muenchen
     4 #
     5 # Isabelle process startup script.
     6 
     7 if [ -L "$0" ]; then
     8   TARGET="$(LC_ALL=C ls -l "$0" | sed 's/.* -> //')"
     9   exec "$(cd "$(dirname "$0")"; cd "$(pwd -P)"; cd "$(dirname "$TARGET")"; pwd)/$(basename "$TARGET")" "$@"
    10 fi
    11 
    12 
    13 ## settings
    14 
    15 PRG="$(basename "$0")"
    16 
    17 ISABELLE_HOME="$(cd "$(dirname "$0")"; cd "$(pwd -P)"; cd ..; pwd)"
    18 source "$ISABELLE_HOME/lib/scripts/getsettings" || exit 2
    19 
    20 
    21 ## diagnostics
    22 
    23 function usage()
    24 {
    25   echo
    26   echo "Usage: $PRG [OPTIONS] [INPUT] [OUTPUT]"
    27   echo
    28   echo "  Options are:"
    29   echo "    -I           startup Isar interaction mode"
    30   echo "    -P           startup Proof General interaction mode"
    31   echo "    -S           secure mode -- disallow critical operations"
    32   echo "    -T ADDR      startup process wrapper, with socket address"
    33   echo "    -W IN:OUT    startup process wrapper, with input/output fifos"
    34   echo "    -e MLTEXT    pass MLTEXT to the ML session"
    35   echo "    -f           pass 'Session.finish();' to the ML session"
    36   echo "    -m MODE      add print mode for output"
    37   echo "    -q           non-interactive session"
    38   echo "    -r           open heap file read-only"
    39   echo "    -u           pass 'use\"ROOT.ML\";' to the ML session"
    40   echo "    -w           reset write permissions on OUTPUT"
    41   echo
    42   echo "  INPUT (default \"$ISABELLE_LOGIC\") and OUTPUT specify in/out heaps."
    43   echo "  These are either names to be searched in the Isabelle path, or"
    44   echo "  actual file names (containing at least one /)."
    45   echo "  If INPUT is \"RAW_ML_SYSTEM\", just start the bare bones ML system."
    46   echo
    47   exit 1
    48 }
    49 
    50 function fail()
    51 {
    52   echo "$1" >&2
    53   exit 2
    54 }
    55 
    56 
    57 ## process command line
    58 
    59 # options
    60 
    61 ISAR=""
    62 PROOFGENERAL=""
    63 SECURE=""
    64 WRAPPER_SOCKET=""
    65 WRAPPER_FIFOS=""
    66 MLTEXT=""
    67 MODES=""
    68 TERMINATE=""
    69 READONLY=""
    70 NOWRITE=""
    71 
    72 while getopts "IPST:W:e:fm:qruw" OPT
    73 do
    74   case "$OPT" in
    75     I)
    76       ISAR=true
    77       ;;
    78     P)
    79       PROOFGENERAL=true
    80       ;;
    81     S)
    82       SECURE=true
    83       ;;
    84     T)
    85       WRAPPER_SOCKET="$OPTARG"
    86       ;;
    87     W)
    88       WRAPPER_FIFOS="$OPTARG"
    89       ;;
    90     e)
    91       MLTEXT="$MLTEXT $OPTARG"
    92       ;;
    93     f)
    94       MLTEXT="$MLTEXT Command_Line.tool0 Session.finish;"
    95       ;;
    96     m)
    97       if [ -z "$MODES" ]; then
    98         MODES="\"$OPTARG\""
    99       else
   100         MODES="\"$OPTARG\", $MODES"
   101       fi
   102       ;;
   103     q)
   104       TERMINATE=true
   105       ;;
   106     r)
   107       READONLY=true
   108       ;;
   109     u)
   110       MLTEXT="$MLTEXT use\"ROOT.ML\";"
   111       ;;
   112     w)
   113       NOWRITE=true
   114       ;;
   115     \?)
   116       usage
   117       ;;
   118   esac
   119 done
   120 
   121 shift $(($OPTIND - 1))
   122 
   123 
   124 # args
   125 
   126 INPUT=""
   127 OUTPUT=""
   128 
   129 if [ "$#" -ge 1 ]; then
   130   INPUT="$1"
   131   shift
   132 fi
   133 
   134 if [ "$#" -ge 1 ]; then
   135   OUTPUT="$1"
   136   shift
   137 fi
   138 
   139 [ "$#" -ne 0 ] && { echo "Bad args: $*"; usage; }
   140 
   141 
   142 ## check ML system
   143 
   144 [ -z "$ML_SYSTEM" ] && fail "Missing ML system settings! Unable to run Isabelle."
   145 
   146 
   147 ## input heap file
   148 
   149 [ -z "$INPUT" ] && INPUT="$ISABELLE_LOGIC"
   150 
   151 case "$INPUT" in
   152   RAW_ML_SYSTEM)
   153     INFILE=""
   154     ;;
   155   */*)
   156     INFILE="$INPUT"
   157     [ ! -f "$INFILE" ] && fail "Bad heap file: \"$INFILE\""
   158     ;;
   159   *)
   160     INFILE=""
   161     ISA_PATH=""
   162 
   163     splitarray ":" "$ISABELLE_PATH"; PATHS=("${SPLITARRAY[@]}")
   164     for DIR in "${PATHS[@]}"
   165     do
   166       DIR="$DIR/$ML_IDENTIFIER"
   167       ISA_PATH="$ISA_PATH  $DIR\n"
   168       [ -z "$INFILE" -a -f "$DIR/$INPUT" ] && INFILE="$DIR/$INPUT"
   169     done
   170 
   171     if [ -z "$INFILE" ]; then
   172       echo "Unknown logic \"$INPUT\" -- no heap file found in:" >&2
   173       echo -ne "$ISA_PATH" >&2
   174       exit 2
   175     fi
   176     ;;
   177 esac
   178 
   179 
   180 ## output heap file
   181 
   182 case "$OUTPUT" in
   183   "")
   184     if [ -z "$READONLY" -a -w "$INFILE" ]; then
   185       perl -e "exit (((stat('$INFILE'))[2] & 0222) != 0 ? 0 : 1);" && OUTFILE="$INFILE"
   186     fi
   187     ;;
   188   */*)
   189     OUTFILE="$OUTPUT"
   190     ;;
   191   *)
   192     mkdir -p "$ISABELLE_OUTPUT"
   193     OUTFILE="$ISABELLE_OUTPUT/$OUTPUT"
   194     ;;
   195 esac
   196 
   197 
   198 ## prepare tmp directory
   199 
   200 [ -z "$ISABELLE_TMP_PREFIX" ] && ISABELLE_TMP_PREFIX=/tmp/isabelle
   201 ISABELLE_PID="$$"
   202 ISABELLE_TMP="$ISABELLE_TMP_PREFIX$ISABELLE_PID"
   203 mkdir -p "$ISABELLE_TMP"
   204 
   205 
   206 ## run it!
   207 
   208 ML_SYSTEM_BASE=$(echo "$ML_SYSTEM" | cut -f1 -d-)
   209 
   210 [ -n "$MODES" ] && MLTEXT="Unsynchronized.change print_mode (append [$MODES]); $MLTEXT"
   211 
   212 [ -n "$SECURE" ] && MLTEXT="$MLTEXT; Secure.set_secure ();"
   213 
   214 NICE="nice"
   215 
   216 if [ -n "$WRAPPER_SOCKET" ]; then
   217   MLTEXT="$MLTEXT; Isabelle_Process.init_socket \"$WRAPPER_SOCKET\";"
   218 elif [ -n "$WRAPPER_FIFOS" ]; then
   219   splitarray ":" "$WRAPPER_FIFOS"; FIFOS=("${SPLITARRAY[@]}")
   220   [ "${#FIFOS[@]}" -eq 2 ] || fail "Expected IN:OUT fifo specification"
   221   [ -p "${FIFOS[0]}" ] || fail "Bad input fifo: ${FIFOS[0]}"
   222   [ -p "${FIFOS[1]}" ] || fail "Bad output fifo: ${FIFOS[1]}"
   223   MLTEXT="$MLTEXT; Isabelle_Process.init_fifos \"${FIFOS[0]}\" \"${FIFOS[1]}\";"
   224 else
   225   if [ -z "$ISABELLE_PROCESS_OPTIONS" ]; then
   226     ISABELLE_PROCESS_OPTIONS="$ISABELLE_TMP/options"
   227     "$ISABELLE_TOOL" options -x "$ISABELLE_PROCESS_OPTIONS" || fail "Failed to retrieve options"
   228   fi
   229   if [ "$INPUT" != RAW_ML_SYSTEM ]; then
   230     MLTEXT="Options.load_default () handle _ => exit 2; $MLTEXT"
   231   fi
   232   if [ -n "$PROOFGENERAL" ]; then
   233     MLTEXT="$MLTEXT; ProofGeneral.init ();"
   234   elif [ -n "$ISAR" ]; then
   235     MLTEXT="$MLTEXT; Isar.main ();"
   236   else
   237     NICE=""
   238   fi
   239 fi
   240 
   241 export INFILE OUTFILE MLTEXT TERMINATE NOWRITE ISABELLE_PID ISABELLE_TMP ISABELLE_PROCESS_OPTIONS
   242 
   243 if [ -f "$ISABELLE_HOME/lib/scripts/run-$ML_SYSTEM" ]; then
   244   $NICE "$ISABELLE_HOME/lib/scripts/run-$ML_SYSTEM"
   245 else
   246   $NICE "$ISABELLE_HOME/lib/scripts/run-$ML_SYSTEM_BASE"
   247 fi
   248 RC="$?"
   249 
   250 [ -n "$ISABELLE_PROCESS_OPTIONS" ] && rm -f "$ISABELLE_PROCESS_OPTIONS"
   251 rmdir "$ISABELLE_TMP"
   252 
   253 exit "$RC"