1 /* $NetBSD: for.c,v 1.141 2021/02/04 21:33:13 rillig Exp $ */
4 * Copyright (c) 1992, The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Handling of .for/.endfor loops in a makefile.
35 * For loops have the form:
37 * .for <varname...> in <value...>
41 * When a .for line is parsed, the following lines are copied to the body of
42 * the .for loop, until the corresponding .endfor line is reached. In this
43 * phase, the body is not yet evaluated. This also applies to any nested
46 * After reaching the .endfor, the values from the .for line are grouped
47 * according to the number of variables. For each such group, the unexpanded
48 * body is scanned for variable expressions, and those that match the variable
49 * names are replaced with expressions of the form ${:U...} or $(:U...).
50 * After that, the body is treated like a file from an .include directive.
53 * For_Eval Evaluate the loop in the passed line.
55 * For_Run Run accumulated loop
60 /* "@(#)for.c 8.1 (Berkeley) 6/6/93" */
61 MAKE_RCSID("$NetBSD: for.c,v 1.141 2021/02/04 21:33:13 rillig Exp $");
64 /* One of the variables to the left of the "in" in a .for loop. */
65 typedef struct ForVar {
70 typedef struct ForLoop {
71 Buffer body; /* Unexpanded body of the loop */
72 Vector /* of ForVar */ vars; /* Iteration variables */
73 Words items; /* Substitution items */
74 Buffer curBody; /* Expanded body of the current iteration */
75 /* Is any of the names 1 character long? If so, when the variable values
76 * are substituted, the parser must handle $V expressions as well, not
77 * only ${V} and $(V). */
79 unsigned int sub_next; /* Where to continue iterating */
83 static ForLoop *accumFor; /* Loop being accumulated */
84 static int forLevel = 0; /* Nesting level */
90 ForLoop *f = bmake_malloc(sizeof *f);
93 Vector_Init(&f->vars, sizeof(ForVar));
94 f->items.words = NULL;
95 f->items.freeIt = NULL;
96 Buf_Init(&f->curBody);
104 ForLoop_Free(ForLoop *f)
108 while (f->vars.len > 0) {
109 ForVar *var = Vector_Pop(&f->vars);
112 Vector_Done(&f->vars);
114 Words_Free(f->items);
115 Buf_Done(&f->curBody);
121 ForLoop_AddVar(ForLoop *f, const char *name, size_t len)
123 ForVar *var = Vector_Push(&f->vars);
124 var->name = bmake_strldup(name, len);
129 ForLoop_ParseVarnames(ForLoop *f, const char **pp)
136 cpp_skip_whitespace(&p);
138 Parse_Error(PARSE_FATAL, "missing `in' in for");
143 * XXX: This allows arbitrary variable names;
144 * see directive-for.mk.
146 for (len = 1; p[len] != '\0' && !ch_isspace(p[len]); len++)
149 if (len == 2 && p[0] == 'i' && p[1] == 'n') {
156 ForLoop_AddVar(f, p, len);
160 if (f->vars.len == 0) {
161 Parse_Error(PARSE_FATAL, "no iteration variables in for");
170 ForLoop_ParseItems(ForLoop *f, const char *p)
174 cpp_skip_whitespace(&p);
176 if (Var_Subst(p, SCOPE_GLOBAL, VARE_WANTRES, &items) != VPR_OK) {
177 Parse_Error(PARSE_FATAL, "Error in .for loop items");
181 f->items = Str_Words(items, FALSE);
184 if (f->items.len == 1 && f->items.words[0][0] == '\0')
185 f->items.len = 0; /* .for var in ${:U} */
187 if (f->items.len != 0 && f->items.len % f->vars.len != 0) {
188 Parse_Error(PARSE_FATAL,
189 "Wrong number of words (%u) in .for "
190 "substitution list with %u variables",
191 (unsigned)f->items.len, (unsigned)f->vars.len);
201 return p[0] == 'f' && p[1] == 'o' && p[2] == 'r' && ch_isspace(p[3]);
205 IsEndfor(const char *p)
207 return p[0] == 'e' && strncmp(p, "endfor", 6) == 0 &&
208 (p[6] == '\0' || ch_isspace(p[6]));
212 * Evaluate the for loop in the passed line. The line looks like this:
213 * .for <varname...> in <value...>
219 * 0: Not a .for statement, parse the line
220 * 1: We found a for loop
221 * -1: A .for statement with a bad syntax error, discard.
224 For_Eval(const char *line)
229 p = line + 1; /* skip the '.' */
230 cpp_skip_whitespace(&p);
234 Parse_Error(PARSE_FATAL, "for-less endfor");
243 if (!ForLoop_ParseVarnames(f, &p)) {
248 if (!ForLoop_ParseItems(f, p)) {
249 /* Continue parsing the .for loop, but don't iterate. */
259 * Add another line to the .for loop that is being built up.
260 * Returns FALSE when the matching .endfor is reached.
263 For_Accum(const char *line)
265 const char *p = line;
269 cpp_skip_whitespace(&p);
272 DEBUG1(FOR, "For: end for %d\n", forLevel);
275 } else if (IsFor(p)) {
277 DEBUG1(FOR, "For: new loop %d\n", forLevel);
281 Buf_AddStr(&accumFor->body, line);
282 Buf_AddByte(&accumFor->body, '\n');
288 for_var_len(const char *var)
290 char ch, var_start, var_end;
295 if (var_start == '\0')
296 /* just escape the $ */
299 if (var_start == '(')
301 else if (var_start == '{')
304 return 1; /* Single char variable */
307 for (len = 1; (ch = var[len++]) != '\0';) {
310 else if (ch == var_end && --depth == 0)
314 /* Variable end not found, escape the $ */
319 * The .for loop substitutes the items as ${:U<value>...}, which means
320 * that characters that break this syntax must be backslash-escaped.
323 NeedsEscapes(const char *value, char endc)
327 for (p = value; *p != '\0'; p++) {
328 if (*p == ':' || *p == '$' || *p == '\\' || *p == endc)
335 * While expanding the body of a .for loop, write the item in the ${:U...}
336 * expression, escaping characters as needed.
338 * The result is later unescaped by ApplyModifier_Defined.
341 Buf_AddEscaped(Buffer *cmds, const char *item, char endc)
345 if (!NeedsEscapes(item, endc)) {
346 Buf_AddStr(cmds, item);
350 /* Escape ':', '$', '\\' and 'endc' - these will be removed later by
351 * :U processing, see ApplyModifier_Defined. */
352 while ((ch = *item++) != '\0') {
354 size_t len = for_var_len(item);
356 Buf_AddBytes(cmds, item - 1, len + 1);
360 Buf_AddByte(cmds, '\\');
361 } else if (ch == ':' || ch == '\\' || ch == endc)
362 Buf_AddByte(cmds, '\\');
363 Buf_AddByte(cmds, ch);
368 * While expanding the body of a .for loop, replace the variable name of an
369 * expression like ${i} or ${i:...} or $(i) or $(i:...) with ":Uvalue".
372 ForLoop_SubstVarLong(ForLoop *f, const char **pp, const char *bodyEnd,
373 char endc, const char **inout_mark)
378 for (i = 0; i < f->vars.len; i++) {
379 ForVar *forVar = Vector_Get(&f->vars, i);
380 char *varname = forVar->name;
381 size_t varnameLen = forVar->nameLen;
383 if (varnameLen >= (size_t)(bodyEnd - p))
385 if (memcmp(p, varname, varnameLen) != 0)
387 /* XXX: why test for backslash here? */
388 if (p[varnameLen] != ':' && p[varnameLen] != endc &&
389 p[varnameLen] != '\\')
393 * Found a variable match. Skip over the variable name and
394 * instead add ':U<value>' to the current body.
396 Buf_AddBytesBetween(&f->curBody, *inout_mark, p);
397 Buf_AddStr(&f->curBody, ":U");
398 Buf_AddEscaped(&f->curBody,
399 f->items.words[f->sub_next + i], endc);
409 * While expanding the body of a .for loop, replace single-character
410 * variable expressions like $i with their ${:U...} expansion.
413 ForLoop_SubstVarShort(ForLoop *f, const char *p, const char **inout_mark)
419 /* Skip $$ and stupid ones. */
420 if (!f->short_var || strchr("}):$", ch) != NULL)
423 vars = Vector_Get(&f->vars, 0);
424 for (i = 0; i < f->vars.len; i++) {
425 const char *varname = vars[i].name;
426 if (varname[0] == ch && varname[1] == '\0')
432 /* Replace $<ch> with ${:U<value>} */
433 Buf_AddBytesBetween(&f->curBody, *inout_mark, p), *inout_mark = p + 1;
434 Buf_AddStr(&f->curBody, "{:U");
435 Buf_AddEscaped(&f->curBody, f->items.words[f->sub_next + i], '}');
436 Buf_AddByte(&f->curBody, '}');
440 * Compute the body for the current iteration by copying the unexpanded body,
441 * replacing the expressions for the iteration variables on the way.
443 * Using variable expressions ensures that the .for loop can't generate
444 * syntax, and that the later parsing will still see a variable.
445 * This code assumes that the variable with the empty name will never be
446 * defined, see unit-tests/varname-empty.mk for more details.
448 * The detection of substitutions of the loop control variables is naive.
449 * Many of the modifiers use '\' to escape '$' (not '$'), so it is possible
450 * to contrive a makefile where an unwanted substitution happens.
453 ForLoop_SubstBody(ForLoop *f)
455 const char *p, *bodyEnd;
456 const char *mark; /* where the last replacement left off */
458 Buf_Empty(&f->curBody);
461 bodyEnd = f->body.data + f->body.len;
462 for (p = mark; (p = strchr(p, '$')) != NULL;) {
463 if (p[1] == '{' || p[1] == '(') {
465 ForLoop_SubstVarLong(f, &p, bodyEnd,
466 p[-1] == '{' ? '}' : ')', &mark);
467 } else if (p[1] != '\0') {
468 ForLoop_SubstVarShort(f, p + 1, &mark);
474 Buf_AddBytesBetween(&f->curBody, mark, bodyEnd);
478 * Compute the body for the current iteration by copying the unexpanded body,
479 * replacing the expressions for the iteration variables on the way.
482 ForReadMore(void *v_arg, size_t *out_len)
486 if (f->sub_next == f->items.len) {
487 /* No more iterations */
492 ForLoop_SubstBody(f);
493 DEBUG1(FOR, "For: loop body:\n%s", f->curBody.data);
494 f->sub_next += (unsigned int)f->vars.len;
496 *out_len = f->curBody.len;
497 return f->curBody.data;
500 /* Run the .for loop, imitating the actions of an include file. */
504 ForLoop *f = accumFor;
507 if (f->items.len == 0) {
509 * Nothing to expand - possibly due to an earlier syntax
516 Parse_SetInput(NULL, lineno, -1, ForReadMore, f);