paste64.pl revision e0a2b6df
1e0a2b6dfSmrg#!/usr/bin/env perl 2e0a2b6dfSmrg# $XTermId: paste64.pl,v 1.11 2014/02/26 20:14:32 tom Exp $ 320d2c4d2Smrg# ----------------------------------------------------------------------------- 420d2c4d2Smrg# this file is part of xterm 5d522f475Smrg# 6e0a2b6dfSmrg# Copyright 2006,2014 by Thomas E. Dickey 720d2c4d2Smrg# 820d2c4d2Smrg# All Rights Reserved 920d2c4d2Smrg# 1020d2c4d2Smrg# Permission is hereby granted, free of charge, to any person obtaining a 1120d2c4d2Smrg# copy of this software and associated documentation files (the 1220d2c4d2Smrg# "Software"), to deal in the Software without restriction, including 1320d2c4d2Smrg# without limitation the rights to use, copy, modify, merge, publish, 1420d2c4d2Smrg# distribute, sublicense, and/or sell copies of the Software, and to 1520d2c4d2Smrg# permit persons to whom the Software is furnished to do so, subject to 1620d2c4d2Smrg# the following conditions: 1720d2c4d2Smrg# 1820d2c4d2Smrg# The above copyright notice and this permission notice shall be included 1920d2c4d2Smrg# in all copies or substantial portions of the Software. 2020d2c4d2Smrg# 2120d2c4d2Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 2220d2c4d2Smrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2320d2c4d2Smrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 2420d2c4d2Smrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 2520d2c4d2Smrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 2620d2c4d2Smrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 2720d2c4d2Smrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2820d2c4d2Smrg# 2920d2c4d2Smrg# Except as contained in this notice, the name(s) of the above copyright 3020d2c4d2Smrg# holders shall not be used in advertising or otherwise to promote the 3120d2c4d2Smrg# sale, use or other dealings in this Software without prior written 3220d2c4d2Smrg# authorization. 3320d2c4d2Smrg# ----------------------------------------------------------------------------- 34d522f475Smrg# Test the paste64 option of xterm. 35d522f475Smrg 36d522f475Smrguse strict; 37e0a2b6dfSmrguse warnings; 38e0a2b6dfSmrg 39d522f475Smrg 40d522f475Smrguse Term::ReadKey; 41d522f475Smrguse IO::Handle; 42d522f475Smrguse MIME::Base64; 43d522f475Smrg 44d522f475Smrgour $target = ""; 45d522f475Smrg 46d522f475Smrgsub to_hex($) { 47d522f475Smrg my $value = $_[0]; 48d522f475Smrg my $result = ""; 49d522f475Smrg my $n; 50d522f475Smrg 51d522f475Smrg for ( $n = 0; $n < length($value); ++$n) { 52d522f475Smrg $result .= sprintf("%02X", ord substr($value,$n,1)); 53d522f475Smrg } 54d522f475Smrg return $result; 55d522f475Smrg} 56d522f475Smrg 57d522f475Smrgsub show_string($) { 58d522f475Smrg my $value = $_[0]; 59d522f475Smrg my $n; 60d522f475Smrg 61d522f475Smrg my $result = ""; 62d522f475Smrg for ( $n = 0; $n < length($value); $n += 1) { 63d522f475Smrg my $c = ord substr($value,$n,1); 64d522f475Smrg if ( $c == ord '\\' ) { 65d522f475Smrg $result .= "\\\\"; 66d522f475Smrg } elsif ( $c == 0x1b ) { 67d522f475Smrg $result .= "\\E"; 68d522f475Smrg } elsif ( $c == 0x7f ) { 69d522f475Smrg $result .= "^?"; 70d522f475Smrg } elsif ( $c == 32 ) { 71d522f475Smrg $result .= "\\s"; 72d522f475Smrg } elsif ( $c < 32 ) { 73d522f475Smrg $result .= sprintf("^%c", $c + 64); 74d522f475Smrg } elsif ( $c > 128 ) { 75d522f475Smrg $result .= sprintf("\\%03o", $c); 76d522f475Smrg } else { 77d522f475Smrg $result .= chr($c); 78d522f475Smrg } 79d522f475Smrg } 80d522f475Smrg 81d522f475Smrg printf "%s\r\n", $result; 82d522f475Smrg} 83d522f475Smrg 84d522f475Smrgsub get_reply($) { 85d522f475Smrg my $command = $_[0]; 86d522f475Smrg my $reply = ""; 87d522f475Smrg 88d522f475Smrg printf "send: "; 89d522f475Smrg show_string($command); 90d522f475Smrg 91d522f475Smrg print STDOUT $command; 92d522f475Smrg autoflush STDOUT 1; 93d522f475Smrg while (1) { 94d522f475Smrg my $test=ReadKey 1; 95d522f475Smrg last if not defined $test; 96d522f475Smrg #printf "%d:%s\r\n", length($reply), to_hex($test); 97d522f475Smrg $reply .= $test; 98d522f475Smrg } 99d522f475Smrg return $reply; 100d522f475Smrg} 101d522f475Smrg 102d522f475Smrgsub get_paste() { 103d522f475Smrg my $reply = get_reply("\x1b]52;" . $target . ";?\x1b\\"); 104d522f475Smrg 105d522f475Smrg printf "read: "; 106d522f475Smrg show_string($reply); 107d522f475Smrg 108d522f475Smrg my $data = $reply; 109d522f475Smrg $data =~ s/^\x1b]52;[[:alnum:]]*;//; 110d522f475Smrg $data =~ s/\x1b\\$//; 111d522f475Smrg printf "chop: "; 112d522f475Smrg show_string($data); 113d522f475Smrg 114d522f475Smrg $data = decode_base64($data); 115d522f475Smrg printf "data: "; 116d522f475Smrg show_string($data); 117d522f475Smrg} 118d522f475Smrg 119d522f475Smrgsub put_paste() { 120d522f475Smrg ReadMode 1; 121d522f475Smrg 122d522f475Smrg printf "data: "; 123d522f475Smrg my $data = ReadLine 0; 124d522f475Smrg chomp $data; 125d522f475Smrg ReadMode 5; 126d522f475Smrg 127d522f475Smrg $data = encode_base64($data); 128d522f475Smrg chomp $data; 129d522f475Smrg printf "data: "; 130d522f475Smrg show_string($data); 131d522f475Smrg 132d522f475Smrg my $send = "\x1b]52;" . $target . ";" . $data . "\x1b\\"; 133d522f475Smrg 134d522f475Smrg printf "send: "; 135d522f475Smrg show_string($send); 136d522f475Smrg} 137d522f475Smrg 138d522f475Smrgsub set_target() { 139d522f475Smrg ReadMode 1; 140d522f475Smrg 141d522f475Smrg printf "target: "; 142d522f475Smrg $target = ReadLine 0; 143d522f475Smrg $target =~ s/[^[:alnum:]]//g; 144d522f475Smrg ReadMode 5; 145d522f475Smrg printf "result: %s\r\n", $target; 146d522f475Smrg} 147d522f475Smrg 148d522f475SmrgReadMode 5, 'STDIN'; # allow single-character inputs 149d522f475Smrgwhile (1) { 150d522f475Smrg my $cmd; 151d522f475Smrg 152d522f475Smrg printf "\r\nCommand (? for help):"; 153d522f475Smrg $cmd = ReadKey 0; 154d522f475Smrg if ( $cmd eq "?" ) { 155d522f475Smrg printf "\r\np=put selection, g=get selection, q=quit, r=reset target, s=set target\r\n"; 156d522f475Smrg } elsif ($cmd eq "p") { 157d522f475Smrg printf " ...put selection\r\n"; 158d522f475Smrg put_paste(); 159d522f475Smrg } elsif ($cmd eq "g") { 160d522f475Smrg printf " ...get selection\r\n"; 161d522f475Smrg get_paste(); 162d522f475Smrg } elsif ($cmd eq "q") { 163d522f475Smrg printf " ...quit\r\n"; 164d522f475Smrg last; 165d522f475Smrg } elsif ($cmd eq "r") { 166d522f475Smrg printf " ...reset\r\n"; 167d522f475Smrg $target = ""; 168d522f475Smrg } elsif ($cmd eq "s") { 169d522f475Smrg printf " ...set target\r\n"; 170d522f475Smrg set_target(); 171d522f475Smrg } 172d522f475Smrg} 173d522f475SmrgReadMode 0, 'STDIN'; # Reset tty mode before exiting 174