for.c revision 1.87 1 1.87 rillig /* $NetBSD: for.c,v 1.87 2020/09/27 16:52:22 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.1 cgd * For_Eval Evaluate the loop in the passed line.
56 1.1 cgd * For_Run Run accumulated loop
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include "make.h"
60 1.38 dsl #include "strlist.h"
61 1.1 cgd
62 1.83 rillig /* "@(#)for.c 8.1 (Berkeley) 6/6/93" */
63 1.87 rillig MAKE_RCSID("$NetBSD: for.c,v 1.87 2020/09/27 16:52:22 rillig Exp $");
64 1.83 rillig
65 1.75 rillig typedef enum {
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.1 cgd /*
74 1.1 cgd * State of a for loop.
75 1.1 cgd */
76 1.86 rillig typedef struct For {
77 1.63 rillig Buffer buf; /* Body of loop */
78 1.63 rillig strlist_t vars; /* Iteration variables */
79 1.63 rillig strlist_t items; /* Substitution items */
80 1.63 rillig char *parse_buf;
81 1.69 rillig /* Is any of the names 1 character long? If so, when the variable values
82 1.69 rillig * are substituted, the parser must handle $V expressions as well, not
83 1.69 rillig * only ${V} and $(V). */
84 1.69 rillig Boolean short_var;
85 1.63 rillig int sub_next;
86 1.2 jtc } For;
87 1.1 cgd
88 1.63 rillig static For *accumFor; /* Loop being accumulated */
89 1.1 cgd
90 1.7 christos
91 1.43 dsl static void
92 1.43 dsl For_Free(For *arg)
93 1.43 dsl {
94 1.46 dsl Buf_Destroy(&arg->buf, TRUE);
95 1.43 dsl strlist_clean(&arg->vars);
96 1.43 dsl strlist_clean(&arg->items);
97 1.43 dsl free(arg->parse_buf);
98 1.43 dsl
99 1.43 dsl free(arg);
100 1.43 dsl }
101 1.43 dsl
102 1.70 rillig /* Evaluate the for loop in the passed line. The line looks like this:
103 1.70 rillig * .for <varname...> in <value...>
104 1.1 cgd *
105 1.13 wiz * Input:
106 1.13 wiz * line Line to parse
107 1.13 wiz *
108 1.1 cgd * Results:
109 1.35 dsl * 0: Not a .for statement, parse the line
110 1.35 dsl * 1: We found a for loop
111 1.35 dsl * -1: A .for statement with a bad syntax error, discard.
112 1.1 cgd */
113 1.1 cgd int
114 1.73 rillig For_Eval(const char *line)
115 1.1 cgd {
116 1.43 dsl For *new_for;
117 1.72 rillig const char *ptr;
118 1.67 rillig Words words;
119 1.33 dsl
120 1.43 dsl /* Skip the '.' and any following whitespace */
121 1.79 rillig for (ptr = line + 1; ch_isspace(*ptr); ptr++)
122 1.32 dsl continue;
123 1.43 dsl
124 1.32 dsl /*
125 1.32 dsl * If we are not in a for loop quickly determine if the statement is
126 1.32 dsl * a for.
127 1.32 dsl */
128 1.32 dsl if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
129 1.79 rillig !ch_isspace(ptr[3])) {
130 1.63 rillig if (ptr[0] == 'e' && strncmp(ptr + 1, "ndfor", 5) == 0) {
131 1.32 dsl Parse_Error(PARSE_FATAL, "for-less endfor");
132 1.32 dsl return -1;
133 1.32 dsl }
134 1.32 dsl return 0;
135 1.32 dsl }
136 1.32 dsl ptr += 3;
137 1.1 cgd
138 1.32 dsl /*
139 1.32 dsl * we found a for loop, and now we are going to parse it.
140 1.32 dsl */
141 1.1 cgd
142 1.43 dsl new_for = bmake_malloc(sizeof *new_for);
143 1.71 rillig Buf_Init(&new_for->buf, 0);
144 1.71 rillig strlist_init(&new_for->vars);
145 1.71 rillig strlist_init(&new_for->items);
146 1.71 rillig new_for->parse_buf = NULL;
147 1.71 rillig new_for->short_var = FALSE;
148 1.71 rillig new_for->sub_next = 0;
149 1.43 dsl
150 1.33 dsl /* Grab the variables. Terminate on "in". */
151 1.72 rillig while (TRUE) {
152 1.72 rillig size_t len;
153 1.72 rillig
154 1.79 rillig while (ch_isspace(*ptr))
155 1.32 dsl ptr++;
156 1.33 dsl if (*ptr == '\0') {
157 1.33 dsl Parse_Error(PARSE_FATAL, "missing `in' in for");
158 1.43 dsl For_Free(new_for);
159 1.33 dsl return -1;
160 1.33 dsl }
161 1.72 rillig
162 1.79 rillig for (len = 1; ptr[len] && !ch_isspace(ptr[len]); len++)
163 1.33 dsl continue;
164 1.33 dsl if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
165 1.33 dsl ptr += 2;
166 1.33 dsl break;
167 1.33 dsl }
168 1.43 dsl if (len == 1)
169 1.69 rillig new_for->short_var = TRUE;
170 1.69 rillig
171 1.66 rillig strlist_add_str(&new_for->vars, bmake_strldup(ptr, len), len);
172 1.72 rillig ptr += len;
173 1.32 dsl }
174 1.1 cgd
175 1.43 dsl if (strlist_num(&new_for->vars) == 0) {
176 1.32 dsl Parse_Error(PARSE_FATAL, "no iteration variables in for");
177 1.43 dsl For_Free(new_for);
178 1.32 dsl return -1;
179 1.32 dsl }
180 1.4 christos
181 1.79 rillig while (ch_isspace(*ptr))
182 1.32 dsl ptr++;
183 1.32 dsl
184 1.32 dsl /*
185 1.70 rillig * Make a list with the remaining words.
186 1.70 rillig * The values are later substituted as ${:U<value>...} so we must
187 1.70 rillig * backslash-escape characters that break that syntax.
188 1.44 dsl * Variables are fully expanded - so it is safe for escape $.
189 1.44 dsl * We can't do the escapes here - because we don't know whether
190 1.70 rillig * we will be substituting into ${...} or $(...).
191 1.32 dsl */
192 1.72 rillig {
193 1.85 rillig char *items;
194 1.85 rillig (void)Var_Subst(ptr, VAR_GLOBAL, VARE_WANTRES, &items);
195 1.85 rillig /* TODO: handle errors */
196 1.72 rillig words = Str_Words(items, FALSE);
197 1.72 rillig free(items);
198 1.72 rillig }
199 1.54 rillig
200 1.67 rillig {
201 1.72 rillig size_t n;
202 1.65 rillig
203 1.67 rillig for (n = 0; n < words.len; n++) {
204 1.75 rillig ForEscapes escapes;
205 1.72 rillig char ch;
206 1.72 rillig
207 1.67 rillig ptr = words.words[n];
208 1.72 rillig if (ptr[0] == '\0')
209 1.49 sjg continue;
210 1.49 sjg escapes = 0;
211 1.49 sjg while ((ch = *ptr++)) {
212 1.63 rillig switch (ch) {
213 1.49 sjg case ':':
214 1.49 sjg case '$':
215 1.49 sjg case '\\':
216 1.49 sjg escapes |= FOR_SUB_ESCAPE_CHAR;
217 1.49 sjg break;
218 1.49 sjg case ')':
219 1.49 sjg escapes |= FOR_SUB_ESCAPE_PAREN;
220 1.49 sjg break;
221 1.72 rillig case '}':
222 1.49 sjg escapes |= FOR_SUB_ESCAPE_BRACE;
223 1.49 sjg break;
224 1.49 sjg }
225 1.49 sjg }
226 1.49 sjg /*
227 1.49 sjg * We have to dup words[n] to maintain the semantics of
228 1.49 sjg * strlist.
229 1.49 sjg */
230 1.67 rillig strlist_add_str(&new_for->items, bmake_strdup(words.words[n]),
231 1.67 rillig escapes);
232 1.39 dsl }
233 1.72 rillig }
234 1.72 rillig
235 1.72 rillig Words_Free(words);
236 1.1 cgd
237 1.72 rillig {
238 1.82 rillig size_t len, n;
239 1.7 christos
240 1.49 sjg if ((len = strlist_num(&new_for->items)) > 0 &&
241 1.49 sjg len % (n = strlist_num(&new_for->vars))) {
242 1.49 sjg Parse_Error(PARSE_FATAL,
243 1.65 rillig "Wrong number of words (%zu) in .for substitution list"
244 1.65 rillig " with %zu vars", len, n);
245 1.49 sjg /*
246 1.49 sjg * Return 'success' so that the body of the .for loop is
247 1.49 sjg * accumulated.
248 1.49 sjg * Remove all items so that the loop doesn't iterate.
249 1.49 sjg */
250 1.49 sjg strlist_clean(&new_for->items);
251 1.49 sjg }
252 1.32 dsl }
253 1.32 dsl
254 1.43 dsl accumFor = new_for;
255 1.32 dsl forLevel = 1;
256 1.32 dsl return 1;
257 1.32 dsl }
258 1.7 christos
259 1.35 dsl /*
260 1.35 dsl * Add another line to a .for loop.
261 1.81 rillig * Returns FALSE when the matching .endfor is reached.
262 1.35 dsl */
263 1.81 rillig Boolean
264 1.73 rillig For_Accum(const char *line)
265 1.32 dsl {
266 1.73 rillig const char *ptr = line;
267 1.26 dsl
268 1.26 dsl if (*ptr == '.') {
269 1.1 cgd
270 1.79 rillig for (ptr++; *ptr && ch_isspace(*ptr); ptr++)
271 1.1 cgd continue;
272 1.1 cgd
273 1.79 rillig if (strncmp(ptr, "endfor", 6) == 0 && (ch_isspace(ptr[6]) || !ptr[6])) {
274 1.1 cgd if (DEBUG(FOR))
275 1.23 dsl (void)fprintf(debug_file, "For: end for %d\n", forLevel);
276 1.32 dsl if (--forLevel <= 0)
277 1.81 rillig return FALSE;
278 1.79 rillig } else if (strncmp(ptr, "for", 3) == 0 && ch_isspace(ptr[3])) {
279 1.1 cgd forLevel++;
280 1.1 cgd if (DEBUG(FOR))
281 1.23 dsl (void)fprintf(debug_file, "For: new loop %d\n", forLevel);
282 1.1 cgd }
283 1.1 cgd }
284 1.1 cgd
285 1.59 rillig Buf_AddStr(&accumFor->buf, line);
286 1.46 dsl Buf_AddByte(&accumFor->buf, '\n');
287 1.81 rillig return TRUE;
288 1.1 cgd }
289 1.1 cgd
290 1.42 dsl
291 1.59 rillig static size_t
292 1.45 dsl for_var_len(const char *var)
293 1.45 dsl {
294 1.45 dsl char ch, var_start, var_end;
295 1.61 rillig int depth;
296 1.61 rillig size_t len;
297 1.45 dsl
298 1.45 dsl var_start = *var;
299 1.45 dsl if (var_start == 0)
300 1.45 dsl /* just escape the $ */
301 1.45 dsl return 0;
302 1.45 dsl
303 1.45 dsl if (var_start == '(')
304 1.45 dsl var_end = ')';
305 1.45 dsl else if (var_start == '{')
306 1.45 dsl var_end = '}';
307 1.45 dsl else
308 1.45 dsl /* Single char variable */
309 1.45 dsl return 1;
310 1.45 dsl
311 1.61 rillig depth = 1;
312 1.45 dsl for (len = 1; (ch = var[len++]) != 0;) {
313 1.45 dsl if (ch == var_start)
314 1.45 dsl depth++;
315 1.45 dsl else if (ch == var_end && --depth == 0)
316 1.45 dsl return len;
317 1.45 dsl }
318 1.45 dsl
319 1.45 dsl /* Variable end not found, escape the $ */
320 1.45 dsl return 0;
321 1.45 dsl }
322 1.45 dsl
323 1.42 dsl static void
324 1.46 dsl for_substitute(Buffer *cmds, strlist_t *items, unsigned int item_no, char ech)
325 1.42 dsl {
326 1.74 rillig const char *item = strlist_str(items, item_no);
327 1.75 rillig ForEscapes escapes = strlist_info(items, item_no);
328 1.61 rillig char ch;
329 1.61 rillig
330 1.42 dsl /* If there were no escapes, or the only escape is the other variable
331 1.42 dsl * terminator, then just substitute the full string */
332 1.74 rillig if (!(escapes &
333 1.63 rillig (ech == ')' ? ~FOR_SUB_ESCAPE_BRACE : ~FOR_SUB_ESCAPE_PAREN))) {
334 1.59 rillig Buf_AddStr(cmds, item);
335 1.42 dsl return;
336 1.42 dsl }
337 1.42 dsl
338 1.74 rillig /* Escape ':', '$', '\\' and 'ech' - these will be removed later by
339 1.74 rillig * :U processing, see ApplyModifier_Defined. */
340 1.45 dsl while ((ch = *item++) != 0) {
341 1.45 dsl if (ch == '$') {
342 1.59 rillig size_t len = for_var_len(item);
343 1.45 dsl if (len != 0) {
344 1.62 rillig Buf_AddBytes(cmds, item - 1, len + 1);
345 1.45 dsl item += len;
346 1.45 dsl continue;
347 1.45 dsl }
348 1.45 dsl Buf_AddByte(cmds, '\\');
349 1.45 dsl } else if (ch == ':' || ch == '\\' || ch == ech)
350 1.42 dsl Buf_AddByte(cmds, '\\');
351 1.42 dsl Buf_AddByte(cmds, ch);
352 1.42 dsl }
353 1.42 dsl }
354 1.42 dsl
355 1.43 dsl static char *
356 1.68 rillig ForIterate(void *v_arg, size_t *ret_len)
357 1.1 cgd {
358 1.43 dsl For *arg = v_arg;
359 1.59 rillig int i;
360 1.42 dsl char *var;
361 1.77 rillig const char *cp;
362 1.77 rillig const char *cmd_cp;
363 1.77 rillig const char *body_end;
364 1.39 dsl char ch;
365 1.39 dsl Buffer cmds;
366 1.76 rillig char *cmds_str;
367 1.61 rillig size_t cmd_len;
368 1.7 christos
369 1.43 dsl if (arg->sub_next + strlist_num(&arg->vars) > strlist_num(&arg->items)) {
370 1.43 dsl /* No more iterations */
371 1.43 dsl For_Free(arg);
372 1.43 dsl return NULL;
373 1.43 dsl }
374 1.1 cgd
375 1.43 dsl free(arg->parse_buf);
376 1.43 dsl arg->parse_buf = NULL;
377 1.7 christos
378 1.39 dsl /*
379 1.39 dsl * Scan the for loop body and replace references to the loop variables
380 1.39 dsl * with variable references that expand to the required text.
381 1.39 dsl * Using variable expansions ensures that the .for loop can't generate
382 1.39 dsl * syntax, and that the later parsing will still see a variable.
383 1.39 dsl * We assume that the null variable will never be defined.
384 1.39 dsl *
385 1.74 rillig * The detection of substitutions of the loop control variable is naive.
386 1.39 dsl * Many of the modifiers use \ to escape $ (not $) so it is possible
387 1.39 dsl * to contrive a makefile where an unwanted substitution happens.
388 1.39 dsl */
389 1.43 dsl
390 1.62 rillig cmd_cp = Buf_GetAll(&arg->buf, &cmd_len);
391 1.59 rillig body_end = cmd_cp + cmd_len;
392 1.62 rillig Buf_Init(&cmds, cmd_len + 256);
393 1.43 dsl for (cp = cmd_cp; (cp = strchr(cp, '$')) != NULL;) {
394 1.43 dsl char ech;
395 1.43 dsl ch = *++cp;
396 1.53 riastrad if ((ch == '(' && (ech = ')', 1)) || (ch == '{' && (ech = '}', 1))) {
397 1.43 dsl cp++;
398 1.43 dsl /* Check variable name against the .for loop variables */
399 1.43 dsl STRLIST_FOREACH(var, &arg->vars, i) {
400 1.59 rillig size_t vlen = strlist_info(&arg->vars, i);
401 1.59 rillig if (memcmp(cp, var, vlen) != 0)
402 1.43 dsl continue;
403 1.59 rillig if (cp[vlen] != ':' && cp[vlen] != ech && cp[vlen] != '\\')
404 1.39 dsl continue;
405 1.43 dsl /* Found a variable match. Replace with :U<value> */
406 1.59 rillig Buf_AddBytesBetween(&cmds, cmd_cp, cp);
407 1.59 rillig Buf_AddStr(&cmds, ":U");
408 1.59 rillig cp += vlen;
409 1.43 dsl cmd_cp = cp;
410 1.46 dsl for_substitute(&cmds, &arg->items, arg->sub_next + i, ech);
411 1.39 dsl break;
412 1.39 dsl }
413 1.43 dsl continue;
414 1.43 dsl }
415 1.43 dsl if (ch == 0)
416 1.43 dsl break;
417 1.43 dsl /* Probably a single character name, ignore $$ and stupid ones. {*/
418 1.43 dsl if (!arg->short_var || strchr("}):$", ch) != NULL) {
419 1.43 dsl cp++;
420 1.43 dsl continue;
421 1.43 dsl }
422 1.43 dsl STRLIST_FOREACH(var, &arg->vars, i) {
423 1.43 dsl if (var[0] != ch || var[1] != 0)
424 1.43 dsl continue;
425 1.43 dsl /* Found a variable match. Replace with ${:U<value>} */
426 1.59 rillig Buf_AddBytesBetween(&cmds, cmd_cp, cp);
427 1.59 rillig Buf_AddStr(&cmds, "{:U");
428 1.43 dsl cmd_cp = ++cp;
429 1.84 rillig for_substitute(&cmds, &arg->items, arg->sub_next + i, '}');
430 1.59 rillig Buf_AddByte(&cmds, '}');
431 1.43 dsl break;
432 1.39 dsl }
433 1.43 dsl }
434 1.59 rillig Buf_AddBytesBetween(&cmds, cmd_cp, body_end);
435 1.43 dsl
436 1.87 rillig *ret_len = Buf_Len(&cmds);
437 1.76 rillig cmds_str = Buf_Destroy(&cmds, FALSE);
438 1.43 dsl if (DEBUG(FOR))
439 1.76 rillig (void)fprintf(debug_file, "For: loop body:\n%s", cmds_str);
440 1.39 dsl
441 1.43 dsl arg->sub_next += strlist_num(&arg->vars);
442 1.43 dsl
443 1.76 rillig arg->parse_buf = cmds_str;
444 1.76 rillig return cmds_str;
445 1.43 dsl }
446 1.7 christos
447 1.60 rillig /* Run the for loop, imitating the actions of an include file. */
448 1.43 dsl void
449 1.43 dsl For_Run(int lineno)
450 1.54 rillig {
451 1.43 dsl For *arg;
452 1.54 rillig
453 1.43 dsl arg = accumFor;
454 1.43 dsl accumFor = NULL;
455 1.1 cgd
456 1.43 dsl if (strlist_num(&arg->items) == 0) {
457 1.58 rillig /* Nothing to expand - possibly due to an earlier syntax error. */
458 1.58 rillig For_Free(arg);
459 1.58 rillig return;
460 1.43 dsl }
461 1.54 rillig
462 1.68 rillig Parse_SetInput(NULL, lineno, -1, ForIterate, arg);
463 1.1 cgd }
464