1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "usage: xdg-open <file-or-url>")
os.Exit(1)
}
file := os.Args[1]
// always notify what we're opening
notifySend("xdg-open: " + file)
// URI scheme dispatch
if handled := dispatchURI(file); handled {
return
}
// it's a file: strip possible file:// prefix
file = strings.TrimPrefix(file, "file://")
mime, err := getMime(file)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
dispatchMime(mime, file)
}
// notifySend fires notify-send and ignores errors.
func notifySend(msg string) {
cmd := exec.Command("notify-send", "--expire-time=500", "--", msg)
cmd.Run()
}
// notifyOnly sends a notification with the given message and exits cleanly.
// Used for URI/MIME types we have no handler for.
func notifyOnly(msg string) {
notifySend(msg)
os.Exit(0)
}
// getMime runs `file -E --brief --mime-type` and returns the trimmed result.
func getMime(path string) (string, error) {
out, err := exec.Command("file", "-E", "--brief", "--mime-type", path).Output()
if err != nil {
return "", fmt.Errorf("file: %w", err)
}
return strings.TrimSpace(string(out)), nil
}
// execInto replaces the current process with the given command + args.
// Uses LookPath so bare binary names are resolved via PATH.
func execInto(bin string, args ...string) {
path, err := exec.LookPath(bin)
if err != nil {
fmt.Fprintf(os.Stderr, "xdg-open: cannot find %q in PATH: %v\n", bin, err)
os.Exit(1)
}
argv := append([]string{path}, args...)
if err := syscall.Exec(path, argv, os.Environ()); err != nil {
fmt.Fprintf(os.Stderr, "xdg-open: exec %q: %v\n", path, err)
os.Exit(1)
}
}
// dispatchURI matches URI schemes and execs the appropriate handler.
// Returns true if the URI was matched (even for notify-only cases).
func dispatchURI(uri string) bool {
switch {
case strings.HasPrefix(uri, "http://"),
strings.HasPrefix(uri, "https://"):
execInto("firefox", uri)
case strings.HasPrefix(uri, "gemini://"),
strings.HasPrefix(uri, "gopher://"),
strings.HasPrefix(uri, "gophers://"):
execInto("lagrange", uri)
case strings.HasPrefix(uri, "mailto:"):
execInto("claws", "--compose", "--eval", uri)
case strings.HasPrefix(uri, "magnet:"):
notifyOnly("No xdg-open handler for the torrent")
case strings.HasPrefix(uri, "irc:"),
strings.HasPrefix(uri, "ircs:"):
notifyOnly("No xdg-open handler for the irc link")
default:
return false
}
return true
}
// mimeRule maps a MIME glob pattern to an action.
type mimeRule struct {
pattern string
action func(file string)
}
var mimeRules = []mimeRule{
{"text/html", func(f string) { execInto("firefox", f) }},
{"text/gemini", func(f string) { execInto("lagrange", f) }},
{"text/gopher", func(f string) { execInto("lagrange", f) }},
{"text/xml", func(f string) { execInto("firefox", f) }},
{"text/csv", func(f string) { execInto("libreoffice", f) }},
{"text/*", func(f string) { execInto("micro", f) }},
{"image/gif", func(f string) { execInto("firefox", f) }},
{"image/svg+xml", func(f string) { execInto("inkscape", f) }},
{"image/*", func(f string) { execInto("imv", f) }},
{"application/pdf", func(f string) { execInto("zathura", f) }},
{"application/x-bittorrent", func(_ string) {
notifyOnly("No xdg-open handler for the torrent")
}},
{"application/pgp-keys", func(f string) {
execInto("gpg", "--import", "--import-options", "show-only", f)
}},
{"application/vnd.oasis.opendocument.*", func(f string) { execInto("libreoffice", f) }},
{"application/vnd.openxmlformats-officedocument.*", func(f string) { execInto("libreoffice", f) }},
{"application/msword", func(f string) { execInto("libreoffice", f) }},
{"inode/directory", func(f string) {
execInto("alacritty", "-e", "ranger", f)
}},
{"x-scheme-handler/irc", func(_ string) {
notifyOnly("No xdg-open handler for the irc link")
}},
{"x-scheme-handler/file", func(_ string) {
notifyOnly("No xdg-open handler for the x-scheme-handler/file")
}},
{"*", dmenuExec},
}
// dispatchMime matches mime against mimeRules (in order) using filepath.Match.
func dispatchMime(mime, file string) {
for _, rule := range mimeRules {
matched, err := filepath.Match(rule.pattern, mime)
if err != nil {
// pattern is a constant; this shouldn't happen
panic(fmt.Sprintf("bad pattern %q: %v", rule.pattern, err))
}
if matched {
rule.action(file)
return
}
}
// should be unreachable: the "*" catch-all always matches
fmt.Fprintf(os.Stderr, "xdg-open: no handler for mime type %q\n", mime)
os.Exit(1)
}
// dmenuExec pipes dmenu_path into dmenu, reads the chosen binary name,
// then execs that binary with the file as argument.
func dmenuExec(file string) {
// run dmenu_path to get all binaries, pipe into dmenu for selection
dmenuPath := exec.Command("dmenu_path")
pathOut, err := dmenuPath.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "xdg-open: dmenu_path: %v\n", err)
os.Exit(1)
}
dmenu := exec.Command("dmenu")
dmenu.Stdin = bytes.NewReader(pathOut)
dmenu.Stderr = os.Stderr
chosen, err := dmenu.Output()
if err != nil {
// user cancelled dmenu (non-zero exit)
os.Exit(0)
}
bin := strings.TrimSpace(string(chosen))
if bin == "" {
os.Exit(0)
}
execInto(bin, file)
}
|