/
it2getvar
executable file
·99 lines (84 loc) · 1.81 KB
/
it2getvar
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
#!/bin/bash
function ctrl_c() {
stty "$saved_stty"
exit 1
}
# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
# only accepts ESC backslash for ST.
function print_osc() {
if [[ $TERM == screen* ]] ; then printf "\033Ptmux;\033\033]"
else
printf "\033]" >& 2
fi
}
# More of the tmux workaround described above.
function print_st() {
if [[ $TERM == screen* ]] ; then
printf "\a\033\\" >& 2
else
printf "\a" >& 2
fi
}
function show_help() {
echo "Usage:" 1>& 2
echo " $(basename $0) name" 1>& 2
}
# Read some bytes from stdin. Pass the number of bytes to read as the first argument.
function read_bytes() {
numbytes=$1
dd bs=1 count=$numbytes 2>/dev/null
}
# read_until c
# Returns bytes read from stdin up to but not including the fist one equal to c
function read_until() {
result=""
while :
do
b=$(read_bytes 1)
if [[ $b == $1 ]]
then
echo "$result"
return
fi
result="$result$b"
done
}
## Main
if [[ $# != 1 ]]
then
show_help
exit 1
fi
if ! test -t 1
then
echo "Standard error not a terminal"
exit 1
fi
# Save the tty's state.
saved_stty=$(stty -g)
# Trap ^C to fix the tty.
trap ctrl_c INT
# Enter raw mode and turn off echo so the terminal and I can chat quietly.
stty -echo -icanon raw
print_osc
printf "1337;ReportVariable=%s" "$(printf "%s" "$1" | base64)" >& 2
print_st
VERSION=$(base64 --version 2>&1)
if [[ "$VERSION" =~ fourmilab ]]; then
BASE64ARG=-d
elif [[ "$VERSION" =~ GNU ]]; then
BASE64ARG=-di
else
BASE64ARG=-D
fi
ignore=$(read_bytes 1)
name=$(read_until )
re='^]1337;ReportVariable=(.*)'
if [[ $name =~ $re ]]
then
printf "%s" $(base64 $BASE64ARG <<< ${BASH_REMATCH[1]})
exit 0
else
exit 1
fi