/usr/share/perl5/cs/Sink.pm is in info2man 1.1-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | #!/usr/bin/perl
#
# A Sink object.
# This is an object to which data may be written, with backends to
# pass the data to several types of downstream object:
# FILE A absolute filehandle.
# APPEND A file opened for append. This becomes a FILE.
# PATH A file opened for write. This becomes a FILE.
# PIPE A shell pipeline to open.
# ARRAY A reference to an array. Data are push()ed onto it.
# SCALAR A reference to a scalar. Data are appened to it.
# Sink Another Sink-like object.
# A Sink does _not_ provide an inherent buffering.
# XXX: this may change with the async extensions.
#
# Methods:
# new type args
# Make a new cs::Sink of one of the types above.
# Args are:
# FILE Reference to a FILE.
# APPEND Pathname.
# PATH Pathname.
# ARRAY Reference to an array.
# SCALAR Reference to a scalar.
# Sink Reference to a Sink.
# Returns undef on failure.
# Flush
# Call the Flush method of the downstream object, if any.
# Write data
# Call the Write method of the downstream object.
# Returns the number of bytes written, or undef on error.
# NOTE: if less than the full data are written, the caller
# must be prepared to deal with the unwritten portion.
# Put @data
# Iterate over Write until all items in @data are written.
# Suck [n]
# Called by the downstream object if it wants "free" data.
# This is primarily to assist asynchronous I/O.
# At most n bytes of data are returned, if n > 0.
# It will call the upstream object's Suck routine at need
# if one is registered.
# SuckFrom ref
# Register ref as the upstream object whose Suck method can
# be called from "free" data.
#
=head1 NAME
cs::Sink - a data sink
=head1 SYNOPSIS
use cs::Sink;
=head1 DESCRIPTION
The cs::Sink module provides generic data sink facilities.
B<cs::Sink>s may be created which wire to a variety of objects.
=cut
use strict qw(vars);
BEGIN { use cs::DEBUG; cs::DEBUG::using(__FILE__); }
use cs::Misc;
use cs::IO;
package cs::Sink;
$cs::Sink::_UseIO=$cs::IO::_UseIO;
if ($cs::Sink::_UseIO)
{ ::need(IO);
::need(IO::File);
::need(IO::Handle);
}
=head1 GENERAL FUNCTIONS
=over 4
=item put(I<sink-args>, I<data...>)
Creates a new B<cs::Sink> using the arguments
in the array references by I<sink-args>
and writes the I<data> to it.
Returns B<undef> on error.
=cut
sub put
{ my($args)=shift;
my($s)=new cs::Sink @$args;
return undef if ! defined $s;
$s->Put(@_);
}
=back
=head1 OBJECT CREATION
=over 4
=item open(I<path>)
Create a new sink attached to the file named in I<path>.
=cut
sub open
{ new cs::Sink (PATH,@_);
}
=item new cs::Sink (I<type>,I<args...>)
Create a new sink of the specified I<type>.
I<args...> varies according to the type:
=over 6
=cut
sub new
{ my($class,$type)=(shift,shift);
my($this)={};
my(@c)=caller;
## main::carp "new Sink (class=$class, type=$type, @=[@_])";
$this->{CALLER}=[ @c ];
$this->{FLAGS}=0;
$this->{BUF}=''; # only used in asynchronous mode
if ($type eq ASYNC)
{ $this->{FLAGS}|=cs::IO::F_ASYNC;
$type=shift;
}
=item B<FILE>, I<handle>
Attach to the filehandle I<handle>.
Flushes any pending output in I<handle> as a side-effect.
=cut
if ($type eq FILE)
{ my($FILE)=shift;
$FILE->flush;
$this->{IO}=($cs::Sink::_UseIO
? new_from_fd IO::Handle (fileno($FILE),"w")
: $FILE);
$this->{FLAGS}|=$cs::IO::F_NOCLOSE;
$this->{FILE}=$FILE;
}
=item B<APPEND>, I<path>
Attach to the file named by I<path> in append mode.
=cut
elsif ($type eq APPEND)
{ my($path,$complex)=@_;
$complex=0 if ! defined $complex;
my($io,$Fpath)=_new_FILE($path,1,$complex);
return undef if ! defined $io;
$this->{IO}=$io;
$this->{PATH}=$Fpath; # debugging
$type=FILE;
}
=item B<PATH>, I<path>
Attach to the file named in I<path> in rewrite mode.
=cut
elsif ($type eq PATH)
{ my($path,$complex)=@_;
$complex=0 if ! defined $complex;
my($io,$Fpath)=_new_FILE($path,0,$complex);
return undef if ! defined $io;
$this->{IO}=$io;
$this->{PATH}=$Fpath; # debugging
$type=FILE;
}
=item B<PIPE>, I<shcmd>
Attach to a pipe to the shell command I<shcmd>.
=cut
elsif ($type eq PIPE)
{ my($pipeline)="| ".shift(@_)." ";
my($io);
$io=($cs::Sink::_UseIO ? new IO::File : cs::IO::mkHandle());
return undef if ! ($cs::Sink::_UseIO
? $io->open($pipeline)
: CORE::open($io,$pipeline));
$this->{IO}=$io;
$type=FILE;
}
=item B<ARRAY>, I<arrayref>
Attach to the array referenced by I<arrayref>.
Each B<Write()> to the sink pushes a single string
onto the array.
=cut
elsif ($type eq ARRAY)
{ $this->{ARRAY}=shift;
}
=item B<SCALAR>, I<scalarref>
Attach to the scalar referenced by I<scalarref>.
Each B<Write()> appends to the scalar.
=cut
elsif ($type eq SCALAR)
{ $this->{SCALAR}=shift;
}
=item B<Sink>, I<sink>
Attach to the B<cs::Sink> object I<sink>.
Typically used by subclasses
to apply a filter to data before depositing in I<sink>.
=cut
elsif ($type eq Sink)
{ $this->{DS}=shift;
}
else
{ warn "cs::Sink::new: unknown type \"$type\"";
return undef;
}
$this->{TYPE}=$type;
bless $this, $class;
if (exists $this->{IO} && ($this->{FLAGS}&$cs::IO::F_ASYNC))
{ cs::IO::selAddSink($this);
}
$this;
}
=back
=item tmpSink(I<tmpnam-args>)
Create a new sink object attached to a new temporary file
allocated by B<cs::Pathname::tmpnam(I<tmpnam-args>)>.
=cut
sub tmpSink
{
::need(cs::Pathname);
new cs::Sink (PATH, cs::Pathname::tmpnam(@_));
}
$cs::Sink::_new_FILE_first=1;
sub _new_FILE
{ my($path,$append,$complex)=@_;
$complex=0 if ! defined $complex;
## warn "Sink::_new_FILE(@_) ...\n";
my($f,@f);
# real path, filter list
($f,@f)=($complex
? cs::IO::choose($path,$append ? '' : undef)
: $path);
if ($append && @f)
{ warn "tried to append to \"$f\" [@f]";
return undef;
}
my($io)=cs::IO::openW($append,$f,@f);
defined $io
? wantarray ? ($io,$f) : $io
: wantarray ? (undef,undef) : undef;
}
sub DESTROY
{ _DESTROY(@_);
}
sub _DESTROY
{ my($this)=shift;
return if ! exists $this->{TYPE}; # already destroyed
my($type)=$this->{TYPE};
## warn "Sink::DESTROY($this)\n";
$this->Flush();
if (! $cs::Sink::_UseIO
&& $type eq FILE
&& ! ($this->{FLAGS} & $cs::IO::F_NOCLOSE))
{ ## warn "CLOSE($this->{IO})";
close($this->{IO})
|| warn "$::cmd: close($this->{IO}): $!";
}
else
{ ## warn "$::cmd: not try to close ".cs::Hier::h2a($this).", made from [@{$this->{CALLER}}]";
}
delete $this->{TYPE};
}
=back
=head1 OBJECT METHODS
=over 4
=item Path()
For sinks attached to files,
return the pathname of the file.
=cut
sub Path($)
{ my($this)=shift;
return undef if ! exists $this->{PATH};
$this->{PATH};
}
=item Handle()
For sinks attached to files or filehandles,
return the filehandle.
=cut
sub Handle($)
{ my($this)=@_;
exists $this->{IO} ? $this->{IO} : undef;
}
=item Put(I<data...>)
Write all the I<data> to the sink.
=cut
sub Put
{ my($this)=shift;
my($alln)=0;
my($n);
my($data)=join('',@_);
WRITE:
while (length $data)
{ $n=$this->Write($data);
if (! defined $n)
{ my@c=caller;
warn "$::cmd: write error (possibly $!), aborting with\n\t\t[$data]\n\tunwritten\n\tfrom [@c]";
return undef;
}
$alln+=$n;
substr($data,$[,$n)='';
}
$alln;
}
sub PollOut
{ my($this)=@_;
return 0 if ! length $this->{BUF};
my($n);
local($_)='';
$n=$this->{IO}->syswrite($this->BUF,length($this->{BUF}));
return undef if ! defined $n;
substr($this->{BUF},$[,$n)='';
length;
}
sub Write
{ my($this,$datum)=@_;
my($type)=$this->{TYPE};
my($io)=$this->{IO};
my($n)=length $datum;
if ($type eq FILE)
{
die "UseIO is back on!" if $cs::Sink::_UseIO;
if (! print $io $datum)
{ undef $n;
warn "print $io \$datum: errno=$!";
}
## # XXX - IO module doesn't like zero sized writes
## if ($n > 0 || ! $cs::Sink::_UseIO)
## { $n=($cs::Sink::_UseIO
## ? $io->syswrite($datum,$n)
## : $this->{FLAGS}&$cs::IO::F_RAWWRITES
## ? syswrite($io,$datum,$n)
## : (print $io $datum)
## ? $n : undef);
## }
}
elsif ($type eq Sink)
{ $n=$this->{DS}->Write($datum);
}
elsif ($type eq ARRAY)
{ push(@{$this->{ARRAY}},$datum);
}
elsif ($type eq SCALAR)
{ ${$this->{SCALAR}}.=$datum;
}
if (! defined $n)
{ my@c=caller;
warn "\$n undef! type=[$type] FILE=[$this->{FILE}]\n\tfrom [@c]";
return undef;
}
$n;
}
sub Flush
{ my($this)=shift;
my($type)=$this->{TYPE};
if ($type eq FILE)
{
$this->{IO}->flush;
}
elsif ($type eq ARRAY || $type eq SCALAR)
{}
elsif ($type eq Sink)
{ $this->{DS}->Flush();
}
else
{ warn "$::cmd: operation Flush not supported on cs::Sink/$type objects";
}
}
# take note of where to suck from
sub SuckFrom
{ my($this,$src)=@_;
$this->{SUCKFROM}=$src;
}
# retrieve any "free" data
# this is the SUCKFROM callback from the downstream module
sub Suck
{ my($this,$n)=@_;
my($type)=$this->{TYPE};
my($datum)='';
if ($type eq FILE || $type eq Sink)
{}
elsif ($type eq ARRAY)
{ $datum=shift(@{$this->{ARRAY}})
if @{$this->{ARRAY}};
}
elsif ($type eq SCALAR)
{ my($len)=length ${$this->{SCALAR}};
if ($n == 0 || $n > $len)
{ $n=$len;
}
$datum=substr(${$this->{SCALAR}},$[,$n);
substr(${$this->{SCALAR}},$[,$n)='';
}
else
{ cs::Upd::err("operation Suck not supported on Sink/$type objects\n");
$datum=undef;
}
# no free data, check for some upstream
if (! length $datum && defined $this->{SUCKFROM})
{ $datum=$this->{SUCKFROM}->Suck($n);
}
# if the free datum is too big, cut it back
if ($n > 0 && length $datum > $n)
{ my($buf)=substr($datum,$[+$n);
substr($datum,$[+$n)='';
$this->_Blow($buf);
}
$datum;
}
# push data back onto the stream for later
sub _Blow
{ my($this,$datum)=@_;
my($type)=$this->{TYPE};
if ($type eq ARRAY){ unshift(@{$this->{ARRAY}},$datum); }
else
{ die "no unsuck op for Sink/$type";
}
}
=back
=head1 SEE ALSO
cs::Source(3), cs::Pathname(3)
=head1 AUTHOR
Cameron Simpson E<lt>cs@zip.com.auE<gt>
=cut
1;
|