aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-12-16 16:02:23 +0100
committerTeddy Wing2017-12-16 16:04:40 +0100
commit7c41d62cf755666df87ad88f82adfcb939730a4f (patch)
treeeac59d6a305744c64812e0af10525bf609a21c14
parent0cefdb4ac9bfd4ebea557c97bdd4a39dc8ae3b10 (diff)
downloadirssi-vimput-7c41d62cf755666df87ad88f82adfcb939730a4f.tar.bz2
Try to use Unix domain sockets for inter-process communication
Wanted to see if this wouldn't block like named pipes do but they fucking do. How the fuck do I get the temp file name to Vim and listen on the pipe/socket at the same time? Damn it! Refered to https://github.com/irssi/scripts.irssi.org/blob/4c409ee2ed0d1378159cbe08264c77f005c1f412/scripts/adv_windowlist.pl among other resources for help with Unix sockets.
-rw-r--r--vimput.pl72
1 files changed, 68 insertions, 4 deletions
diff --git a/vimput.pl b/vimput.pl
index 3ef0e87..e7a1487 100644
--- a/vimput.pl
+++ b/vimput.pl
@@ -1,6 +1,7 @@
use strict;
-use File::Temp qw(tmpnam tempfile);
+use File::Temp qw(tmpnam tempfile tempdir);
+use IO::Socket::UNIX;
use POSIX qw(mkfifo);
use Irssi;
@@ -15,6 +16,7 @@ our %IRSSI = {
};
+use constant VIMPUT_IPC_COMMAND_PREFIX => '%%%___VIMPUT___%%%: ';
use constant CTRL_X => 24;
@@ -141,9 +143,14 @@ sub update_input_line_when_finished {
# unlink $fuckyoumotherfucker;
- my ($read_handle, $write_handle);
+ my ($read_handle, $write_handle, $command_handle, $fuckface);
pipe($read_handle, $write_handle);
+ # pipe($fuckface, $command_handle);
+ # pipe($read_handle, $command_handle);
+
+ # $write_handle->autoflush(1);
+ # $write_handle->blocking(0);
my $pid = fork();
@@ -151,6 +158,8 @@ sub update_input_line_when_finished {
print "Failed to fork: $!"; # TODO: Irssi print
close $read_handle;
close $write_handle;
+ close $command_handle;
+ # close $fuckface;
return;
}
@@ -171,7 +180,45 @@ if ($pid == 0) {
# close $fifo;
# unlink $fuckyoumotherfucker;
- print $write_handle 'worked?';
+ my $fifo_path = tmpnam();
+ # my $tempdir = tempdir('vimput.XXXXXXXXXX', TMPDIR => 1, CLEANUP => 1);
+ # my $fifo_path = "$tempdir/fifo";
+
+ print $write_handle VIMPUT_IPC_COMMAND_PREFIX . $fifo_path;
+ # print $command_handle VIMPUT_IPC_COMMAND_PREFIX . $fifo_path;
+ # close $command_handle;
+
+ # mkfifo($fifo_path, 0600) or die $!;
+ #
+ # open my $fifo, '<', $fifo_path or die $!;
+ # $fifo->autoflush(1);
+ #
+ # while (<$fifo>) {
+ # print $write_handle $_;
+ # }
+ #
+ # close $fifo;
+
+ my $socket_path = $fifo_path;
+
+ my $socket = IO::Socket::UNIX->new(
+ Local => $socket_path,
+ Type => SOCK_STREAM,
+ Listen => 1,
+ ) or die "Failed to create socket: $!";
+
+ # $socket->blocking(0);
+
+ my $connection = $socket->accept();
+ $connection->autoflush(1);
+
+ while (my $line = <$connection>) {
+ print $write_handle $line;
+ }
+ sleep 5;
+
+ close $socket;
+
close $write_handle;
POSIX::_exit(0);
@@ -189,6 +236,14 @@ else {
\&pipe_input,
\@args,
);
+ # my $p2;
+ # my @ar2 = ($fuckface, \$p2);
+ # $p2 = Irssi::input_add(
+ # fileno $fuckface,
+ # Irssi::INPUT_READ,
+ # \&pipe_input,
+ # \@args
+ # );
}
}
@@ -198,7 +253,16 @@ sub pipe_input {
my ($read_handle, $pipe_tag) = @$args;
my $input = <$read_handle>;
- print 'I: ' . $input;
+
+ # if (index($input, VIMPUT_IPC_COMMAND_PREFIX) == 0) {
+ # print substr $input, length(VIMPUT_IPC_COMMAND_PREFIX);
+ # }
+ # else {
+ print 'I: ' . $input;
+ # }
+
+ # TODO: Add $forked to not spawn more than one children unnecessarily
+
close $read_handle;
Irssi::input_remove($$pipe_tag);
}