summaryrefslogtreecommitdiff
path: root/bin/open
blob: 65efddb5f20c95852c3172cb6ab4195595ad8d98 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
#
# macOS-style `open` for Linux desktops. Linked on every host like the rest of
# bin/, so it steps aside where it does not apply: on macOS it hands over to the
# real /usr/bin/open, and on Linux it refuses without a graphical session.
#
# Everything starts detached, as it does on macOS: a new session (setsid), stdin
# from /dev/null, stdout/stderr to the journal. So the app neither dies with the
# terminal nor holds it open -- an ssh session that ran `open` can still exit.
# Its output: journalctl --user -t <program>

# First, before anything bash 3.2 (macOS's /bin/bash) might not parse.
if [[ "$OSTYPE" == darwin* ]]
then
  exec /usr/bin/open "$@"
fi

set -euo pipefail

usage() {
  cat <<'EOF'
Usage: open [-a APP] [-n] [-W] [FILE|URL ...] [--args ARG ...]
       open -t|-e [FILE ...]
       open -R FILE ...

  (none)   open each FILE or URL with its default handler (xdg-open)
  -a APP   open with APP: a desktop entry id (org.kde.dolphin or just dolphin),
           its Name= (Alacritty), a path to a .desktop file, or a command
  -t, -e   open with the default text editor
  -R       reveal in the file manager
  -W       wait for the app to exit instead of detaching (needs -a, -t or -e)
  -n       accepted for compatibility; every launch is a new process anyway
  --args   pass everything after it to the app verbatim
EOF
}

die() {
  >&2 echo "open: $*"
  exit 1
}

