nify the handler functions
Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
parent
f68c9e817b
commit
a77e18d908
75
sx-open
75
sx-open
|
@ -18,53 +18,36 @@ act() {
|
|||
return 0
|
||||
}
|
||||
|
||||
# handle_uri <res> <uri>
|
||||
# handle_target <res> <uri>
|
||||
# 1: cmd failed
|
||||
# 3: no handler
|
||||
handle_uri() {
|
||||
handle_target() {
|
||||
declare -n result=$1
|
||||
declare h cmd regex target=$2
|
||||
|
||||
set -- "${uri_handlers[@]}"
|
||||
|
||||
while (( $# )); do
|
||||
cmd=$1; regex=$2
|
||||
|
||||
if [[ "$target" =~ $regex ]]; then
|
||||
act ${cmd} "$target"; result=$?
|
||||
(( result )) && return 1
|
||||
|
||||
return 0
|
||||
fi
|
||||
|
||||
shift 2
|
||||
done
|
||||
|
||||
return 3
|
||||
}
|
||||
|
||||
# handle_file <res> <file>
|
||||
# 1: cmd failed
|
||||
# 2: no such file
|
||||
# 3: no handler
|
||||
handle_file() {
|
||||
declare -n result=$1
|
||||
declare m \
|
||||
target_mimetype charset \
|
||||
cmd regex \
|
||||
declare h cmd regex target_is_file target target_left
|
||||
target_is_file=0
|
||||
target=$2
|
||||
target_left=$target
|
||||
|
||||
[[ -e "$target" ]] || return 3
|
||||
if [[ -e "$target" ]]; then
|
||||
target_is_file=1
|
||||
|
||||
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
|
||||
|
||||
IFS=';' read target_mimetype charset <<< $( file -ib "$target" )
|
||||
target_left=$target_mimetype
|
||||
|
||||
set -- "${mime_handlers[@]}"
|
||||
elif is_uri "$target"; then
|
||||
set -- "${uri_handlers[@]}"
|
||||
else
|
||||
return 2
|
||||
fi
|
||||
|
||||
while (( $# )); do
|
||||
cmd=$1; regex=$2
|
||||
cmd=( $1 ); regex=$2
|
||||
|
||||
if [[ "$target_mimetype" =~ $regex ]]; then
|
||||
act ${cmd} "$target"; result=$?
|
||||
if [[ "$target_left" =~ $regex ]]; then
|
||||
act "${cmd[@]}" "$target"; result=$?
|
||||
(( result )) && return 1
|
||||
|
||||
return 0
|
||||
|
@ -89,7 +72,7 @@ mime() {
|
|||
declare r handler=$1; shift
|
||||
|
||||
for r in "$@"; do
|
||||
uri_handlers+=( "$handler" "$r" )
|
||||
mime_handlers+=( "$handler" "$r" )
|
||||
done
|
||||
}
|
||||
|
||||
|
@ -130,25 +113,21 @@ main() {
|
|||
# Treat file:// as local paths.
|
||||
[[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
|
||||
|
||||
if [[ -e "$target" ]]; then
|
||||
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
|
||||
|
||||
handle_file cmd_result "$target"
|
||||
elif is_uri "$target"; then
|
||||
handle_uri cmd_result "$target"
|
||||
else
|
||||
printf 'No such file or directory: %s\n' "$target" >&2
|
||||
return 2
|
||||
fi
|
||||
handle_target cmd_result "$target"
|
||||
|
||||
case $? in
|
||||
(1)
|
||||
printf 'Action failed with exit code: %s\n' "$cmd_result" >&2
|
||||
printf 'Action failed with exit code: “%s”\n' "$cmd_result" >&2
|
||||
return 4
|
||||
;;
|
||||
|
||||
(2)
|
||||
printf 'No such file or directory: “%s”\n' "$target" >&2
|
||||
return 2
|
||||
;;
|
||||
|
||||
(3)
|
||||
printf 'No handlers found\n' >&2
|
||||
printf 'No handlers found for “%s”\n' "$target" >&2
|
||||
return 3
|
||||
;;
|
||||
esac
|
||||
|
|
Loading…
Reference in New Issue
Block a user