1169689Skan# autoconf -- create `configure' using m4 macros
2169689Skan# Copyright (C) 2003, 2006  Free Software Foundation, Inc.
3169689Skan
4169689Skan# This program is free software; you can redistribute it and/or modify
5169689Skan# it under the terms of the GNU General Public License as published by
6169689Skan# the Free Software Foundation; either version 2, or (at your option)
7169689Skan# any later version.
8169689Skan
9169689Skan# This program is distributed in the hope that it will be useful,
10169689Skan# but WITHOUT ANY WARRANTY; without even the implied warranty of
11169689Skan# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12169689Skan# GNU General Public License for more details.
13169689Skan
14169689Skan# You should have received a copy of the GNU General Public License
15169689Skan# along with this program; if not, write to the Free Software
16169689Skan# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17169689Skan# 02110-1301, USA.
18169689Skan
19169689Skanpackage Autom4te::C4che;
20169689Skan
21169689Skan=head1 NAME
22169689Skan
23169689SkanAutom4te::C4che - a single m4 run request
24169689Skan
25169689Skan=head1 SYNOPSIS
26169689Skan
27169689Skan  use Autom4te::C4che;
28169689Skan
29169689Skan=head1 DESCRIPTION
30169689Skan
31169689SkanThis Perl module handles the cache of M4 runs used by autom4te.
32169689Skan
33169689Skan=cut
34169689Skan
35169689Skanuse Data::Dumper;
36169689Skanuse Autom4te::Request;
37169689Skanuse Carp;
38169689Skanuse strict;
39169689Skan
40169689Skan=over 4
41169689Skan
42169689Skan=item @request
43169689Skan
44169689SkanList of requests.
45169689Skan
46169689SkanWe cannot declare it "my" as the loading, performed via "do", would
47refer to another scope, and @request would not be updated.  It used to
48work with "my" vars, and I do not know whether the current behavior
49(5.6) is wanted or not.
50
51=cut
52
53use vars qw(@request);
54
55=item C<$req = Autom4te::C4che-E<gt>retrieve (%attr)>
56
57Find a request with the same path and input.
58
59=cut
60
61sub retrieve($%)
62{
63  my ($self, %attr) = @_;
64
65  foreach (@request)
66    {
67      # Same path.
68      next
69	if join ("\n", @{$_->path}) ne join ("\n", @{$attr{path}});
70
71      # Same inputs.
72      next
73	if join ("\n", @{$_->input}) ne join ("\n", @{$attr{input}});
74
75      # Found it.
76      return $_;
77    }
78
79  return undef;
80}
81
82=item C<$req = Autom4te::C4che-E<gt>register (%attr)>
83
84Create and register a request for these path and input.
85
86=cut
87
88# $REQUEST-OBJ
89# register ($SELF, %ATTR)
90# -----------------------
91# NEW should not be called directly.
92# Private.
93sub register ($%)
94{
95  my ($self, %attr) = @_;
96
97  # path and input are the only ID for a request object.
98  my $obj = new Autom4te::Request ('path'  => $attr{path},
99				   'input' => $attr{input});
100  push @request, $obj;
101
102  # Assign an id for cache file.
103  $obj->id ("$#request");
104
105  return $obj;
106}
107
108
109=item C<$req = Autom4te::C4che-E<gt>request (%request)>
110
111Get (retrieve or create) a request for the path C<$request{path}> and
112the input C<$request{input}>.
113
114=cut
115
116# $REQUEST-OBJ
117# request($SELF, %REQUEST)
118# ------------------------
119sub request ($%)
120{
121  my ($self, %request) = @_;
122
123  my $req =
124    Autom4te::C4che->retrieve (%request)
125    || Autom4te::C4che->register (%request);
126
127  # If there are new traces to produce, then we are not valid.
128  foreach (@{$request{'macro'}})
129    {
130      if (! exists ${$req->macro}{$_})
131	{
132	  ${$req->macro}{$_} = 1;
133	  $req->valid (0);
134	}
135    }
136
137  # It would be great to have $REQ check that it is up to date wrt
138  # its dependencies, but that requires getting traces (to fetch the
139  # included files), which is out of the scope of Request (currently?).
140
141  return $req;
142}
143
144
145=item C<$string = Autom4te::C4che-E<gt>marshall ()>
146
147Serialize all the current requests.
148
149=cut
150
151
152# marshall($SELF)
153# ---------------
154sub marshall ($)
155{
156  my ($caller) = @_;
157  my $res = '';
158
159  my $marshall = Data::Dumper->new ([\@request], [qw (*request)]);
160  $marshall->Indent(2)->Terse(0);
161  $res = $marshall->Dump . "\n";
162
163  return $res;
164}
165
166
167=item C<Autom4te::C4che-E<gt>save ($file)>
168
169Save the cache in the C<$file> file object.
170
171=cut
172
173# SAVE ($FILE)
174# ------------
175sub save ($$)
176{
177  my ($self, $file) = @_;
178
179  confess "cannot save a single request\n"
180    if ref ($self);
181
182  $file->seek (0, 0);
183  $file->truncate (0);
184  print $file
185    "# This file was generated.\n",
186    "# It contains the lists of macros which have been traced.\n",
187    "# It can be safely removed.\n",
188    "\n",
189    $self->marshall;
190}
191
192
193=item C<Autom4te::C4che-E<gt>load ($file)>
194
195Load the cache from the C<$file> file object.
196
197=cut
198
199# LOAD ($FILE)
200# ------------
201sub load ($$)
202{
203  my ($self, $file) = @_;
204  my $fname = $file->name;
205
206  confess "cannot load a single request\n"
207    if ref ($self);
208
209  my $contents = join "", $file->getlines;
210
211  eval $contents;
212
213  confess "cannot eval $fname: $@\n" if $@;
214}
215
216
217=head1 SEE ALSO
218
219L<Autom4te::Request>
220
221=head1 HISTORY
222
223Written by Akim Demaille E<lt>F<akim@freefriends.org>E<gt>.
224
225=cut
226
2271; # for require
228
229### Setup "GNU" style for perl-mode and cperl-mode.
230## Local Variables:
231## perl-indent-level: 2
232## perl-continued-statement-offset: 2
233## perl-continued-brace-offset: 0
234## perl-brace-offset: 0
235## perl-brace-imaginary-offset: 0
236## perl-label-offset: -2
237## cperl-indent-level: 2
238## cperl-brace-offset: 0
239## cperl-continued-brace-offset: 0
240## cperl-label-offset: -2
241## cperl-extra-newline-before-brace: t
242## cperl-merge-trailing-else: nil
243## cperl-continued-statement-offset: 2
244## End:
245