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