From 7c41d62cf755666df87ad88f82adfcb939730a4f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 16 Dec 2017 16:02:23 +0100 Subject: 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. --- vimput.pl | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file 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); } -- cgit v1.2.3