for.c revision 1.12 1 1.12 christos /* $NetBSD: for.c,v 1.12 2002/03/12 20:15:15 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.12 christos static char rcsid[] = "$NetBSD: for.c,v 1.12 2002/03/12 20:15:15 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.12 christos __RCSID("$NetBSD: for.c,v 1.12 2002/03/12 20:15:15 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.1 cgd #include <ctype.h>
60 1.7 christos #include <assert.h>
61 1.1 cgd #include "make.h"
62 1.1 cgd #include "hash.h"
63 1.1 cgd #include "dir.h"
64 1.1 cgd #include "buf.h"
65 1.1 cgd
66 1.1 cgd /*
67 1.1 cgd * For statements are of the form:
68 1.1 cgd *
69 1.1 cgd * .for <variable> in <varlist>
70 1.1 cgd * ...
71 1.1 cgd * .endfor
72 1.1 cgd *
73 1.1 cgd * The trick is to look for the matching end inside for for loop
74 1.1 cgd * To do that, we count the current nesting level of the for loops.
75 1.1 cgd * and the .endfor statements, accumulating all the statements between
76 1.4 christos * the initial .for loop and the matching .endfor;
77 1.1 cgd * then we evaluate the for loop for each variable in the varlist.
78 1.7 christos *
79 1.7 christos * Note that any nested fors are just passed through; they get handled
80 1.7 christos * recursively in For_Eval when we're expanding the enclosing for in
81 1.7 christos * For_Run.
82 1.1 cgd */
83 1.1 cgd
84 1.1 cgd static int forLevel = 0; /* Nesting level */
85 1.1 cgd
86 1.1 cgd /*
87 1.1 cgd * State of a for loop.
88 1.1 cgd */
89 1.2 jtc typedef struct _For {
90 1.7 christos Buffer buf; /* Body of loop */
91 1.7 christos char **vars; /* Iteration variables */
92 1.7 christos int nvars; /* # of iteration vars */
93 1.7 christos Lst lst; /* List of items */
94 1.2 jtc } For;
95 1.1 cgd
96 1.7 christos static For accumFor; /* Loop being accumulated */
97 1.1 cgd
98 1.7 christos static void ForAddVar __P((const char *, size_t));
99 1.1 cgd
100 1.1 cgd
101 1.1 cgd
102 1.7 christos
104 1.7 christos /*-
105 1.7 christos *-----------------------------------------------------------------------
106 1.7 christos * ForAddVar --
107 1.7 christos * Add an iteration variable to the currently accumulating for.
108 1.7 christos *
109 1.7 christos * Results: none
110 1.7 christos * Side effects: no additional side effects.
111 1.7 christos *-----------------------------------------------------------------------
112 1.7 christos */
113 1.7 christos static void
114 1.7 christos ForAddVar(data, len)
115 1.7 christos const char *data;
116 1.7 christos size_t len;
117 1.7 christos {
118 1.8 simonb Buffer buf;
119 1.7 christos int varlen;
120 1.7 christos
121 1.7 christos buf = Buf_Init(0);
122 1.7 christos Buf_AddBytes(buf, len, (Byte *) data);
123 1.7 christos
124 1.7 christos accumFor.nvars++;
125 1.7 christos accumFor.vars = erealloc(accumFor.vars, accumFor.nvars*sizeof(char *));
126 1.7 christos
127 1.7 christos accumFor.vars[accumFor.nvars-1] = (char *) Buf_GetAll(buf, &varlen);
128 1.7 christos
129 1.7 christos Buf_Destroy(buf, FALSE);
130 1.7 christos }
131 1.1 cgd
132 1.1 cgd /*-
133 1.1 cgd *-----------------------------------------------------------------------
134 1.1 cgd * For_Eval --
135 1.1 cgd * Evaluate the for loop in the passed line. The line
136 1.1 cgd * looks like this:
137 1.1 cgd * .for <variable> in <varlist>
138 1.1 cgd *
139 1.1 cgd * Results:
140 1.1 cgd * TRUE: We found a for loop, or we are inside a for loop
141 1.1 cgd * FALSE: We did not find a for loop, or we found the end of the for
142 1.1 cgd * for loop.
143 1.1 cgd *
144 1.1 cgd * Side Effects:
145 1.1 cgd * None.
146 1.1 cgd *
147 1.1 cgd *-----------------------------------------------------------------------
148 1.1 cgd */
149 1.1 cgd int
150 1.1 cgd For_Eval (line)
151 1.1 cgd char *line; /* Line to parse */
152 1.7 christos {
153 1.1 cgd char *ptr = line, *sub, *in, *wrd;
154 1.1 cgd int level; /* Level at which to report errors. */
155 1.1 cgd
156 1.1 cgd level = PARSE_FATAL;
157 1.1 cgd
158 1.1 cgd
159 1.1 cgd if (forLevel == 0) {
160 1.1 cgd Buffer buf;
161 1.12 christos int varlen;
162 1.1 cgd static const char instr[] = "in";
163 1.2 jtc
164 1.1 cgd for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
165 1.1 cgd continue;
166 1.1 cgd /*
167 1.1 cgd * If we are not in a for loop quickly determine if the statement is
168 1.1 cgd * a for.
169 1.2 jtc */
170 1.2 jtc if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
171 1.1 cgd !isspace((unsigned char) ptr[3]))
172 1.1 cgd return FALSE;
173 1.4 christos ptr += 3;
174 1.1 cgd
175 1.1 cgd /*
176 1.1 cgd * we found a for loop, and now we are going to parse it.
177 1.2 jtc */
178 1.1 cgd while (*ptr && isspace((unsigned char) *ptr))
179 1.4 christos ptr++;
180 1.1 cgd
181 1.7 christos /*
182 1.1 cgd * Find the "in".
183 1.7 christos */
184 1.7 christos for (in = ptr; *in; in++) {
185 1.7 christos if (isspace((unsigned char) in[0]) && in[1]== 'i' &&
186 1.7 christos in[2] == 'n' &&
187 1.7 christos (in[3] == '\0' || isspace((unsigned char) in[3])))
188 1.7 christos break;
189 1.7 christos }
190 1.7 christos if (*in == '\0') {
191 1.1 cgd Parse_Error(level, "missing `in' in for");
192 1.1 cgd return 0;
193 1.1 cgd }
194 1.1 cgd
195 1.7 christos /*
196 1.1 cgd * Grab the variables.
197 1.7 christos */
198 1.7 christos accumFor.vars = NULL;
199 1.7 christos
200 1.7 christos while (ptr < in) {
201 1.7 christos wrd = ptr;
202 1.7 christos while (*ptr && !isspace((unsigned char) *ptr))
203 1.7 christos ptr++;
204 1.7 christos ForAddVar(wrd, ptr - wrd);
205 1.7 christos while (*ptr && isspace((unsigned char) *ptr))
206 1.7 christos ptr++;
207 1.7 christos }
208 1.7 christos
209 1.7 christos if (accumFor.nvars == 0) {
210 1.1 cgd Parse_Error(level, "no iteration variables in for");
211 1.1 cgd return 0;
212 1.7 christos }
213 1.7 christos
214 1.12 christos /* At this point we should be pointing right at the "in" */
215 1.12 christos /*
216 1.12 christos * compensate for hp/ux's brain damaged assert macro that
217 1.12 christos * does not handle double quotes nicely.
218 1.12 christos */
219 1.7 christos assert(!memcmp(ptr, instr, 2));
220 1.1 cgd ptr += 2;
221 1.2 jtc
222 1.1 cgd while (*ptr && isspace((unsigned char) *ptr))
223 1.1 cgd ptr++;
224 1.1 cgd
225 1.1 cgd /*
226 1.1 cgd * Make a list with the remaining words
227 1.7 christos */
228 1.1 cgd accumFor.lst = Lst_Init(FALSE);
229 1.4 christos buf = Buf_Init(0);
230 1.1 cgd sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
231 1.1 cgd
232 1.1 cgd #define ADDWORD() \
233 1.1 cgd Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \
234 1.7 christos Buf_AddByte(buf, (Byte) '\0'), \
235 1.1 cgd Lst_AtFront(accumFor.lst, (ClientData) Buf_GetAll(buf, &varlen)), \
236 1.1 cgd Buf_Destroy(buf, FALSE)
237 1.2 jtc
238 1.1 cgd for (ptr = sub; *ptr && isspace((unsigned char) *ptr); ptr++)
239 1.1 cgd continue;
240 1.1 cgd
241 1.2 jtc for (wrd = ptr; *ptr; ptr++)
242 1.1 cgd if (isspace((unsigned char) *ptr)) {
243 1.1 cgd ADDWORD();
244 1.2 jtc buf = Buf_Init(0);
245 1.1 cgd while (*ptr && isspace((unsigned char) *ptr))
246 1.1 cgd ptr++;
247 1.1 cgd wrd = ptr--;
248 1.7 christos }
249 1.7 christos if (DEBUG(FOR)) {
250 1.7 christos int i;
251 1.7 christos for (i = 0; i < accumFor.nvars; i++) {
252 1.7 christos (void) fprintf(stderr, "For: variable %s\n", accumFor.vars[i]);
253 1.7 christos }
254 1.7 christos (void) fprintf(stderr, "For: list %s\n", sub);
255 1.4 christos }
256 1.1 cgd if (ptr - wrd > 0)
257 1.1 cgd ADDWORD();
258 1.1 cgd else
259 1.1 cgd Buf_Destroy(buf, TRUE);
260 1.4 christos free((Address) sub);
261 1.7 christos
262 1.1 cgd accumFor.buf = Buf_Init(0);
263 1.1 cgd forLevel++;
264 1.1 cgd return 1;
265 1.1 cgd }
266 1.1 cgd else if (*ptr == '.') {
267 1.2 jtc
268 1.1 cgd for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
269 1.1 cgd continue;
270 1.2 jtc
271 1.2 jtc if (strncmp(ptr, "endfor", 6) == 0 &&
272 1.1 cgd (isspace((unsigned char) ptr[6]) || !ptr[6])) {
273 1.1 cgd if (DEBUG(FOR))
274 1.1 cgd (void) fprintf(stderr, "For: end for %d\n", forLevel);
275 1.1 cgd if (--forLevel < 0) {
276 1.1 cgd Parse_Error (level, "for-less endfor");
277 1.1 cgd return 0;
278 1.1 cgd }
279 1.2 jtc }
280 1.2 jtc else if (strncmp(ptr, "for", 3) == 0 &&
281 1.1 cgd isspace((unsigned char) ptr[3])) {
282 1.1 cgd forLevel++;
283 1.1 cgd if (DEBUG(FOR))
284 1.1 cgd (void) fprintf(stderr, "For: new loop %d\n", forLevel);
285 1.1 cgd }
286 1.1 cgd }
287 1.1 cgd
288 1.7 christos if (forLevel != 0) {
289 1.7 christos Buf_AddBytes(accumFor.buf, strlen(line), (Byte *) line);
290 1.1 cgd Buf_AddByte(accumFor.buf, (Byte) '\n');
291 1.1 cgd return 1;
292 1.1 cgd }
293 1.1 cgd else {
294 1.1 cgd return 0;
295 1.1 cgd }
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd
299 1.1 cgd /*-
301 1.7 christos *-----------------------------------------------------------------------
302 1.1 cgd * For_Run --
303 1.1 cgd * Run the for loop, imitating the actions of an include file
304 1.1 cgd *
305 1.1 cgd * Results:
306 1.1 cgd * None.
307 1.1 cgd *
308 1.1 cgd * Side Effects:
309 1.1 cgd * None.
310 1.1 cgd *
311 1.1 cgd *-----------------------------------------------------------------------
312 1.1 cgd */
313 1.1 cgd void
314 1.2 jtc For_Run()
315 1.7 christos {
316 1.7 christos For arg;
317 1.7 christos LstNode ln;
318 1.10 mycroft char **values;
319 1.7 christos int i, done = 0, len;
320 1.7 christos char *guy, *orig_guy, *old_guy;
321 1.7 christos
322 1.7 christos if (accumFor.buf == NULL || accumFor.vars == NULL || accumFor.lst == NULL)
323 1.7 christos return;
324 1.7 christos arg = accumFor;
325 1.7 christos accumFor.buf = NULL;
326 1.7 christos accumFor.vars = NULL;
327 1.1 cgd accumFor.nvars = 0;
328 1.7 christos accumFor.lst = NULL;
329 1.1 cgd
330 1.1 cgd if (Lst_Open(arg.lst) != SUCCESS)
331 1.7 christos return;
332 1.7 christos
333 1.7 christos values = emalloc(arg.nvars * sizeof(char *));
334 1.7 christos
335 1.7 christos while (!done) {
336 1.7 christos /*
337 1.7 christos * due to the dumb way this is set up, this loop must run
338 1.7 christos * backwards.
339 1.7 christos */
340 1.7 christos for (i = arg.nvars - 1; i >= 0; i--) {
341 1.7 christos ln = Lst_Next(arg.lst);
342 1.7 christos if (ln == NILLNODE) {
343 1.7 christos if (i != arg.nvars-1) {
344 1.7 christos Parse_Error(PARSE_FATAL,
345 1.7 christos "Not enough words in for substitution list");
346 1.7 christos }
347 1.7 christos done = 1;
348 1.7 christos break;
349 1.7 christos } else {
350 1.7 christos values[i] = (char *) Lst_Datum(ln);
351 1.7 christos }
352 1.7 christos }
353 1.7 christos if (done)
354 1.7 christos break;
355 1.11 sjg
356 1.7 christos for (i = 0; i < arg.nvars; i++) {
357 1.7 christos Var_Set(arg.vars[i], values[i], VAR_GLOBAL, 0);
358 1.7 christos if (DEBUG(FOR))
359 1.7 christos (void) fprintf(stderr, "--- %s = %s\n", arg.vars[i],
360 1.7 christos values[i]);
361 1.7 christos }
362 1.7 christos
363 1.7 christos /*
364 1.7 christos * Hack, hack, kludge.
365 1.7 christos * This is really ugly, but to do it any better way would require
366 1.7 christos * making major changes to var.c, which I don't want to get into
367 1.7 christos * yet. There is no mechanism for expanding some variables, only
368 1.7 christos * for expanding a single variable. That should be corrected, but
369 1.7 christos * not right away. (XXX)
370 1.7 christos */
371 1.10 mycroft
372 1.10 mycroft guy = (char *) Buf_GetAll(arg.buf, &len);
373 1.10 mycroft orig_guy = guy;
374 1.10 mycroft for (i = 0; i < arg.nvars; i++) {
375 1.10 mycroft old_guy = guy;
376 1.10 mycroft guy = Var_Subst(arg.vars[i], guy, VAR_GLOBAL, FALSE);
377 1.10 mycroft if (old_guy != orig_guy)
378 1.7 christos free(old_guy);
379 1.7 christos }
380 1.7 christos Parse_FromString(guy);
381 1.10 mycroft
382 1.7 christos for (i = 0; i < arg.nvars; i++)
383 1.7 christos Var_Delete(arg.vars[i], VAR_GLOBAL);
384 1.7 christos }
385 1.7 christos
386 1.7 christos free(values);
387 1.7 christos
388 1.7 christos Lst_Close(arg.lst);
389 1.7 christos
390 1.7 christos for (i=0; i<arg.nvars; i++) {
391 1.7 christos free(arg.vars[i]);
392 1.1 cgd }
393 1.2 jtc free(arg.vars);
394 1.1 cgd
395 1.1 cgd Lst_Destroy(arg.lst, (void (*) __P((ClientData))) free);
396 Buf_Destroy(arg.buf, TRUE);
397 }
398