halrun.in
1 #!/bin/bash 2 3 PATH=@EMC2_BIN_DIR@:$PATH 4 REALTIME=@REALTIME@ 5 THIS=$(basename $0) 6 7 export HAL_RTMOD_DIR=@EMC2_RTLIB_DIR@ 8 9 help () { 10 halcmd -h 11 cat <<EOF 12 13 $THIS Usage: 14 15 $THIS [-I] [halcmd_opts] [filename[.hal|.tcl]] 16 $THIS -T [halcmd_opts] [filename[.hal|.tcl]] 17 18 $THIS -h this help 19 $THIS -U forcibly cause the realtime environment to exit 20 21 filename[.hal|.tcl] may also be specified as '-f filename[.hal|.tcl]' 22 halcmd_opts apply for .hal files only 23 24 Interactive if: no filename (runs halcmd) 25 or -I (runs halcmd) 26 or -T (runs haltcl) 27 28 EOF 29 } 30 31 INTERACTIVE="" 32 inifile="" 33 theargs="" 34 while getopts "ef:hi:kqsvIRQTUV" opt ; do 35 case $opt in 36 h) help; exit 0;; 37 38 U) halcmd -R 39 halcmd stop 40 halcmd unload all 41 $REALTIME stop 42 exit 0;; 43 44 f) filename=$OPTARG;; 45 i) inifile=$OPTARG;; 46 47 I) INTERACTIVE="halcmd -kf";; 48 T) INTERACTIVE="haltcl";; 49 50 e) theargs="$theargs -$opt";; 51 k) theargs="$theargs -$opt";; 52 q) theargs="$theargs -$opt";; 53 s) theargs="$theargs -$opt";; 54 v) theargs="$theargs -$opt";; 55 R) theargs="$theargs -$opt";; 56 Q) theargs="$theargs -$opt";; 57 V) theargs="$theargs -$opt";; 58 \?) echo "" 59 echo "For usage try: $THIS -h" 60 exit 1;; 61 esac 62 done 63 shift $(($OPTIND - 1)) 64 65 if [ $# -gt 1 ] ; then 66 echo "$THIS: too many arguments <$*>" 67 exit 1 68 fi 69 70 # filename can be specified two ways: 71 # as parameter for -f ('f filename') 72 # or as trailing parameter 73 if [ $# -gt 0 ] ; then 74 if [ "X$filename" = "X" ] ; then 75 filename=$1 76 shift 77 else 78 echo "$THIS: Error: Specified '-f $1' and also <$filename>" 79 exit 1 80 fi 81 fi 82 83 if $REALTIME status > /dev/null; then 84 echo "$THIS: Realtime already running. Use 'halrun -U' to stop existing realtime session." 1>&2 85 exit 1 86 fi 87 88 HAVEFILE=false 89 IS_HALTCL=false 90 IS_INI=false 91 case $filename in 92 *.hal) HAVEFILE=true 93 if [ -n "$inifile" ] ; then 94 theargs="$theargs -i $inifile" 95 fi 96 # halcmd uses all $theargs: 97 set -- "$theargs -f $filename";; 98 *.tcl) HAVEFILE=true 99 IS_HALTCL=true 100 # haltcl uses only -i arg 101 if [ -n "$inifile" ] ; then 102 tclargs="-i $inifile" 103 else 104 tclargs="" 105 fi 106 # haltcl only uses inifilename and filename: 107 set -- "$tclargs $filename";; 108 *.ini) HAVEFILE=true 109 IS_INI=true 110 ;; 111 "") # for nil filename, support interactive halcmd 112 # or haltcl if -T was used 113 if [ "$INTERACTIVE" != "haltcl" ] ; then 114 INTERACTIVE="halcmd $theargs -kf" 115 fi 116 ;; 117 *) echo "$THIS: Unknown file extension for filename=<$filename>" 118 exit 1 119 ;; 120 esac 121 122 case "$INTERACTIVE" in 123 halcmd*) if [ -n "$inifile" ] ; then 124 INTERACTIVE="$INTERACTIVE -i $inifile" 125 fi 126 ;; 127 haltcl) if $IS_HALTCL ; then 128 if [ "$theargs" != "" ] ; then 129 echo "$THIS: args not accepted for haltcl <$theargs>" 130 exit 1 131 fi 132 else 133 # allowed -T (tcl interactive) with startup .hal file 134 : 135 fi 136 if [ -n "$inifile" ] ; then 137 INTERACTIVE="haltcl -i $inifile" 138 fi 139 ;; 140 esac 141 142 $REALTIME start || exit $? 143 144 if $HAVEFILE ; then 145 if $IS_INI; then 146 if ! inivar -ini "$filename" -sec HAL -var TWOPASS > /dev/null 2>&1 ; then 147 echo "$THIS: A .ini file may only be used if it specifies [HAL]TWOPASS" 148 result=1 149 else 150 halcmd_twopass "$filename"; result=$? 151 fi 152 elif $IS_HALTCL; then 153 haltcl $@; result=$? 154 else 155 halcmd $@; result=$? 156 fi 157 fi 158 159 if [ ! -z "$INTERACTIVE" ]; then $INTERACTIVE; fi 160 161 halcmd stop || result=$? 162 halcmd unload all || result=$? 163 164 $REALTIME stop || result=$? 165 166 exit $result