# The value of KEY in the [Desktop Entry] group, ignoring [Desktop Action ...]
# groups, which reuse Name= and Exec=.
entry_key() {
  awk -v key="$2=" '
    /^\[/ { in_entry = ($0 == "[Desktop Entry]"); next }
    in_entry && index($0, key) == 1 { print substr($0, length(key) + 1); exit }
  ' "$1"
}

# Path of the desktop entry for $1, case-insensitively: its id with or without
# .desktop (org.kde.dolphin), the last part of a reverse-DNS id (dolphin), or
# failing both its Name= (Alacritty). User entries shadow system ones.
find_entry() {
  local want="${1%.desktop}" dirs dir f id entries=()
  want="${want,,}"
  IFS=: read -ra dirs <<< "${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  for dir in "${dirs[@]}"
  do
    for f in "$dir"/applications/*.desktop
    do
      [[ -f "$f" ]] || continue
      entries+=("$f")
      id="${f##*/}"
      id="${id%.desktop}"
      id="${id,,}"
      if [[ "$id" == "$want" || "${id##*.}" == "$want" ]]
      then
        echo "$f"
        return
      fi
    done
  done
  (( ${#entries[@]} )) || return 1
  awk -v want="$want" '
    FNR == 1 { in_entry = 0 }
    /^\[/ { in_entry = ($0 == "[Desktop Entry]"); next }
    in_entry && /^Name=/ && tolower(substr($0, 6)) == want { print FILENAME; found = 1; exit }
    END { exit !found }
  ' "${entries[@]}"
}

# Splits an Exec= value into NUL-terminated words by the desktop entry spec:
# blanks separate, "..." groups, and within quotes a backslash escapes the next
# character. The spec's outer string escapes (\s, \\\\) are not undone.
exec_words() {
  local s="$1" word='' have='' quoted='' c i
  for (( i = 0; i < ${#s}; i++ ))
  do
    c="${s:i:1}"
    if [[ -n "$quoted" ]]
    then
      if [[ "$c" == '\' ]]
      then
        (( ++i ))
        word+="${s:i:1}"
      elif [[ "$c" == '"' ]]
      then
        quoted=''
      else
        word+="$c"
      fi
    elif [[ "$c" == '"' ]]
    then
      quoted=1 have=1
    elif [[ "$c" == ' ' || "$c" == $'\t' ]]
    then
      if [[ -n "$have" ]]
      then
        printf '%s\0' "$word"
      fi
      word='' have=''
    else
      word+="$c" have=1
    fi
  done
  if [[ -n "$have" ]]
  then
    printf '%s\0' "$word"
  fi
}

# Fills argv from a desktop entry. The files replace its %f/%F/%u/%U field code,
# or go last if it has none, and --args go just before them. %f and %u mean one
# file per launch in the spec; all of them are passed to a single launch here.
from_entry() {
  local entry="$1" exec word t placed='' extras_placed=''
  exec="$(entry_key "$entry" Exec)"
  [[ -n "$exec" ]] || die "$entry has no Exec="
  argv=()
  if [[ "$(entry_key "$entry" Terminal)" == true ]]
  then
    for t in ${TERMINAL:-} alacritty konsole xterm
    do
      if command -v "$t" >/dev/null
      then
        argv=("$t" -e)
        break
      fi
    done
    (( ${#argv[@]} )) || die "$entry needs a terminal and none was found"
  fi
  while IFS= read -r -d '' word
  do
    case "$word" in
      # flatpak's file forwarding markers: app arguments go before them, not inside
      @@u|@@f)
        [[ -n "$extras_placed" ]] || argv+=("${extra[@]}")
        extras_placed=1
        argv+=("$word")
        ;;
      %[fFuU])
        [[ -n "$extras_placed" ]] || argv+=("${extra[@]}")
        argv+=("${files[@]}")
        extras_placed=1 placed=1
        ;;
      %%) argv+=(%) ;;
      %?) ;;
      *) argv+=("${word//"%%"/%}") ;;
    esac
  done < <(exec_words "$exec")
  [[ -n "$extras_placed" ]] || argv+=("${extra[@]}")
  [[ -n "$placed" ]] || argv+=("${files[@]}")
}

# Fills argv with the command line that opens the files with app $1.
resolve() {
  local app="$1" entry cmd
  if [[ "$app" == *.desktop && -f "$app" ]]
  then
    entry="$app"
  else
    entry="$(find_entry "$app")" || entry=''
  fi
  if [[ -n "$entry" ]]
  then
    from_entry "$entry"
  elif cmd="$(command -v -- "$app" || command -v -- "${app,,}")"
  then
    argv=("$cmd" "${extra[@]}" "${files[@]}")
  else
    die "no application named $app"
  fi
  command -v -- "${argv[0]}" >/dev/null || die "$app: ${argv[0]} is not installed"
}

launch() {
  if [[ -n "$wait" ]]
  then
    "$@"
  elif command -v systemd-cat >/dev/null
  then
    setsid -f systemd-cat -t "${1##*/}" "$@" </dev/null >/dev/null 2>&1
  else
    setsid -f "$@" </dev/null >/dev/null 2>&1
  fi
}

# Percent-encodes byte by byte, hence LC_ALL=C: ${path:i:1} must be one byte,
# not one character, for a UTF-8 name to come out right.
file_uri() {
  local LC_ALL=C path out='' c i
  path="$(realpath -- "$1")"
  for (( i = 0; i < ${#path}; i++ ))
  do
    c="${path:i:1}"
    case "$c" in
      [A-Za-z0-9/._~-]) out+="$c" ;;
      *) printf -v c '%%%02X' "'$c"; out+="$c" ;;
    esac
  done
  echo "file://$out"
}

# Through org.freedesktop.FileManager1, which Dolphin, Nautilus, Nemo and Thunar
# all implement; without it, open the containing directories instead.
reveal() {
  local f uris=()
  (( $# )) || die "-R needs a file"
  for f in "$@"
  do
    [[ -e "$f" ]] || die "the file $f does not exist"
    uris+=("$(file_uri "$f")")
  done
  if dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 \
    /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems \
    "array:string:$(IFS=,; echo "${uris[*]}")" string:'' >/dev/null 2>&1
  then
    return
  fi
  for f in "$@"
  do
    launch xdg-open "$(dirname -- "$(realpath -- "$f")")"
  done
}

app='' editor='' reveal='' wait=''
files=() extra=() argv=()
while (( $# ))
do
  case "$1" in
    -a) (( $# >= 2 )) || die "-a needs an application"; app="$2"; shift 2 ;;
    -t|-e) editor=1; shift ;;
    -R) reveal=1; shift ;;
    -W) wait=1; shift ;;
    -n) shift ;;
    -h|--help) usage; exit 0 ;;
    --args) shift; extra=("$@"); break ;;
    --) shift; files+=("$@"); break ;;
    # Bundled short flags, as in `open -na Alacritty`.
    -[!-]?*)
      flags=()
      for (( i = 1; i < ${#1}; i++ ))
      do
        flags+=("-${1:i:1}")
      done
      shift
      set -- "${flags[@]}" "$@"
      ;;
    -?*) die "unsupported option $1 (see open --help)" ;;
    *) files+=("$1"); shift ;;
  esac
done

# A server, or ssh into a desktop without forwarding: nothing could show up.
[[ -n "${WAYLAND_DISPLAY:-}${DISPLAY:-}" ]] || die "no graphical session (WAYLAND_DISPLAY and DISPLAY are unset)"

for f in "${files[@]}"
do
  [[ -e "$f" || "$f" =~ ^[A-Za-z][A-Za-z0-9+.-]*: ]] || die "the file $f does not exist"
done

if [[ -n "$reveal" ]]
then
  reveal "${files[@]}"
  exit
fi

if [[ -n "$editor" ]]
then
  app="$(xdg-mime query default text/plain)"
  [[ -n "$app" ]] || die "no default text editor (xdg-mime query default text/plain)"
fi

if [[ -z "$app" ]]
then
  [[ -z "$wait" ]] || die "-W needs -a, -t or -e: xdg-open returns before the app exits"
  (( ${#extra[@]} == 0 )) || die "--args needs -a"
  if (( ${#files[@]} == 0 ))
  then
    >&2 usage
    exit 1
  fi
  for f in "${files[@]}"
  do
    launch xdg-open "$f"
  done
  exit
fi

resolve "$app"
launch "${argv[@]}"