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