it2getvar (1865B)
1 #!/bin/bash 2 3 # tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; 4 # <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It 5 # only accepts ESC backslash for ST. 6 function print_osc() { 7 if [[ $TERM == screen* ]] ; then printf "\033Ptmux;\033\033]" 8 else 9 printf "\033]" >& 2 10 fi 11 } 12 13 # More of the tmux workaround described above. 14 function print_st() { 15 if [[ $TERM == screen* ]] ; then 16 printf "\a\033\\" >& 2 17 else 18 printf "\a" >& 2 19 fi 20 } 21 22 function show_help() { 23 echo "Usage:" 1>& 2 24 echo " $(basename $0) name" 1>& 2 25 } 26 27 # Read some bytes from stdin. Pass the number of bytes to read as the first argument. 28 function read_bytes() { 29 numbytes=$1 30 dd bs=1 count=$numbytes 2>/dev/null 31 } 32 33 # read_until c 34 # Returns bytes read from stdin up to but not including the fist one equal to c 35 function read_until() { 36 result="" 37 while : 38 do 39 b=$(read_bytes 1) 40 if [[ $b == $1 ]] 41 then 42 echo "$result" 43 return 44 fi 45 result="$result$b" 46 done 47 } 48 49 ## Main 50 if [[ $# != 1 ]] 51 then 52 show_help 53 exit 1 54 fi 55 56 if ! test -t 1 57 then 58 echo "Standard error not a terminal" 59 exit 1 60 fi 61 62 trap clean_up EXIT 63 _STTY=$(stty -g) ## Save current terminal setup 64 65 function clean_up() { 66 stty "$_STTY" ## Restore terminal settings 67 } 68 69 # Enter raw mode and turn off echo so the terminal and I can chat quietly. 70 stty -echo -icanon raw 71 72 print_osc 73 printf "1337;ReportVariable=%s" "$(printf "%s" "$1" | base64)" >& 2 74 print_st 75 76 VERSION=$(base64 --version 2>&1) 77 if [[ "$VERSION" =~ fourmilab ]]; then 78 BASE64ARG=-d 79 elif [[ "$VERSION" =~ GNU ]]; then 80 BASE64ARG=-di 81 else 82 BASE64ARG=-D 83 fi 84 85 ignore=$(read_bytes 1) 86 name=$(read_until ) 87 re='^]1337;ReportVariable=(.*)' 88 if [[ $name =~ $re ]] 89 then 90 printf "%s" $(base64 $BASE64ARG <<< ${BASH_REMATCH[1]}) 91 exit 0 92 else 93 exit 1 94 fi 95