123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #!/bin/sh
- #shellcheck disable=SC2039,SC1090
-
- #
- # Shell compatibility
- #
- # This should be the binary name of the shell we're running and is used to
- # choose where to look for modules
- #
- SHELLFU_COMPAT=${SHELLFU_COMPAT:-$(ps -p$$ -ocmd= | cut -d' ' -f1 | rev | cut -d/ -f1 | rev)}
-
- #
- # Our installation directory
- #
- SHELLFU_DIR=${SHELLFU_DIR:-__SHELLFU_DIR__}
-
- #
- # Shellfu internals debug mode
- #
- SHELLFU_DEBUGINIT=${SHELLFU_DEBUGINIT:-false}
-
- #
- # Target to list imported functions in embedding mode
- #
- SHELLFU_EMBEDTMP=${SHELLFU_EMBEDTMP:-}
-
- #
- # True if running in embedded mode (imports do nothing)
- #
- SHELLFU_EMBEDDED=${SHELLFU_EMBEDDED:-false}
-
- #
- # Last git commit hash before build
- #
- SHELLFU_GIT_HASH=__MKIT_PROJ_GIT_LASTHASH__
-
- #
- # Includes folder prefix
- #
- # Modules are searched for in path formed by this prefix, plus
- # 'sh', plus run-time value of SHELLFU_COMPAT, and if set, in
- # directories listed in SHELLFU_PATH
- #
- SHELLFU_INCLUDE=${SHELLFU_INCLUDE:-$SHELLFU_DIR/include}
-
- #
- # Colon separated list of additional paths to search modules
- #
- SHELLFU_PATH=${SHELLFU_PATH:-}
-
- #
- # Shellfu version (SemVer 2.0)
- #
- SHELLFU_VERSION=__MKIT_PROJ_VERSION__
-
- #
- # Colon-separated list of already imported modules
- #
- __SHELLFU_IMPORTED=${__SHELLFU_IMPORTED:-}
-
- shellfu() {
- local cmd=$1; shift
- local msg
-
- # debug on import
- case $SHELLFU_DEBUGINIT:$1 in
- true:import)
- shellfu __debug "\$*='$*'"
- shellfu __debug "SHELLFU_COMPAT='$SHELLFU_COMPAT'"
- shellfu __debug "SHELLFU_DIR='$SHELLFU_DIR'"
- shellfu __debug "SHELLFU_DEBUGINIT='$SHELLFU_DEBUGINIT'"
- shellfu __debug "SHELLFU_EMBEDDED='$SHELLFU_EMBEDDED'"
- shellfu __debug "SHELLFU_EMBEDTMP='$SHELLFU_EMBEDTMP'"
- shellfu __debug "SHELLFU_GIT_HASH='$SHELLFU_GIT_HASH'"
- shellfu __debug "__SHELLFU_IMPORTED='$__SHELLFU_IMPORTED'"
- shellfu __debug "SHELLFU_INCLUDE='$SHELLFU_INCLUDE'"
- shellfu __debug "SHELLFU_PATH='$SHELLFU_PATH'"
- shellfu __debug "SHELLFU_VERSION='$SHELLFU_VERSION'"
- ;;
- esac
-
- case $cmd in
- __debug)
- $SHELLFU_DEBUGINIT || return 0
- for msg in "$@";
- do
- echo "shellfu:debug: $msg" >&2
- done
- ;;
- __die)
- for msg in "$@";
- do
- echo "shellfu:fatal: $msg" >&2
- done
- test -z "$PS1" && exit 3
- return 3
- ;;
- __warn)
- for msg in "$@";
- do
- echo "shellfu: $msg" >&2
- done
- ;;
-
- import)
- #
- # Import module named $1
- #
- local mname=$1
- shellfu _is_imported "$mname" && return 0
- if shellfu _do_import "$mname";
- then
- __SHELLFU_IMPORTED="$__SHELLFU_IMPORTED${__SHELLFU_IMPORTED:+:}$mname"
- else
- shellfu __die "cannot import module: $mname"
- fi
- ;;
- try_import)
- #
- # Try if module $1 could be imported
- #
- local mname=$1
- ( shellfu _do_import "$mname" )
- ;;
-
- _do_import)
- #
- # Really import module named $1
- #
- $SHELLFU_EMBEDDED && return 0
- local mname=$1
- local es=4
- local mpath
- shellfu __debug "importing module $mname"
- mpath=$(shellfu _select_mfile "$mname") || {
- shellfu __warn "cannot find module: $mname"
- return 3
- }
- shellfu __debug "found module file: $mpath"
- bash -n "$mpath" || return 3
- . "$mpath"
- es=$?
- test -n "$SHELLFU_EMBEDTMP" && echo "$mpath" >> "$SHELLFU_EMBEDTMP"
- local init=__shellfu_${mname}__init
- if test "$(command -v "$init")" = "$init";
- then
- shellfu __debug "launching init function: $init"
- $init
- es=$?
- fi
- return $es
- ;;
- _is_imported)
- #
- # True if module $1 is already imported
- #
- local mname=$1
- echo "$__SHELLFU_IMPORTED" | tr : \\n | grep -qx "$mname"
- ;;
-
- _list_mfiles)
- #
- # Find module files of module matching $1 (wildcard expression)
- #
- local ex="${1:-*}"
- local incdir
- echo "$SHELLFU_INCLUDE-sh:$SHELLFU_INCLUDE-$SHELLFU_COMPAT:$SHELLFU_PATH" \
- | tr ":" '\n' \
- | awk '!seen[$0]++' \
- | while read -r incdir;
- do
- shellfu __debug "checking: $incdir"
- test -d "$incdir" || continue
- shellfu __debug "looking into: $incdir"
- find "$incdir" -name "$ex.sh"
- done
- ;;
- _select_mfile)
- #
- # Select file that contains module named $1
- #
- shellfu _list_mfiles "$1" | grep -m1 .
- ;;
-
- *)
- shellfu __die "unknown shellfu command: $cmd"
- ;;
- esac
- }
|