2014-09-25 13:26:15 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# This is an attempt to replace xdg-open with something sane.
|
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
usage() {
|
|
|
|
cat <<- EOF
|
|
|
|
sx-open [-dhv] <uri/file>
|
|
|
|
Flags:
|
|
|
|
-d Dry run
|
|
|
|
-v Verbose
|
|
|
|
-h Help
|
|
|
|
EOF
|
|
|
|
}
|
2014-09-25 13:26:15 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
act() {
|
|
|
|
(( verbose )) && printf 'CMD: %s\n' "$*" >&2
|
|
|
|
(( dry_run )) || { "$@"; return $?; }
|
2014-09-25 13:26:15 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
# handle_target <res> <uri>
|
2018-07-27 03:32:29 +00:00
|
|
|
# 1: cmd failed
|
|
|
|
# 3: no handler
|
2018-07-27 14:35:51 +00:00
|
|
|
handle_target() {
|
2018-07-27 03:32:29 +00:00
|
|
|
declare -n result=$1
|
2018-07-27 14:35:51 +00:00
|
|
|
declare h cmd regex target_is_file target target_left
|
|
|
|
target_is_file=0
|
2018-07-27 03:32:29 +00:00
|
|
|
target=$2
|
2018-07-27 14:35:51 +00:00
|
|
|
target_left=$target
|
2018-07-27 03:32:29 +00:00
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
if [[ -e "$target" ]]; then
|
|
|
|
target_is_file=1
|
2018-07-27 03:32:29 +00:00
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
|
2014-09-25 13:26:15 +00:00
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
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
|
2018-07-27 05:05:14 +00:00
|
|
|
|
|
|
|
while (( $# )); do
|
2018-07-27 14:35:51 +00:00
|
|
|
cmd=( $1 ); regex=$2
|
2018-07-27 05:05:14 +00:00
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
if [[ "$target_left" =~ $regex ]]; then
|
|
|
|
act "${cmd[@]}" "$target"; result=$?
|
2018-07-27 03:32:29 +00:00
|
|
|
(( result )) && return 1
|
|
|
|
|
2014-09-25 15:05:17 +00:00
|
|
|
return 0
|
2018-07-27 03:32:29 +00:00
|
|
|
fi
|
2018-07-27 05:05:14 +00:00
|
|
|
|
|
|
|
shift 2
|
2014-09-25 13:26:15 +00:00
|
|
|
done
|
2014-09-25 15:05:17 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
return 3
|
2014-09-25 13:26:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
# DSL
|
2018-07-27 13:28:13 +00:00
|
|
|
uri() {
|
|
|
|
declare r handler=$1; shift
|
|
|
|
|
|
|
|
for r in "$@"; do
|
|
|
|
uri_handlers+=( "$handler" "$r" )
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
mime() {
|
|
|
|
declare r handler=$1; shift
|
|
|
|
|
|
|
|
for r in "$@"; do
|
2018-07-27 14:35:51 +00:00
|
|
|
mime_handlers+=( "$handler" "$r" )
|
2018-07-27 13:28:13 +00:00
|
|
|
done
|
|
|
|
}
|
2018-07-27 03:32:29 +00:00
|
|
|
|
2018-07-27 13:49:08 +00:00
|
|
|
scheme() {
|
|
|
|
declare r handler=$1; shift
|
|
|
|
|
|
|
|
for s in "$@"; do
|
|
|
|
uri_handlers+=( "$handler" "^$s:" )
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
is_uri() [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9\+\.\-]+:.+ ]]
|
|
|
|
|
2014-09-25 13:26:15 +00:00
|
|
|
main() {
|
2018-07-27 03:32:29 +00:00
|
|
|
declare cmd_result target
|
|
|
|
|
|
|
|
# Source the config file.
|
|
|
|
cfg_file="$HOME/.config/sx-open.cfg"
|
|
|
|
[[ -f "$cfg_file" ]] && { source "$cfg_file"; }
|
|
|
|
|
|
|
|
while (( $# )); do
|
|
|
|
case $1 in
|
2018-07-27 04:47:43 +00:00
|
|
|
(-d) dry_run=1; verbose=1;;
|
2018-07-27 03:32:29 +00:00
|
|
|
(-v) verbose=1;;
|
2018-07-27 04:47:43 +00:00
|
|
|
(-h) usage; return 0;;
|
2014-11-25 10:22:15 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
(--) shift; break;;
|
|
|
|
(*) break;;
|
|
|
|
esac
|
2014-11-21 11:14:01 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2018-07-27 04:47:43 +00:00
|
|
|
target=$1; [[ "$target" ]] || { usage; exit; }
|
2018-07-27 03:32:29 +00:00
|
|
|
|
2018-07-27 04:47:43 +00:00
|
|
|
(( dry_run )) && printf 'Dry run: not actually running the handler\n' >&2
|
2014-09-25 15:11:43 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
# Treat file:// as local paths.
|
|
|
|
[[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
|
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
handle_target cmd_result "$target"
|
2018-07-27 03:32:29 +00:00
|
|
|
|
|
|
|
case $? in
|
|
|
|
(1)
|
2018-07-27 14:35:51 +00:00
|
|
|
printf 'Action failed with exit code: “%s”\n' "$cmd_result" >&2
|
2018-07-27 03:32:29 +00:00
|
|
|
return 4
|
|
|
|
;;
|
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
(2)
|
|
|
|
printf 'No such file or directory: “%s”\n' "$target" >&2
|
|
|
|
return 2
|
|
|
|
;;
|
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
(3)
|
2018-07-27 14:35:51 +00:00
|
|
|
printf 'No handlers found for “%s”\n' "$target" >&2
|
2018-07-27 03:32:29 +00:00
|
|
|
return 3
|
|
|
|
;;
|
|
|
|
esac
|
2014-09-25 18:16:11 +00:00
|
|
|
|
|
|
|
return 0
|
2014-09-25 13:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|