diff --git a/iocsh.bash b/iocsh.bash old mode 100644 new mode 100755 index 1dc215476903cb2292430ed9807574a172a9ae95..4de0c43c3a576f28fee32edab1942d9ee9a887b4 --- a/iocsh.bash +++ b/iocsh.bash @@ -1,10 +1,38 @@ #!/bin/bash +# +# Copyright (c) 2004 - 2017 Paul Scherrer Institute +# Copyright (c) 2017 - Present European Spallation Source ERIC +# +# The program is free software: you can redistribute +# it and/or modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, either version 2 of the +# License, or any newer version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see https://www.gnu.org/licenses/gpl-2.0.txt +# +# +# PSI original iocsh author : Dirk Zimoch +# ESS specific iocsh author : Jeong Han Lee +# email : han.lee@esss.se +# declare -gr SC_SCRIPT="$(realpath "$0")" declare -gr SC_SCRIPTNAME=${0##*/} declare -gr SC_TOP="$(dirname "$SC_SCRIPT")" +declare -g SC_VERSION="43be69b" declare -g STARTUP="" +# TODO +# when require install this script, at that moment, update this! +# +SC_VERSION="$(git rev-parse --short HEAD)" + set -a . ${SC_TOP}/ess-env.conf @@ -13,46 +41,21 @@ set +a . ${SC_TOP}/iocsh_functions -case $1 in - ( -h | "-?" | -help | --help ) - help - ;; - ( -v | -ver | --ver | -version | --version ) - version - ;; - ( -3.* ) - unset EPICS_BASE; - EPICS_BASE=$(select_epics_base "$1"); - shift - ;; -esac - -if [[ $(checkIfDir ${EPICS_BASE}) -eq "$NON_EXIST" ]]; then - - printf "EPICS_BASE is defined to use. \n"; - printf "Please check your environment!\n"; - exit; -fi - -declare -g RUNNING_EPICS_BASE_VER=${EPICS_BASE##*/*-} -declare -g RUNNING_REQUIRE_PATH=${REQUIRE_PATH}/R${RUNNING_EPICS_BASE_VER} +check_mandatory_env_settings STARTUP=/tmp/${SC_SCRIPTNAME}_${IOC}_startup.$BASHPID -declare -a ioc_env=(PWD EPICS_HOST_ARCH REQUIRE_PATH EPICS_CA_ADDR_LIST); - trap "softIoc_end" EXIT SIGTERM { - printIocEnv "${ioc_env}" - - loadRequire - loadFiles "$@" + printIocEnv; + loadRequire ; + loadFiles "$@"; if [ "$init" != NO ]; then printf "iocInit\n" fi - + } > ${STARTUP} @@ -61,4 +64,4 @@ ulimit -c unlimited # PREFIX:exit & PREFIX:BaseVersion PVs are added to softIoc # We can end this IOC via caput PREFIX:exit 1 -softIoc -D ${EPICS_BASE}/dbd/softIoc.dbd -x "TEST" "${STARTUP}" 2>&1 +softIoc -D ${EPICS_BASE}/dbd/softIoc.dbd "${STARTUP}" 2>&1 diff --git a/iocsh_functions b/iocsh_functions index 58ccaf69831f93c451fccd9792792657e5fd4d73..94f6a0289f93a69966f91f97f5804723f45b10a7 100644 --- a/iocsh_functions +++ b/iocsh_functions @@ -64,45 +64,6 @@ function checkIfVar() { } -function version () { - { - echo "European Spallation Souce ERIC iocsh" - } >&2 - exit -} - - - -function help () { - { - echo "usage: iocsh [options] [files]" - echo "Start an EPICS iocsh and load files" - echo "Recognized filetypes: *.db *.dbt *.template *.subs *.subst *.dbd *.so" - echo - echo "Possible options:" - echo " -3.xx.yy: Set EPICS base version." -# echo " -32: Force 32 bit version (on 64 bit systems)." - echo " -? or -h or --help: Show this page and exit." - echo " -v or --version: Show version and exit." - echo " -c: The next string is executed as a command by the EPICS shell." - echo " -s: The next string is a sequencer program (and arguments), run with 'seq'." - echo " This forces an 'iocInit' before running the program." - echo " -r: The next string is a module (and version), loaded via 'require'." - echo " -n: The next string is the IOC name (used for prompt)." - echo " Default: dirname if parent dir is \"ioc\" otherwise hostname." - echo - echo "Supported filetypes:" - echo "*.db, *.dbt and *.template are loaded via 'dbLoadRecords'." - echo " After the filename, you can specify substitutions like MACRO=value." - echo "*.subs and *.subst are loaded via 'dbLoadTemplate'." - echo "*.dbd is loaded via 'dbLoadDatabase'." - echo "*.so is loaded via 'ld' or 'dlload' (3.14.12 or higer)." - echo "If an argument is @file, more arguments are read from that file." - echo "All other files are executed as startup scripts by the EPICS shell." - } >&2 - exit -} - function subst () { subst="" @@ -121,29 +82,104 @@ function subst () { } -function printIocEnv() { +function select_epics_base() { + + local base_version=$1; + local epics_base=""; + epics_base=${EPICS}/base-${base_version#-} + echo ${epics_base} + +} + + +function version () { + printf "%s : %s%s\n" "European Spallation Source ERIC" "$SC_SCRIPTNAME" ${SC_VERSION:+" ($SC_VERSION)"} >&2 + exit +} + + - local var=""; +function printIocEnv () +{ + declare -a var_list=(); + var_list+=" "; var_list+=HOSTDISPLAY; + var_list+=" "; var_list+=WINDOWID; + var_list+=" "; var_list+=PWD; + var_list+=" "; var_list+=USER; + var_list+=" "; var_list+=LOGNAME; + var_list+=" "; var_list+=EPICS_HOST_ARCH; + var_list+=" "; var_list+=EPICS_LOCATION; + var_list+=" "; var_list+=EPICS; + var_list+=" "; var_list+=EPICS_MODULES; + var_list+=" "; var_list+=REQUIRE; + var_list+=" "; var_list+=REQUIRE_VERSION; + var_list+=" "; var_list+=REQUIRE_BIN + var_list+=" "; var_list+=REQUIRE_LIB; + var_list+=" "; var_list+=REQUIRE_DBD; + var_list+=" "; var_list+=EPICS_CA_AUTO_ADDR_LIST; + var_list+=" "; var_list+=EPICS_CA_ADDR_LIST; + var_list+=" "; var_list+=LD_LIBRARY_PATH; - printf "# date=\"$(date)\"\n" - printf "# user=\"${USER:-$(whoami)}\"\n" + printf "#\n"; + printf "# Start at \"$(date)\"\n" + printf "#\n"; + printf "# %s : %s%s\n" "European Spallation Source ERIC" "$SC_SCRIPTNAME" ${SC_VERSION:+" ($SC_VERSION)"}; + printf "#\n"; + - for var in PWD EPICS_HOST_ARCH REQUIRE_PATH EPICS_CA_ADDR_LIST - do + for var in ${var_list[@]}; do printf "# $var=\"${!var}\"\n" done - printf "\n"; + printf "#\n"; + printf "# Check which version and other environment variables\n"; + printf "#\n"; +} + + + +# Ctrl+c : OK +# exit : OK +# kill softioc process : OK +# kill main precess : Enter twice in terminal, +# close softIoc, but STATUP file is remained. +# + + + +function softIoc_end() { + local pids=$(jobs -pr); + [ -n "$pids" ] && kill $pids; + stty sane; + printf "\n\n"; + + rm -f $STARTUP; + } -function loadRequire() { + +function die() #@ Print error message and exit with error code +{ + #@ USAGE: die [errno [message]] + error=${1:-1} + ## exits with 1 if error number not given + shift + [ -n "$*" ] && + printf "%s%s: %s\n" "$SC_SCRIPTNAME" ${SC_VERSION:+" ($SC_VERSION)"} "$*" >&2 + exit "$error" +} + + + +function loadRequire() +{ local libPrefix=lib local libPostfix=.so local libName=${libPrefix}${REQUIRE}${libPostfix} - local require_lib=${RUNNING_REQUIRE_PATH}/lib/${EPICS_HOST_ARCH}/${libName} - local require_dbd=${RUNNING_REQUIRE_PATH}/dbd/${REQUIRE}.dbd + local require_lib=${REQUIRE_LIB}/${EPICS_HOST_ARCH}/${libName} + local require_dbd=${REQUIRE_DBD}/${REQUIRE}.dbd local load_cmd="dlload" @@ -154,102 +190,126 @@ function loadRequire() { } +function check_mandatory_env_settings () +{ + declare -a var_list=(); + + var_list+=EPICS_HOST_ARCH; + var_list+=" "; + var_list+=EPICS_LOCATION; + var_list+=" "; + var_list+=EPICS; + var_list+=" "; + var_list+=EPICS_MODULES; + var_list+=" "; + var_list+=EPICS_BASE; + var_list+=" "; + var_list+=REQUIRE; + var_list+=" "; + var_list+=REQUIRE_BIN + var_list+=" "; + var_list+=REQUIRE_LIB; + var_list+=" "; + var_list+=REQUIRE_DBD; + var_list+=" "; + var_list+=REQUIRE_VERSION; + + for var in ${var_list[@]}; do + if [[ $(checkIfVar ${!var}) -eq "$NON_EXIST" ]]; then + die 1 " $var is not defined!. Check e3-env/setE3Env.bash" + fi + done +} + + + function loadFiles () { - while [ "$#" -gt 0 ] - do + while [ "$#" -gt 0 ]; do file=$1 case $file in - ( -h | "-?" | -help | --help ) - help - ;; - ( -v | -ver | --ver | -version | --version ) - version - ;; - ( @* ) - loadFiles $(cat ${file#@}) - ;; - ( *.db | *.template) - subst="" - while [ "$#" -gt 1 ] - do - case $2 in - ( *=* ) - subst="$subst,$2"; shift - ;; - ( * ) - break - ;; + -h | "-?" | -help | --help ) + help + ;; + -v | -ver | --ver | -version | --version ) + version + ;; + @* ) + loadFiles $(cat ${file#@}) + ;; + *.db | *.template) + subst="" + while [ "$#" -gt 1 ]; do + case $2 in + ( *=* ) + subst="$subst,$2"; shift + ;; + ( * ) + break + ;; + esac + done + echo "dbLoadRecords \"$file\",\"${subst#,}\"" + ;; + *.subs | *.subst ) + echo "dbLoadTemplate \"$file\"" + ;; + *.dbd ) + # some dbd files must be loaded before main to take effect + echo "dbLoadDatabase \"$file\",\"$DBD\"" + ;; + *.so ) + if [ $BASEMINOR -ge 15 -o $BASEPATCH -ge 12 ]; then + echo "dlload \"$file\"" + else + echo "ld \"$file\"" + fi + ;; + -c ) + shift + case $1 in + seq* ) + if [ "$init" != NO ]; then + echo "iocInit" + init=NO + fi + ;; + iocInit ) + init=NO + ;; esac - done - echo "dbLoadRecords \"$file\",\"${subst#,}\"" - ;; - ( *.subs | *.subst ) - echo "dbLoadTemplate \"$file\"" - ;; - ( *.dbd ) - # some dbd files must be loaded before main to take effect - echo "dbLoadDatabase \"$file\",\"$DBD\"" - ;; - ( *.so ) - if [ $BASEMINOR -ge 15 -o $BASEPATCH -ge 12 ] - then - echo "dlload \"$file\"" - else - echo "ld \"$file\"" - fi - ;; - ( -c ) - shift - case $1 in - ( seq* ) - if [ "$init" != NO ] - then - echo "iocInit" - init=NO + echo $1 + ;; + -s ) + shift + if [ "$init" != NO ]; then + echo "iocInit" + init=NO fi + echo "seq $1" ;; - ( iocInit ) - init=NO + -r ) + shift + echo "require $1" + ;; + -n ) + shift + IOC="$1" + ;; + # -32 ) + # echo "-32 option must come before all others (except -3.xx.yy)" >&2 + # exit 1 + # ;; + -* ) + { + echo "unknown option $1" + echo "try: $(basename $0) --help" + } >&2 + exit 1 + ;; + * ) + echo "< \"$file\"" + if grep -q iocInit $file; then init=NO; fi ;; - esac - echo $1 - ;; - ( -s ) - shift - if [ "$init" != NO ] - then - echo "iocInit" - init=NO - fi - echo "seq $1" - ;; - ( -r ) - shift - echo "require $1" - ;; - ( -n ) - shift - IOC="$1" - ;; - ( -3.* ) - echo "Version $file must be first argument" >&2 - exit 1 - ;; - ( -32 ) - echo "-32 option must come before all others (except -3.xx.yy)" >&2 - exit 1 - ;; - ( -* ) - { - echo "unknown option $1" - echo "try: $(basename $0) --help" - } >&2 - exit 1 - ;; - ( * ) - echo "< \"$file\"" - if grep -q iocInit $file; then init=NO; fi - ;; esac shift done @@ -258,31 +318,32 @@ function loadFiles () { -function select_epics_base() { - - local base_version=$1; - local epics_base=""; - epics_base=${EPICS_LOCATION}/base-${base_version#-} - echo ${epics_base} - -} - -# Ctrl+c : OK -# exit : OK -# kill softioc process : OK -# kill main precess : Enter twice in terminal, -# close softIoc, but STATUP file is remained. -# - - -function softIoc_end() { - local pids=$(jobs -pr); - [ -n "$pids" ] && kill $pids; - stty sane; - printf "\n\n"; - - rm -f $STARTUP; - - +function help () { + { + echo "usage: iocsh [options] [files]" + echo "Start an EPICS iocsh and load files" + echo "Recognized filetypes: *.db *.dbt *.template *.subs *.subst *.dbd *.so" + echo + echo "Possible options:" +# echo " -3.xx.yy: Set EPICS base version." +# echo " -32: Force 32 bit version (on 64 bit systems)." + echo " -? or -h or --help: Show this page and exit." + echo " -v or --version: Show version and exit." + echo " -c: The next string is executed as a command by the EPICS shell." + echo " -s: The next string is a sequencer program (and arguments), run with 'seq'." + echo " This forces an 'iocInit' before running the program." + echo " -r: The next string is a module (and version), loaded via 'require'." + echo " -n: The next string is the IOC name (used for prompt)." + echo " Default: dirname if parent dir is \"ioc\" otherwise hostname." + echo + echo "Supported filetypes:" + echo "*.db, *.dbt and *.template are loaded via 'dbLoadRecords'." + echo " After the filename, you can specify substitutions like MACRO=value." + echo "*.subs and *.subst are loaded via 'dbLoadTemplate'." + echo "*.dbd is loaded via 'dbLoadDatabase'." +# echo "*.so is loaded via 'ld' or 'dlload' (3.14.12 or higer)." + echo "If an argument is @file, more arguments are read from that file." + echo "All other files are executed as startup scripts by the EPICS shell." + } >&2 + exit } -