1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/* $NetBSD: for.c,v 1.1.1.1 2004/03/11 13:04:07 grant Exp $ */
/*
* Copyright (c) 1992, The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef MAKE_BOOTSTRAP
static char rcsid[] = "$NetBSD: for.c,v 1.1.1.1 2004/03/11 13:04:07 grant Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: for.c,v 1.1.1.1 2004/03/11 13:04:07 grant Exp $");
#endif
#endif /* not lint */
#endif
/*-
* for.c --
* Functions to handle loops in a makefile.
*
* Interface:
* For_Eval Evaluate the loop in the passed line.
* For_Run Run accumulated loop
*
*/
#include <ctype.h>
#include <assert.h>
#include "make.h"
#include "hash.h"
#include "dir.h"
#include "buf.h"
/*
* For statements are of the form:
*
* .for <variable> in <varlist>
* ...
* .endfor
*
* The trick is to look for the matching end inside for for loop
* To do that, we count the current nesting level of the for loops.
* and the .endfor statements, accumulating all the statements between
* the initial .for loop and the matching .endfor;
* then we evaluate the for loop for each variable in the varlist.
*
* Note that any nested fors are just passed through; they get handled
* recursively in For_Eval when we're expanding the enclosing for in
* For_Run.
*/
static int forLevel = 0; /* Nesting level */
/*
* State of a for loop.
*/
typedef struct _For {
Buffer buf; /* Body of loop */
char **vars; /* Iteration variables */
int nvars; /* # of iteration vars */
Lst lst; /* List of items */
} For;
static For accumFor; /* Loop being accumulated */
static void ForAddVar __P((const char *, size_t));
/*-
*-----------------------------------------------------------------------
* ForAddVar --
* Add an iteration variable to the currently accumulating for.
*
* Results: none
* Side effects: no additional side effects.
*-----------------------------------------------------------------------
*/
static void
ForAddVar(data, len)
const char *data;
size_t len;
{
Buffer buf;
int varlen;
buf = Buf_Init(0);
Buf_AddBytes(buf, len, (Byte *) data);
accumFor.nvars++;
accumFor.vars = erealloc(accumFor.vars, accumFor.nvars*sizeof(char *));
accumFor.vars[accumFor.nvars-1] = (char *) Buf_GetAll(buf, &varlen);
Buf_Destroy(buf, FALSE);
}
/*-
*-----------------------------------------------------------------------
* For_Eval --
* Evaluate the for loop in the passed line. The line
* looks like this:
* .for <variable> in <varlist>
*
* Results:
* TRUE: We found a for loop, or we are inside a for loop
* FALSE: We did not find a for loop, or we found the end of the for
* for loop.
*
* Side Effects:
* None.
*
*-----------------------------------------------------------------------
*/
int
For_Eval (line)
char *line; /* Line to parse */
{
char *ptr = line, *sub, *in, *wrd;
int level; /* Level at which to report errors. */
level = PARSE_FATAL;
if (forLevel == 0) {
Buffer buf;
int varlen;
for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
continue;
/*
* If we are not in a for loop quickly determine if the statement is
* a for.
*/
if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
!isspace((unsigned char) ptr[3]))
return FALSE;
ptr += 3;
/*
* we found a for loop, and now we are going to parse it.
*/
while (*ptr && isspace((unsigned char) *ptr))
ptr++;
/*
* Find the "in".
*/
for (in = ptr; *in; in++) {
if (isspace((unsigned char) in[0]) && in[1]== 'i' &&
in[2] == 'n' &&
(in[3] == '\0' || isspace((unsigned char) in[3])))
break;
}
if (*in == '\0') {
Parse_Error(level, "missing `in' in for");
return 0;
}
/*
* Grab the variables.
*/
accumFor.vars = NULL;
while (ptr < in) {
wrd = ptr;
while (*ptr && !isspace((unsigned char) *ptr))
ptr++;
ForAddVar(wrd, ptr - wrd);
while (*ptr && isspace((unsigned char) *ptr))
ptr++;
}
if (accumFor.nvars == 0) {
Parse_Error(level, "no iteration variables in for");
return 0;
}
/* At this point we should be pointing right at the "in" */
assert(!memcmp(ptr, "in", 2));
ptr += 2;
while (*ptr && isspace((unsigned char) *ptr))
ptr++;
/*
* Make a list with the remaining words
*/
accumFor.lst = Lst_Init(FALSE);
buf = Buf_Init(0);
sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
#define ADDWORD() \
Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \
Buf_AddByte(buf, (Byte) '\0'), \
Lst_AtFront(accumFor.lst, (ClientData) Buf_GetAll(buf, &varlen)), \
Buf_Destroy(buf, FALSE)
for (ptr = sub; *ptr && isspace((unsigned char) *ptr); ptr++)
continue;
for (wrd = ptr; *ptr; ptr++)
if (isspace((unsigned char) *ptr)) {
ADDWORD();
buf = Buf_Init(0);
while (*ptr && isspace((unsigned char) *ptr))
ptr++;
wrd = ptr--;
}
if (DEBUG(FOR)) {
int i;
for (i = 0; i < accumFor.nvars; i++) {
(void) fprintf(stderr, "For: variable %s\n", accumFor.vars[i]);
}
(void) fprintf(stderr, "For: list %s\n", sub);
}
if (ptr - wrd > 0)
ADDWORD();
else
Buf_Destroy(buf, TRUE);
free((Address) sub);
accumFor.buf = Buf_Init(0);
forLevel++;
return 1;
}
else if (*ptr == '.') {
for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
continue;
if (strncmp(ptr, "endfor", 6) == 0 &&
(isspace((unsigned char) ptr[6]) || !ptr[6])) {
if (DEBUG(FOR))
(void) fprintf(stderr, "For: end for %d\n", forLevel);
if (--forLevel < 0) {
Parse_Error (level, "for-less endfor");
return 0;
}
}
else if (strncmp(ptr, "for", 3) == 0 &&
isspace((unsigned char) ptr[3])) {
forLevel++;
if (DEBUG(FOR))
(void) fprintf(stderr, "For: new loop %d\n", forLevel);
}
}
if (forLevel != 0) {
Buf_AddBytes(accumFor.buf, strlen(line), (Byte *) line);
Buf_AddByte(accumFor.buf, (Byte) '\n');
return 1;
}
else {
return 0;
}
}
/*-
*-----------------------------------------------------------------------
* For_Run --
* Run the for loop, imitating the actions of an include file
*
* Results:
* None.
*
* Side Effects:
* None.
*
*-----------------------------------------------------------------------
*/
void
For_Run()
{
For arg;
LstNode ln;
char **values;
int i, done = 0, len;
char *guy, *orig_guy, *old_guy;
if (accumFor.buf == NULL || accumFor.vars == NULL || accumFor.lst == NULL)
return;
arg = accumFor;
accumFor.buf = NULL;
accumFor.vars = NULL;
accumFor.nvars = 0;
accumFor.lst = NULL;
if (Lst_Open(arg.lst) != SUCCESS)
return;
values = emalloc(arg.nvars * sizeof(char *));
while (!done) {
/*
* due to the dumb way this is set up, this loop must run
* backwards.
*/
for (i = arg.nvars - 1; i >= 0; i--) {
ln = Lst_Next(arg.lst);
if (ln == NILLNODE) {
if (i != arg.nvars-1) {
Parse_Error(PARSE_FATAL,
"Not enough words in for substitution list");
}
done = 1;
break;
} else {
values[i] = (char *) Lst_Datum(ln);
}
}
if (done)
break;
for (i = 0; i < arg.nvars; i++) {
Var_Set(arg.vars[i], values[i], VAR_GLOBAL, 0);
if (DEBUG(FOR))
(void) fprintf(stderr, "--- %s = %s\n", arg.vars[i],
values[i]);
}
/*
* Hack, hack, kludge.
* This is really ugly, but to do it any better way would require
* making major changes to var.c, which I don't want to get into
* yet. There is no mechanism for expanding some variables, only
* for expanding a single variable. That should be corrected, but
* not right away. (XXX)
*/
guy = (char *) Buf_GetAll(arg.buf, &len);
orig_guy = guy;
for (i = 0; i < arg.nvars; i++) {
old_guy = guy;
guy = Var_Subst(arg.vars[i], guy, VAR_GLOBAL, FALSE);
if (old_guy != orig_guy)
free(old_guy);
}
Parse_FromString(guy);
for (i = 0; i < arg.nvars; i++)
Var_Delete(arg.vars[i], VAR_GLOBAL);
}
free(values);
Lst_Close(arg.lst);
for (i=0; i<arg.nvars; i++) {
free(arg.vars[i]);
}
free(arg.vars);
Lst_Destroy(arg.lst, (void (*) __P((ClientData))) free);
Buf_Destroy(arg.buf, TRUE);
}
|