forked from seyhani/UT-DMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmate
386 lines (333 loc) · 8.95 KB
/
rmate
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env bash
# rmate
# Copyright (C) 2011-2016 by Harald Lapp <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This script can be found at:
# https://github.com/aurora/rmate
#
#
# This script is a pure bash compatible shell script implementing remote
# textmate functionality
#
#
# Thanks very much to all users and contributors! All bug-reports,
# feature-requests, patches, etc. are greatly appreciated! :-)
#
# init
#
version="0.9.8"
version_date="2016-04-14"
version_string="rmate-sh $version ($version_date)"
# determine hostname
function hostname_command(){
if command -v hostname >/dev/null 2>&1; then
echo "hostname"
else {
HOSTNAME_DESCRIPTOR="/proc/sys/kernel/hostname"
if test -r "$HOSTNAME_DESCRIPTOR"; then
echo "cat $HOSTNAME_DESCRIPTOR"
else
echo "hostname"
fi
}
fi
}
hostname=$($(hostname_command))
# default configuration
host=localhost
port=52698
eval home=$(builtin printf "~%q" "${SUDO_USER:-$LOGNAME}")
function load_config {
local rc_file=$1
local row
local host_pattern="^host(:[[:space:]]+|=)([^ ]+)"
local port_pattern="^port(:[[:space:]]+|=)([0-9]+)"
if [ -f "$rc_file" ]; then
while read row; do
if [[ "$row" =~ $host_pattern ]]; then
host=${BASH_REMATCH[2]}
elif [[ "$row" =~ $port_pattern ]]; then
port=${BASH_REMATCH[2]}
fi
done < "$rc_file"
fi
}
for i in "/etc/${0##*/}" $home/."${0##*/}/${0##*/}.rc" $home/."${0##*/}.rc"; do
load_config $i
done
host="${RMATE_HOST:-$host}"
port="${RMATE_PORT:-$port}"
# misc initialization
filepath=""
selection=""
displayname=""
filetype=""
verbose=false
nowait=true
force=false
# process command-line parameters
#
function showusage {
echo "usage: $(basename $0) [arguments] file-path edit specified file
or: $(basename $0) [arguments] - read text from stdin
-H, --host HOST Connect to HOST. Use 'auto' to detect the host from
SSH. Defaults to $host.
-p, --port PORT Port number to use for connection. Defaults to $port.
-w, --[no-]wait Wait for file to be closed by TextMate.
-l, --line LINE Place caret on line number after loading file.
+N Alias for --line, if N is a number (eg.: +5).
-m, --name NAME The display name shown in TextMate.
-t, --type TYPE Treat file as having specified type.
-n, --new Open in a new window (Sublime Text).
-f, --force Open even if file is not writable.
-v, --verbose Verbose logging messages.
-h, --help Display this usage information.
--version Show version and exit.
"
}
function log {
if [[ $verbose = true ]]; then
echo "$@" 1>&2
fi
}
function dirpath {
(cd `dirname $1` >/dev/null 2>/dev/null; pwd -P)
}
function canonicalize {
filepath="$1"
dir=`dirpath "$filepath"`
if [ -L "$filepath" ]; then
relativepath=`cd "$dir"; readlink \`basename "$filepath"\``
result=`dirpath "$relativepath"`/`basename "$relativepath"`
else
result=`basename "$filepath"`
if [ "$dir" = '/' ]; then
result="$dir$result"
else
result="$dir/$result"
fi
fi
echo $result
}
while [[ "${1:0:1}" = "-" || "$1" =~ ^\+([0-9]+)$ ]]; do
case $1 in
-)
break
;;
-H|--host)
host=$2
shift
;;
-p|--port)
port=$2
shift
;;
-w|--wait)
nowait=false
;;
--no-wait)
nowait=true
;;
-l|--line)
selection=$2
shift
;;
-m|--name)
displayname=$2
shift
;;
-t|--type)
filetype=$2
shift
;;
-n|--new)
new=true
;;
-f|--force)
force=true
;;
-v|--verbose)
verbose=true
;;
--version)
echo $version_string
exit 1
;;
-h|-\?|--help)
showusage
exit 1
;;
+[0-9]*)
selection=${1:1}
;;
*)
showusage
exit 1
;;
esac
shift
done
if [[ "$host" = "auto" && "$SSH_CONNECTION" != "" ]]; then
host=${SSH_CONNECTION%% *}
fi
filepath="$1"
if [ "$filepath" = "" ]; then
if [[ $nowait = false ]]; then
filepath='-'
else
case "$-" in
*i*)
showusage
exit 1
;;
*)
filepath='-'
;;
esac
fi
fi
shift
if [ ! "${#@}" -eq 0 ]; then
echo "There is more than one file specified. Opening only $filepath and ignoring other."
fi
if [ "$filepath" != "-" ]; then
realpath=`canonicalize "$filepath"`
log $realpath
if [ -d "$filepath" ]; then
echo "$filepath is a directory and rmate is unable to handle directories."
exit 1
fi
if [ -f "$realpath" ] && [ ! -w "$realpath" ]; then
if [[ $force = false ]]; then
echo "File $filepath is not writable! Use -f to open anyway."
exit 1
elif [[ $verbose = true ]]; then
log "File $filepath is not writable! Opening anyway."
fi
fi
if [ "$displayname" = "" ]; then
displayname="$hostname:$filepath"
fi
else
displayname="$hostname:untitled"
fi
#------------------------------------------------------------
# main
#------------------------------------------------------------
function handle_connection {
local cmd
local name
local value
local token
local tmp
while read 0<&3; do
REPLY="${REPLY#"${REPLY%%[![:space:]]*}"}"
REPLY="${REPLY%"${REPLY##*[![:space:]]}"}"
cmd=$REPLY
token=""
tmp=""
while read 0<&3; do
REPLY="${REPLY#"${REPLY%%[![:space:]]*}"}"
REPLY="${REPLY%"${REPLY##*[![:space:]]}"}"
if [ "$REPLY" = "" ]; then
break
fi
name="${REPLY%%:*}"
value="${REPLY##*:}"
value="${value#"${value%%[![:space:]]*}"}" # fix textmate syntax highlighting: "
case $name in
"token")
token=$value
;;
"data")
if [ "$tmp" = "" ]; then
tmp="/tmp/rmate.$RANDOM.$$"
touch "$tmp"
fi
dd bs=1 count=$value <&3 >>"$tmp" 2>/dev/null
;;
*)
;;
esac
done
if [[ "$cmd" = "close" ]]; then
log "Closing $token"
if [[ "$token" == "-" ]]; then
echo -n "$CONTENT"
fi
elif [[ "$cmd" = "save" ]]; then
log "Saving $token"
if [ "$token" != "-" ]; then
cat "$tmp" > "$token"
else
CONTENT=`cat "$tmp"`
fi
rm "$tmp"
fi
done
log "Done"
}
# connect to textmate and send command
#
exec 3<> /dev/tcp/$host/$port
if [ $? -gt 0 ]; then
echo "Unable to connect to TextMate on $host:$port"
exit 1
fi
read server_info 0<&3
log $server_info
echo "open" 1>&3
echo "display-name: $displayname" 1>&3
echo "real-path: $realpath" 1>&3
echo "data-on-save: yes" 1>&3
echo "re-activate: yes" 1>&3
echo "token: $filepath" 1>&3
if [[ $new = true ]]; then
echo "new: yes" 1>&3
fi
if [ "$selection" != "" ]; then
echo "selection: $selection" 1>&3
fi
if [ "$filetype" != "" ]; then
echo "file-type: $filetype" 1>&3
fi
if [ "$filepath" != "-" ] && [ -f "$filepath" ]; then
filesize=`ls -lLn "$realpath" | awk '{print $5}'`
echo "data: $filesize" 1>&3
cat "$realpath" 1>&3
elif [ "$filepath" = "-" ]; then
if [ -t 0 ]; then
echo "Reading from stdin, press ^D to stop"
else
log "Reading from stdin"
fi
# preserve trailing newlines
data=`cat; echo x`
data=${data%x}
filesize=$(echo -ne "$data" | wc -c)
echo "data: $filesize" 1>&3
echo -n "$data" 1>&3
else
echo "data: 0" 1>&3
fi
echo 1>&3
echo "." 1>&3
if [[ $nowait = true ]]; then
exec </dev/null >/dev/null 2>/dev/null
( (handle_connection &) &)
else
handle_connection
fi