Home | History | Annotate | Line # | Download | only in make
for.c revision 1.154
      1  1.154    rillig /*	$NetBSD: for.c,v 1.154 2022/01/02 01:54:43 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.135    rillig /*
     33   1.80    rillig  * Handling of .for/.endfor loops in a makefile.
     34   1.80    rillig  *
     35  1.123    rillig  * For loops have the form:
     36   1.80    rillig  *
     37  1.123    rillig  *	.for <varname...> in <value...>
     38  1.123    rillig  *	# the body
     39  1.123    rillig  *	.endfor
     40  1.123    rillig  *
     41  1.123    rillig  * When a .for line is parsed, the following lines are copied to the body of
     42  1.123    rillig  * the .for loop, until the corresponding .endfor line is reached.  In this
     43  1.123    rillig  * phase, the body is not yet evaluated.  This also applies to any nested
     44  1.123    rillig  * .for loops.
     45  1.123    rillig  *
     46  1.123    rillig  * After reaching the .endfor, the values from the .for line are grouped
     47  1.123    rillig  * according to the number of variables.  For each such group, the unexpanded
     48  1.149    rillig  * body is scanned for variable expressions, and those that match the
     49  1.149    rillig  * variable names are replaced with expressions of the form ${:U...}.  After
     50  1.149    rillig  * that, the body is treated like a file from an .include directive.
     51    1.1       cgd  *
     52    1.1       cgd  * Interface:
     53   1.88    rillig  *	For_Eval	Evaluate the loop in the passed line.
     54   1.88    rillig  *
     55    1.1       cgd  *	For_Run		Run accumulated loop
     56    1.1       cgd  */
     57    1.1       cgd 
     58  1.112    rillig #include "make.h"
     59    1.1       cgd 
     60   1.83    rillig /*	"@(#)for.c	8.1 (Berkeley) 6/6/93"	*/
     61  1.154    rillig MAKE_RCSID("$NetBSD: for.c,v 1.154 2022/01/02 01:54:43 rillig Exp $");
     62   1.42       dsl 
     63    1.1       cgd 
     64   1.96    rillig /* One of the variables to the left of the "in" in a .for loop. */
     65   1.96    rillig typedef struct ForVar {
     66  1.118    rillig 	char *name;
     67  1.123    rillig 	size_t nameLen;
     68   1.96    rillig } ForVar;
     69   1.96    rillig 
     70  1.136    rillig typedef struct ForLoop {
     71  1.118    rillig 	Buffer body;		/* Unexpanded body of the loop */
     72  1.118    rillig 	Vector /* of ForVar */ vars; /* Iteration variables */
     73  1.148    rillig 	SubstringWords items;	/* Substitution items */
     74  1.149    rillig 	unsigned int nextItem;	/* Where to continue iterating */
     75  1.136    rillig } ForLoop;
     76    1.1       cgd 
     77  1.137    rillig 
     78  1.143    rillig static ForLoop *accumFor;	/* Loop being accumulated */
     79  1.137    rillig static int forLevel = 0;	/* Nesting level */
     80    1.1       cgd 
     81  1.137    rillig 
     82  1.137    rillig static ForLoop *
     83  1.137    rillig ForLoop_New(void)
     84   1.96    rillig {
     85  1.137    rillig 	ForLoop *f = bmake_malloc(sizeof *f);
     86  1.137    rillig 
     87  1.137    rillig 	Buf_Init(&f->body);
     88  1.137    rillig 	Vector_Init(&f->vars, sizeof(ForVar));
     89  1.148    rillig 	SubstringWords_Init(&f->items);
     90  1.149    rillig 	f->nextItem = 0;
     91  1.137    rillig 
     92  1.137    rillig 	return f;
     93   1.96    rillig }
     94   1.96    rillig 
     95   1.96    rillig static void
     96  1.136    rillig ForLoop_Free(ForLoop *f)
     97   1.96    rillig {
     98  1.139    rillig 	Buf_Done(&f->body);
     99   1.96    rillig 
    100  1.118    rillig 	while (f->vars.len > 0) {
    101  1.118    rillig 		ForVar *var = Vector_Pop(&f->vars);
    102  1.118    rillig 		free(var->name);
    103  1.118    rillig 	}
    104  1.118    rillig 	Vector_Done(&f->vars);
    105   1.96    rillig 
    106  1.148    rillig 	SubstringWords_Free(f->items);
    107   1.96    rillig 
    108  1.118    rillig 	free(f);
    109   1.43       dsl }
    110   1.43       dsl 
    111  1.137    rillig static void
    112  1.137    rillig ForLoop_AddVar(ForLoop *f, const char *name, size_t len)
    113  1.137    rillig {
    114  1.137    rillig 	ForVar *var = Vector_Push(&f->vars);
    115  1.137    rillig 	var->name = bmake_strldup(name, len);
    116  1.137    rillig 	var->nameLen = len;
    117  1.137    rillig }
    118  1.137    rillig 
    119  1.142    rillig static bool
    120  1.138    rillig ForLoop_ParseVarnames(ForLoop *f, const char **pp)
    121  1.138    rillig {
    122  1.138    rillig 	const char *p = *pp;
    123  1.138    rillig 
    124  1.138    rillig 	for (;;) {
    125  1.138    rillig 		size_t len;
    126  1.138    rillig 
    127  1.138    rillig 		cpp_skip_whitespace(&p);
    128  1.138    rillig 		if (*p == '\0') {
    129  1.138    rillig 			Parse_Error(PARSE_FATAL, "missing `in' in for");
    130  1.142    rillig 			return false;
    131  1.138    rillig 		}
    132  1.138    rillig 
    133  1.138    rillig 		/*
    134  1.138    rillig 		 * XXX: This allows arbitrary variable names;
    135  1.138    rillig 		 * see directive-for.mk.
    136  1.138    rillig 		 */
    137  1.138    rillig 		for (len = 1; p[len] != '\0' && !ch_isspace(p[len]); len++)
    138  1.138    rillig 			continue;
    139  1.138    rillig 
    140  1.138    rillig 		if (len == 2 && p[0] == 'i' && p[1] == 'n') {
    141  1.138    rillig 			p += 2;
    142  1.138    rillig 			break;
    143  1.138    rillig 		}
    144  1.138    rillig 
    145  1.138    rillig 		ForLoop_AddVar(f, p, len);
    146  1.138    rillig 		p += len;
    147  1.138    rillig 	}
    148  1.138    rillig 
    149  1.138    rillig 	if (f->vars.len == 0) {
    150  1.138    rillig 		Parse_Error(PARSE_FATAL, "no iteration variables in for");
    151  1.142    rillig 		return false;
    152  1.138    rillig 	}
    153  1.138    rillig 
    154  1.138    rillig 	*pp = p;
    155  1.142    rillig 	return true;
    156  1.138    rillig }
    157  1.138    rillig 
    158  1.142    rillig static bool
    159  1.138    rillig ForLoop_ParseItems(ForLoop *f, const char *p)
    160  1.138    rillig {
    161  1.138    rillig 	char *items;
    162  1.138    rillig 
    163  1.138    rillig 	cpp_skip_whitespace(&p);
    164  1.138    rillig 
    165  1.141    rillig 	if (Var_Subst(p, SCOPE_GLOBAL, VARE_WANTRES, &items) != VPR_OK) {
    166  1.138    rillig 		Parse_Error(PARSE_FATAL, "Error in .for loop items");
    167  1.142    rillig 		return false;
    168  1.138    rillig 	}
    169  1.138    rillig 
    170  1.148    rillig 	f->items = Substring_Words(items, false);
    171  1.138    rillig 	free(items);
    172  1.138    rillig 
    173  1.148    rillig 	if (f->items.len == 1 && Substring_IsEmpty(f->items.words[0]))
    174  1.151    rillig 		f->items.len = 0;	/* .for var in ${:U} */
    175  1.138    rillig 
    176  1.138    rillig 	if (f->items.len != 0 && f->items.len % f->vars.len != 0) {
    177  1.138    rillig 		Parse_Error(PARSE_FATAL,
    178  1.138    rillig 		    "Wrong number of words (%u) in .for "
    179  1.138    rillig 		    "substitution list with %u variables",
    180  1.138    rillig 		    (unsigned)f->items.len, (unsigned)f->vars.len);
    181  1.142    rillig 		return false;
    182  1.138    rillig 	}
    183  1.138    rillig 
    184  1.142    rillig 	return true;
    185  1.138    rillig }
    186  1.138    rillig 
    187  1.142    rillig static bool
    188  1.106    rillig IsFor(const char *p)
    189  1.106    rillig {
    190  1.118    rillig 	return p[0] == 'f' && p[1] == 'o' && p[2] == 'r' && ch_isspace(p[3]);
    191  1.106    rillig }
    192  1.106    rillig 
    193  1.142    rillig static bool
    194  1.106    rillig IsEndfor(const char *p)
    195  1.106    rillig {
    196  1.118    rillig 	return p[0] == 'e' && strncmp(p, "endfor", 6) == 0 &&
    197  1.118    rillig 	       (p[6] == '\0' || ch_isspace(p[6]));
    198  1.106    rillig }
    199  1.106    rillig 
    200  1.122    rillig /*
    201  1.122    rillig  * Evaluate the for loop in the passed line. The line looks like this:
    202   1.70    rillig  *	.for <varname...> in <value...>
    203    1.1       cgd  *
    204   1.13       wiz  * Input:
    205   1.13       wiz  *	line		Line to parse
    206   1.13       wiz  *
    207    1.1       cgd  * Results:
    208  1.153    rillig  *	0: Not a .for statement, parse the line
    209   1.35       dsl  *	1: We found a for loop
    210  1.153    rillig  *	-1: A .for statement with a bad syntax error, discard.
    211    1.1       cgd  */
    212    1.1       cgd int
    213   1.73    rillig For_Eval(const char *line)
    214    1.1       cgd {
    215  1.136    rillig 	ForLoop *f;
    216  1.118    rillig 	const char *p;
    217   1.33       dsl 
    218  1.118    rillig 	p = line + 1;		/* skip the '.' */
    219  1.118    rillig 	cpp_skip_whitespace(&p);
    220   1.43       dsl 
    221  1.118    rillig 	if (!IsFor(p)) {
    222  1.118    rillig 		if (IsEndfor(p)) {
    223  1.118    rillig 			Parse_Error(PARSE_FATAL, "for-less endfor");
    224  1.118    rillig 			return -1;
    225  1.118    rillig 		}
    226  1.118    rillig 		return 0;
    227   1.32       dsl 	}
    228  1.118    rillig 	p += 3;
    229  1.118    rillig 
    230  1.137    rillig 	f = ForLoop_New();
    231  1.118    rillig 
    232  1.138    rillig 	if (!ForLoop_ParseVarnames(f, &p)) {
    233  1.136    rillig 		ForLoop_Free(f);
    234  1.118    rillig 		return -1;
    235  1.118    rillig 	}
    236   1.72    rillig 
    237  1.138    rillig 	if (!ForLoop_ParseItems(f, p)) {
    238  1.138    rillig 		/* Continue parsing the .for loop, but don't iterate. */
    239  1.138    rillig 		f->items.len = 0;
    240  1.118    rillig 	}
    241  1.118    rillig 
    242  1.118    rillig 	accumFor = f;
    243  1.118    rillig 	forLevel = 1;
    244  1.118    rillig 	return 1;
    245   1.32       dsl }
    246    1.7  christos 
    247   1.35       dsl /*
    248  1.136    rillig  * Add another line to the .for loop that is being built up.
    249  1.142    rillig  * Returns false when the matching .endfor is reached.
    250   1.35       dsl  */
    251  1.142    rillig bool
    252   1.73    rillig For_Accum(const char *line)
    253   1.32       dsl {
    254  1.140    rillig 	const char *p = line;
    255   1.26       dsl 
    256  1.140    rillig 	if (*p == '.') {
    257  1.140    rillig 		p++;
    258  1.140    rillig 		cpp_skip_whitespace(&p);
    259  1.118    rillig 
    260  1.140    rillig 		if (IsEndfor(p)) {
    261  1.118    rillig 			DEBUG1(FOR, "For: end for %d\n", forLevel);
    262  1.118    rillig 			if (--forLevel <= 0)
    263  1.142    rillig 				return false;
    264  1.140    rillig 		} else if (IsFor(p)) {
    265  1.118    rillig 			forLevel++;
    266  1.118    rillig 			DEBUG1(FOR, "For: new loop %d\n", forLevel);
    267  1.118    rillig 		}
    268  1.118    rillig 	}
    269  1.118    rillig 
    270  1.118    rillig 	Buf_AddStr(&accumFor->body, line);
    271  1.118    rillig 	Buf_AddByte(&accumFor->body, '\n');
    272  1.142    rillig 	return true;
    273    1.1       cgd }
    274    1.1       cgd 
    275   1.42       dsl 
    276   1.59    rillig static size_t
    277  1.148    rillig ExprLen(const char *s, const char *e)
    278   1.45       dsl {
    279  1.148    rillig 	char expr_open, expr_close;
    280  1.118    rillig 	int depth;
    281  1.148    rillig 	const char *p;
    282   1.45       dsl 
    283  1.148    rillig 	if (s == e)
    284  1.148    rillig 		return 0;	/* just escape the '$' */
    285  1.118    rillig 
    286  1.148    rillig 	expr_open = s[0];
    287  1.147    rillig 	if (expr_open == '(')
    288  1.147    rillig 		expr_close = ')';
    289  1.147    rillig 	else if (expr_open == '{')
    290  1.147    rillig 		expr_close = '}';
    291  1.118    rillig 	else
    292  1.118    rillig 		return 1;	/* Single char variable */
    293  1.118    rillig 
    294  1.118    rillig 	depth = 1;
    295  1.148    rillig 	for (p = s + 1; p != e; p++) {
    296  1.148    rillig 		if (*p == expr_open)
    297  1.118    rillig 			depth++;
    298  1.148    rillig 		else if (*p == expr_close && --depth == 0)
    299  1.148    rillig 			return (size_t)(p + 1 - s);
    300  1.118    rillig 	}
    301   1.45       dsl 
    302  1.147    rillig 	/* Expression end not found, escape the $ */
    303  1.118    rillig 	return 0;
    304   1.45       dsl }
    305   1.45       dsl 
    306  1.122    rillig /*
    307  1.122    rillig  * The .for loop substitutes the items as ${:U<value>...}, which means
    308  1.122    rillig  * that characters that break this syntax must be backslash-escaped.
    309  1.122    rillig  */
    310  1.142    rillig static bool
    311  1.148    rillig NeedsEscapes(Substring value, char endc)
    312  1.115    rillig {
    313  1.118    rillig 	const char *p;
    314  1.115    rillig 
    315  1.148    rillig 	for (p = value.start; p != value.end; p++) {
    316  1.144    rillig 		if (*p == ':' || *p == '$' || *p == '\\' || *p == endc ||
    317  1.144    rillig 		    *p == '\n')
    318  1.142    rillig 			return true;
    319  1.118    rillig 	}
    320  1.142    rillig 	return false;
    321  1.115    rillig }
    322  1.115    rillig 
    323  1.122    rillig /*
    324  1.122    rillig  * While expanding the body of a .for loop, write the item in the ${:U...}
    325  1.115    rillig  * expression, escaping characters as needed.
    326  1.115    rillig  *
    327  1.122    rillig  * The result is later unescaped by ApplyModifier_Defined.
    328  1.122    rillig  */
    329   1.42       dsl static void
    330  1.148    rillig Buf_AddEscaped(Buffer *cmds, Substring item, char endc)
    331   1.42       dsl {
    332  1.148    rillig 	const char *p;
    333  1.118    rillig 	char ch;
    334  1.118    rillig 
    335  1.127    rillig 	if (!NeedsEscapes(item, endc)) {
    336  1.148    rillig 		Buf_AddBytesBetween(cmds, item.start, item.end);
    337  1.118    rillig 		return;
    338  1.118    rillig 	}
    339   1.61    rillig 
    340  1.151    rillig 	/*
    341  1.151    rillig 	 * Escape ':', '$', '\\' and 'endc' - these will be removed later by
    342  1.151    rillig 	 * :U processing, see ApplyModifier_Defined.
    343  1.151    rillig 	 */
    344  1.148    rillig 	for (p = item.start; p != item.end; p++) {
    345  1.148    rillig 		ch = *p;
    346  1.118    rillig 		if (ch == '$') {
    347  1.148    rillig 			size_t len = ExprLen(p + 1, item.end);
    348  1.118    rillig 			if (len != 0) {
    349  1.147    rillig 				/*
    350  1.147    rillig 				 * XXX: Should a '\' be added here?
    351  1.147    rillig 				 * See directive-for-escape.mk, ExprLen.
    352  1.147    rillig 				 */
    353  1.148    rillig 				Buf_AddBytes(cmds, p, 1 + len);
    354  1.148    rillig 				p += len;
    355  1.118    rillig 				continue;
    356  1.118    rillig 			}
    357  1.118    rillig 			Buf_AddByte(cmds, '\\');
    358  1.127    rillig 		} else if (ch == ':' || ch == '\\' || ch == endc)
    359  1.118    rillig 			Buf_AddByte(cmds, '\\');
    360  1.144    rillig 		else if (ch == '\n') {
    361  1.144    rillig 			Parse_Error(PARSE_FATAL, "newline in .for value");
    362  1.144    rillig 			ch = ' ';	/* prevent newline injection */
    363  1.144    rillig 		}
    364  1.118    rillig 		Buf_AddByte(cmds, ch);
    365  1.118    rillig 	}
    366   1.42       dsl }
    367   1.42       dsl 
    368  1.122    rillig /*
    369  1.146    rillig  * When expanding the body of a .for loop, replace the variable name of an
    370  1.123    rillig  * expression like ${i} or ${i:...} or $(i) or $(i:...) with ":Uvalue".
    371  1.122    rillig  */
    372   1.97    rillig static void
    373  1.153    rillig ForLoop_SubstVarLong(ForLoop *f, Buffer *body, const char **pp,
    374  1.153    rillig 		     const char *end, char endc, const char **inout_mark)
    375   1.97    rillig {
    376  1.118    rillig 	size_t i;
    377  1.118    rillig 	const char *p = *pp;
    378   1.97    rillig 
    379  1.118    rillig 	for (i = 0; i < f->vars.len; i++) {
    380  1.143    rillig 		const ForVar *forVar = Vector_Get(&f->vars, i);
    381  1.143    rillig 		const char *varname = forVar->name;
    382  1.123    rillig 		size_t varnameLen = forVar->nameLen;
    383  1.118    rillig 
    384  1.153    rillig 		if (varnameLen >= (size_t)(end - p))
    385  1.132    rillig 			continue;
    386  1.123    rillig 		if (memcmp(p, varname, varnameLen) != 0)
    387  1.118    rillig 			continue;
    388  1.118    rillig 		/* XXX: why test for backslash here? */
    389  1.127    rillig 		if (p[varnameLen] != ':' && p[varnameLen] != endc &&
    390  1.123    rillig 		    p[varnameLen] != '\\')
    391  1.118    rillig 			continue;
    392  1.118    rillig 
    393  1.131    rillig 		/*
    394  1.131    rillig 		 * Found a variable match.  Skip over the variable name and
    395  1.131    rillig 		 * instead add ':U<value>' to the current body.
    396  1.131    rillig 		 */
    397  1.153    rillig 		Buf_AddBytesBetween(body, *inout_mark, p);
    398  1.153    rillig 		Buf_AddStr(body, ":U");
    399  1.153    rillig 		Buf_AddEscaped(body, f->items.words[f->nextItem + i], endc);
    400  1.118    rillig 
    401  1.123    rillig 		p += varnameLen;
    402  1.118    rillig 		*inout_mark = p;
    403  1.131    rillig 		*pp = p;
    404  1.131    rillig 		return;
    405  1.118    rillig 	}
    406   1.97    rillig }
    407   1.97    rillig 
    408  1.122    rillig /*
    409  1.146    rillig  * When expanding the body of a .for loop, replace single-character
    410  1.122    rillig  * variable expressions like $i with their ${:U...} expansion.
    411  1.122    rillig  */
    412   1.98    rillig static void
    413  1.153    rillig ForLoop_SubstVarShort(ForLoop *f, Buffer *body,
    414  1.153    rillig 		      const char *p, const char **inout_mark)
    415   1.98    rillig {
    416  1.123    rillig 	const char ch = *p;
    417  1.143    rillig 	const ForVar *vars;
    418  1.118    rillig 	size_t i;
    419  1.118    rillig 
    420  1.130    rillig 	/* Skip $$ and stupid ones. */
    421  1.146    rillig 	if (ch == '}' || ch == ')' || ch == ':' || ch == '$')
    422  1.118    rillig 		return;
    423   1.98    rillig 
    424  1.130    rillig 	vars = Vector_Get(&f->vars, 0);
    425  1.118    rillig 	for (i = 0; i < f->vars.len; i++) {
    426  1.130    rillig 		const char *varname = vars[i].name;
    427  1.130    rillig 		if (varname[0] == ch && varname[1] == '\0')
    428  1.130    rillig 			goto found;
    429  1.130    rillig 	}
    430  1.130    rillig 	return;
    431  1.130    rillig 
    432  1.130    rillig found:
    433  1.153    rillig 	Buf_AddBytesBetween(body, *inout_mark, p);
    434  1.147    rillig 	*inout_mark = p + 1;
    435  1.147    rillig 
    436  1.130    rillig 	/* Replace $<ch> with ${:U<value>} */
    437  1.153    rillig 	Buf_AddStr(body, "{:U");
    438  1.153    rillig 	Buf_AddEscaped(body, f->items.words[f->nextItem + i], '}');
    439  1.153    rillig 	Buf_AddByte(body, '}');
    440   1.98    rillig }
    441   1.98    rillig 
    442   1.95    rillig /*
    443  1.128    rillig  * Compute the body for the current iteration by copying the unexpanded body,
    444  1.128    rillig  * replacing the expressions for the iteration variables on the way.
    445  1.129    rillig  *
    446  1.129    rillig  * Using variable expressions ensures that the .for loop can't generate
    447  1.129    rillig  * syntax, and that the later parsing will still see a variable.
    448  1.129    rillig  * This code assumes that the variable with the empty name will never be
    449  1.129    rillig  * defined, see unit-tests/varname-empty.mk for more details.
    450  1.129    rillig  *
    451  1.140    rillig  * The detection of substitutions of the loop control variables is naive.
    452  1.147    rillig  * Many of the modifiers use '\$' instead of '$$' to escape '$', so it is
    453  1.147    rillig  * possible to contrive a makefile where an unwanted substitution happens.
    454  1.128    rillig  */
    455  1.128    rillig static void
    456  1.153    rillig ForLoop_SubstBody(ForLoop *f, Buffer *body)
    457  1.128    rillig {
    458  1.153    rillig 	const char *p, *end;
    459  1.146    rillig 	const char *mark;	/* where the last substitution left off */
    460  1.128    rillig 
    461  1.153    rillig 	Buf_Empty(body);
    462  1.128    rillig 
    463  1.128    rillig 	mark = f->body.data;
    464  1.153    rillig 	end = f->body.data + f->body.len;
    465  1.128    rillig 	for (p = mark; (p = strchr(p, '$')) != NULL;) {
    466  1.128    rillig 		if (p[1] == '{' || p[1] == '(') {
    467  1.146    rillig 			char endc = p[1] == '{' ? '}' : ')';
    468  1.128    rillig 			p += 2;
    469  1.153    rillig 			ForLoop_SubstVarLong(f, body, &p, end, endc, &mark);
    470  1.128    rillig 		} else if (p[1] != '\0') {
    471  1.153    rillig 			ForLoop_SubstVarShort(f, body, p + 1, &mark);
    472  1.130    rillig 			p += 2;
    473  1.128    rillig 		} else
    474  1.128    rillig 			break;
    475  1.128    rillig 	}
    476  1.128    rillig 
    477  1.153    rillig 	Buf_AddBytesBetween(body, mark, end);
    478  1.128    rillig }
    479  1.128    rillig 
    480  1.128    rillig /*
    481  1.129    rillig  * Compute the body for the current iteration by copying the unexpanded body,
    482  1.129    rillig  * replacing the expressions for the iteration variables on the way.
    483   1.95    rillig  */
    484  1.154    rillig bool
    485  1.154    rillig For_NextIteration(ForLoop *f, Buffer *body)
    486    1.1       cgd {
    487  1.149    rillig 	if (f->nextItem == f->items.len) {
    488  1.118    rillig 		/* No more iterations */
    489  1.136    rillig 		ForLoop_Free(f);
    490  1.154    rillig 		return false;
    491  1.118    rillig 	}
    492  1.118    rillig 
    493  1.154    rillig 	ForLoop_SubstBody(f, body);
    494  1.154    rillig 	DEBUG1(FOR, "For: loop body:\n%s", body->data);
    495  1.149    rillig 	f->nextItem += (unsigned int)f->vars.len;
    496  1.154    rillig 	return true;
    497   1.43       dsl }
    498    1.7  christos 
    499  1.120    rillig /* Run the .for loop, imitating the actions of an include file. */
    500   1.43       dsl void
    501   1.43       dsl For_Run(int lineno)
    502   1.54    rillig {
    503  1.154    rillig 	Buffer buf;
    504  1.136    rillig 	ForLoop *f = accumFor;
    505  1.118    rillig 	accumFor = NULL;
    506    1.1       cgd 
    507  1.154    rillig 	if (f->items.len > 0) {
    508  1.154    rillig 		Buf_Init(&buf);
    509  1.154    rillig 		Parse_PushInput(NULL, lineno, buf, f);
    510  1.154    rillig 	} else
    511  1.136    rillig 		ForLoop_Free(f);
    512    1.1       cgd }
    513