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