D1779.func-source-and-lineno.sh (2708B)
1 #!/bin/bash 2 3 function ble/function#get-source-and-lineno/.extract { 4 local command=$1 5 if [[ ${FUNCNAME[1]-} == "$_ble_util_function_util" && ${FUNCNAME[2]-} != "$_ble_util_function_util" ]]; then 6 return 1 7 else 8 if [[ ${FUNCNAME[1]-} == "$_ble_util_function_name" ]]; then 9 local src=${BASH_SOURCE[1]} 10 local line=${BASH_LINENO[0]} 11 echo "$src:$line" >/dev/tty 12 if [[ -s $src ]]; then 13 less +"${line}g" "$src" 14 fi 15 else 16 declare -p BASH_SOURCE BASH_LINENO FUNCNAME >/dev/tty 17 fi 18 return 0 19 fi 20 } 1>&11 2>&12 21 22 function ble/function#get-source-and-lineno { 23 local _ble_util_function_name=$1 24 local _ble_util_function_util=$FUNCNAME 25 if ble/is-function "$_ble_util_function_name"; then 26 ( 27 declare -ft "$_ble_util_function_name" 28 builtin trap 'ble/function#get-source-and-lineno/.extract && return 0' DEBUG 29 "$_ble_util_function_name" 30 ) 11>&1 12>&2 31 fi 32 } 33 34 function ble/function#get-source-and-lineno.impl2 { 35 local ret unset_extdebug= 36 if ! shopt -q extdebug; then 37 unset_extdebug=1 38 shopt -s extdebug 39 fi 40 ble/util/assign ret "declare -F '$1' &>/dev/null"; local ext=$? 41 if [[ $unset_extdebug ]]: then 42 shopt -u extdebug 43 fi 44 45 if ((ext==0)); then 46 ret=${ret#*' '} 47 lineno=${ret%%' '*} 48 source=${ret#*' '} 49 fi 50 return "$ext" 51 } 52 53 function ble/function#get-source-and-lineno.impl3 { 54 local ret shopt=$BASHOPTS # 古い bash で使えない 55 shopt -s extdebug 56 ble/util/assign ret "declare -F '$1' &>/dev/null"; local ext=$? 57 [[ :$unset_extdebug: == *:extdebug:* ]] || shopt -u extdebug 58 59 if ((ext==0)); then 60 ret=${ret#*' '} 61 lineno=${ret%%' '*} 62 source=${ret#*' '} 63 fi 64 return "$ext" 65 } 66 67 function ble/function#get-source-and-lineno.impl4 { 68 local ret ext 69 if ! shopt -q extdebug; then 70 shopt -s extdebug 71 ble/util/assign ret "declare -F '$1' &>/dev/null"; ext=$? 72 shopt -u extdebug 73 else 74 ble/util/assign ret "declare -F '$1' &>/dev/null"; ext=$? 75 fi 76 if ((ext==0)); then 77 ret=${ret#*' '} 78 lineno=${ret%%' '*} 79 source=${ret#*' '} 80 fi 81 return "$ext" 82 } 83 84 function ble/function#get-source-and-lineno.impl2a { 85 local ret unset_extdebug= 86 shopt -q extdebug || { unset_extdebug=1; shopt -s extdebug; } 87 ble/util/assign ret "declare -F '$1' &>/dev/null"; local ext=$? 88 [[ ! $unset_extdebug ]] || shopt -u extdebug 89 if ((ext==0)); then 90 ret=${ret#*' '} 91 lineno=${ret%%' '*} 92 source=${ret#*' '} 93 fi 94 return "$ext" 95 } 96 97 #ble/function#get-source-and-lineno ble/util/assign 98 #ble/function#get-source-and-lineno ble/function#get-source-and-lineno 99 #ble/function#get-source-and-lineno ble/util/is-stdin-ready 100 #ble/function#get-source-and-lineno ble/util/setexit