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