make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / xwin-tools / rxvt-inband
blob6bc4599af4d4bc33504f0bb28d8a6d9458fc09d9
1 #!/bin/bash
3 osc()
5 echo -en "\033]777;inbandfiletransfer;$1\007"
8 act=download
9 err=0
11 while [ -n "$1" ]
13 case "$1" in
14 -d) act=download
16 -u) act=upload
18 -*) echo "Unknown option: $1" >&2
19 exit 1;;
20 --) shift
21 break;;
22 *) break;;
23 esac
24 shift
25 done
27 usage()
29 terminalSystem=n/a
30 if [ -n "$SSH_CLIENT" ]
31 then
32 terminalSystem=${SSH_CLIENT%% *}
35 echo "Usage: $0 [-d <path> | -u [path]]
36 -d Download from system running shell to system running terminal
37 $(hostname) => $terminalSystem
38 You can give a file to download or a folder to download it in tar.gz.
39 You will be prompted for the target file.
40 -u Upload file from system running terminal to system running shell
41 $terminalSystem => $(hostname)
42 You will be prompted for the source file.
43 I save the file in the given path, or in working directory if path is
44 unspecified, as the same name as source file.
45 If path is not a directory, the I save in it and overwrite if exists.
47 Don't forget to load rxvt extension by Xresouces directive:
48 \"URxvt.perl-ext: default,inbandfiletransfer-osc\"" >&2
51 case "$act" in
52 download)
53 if [ -z "$1" ]
54 then
55 usage
56 err=1
57 else
58 if [ -r "$1" ]
59 then
60 name=`readlink -f "$1"`
61 name=${name##*/}
62 name=${name//;/}
63 if [ -d "$1" ]
64 then
65 osc2="$name.tar.gz;-1"
66 else
67 size=`stat -c %s "$1"`
68 osc2="$name;$size"
71 stty -echo
72 osc "download;base64;$osc2"
73 read -n 1 -s accepted
75 if [ "$accepted" = "y" ]
76 then
77 if [ -d "$1" ]
78 then
79 tar czf - "$1" 2>/dev/null | base64
80 else
81 cat "$1" | base64
83 err=${PIPESTATUS[0]}
84 echo =====
85 else
86 echo "File transfer cancelled." >&2
87 err=2
89 stty echo
90 else
91 echo "Can not read: $1" >&2
92 err=1
97 upload)
98 if [ -z "$1" ]
99 then
100 set .
103 dir=`dirname "$1"`
104 wrtbl=$1
105 [ ! -e "$1" ] && wrtbl=$dir
107 if [ -w "$wrtbl" ]
108 then
109 stty -echo
110 echo "Ready to upload" >&2
111 osc "upload;base64"
112 read -n 1 accepted
114 if [ "$accepted" = "y" ]
115 then
116 read name
117 if [ -d "$1" ]
118 then
119 name=${name##*/}
120 outfile=$1/$name
121 else
122 outfile=$1
125 while true
127 read size
128 if [ -z "$size" ] || ! [ "$size" -ge 0 ] 2>/dev/null
129 then
130 echo "Upload error" >&2
131 echo '!'
132 break
133 elif [ $size = 0 ]
134 then
135 break
138 read -N $size data
139 echo "$data"
140 echo =received >&2
141 done | base64 -d >"$outfile"
143 err=${PIPESTATUS[1]}
144 if [ $err = 0 ]
145 then
146 echo "File transfer completed: $outfile"
147 else
148 echo "Error $err" >&2
150 else
151 echo "File transfer cancelled." >&2
152 err=2
154 stty echo
155 else
156 echo "Can not write: $1" >&2
157 err=1
160 esac
162 exit $err