#!/bin/bash FFOO_DIR=${FFOO_DIR:-__FFOO_DIR__} FFOO_DEBUG=${FFOO_DEBUG:-false} FFOO_DEBUGINIT=${FFOO_DEBUGINIT:-false} FFOO_INCLUDE=${FFOO_INCLUDE:-$FFOO_DIR/include} FFOO_MKPRETTY=${FFOO_MKPRETTY:-plain} 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 1 ;; doc) ffoo _cat_function "$2" | ffoo _filter_doc ;; import) ffoo __debug "importing module $2" local mfile=$(ffoo _select_mfile $2) test -n "$mfile" || ffoo __die "cannot find module: $2" ffoo __debug "found module file: $mfile" bash -n $mfile && . $mfile return $? ;; _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_INCLUDE='$FFOO_INCLUDE'" ffoo __debug "FFOO_INI_PATH='$FFOO_INI_PATH'" ffoo __debug "FFOO_MKPRETTY='$FFOO_MKPRETTY'" ffoo __debug "FFOO_VERBOSE='$FFOO_VERBOSE'" ffoo __debug "FFOO_VERSION='$FFOO_VERSION'"