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