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