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