This occasionally comes in handy, so I'll add it to utils.
[sgt/utils] / reservoir / reservoir
diff --git a/reservoir/reservoir b/reservoir/reservoir
new file mode 100755 (executable)
index 0000000..fe01ba1
--- /dev/null
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+
+# reservoir -- read stdin until EOF, and _then_ write it all to stdout.
+
+$usage =
+  "usage: reservoir [ -o file ]\n".
+  "where: -o file                open and write to file after end of input\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 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;
+
+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: 4876 $' =~ /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 =~ /^o(.*)$/) {
+    $outputfile = $1;
+    $outputfile = shift @ARGV if $outputfile eq "";
+    die "reservoir: expected file name after '-o'\n" if $outputfile eq "";
+  } else {
+    die "reservoir: unrecognised option '-$arg'\n";
+  }
+}
+
+die $usage if $#ARGV > 0;
+
+$data = '';
+
+$data .= $_ while <STDIN>;
+
+if (defined $outputfile) {
+    open OUT, ">$outputfile" or die "$outputfile: open: $!\n";
+    select OUT;
+}
+print $data;