aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfbt <fbt@fleshless.org>2018-08-01 19:23:59 +0300
committerfbt <fbt@fleshless.org>2018-08-01 19:23:59 +0300
commit509f11cd8756616b8f5b8d9d97049d11d3918a55 (patch)
tree1617b9de44941aa0b5cc51679df20e7b0d680d9a
parentada3aafd2ca186d9a4826734d824f53fe533294f (diff)
downloadsx-open-509f11cd8756616b8f5b8d9d97049d11d3918a55.tar.gz
sx-open-509f11cd8756616b8f5b8d9d97049d11d3918a55.zip
Handle symlinks gracefully1.6.2
Signed-off-by: fbt <fbt@fleshless.org>
-rwxr-xr-xsx-open18
1 files changed, 15 insertions, 3 deletions
diff --git a/sx-open b/sx-open
index 63ade16..cdc98e3 100755
--- a/sx-open
+++ b/sx-open
@@ -108,6 +108,7 @@ handle_target() {
declare -n result=$1
declare h cmd regex target_is_file target target_left cmd_is_template
cmd_append_target=1
+ match=0
target=$2
target_left=$target
@@ -116,9 +117,11 @@ handle_target() {
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
- IFS=';' read target_mimetype charset <<< $( file -ibL "$target" )
+ IFS=';' read target_mimetype _ <<< $( file -ib "$target" )
target_left=$target_mimetype
+ IFS=';' read target_mimetype_true _ <<< $( file -ib "$target" )
+
set -- "${mime_handlers[@]}"
elif is_uri "$target"; then
set -- "${uri_handlers[@]}"
@@ -130,13 +133,22 @@ handle_target() {
cmd_str=$1 regex=$2
cmd=()
+ # Fix the regex
+ [[ $regex =~ \^?([^\$]+)\$? ]] && regex="^${BASH_REMATCH[1]}$"
+
if [[ $cmd_str == *'%target%'* ]]; then
cmd=( ${cmd_str//%target%/$target} )
else
cmd=( $cmd_str "$target" )
fi
- if [[ "$target_left" =~ $regex ]]; then
+ if [[ $target_left =~ $regex ]]; then
+ match=1
+ elif [[ $target_mimetype_true ]]; then
+ [[ $target_mimetype_true =~ $regex ]] && match=1
+ fi
+
+ if (( match )); then
act "${cmd[@]}"; result=$?
(( result )) && return 1
@@ -170,7 +182,7 @@ scheme() {
declare r handler=$1; shift
for s in "$@"; do
- uri_handlers+=( "$handler" "^$s:" )
+ uri_handlers+=( "$handler" "$s:.+" )
done
}