source: trunk/essentials/sys-devel/automake-1.9/lib/Automake/XFile.pm

Last change on this file was 3086, checked in by bird, 18 years ago

automake 1.9.6

File size: 6.8 KB
Line 
1# Copyright (C) 2001, 2003 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2, or (at your option)
6# any later version.
7
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16# 02110-1301, USA.
17
18# Written by Akim Demaille <akim@freefriends.org>.
19
20###############################################################
21# The main copy of this file is in Automake's CVS repository. #
22# Updates should be sent to automake-patches@gnu.org. #
23###############################################################
24
25package Automake::XFile;
26
27=head1 NAME
28
29Automake::XFile - supply object methods for filehandles with error handling
30
31=head1 SYNOPSIS
32
33 use Automake::XFile;
34
35 $fh = new Automake::XFile;
36 $fh->open ("< file");
37 # No need to check $FH: we died if open failed.
38 print <$fh>;
39 $fh->close;
40 # No need to check the return value of close: we died if it failed.
41
42 $fh = new Automake::XFile "> file";
43 # No need to check $FH: we died if new failed.
44 print $fh "bar\n";
45 $fh->close;
46
47 $fh = new Automake::XFile "file", "r";
48 # No need to check $FH: we died if new failed.
49 defined $fh
50 print <$fh>;
51 undef $fh; # automatically closes the file and checks for errors.
52
53 $fh = new Automake::XFile "file", O_WRONLY | O_APPEND;
54 # No need to check $FH: we died if new failed.
55 print $fh "corge\n";
56
57 $pos = $fh->getpos;
58 $fh->setpos ($pos);
59
60 undef $fh; # automatically closes the file and checks for errors.
61
62 autoflush STDOUT 1;
63
64=head1 DESCRIPTION
65
66C<Automake::XFile> inherits from C<IO::File>. It provides the method
67C<name> returning the file name. It provides dying version of the
68methods C<close>, C<lock> (corresponding to C<flock>), C<new>,
69C<open>, C<seek>, and C<trunctate>. It also overrides the C<getline>
70and C<getlines> methods to translate C<\r\n> to C<\n>.
71
72=head1 SEE ALSO
73
74L<perlfunc>,
75L<perlop/"I/O Operators">,
76L<IO::File>
77L<IO::Handle>
78L<IO::Seekable>
79
80=head1 HISTORY
81
82Derived from IO::File.pm by Akim Demaille E<lt>F<akim@freefriends.org>E<gt>.
83
84=cut
85
86require 5.000;
87use strict;
88use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD @ISA);
89use Carp;
90use Errno;
91use IO::File;
92use File::Basename;
93use Automake::ChannelDefs;
94use Automake::FileUtils;
95
96require Exporter;
97require DynaLoader;
98
99@ISA = qw(IO::File Exporter DynaLoader);
100
101$VERSION = "1.2";
102
103@EXPORT = @IO::File::EXPORT;
104
105eval {
106 # Make all Fcntl O_XXX and LOCK_XXX constants available for importing
107 require Fcntl;
108 my @O = grep /^(LOCK|O)_/, @Fcntl::EXPORT, @Fcntl::EXPORT_OK;
109 Fcntl->import (@O); # first we import what we want to export
110 push (@EXPORT, @O);
111};
112
113# Used in croak error messages.
114my $me = basename ($0);
115
116################################################
117## Constructor
118##
119
120sub new
121{
122 my $type = shift;
123 my $class = ref $type || $type || "Automake::XFile";
124 my $fh = $class->SUPER::new ();
125 if (@_)
126 {
127 $fh->open (@_);
128 }
129 $fh;
130}
131
132################################################
133## Open
134##
135
136sub open
137{
138 my $fh = shift;
139 my ($file) = @_;
140
141 # WARNING: Gross hack: $FH is a typeglob: use its hash slot to store
142 # the `name' of the file we are opening. See the example with
143 # io_socket_timeout in IO::Socket for more, and read Graham's
144 # comment in IO::Handle.
145 ${*$fh}{'autom4te_xfile_file'} = "$file";
146
147 if (!$fh->SUPER::open (@_))
148 {
149 fatal "cannot open $file: $!";
150 }
151
152 # In case we're running under MSWindows, don't write with CRLF.
153 # (This circumvents a bug in at least Cygwin bash where the shell
154 # parsing fails on lines ending with the continuation character '\'
155 # and CRLF).
156 binmode $fh if $file =~ /^\s*>/;
157}
158
159################################################
160## Close
161##
162
163sub close
164{
165 my $fh = shift;
166 if (!$fh->SUPER::close (@_))
167 {
168 my $file = $fh->name;
169 Automake::FileUtils::handle_exec_errors $file
170 unless $!;
171 fatal "cannot close $file: $!";
172 }
173}
174
175################################################
176## Getline
177##
178
179# Some Win32/perl installations fail to translate \r\n to \n on input
180# so we do that here.
181sub getline
182{
183 local $_ = $_[0]->SUPER::getline;
184 # Perform a _global_ replacement: $_ may can contains many lines
185 # in slurp mode ($/ = undef).
186 s/\015\012/\n/gs if defined $_;
187 return $_;
188}
189
190################################################
191## Getlines
192##
193
194sub getlines
195{
196 my @res = ();
197 my $line;
198 push @res, $line while $line = $_[0]->getline;
199 return @res;
200}
201
202################################################
203## Name
204##
205
206sub name
207{
208 my $fh = shift;
209 return ${*$fh}{'autom4te_xfile_file'};
210}
211
212################################################
213## Lock
214##
215
216sub lock
217{
218 my ($fh, $mode) = @_;
219 # Cannot use @_ here.
220
221 # On some systems (e.g. GNU/Linux with NFSv2), flock(2) does not work over
222 # NFS, but Perl prefers that over fcntl(2) if it exists and if
223 # perl was not built with -Ud_flock. Normally, this problem is harmless,
224 # so ignore the ENOLCK errors that are reported in that situation,
225 # However, if the invoker is using "make -j", the problem is not harmless,
226 # so report it in that case, by inspecting MAKEFLAGS and looking for
227 # any arguments indicating that the invoker used -j.
228 # Admittedly this is a bit of a hack.
229 if (!flock ($fh, $mode)
230 && (!$!{ENOLCK}
231 || " -$ENV{'MAKEFLAGS'}" =~ / (-[BdeikrRsSw]*j|---?jobs)/))
232 {
233 my $file = $fh->name;
234 fatal "cannot lock $file with mode $mode (perhaps you are running make -j on a lame NFS client?): $!";
235 }
236}
237
238################################################
239## Seek
240##
241
242sub seek
243{
244 my $fh = shift;
245 # Cannot use @_ here.
246 if (!seek ($fh, $_[0], $_[1]))
247 {
248 my $file = $fh->name;
249 fatal "$me: cannot rewind $file with @_: $!";
250 }
251}
252
253################################################
254## Truncate
255##
256
257sub truncate
258{
259 my ($fh, $len) = @_;
260 if (!truncate ($fh, $len))
261 {
262 my $file = $fh->name;
263 fatal "cannot truncate $file at $len: $!";
264 }
265}
266
2671;
268
269### Setup "GNU" style for perl-mode and cperl-mode.
270## Local Variables:
271## perl-indent-level: 2
272## perl-continued-statement-offset: 2
273## perl-continued-brace-offset: 0
274## perl-brace-offset: 0
275## perl-brace-imaginary-offset: 0
276## perl-label-offset: -2
277## cperl-indent-level: 2
278## cperl-brace-offset: 0
279## cperl-continued-brace-offset: 0
280## cperl-label-offset: -2
281## cperl-extra-newline-before-brace: t
282## cperl-merge-trailing-else: nil
283## cperl-continued-statement-offset: 2
284## End:
Note: See TracBrowser for help on using the repository browser.