summaryrefslogtreecommitdiff
path: root/less-osc8-open.sh
diff options
context:
space:
mode:
Diffstat (limited to 'less-osc8-open.sh')
-rwxr-xr-xless-osc8-open.sh64
1 files changed, 64 insertions, 0 deletions
diff --git a/less-osc8-open.sh b/less-osc8-open.sh
new file mode 100755
index 000000000000..fbcde90a9824
--- /dev/null
+++ b/less-osc8-open.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+# Less OSC 8 handler.
+
+echo() { printf %s\\n "$*"; }
+
+# Open a link of the form "file://HOST/PATH" or "file:///PATH"
+open_file() {
+ case $1 in
+ file:///* | file://localhost/*)
+ less -- "/${1#file://*/}"
+ ;;
+
+ file://*/*)
+ linkhost=${1#file://}; linkhost=${linkhost%%/*}
+ localhost=${HOSTNAME:-$(bash -c 'printf %s "$HOSTNAME"' || uname -n)} 2>/dev/null
+
+ case $localhost in
+ */* | '')
+ echo "unable to detect local hostname: $localhost"
+ exit 1
+ ;;
+ "$linkhost")
+ less -- "/${1#file://*/}"
+ ;;
+ *)
+ echo "cannot open remote file: $1"
+ exit 1
+ ;;
+ esac
+ ;;
+
+ *)
+ echo "invalid file link: $1"
+ exit 1
+ ;;
+ esac
+}
+
+# Open a link of the form "man:NAME" or "man:NAME(SECTION)".
+open_man() {
+ case $1 in
+ man:?*\(*\) )
+ sect=${1#*\(}; sect=${sect%?}
+ name=${1#man:}; name=${name%%\(*}
+ man -- ${sect:+"$sect"} "$name"
+ ;;
+
+ man:?*)
+ man -- "${1#man:}"
+ ;;
+
+ *)
+ echo "invalid man link: $1"
+ exit 1
+ ;;
+ esac
+}
+
+case $1 in
+file:*) open_file "$1" ;;
+man:*) open_man "$1" ;;
+*) echo "unsupported link type: $1"; exit 1 ;;
+esac