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