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