for.c revision 1.42 1 1.42 dsl /* $NetBSD: for.c,v 1.42 2009/01/10 16:59:02 dsl 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.15 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.17 ross #ifndef MAKE_NATIVE
33 1.42 dsl static char rcsid[] = "$NetBSD: for.c,v 1.42 2009/01/10 16:59:02 dsl Exp $";
34 1.6 lukem #else
35 1.5 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.3 christos #if 0
38 1.4 christos static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93";
39 1.3 christos #else
40 1.42 dsl __RCSID("$NetBSD: for.c,v 1.42 2009/01/10 16:59:02 dsl Exp $");
41 1.3 christos #endif
42 1.1 cgd #endif /* not lint */
43 1.6 lukem #endif
44 1.1 cgd
45 1.1 cgd /*-
46 1.1 cgd * for.c --
47 1.1 cgd * Functions to handle loops in a makefile.
48 1.1 cgd *
49 1.1 cgd * Interface:
50 1.1 cgd * For_Eval Evaluate the loop in the passed line.
51 1.1 cgd * For_Run Run accumulated loop
52 1.1 cgd *
53 1.1 cgd */
54 1.1 cgd
55 1.13 wiz #include <assert.h>
56 1.1 cgd #include <ctype.h>
57 1.13 wiz
58 1.1 cgd #include "make.h"
59 1.1 cgd #include "hash.h"
60 1.1 cgd #include "dir.h"
61 1.1 cgd #include "buf.h"
62 1.38 dsl #include "strlist.h"
63 1.1 cgd
64 1.42 dsl #define FOR_SUB_ESCAPE_COLON 1
65 1.42 dsl #define FOR_SUB_ESCAPE_BRACE 2
66 1.42 dsl #define FOR_SUB_ESCAPE_PAREN 4
67 1.42 dsl
68 1.1 cgd /*
69 1.1 cgd * For statements are of the form:
70 1.1 cgd *
71 1.1 cgd * .for <variable> in <varlist>
72 1.1 cgd * ...
73 1.1 cgd * .endfor
74 1.1 cgd *
75 1.1 cgd * The trick is to look for the matching end inside for for loop
76 1.1 cgd * To do that, we count the current nesting level of the for loops.
77 1.1 cgd * and the .endfor statements, accumulating all the statements between
78 1.4 christos * the initial .for loop and the matching .endfor;
79 1.1 cgd * then we evaluate the for loop for each variable in the varlist.
80 1.7 christos *
81 1.7 christos * Note that any nested fors are just passed through; they get handled
82 1.7 christos * recursively in For_Eval when we're expanding the enclosing for in
83 1.7 christos * For_Run.
84 1.1 cgd */
85 1.1 cgd
86 1.1 cgd static int forLevel = 0; /* Nesting level */
87 1.1 cgd
88 1.1 cgd /*
89 1.1 cgd * State of a for loop.
90 1.1 cgd */
91 1.2 jtc typedef struct _For {
92 1.7 christos Buffer buf; /* Body of loop */
93 1.38 dsl strlist_t vars; /* Iteration variables */
94 1.38 dsl strlist_t items; /* Substitution 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.1 cgd
100 1.7 christos
102 1.33 dsl static char *
103 1.33 dsl make_str(const char *ptr, int len)
104 1.33 dsl {
105 1.33 dsl char *new_ptr;
106 1.33 dsl
107 1.33 dsl new_ptr = bmake_malloc(len + 1);
108 1.33 dsl memcpy(new_ptr, ptr, len);
109 1.33 dsl new_ptr[len] = 0;
110 1.33 dsl return new_ptr;
111 1.33 dsl }
112 1.7 christos
113 1.7 christos /*-
114 1.1 cgd *-----------------------------------------------------------------------
115 1.1 cgd * For_Eval --
116 1.1 cgd * Evaluate the for loop in the passed line. The line
117 1.1 cgd * looks like this:
118 1.1 cgd * .for <variable> in <varlist>
119 1.13 wiz *
120 1.13 wiz * Input:
121 1.13 wiz * line Line to parse
122 1.1 cgd *
123 1.35 dsl * Results:
124 1.35 dsl * 0: Not a .for statement, parse the line
125 1.35 dsl * 1: We found a for loop
126 1.1 cgd * -1: A .for statement with a bad syntax error, discard.
127 1.1 cgd *
128 1.1 cgd * Side Effects:
129 1.1 cgd * None.
130 1.1 cgd *
131 1.1 cgd *-----------------------------------------------------------------------
132 1.1 cgd */
133 1.13 wiz int
134 1.1 cgd For_Eval(char *line)
135 1.33 dsl {
136 1.33 dsl char *ptr = line, *sub;
137 1.39 dsl int len;
138 1.42 dsl int escapes;
139 1.33 dsl unsigned char ch;
140 1.33 dsl
141 1.33 dsl /* Forget anything we previously knew about - it cannot be useful */
142 1.32 dsl memset(&accumFor, 0, sizeof accumFor);
143 1.32 dsl
144 1.32 dsl forLevel = 0;
145 1.32 dsl for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
146 1.32 dsl continue;
147 1.32 dsl /*
148 1.32 dsl * If we are not in a for loop quickly determine if the statement is
149 1.32 dsl * a for.
150 1.32 dsl */
151 1.32 dsl if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
152 1.32 dsl !isspace((unsigned char) ptr[3])) {
153 1.32 dsl if (ptr[0] == 'e' && strncmp(ptr+1, "ndfor", 5) == 0) {
154 1.32 dsl Parse_Error(PARSE_FATAL, "for-less endfor");
155 1.32 dsl return -1;
156 1.32 dsl }
157 1.32 dsl return 0;
158 1.32 dsl }
159 1.1 cgd ptr += 3;
160 1.32 dsl
161 1.32 dsl /*
162 1.32 dsl * we found a for loop, and now we are going to parse it.
163 1.1 cgd */
164 1.33 dsl
165 1.33 dsl /* Grab the variables. Terminate on "in". */
166 1.32 dsl for (;; ptr += len) {
167 1.32 dsl while (*ptr && isspace((unsigned char) *ptr))
168 1.33 dsl ptr++;
169 1.33 dsl if (*ptr == '\0') {
170 1.33 dsl Parse_Error(PARSE_FATAL, "missing `in' in for");
171 1.33 dsl return -1;
172 1.33 dsl }
173 1.33 dsl for (len = 1; ptr[len] && !isspace((unsigned char)ptr[len]); len++)
174 1.33 dsl continue;
175 1.33 dsl if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
176 1.33 dsl ptr += 2;
177 1.33 dsl break;
178 1.42 dsl }
179 1.32 dsl strlist_add_str(&accumFor.vars, make_str(ptr, len), len);
180 1.1 cgd }
181 1.38 dsl
182 1.32 dsl if (strlist_num(&accumFor.vars) == 0) {
183 1.32 dsl Parse_Error(PARSE_FATAL, "no iteration variables in for");
184 1.32 dsl return -1;
185 1.4 christos }
186 1.32 dsl
187 1.32 dsl while (*ptr && isspace((unsigned char) *ptr))
188 1.32 dsl ptr++;
189 1.32 dsl
190 1.32 dsl /*
191 1.42 dsl * Make a list with the remaining words
192 1.39 dsl * The values are substituted as ${:U<value>...} so we must \ escape
193 1.32 dsl * characters that break that syntax - particularly ':', maybe $ and \.
194 1.32 dsl */
195 1.4 christos sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
196 1.38 dsl
197 1.33 dsl for (ptr = sub;; ptr += len) {
198 1.33 dsl while (*ptr && isspace((unsigned char)*ptr))
199 1.33 dsl ptr++;
200 1.33 dsl if (*ptr == 0)
201 1.39 dsl break;
202 1.42 dsl escapes = 0;
203 1.42 dsl for (len = 0; (ch = ptr[len]) != 0 && !isspace(ch); len++) {
204 1.42 dsl if (ch == ':')
205 1.42 dsl escapes |= FOR_SUB_ESCAPE_COLON;
206 1.42 dsl else if (ch == ')')
207 1.42 dsl escapes |= FOR_SUB_ESCAPE_PAREN;
208 1.42 dsl else if (ch == /*{*/ '}')
209 1.39 dsl escapes |= FOR_SUB_ESCAPE_BRACE;
210 1.42 dsl }
211 1.33 dsl strlist_add_str(&accumFor.items, make_str(ptr, len), escapes);
212 1.1 cgd }
213 1.33 dsl
214 1.7 christos free(sub);
215 1.38 dsl
216 1.33 dsl if (strlist_num(&accumFor.items) % strlist_num(&accumFor.vars)) {
217 1.33 dsl Parse_Error(PARSE_FATAL,
218 1.38 dsl "Wrong number of words in .for substitution list %d %d",
219 1.33 dsl strlist_num(&accumFor.items), strlist_num(&accumFor.vars));
220 1.33 dsl /*
221 1.33 dsl * Return 'success' so that the body of the .for loop is accumulated.
222 1.33 dsl * The loop will have zero iterations expanded due a later test.
223 1.32 dsl */
224 1.32 dsl }
225 1.32 dsl
226 1.32 dsl accumFor.buf = Buf_Init(0);
227 1.32 dsl forLevel = 1;
228 1.32 dsl return 1;
229 1.7 christos }
230 1.35 dsl
231 1.35 dsl /*
232 1.35 dsl * Add another line to a .for loop.
233 1.35 dsl * Returns 0 when the matching .enfor is reached.
234 1.35 dsl */
235 1.32 dsl
236 1.32 dsl int
237 1.32 dsl For_Accum(char *line)
238 1.32 dsl {
239 1.26 dsl char *ptr = line;
240 1.26 dsl
241 1.1 cgd if (*ptr == '.') {
242 1.2 jtc
243 1.1 cgd for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
244 1.1 cgd continue;
245 1.2 jtc
246 1.26 dsl if (strncmp(ptr, "endfor", 6) == 0 &&
247 1.1 cgd (isspace((unsigned char) ptr[6]) || !ptr[6])) {
248 1.23 dsl if (DEBUG(FOR))
249 1.32 dsl (void)fprintf(debug_file, "For: end for %d\n", forLevel);
250 1.1 cgd if (--forLevel <= 0)
251 1.26 dsl return 0;
252 1.2 jtc } else if (strncmp(ptr, "for", 3) == 0 &&
253 1.1 cgd isspace((unsigned char) ptr[3])) {
254 1.1 cgd forLevel++;
255 1.23 dsl if (DEBUG(FOR))
256 1.1 cgd (void)fprintf(debug_file, "For: new loop %d\n", forLevel);
257 1.1 cgd }
258 1.1 cgd }
259 1.32 dsl
260 1.32 dsl Buf_AddBytes(accumFor.buf, strlen(line), (Byte *)line);
261 1.32 dsl Buf_AddByte(accumFor.buf, (Byte)'\n');
262 1.1 cgd return 1;
263 1.1 cgd }
264 1.1 cgd
265 1.1 cgd
266 1.1 cgd /*-
268 1.7 christos *-----------------------------------------------------------------------
269 1.1 cgd * For_Run --
270 1.1 cgd * Run the for loop, imitating the actions of an include file
271 1.1 cgd *
272 1.1 cgd * Results:
273 1.1 cgd * None.
274 1.1 cgd *
275 1.1 cgd * Side Effects:
276 1.1 cgd * None.
277 1.1 cgd *
278 1.42 dsl *-----------------------------------------------------------------------
279 1.42 dsl */
280 1.42 dsl
281 1.42 dsl static void
282 1.42 dsl for_substitute(Buffer cmds, strlist_t *items, unsigned int item_no, char ech)
283 1.42 dsl {
284 1.42 dsl int depth, var_depth;
285 1.42 dsl int escape;
286 1.42 dsl const char *item = strlist_str(items, item_no);
287 1.42 dsl int i;
288 1.42 dsl char ch;
289 1.42 dsl #define MAX_DEPTH 0x7fffffff
290 1.42 dsl
291 1.42 dsl /* If there were no escapes, or the only escape is the other variable
292 1.42 dsl * terminator, then just substitute the full string */
293 1.42 dsl if (!(strlist_info(items, item_no) &
294 1.42 dsl (ech == ')' ? ~FOR_SUB_ESCAPE_BRACE : ~FOR_SUB_ESCAPE_PAREN))) {
295 1.42 dsl Buf_AddBytes(cmds, strlen(item), item);
296 1.42 dsl return;
297 1.42 dsl }
298 1.42 dsl
299 1.42 dsl /* Escape ':' and 'ech' provided they aren't inside variable expansions */
300 1.42 dsl depth = 0;
301 1.42 dsl var_depth = MAX_DEPTH;
302 1.42 dsl escape = -1;
303 1.42 dsl for (i = 0; (ch = item[i]) != 0; i++) {
304 1.42 dsl /* Loose determination of nested variable definitions. */
305 1.42 dsl if (ch == '(' || ch == '{') {
306 1.42 dsl depth++;
307 1.42 dsl if (var_depth == MAX_DEPTH && i != 0 && item[i-1] == '$')
308 1.42 dsl var_depth = depth;
309 1.42 dsl } else if (ch == ')' || ch == '}') {
310 1.42 dsl if (ch == ech && depth < var_depth)
311 1.42 dsl escape = i;
312 1.42 dsl if (depth == var_depth)
313 1.42 dsl var_depth = MAX_DEPTH;
314 1.42 dsl depth--;
315 1.42 dsl } else if (ch == ':' && depth < var_depth)
316 1.42 dsl escape = i;
317 1.42 dsl if (escape == i)
318 1.42 dsl Buf_AddByte(cmds, '\\');
319 1.42 dsl Buf_AddByte(cmds, ch);
320 1.42 dsl }
321 1.42 dsl
322 1.42 dsl if (escape == -1) {
323 1.42 dsl /* We didn't actually need to escape anything, remember for next time */
324 1.42 dsl strlist_set_info(items, item_no, strlist_info(items, item_no) &
325 1.42 dsl (ech == ')' ? ~FOR_SUB_ESCAPE_PAREN : ~FOR_SUB_ESCAPE_BRACE));
326 1.42 dsl }
327 1.1 cgd }
328 1.16 enami
329 1.1 cgd void
330 1.2 jtc For_Run(int lineno)
331 1.38 dsl {
332 1.39 dsl For arg;
333 1.39 dsl int i, len;
334 1.42 dsl unsigned int num_items;
335 1.39 dsl char *for_body;
336 1.39 dsl char *var;
337 1.39 dsl char *cp;
338 1.39 dsl char *cmd_cp;
339 1.39 dsl char *body_end;
340 1.39 dsl char ch;
341 1.7 christos Buffer cmds;
342 1.7 christos int short_var;
343 1.38 dsl
344 1.1 cgd arg = accumFor;
345 1.39 dsl memset(&accumFor, 0, sizeof accumFor);
346 1.39 dsl
347 1.33 dsl num_items = strlist_num(&arg.items);
348 1.38 dsl if (num_items % strlist_num(&arg.vars))
349 1.7 christos /* Error message already printed */
350 1.39 dsl goto out;
351 1.39 dsl
352 1.39 dsl short_var = 0;
353 1.39 dsl STRLIST_FOREACH(var, &arg.vars, i) {
354 1.39 dsl if (var[1] == 0) {
355 1.10 mycroft short_var = 1;
356 1.7 christos break;
357 1.7 christos }
358 1.39 dsl }
359 1.39 dsl
360 1.39 dsl /*
361 1.39 dsl * Scan the for loop body and replace references to the loop variables
362 1.39 dsl * with variable references that expand to the required text.
363 1.39 dsl * Using variable expansions ensures that the .for loop can't generate
364 1.39 dsl * syntax, and that the later parsing will still see a variable.
365 1.39 dsl * We assume that the null variable will never be defined.
366 1.39 dsl *
367 1.39 dsl * The detection of substitions of the loop control variable is naive.
368 1.39 dsl * Many of the modifiers use \ to escape $ (not $) so it is possible
369 1.39 dsl * to contrive a makefile where an unwanted substitution happens.
370 1.39 dsl *
371 1.39 dsl * Each loop expansion is fed back into the parser as if it were an
372 1.39 dsl * include file. This means we have to generate the last iteration first.
373 1.39 dsl */
374 1.39 dsl while (num_items != 0) {
375 1.39 dsl num_items -= strlist_num(&arg.vars);
376 1.39 dsl for_body = (char *)Buf_GetAll(arg.buf, &len);
377 1.39 dsl body_end = for_body + len;
378 1.39 dsl cmds = Buf_Init(len + 256);
379 1.42 dsl cmd_cp = for_body;
380 1.39 dsl for (cp = for_body; (cp = strchr(cp, '$')) != NULL;) {
381 1.42 dsl char ech;
382 1.39 dsl ch = *++cp;
383 1.41 dsl if ((ch == '(' && (ech = ')')) || (ch == '{' && (ech = '}'))) {
384 1.39 dsl cp++;
385 1.42 dsl /* Check variable name against the .for loop variables */
386 1.39 dsl STRLIST_FOREACH(var, &arg.vars, i) {
387 1.39 dsl len = strlist_info(&arg.vars, i);
388 1.41 dsl if (memcmp(cp, var, len) != 0)
389 1.39 dsl continue;
390 1.42 dsl if (cp[len] != ':' && cp[len] != ech && cp[len] != '\\')
391 1.39 dsl continue;
392 1.39 dsl /* Found a variable match. Replace with :U<value> */
393 1.39 dsl Buf_AddBytes(cmds, cp - cmd_cp, cmd_cp);
394 1.39 dsl Buf_AddBytes(cmds, 2, ":U");
395 1.42 dsl cp += len;
396 1.39 dsl cmd_cp = cp;
397 1.39 dsl for_substitute(cmds, &arg.items, num_items + i, ech);
398 1.39 dsl break;
399 1.39 dsl }
400 1.39 dsl continue;
401 1.39 dsl }
402 1.42 dsl if (ch == 0)
403 1.39 dsl break;
404 1.39 dsl /* Probably a single character name, ignore $$ and stupid ones. {*/
405 1.39 dsl if (!short_var || strchr("}):$", ch) != NULL) {
406 1.39 dsl cp++;
407 1.39 dsl continue;
408 1.39 dsl }
409 1.39 dsl STRLIST_FOREACH(var, &arg.vars, i) {
410 1.39 dsl if (var[0] != ch || var[1] != 0)
411 1.39 dsl continue;
412 1.39 dsl /* Found a variable match. Replace with ${:U<value>} */
413 1.39 dsl Buf_AddBytes(cmds, cp - cmd_cp, cmd_cp);
414 1.42 dsl Buf_AddBytes(cmds, 3, "{:U");
415 1.39 dsl cmd_cp = ++cp;
416 1.39 dsl for_substitute(cmds, &arg.items, num_items + i, /*{*/ '}');
417 1.39 dsl Buf_AddBytes(cmds, 1, "}");
418 1.39 dsl break;
419 1.39 dsl }
420 1.39 dsl }
421 1.39 dsl Buf_AddBytes(cmds, body_end - cmd_cp, cmd_cp);
422 1.39 dsl
423 1.39 dsl cp = Buf_GetAll(cmds, NULL);
424 1.39 dsl if (DEBUG(FOR))
425 1.39 dsl (void)fprintf(debug_file, "For: loop body:\n%s", cp);
426 1.39 dsl Parse_SetInput(NULL, lineno, -1, cp);
427 1.7 christos Buf_Destroy(cmds, FALSE);
428 1.38 dsl }
429 1.38 dsl
430 1.38 dsl out:
431 1.1 cgd strlist_clean(&arg.vars);
432 1.1 cgd strlist_clean(&arg.items);
433 1.1 cgd
434 Buf_Destroy(arg.buf, TRUE);
435 }
436