fe01ba153ccfb82235bb7fc68bd57ca208b0b555
[sgt/utils] / reservoir / reservoir
1 #!/usr/bin/env perl
2
3 # reservoir -- read stdin until EOF, and _then_ write it all to stdout.
4
5 $usage =
6 "usage: reservoir [ -o file ]\n".
7 "where: -o file open and write to file after end of input\n".
8 " also: reservoir --version report version number\n" .
9 " reservoir --help display this help text\n" .
10 " reservoir --licence display (MIT) licence text\n";
11
12 $licence =
13 "reservoir is copyright 2005 Simon Tatham.\n" .
14 "\n" .
15 "Permission is hereby granted, free of charge, to any person\n" .
16 "obtaining a copy of this software and associated documentation files\n" .
17 "(the \"Software\"), to deal in the Software without restriction,\n" .
18 "including without limitation the rights to use, copy, modify, merge,\n" .
19 "publish, distribute, sublicense, and/or sell copies of the Software,\n" .
20 "and to permit persons to whom the Software is furnished to do so,\n" .
21 "subject to the following conditions:\n" .
22 "\n" .
23 "The above copyright notice and this permission notice shall be\n" .
24 "included in all copies or substantial portions of the Software.\n" .
25 "\n" .
26 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" .
27 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" .
28 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" .
29 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" .
30 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" .
31 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" .
32 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" .
33 "SOFTWARE.\n";
34
35 $outputfile = undef;
36
37 while ($_=shift @ARGV) {
38 last if /^--$/;
39 unshift (@ARGV, $_), last unless /^-(.*)/;
40 $arg = $1;
41 if ($arg eq "-help") {
42 print STDERR $usage;
43 exit 0;
44 } elsif ($arg eq "-version") {
45 if ('$Revision: 4876 $' =~ /Revision:\s+(\d+)/) {
46 print "reservoir revision $1\n";
47 } else {
48 print "reservoir: unknown revision\n";
49 }
50 exit 0;
51 } elsif ($arg eq "-licence" or $arg eq "-license") {
52 print $licence;
53 exit 0;
54 } elsif ($arg =~ /^o(.*)$/) {
55 $outputfile = $1;
56 $outputfile = shift @ARGV if $outputfile eq "";
57 die "reservoir: expected file name after '-o'\n" if $outputfile eq "";
58 } else {
59 die "reservoir: unrecognised option '-$arg'\n";
60 }
61 }
62
63 die $usage if $#ARGV > 0;
64
65 $data = '';
66
67 $data .= $_ while <STDIN>;
68
69 if (defined $outputfile) {
70 open OUT, ">$outputfile" or die "$outputfile: open: $!\n";
71 select OUT;
72 }
73 print $data;