summaryrefslogtreecommitdiffstats
path: root/src/parcat
blob: b4956b0704e7e63bc4aed8edfe55dd8985b2793a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl

# Copyright (C) 2016-2024 Ole Tange, http://ole.tange.dk and Free
# Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>
# or write to the Free Software Foundation, Inc., 51 Franklin St,
# Fifth Floor, Boston, MA 02110-1301 USA
#
# SPDX-FileCopyrightText: 2021-2024 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
# SPDX-License-Identifier: GPL-3.0-or-later

use Symbol qw(gensym);
use IPC::Open3;
use POSIX qw(:errno_h);
use IO::Select;
use strict;
use threads;
use threads::shared;
use Thread::Queue;


my $opened :shared;
my $q = Thread::Queue->new();
my $okq = Thread::Queue->new();
my @producers;

if(not @ARGV) {
    if(-t *STDIN) {
	print "Usage:\n";
	print "  parcat file(s)\n";
	print "  cat argfile | parcat\n";
    } else {
	# Read arguments from stdin
	chomp(@ARGV = <STDIN>);
    }
}

my $files_to_open = 0;
# Default: fd = stdout
my $fd = 1;
for (@ARGV) {
    # --rm = remove file when opened
    /^--rm$/ and do { $opt::rm = 1; next; };
    # -1 = output to fd 1, -2 = output to fd 2
    /^-(\d+)$/ and do { $fd = $1; next; };
    push @producers, threads->create('producer', $_, $fd);
    $files_to_open++;
}

sub producer {
    # Open a file/fifo, set non blocking, enqueue fileno of the file handle
    my $file = shift;
    my $output_fd = shift;
    open(my $fh, "<", $file) || do {
	print STDERR "parcat: Cannot open $file\n";
	exit(1);
    };
    # Remove file when it has been opened
    if($opt::rm) {
	unlink $file;
    }
    set_fh_non_blocking($fh);
    $opened++;
    # Pass the fileno to parent
    $q->enqueue(fileno($fh),$output_fd);
    # Get an OK that the $fh is opened and we can release the $fh
    while(1) {
	my $ok = $okq->dequeue();
	if($ok == fileno($fh)) { last; }
	# Not ours - very unlikely to happen
	$okq->enqueue($ok);
    }
    return;
}

my $s = IO::Select->new();
my %buffer;

sub add_file {
    my $infd = shift;
    my $outfd = shift;
    open(my $infh, "<&=", $infd) || die;
    open(my $outfh, ">&=", $outfd) || die;
    $s->add($infh);
    # Tell the producer now opened here and can be released
    $okq->enqueue($infd);
    # Initialize the buffer
    @{$buffer{$infh}{$outfd}} = ();
    $Global::fh{$outfd} = $outfh;
}

sub add_files {
    # Non-blocking dequeue
    my ($infd,$outfd);
    do {
	($infd,$outfd) = $q->dequeue_nb(2);
	if(defined($outfd)) {
	    add_file($infd,$outfd);
	}
    } while(defined($outfd));
}

sub add_files_block {
    # Blocking dequeue
    my ($infd,$outfd) = $q->dequeue(2);
    add_file($infd,$outfd);
}


my $fd;
my (@ready,$infh,$rv,$buf);
do {
    # Wait until at least one file is opened
    add_files_block();
    while($q->pending or keys %buffer) {
	add_files();
	while(keys %buffer) {
	    @ready = $s->can_read(0.01);
	    if(not @ready) {
		add_files();
	    }
	    for $infh (@ready) {
		# There is only one key, namely the output file descriptor
		for my $outfd (keys %{$buffer{$infh}}) {
		    $rv = sysread($infh, $buf, 65536);
		    if (!$rv) {
			if($! == EAGAIN) {
			    # Would block: Nothing read
			    next;
			} else {
			    # Nothing read, but would not block:
			    # This file is done
			    $s->remove($infh);
			    for(@{$buffer{$infh}{$outfd}}) {
				syswrite($Global::fh{$outfd},$_);
			    }
			    delete $buffer{$infh};
			    # Closing the $infh causes it to block
			    # close $infh;
			    add_files();
			    next;
			}
		    }
		    # Something read.
		    # Find \n or \r for full line
		    my $i = (rindex($buf,"\n")+1);
		    if($i) {
			# Print full line
			for(@{$buffer{$infh}{$outfd}}, substr($buf,0,$i)) {
			    syswrite($Global::fh{$outfd},$_);
			}
			# @buffer = remaining half line
			$buffer{$infh}{$outfd} = [substr($buf,$i,$rv-$i)];
		    } else {
			# Something read, but not a full line
			push @{$buffer{$infh}{$outfd}}, $buf;
		    }
		    redo;
		}
	    }
	}
    }
} while($opened < $files_to_open);


for (@producers) {
    $_->join();
}

sub set_fh_non_blocking {
    # Set filehandle as non-blocking
    # Inputs:
    #   $fh = filehandle to be blocking
    # Returns:
    #   N/A
    my $fh = shift;
    $Global::use{"Fcntl"} ||= eval "use Fcntl qw(:DEFAULT :flock); 1;";
    my $flags;
    fcntl($fh, &F_GETFL, $flags) || die $!; # Get the current flags on the filehandle
    $flags |= &O_NONBLOCK; # Add non-blocking to the flags
    fcntl($fh, &F_SETFL, $flags) || die $!; # Set the flags on the filehandle
}