Home | History | Annotate | Line # | Download | only in make
for.c revision 1.128
      1  1.128    rillig /*	$NetBSD: for.c,v 1.128 2020/12/31 04:31:36 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.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.123    rillig  * body is scanned for variable expressions, and those that match the variable
     49  1.123    rillig  * names are replaced with expressions of the form ${:U...} or $(:U...).
     50  1.123    rillig  * After 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.128    rillig MAKE_RCSID("$NetBSD: for.c,v 1.128 2020/12/31 04:31:36 rillig Exp $");
     62   1.42       dsl 
     63   1.63    rillig static int forLevel = 0;	/* Nesting level */
     64    1.1       cgd 
     65   1.96    rillig /* One of the variables to the left of the "in" in a .for loop. */
     66   1.96    rillig typedef struct ForVar {
     67  1.118    rillig 	char *name;
     68  1.123    rillig 	size_t nameLen;
     69   1.96    rillig } ForVar;
     70   1.96    rillig 
     71    1.1       cgd /*
     72    1.1       cgd  * State of a for loop.
     73    1.1       cgd  */
     74   1.86    rillig typedef struct For {
     75  1.118    rillig 	Buffer body;		/* Unexpanded body of the loop */
     76  1.118    rillig 	Vector /* of ForVar */ vars; /* Iteration variables */
     77  1.118    rillig 	Words items;		/* Substitution items */
     78  1.118    rillig 	Buffer curBody;		/* Expanded body of the current iteration */
     79  1.118    rillig 	/* Is any of the names 1 character long? If so, when the variable values
     80  1.118    rillig 	 * are substituted, the parser must handle $V expressions as well, not
     81  1.118    rillig 	 * only ${V} and $(V). */
     82  1.118    rillig 	Boolean short_var;
     83  1.118    rillig 	unsigned int sub_next;	/* Where to continue iterating */
     84    1.2       jtc } For;
     85    1.1       cgd 
     86   1.63    rillig static For *accumFor;		/* Loop being accumulated */
     87    1.1       cgd 
     88   1.96    rillig static void
     89   1.96    rillig ForAddVar(For *f, const char *name, size_t len)
     90   1.96    rillig {
     91  1.118    rillig 	ForVar *var = Vector_Push(&f->vars);
     92  1.118    rillig 	var->name = bmake_strldup(name, len);
     93  1.123    rillig 	var->nameLen = len;
     94   1.96    rillig }
     95   1.96    rillig 
     96   1.96    rillig static void
     97  1.109    rillig For_Free(For *f)
     98   1.96    rillig {
     99  1.118    rillig 	Buf_Destroy(&f->body, TRUE);
    100   1.96    rillig 
    101  1.118    rillig 	while (f->vars.len > 0) {
    102  1.118    rillig 		ForVar *var = Vector_Pop(&f->vars);
    103  1.118    rillig 		free(var->name);
    104  1.118    rillig 	}
    105  1.118    rillig 	Vector_Done(&f->vars);
    106   1.96    rillig 
    107  1.118    rillig 	Words_Free(f->items);
    108  1.118    rillig 	Buf_Destroy(&f->curBody, TRUE);
    109   1.96    rillig 
    110  1.118    rillig 	free(f);
    111   1.43       dsl }
    112   1.43       dsl 
    113  1.106    rillig static Boolean
    114  1.106    rillig IsFor(const char *p)
    115  1.106    rillig {
    116  1.118    rillig 	return p[0] == 'f' && p[1] == 'o' && p[2] == 'r' && ch_isspace(p[3]);
    117  1.106    rillig }
    118  1.106    rillig 
    119  1.106    rillig static Boolean
    120  1.106    rillig IsEndfor(const char *p)
    121  1.106    rillig {
    122  1.118    rillig 	return p[0] == 'e' && strncmp(p, "endfor", 6) == 0 &&
    123  1.118    rillig 	       (p[6] == '\0' || ch_isspace(p[6]));
    124  1.106    rillig }
    125  1.106    rillig 
    126  1.122    rillig /*
    127  1.122    rillig  * Evaluate the for loop in the passed line. The line looks like this:
    128   1.70    rillig  *	.for <varname...> in <value...>
    129    1.1       cgd  *
    130   1.13       wiz  * Input:
    131   1.13       wiz  *	line		Line to parse
    132   1.13       wiz  *
    133    1.1       cgd  * Results:
    134   1.35       dsl  *      0: Not a .for statement, parse the line
    135   1.35       dsl  *	1: We found a for loop
    136   1.35       dsl  *     -1: A .for statement with a bad syntax error, discard.
    137    1.1       cgd  */
    138    1.1       cgd int
    139   1.73    rillig For_Eval(const char *line)
    140    1.1       cgd {
    141  1.118    rillig 	For *f;
    142  1.118    rillig 	const char *p;
    143   1.33       dsl 
    144  1.118    rillig 	p = line + 1;		/* skip the '.' */
    145  1.118    rillig 	cpp_skip_whitespace(&p);
    146   1.43       dsl 
    147  1.118    rillig 	if (!IsFor(p)) {
    148  1.118    rillig 		if (IsEndfor(p)) {
    149  1.118    rillig 			Parse_Error(PARSE_FATAL, "for-less endfor");
    150  1.118    rillig 			return -1;
    151  1.118    rillig 		}
    152  1.118    rillig 		return 0;
    153   1.32       dsl 	}
    154  1.118    rillig 	p += 3;
    155  1.118    rillig 
    156  1.118    rillig 	/*
    157  1.118    rillig 	 * we found a for loop, and now we are going to parse it.
    158  1.118    rillig 	 */
    159  1.118    rillig 
    160  1.118    rillig 	f = bmake_malloc(sizeof *f);
    161  1.118    rillig 	Buf_Init(&f->body);
    162  1.118    rillig 	Vector_Init(&f->vars, sizeof(ForVar));
    163  1.118    rillig 	f->items.words = NULL;
    164  1.118    rillig 	f->items.freeIt = NULL;
    165  1.118    rillig 	Buf_Init(&f->curBody);
    166  1.118    rillig 	f->short_var = FALSE;
    167  1.118    rillig 	f->sub_next = 0;
    168  1.118    rillig 
    169  1.118    rillig 	/* Grab the variables. Terminate on "in". */
    170  1.118    rillig 	for (;;) {
    171  1.118    rillig 		size_t len;
    172  1.118    rillig 
    173  1.118    rillig 		cpp_skip_whitespace(&p);
    174  1.118    rillig 		if (*p == '\0') {
    175  1.118    rillig 			Parse_Error(PARSE_FATAL, "missing `in' in for");
    176  1.118    rillig 			For_Free(f);
    177  1.118    rillig 			return -1;
    178  1.118    rillig 		}
    179  1.118    rillig 
    180  1.118    rillig 		/*
    181  1.118    rillig 		 * XXX: This allows arbitrary variable names;
    182  1.118    rillig 		 * see directive-for.mk.
    183  1.118    rillig 		 */
    184  1.118    rillig 		for (len = 1; p[len] != '\0' && !ch_isspace(p[len]); len++)
    185  1.118    rillig 			continue;
    186  1.118    rillig 
    187  1.118    rillig 		if (len == 2 && p[0] == 'i' && p[1] == 'n') {
    188  1.118    rillig 			p += 2;
    189  1.118    rillig 			break;
    190  1.118    rillig 		}
    191  1.118    rillig 		if (len == 1)
    192  1.118    rillig 			f->short_var = TRUE;
    193    1.1       cgd 
    194  1.118    rillig 		ForAddVar(f, p, len);
    195  1.118    rillig 		p += len;
    196  1.118    rillig 	}
    197   1.43       dsl 
    198  1.118    rillig 	if (f->vars.len == 0) {
    199  1.118    rillig 		Parse_Error(PARSE_FATAL, "no iteration variables in for");
    200  1.118    rillig 		For_Free(f);
    201  1.118    rillig 		return -1;
    202  1.118    rillig 	}
    203   1.72    rillig 
    204  1.105    rillig 	cpp_skip_whitespace(&p);
    205  1.118    rillig 
    206  1.118    rillig 	{
    207  1.118    rillig 		char *items;
    208  1.121    rillig 		if (Var_Subst(p, VAR_GLOBAL, VARE_WANTRES, &items) != VPR_OK) {
    209  1.121    rillig 			Parse_Error(PARSE_FATAL, "Error in .for loop items");
    210  1.121    rillig 			f->items.len = 0;
    211  1.121    rillig 			goto done;
    212  1.121    rillig 		}
    213  1.121    rillig 
    214  1.118    rillig 		f->items = Str_Words(items, FALSE);
    215  1.118    rillig 		free(items);
    216  1.118    rillig 
    217  1.118    rillig 		if (f->items.len == 1 && f->items.words[0][0] == '\0')
    218  1.118    rillig 			f->items.len = 0; /* .for var in ${:U} */
    219  1.118    rillig 	}
    220  1.118    rillig 
    221  1.118    rillig 	{
    222  1.118    rillig 		size_t nitems, nvars;
    223  1.118    rillig 
    224  1.118    rillig 		if ((nitems = f->items.len) > 0 &&
    225  1.118    rillig 		    nitems % (nvars = f->vars.len)) {
    226  1.118    rillig 			Parse_Error(PARSE_FATAL,
    227  1.118    rillig 			    "Wrong number of words (%u) in .for "
    228  1.118    rillig 			    "substitution list with %u variables",
    229  1.118    rillig 			    (unsigned)nitems, (unsigned)nvars);
    230  1.118    rillig 			/*
    231  1.118    rillig 			 * Return 'success' so that the body of the .for loop
    232  1.118    rillig 			 * is accumulated.
    233  1.118    rillig 			 * Remove all items so that the loop doesn't iterate.
    234  1.118    rillig 			 */
    235  1.118    rillig 			f->items.len = 0;
    236  1.118    rillig 		}
    237  1.118    rillig 	}
    238  1.118    rillig 
    239  1.121    rillig done:
    240  1.118    rillig 	accumFor = f;
    241  1.118    rillig 	forLevel = 1;
    242  1.118    rillig 	return 1;
    243   1.32       dsl }
    244    1.7  christos 
    245   1.35       dsl /*
    246   1.35       dsl  * Add another line to a .for loop.
    247   1.81    rillig  * Returns FALSE when the matching .endfor is reached.
    248   1.35       dsl  */
    249   1.81    rillig Boolean
    250   1.73    rillig For_Accum(const char *line)
    251   1.32       dsl {
    252  1.118    rillig 	const char *ptr = line;
    253   1.26       dsl 
    254  1.118    rillig 	if (*ptr == '.') {
    255  1.118    rillig 		ptr++;
    256  1.118    rillig 		cpp_skip_whitespace(&ptr);
    257  1.118    rillig 
    258  1.118    rillig 		if (IsEndfor(ptr)) {
    259  1.118    rillig 			DEBUG1(FOR, "For: end for %d\n", forLevel);
    260  1.118    rillig 			if (--forLevel <= 0)
    261  1.118    rillig 				return FALSE;
    262  1.118    rillig 		} else if (IsFor(ptr)) {
    263  1.118    rillig 			forLevel++;
    264  1.118    rillig 			DEBUG1(FOR, "For: new loop %d\n", forLevel);
    265  1.118    rillig 		}
    266  1.118    rillig 	}
    267  1.118    rillig 
    268  1.118    rillig 	Buf_AddStr(&accumFor->body, line);
    269  1.118    rillig 	Buf_AddByte(&accumFor->body, '\n');
    270  1.118    rillig 	return TRUE;
    271    1.1       cgd }
    272    1.1       cgd 
    273   1.42       dsl 
    274   1.59    rillig static size_t
    275   1.45       dsl for_var_len(const char *var)
    276   1.45       dsl {
    277  1.118    rillig 	char ch, var_start, var_end;
    278  1.118    rillig 	int depth;
    279  1.118    rillig 	size_t len;
    280   1.45       dsl 
    281  1.118    rillig 	var_start = *var;
    282  1.118    rillig 	if (var_start == '\0')
    283  1.118    rillig 		/* just escape the $ */
    284  1.118    rillig 		return 0;
    285  1.118    rillig 
    286  1.118    rillig 	if (var_start == '(')
    287  1.118    rillig 		var_end = ')';
    288  1.118    rillig 	else if (var_start == '{')
    289  1.118    rillig 		var_end = '}';
    290  1.118    rillig 	else
    291  1.118    rillig 		return 1;	/* Single char variable */
    292  1.118    rillig 
    293  1.118    rillig 	depth = 1;
    294  1.118    rillig 	for (len = 1; (ch = var[len++]) != '\0';) {
    295  1.118    rillig 		if (ch == var_start)
    296  1.118    rillig 			depth++;
    297  1.118    rillig 		else if (ch == var_end && --depth == 0)
    298  1.118    rillig 			return len;
    299  1.118    rillig 	}
    300   1.45       dsl 
    301  1.118    rillig 	/* Variable end not found, escape the $ */
    302  1.118    rillig 	return 0;
    303   1.45       dsl }
    304   1.45       dsl 
    305  1.122    rillig /*
    306  1.122    rillig  * The .for loop substitutes the items as ${:U<value>...}, which means
    307  1.122    rillig  * that characters that break this syntax must be backslash-escaped.
    308  1.122    rillig  */
    309  1.115    rillig static Boolean
    310  1.115    rillig NeedsEscapes(const char *word, char endc)
    311  1.115    rillig {
    312  1.118    rillig 	const char *p;
    313  1.115    rillig 
    314  1.118    rillig 	for (p = word; *p != '\0'; p++) {
    315  1.118    rillig 		if (*p == ':' || *p == '$' || *p == '\\' || *p == endc)
    316  1.118    rillig 			return TRUE;
    317  1.118    rillig 	}
    318  1.118    rillig 	return FALSE;
    319  1.115    rillig }
    320  1.115    rillig 
    321  1.122    rillig /*
    322  1.122    rillig  * While expanding the body of a .for loop, write the item in the ${:U...}
    323  1.115    rillig  * expression, escaping characters as needed.
    324  1.115    rillig  *
    325  1.122    rillig  * The result is later unescaped by ApplyModifier_Defined.
    326  1.122    rillig  */
    327   1.42       dsl static void
    328  1.127    rillig Buf_AddEscaped(Buffer *cmds, const char *item, char endc)
    329   1.42       dsl {
    330  1.118    rillig 	char ch;
    331  1.118    rillig 
    332  1.127    rillig 	if (!NeedsEscapes(item, endc)) {
    333  1.118    rillig 		Buf_AddStr(cmds, item);
    334  1.118    rillig 		return;
    335  1.118    rillig 	}
    336   1.61    rillig 
    337  1.127    rillig 	/* Escape ':', '$', '\\' and 'endc' - these will be removed later by
    338  1.118    rillig 	 * :U processing, see ApplyModifier_Defined. */
    339  1.118    rillig 	while ((ch = *item++) != '\0') {
    340  1.118    rillig 		if (ch == '$') {
    341  1.118    rillig 			size_t len = for_var_len(item);
    342  1.118    rillig 			if (len != 0) {
    343  1.118    rillig 				Buf_AddBytes(cmds, item - 1, len + 1);
    344  1.118    rillig 				item += len;
    345  1.118    rillig 				continue;
    346  1.118    rillig 			}
    347  1.118    rillig 			Buf_AddByte(cmds, '\\');
    348  1.127    rillig 		} else if (ch == ':' || ch == '\\' || ch == endc)
    349  1.118    rillig 			Buf_AddByte(cmds, '\\');
    350  1.118    rillig 		Buf_AddByte(cmds, ch);
    351  1.118    rillig 	}
    352   1.42       dsl }
    353   1.42       dsl 
    354  1.122    rillig /*
    355  1.123    rillig  * While expanding the body of a .for loop, replace the inner part of an
    356  1.123    rillig  * expression like ${i} or ${i:...} or $(i) or $(i:...) with ":Uvalue".
    357  1.122    rillig  */
    358   1.97    rillig static void
    359  1.127    rillig SubstVarLong(For *f, const char **pp, const char **inout_mark, char endc)
    360   1.97    rillig {
    361  1.118    rillig 	size_t i;
    362  1.118    rillig 	const char *p = *pp;
    363   1.97    rillig 
    364  1.118    rillig 	for (i = 0; i < f->vars.len; i++) {
    365  1.118    rillig 		ForVar *forVar = Vector_Get(&f->vars, i);
    366  1.123    rillig 		char *varname = forVar->name;
    367  1.123    rillig 		size_t varnameLen = forVar->nameLen;
    368  1.118    rillig 
    369  1.123    rillig 		/* XXX: undefined behavior for p if varname is longer than p? */
    370  1.123    rillig 		if (memcmp(p, varname, varnameLen) != 0)
    371  1.118    rillig 			continue;
    372  1.118    rillig 		/* XXX: why test for backslash here? */
    373  1.127    rillig 		if (p[varnameLen] != ':' && p[varnameLen] != endc &&
    374  1.123    rillig 		    p[varnameLen] != '\\')
    375  1.118    rillig 			continue;
    376  1.118    rillig 
    377  1.118    rillig 		/* Found a variable match. Replace with :U<value> */
    378  1.118    rillig 		Buf_AddBytesBetween(&f->curBody, *inout_mark, p);
    379  1.118    rillig 		Buf_AddStr(&f->curBody, ":U");
    380  1.118    rillig 		Buf_AddEscaped(&f->curBody,
    381  1.127    rillig 		    f->items.words[f->sub_next + i], endc);
    382  1.118    rillig 
    383  1.123    rillig 		p += varnameLen;
    384  1.118    rillig 		*inout_mark = p;
    385  1.118    rillig 		break;
    386  1.118    rillig 	}
    387   1.97    rillig 
    388  1.118    rillig 	*pp = p;
    389   1.97    rillig }
    390   1.97    rillig 
    391  1.122    rillig /*
    392  1.122    rillig  * While expanding the body of a .for loop, replace single-character
    393  1.122    rillig  * variable expressions like $i with their ${:U...} expansion.
    394  1.122    rillig  */
    395   1.98    rillig static void
    396  1.123    rillig SubstVarShort(For *f, const char **pp, const char **inout_mark)
    397   1.98    rillig {
    398  1.118    rillig 	const char *p = *pp;
    399  1.123    rillig 	const char ch = *p;
    400  1.118    rillig 	size_t i;
    401  1.118    rillig 
    402  1.118    rillig 	/* Probably a single character name, ignore $$ and stupid ones. */
    403  1.118    rillig 	if (!f->short_var || strchr("}):$", ch) != NULL) {
    404  1.118    rillig 		p++;
    405  1.118    rillig 		*pp = p;
    406  1.118    rillig 		return;
    407  1.118    rillig 	}
    408   1.98    rillig 
    409  1.118    rillig 	for (i = 0; i < f->vars.len; i++) {
    410  1.118    rillig 		ForVar *var = Vector_Get(&f->vars, i);
    411  1.118    rillig 		const char *varname = var->name;
    412  1.118    rillig 		if (varname[0] != ch || varname[1] != '\0')
    413  1.118    rillig 			continue;
    414  1.118    rillig 
    415  1.118    rillig 		/* Found a variable match. Replace with ${:U<value>} */
    416  1.118    rillig 		Buf_AddBytesBetween(&f->curBody, *inout_mark, p);
    417  1.118    rillig 		Buf_AddStr(&f->curBody, "{:U");
    418  1.118    rillig 		Buf_AddEscaped(&f->curBody,
    419  1.118    rillig 		    f->items.words[f->sub_next + i], '}');
    420  1.118    rillig 		Buf_AddByte(&f->curBody, '}');
    421   1.98    rillig 
    422  1.118    rillig 		*inout_mark = ++p;
    423  1.118    rillig 		break;
    424  1.118    rillig 	}
    425   1.98    rillig 
    426  1.118    rillig 	*pp = p;
    427   1.98    rillig }
    428   1.98    rillig 
    429   1.95    rillig /*
    430  1.128    rillig  * Compute the body for the current iteration by copying the unexpanded body,
    431  1.128    rillig  * replacing the expressions for the iteration variables on the way.
    432  1.128    rillig  */
    433  1.128    rillig static void
    434  1.128    rillig ForSubstBody(For *f)
    435  1.128    rillig {
    436  1.128    rillig 	const char *p;
    437  1.128    rillig 	const char *mark;	/* where the last replacement left off */
    438  1.128    rillig 
    439  1.128    rillig 	Buf_Empty(&f->curBody);
    440  1.128    rillig 
    441  1.128    rillig 	mark = f->body.data;
    442  1.128    rillig 	for (p = mark; (p = strchr(p, '$')) != NULL;) {
    443  1.128    rillig 		if (p[1] == '{' || p[1] == '(') {
    444  1.128    rillig 			p += 2;
    445  1.128    rillig 			SubstVarLong(f, &p, &mark, p[-1] == '{' ? '}' : ')');
    446  1.128    rillig 		} else if (p[1] != '\0') {
    447  1.128    rillig 			p++;
    448  1.128    rillig 			SubstVarShort(f, &p, &mark);
    449  1.128    rillig 		} else
    450  1.128    rillig 			break;
    451  1.128    rillig 	}
    452  1.128    rillig 
    453  1.128    rillig 	Buf_AddBytesBetween(&f->curBody, mark, f->body.data + f->body.len);
    454  1.128    rillig }
    455  1.128    rillig 
    456  1.128    rillig /*
    457   1.95    rillig  * Scan the for loop body and replace references to the loop variables
    458   1.95    rillig  * with variable references that expand to the required text.
    459   1.95    rillig  *
    460  1.120    rillig  * Using variable expressions ensures that the .for loop can't generate
    461   1.95    rillig  * syntax, and that the later parsing will still see a variable.
    462  1.120    rillig  * This code assumes that the variable with the empty name will never be
    463  1.120    rillig  * defined, see unit-tests/varname-empty.mk for more details.
    464   1.95    rillig  *
    465   1.95    rillig  * The detection of substitutions of the loop control variable is naive.
    466   1.95    rillig  * Many of the modifiers use \ to escape $ (not $) so it is possible
    467   1.95    rillig  * to contrive a makefile where an unwanted substitution happens.
    468   1.95    rillig  */
    469   1.43       dsl static char *
    470  1.119    rillig ForReadMore(void *v_arg, size_t *out_len)
    471    1.1       cgd {
    472  1.118    rillig 	For *f = v_arg;
    473  1.118    rillig 
    474  1.124    rillig 	if (f->sub_next == f->items.len) {
    475  1.118    rillig 		/* No more iterations */
    476  1.118    rillig 		For_Free(f);
    477  1.118    rillig 		return NULL;
    478  1.118    rillig 	}
    479  1.118    rillig 
    480  1.128    rillig 	ForSubstBody(f);
    481  1.128    rillig 	DEBUG1(FOR, "For: loop body:\n%s", f->curBody.data);
    482  1.118    rillig 	f->sub_next += f->vars.len;
    483  1.118    rillig 
    484  1.128    rillig 	*out_len = f->curBody.len;
    485  1.128    rillig 	return f->curBody.data;
    486   1.43       dsl }
    487    1.7  christos 
    488  1.120    rillig /* Run the .for loop, imitating the actions of an include file. */
    489   1.43       dsl void
    490   1.43       dsl For_Run(int lineno)
    491   1.54    rillig {
    492  1.118    rillig 	For *f = accumFor;
    493  1.118    rillig 	accumFor = NULL;
    494    1.1       cgd 
    495  1.118    rillig 	if (f->items.len == 0) {
    496  1.118    rillig 		/*
    497  1.118    rillig 		 * Nothing to expand - possibly due to an earlier syntax
    498  1.118    rillig 		 * error.
    499  1.118    rillig 		 */
    500  1.118    rillig 		For_Free(f);
    501  1.118    rillig 		return;
    502  1.118    rillig 	}
    503   1.54    rillig 
    504  1.119    rillig 	Parse_SetInput(NULL, lineno, -1, ForReadMore, f);
    505    1.1       cgd }
    506