xref: /freebsd/contrib/less/less-osc8-open.sh (revision fa0dc4f0f96a1b77d4be7bcdbf965897cda14521)
1#!/bin/sh
2
3# Less OSC 8 handler.
4
5echo() { printf %s\\n "$*"; }
6
7# Open a link of the form "file://HOST/PATH" or "file:///PATH"
8open_file() {
9	case $1 in
10	file:///* | file://localhost/*)
11		less -- "/${1#file://*/}"
12		;;
13
14	file://*/*)
15		linkhost=${1#file://}; linkhost=${linkhost%%/*}
16		localhost=${HOSTNAME:-$(bash -c 'printf %s "$HOSTNAME"' || uname -n)} 2>/dev/null
17
18		case $localhost in
19		*/* | '')
20			echo "unable to detect local hostname: $localhost"
21			exit 1
22			;;
23		"$linkhost")
24			less -- "/${1#file://*/}"
25			;;
26		*)
27			echo "cannot open remote file: $1"
28			exit 1
29			;;
30		esac
31		;;
32
33	*)
34		echo "invalid file link: $1"
35		exit 1
36		;;
37	esac
38}
39
40# Open a link of the form "man:NAME" or "man:NAME(SECTION)".
41open_man() {
42	case $1 in
43	man:?*\(*\) )
44		sect=${1#*\(}; sect=${sect%?}
45		name=${1#man:}; name=${name%%\(*}
46		man -- ${sect:+"$sect"} "$name"
47		;;
48
49	man:?*)
50		man -- "${1#man:}"
51		;;
52
53	*)
54		echo "invalid man link: $1"
55		exit 1
56		;;
57	esac
58}
59
60case $1 in
61file:*) open_file "$1" ;;
62man:*) open_man "$1" ;;
63*) echo "unsupported link type: $1"; exit 1 ;;
64esac
65