#!/usr/bin/env perl # reservoir -- read stdin until EOF, and _then_ write it all to stdout. $usage = "usage: reservoir [ -o file | -O file ]\n". "where: -O file open and write to file after end of input\n". " -o file same, but only if output is non-empty\n". " also: reservoir --version report version number\n" . " reservoir --help display this help text\n" . " reservoir --licence display (MIT) licence text\n"; $licence = "reservoir is copyright 2005,2008 Simon Tatham.\n" . "\n" . "Permission is hereby granted, free of charge, to any person\n" . "obtaining a copy of this software and associated documentation files\n" . "(the \"Software\"), to deal in the Software without restriction,\n" . "including without limitation the rights to use, copy, modify, merge,\n" . "publish, distribute, sublicense, and/or sell copies of the Software,\n" . "and to permit persons to whom the Software is furnished to do so,\n" . "subject to the following conditions:\n" . "\n" . "The above copyright notice and this permission notice shall be\n" . "included in all copies or substantial portions of the Software.\n" . "\n" . "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" . "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" . "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" . "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" . "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" . "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" . "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" . "SOFTWARE.\n"; $outputfile = undef; $allowemptyoutput = 0; while ($_=shift @ARGV) { last if /^--$/; unshift (@ARGV, $_), last unless /^-(.*)/; $arg = $1; if ($arg eq "-help") { print STDERR $usage; exit 0; } elsif ($arg eq "-version") { if ('$Revision$' =~ /Revision:\s+(\d+)/) { print "reservoir revision $1\n"; } else { print "reservoir: unknown revision\n"; } exit 0; } elsif ($arg eq "-licence" or $arg eq "-license") { print $licence; exit 0; } elsif ($arg =~ /^([oO])(.*)$/) { $allowemptyoutput = ($1 eq "O"); $outputfile = $2; $outputfile = shift @ARGV if $outputfile eq ""; die "reservoir: expected file name after '-$1'\n" if $outputfile eq ""; } else { die "reservoir: unrecognised option '-$arg'\n"; } } die $usage if $#ARGV > 0; $data = ''; $data .= $_ while ; if (defined $outputfile) { exit unless length($data) or $allowemptyoutput; open OUT, ">$outputfile" or die "$outputfile: open: $!\n"; select OUT; } print $data;