123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #!/bin/bash
-
- FFOO_DIR=${FFOO_DIR:-__FFOO_DIR__}
- FFOO_DEBUG=${FFOO_DEBUG:-false}
- FFOO_DEBUGINIT=${FFOO_DEBUGINIT:-false}
- FFOO_IMPORTED=${FFOO_IMPORTED:-}
- FFOO_INCLUDE=${FFOO_INCLUDE:-$FFOO_DIR/include}
- FFOO_PRETTY=${FFOO_PRETTY:-plain}
- FFOO_PATH=${FFOO_PATH:-}
- FFOO_VERBOSE=${FFOO_VERBOSE:-false}
- FFOO_VERSION=__VERSION__
-
- ffoo() {
- case $1 in
- __debug)
- $FFOO_DEBUGINIT || return 0
- shift
- echo "debug: $@" >&2
- ;;
- __die)
- shift
- echo "$@" >&2
- test -z "$PS1" && exit 3
- return 3
- ;;
- __warn)
- shift
- echo "$@" >&2
- ;;
-
- doc)
- ffoo _cat_function "$2" | ffoo _filter_doc
- ;;
- import)
- local mname=$2
- ffoo is_imported $mname && return 0
- if ffoo _do_import $mname;
- then
- FFOO_IMPORTED="$FFOO_IMPORTED${FFOO_IMPORTED:+:}$mname"
- else
- ffoo __die "cannot import module: $mname"
- fi
- ;;
- try_import)
- local mname=$2
- ( ffoo _do_import $mname )
- ;;
- is_imported)
- local mname=$2
- echo "$FFOO_IMPORTED" | tr : \\n | grep -qx "$mname"
- ;;
-
- _do_import)
- local es=4
- ffoo __debug "importing module $mname"
- local mpath=$(ffoo _select_mfile $mname)
- test -n "$mpath" || {
- ffoo __warn "cannot find module: $mname"
- return 3
- }
- ffoo __debug "found module file: $mpath"
- bash -n $mpath || return 3
- . $mpath
- es=$?
- local init=__ffoo_${mname}__init
- if test "$(type -t $init)" == function;
- then
- ffoo __debug "launching init function: $init"
- $init
- es=$?
- fi
- return $es
- ;;
-
- _parse)
- # convert stdin from $1 to $2 (can be 1:N conversion)
- local src=$2
- local tgt=$3
- local line
- while read line;
- do
- ffoo _parse_${src}_to_${tgt} "$line"
- done
- ;;
- _parse_path_to_mod)
- local fname="${2##*/}"; echo "${fname%%.sh}"
- ;;
- _parse_fspec_to_mod)
- echo "${2%%.*}"
- ;;
- _parse_fspec_to_fun)
- local mod=$(ffoo _parse_fspec_to_mod "$2")
- echo "${2##$mod.}"
- ;;
- _parse_path_to_fspeclist)
- grep -HE '^[[:alnum:]_]+\(\) \{' "$2" \
- | sed -e 's/^.*\///; s/\.sh:/./; s/(.*//'
- ;;
- _parse_path_to_fnlist)
- grep -E '^[[:alnum:]_]+\(\) \{' "$2" \
- | sed -e 's/^.*\///; s/(.*//'
- ;;
-
- _cat_function)
- local mod=$(ffoo _parse_fspec_to_mod "$2")
- local fun=$(ffoo _parse_fspec_to_fun "$2")
- local mfname=$(ffoo _select_mfile "$mod")
- test -n "$mfname" || ffoo __die "unknown module: $mod"
- name=$fun perl -we '
- undef $/;
- my $name = $ENV{name};
- my ($fbody) = <> =~ m/^($name\(\) \{.*?^\}$)/ms;
- print "$fbody\n" if defined $fbody;
- ' < "$mfname"
- ;;
- _filter_doc)
- # 1. line "^ #$" opens doc
- # 2. first line that is not "^ #" closes doc
- # 3. first and last line will be stripped
- perl -we '
- my $isdoc;
- while (<>) {
- if (m/^ #$/) {
- $isdoc = 1;
- } elsif (m/^ [^#]/) {
- exit;
- }
- next unless $isdoc;
- s/ # ?//;
- print;
- }' \
- | head -n -1 \
- | tail -n +2
- ;;
-
- _list_modules)
- ffoo _list_mfiles "$2" | ffoo _parse path mod
- ;;
- _list_mfiles)
- local ex="${2:-*}"
- find $FFOO_INCLUDE ${FFOO_PATH/:/ } -name "$ex.sh"
- ;;
- _select_mfile)
- ffoo _list_mfiles $2 | head -1
- ;;
- _list_functions)
- ffoo _list_mfiles "$2" | ffoo _parse path fspeclist
- ;;
- _list_functions_in)
- ffoo _list_mfiles "$2" | ffoo _parse path fnlist
- ;;
-
- *)
- ffoo __die "unknown ffoo command: $1"
- ;;
- esac
- }
-
- ffoo __debug "FFOO_DIR='$FFOO_DIR'"
- ffoo __debug "FFOO_DEBUG='$FFOO_DEBUG'"
- ffoo __debug "FFOO_DEBUGINIT='$FFOO_DEBUGINIT'"
- ffoo __debug "FFOO_IMPORTED='$FFOO_IMPORTED'"
- ffoo __debug "FFOO_INCLUDE='$FFOO_INCLUDE'"
- ffoo __debug "FFOO_PRETTY='$FFOO_PRETTY'"
- ffoo __debug "FFOO_PATH='$FFOO_PATH'"
- ffoo __debug "FFOO_VERBOSE='$FFOO_VERBOSE'"
- ffoo __debug "FFOO_VERSION='$FFOO_VERSION'"
|