for.c revision 1.100 1 1.100 rillig /* $NetBSD: for.c,v 1.100 2020/10/25 14:58:23 rillig 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.1 cgd /*-
33 1.80 rillig * Handling of .for/.endfor loops in a makefile.
34 1.80 rillig *
35 1.80 rillig * For loops are of the form:
36 1.80 rillig *
37 1.80 rillig * .for <varname...> in <value...>
38 1.80 rillig * ...
39 1.80 rillig * .endfor
40 1.80 rillig *
41 1.80 rillig * When a .for line is parsed, all following lines are accumulated into a
42 1.80 rillig * buffer, up to but excluding the corresponding .endfor line. To find the
43 1.80 rillig * corresponding .endfor, the number of nested .for and .endfor directives
44 1.80 rillig * are counted.
45 1.80 rillig *
46 1.80 rillig * During parsing, any nested .for loops are just passed through; they get
47 1.80 rillig * handled recursively in For_Eval when the enclosing .for loop is evaluated
48 1.80 rillig * in For_Run.
49 1.80 rillig *
50 1.80 rillig * When the .for loop has been parsed completely, the variable expressions
51 1.80 rillig * for the iteration variables are replaced with expressions of the form
52 1.80 rillig * ${:Uvalue}, and then this modified body is "included" as a special file.
53 1.1 cgd *
54 1.1 cgd * Interface:
55 1.88 rillig * For_Eval Evaluate the loop in the passed line.
56 1.88 rillig *
57 1.1 cgd * For_Run Run accumulated loop
58 1.1 cgd */
59 1.1 cgd
60 1.1 cgd #include "make.h"
61 1.1 cgd
62 1.83 rillig /* "@(#)for.c 8.1 (Berkeley) 6/6/93" */
63 1.100 rillig MAKE_RCSID("$NetBSD: for.c,v 1.100 2020/10/25 14:58:23 rillig Exp $");
64 1.83 rillig
65 1.94 rillig typedef enum ForEscapes {
66 1.75 rillig FOR_SUB_ESCAPE_CHAR = 0x0001,
67 1.75 rillig FOR_SUB_ESCAPE_BRACE = 0x0002,
68 1.75 rillig FOR_SUB_ESCAPE_PAREN = 0x0004
69 1.75 rillig } ForEscapes;
70 1.42 dsl
71 1.63 rillig static int forLevel = 0; /* Nesting level */
72 1.1 cgd
73 1.96 rillig /* One of the variables to the left of the "in" in a .for loop. */
74 1.96 rillig typedef struct ForVar {
75 1.96 rillig char *name;
76 1.96 rillig size_t len;
77 1.96 rillig } ForVar;
78 1.96 rillig
79 1.96 rillig /* One of the items to the right of the "in" in a .for loop. */
80 1.96 rillig typedef struct ForItem {
81 1.100 rillig char *value;
82 1.96 rillig } ForItem;
83 1.96 rillig
84 1.1 cgd /*
85 1.1 cgd * State of a for loop.
86 1.1 cgd */
87 1.86 rillig typedef struct For {
88 1.63 rillig Buffer buf; /* Body of loop */
89 1.96 rillig Vector /* of ForVar */ vars;
90 1.96 rillig Vector /* of ForItem */ items; /* Substitution items */
91 1.63 rillig char *parse_buf;
92 1.69 rillig /* Is any of the names 1 character long? If so, when the variable values
93 1.69 rillig * are substituted, the parser must handle $V expressions as well, not
94 1.69 rillig * only ${V} and $(V). */
95 1.69 rillig Boolean short_var;
96 1.93 rillig unsigned int sub_next;
97 1.2 jtc } For;
98 1.1 cgd
99 1.63 rillig static For *accumFor; /* Loop being accumulated */
100 1.1 cgd
101 1.96 rillig static void
102 1.96 rillig ForAddVar(For *f, const char *name, size_t len)
103 1.96 rillig {
104 1.96 rillig ForVar *var = Vector_Push(&f->vars);
105 1.96 rillig var->name = bmake_strldup(name, len);
106 1.96 rillig var->len = len;
107 1.96 rillig }
108 1.96 rillig
109 1.96 rillig static void
110 1.96 rillig ForVarDone(ForVar *var)
111 1.96 rillig {
112 1.96 rillig free(var->name);
113 1.96 rillig }
114 1.96 rillig
115 1.96 rillig static void
116 1.100 rillig ForAddItem(For *f, const char *value)
117 1.96 rillig {
118 1.96 rillig ForItem *item = Vector_Push(&f->items);
119 1.96 rillig item->value = bmake_strdup(value);
120 1.96 rillig }
121 1.96 rillig
122 1.96 rillig static void
123 1.96 rillig ForItemDone(ForItem *item)
124 1.96 rillig {
125 1.96 rillig free(item->value);
126 1.96 rillig }
127 1.7 christos
128 1.43 dsl static void
129 1.43 dsl For_Free(For *arg)
130 1.43 dsl {
131 1.46 dsl Buf_Destroy(&arg->buf, TRUE);
132 1.96 rillig
133 1.96 rillig while (arg->vars.len > 0)
134 1.96 rillig ForVarDone(Vector_Pop(&arg->vars));
135 1.96 rillig Vector_Done(&arg->vars);
136 1.96 rillig
137 1.96 rillig while (arg->items.len > 0)
138 1.96 rillig ForItemDone(Vector_Pop(&arg->items));
139 1.96 rillig Vector_Done(&arg->items);
140 1.96 rillig
141 1.43 dsl free(arg->parse_buf);
142 1.43 dsl
143 1.43 dsl free(arg);
144 1.43 dsl }
145 1.43 dsl
146 1.99 rillig static ForEscapes
147 1.99 rillig GetEscapes(const char *word)
148 1.99 rillig {
149 1.99 rillig const char *p;
150 1.99 rillig ForEscapes escapes = 0;
151 1.99 rillig
152 1.99 rillig for (p = word; *p != '\0'; p++) {
153 1.99 rillig switch (*p) {
154 1.99 rillig case ':':
155 1.99 rillig case '$':
156 1.99 rillig case '\\':
157 1.99 rillig escapes |= FOR_SUB_ESCAPE_CHAR;
158 1.99 rillig break;
159 1.99 rillig case ')':
160 1.99 rillig escapes |= FOR_SUB_ESCAPE_PAREN;
161 1.99 rillig break;
162 1.99 rillig case '}':
163 1.99 rillig escapes |= FOR_SUB_ESCAPE_BRACE;
164 1.99 rillig break;
165 1.99 rillig }
166 1.99 rillig }
167 1.99 rillig return escapes;
168 1.99 rillig }
169 1.99 rillig
170 1.70 rillig /* Evaluate the for loop in the passed line. The line looks like this:
171 1.70 rillig * .for <varname...> in <value...>
172 1.1 cgd *
173 1.13 wiz * Input:
174 1.13 wiz * line Line to parse
175 1.13 wiz *
176 1.1 cgd * Results:
177 1.35 dsl * 0: Not a .for statement, parse the line
178 1.35 dsl * 1: We found a for loop
179 1.35 dsl * -1: A .for statement with a bad syntax error, discard.
180 1.1 cgd */
181 1.1 cgd int
182 1.73 rillig For_Eval(const char *line)
183 1.1 cgd {
184 1.43 dsl For *new_for;
185 1.72 rillig const char *ptr;
186 1.67 rillig Words words;
187 1.33 dsl
188 1.43 dsl /* Skip the '.' and any following whitespace */
189 1.92 rillig ptr = line + 1;
190 1.92 rillig cpp_skip_whitespace(&ptr);
191 1.43 dsl
192 1.32 dsl /*
193 1.32 dsl * If we are not in a for loop quickly determine if the statement is
194 1.32 dsl * a for.
195 1.32 dsl */
196 1.32 dsl if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
197 1.79 rillig !ch_isspace(ptr[3])) {
198 1.63 rillig if (ptr[0] == 'e' && strncmp(ptr + 1, "ndfor", 5) == 0) {
199 1.32 dsl Parse_Error(PARSE_FATAL, "for-less endfor");
200 1.32 dsl return -1;
201 1.32 dsl }
202 1.32 dsl return 0;
203 1.32 dsl }
204 1.32 dsl ptr += 3;
205 1.1 cgd
206 1.32 dsl /*
207 1.32 dsl * we found a for loop, and now we are going to parse it.
208 1.32 dsl */
209 1.1 cgd
210 1.43 dsl new_for = bmake_malloc(sizeof *new_for);
211 1.71 rillig Buf_Init(&new_for->buf, 0);
212 1.96 rillig Vector_Init(&new_for->vars, sizeof(ForVar));
213 1.96 rillig Vector_Init(&new_for->items, sizeof(ForItem));
214 1.71 rillig new_for->parse_buf = NULL;
215 1.71 rillig new_for->short_var = FALSE;
216 1.71 rillig new_for->sub_next = 0;
217 1.43 dsl
218 1.33 dsl /* Grab the variables. Terminate on "in". */
219 1.93 rillig for (;;) {
220 1.72 rillig size_t len;
221 1.72 rillig
222 1.92 rillig cpp_skip_whitespace(&ptr);
223 1.33 dsl if (*ptr == '\0') {
224 1.33 dsl Parse_Error(PARSE_FATAL, "missing `in' in for");
225 1.43 dsl For_Free(new_for);
226 1.33 dsl return -1;
227 1.33 dsl }
228 1.72 rillig
229 1.79 rillig for (len = 1; ptr[len] && !ch_isspace(ptr[len]); len++)
230 1.33 dsl continue;
231 1.33 dsl if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
232 1.33 dsl ptr += 2;
233 1.33 dsl break;
234 1.33 dsl }
235 1.43 dsl if (len == 1)
236 1.69 rillig new_for->short_var = TRUE;
237 1.69 rillig
238 1.96 rillig ForAddVar(new_for, ptr, len);
239 1.72 rillig ptr += len;
240 1.32 dsl }
241 1.1 cgd
242 1.96 rillig if (new_for->vars.len == 0) {
243 1.32 dsl Parse_Error(PARSE_FATAL, "no iteration variables in for");
244 1.43 dsl For_Free(new_for);
245 1.32 dsl return -1;
246 1.32 dsl }
247 1.4 christos
248 1.92 rillig cpp_skip_whitespace(&ptr);
249 1.32 dsl
250 1.32 dsl /*
251 1.70 rillig * Make a list with the remaining words.
252 1.70 rillig * The values are later substituted as ${:U<value>...} so we must
253 1.70 rillig * backslash-escape characters that break that syntax.
254 1.44 dsl * Variables are fully expanded - so it is safe for escape $.
255 1.44 dsl * We can't do the escapes here - because we don't know whether
256 1.70 rillig * we will be substituting into ${...} or $(...).
257 1.32 dsl */
258 1.72 rillig {
259 1.85 rillig char *items;
260 1.85 rillig (void)Var_Subst(ptr, VAR_GLOBAL, VARE_WANTRES, &items);
261 1.85 rillig /* TODO: handle errors */
262 1.72 rillig words = Str_Words(items, FALSE);
263 1.72 rillig free(items);
264 1.72 rillig }
265 1.54 rillig
266 1.67 rillig {
267 1.99 rillig size_t i;
268 1.65 rillig
269 1.99 rillig for (i = 0; i < words.len; i++) {
270 1.99 rillig const char *word = words.words[i];
271 1.72 rillig
272 1.99 rillig if (word[0] == '\0')
273 1.99 rillig continue; /* .for var in ${:U} */
274 1.99 rillig
275 1.100 rillig ForAddItem(new_for, word);
276 1.39 dsl }
277 1.72 rillig }
278 1.72 rillig
279 1.72 rillig Words_Free(words);
280 1.1 cgd
281 1.72 rillig {
282 1.95 rillig size_t nitems, nvars;
283 1.7 christos
284 1.96 rillig if ((nitems = new_for->items.len) > 0 &&
285 1.96 rillig nitems % (nvars = new_for->vars.len)) {
286 1.49 sjg Parse_Error(PARSE_FATAL,
287 1.65 rillig "Wrong number of words (%zu) in .for substitution list"
288 1.95 rillig " with %zu vars", nitems, nvars);
289 1.49 sjg /*
290 1.49 sjg * Return 'success' so that the body of the .for loop is
291 1.49 sjg * accumulated.
292 1.49 sjg * Remove all items so that the loop doesn't iterate.
293 1.49 sjg */
294 1.96 rillig while (new_for->items.len > 0)
295 1.96 rillig ForItemDone(Vector_Pop(&new_for->items));
296 1.49 sjg }
297 1.32 dsl }
298 1.32 dsl
299 1.43 dsl accumFor = new_for;
300 1.32 dsl forLevel = 1;
301 1.32 dsl return 1;
302 1.32 dsl }
303 1.7 christos
304 1.35 dsl /*
305 1.35 dsl * Add another line to a .for loop.
306 1.81 rillig * Returns FALSE when the matching .endfor is reached.
307 1.35 dsl */
308 1.81 rillig Boolean
309 1.73 rillig For_Accum(const char *line)
310 1.32 dsl {
311 1.73 rillig const char *ptr = line;
312 1.26 dsl
313 1.26 dsl if (*ptr == '.') {
314 1.92 rillig ptr++;
315 1.92 rillig cpp_skip_whitespace(&ptr);
316 1.1 cgd
317 1.79 rillig if (strncmp(ptr, "endfor", 6) == 0 && (ch_isspace(ptr[6]) || !ptr[6])) {
318 1.89 rillig DEBUG1(FOR, "For: end for %d\n", forLevel);
319 1.32 dsl if (--forLevel <= 0)
320 1.81 rillig return FALSE;
321 1.79 rillig } else if (strncmp(ptr, "for", 3) == 0 && ch_isspace(ptr[3])) {
322 1.1 cgd forLevel++;
323 1.89 rillig DEBUG1(FOR, "For: new loop %d\n", forLevel);
324 1.1 cgd }
325 1.1 cgd }
326 1.1 cgd
327 1.59 rillig Buf_AddStr(&accumFor->buf, line);
328 1.46 dsl Buf_AddByte(&accumFor->buf, '\n');
329 1.81 rillig return TRUE;
330 1.1 cgd }
331 1.1 cgd
332 1.42 dsl
333 1.59 rillig static size_t
334 1.45 dsl for_var_len(const char *var)
335 1.45 dsl {
336 1.45 dsl char ch, var_start, var_end;
337 1.61 rillig int depth;
338 1.61 rillig size_t len;
339 1.45 dsl
340 1.45 dsl var_start = *var;
341 1.45 dsl if (var_start == 0)
342 1.45 dsl /* just escape the $ */
343 1.45 dsl return 0;
344 1.45 dsl
345 1.45 dsl if (var_start == '(')
346 1.45 dsl var_end = ')';
347 1.45 dsl else if (var_start == '{')
348 1.45 dsl var_end = '}';
349 1.45 dsl else
350 1.45 dsl /* Single char variable */
351 1.45 dsl return 1;
352 1.45 dsl
353 1.61 rillig depth = 1;
354 1.45 dsl for (len = 1; (ch = var[len++]) != 0;) {
355 1.45 dsl if (ch == var_start)
356 1.45 dsl depth++;
357 1.45 dsl else if (ch == var_end && --depth == 0)
358 1.45 dsl return len;
359 1.45 dsl }
360 1.45 dsl
361 1.45 dsl /* Variable end not found, escape the $ */
362 1.45 dsl return 0;
363 1.45 dsl }
364 1.45 dsl
365 1.42 dsl static void
366 1.96 rillig for_substitute(Buffer *cmds, ForItem *forItem, char ech)
367 1.42 dsl {
368 1.96 rillig const char *item = forItem->value;
369 1.100 rillig ForEscapes escapes = GetEscapes(item);
370 1.61 rillig char ch;
371 1.61 rillig
372 1.42 dsl /* If there were no escapes, or the only escape is the other variable
373 1.42 dsl * terminator, then just substitute the full string */
374 1.93 rillig if (!(escapes & (ech == ')' ? ~(unsigned)FOR_SUB_ESCAPE_BRACE
375 1.93 rillig : ~(unsigned)FOR_SUB_ESCAPE_PAREN))) {
376 1.59 rillig Buf_AddStr(cmds, item);
377 1.42 dsl return;
378 1.42 dsl }
379 1.42 dsl
380 1.74 rillig /* Escape ':', '$', '\\' and 'ech' - these will be removed later by
381 1.74 rillig * :U processing, see ApplyModifier_Defined. */
382 1.95 rillig while ((ch = *item++) != '\0') {
383 1.45 dsl if (ch == '$') {
384 1.59 rillig size_t len = for_var_len(item);
385 1.45 dsl if (len != 0) {
386 1.62 rillig Buf_AddBytes(cmds, item - 1, len + 1);
387 1.45 dsl item += len;
388 1.45 dsl continue;
389 1.45 dsl }
390 1.45 dsl Buf_AddByte(cmds, '\\');
391 1.45 dsl } else if (ch == ':' || ch == '\\' || ch == ech)
392 1.42 dsl Buf_AddByte(cmds, '\\');
393 1.42 dsl Buf_AddByte(cmds, ch);
394 1.42 dsl }
395 1.42 dsl }
396 1.42 dsl
397 1.97 rillig static void
398 1.97 rillig SubstVarLong(For *arg, const char **inout_cp, const char **inout_cmd_cp,
399 1.97 rillig Buffer *cmds, char ech)
400 1.97 rillig {
401 1.97 rillig size_t i;
402 1.97 rillig const char *cp = *inout_cp;
403 1.97 rillig const char *cmd_cp = *inout_cmd_cp;
404 1.97 rillig
405 1.97 rillig for (i = 0; i < arg->vars.len; i++) {
406 1.97 rillig ForVar *forVar = Vector_Get(&arg->vars, i);
407 1.97 rillig char *var = forVar->name;
408 1.97 rillig size_t vlen = forVar->len;
409 1.97 rillig
410 1.97 rillig /* XXX: undefined behavior for cp if vlen is longer than cp? */
411 1.97 rillig if (memcmp(cp, var, vlen) != 0)
412 1.97 rillig continue;
413 1.97 rillig /* XXX: why test for backslash here? */
414 1.97 rillig if (cp[vlen] != ':' && cp[vlen] != ech && cp[vlen] != '\\')
415 1.97 rillig continue;
416 1.97 rillig
417 1.97 rillig /* Found a variable match. Replace with :U<value> */
418 1.97 rillig Buf_AddBytesBetween(cmds, cmd_cp, cp);
419 1.97 rillig Buf_AddStr(cmds, ":U");
420 1.97 rillig cp += vlen;
421 1.97 rillig cmd_cp = cp;
422 1.97 rillig for_substitute(cmds, Vector_Get(&arg->items, arg->sub_next + i), ech);
423 1.97 rillig break;
424 1.97 rillig }
425 1.97 rillig
426 1.97 rillig *inout_cp = cp;
427 1.97 rillig *inout_cmd_cp = cmd_cp;
428 1.97 rillig }
429 1.97 rillig
430 1.98 rillig static void
431 1.98 rillig SubstVarShort(For *arg, char const ch,
432 1.98 rillig const char **inout_cp, const char **input_cmd_cp, Buffer *cmds)
433 1.98 rillig {
434 1.98 rillig const char *cp = *inout_cp;
435 1.98 rillig const char *cmd_cp = *input_cmd_cp;
436 1.98 rillig size_t i;
437 1.98 rillig
438 1.98 rillig /* Probably a single character name, ignore $$ and stupid ones. {*/
439 1.98 rillig if (!arg->short_var || strchr("}):$", ch) != NULL) {
440 1.98 rillig cp++;
441 1.98 rillig *inout_cp = cp;
442 1.98 rillig return;
443 1.98 rillig }
444 1.98 rillig
445 1.98 rillig for (i = 0; i < arg->vars.len; i++) {
446 1.98 rillig ForVar *forVar = Vector_Get(&arg->vars, i);
447 1.98 rillig char *var = forVar->name;
448 1.98 rillig if (var[0] != ch || var[1] != 0)
449 1.98 rillig continue;
450 1.98 rillig
451 1.98 rillig /* Found a variable match. Replace with ${:U<value>} */
452 1.98 rillig Buf_AddBytesBetween(cmds, cmd_cp, cp);
453 1.98 rillig Buf_AddStr(cmds, "{:U");
454 1.98 rillig cmd_cp = ++cp;
455 1.98 rillig for_substitute(cmds, Vector_Get(&arg->items, arg->sub_next + i), '}');
456 1.98 rillig Buf_AddByte(cmds, '}');
457 1.98 rillig break;
458 1.98 rillig }
459 1.98 rillig
460 1.98 rillig *inout_cp = cp;
461 1.98 rillig *input_cmd_cp = cmd_cp;
462 1.98 rillig }
463 1.98 rillig
464 1.95 rillig /*
465 1.95 rillig * Scan the for loop body and replace references to the loop variables
466 1.95 rillig * with variable references that expand to the required text.
467 1.95 rillig *
468 1.95 rillig * Using variable expansions ensures that the .for loop can't generate
469 1.95 rillig * syntax, and that the later parsing will still see a variable.
470 1.95 rillig * We assume that the null variable will never be defined.
471 1.95 rillig *
472 1.95 rillig * The detection of substitutions of the loop control variable is naive.
473 1.95 rillig * Many of the modifiers use \ to escape $ (not $) so it is possible
474 1.95 rillig * to contrive a makefile where an unwanted substitution happens.
475 1.95 rillig */
476 1.43 dsl static char *
477 1.68 rillig ForIterate(void *v_arg, size_t *ret_len)
478 1.1 cgd {
479 1.43 dsl For *arg = v_arg;
480 1.77 rillig const char *cp;
481 1.77 rillig const char *cmd_cp;
482 1.77 rillig const char *body_end;
483 1.39 dsl Buffer cmds;
484 1.76 rillig char *cmds_str;
485 1.61 rillig size_t cmd_len;
486 1.7 christos
487 1.96 rillig if (arg->sub_next + arg->vars.len > arg->items.len) {
488 1.43 dsl /* No more iterations */
489 1.43 dsl For_Free(arg);
490 1.43 dsl return NULL;
491 1.43 dsl }
492 1.1 cgd
493 1.43 dsl free(arg->parse_buf);
494 1.43 dsl arg->parse_buf = NULL;
495 1.7 christos
496 1.62 rillig cmd_cp = Buf_GetAll(&arg->buf, &cmd_len);
497 1.59 rillig body_end = cmd_cp + cmd_len;
498 1.62 rillig Buf_Init(&cmds, cmd_len + 256);
499 1.43 dsl for (cp = cmd_cp; (cp = strchr(cp, '$')) != NULL;) {
500 1.95 rillig char ch, ech;
501 1.43 dsl ch = *++cp;
502 1.53 riastrad if ((ch == '(' && (ech = ')', 1)) || (ch == '{' && (ech = '}', 1))) {
503 1.43 dsl cp++;
504 1.43 dsl /* Check variable name against the .for loop variables */
505 1.97 rillig SubstVarLong(arg, &cp, &cmd_cp, &cmds, ech);
506 1.43 dsl continue;
507 1.43 dsl }
508 1.95 rillig if (ch == '\0')
509 1.43 dsl break;
510 1.97 rillig
511 1.98 rillig SubstVarShort(arg, ch, &cp, &cmd_cp, &cmds);
512 1.43 dsl }
513 1.59 rillig Buf_AddBytesBetween(&cmds, cmd_cp, body_end);
514 1.43 dsl
515 1.87 rillig *ret_len = Buf_Len(&cmds);
516 1.76 rillig cmds_str = Buf_Destroy(&cmds, FALSE);
517 1.89 rillig DEBUG1(FOR, "For: loop body:\n%s", cmds_str);
518 1.39 dsl
519 1.96 rillig arg->sub_next += arg->vars.len;
520 1.43 dsl
521 1.76 rillig arg->parse_buf = cmds_str;
522 1.76 rillig return cmds_str;
523 1.43 dsl }
524 1.7 christos
525 1.60 rillig /* Run the for loop, imitating the actions of an include file. */
526 1.43 dsl void
527 1.43 dsl For_Run(int lineno)
528 1.54 rillig {
529 1.43 dsl For *arg;
530 1.54 rillig
531 1.43 dsl arg = accumFor;
532 1.43 dsl accumFor = NULL;
533 1.1 cgd
534 1.96 rillig if (arg->items.len == 0) {
535 1.58 rillig /* Nothing to expand - possibly due to an earlier syntax error. */
536 1.58 rillig For_Free(arg);
537 1.58 rillig return;
538 1.43 dsl }
539 1.54 rillig
540 1.68 rillig Parse_SetInput(NULL, lineno, -1, ForIterate, arg);
541 1.1 cgd }
542