Skip to content

Instantly share code, notes, and snippets.

@saitoha
Created August 5, 2012 07:48
Show Gist options
  • Select an option

  • Save saitoha/3262692 to your computer and use it in GitHub Desktop.

Select an option

Save saitoha/3262692 to your computer and use it in GitHub Desktop.
OSC 52 get access for a MouseTerm branch (https://gist.github.com/3260028)
diff --git a/MTParser.rl b/MTParser.rl
index db85428..80f069c 100644
--- a/MTParser.rl
+++ b/MTParser.rl
@@ -1,6 +1,4 @@
#import <Cocoa/Cocoa.h>
-#import <apr-1/apr.h>
-#import <apr-1/apr_base64.h>
#import <math.h>
#import "Mouse.h"
#import "MouseTerm.h"
@@ -78,7 +76,7 @@
[mobj MouseTerm_setMouseProtocol: NORMAL_PROTOCOL];
}
- action handle_osc52_start
+ action handle_osc52_set_start
{
if ([NSView MouseTerm_getBase64CopyEnabled]) {
if (osc52Buffer)
@@ -87,6 +85,16 @@
}
}
+ action handle_osc52_get
+ {
+ if (osc52Buffer) {
+ [osc52Buffer release];
+ osc52Buffer = nil;
+ }
+
+ [mobj MouseTerm_osc52GetAccess];
+ }
+
action handle_osc52
{
if (osc52Buffer)
@@ -98,15 +106,7 @@
if (osc52Buffer) {
NSString *str= [[NSString alloc] initWithData:osc52Buffer
encoding:NSASCIIStringEncoding];
- char *encodedBuffer = (char*)[str cStringUsingEncoding:NSASCIIStringEncoding];
- int destLength = apr_base64_decode_len(encodedBuffer);
- char *decodedBuffer = malloc(destLength);
- apr_base64_decode(decodedBuffer, encodedBuffer);
- NSString *resultString = [NSString stringWithUTF8String:decodedBuffer];
- free(decodedBuffer);
-
- [[[[mobj controller] activePane] view] copy: nil];
- [mobj MouseTerm_writeToPasteBoard: resultString];
+ [mobj MouseTerm_osc52SetAccess:str];
[osc52Buffer release];
osc52Buffer = nil;
}
@@ -116,6 +116,7 @@
csi = esc . "[";
flag = ("h" | "l") @handle_flag;
osc = esc . ']';
+ dcs = esc . 'P';
appkeys = "1";
mouse = "100" . ([0123]) @handle_mouse_digit;
debug = (csi . "li");
@@ -127,7 +128,8 @@
bel = 0x07;
st = esc . "\\" | 0x9c;
base64 = ([a-zA-Z0-9\+/]+[=]*);
- osc52 = osc . "52;" . [0-9]* . ";" @handle_osc52_start;
+ osc52 = osc . "52;" . [psc0-9]* . (";" @handle_osc52_set_start)
+ . ("?" @handle_osc52_get)?;
main := ((base64 @handle_osc52
| (bel | st) @handle_osc_end
diff --git a/MTShell.h b/MTShell.h
index e4dabf6..ab52e3b 100644
--- a/MTShell.h
+++ b/MTShell.h
@@ -22,6 +22,9 @@
- (BOOL) MouseTerm_writeToPasteBoard: (NSString*) stringToWrite;
- (NSString*) MouseTerm_readFromPasteBoard;
+- (void) MouseTerm_osc52SetAccess: (NSString*) stringToWrite;
+- (void) MouseTerm_osc52GetAccess;
+
- (void) MouseTerm_setParserState: (MTParserState*) parserState;
- (MTParserState*) MouseTerm_getParserState;
diff --git a/MTShell.m b/MTShell.m
index 98d6d99..c7abade 100644
--- a/MTShell.m
+++ b/MTShell.m
@@ -1,4 +1,7 @@
#import <Cocoa/Cocoa.h>
+#import <apr-1/apr.h>
+#import <apr-1/apr_base64.h>
+#import "terminal.h"
#import "Mouse.h"
#import "MouseTerm.h"
#import "MTParserState.h"
@@ -97,6 +100,42 @@
return [[NSPasteboard generalPasteboard] stringForType:NSStringPboardType];
}
+- (void) MouseTerm_osc52SetAccess: (NSString*) stringToWrite
+{
+ char *encodedBuffer = (char*)[stringToWrite cStringUsingEncoding:NSASCIIStringEncoding];
+ int destLength = apr_base64_decode_len(encodedBuffer);
+ char *decodedBuffer = malloc(destLength);
+ apr_base64_decode(decodedBuffer, encodedBuffer);
+ NSString *resultString = [NSString stringWithUTF8String:decodedBuffer];
+ free(decodedBuffer);
+
+ [[[[(TTShell*)self controller] activePane] view] copy: nil];
+ [self MouseTerm_writeToPasteBoard: resultString];
+}
+
+- (void) MouseTerm_osc52GetAccess
+{
+ // ref: http://blog.livedoor.jp/jgoamakf/archives/51160286.html
+ NSString *str = [self MouseTerm_readFromPasteBoard];
+ char *sourceCString = (char*)[str UTF8String];
+ const char prefix[] = "\x1b]52;c;"; // OSC52 from Clipboard
+ const char postfix[] = "\x1b\\"; // ST
+
+ int sourceLength = strlen(sourceCString);
+ int resultLength = apr_base64_encode_len(sourceLength);
+ int allLength = sizeof(prefix) + resultLength + sizeof(postfix);
+ char *encodedBuffer = (char*)malloc(allLength);
+ char *it = encodedBuffer;
+ memcpy(it, prefix, sizeof(prefix));
+ it += sizeof(prefix);
+ apr_base64_encode(it, sourceCString, sourceLength);
+ it += resultLength;
+ memcpy(it, postfix, sizeof(postfix));
+
+ [(TTShell*)self writeData: [NSData dataWithBytes: encodedBuffer
+ length: allLength]];
+}
+
- (void) MouseTerm_setParserState: (MTParserState*) parserState
{
NSValue *ptr = [self MouseTerm_initVars];
diff --git a/Terminal.h b/Terminal.h
index e1a871f..4b1aa88 100644
--- a/Terminal.h
+++ b/Terminal.h
@@ -22,6 +22,7 @@ typedef struct
@interface TTShell: NSObject
- (void) writeData: (NSData*) data;
+- (id)controller;
@end
@interface TTLogicalScreen: NSObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment