xref: /freebsd/contrib/bmake/for.c (revision c59c3bf34db360695f07735bebc76a768cac5afc)
1*c59c3bf3SSimon J. Gerraty /*	$NetBSD: for.c,v 1.178 2024/01/21 15:02:17 rillig Exp $	*/
23955d011SMarcel Moolenaar 
33955d011SMarcel Moolenaar /*
43955d011SMarcel Moolenaar  * Copyright (c) 1992, The Regents of the University of California.
53955d011SMarcel Moolenaar  * All rights reserved.
63955d011SMarcel Moolenaar  *
73955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
83955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
93955d011SMarcel Moolenaar  * are met:
103955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
113955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
123955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
133955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
143955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
153955d011SMarcel Moolenaar  * 3. Neither the name of the University nor the names of its contributors
163955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
173955d011SMarcel Moolenaar  *    without specific prior written permission.
183955d011SMarcel Moolenaar  *
193955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
203955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
213955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
223955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
233955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
243955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
253955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
283955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
293955d011SMarcel Moolenaar  * SUCH DAMAGE.
303955d011SMarcel Moolenaar  */
313955d011SMarcel Moolenaar 
32dba7b0efSSimon J. Gerraty /*
33956e45f6SSimon J. Gerraty  * Handling of .for/.endfor loops in a makefile.
343955d011SMarcel Moolenaar  *
3506b9b3e0SSimon J. Gerraty  * For loops have the form:
363955d011SMarcel Moolenaar  *
37956e45f6SSimon J. Gerraty  *	.for <varname...> in <value...>
3806b9b3e0SSimon J. Gerraty  *	# the body
393955d011SMarcel Moolenaar  *	.endfor
403955d011SMarcel Moolenaar  *
4106b9b3e0SSimon J. Gerraty  * When a .for line is parsed, the following lines are copied to the body of
4206b9b3e0SSimon J. Gerraty  * the .for loop, until the corresponding .endfor line is reached.  In this
4306b9b3e0SSimon J. Gerraty  * phase, the body is not yet evaluated.  This also applies to any nested
4406b9b3e0SSimon J. Gerraty  * .for loops.
453955d011SMarcel Moolenaar  *
4606b9b3e0SSimon J. Gerraty  * After reaching the .endfor, the values from the .for line are grouped
4706b9b3e0SSimon J. Gerraty  * according to the number of variables.  For each such group, the unexpanded
48d5e0a182SSimon J. Gerraty  * body is scanned for expressions, and those that match the
4912904384SSimon J. Gerraty  * variable names are replaced with expressions of the form ${:U...}.  After
5012904384SSimon J. Gerraty  * that, the body is treated like a file from an .include directive.
51956e45f6SSimon J. Gerraty  *
52956e45f6SSimon J. Gerraty  * Interface:
53956e45f6SSimon J. Gerraty  *	For_Eval	Evaluate the loop in the passed line.
54956e45f6SSimon J. Gerraty  *
55956e45f6SSimon J. Gerraty  *	For_Run		Run accumulated loop
563955d011SMarcel Moolenaar  */
573955d011SMarcel Moolenaar 
58956e45f6SSimon J. Gerraty #include "make.h"
59956e45f6SSimon J. Gerraty 
60956e45f6SSimon J. Gerraty /*	"@(#)for.c	8.1 (Berkeley) 6/6/93"	*/
61*c59c3bf3SSimon J. Gerraty MAKE_RCSID("$NetBSD: for.c,v 1.178 2024/01/21 15:02:17 rillig Exp $");
62956e45f6SSimon J. Gerraty 
633955d011SMarcel Moolenaar 
64dba7b0efSSimon J. Gerraty typedef struct ForLoop {
659f45a3c8SSimon J. Gerraty 	Vector /* of 'char *' */ vars; /* Iteration variables */
6612904384SSimon J. Gerraty 	SubstringWords items;	/* Substitution items */
679f45a3c8SSimon J. Gerraty 	Buffer body;		/* Unexpanded body of the loop */
6812904384SSimon J. Gerraty 	unsigned int nextItem;	/* Where to continue iterating */
69dba7b0efSSimon J. Gerraty } ForLoop;
703955d011SMarcel Moolenaar 
713955d011SMarcel Moolenaar 
72dba7b0efSSimon J. Gerraty static ForLoop *accumFor;	/* Loop being accumulated */
73dba7b0efSSimon J. Gerraty 
74dba7b0efSSimon J. Gerraty 
75c1d01b5fSSimon J. Gerraty /* See LK_FOR_BODY. */
76c1d01b5fSSimon J. Gerraty static void
77c1d01b5fSSimon J. Gerraty skip_whitespace_or_line_continuation(const char **pp)
78c1d01b5fSSimon J. Gerraty {
79c1d01b5fSSimon J. Gerraty 	const char *p = *pp;
80c1d01b5fSSimon J. Gerraty 	for (;;) {
81c1d01b5fSSimon J. Gerraty 		if (ch_isspace(*p))
82c1d01b5fSSimon J. Gerraty 			p++;
83c1d01b5fSSimon J. Gerraty 		else if (p[0] == '\\' && p[1] == '\n')
84c1d01b5fSSimon J. Gerraty 			p += 2;
85c1d01b5fSSimon J. Gerraty 		else
86c1d01b5fSSimon J. Gerraty 			break;
87c1d01b5fSSimon J. Gerraty 	}
88c1d01b5fSSimon J. Gerraty 	*pp = p;
89c1d01b5fSSimon J. Gerraty }
90c1d01b5fSSimon J. Gerraty 
91dba7b0efSSimon J. Gerraty static ForLoop *
92dba7b0efSSimon J. Gerraty ForLoop_New(void)
93956e45f6SSimon J. Gerraty {
94dba7b0efSSimon J. Gerraty 	ForLoop *f = bmake_malloc(sizeof *f);
95dba7b0efSSimon J. Gerraty 
969f45a3c8SSimon J. Gerraty 	Vector_Init(&f->vars, sizeof(char *));
9712904384SSimon J. Gerraty 	SubstringWords_Init(&f->items);
989f45a3c8SSimon J. Gerraty 	Buf_Init(&f->body);
9912904384SSimon J. Gerraty 	f->nextItem = 0;
100dba7b0efSSimon J. Gerraty 
101dba7b0efSSimon J. Gerraty 	return f;
102956e45f6SSimon J. Gerraty }
1033955d011SMarcel Moolenaar 
1049f45a3c8SSimon J. Gerraty void
105dba7b0efSSimon J. Gerraty ForLoop_Free(ForLoop *f)
1063955d011SMarcel Moolenaar {
1079f45a3c8SSimon J. Gerraty 	while (f->vars.len > 0)
1089f45a3c8SSimon J. Gerraty 		free(*(char **)Vector_Pop(&f->vars));
109956e45f6SSimon J. Gerraty 	Vector_Done(&f->vars);
110956e45f6SSimon J. Gerraty 
11112904384SSimon J. Gerraty 	SubstringWords_Free(f->items);
1129f45a3c8SSimon J. Gerraty 	Buf_Done(&f->body);
113956e45f6SSimon J. Gerraty 
114956e45f6SSimon J. Gerraty 	free(f);
1153955d011SMarcel Moolenaar }
1163955d011SMarcel Moolenaar 
1179f45a3c8SSimon J. Gerraty char *
118148ee845SSimon J. Gerraty ForLoop_Details(const ForLoop *f)
119dba7b0efSSimon J. Gerraty {
1209f45a3c8SSimon J. Gerraty 	size_t i, n;
1219f45a3c8SSimon J. Gerraty 	const char **vars;
1229f45a3c8SSimon J. Gerraty 	const Substring *items;
1239f45a3c8SSimon J. Gerraty 	Buffer buf;
1249f45a3c8SSimon J. Gerraty 
1259f45a3c8SSimon J. Gerraty 	n = f->vars.len;
1269f45a3c8SSimon J. Gerraty 	vars = f->vars.items;
1279f45a3c8SSimon J. Gerraty 	assert(f->nextItem >= n);
1289f45a3c8SSimon J. Gerraty 	items = f->items.words + f->nextItem - n;
1299f45a3c8SSimon J. Gerraty 
1309f45a3c8SSimon J. Gerraty 	Buf_Init(&buf);
1319f45a3c8SSimon J. Gerraty 	for (i = 0; i < n; i++) {
1329f45a3c8SSimon J. Gerraty 		if (i > 0)
1339f45a3c8SSimon J. Gerraty 			Buf_AddStr(&buf, ", ");
1349f45a3c8SSimon J. Gerraty 		Buf_AddStr(&buf, vars[i]);
1359f45a3c8SSimon J. Gerraty 		Buf_AddStr(&buf, " = ");
136148ee845SSimon J. Gerraty 		Buf_AddRange(&buf, items[i].start, items[i].end);
1379f45a3c8SSimon J. Gerraty 	}
1389f45a3c8SSimon J. Gerraty 	return Buf_DoneData(&buf);
139dba7b0efSSimon J. Gerraty }
140dba7b0efSSimon J. Gerraty 
141b0c40a00SSimon J. Gerraty static bool
142c1d01b5fSSimon J. Gerraty IsValidInVarname(char c)
143c1d01b5fSSimon J. Gerraty {
144c1d01b5fSSimon J. Gerraty 	return c != '$' && c != ':' && c != '\\' &&
145c1d01b5fSSimon J. Gerraty 	    c != '(' && c != '{' && c != ')' && c != '}';
146c1d01b5fSSimon J. Gerraty }
147c1d01b5fSSimon J. Gerraty 
148c1d01b5fSSimon J. Gerraty static void
149dba7b0efSSimon J. Gerraty ForLoop_ParseVarnames(ForLoop *f, const char **pp)
150dba7b0efSSimon J. Gerraty {
151dba7b0efSSimon J. Gerraty 	const char *p = *pp;
152dba7b0efSSimon J. Gerraty 
153dba7b0efSSimon J. Gerraty 	for (;;) {
154dba7b0efSSimon J. Gerraty 		size_t len;
155dba7b0efSSimon J. Gerraty 
156dba7b0efSSimon J. Gerraty 		cpp_skip_whitespace(&p);
157dba7b0efSSimon J. Gerraty 		if (*p == '\0') {
158dba7b0efSSimon J. Gerraty 			Parse_Error(PARSE_FATAL, "missing `in' in for");
159c1d01b5fSSimon J. Gerraty 			f->vars.len = 0;
160c1d01b5fSSimon J. Gerraty 			return;
161dba7b0efSSimon J. Gerraty 		}
162dba7b0efSSimon J. Gerraty 
163c1d01b5fSSimon J. Gerraty 		for (len = 0; p[len] != '\0' && !ch_isspace(p[len]); len++) {
164c1d01b5fSSimon J. Gerraty 			if (!IsValidInVarname(p[len])) {
165c1d01b5fSSimon J. Gerraty 				Parse_Error(PARSE_FATAL,
166c1d01b5fSSimon J. Gerraty 				    "invalid character '%c' "
167c1d01b5fSSimon J. Gerraty 				    "in .for loop variable name",
168c1d01b5fSSimon J. Gerraty 				    p[len]);
169c1d01b5fSSimon J. Gerraty 				f->vars.len = 0;
170c1d01b5fSSimon J. Gerraty 				return;
171c1d01b5fSSimon J. Gerraty 			}
172c1d01b5fSSimon J. Gerraty 		}
173dba7b0efSSimon J. Gerraty 
174dba7b0efSSimon J. Gerraty 		if (len == 2 && p[0] == 'i' && p[1] == 'n') {
175dba7b0efSSimon J. Gerraty 			p += 2;
176dba7b0efSSimon J. Gerraty 			break;
177dba7b0efSSimon J. Gerraty 		}
178dba7b0efSSimon J. Gerraty 
1799f45a3c8SSimon J. Gerraty 		*(char **)Vector_Push(&f->vars) = bmake_strldup(p, len);
180dba7b0efSSimon J. Gerraty 		p += len;
181dba7b0efSSimon J. Gerraty 	}
182dba7b0efSSimon J. Gerraty 
183dba7b0efSSimon J. Gerraty 	if (f->vars.len == 0) {
184dba7b0efSSimon J. Gerraty 		Parse_Error(PARSE_FATAL, "no iteration variables in for");
185c1d01b5fSSimon J. Gerraty 		return;
186dba7b0efSSimon J. Gerraty 	}
187dba7b0efSSimon J. Gerraty 
188dba7b0efSSimon J. Gerraty 	*pp = p;
189dba7b0efSSimon J. Gerraty }
190dba7b0efSSimon J. Gerraty 
191b0c40a00SSimon J. Gerraty static bool
192dba7b0efSSimon J. Gerraty ForLoop_ParseItems(ForLoop *f, const char *p)
193dba7b0efSSimon J. Gerraty {
194dba7b0efSSimon J. Gerraty 	char *items;
195dba7b0efSSimon J. Gerraty 
196dba7b0efSSimon J. Gerraty 	cpp_skip_whitespace(&p);
197dba7b0efSSimon J. Gerraty 
1988c973ee2SSimon J. Gerraty 	items = Var_Subst(p, SCOPE_GLOBAL, VARE_WANTRES);
1998c973ee2SSimon J. Gerraty 	if (items == var_Error) {
2008c973ee2SSimon J. Gerraty 		/* TODO: Make this part of the code reachable. */
201dba7b0efSSimon J. Gerraty 		Parse_Error(PARSE_FATAL, "Error in .for loop items");
202b0c40a00SSimon J. Gerraty 		return false;
203dba7b0efSSimon J. Gerraty 	}
204dba7b0efSSimon J. Gerraty 
20512904384SSimon J. Gerraty 	f->items = Substring_Words(items, false);
206dba7b0efSSimon J. Gerraty 	free(items);
207dba7b0efSSimon J. Gerraty 
20812904384SSimon J. Gerraty 	if (f->items.len == 1 && Substring_IsEmpty(f->items.words[0]))
209dba7b0efSSimon J. Gerraty 		f->items.len = 0;	/* .for var in ${:U} */
210dba7b0efSSimon J. Gerraty 
2119f45a3c8SSimon J. Gerraty 	if (f->items.len % f->vars.len != 0) {
212dba7b0efSSimon J. Gerraty 		Parse_Error(PARSE_FATAL,
213dba7b0efSSimon J. Gerraty 		    "Wrong number of words (%u) in .for "
214dba7b0efSSimon J. Gerraty 		    "substitution list with %u variables",
215dba7b0efSSimon J. Gerraty 		    (unsigned)f->items.len, (unsigned)f->vars.len);
216b0c40a00SSimon J. Gerraty 		return false;
217dba7b0efSSimon J. Gerraty 	}
218dba7b0efSSimon J. Gerraty 
219b0c40a00SSimon J. Gerraty 	return true;
220dba7b0efSSimon J. Gerraty }
221dba7b0efSSimon J. Gerraty 
222b0c40a00SSimon J. Gerraty static bool
223956e45f6SSimon J. Gerraty IsFor(const char *p)
224956e45f6SSimon J. Gerraty {
225956e45f6SSimon J. Gerraty 	return p[0] == 'f' && p[1] == 'o' && p[2] == 'r' && ch_isspace(p[3]);
226956e45f6SSimon J. Gerraty }
2273955d011SMarcel Moolenaar 
228b0c40a00SSimon J. Gerraty static bool
229956e45f6SSimon J. Gerraty IsEndfor(const char *p)
230956e45f6SSimon J. Gerraty {
231956e45f6SSimon J. Gerraty 	return p[0] == 'e' && strncmp(p, "endfor", 6) == 0 &&
232956e45f6SSimon J. Gerraty 	       (p[6] == '\0' || ch_isspace(p[6]));
233956e45f6SSimon J. Gerraty }
234956e45f6SSimon J. Gerraty 
23506b9b3e0SSimon J. Gerraty /*
23606b9b3e0SSimon J. Gerraty  * Evaluate the for loop in the passed line. The line looks like this:
237956e45f6SSimon J. Gerraty  *	.for <varname...> in <value...>
238956e45f6SSimon J. Gerraty  *
239956e45f6SSimon J. Gerraty  * Results:
2409f45a3c8SSimon J. Gerraty  *	0	not a .for directive
2419f45a3c8SSimon J. Gerraty  *	1	found a .for directive
2429f45a3c8SSimon J. Gerraty  *	-1	erroneous .for directive
243956e45f6SSimon J. Gerraty  */
244956e45f6SSimon J. Gerraty int
245956e45f6SSimon J. Gerraty For_Eval(const char *line)
246956e45f6SSimon J. Gerraty {
247956e45f6SSimon J. Gerraty 	const char *p;
2489f45a3c8SSimon J. Gerraty 	ForLoop *f;
249956e45f6SSimon J. Gerraty 
250956e45f6SSimon J. Gerraty 	p = line + 1;		/* skip the '.' */
251c1d01b5fSSimon J. Gerraty 	skip_whitespace_or_line_continuation(&p);
252956e45f6SSimon J. Gerraty 
2539f45a3c8SSimon J. Gerraty 	if (IsFor(p)) {
254956e45f6SSimon J. Gerraty 		p += 3;
255956e45f6SSimon J. Gerraty 
256dba7b0efSSimon J. Gerraty 		f = ForLoop_New();
257c1d01b5fSSimon J. Gerraty 		ForLoop_ParseVarnames(f, &p);
258c1d01b5fSSimon J. Gerraty 		if (f->vars.len > 0 && !ForLoop_ParseItems(f, p))
2599f45a3c8SSimon J. Gerraty 			f->items.len = 0;	/* don't iterate */
2603955d011SMarcel Moolenaar 
261956e45f6SSimon J. Gerraty 		accumFor = f;
2623955d011SMarcel Moolenaar 		return 1;
2639f45a3c8SSimon J. Gerraty 	} else if (IsEndfor(p)) {
2649f45a3c8SSimon J. Gerraty 		Parse_Error(PARSE_FATAL, "for-less endfor");
2659f45a3c8SSimon J. Gerraty 		return -1;
2669f45a3c8SSimon J. Gerraty 	} else
2679f45a3c8SSimon J. Gerraty 		return 0;
2683955d011SMarcel Moolenaar }
2693955d011SMarcel Moolenaar 
2703955d011SMarcel Moolenaar /*
271dba7b0efSSimon J. Gerraty  * Add another line to the .for loop that is being built up.
272b0c40a00SSimon J. Gerraty  * Returns false when the matching .endfor is reached.
2733955d011SMarcel Moolenaar  */
274b0c40a00SSimon J. Gerraty bool
2759f45a3c8SSimon J. Gerraty For_Accum(const char *line, int *forLevel)
2763955d011SMarcel Moolenaar {
277dba7b0efSSimon J. Gerraty 	const char *p = line;
2783955d011SMarcel Moolenaar 
279dba7b0efSSimon J. Gerraty 	if (*p == '.') {
280dba7b0efSSimon J. Gerraty 		p++;
281c1d01b5fSSimon J. Gerraty 		skip_whitespace_or_line_continuation(&p);
2823955d011SMarcel Moolenaar 
283dba7b0efSSimon J. Gerraty 		if (IsEndfor(p)) {
2849f45a3c8SSimon J. Gerraty 			DEBUG1(FOR, "For: end for %d\n", *forLevel);
2859f45a3c8SSimon J. Gerraty 			if (--*forLevel == 0)
286b0c40a00SSimon J. Gerraty 				return false;
287dba7b0efSSimon J. Gerraty 		} else if (IsFor(p)) {
2889f45a3c8SSimon J. Gerraty 			(*forLevel)++;
2899f45a3c8SSimon J. Gerraty 			DEBUG1(FOR, "For: new loop %d\n", *forLevel);
2903955d011SMarcel Moolenaar 		}
2913955d011SMarcel Moolenaar 	}
2923955d011SMarcel Moolenaar 
293956e45f6SSimon J. Gerraty 	Buf_AddStr(&accumFor->body, line);
294956e45f6SSimon J. Gerraty 	Buf_AddByte(&accumFor->body, '\n');
295b0c40a00SSimon J. Gerraty 	return true;
2963955d011SMarcel Moolenaar }
2973955d011SMarcel Moolenaar 
298954401e6SSimon J. Gerraty /*
299954401e6SSimon J. Gerraty  * When the body of a '.for i' loop is prepared for an iteration, each
300954401e6SSimon J. Gerraty  * occurrence of $i in the body is replaced with ${:U...}, inserting the
301d5e0a182SSimon J. Gerraty  * value of the item.  If this item contains a '$', it may be the start of an
302d5e0a182SSimon J. Gerraty  * expression.  This expression is copied verbatim, its length is
303954401e6SSimon J. Gerraty  * determined here, in a rather naive way, ignoring escape characters and
304954401e6SSimon J. Gerraty  * funny delimiters in modifiers like ':S}from}to}'.
305954401e6SSimon J. Gerraty  */
3062c3632d1SSimon J. Gerraty static size_t
30712904384SSimon J. Gerraty ExprLen(const char *s, const char *e)
3083955d011SMarcel Moolenaar {
30912904384SSimon J. Gerraty 	char expr_open, expr_close;
3103955d011SMarcel Moolenaar 	int depth;
31112904384SSimon J. Gerraty 	const char *p;
3123955d011SMarcel Moolenaar 
31312904384SSimon J. Gerraty 	if (s == e)
31412904384SSimon J. Gerraty 		return 0;	/* just escape the '$' */
3153955d011SMarcel Moolenaar 
31612904384SSimon J. Gerraty 	expr_open = s[0];
31712904384SSimon J. Gerraty 	if (expr_open == '(')
31812904384SSimon J. Gerraty 		expr_close = ')';
31912904384SSimon J. Gerraty 	else if (expr_open == '{')
32012904384SSimon J. Gerraty 		expr_close = '}';
3213955d011SMarcel Moolenaar 	else
32206b9b3e0SSimon J. Gerraty 		return 1;	/* Single char variable */
3233955d011SMarcel Moolenaar 
3243955d011SMarcel Moolenaar 	depth = 1;
32512904384SSimon J. Gerraty 	for (p = s + 1; p != e; p++) {
32612904384SSimon J. Gerraty 		if (*p == expr_open)
3273955d011SMarcel Moolenaar 			depth++;
32812904384SSimon J. Gerraty 		else if (*p == expr_close && --depth == 0)
32912904384SSimon J. Gerraty 			return (size_t)(p + 1 - s);
3303955d011SMarcel Moolenaar 	}
3313955d011SMarcel Moolenaar 
33212904384SSimon J. Gerraty 	/* Expression end not found, escape the $ */
3333955d011SMarcel Moolenaar 	return 0;
3343955d011SMarcel Moolenaar }
3353955d011SMarcel Moolenaar 
33606b9b3e0SSimon J. Gerraty /*
33706b9b3e0SSimon J. Gerraty  * The .for loop substitutes the items as ${:U<value>...}, which means
33806b9b3e0SSimon J. Gerraty  * that characters that break this syntax must be backslash-escaped.
33906b9b3e0SSimon J. Gerraty  */
340b0c40a00SSimon J. Gerraty static bool
34112904384SSimon J. Gerraty NeedsEscapes(Substring value, char endc)
342e2eeea75SSimon J. Gerraty {
343e2eeea75SSimon J. Gerraty 	const char *p;
344e2eeea75SSimon J. Gerraty 
34512904384SSimon J. Gerraty 	for (p = value.start; p != value.end; p++) {
34612904384SSimon J. Gerraty 		if (*p == ':' || *p == '$' || *p == '\\' || *p == endc ||
34712904384SSimon J. Gerraty 		    *p == '\n')
348b0c40a00SSimon J. Gerraty 			return true;
349e2eeea75SSimon J. Gerraty 	}
350b0c40a00SSimon J. Gerraty 	return false;
351e2eeea75SSimon J. Gerraty }
352e2eeea75SSimon J. Gerraty 
35306b9b3e0SSimon J. Gerraty /*
354148ee845SSimon J. Gerraty  * While expanding the body of a .for loop, write the item as a ${:U...}
3559f45a3c8SSimon J. Gerraty  * expression, escaping characters as needed.  The result is later unescaped
3569f45a3c8SSimon J. Gerraty  * by ApplyModifier_Defined.
35706b9b3e0SSimon J. Gerraty  */
3583955d011SMarcel Moolenaar static void
3599f45a3c8SSimon J. Gerraty AddEscaped(Buffer *cmds, Substring item, char endc)
3603955d011SMarcel Moolenaar {
36112904384SSimon J. Gerraty 	const char *p;
3623955d011SMarcel Moolenaar 	char ch;
3633955d011SMarcel Moolenaar 
36406b9b3e0SSimon J. Gerraty 	if (!NeedsEscapes(item, endc)) {
365148ee845SSimon J. Gerraty 		Buf_AddRange(cmds, item.start, item.end);
3663955d011SMarcel Moolenaar 		return;
3673955d011SMarcel Moolenaar 	}
3683955d011SMarcel Moolenaar 
3699f45a3c8SSimon J. Gerraty 	for (p = item.start; p != item.end;) {
37012904384SSimon J. Gerraty 		ch = *p;
3713955d011SMarcel Moolenaar 		if (ch == '$') {
37212904384SSimon J. Gerraty 			size_t len = ExprLen(p + 1, item.end);
3733955d011SMarcel Moolenaar 			if (len != 0) {
37412904384SSimon J. Gerraty 				/*
37512904384SSimon J. Gerraty 				 * XXX: Should a '\' be added here?
37612904384SSimon J. Gerraty 				 * See directive-for-escape.mk, ExprLen.
37712904384SSimon J. Gerraty 				 */
37812904384SSimon J. Gerraty 				Buf_AddBytes(cmds, p, 1 + len);
3799f45a3c8SSimon J. Gerraty 				p += 1 + len;
3803955d011SMarcel Moolenaar 				continue;
3813955d011SMarcel Moolenaar 			}
3823955d011SMarcel Moolenaar 			Buf_AddByte(cmds, '\\');
38306b9b3e0SSimon J. Gerraty 		} else if (ch == ':' || ch == '\\' || ch == endc)
3843955d011SMarcel Moolenaar 			Buf_AddByte(cmds, '\\');
38512904384SSimon J. Gerraty 		else if (ch == '\n') {
38612904384SSimon J. Gerraty 			Parse_Error(PARSE_FATAL, "newline in .for value");
38712904384SSimon J. Gerraty 			ch = ' ';	/* prevent newline injection */
38812904384SSimon J. Gerraty 		}
3893955d011SMarcel Moolenaar 		Buf_AddByte(cmds, ch);
3909f45a3c8SSimon J. Gerraty 		p++;
3913955d011SMarcel Moolenaar 	}
3923955d011SMarcel Moolenaar }
3933955d011SMarcel Moolenaar 
39406b9b3e0SSimon J. Gerraty /*
395148ee845SSimon J. Gerraty  * While expanding the body of a .for loop, replace the variable name of an
39606b9b3e0SSimon J. Gerraty  * expression like ${i} or ${i:...} or $(i) or $(i:...) with ":Uvalue".
39706b9b3e0SSimon J. Gerraty  */
398956e45f6SSimon J. Gerraty static void
3999f45a3c8SSimon J. Gerraty ForLoop_SubstVarLong(ForLoop *f, unsigned int firstItem, Buffer *body,
4009f45a3c8SSimon J. Gerraty 		     const char **pp, char endc, const char **inout_mark)
4013955d011SMarcel Moolenaar {
402956e45f6SSimon J. Gerraty 	size_t i;
4039f45a3c8SSimon J. Gerraty 	const char *start = *pp;
404148ee845SSimon J. Gerraty 	const char **varnames = Vector_Get(&f->vars, 0);
4053955d011SMarcel Moolenaar 
406956e45f6SSimon J. Gerraty 	for (i = 0; i < f->vars.len; i++) {
4079f45a3c8SSimon J. Gerraty 		const char *p = start;
408956e45f6SSimon J. Gerraty 
409148ee845SSimon J. Gerraty 		if (!cpp_skip_string(&p, varnames[i]))
410956e45f6SSimon J. Gerraty 			continue;
411956e45f6SSimon J. Gerraty 		/* XXX: why test for backslash here? */
4129f45a3c8SSimon J. Gerraty 		if (*p != ':' && *p != endc && *p != '\\')
413956e45f6SSimon J. Gerraty 			continue;
414956e45f6SSimon J. Gerraty 
41506b9b3e0SSimon J. Gerraty 		/*
41606b9b3e0SSimon J. Gerraty 		 * Found a variable match.  Skip over the variable name and
41706b9b3e0SSimon J. Gerraty 		 * instead add ':U<value>' to the current body.
41806b9b3e0SSimon J. Gerraty 		 */
419148ee845SSimon J. Gerraty 		Buf_AddRange(body, *inout_mark, start);
4209f45a3c8SSimon J. Gerraty 		Buf_AddStr(body, ":U");
4219f45a3c8SSimon J. Gerraty 		AddEscaped(body, f->items.words[firstItem + i], endc);
422956e45f6SSimon J. Gerraty 
423956e45f6SSimon J. Gerraty 		*inout_mark = p;
424956e45f6SSimon J. Gerraty 		*pp = p;
425956e45f6SSimon J. Gerraty 		return;
426956e45f6SSimon J. Gerraty 	}
427956e45f6SSimon J. Gerraty }
4283955d011SMarcel Moolenaar 
4293955d011SMarcel Moolenaar /*
430148ee845SSimon J. Gerraty  * While expanding the body of a .for loop, replace single-character
431d5e0a182SSimon J. Gerraty  * expressions like $i with their ${:U...} expansion.
43206b9b3e0SSimon J. Gerraty  */
43306b9b3e0SSimon J. Gerraty static void
4349f45a3c8SSimon J. Gerraty ForLoop_SubstVarShort(ForLoop *f, unsigned int firstItem, Buffer *body,
4359f45a3c8SSimon J. Gerraty 		      const char *p, const char **inout_mark)
43606b9b3e0SSimon J. Gerraty {
437*c59c3bf3SSimon J. Gerraty 	char ch = *p;
4389f45a3c8SSimon J. Gerraty 	const char **vars;
43906b9b3e0SSimon J. Gerraty 	size_t i;
44006b9b3e0SSimon J. Gerraty 
44106b9b3e0SSimon J. Gerraty 	/* Skip $$ and stupid ones. */
44212904384SSimon J. Gerraty 	if (ch == '}' || ch == ')' || ch == ':' || ch == '$')
44306b9b3e0SSimon J. Gerraty 		return;
44406b9b3e0SSimon J. Gerraty 
44506b9b3e0SSimon J. Gerraty 	vars = Vector_Get(&f->vars, 0);
44606b9b3e0SSimon J. Gerraty 	for (i = 0; i < f->vars.len; i++) {
4479f45a3c8SSimon J. Gerraty 		const char *varname = vars[i];
44806b9b3e0SSimon J. Gerraty 		if (varname[0] == ch && varname[1] == '\0')
44906b9b3e0SSimon J. Gerraty 			goto found;
45006b9b3e0SSimon J. Gerraty 	}
45106b9b3e0SSimon J. Gerraty 	return;
45206b9b3e0SSimon J. Gerraty 
45306b9b3e0SSimon J. Gerraty found:
454148ee845SSimon J. Gerraty 	Buf_AddRange(body, *inout_mark, p);
45512904384SSimon J. Gerraty 	*inout_mark = p + 1;
45612904384SSimon J. Gerraty 
45706b9b3e0SSimon J. Gerraty 	/* Replace $<ch> with ${:U<value>} */
4589f45a3c8SSimon J. Gerraty 	Buf_AddStr(body, "{:U");
4599f45a3c8SSimon J. Gerraty 	AddEscaped(body, f->items.words[firstItem + i], '}');
4609f45a3c8SSimon J. Gerraty 	Buf_AddByte(body, '}');
46106b9b3e0SSimon J. Gerraty }
46206b9b3e0SSimon J. Gerraty 
46306b9b3e0SSimon J. Gerraty /*
46406b9b3e0SSimon J. Gerraty  * Compute the body for the current iteration by copying the unexpanded body,
46506b9b3e0SSimon J. Gerraty  * replacing the expressions for the iteration variables on the way.
466956e45f6SSimon J. Gerraty  *
467d5e0a182SSimon J. Gerraty  * Using expressions ensures that the .for loop can't generate
468148ee845SSimon J. Gerraty  * syntax, and that the later parsing will still see an expression.
469148ee845SSimon J. Gerraty  * This code assumes that the variable with the empty name is never defined,
470148ee845SSimon J. Gerraty  * see unit-tests/varname-empty.mk.
4713955d011SMarcel Moolenaar  *
472dba7b0efSSimon J. Gerraty  * The detection of substitutions of the loop control variables is naive.
47312904384SSimon J. Gerraty  * Many of the modifiers use '\$' instead of '$$' to escape '$', so it is
47412904384SSimon J. Gerraty  * possible to contrive a makefile where an unwanted substitution happens.
475148ee845SSimon J. Gerraty  * See unit-tests/directive-for-escape.mk.
4763955d011SMarcel Moolenaar  */
47706b9b3e0SSimon J. Gerraty static void
4789f45a3c8SSimon J. Gerraty ForLoop_SubstBody(ForLoop *f, unsigned int firstItem, Buffer *body)
47906b9b3e0SSimon J. Gerraty {
4809f45a3c8SSimon J. Gerraty 	const char *p, *end;
48112904384SSimon J. Gerraty 	const char *mark;	/* where the last substitution left off */
48206b9b3e0SSimon J. Gerraty 
4839f45a3c8SSimon J. Gerraty 	Buf_Clear(body);
48406b9b3e0SSimon J. Gerraty 
48506b9b3e0SSimon J. Gerraty 	mark = f->body.data;
4869f45a3c8SSimon J. Gerraty 	end = f->body.data + f->body.len;
48706b9b3e0SSimon J. Gerraty 	for (p = mark; (p = strchr(p, '$')) != NULL;) {
48806b9b3e0SSimon J. Gerraty 		if (p[1] == '{' || p[1] == '(') {
48912904384SSimon J. Gerraty 			char endc = p[1] == '{' ? '}' : ')';
49006b9b3e0SSimon J. Gerraty 			p += 2;
4919f45a3c8SSimon J. Gerraty 			ForLoop_SubstVarLong(f, firstItem, body,
4929f45a3c8SSimon J. Gerraty 			    &p, endc, &mark);
49306b9b3e0SSimon J. Gerraty 		} else if (p[1] != '\0') {
4949f45a3c8SSimon J. Gerraty 			ForLoop_SubstVarShort(f, firstItem, body,
4959f45a3c8SSimon J. Gerraty 			    p + 1, &mark);
49606b9b3e0SSimon J. Gerraty 			p += 2;
49706b9b3e0SSimon J. Gerraty 		} else
49806b9b3e0SSimon J. Gerraty 			break;
49906b9b3e0SSimon J. Gerraty 	}
50006b9b3e0SSimon J. Gerraty 
501148ee845SSimon J. Gerraty 	Buf_AddRange(body, mark, end);
50206b9b3e0SSimon J. Gerraty }
50306b9b3e0SSimon J. Gerraty 
50406b9b3e0SSimon J. Gerraty /*
50506b9b3e0SSimon J. Gerraty  * Compute the body for the current iteration by copying the unexpanded body,
50606b9b3e0SSimon J. Gerraty  * replacing the expressions for the iteration variables on the way.
50706b9b3e0SSimon J. Gerraty  */
5089f45a3c8SSimon J. Gerraty bool
5099f45a3c8SSimon J. Gerraty For_NextIteration(ForLoop *f, Buffer *body)
510956e45f6SSimon J. Gerraty {
5119f45a3c8SSimon J. Gerraty 	if (f->nextItem == f->items.len)
5129f45a3c8SSimon J. Gerraty 		return false;
5133955d011SMarcel Moolenaar 
51412904384SSimon J. Gerraty 	f->nextItem += (unsigned int)f->vars.len;
5159f45a3c8SSimon J. Gerraty 	ForLoop_SubstBody(f, f->nextItem - (unsigned int)f->vars.len, body);
516148ee845SSimon J. Gerraty 	if (DEBUG(FOR)) {
517148ee845SSimon J. Gerraty 		char *details = ForLoop_Details(f);
518148ee845SSimon J. Gerraty 		debug_printf("For: loop body with %s:\n%s",
519148ee845SSimon J. Gerraty 		    details, body->data);
520148ee845SSimon J. Gerraty 		free(details);
521148ee845SSimon J. Gerraty 	}
5229f45a3c8SSimon J. Gerraty 	return true;
5233955d011SMarcel Moolenaar }
5243955d011SMarcel Moolenaar 
5254fde40d9SSimon J. Gerraty /* Break out of the .for loop. */
5264fde40d9SSimon J. Gerraty void
5274fde40d9SSimon J. Gerraty For_Break(ForLoop *f)
5284fde40d9SSimon J. Gerraty {
5294fde40d9SSimon J. Gerraty 	f->nextItem = (unsigned int)f->items.len;
5304fde40d9SSimon J. Gerraty }
5314fde40d9SSimon J. Gerraty 
53206b9b3e0SSimon J. Gerraty /* Run the .for loop, imitating the actions of an include file. */
5333955d011SMarcel Moolenaar void
5349f45a3c8SSimon J. Gerraty For_Run(unsigned headLineno, unsigned bodyReadLines)
5353955d011SMarcel Moolenaar {
5369f45a3c8SSimon J. Gerraty 	Buffer buf;
537dba7b0efSSimon J. Gerraty 	ForLoop *f = accumFor;
5383955d011SMarcel Moolenaar 	accumFor = NULL;
5393955d011SMarcel Moolenaar 
5409f45a3c8SSimon J. Gerraty 	if (f->items.len > 0) {
5419f45a3c8SSimon J. Gerraty 		Buf_Init(&buf);
5429f45a3c8SSimon J. Gerraty 		Parse_PushInput(NULL, headLineno, bodyReadLines, buf, f);
5439f45a3c8SSimon J. Gerraty 	} else
544dba7b0efSSimon J. Gerraty 		ForLoop_Free(f);
5453955d011SMarcel Moolenaar }
546