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
|
2018-07-27 15:34:22 +00:00
|
|
|
-v Verbose mode
|
|
|
|
-n Disable desktop notifications
|
2018-07-27 03:32:29 +00:00
|
|
|
-h Help
|
|
|
|
EOF
|
|
|
|
}
|
2014-09-25 13:26:15 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
act() {
|
2018-07-27 17:03:36 +00:00
|
|
|
cfg verbose && printf 'CMD: %s\n' "$*" >&2
|
|
|
|
cfg dryrun || { "$@"; return $?; }
|
2014-09-25 13:26:15 +00:00
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-07-27 17:03:36 +00:00
|
|
|
# cfg foo bool = [true|1]
|
|
|
|
# cfg foo [string] = 'bar'
|
|
|
|
# cfg foo
|
|
|
|
cfg() {
|
|
|
|
declare s_name=$1; shift
|
|
|
|
declare -n \
|
|
|
|
_s="cfg[$s_name]" \
|
|
|
|
_s_type="cfg[${s_name}_type]"
|
|
|
|
|
|
|
|
if (( $# )); then
|
|
|
|
while (( $# )); do
|
|
|
|
case $1 in
|
|
|
|
(bool|str) _s_type="$1";;
|
|
|
|
|
|
|
|
(true|false|0|1)
|
|
|
|
_s_type='bool'
|
|
|
|
_s="$1"
|
|
|
|
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
|
|
|
|
(=)
|
|
|
|
[[ $_s_type ]] || _s_type='string'
|
|
|
|
|
|
|
|
[[ $_s_type == 'bool' ]] && {
|
|
|
|
case $2 in
|
|
|
|
(true|false|0|1) true;;
|
|
|
|
(*)
|
|
|
|
error "On line $LINENO, in $FUNCNAME: Invalid value for '$s_name' type 'bool': '$2'"
|
2018-07-27 17:21:26 +00:00
|
|
|
exit 111
|
2018-07-27 17:03:36 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
_s="$2"
|
|
|
|
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
|
|
|
|
(*)
|
|
|
|
error "On line $LINENO, in $FUNCNAME: Syntax error: “cfg $s_name $*”"
|
|
|
|
exit 115
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
else
|
|
|
|
[[ -n $_s ]] || {
|
|
|
|
error "On line $LINENO, in $FUNCNAME: invalid option: '$s_name'"
|
2018-07-27 17:21:26 +00:00
|
|
|
exit 113
|
2018-07-27 17:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case $_s_type in
|
|
|
|
(bool)
|
|
|
|
case $_s in
|
|
|
|
(true|1) return 0;;
|
|
|
|
(false|0) return 1;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
|
|
|
|
(*)
|
|
|
|
printf '%s\n' "$_s"
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:32:42 +00:00
|
|
|
notify() {
|
2018-07-27 17:03:36 +00:00
|
|
|
cfg notify || return 7 # disabled
|
|
|
|
|
2018-07-27 15:32:42 +00:00
|
|
|
[[ $DISPLAY ]] || return 3
|
|
|
|
[[ $notifier ]] || return 5
|
|
|
|
|
|
|
|
"${notifier[@]}" 'sx-open' "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
error() {
|
|
|
|
printf '%s\n' "$*" >&2
|
|
|
|
notify "$*"
|
|
|
|
}
|
|
|
|
|
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:48:58 +00:00
|
|
|
declare h cmd regex target_is_file target target_left cmd_is_template
|
2018-07-27 17:43:04 +00:00
|
|
|
cmd_append_target=1
|
2018-08-01 16:23:59 +00:00
|
|
|
match=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-08-01 16:23:59 +00:00
|
|
|
IFS=';' read target_mimetype _ <<< $( file -ib "$target" )
|
2018-07-27 14:35:51 +00:00
|
|
|
target_left=$target_mimetype
|
|
|
|
|
2018-08-01 16:23:59 +00:00
|
|
|
IFS=';' read target_mimetype_true _ <<< $( file -ib "$target" )
|
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
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 20:49:51 +00:00
|
|
|
cmd_str=$1 regex=$2
|
|
|
|
cmd=()
|
2018-07-27 05:05:14 +00:00
|
|
|
|
2018-08-01 16:23:59 +00:00
|
|
|
# Fix the regex
|
|
|
|
[[ $regex =~ \^?([^\$]+)\$? ]] && regex="^${BASH_REMATCH[1]}$"
|
|
|
|
|
2018-07-27 20:49:51 +00:00
|
|
|
if [[ $cmd_str == *'%target%'* ]]; then
|
|
|
|
cmd=( ${cmd_str//%target%/$target} )
|
2018-07-27 17:43:04 +00:00
|
|
|
else
|
2018-07-27 20:49:51 +00:00
|
|
|
cmd=( $cmd_str "$target" )
|
2018-07-27 17:43:04 +00:00
|
|
|
fi
|
2018-07-27 14:48:58 +00:00
|
|
|
|
2018-08-01 16:23:59 +00:00
|
|
|
if [[ $target_left =~ $regex ]]; then
|
|
|
|
match=1
|
|
|
|
elif [[ $target_mimetype_true ]]; then
|
|
|
|
[[ $target_mimetype_true =~ $regex ]] && match=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if (( match )); then
|
2018-07-27 14:48:58 +00:00
|
|
|
act "${cmd[@]}"; 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
|
2018-08-01 16:23:59 +00:00
|
|
|
uri_handlers+=( "$handler" "$s:.+" )
|
2018-07-27 13:49:08 +00:00
|
|
|
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
|
|
|
|
|
2018-07-27 17:03:36 +00:00
|
|
|
declare -gA cfg
|
|
|
|
cfg verbose bool = false
|
|
|
|
cfg dryrun bool = false
|
|
|
|
cfg notify bool = true
|
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
# 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 17:03:36 +00:00
|
|
|
(-d)
|
|
|
|
cfg dryrun = true
|
|
|
|
cfg verbose = true
|
|
|
|
;;
|
2014-11-25 10:22:15 +00:00
|
|
|
|
2018-07-27 17:03:36 +00:00
|
|
|
(-v) cfg verbose = true;;
|
|
|
|
(-n) cfg notify = false;; # disable desktop notifications
|
|
|
|
|
|
|
|
(-h) usage; return 0;;
|
2018-07-27 15:34:22 +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 17:03:36 +00:00
|
|
|
cfg dryrun && {
|
|
|
|
printf 'Dry run: not actually running the handler\n' >&2
|
|
|
|
cfg verbose true
|
|
|
|
}
|
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 15:32:42 +00:00
|
|
|
# Figure out if we're in X and set $notifier if we are.
|
|
|
|
if [[ $DISPLAY ]]; then
|
|
|
|
# Do we have notify-send?
|
|
|
|
notifier=$(type -P 'notify-send')
|
|
|
|
fi
|
|
|
|
|
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 15:32:42 +00:00
|
|
|
error "Action on “$target” failed with exit code: “$cmd_result”"
|
2018-07-27 03:32:29 +00:00
|
|
|
return 4
|
|
|
|
;;
|
|
|
|
|
2018-07-27 14:35:51 +00:00
|
|
|
(2)
|
2018-07-27 15:32:42 +00:00
|
|
|
error "No such file or directory: “$target”"
|
2018-07-27 14:35:51 +00:00
|
|
|
return 2
|
|
|
|
;;
|
|
|
|
|
2018-07-27 03:32:29 +00:00
|
|
|
(3)
|
2018-07-27 15:32:42 +00:00
|
|
|
error "No handlers found for “$target”"
|
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 "$@"
|