-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailcap_open
executable file
·273 lines (222 loc) · 6.58 KB
/
mailcap_open
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
#!/bin/sh -ef
# shellcheck disable=SC2209,SC2086
# XXX this script and its documentation could probably use a refactor
help() {
cat - >&2 <<EOF
mailcap_open - file opener based on mailcap files
mailcap_open [file | URL]
...and the heavy lifting is done by the mailcap file(s) (see MAILCAPS
below). If the file argument is omitted or - then the standard input
will be read instead.
IMPORTANT: Occurrences of %s must be double quoted (or unquoted if you
want word-splitting) in your mailcap files. This is because it is
actually substituted for a shell variable, not a filename. This is
done on purpose to avoid issues with special characters.
So don't single quote %s! It will cause errors! You have been warned!
Also don't forget to set the TERMCMD environment variable, see below.
The x-url field must be set on a mailcap entry in order for it to be
invoked with a URL (unless MCO_IGNOREURLS is set). The command line
must contain a %s in order to work with URLs.
The following environment variables are used:
TERMCMD
The prefix command for spawning programs in a terminal, e.g. xterm -e
for xterm. There is no default, this MUST be set if mailcap_open
isn't being spawned from a terminal.
MAILCAPS
The colon-delimited mailcap search path defined in RFC 1524,
defaults to:
~/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap
MIMER
The command used to get the mimetype of a file, defaults to:
xdg-mime query filetype
MIMETYPE
Sets the mimetype of the file, skips calling MIMER.
MCO_NODETACH
If set, this will prevent mailcap_open from opening the file in the
background.
MCO_DOWNLOADER
The command used to download from URLs if MCO_HANDLER is unset,
defaults to curl
MCO_HANDLER
A command that may override the handling of the file mailcap_open
was invoked with (unless the file was stdin). A handler will be
invoked like so:
handler {file | URL}
If MCO_HANDLER exits with an error code then mailcap_open will
assume the handler has already opened the file and mailcap_open will
do an early exit (never interpreting the mailcap file).
If MCO_HANDLER exits successfully then the mailcap will be
interpreted as normal.
If MCO_HANDLER is unset then mailcap_open will default to using
MCO_DOWNLOADER to handle URLs, and use the mailcap file for handling
normal files.
Ideally a custom handler should always throw an error in the case of
a URL.
MCO_IGNOREURLS
Don't do any special handling for URLs. This will likely result in
errors if a URL is passed to a command that doesn't support URLs, but
this will prevent the error of file paths match *://* being treated as
URLs. This will also skip MCO_DOWNLOADER logic.
PLEASE_USE_NOHUP
mailcap_open will usually use setsid to detach a process, but you can
set this variable to use nohup instead.
This will only have an effect when the prerequisites for detaching a
process have already been met. If setsid isn't available on your
system then mailcap_open will automatically fallback to nohup without
the need for this environment variable.
EOF
}
# gsub var target replacement [target replacement...]
gsub() {
var="$1"; eval _var='$'"$var"; shift 1;
while :; do
case "$_var" in
*"$1"*)
_var="${_var%%"$1"*}${2}${_var#*"$1"}"
;;
*)
shift 2
if [ "$#" -eq 0 ]; then
break
fi
;;
esac
done
eval "$var"='$_var'; unset -v var _var;
}
# Strips surrounding whitespace from a variable
strip_whitespace() {
var="$1"; eval _var='$'"$var";
IFS=' ' read -r "$1" <<-EOF
$_var
EOF
unset -v var _var
}
set_trap() { trap 'unset_trap; rm -f "$tmp"' EXIT TERM QUIT HUP INT ABRT; }
unset_trap() { trap - EXIT TERM QUIT HUP INT ABRT; }
case "$1" in --help|-h) help; exit 0;; esac
FILENAME=${1:-'-'}
case "$FILENAME" in
*://*) [ "$MCO_IGNOREURLS" ] || url=1 ;;
*) unset -v url ;;
esac
unset -v tmp; set_trap;
if [ "$FILENAME" = '-' ]; then
tmp=$(mktemp)
cat - > "$tmp"
FILENAME=$tmp
elif [ "$MCO_HANDLER" ]; then
$MCO_HANDLER "$FILENAME" || exit 0
else
if [ "$url" ]; then
tmp=$(mktemp)
${MCO_DOWNLOADER:-curl -LSsf --} "$FILENAME" > "$tmp"
FILENAME=$tmp
fi
fi
: "${MAILCAPS:=${HOME:?}/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap}"
MAILCAP=$(IFS=:; cat $MAILCAPS 2> /dev/null || :)
: "${MAILCAP:?No mailcap files (or all mailcap files are empty)}"
: "${MIMETYPE:=$(${MIMER:=xdg-mime query filetype} "$FILENAME")}"
if [ -t 0 ] && [ -t 1 ] && [ -t 2 ]; then
TTY=$(tty) || unset -v TTY
else
unset -v TTY
fi
if [ ! "$TTY" ]; then
: "${TERMCMD:?'TERMCMD must be set (e.g. xterm -e)'}"
fi
IFS=';'
exec <<-EOF
$MAILCAP
EOF
while read -r FILE_MIMETYPE cmd opts; do
unset -v copiousoutput needsterminal skip x_url
# Skip comments and empty lines
case "$FILE_MIMETYPE" in ''|\#*) continue ;; esac
strip_whitespace FILE_MIMETYPE
# Skip non-matching mimetypes
# shellcheck disable=SC2254 # (allow globs)
case "$MIMETYPE" in
"$FILE_MIMETYPE"|$FILE_MIMETYPE) ;;
*) continue ;;
esac
for opt in $opts; do
strip_whitespace opt
case "$opt" in
needsterminal) needsterminal=1 ;;
copiousoutput) copiousoutput=1 ;;
test=*)
if ! eval "${opt#test=}" > /dev/null 2>&1; then
skip=1; break;
fi
;;
notes=*) ;;
description=*) ;;
nametemplate=*) ;;
x11-bitmap=*) ;;
compose=*) ;;
composetyped=*) ;;
print=*) ;;
textualnewlines=*) ;;
x-url) x_url=1 ;;
x-*) ;;
*)
echo "Warning, unrecognized option: $opt" >&2
;;
esac
done
if [ "$skip" ]; then
continue
fi
if [ "$url" ] && [ ! "$x_url" ]; then
continue
fi
if [ "$needsterminal" ] && [ ! "$TTY" ]; then
spawn_terminal=1
detach=1
elif [ ! "$needsterminal" ]; then
unset -v spawn_terminal
detach=1
else
unset -v spawn_terminal detach
fi
if [ "$MCO_NODETACH" ]; then
unset -v detach
fi
# fallback to nohup, since setsid isn't POSIX
if [ ! "$PLEASE_USE_NOHUP" ] && command -v setsid > /dev/null 2>&1; then
detacher=setsid
else
detacher=nohup
fi
# usually wouldn't do this as a function but this needs to be able
# to run in the background and foreground
open() {
if [ ! "$needsterminal" ] && [ ! "$copiousoutput" ]; then
exec > /dev/null 2>&1
elif [ "$needsterminal" ] && [ "$TTY" ]; then
exec < "$TTY"
fi
# Write file contents into stdin if %s is missing, but not for URLs
case "$cmd" in *%s*) ;; *)
[ "$url" ] || exec < "$FILENAME" ;;
esac
gsub cmd %s '$FILENAME' %t "$FILE_MIMETYPE"
IFS=' '
eval "${detach:+${detacher}}" \
"${spawn_terminal:+$TERMCMD}" \
"$cmd" \
"${copiousoutput:+| ${PAGER:-less}}" \
|| :
}
if [ "$detach" ]; then
# only the child process should have the trap
unset_trap
(set_trap; open) &
else
open
fi
exit 0
done
exit 1