Home | History | Annotate | Line # | Download | only in make
for.c revision 1.97
      1  1.97    rillig /*	$NetBSD: for.c,v 1.97 2020/10/25 13:45:33 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.1       cgd #include    "make.h"
     61   1.1       cgd 
     62  1.83    rillig /*	"@(#)for.c	8.1 (Berkeley) 6/6/93"	*/
     63  1.97    rillig MAKE_RCSID("$NetBSD: for.c,v 1.97 2020/10/25 13:45:33 rillig Exp $");
     64  1.83    rillig 
     65  1.94    rillig typedef enum ForEscapes {
     66  1.75    rillig     FOR_SUB_ESCAPE_CHAR = 0x0001,
     67  1.75    rillig     FOR_SUB_ESCAPE_BRACE = 0x0002,
     68  1.75    rillig     FOR_SUB_ESCAPE_PAREN = 0x0004
     69  1.75    rillig } ForEscapes;
     70  1.42       dsl 
     71  1.63    rillig static int forLevel = 0;	/* Nesting level */
     72   1.1       cgd 
     73  1.96    rillig /* One of the variables to the left of the "in" in a .for loop. */
     74  1.96    rillig typedef struct ForVar {
     75  1.96    rillig     char *name;
     76  1.96    rillig     size_t len;
     77  1.96    rillig } ForVar;
     78  1.96    rillig 
     79  1.96    rillig /* One of the items to the right of the "in" in a .for loop. */
     80  1.96    rillig typedef struct ForItem {
     81  1.96    rillig     char *value;		/* unexpanded */
     82  1.96    rillig     ForEscapes escapes;
     83  1.96    rillig } ForItem;
     84  1.96    rillig 
     85   1.1       cgd /*
     86   1.1       cgd  * State of a for loop.
     87   1.1       cgd  */
     88  1.86    rillig typedef struct For {
     89  1.63    rillig     Buffer buf;			/* Body of loop */
     90  1.96    rillig     Vector /* of ForVar */ vars;
     91  1.96    rillig     Vector /* of ForItem */ items;	/* Substitution items */
     92  1.63    rillig     char *parse_buf;
     93  1.69    rillig     /* Is any of the names 1 character long? If so, when the variable values
     94  1.69    rillig      * are substituted, the parser must handle $V expressions as well, not
     95  1.69    rillig      * only ${V} and $(V). */
     96  1.69    rillig     Boolean short_var;
     97  1.93    rillig     unsigned int sub_next;
     98   1.2       jtc } For;
     99   1.1       cgd 
    100  1.63    rillig static For *accumFor;		/* Loop being accumulated */
    101   1.1       cgd 
    102  1.96    rillig static void
    103  1.96    rillig ForAddVar(For *f, const char *name, size_t len)
    104  1.96    rillig {
    105  1.96    rillig     ForVar *var = Vector_Push(&f->vars);
    106  1.96    rillig     var->name = bmake_strldup(name, len);
    107  1.96    rillig     var->len = len;
    108  1.96    rillig }
    109  1.96    rillig 
    110  1.96    rillig static void
    111  1.96    rillig ForVarDone(ForVar *var)
    112  1.96    rillig {
    113  1.96    rillig     free(var->name);
    114  1.96    rillig }
    115  1.96    rillig 
    116  1.96    rillig static void
    117  1.96    rillig ForAddItem(For *f, const char *value, ForEscapes escapes)
    118  1.96    rillig {
    119  1.96    rillig     ForItem *item = Vector_Push(&f->items);
    120  1.96    rillig     item->value = bmake_strdup(value);
    121  1.96    rillig     item->escapes = escapes;
    122  1.96    rillig }
    123  1.96    rillig 
    124  1.96    rillig static void
    125  1.96    rillig ForItemDone(ForItem *item)
    126  1.96    rillig {
    127  1.96    rillig     free(item->value);
    128  1.96    rillig }
    129   1.7  christos 
    130  1.43       dsl static void
    131  1.43       dsl For_Free(For *arg)
    132  1.43       dsl {
    133  1.46       dsl     Buf_Destroy(&arg->buf, TRUE);
    134  1.96    rillig 
    135  1.96    rillig     while (arg->vars.len > 0)
    136  1.96    rillig         ForVarDone(Vector_Pop(&arg->vars));
    137  1.96    rillig     Vector_Done(&arg->vars);
    138  1.96    rillig 
    139  1.96    rillig     while (arg->items.len > 0)
    140  1.96    rillig 	ForItemDone(Vector_Pop(&arg->items));
    141  1.96    rillig     Vector_Done(&arg->items);
    142  1.96    rillig 
    143  1.43       dsl     free(arg->parse_buf);
    144  1.43       dsl 
    145  1.43       dsl     free(arg);
    146  1.43       dsl }
    147  1.43       dsl 
    148  1.70    rillig /* Evaluate the for loop in the passed line. The line looks like this:
    149  1.70    rillig  *	.for <varname...> in <value...>
    150   1.1       cgd  *
    151  1.13       wiz  * Input:
    152  1.13       wiz  *	line		Line to parse
    153  1.13       wiz  *
    154   1.1       cgd  * Results:
    155  1.35       dsl  *      0: Not a .for statement, parse the line
    156  1.35       dsl  *	1: We found a for loop
    157  1.35       dsl  *     -1: A .for statement with a bad syntax error, discard.
    158   1.1       cgd  */
    159   1.1       cgd int
    160  1.73    rillig For_Eval(const char *line)
    161   1.1       cgd {
    162  1.43       dsl     For *new_for;
    163  1.72    rillig     const char *ptr;
    164  1.67    rillig     Words words;
    165  1.33       dsl 
    166  1.43       dsl     /* Skip the '.' and any following whitespace */
    167  1.92    rillig     ptr = line + 1;
    168  1.92    rillig     cpp_skip_whitespace(&ptr);
    169  1.43       dsl 
    170  1.32       dsl     /*
    171  1.32       dsl      * If we are not in a for loop quickly determine if the statement is
    172  1.32       dsl      * a for.
    173  1.32       dsl      */
    174  1.32       dsl     if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
    175  1.79    rillig 	!ch_isspace(ptr[3])) {
    176  1.63    rillig 	if (ptr[0] == 'e' && strncmp(ptr + 1, "ndfor", 5) == 0) {
    177  1.32       dsl 	    Parse_Error(PARSE_FATAL, "for-less endfor");
    178  1.32       dsl 	    return -1;
    179  1.32       dsl 	}
    180  1.32       dsl 	return 0;
    181  1.32       dsl     }
    182  1.32       dsl     ptr += 3;
    183   1.1       cgd 
    184  1.32       dsl     /*
    185  1.32       dsl      * we found a for loop, and now we are going to parse it.
    186  1.32       dsl      */
    187   1.1       cgd 
    188  1.43       dsl     new_for = bmake_malloc(sizeof *new_for);
    189  1.71    rillig     Buf_Init(&new_for->buf, 0);
    190  1.96    rillig     Vector_Init(&new_for->vars, sizeof(ForVar));
    191  1.96    rillig     Vector_Init(&new_for->items, sizeof(ForItem));
    192  1.71    rillig     new_for->parse_buf = NULL;
    193  1.71    rillig     new_for->short_var = FALSE;
    194  1.71    rillig     new_for->sub_next = 0;
    195  1.43       dsl 
    196  1.33       dsl     /* Grab the variables. Terminate on "in". */
    197  1.93    rillig     for (;;) {
    198  1.72    rillig 	size_t len;
    199  1.72    rillig 
    200  1.92    rillig 	cpp_skip_whitespace(&ptr);
    201  1.33       dsl 	if (*ptr == '\0') {
    202  1.33       dsl 	    Parse_Error(PARSE_FATAL, "missing `in' in for");
    203  1.43       dsl 	    For_Free(new_for);
    204  1.33       dsl 	    return -1;
    205  1.33       dsl 	}
    206  1.72    rillig 
    207  1.79    rillig 	for (len = 1; ptr[len] && !ch_isspace(ptr[len]); len++)
    208  1.33       dsl 	    continue;
    209  1.33       dsl 	if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
    210  1.33       dsl 	    ptr += 2;
    211  1.33       dsl 	    break;
    212  1.33       dsl 	}
    213  1.43       dsl 	if (len == 1)
    214  1.69    rillig 	    new_for->short_var = TRUE;
    215  1.69    rillig 
    216  1.96    rillig 	ForAddVar(new_for, ptr, len);
    217  1.72    rillig 	ptr += len;
    218  1.32       dsl     }
    219   1.1       cgd 
    220  1.96    rillig     if (new_for->vars.len == 0) {
    221  1.32       dsl 	Parse_Error(PARSE_FATAL, "no iteration variables in for");
    222  1.43       dsl 	For_Free(new_for);
    223  1.32       dsl 	return -1;
    224  1.32       dsl     }
    225   1.4  christos 
    226  1.92    rillig     cpp_skip_whitespace(&ptr);
    227  1.32       dsl 
    228  1.32       dsl     /*
    229  1.70    rillig      * Make a list with the remaining words.
    230  1.70    rillig      * The values are later substituted as ${:U<value>...} so we must
    231  1.70    rillig      * backslash-escape characters that break that syntax.
    232  1.44       dsl      * Variables are fully expanded - so it is safe for escape $.
    233  1.44       dsl      * We can't do the escapes here - because we don't know whether
    234  1.70    rillig      * we will be substituting into ${...} or $(...).
    235  1.32       dsl      */
    236  1.72    rillig     {
    237  1.85    rillig 	char *items;
    238  1.85    rillig 	(void)Var_Subst(ptr, VAR_GLOBAL, VARE_WANTRES, &items);
    239  1.85    rillig 	/* TODO: handle errors */
    240  1.72    rillig 	words = Str_Words(items, FALSE);
    241  1.72    rillig 	free(items);
    242  1.72    rillig     }
    243  1.54    rillig 
    244  1.67    rillig     {
    245  1.72    rillig 	size_t n;
    246  1.65    rillig 
    247  1.67    rillig 	for (n = 0; n < words.len; n++) {
    248  1.75    rillig 	    ForEscapes escapes;
    249  1.72    rillig 	    char ch;
    250  1.72    rillig 
    251  1.67    rillig 	    ptr = words.words[n];
    252  1.72    rillig 	    if (ptr[0] == '\0')
    253  1.49       sjg 		continue;
    254  1.49       sjg 	    escapes = 0;
    255  1.49       sjg 	    while ((ch = *ptr++)) {
    256  1.63    rillig 		switch (ch) {
    257  1.49       sjg 		case ':':
    258  1.49       sjg 		case '$':
    259  1.49       sjg 		case '\\':
    260  1.49       sjg 		    escapes |= FOR_SUB_ESCAPE_CHAR;
    261  1.49       sjg 		    break;
    262  1.49       sjg 		case ')':
    263  1.49       sjg 		    escapes |= FOR_SUB_ESCAPE_PAREN;
    264  1.49       sjg 		    break;
    265  1.72    rillig 		case '}':
    266  1.49       sjg 		    escapes |= FOR_SUB_ESCAPE_BRACE;
    267  1.49       sjg 		    break;
    268  1.49       sjg 		}
    269  1.49       sjg 	    }
    270  1.96    rillig 	    ForAddItem(new_for, words.words[n], escapes);
    271  1.39       dsl 	}
    272  1.72    rillig     }
    273  1.72    rillig 
    274  1.72    rillig     Words_Free(words);
    275   1.1       cgd 
    276  1.72    rillig     {
    277  1.95    rillig 	size_t nitems, nvars;
    278   1.7  christos 
    279  1.96    rillig 	if ((nitems = new_for->items.len) > 0 &&
    280  1.96    rillig 	    nitems % (nvars = new_for->vars.len)) {
    281  1.49       sjg 	    Parse_Error(PARSE_FATAL,
    282  1.65    rillig 			"Wrong number of words (%zu) in .for substitution list"
    283  1.95    rillig 			" with %zu vars", nitems, nvars);
    284  1.49       sjg 	    /*
    285  1.49       sjg 	     * Return 'success' so that the body of the .for loop is
    286  1.49       sjg 	     * accumulated.
    287  1.49       sjg 	     * Remove all items so that the loop doesn't iterate.
    288  1.49       sjg 	     */
    289  1.96    rillig 	    while (new_for->items.len > 0)
    290  1.96    rillig 		ForItemDone(Vector_Pop(&new_for->items));
    291  1.49       sjg 	}
    292  1.32       dsl     }
    293  1.32       dsl 
    294  1.43       dsl     accumFor = new_for;
    295  1.32       dsl     forLevel = 1;
    296  1.32       dsl     return 1;
    297  1.32       dsl }
    298   1.7  christos 
    299  1.35       dsl /*
    300  1.35       dsl  * Add another line to a .for loop.
    301  1.81    rillig  * Returns FALSE when the matching .endfor is reached.
    302  1.35       dsl  */
    303  1.81    rillig Boolean
    304  1.73    rillig For_Accum(const char *line)
    305  1.32       dsl {
    306  1.73    rillig     const char *ptr = line;
    307  1.26       dsl 
    308  1.26       dsl     if (*ptr == '.') {
    309  1.92    rillig 	ptr++;
    310  1.92    rillig 	cpp_skip_whitespace(&ptr);
    311   1.1       cgd 
    312  1.79    rillig 	if (strncmp(ptr, "endfor", 6) == 0 && (ch_isspace(ptr[6]) || !ptr[6])) {
    313  1.89    rillig 	    DEBUG1(FOR, "For: end for %d\n", forLevel);
    314  1.32       dsl 	    if (--forLevel <= 0)
    315  1.81    rillig 		return FALSE;
    316  1.79    rillig 	} else if (strncmp(ptr, "for", 3) == 0 && ch_isspace(ptr[3])) {
    317   1.1       cgd 	    forLevel++;
    318  1.89    rillig 	    DEBUG1(FOR, "For: new loop %d\n", forLevel);
    319   1.1       cgd 	}
    320   1.1       cgd     }
    321   1.1       cgd 
    322  1.59    rillig     Buf_AddStr(&accumFor->buf, line);
    323  1.46       dsl     Buf_AddByte(&accumFor->buf, '\n');
    324  1.81    rillig     return TRUE;
    325   1.1       cgd }
    326   1.1       cgd 
    327  1.42       dsl 
    328  1.59    rillig static size_t
    329  1.45       dsl for_var_len(const char *var)
    330  1.45       dsl {
    331  1.45       dsl     char ch, var_start, var_end;
    332  1.61    rillig     int depth;
    333  1.61    rillig     size_t len;
    334  1.45       dsl 
    335  1.45       dsl     var_start = *var;
    336  1.45       dsl     if (var_start == 0)
    337  1.45       dsl 	/* just escape the $ */
    338  1.45       dsl 	return 0;
    339  1.45       dsl 
    340  1.45       dsl     if (var_start == '(')
    341  1.45       dsl 	var_end = ')';
    342  1.45       dsl     else if (var_start == '{')
    343  1.45       dsl 	var_end = '}';
    344  1.45       dsl     else
    345  1.45       dsl 	/* Single char variable */
    346  1.45       dsl 	return 1;
    347  1.45       dsl 
    348  1.61    rillig     depth = 1;
    349  1.45       dsl     for (len = 1; (ch = var[len++]) != 0;) {
    350  1.45       dsl 	if (ch == var_start)
    351  1.45       dsl 	    depth++;
    352  1.45       dsl 	else if (ch == var_end && --depth == 0)
    353  1.45       dsl 	    return len;
    354  1.45       dsl     }
    355  1.45       dsl 
    356  1.45       dsl     /* Variable end not found, escape the $ */
    357  1.45       dsl     return 0;
    358  1.45       dsl }
    359  1.45       dsl 
    360  1.42       dsl static void
    361  1.96    rillig for_substitute(Buffer *cmds, ForItem *forItem, char ech)
    362  1.42       dsl {
    363  1.96    rillig     const char *item = forItem->value;
    364  1.96    rillig     ForEscapes escapes = forItem->escapes;
    365  1.61    rillig     char ch;
    366  1.61    rillig 
    367  1.42       dsl     /* If there were no escapes, or the only escape is the other variable
    368  1.42       dsl      * terminator, then just substitute the full string */
    369  1.93    rillig     if (!(escapes & (ech == ')' ? ~(unsigned)FOR_SUB_ESCAPE_BRACE
    370  1.93    rillig 				: ~(unsigned)FOR_SUB_ESCAPE_PAREN))) {
    371  1.59    rillig 	Buf_AddStr(cmds, item);
    372  1.42       dsl 	return;
    373  1.42       dsl     }
    374  1.42       dsl 
    375  1.74    rillig     /* Escape ':', '$', '\\' and 'ech' - these will be removed later by
    376  1.74    rillig      * :U processing, see ApplyModifier_Defined. */
    377  1.95    rillig     while ((ch = *item++) != '\0') {
    378  1.45       dsl 	if (ch == '$') {
    379  1.59    rillig 	    size_t len = for_var_len(item);
    380  1.45       dsl 	    if (len != 0) {
    381  1.62    rillig 		Buf_AddBytes(cmds, item - 1, len + 1);
    382  1.45       dsl 		item += len;
    383  1.45       dsl 		continue;
    384  1.45       dsl 	    }
    385  1.45       dsl 	    Buf_AddByte(cmds, '\\');
    386  1.45       dsl 	} else if (ch == ':' || ch == '\\' || ch == ech)
    387  1.42       dsl 	    Buf_AddByte(cmds, '\\');
    388  1.42       dsl 	Buf_AddByte(cmds, ch);
    389  1.42       dsl     }
    390  1.42       dsl }
    391  1.42       dsl 
    392  1.97    rillig static void
    393  1.97    rillig SubstVarLong(For *arg, const char **inout_cp, const char **inout_cmd_cp,
    394  1.97    rillig 	     Buffer *cmds, char ech)
    395  1.97    rillig {
    396  1.97    rillig     size_t i;
    397  1.97    rillig     const char *cp = *inout_cp;
    398  1.97    rillig     const char *cmd_cp = *inout_cmd_cp;
    399  1.97    rillig 
    400  1.97    rillig     for (i = 0; i < arg->vars.len; i++) {
    401  1.97    rillig 	ForVar *forVar = Vector_Get(&arg->vars, i);
    402  1.97    rillig 	char *var = forVar->name;
    403  1.97    rillig 	size_t vlen = forVar->len;
    404  1.97    rillig 
    405  1.97    rillig 	/* XXX: undefined behavior for cp if vlen is longer than cp? */
    406  1.97    rillig 	if (memcmp(cp, var, vlen) != 0)
    407  1.97    rillig 	    continue;
    408  1.97    rillig 	/* XXX: why test for backslash here? */
    409  1.97    rillig 	if (cp[vlen] != ':' && cp[vlen] != ech && cp[vlen] != '\\')
    410  1.97    rillig 	    continue;
    411  1.97    rillig 
    412  1.97    rillig 	/* Found a variable match. Replace with :U<value> */
    413  1.97    rillig 	Buf_AddBytesBetween(cmds, cmd_cp, cp);
    414  1.97    rillig 	Buf_AddStr(cmds, ":U");
    415  1.97    rillig 	cp += vlen;
    416  1.97    rillig 	cmd_cp = cp;
    417  1.97    rillig 	for_substitute(cmds, Vector_Get(&arg->items, arg->sub_next + i), ech);
    418  1.97    rillig 	break;
    419  1.97    rillig     }
    420  1.97    rillig 
    421  1.97    rillig     *inout_cp = cp;
    422  1.97    rillig     *inout_cmd_cp = cmd_cp;
    423  1.97    rillig }
    424  1.97    rillig 
    425  1.95    rillig /*
    426  1.95    rillig  * Scan the for loop body and replace references to the loop variables
    427  1.95    rillig  * with variable references that expand to the required text.
    428  1.95    rillig  *
    429  1.95    rillig  * Using variable expansions ensures that the .for loop can't generate
    430  1.95    rillig  * syntax, and that the later parsing will still see a variable.
    431  1.95    rillig  * We assume that the null variable will never be defined.
    432  1.95    rillig  *
    433  1.95    rillig  * The detection of substitutions of the loop control variable is naive.
    434  1.95    rillig  * Many of the modifiers use \ to escape $ (not $) so it is possible
    435  1.95    rillig  * to contrive a makefile where an unwanted substitution happens.
    436  1.95    rillig  */
    437  1.43       dsl static char *
    438  1.68    rillig ForIterate(void *v_arg, size_t *ret_len)
    439   1.1       cgd {
    440  1.43       dsl     For *arg = v_arg;
    441  1.77    rillig     const char *cp;
    442  1.77    rillig     const char *cmd_cp;
    443  1.77    rillig     const char *body_end;
    444  1.39       dsl     Buffer cmds;
    445  1.76    rillig     char *cmds_str;
    446  1.61    rillig     size_t cmd_len;
    447   1.7  christos 
    448  1.96    rillig     if (arg->sub_next + arg->vars.len > arg->items.len) {
    449  1.43       dsl 	/* No more iterations */
    450  1.43       dsl 	For_Free(arg);
    451  1.43       dsl 	return NULL;
    452  1.43       dsl     }
    453   1.1       cgd 
    454  1.43       dsl     free(arg->parse_buf);
    455  1.43       dsl     arg->parse_buf = NULL;
    456   1.7  christos 
    457  1.62    rillig     cmd_cp = Buf_GetAll(&arg->buf, &cmd_len);
    458  1.59    rillig     body_end = cmd_cp + cmd_len;
    459  1.62    rillig     Buf_Init(&cmds, cmd_len + 256);
    460  1.43       dsl     for (cp = cmd_cp; (cp = strchr(cp, '$')) != NULL;) {
    461  1.95    rillig 	char ch, ech;
    462  1.95    rillig 	unsigned int i;
    463  1.43       dsl 	ch = *++cp;
    464  1.53  riastrad 	if ((ch == '(' && (ech = ')', 1)) || (ch == '{' && (ech = '}', 1))) {
    465  1.43       dsl 	    cp++;
    466  1.43       dsl 	    /* Check variable name against the .for loop variables */
    467  1.97    rillig 	    SubstVarLong(arg, &cp, &cmd_cp, &cmds, ech);
    468  1.43       dsl 	    continue;
    469  1.43       dsl 	}
    470  1.95    rillig 	if (ch == '\0')
    471  1.43       dsl 	    break;
    472  1.97    rillig 
    473  1.43       dsl 	/* Probably a single character name, ignore $$ and stupid ones. {*/
    474  1.43       dsl 	if (!arg->short_var || strchr("}):$", ch) != NULL) {
    475  1.43       dsl 	    cp++;
    476  1.43       dsl 	    continue;
    477  1.43       dsl 	}
    478  1.96    rillig 	for (i = 0; i < arg->vars.len; i++) {
    479  1.96    rillig 	    ForVar *forVar = Vector_Get(&arg->vars, i);
    480  1.96    rillig 	    char *var = forVar->name;
    481  1.43       dsl 	    if (var[0] != ch || var[1] != 0)
    482  1.43       dsl 		continue;
    483  1.43       dsl 	    /* Found a variable match. Replace with ${:U<value>} */
    484  1.59    rillig 	    Buf_AddBytesBetween(&cmds, cmd_cp, cp);
    485  1.59    rillig 	    Buf_AddStr(&cmds, "{:U");
    486  1.43       dsl 	    cmd_cp = ++cp;
    487  1.96    rillig 	    for_substitute(&cmds, Vector_Get(&arg->items, arg->sub_next + i), '}');
    488  1.59    rillig 	    Buf_AddByte(&cmds, '}');
    489  1.43       dsl 	    break;
    490  1.39       dsl 	}
    491  1.43       dsl     }
    492  1.59    rillig     Buf_AddBytesBetween(&cmds, cmd_cp, body_end);
    493  1.43       dsl 
    494  1.87    rillig     *ret_len = Buf_Len(&cmds);
    495  1.76    rillig     cmds_str = Buf_Destroy(&cmds, FALSE);
    496  1.89    rillig     DEBUG1(FOR, "For: loop body:\n%s", cmds_str);
    497  1.39       dsl 
    498  1.96    rillig     arg->sub_next += arg->vars.len;
    499  1.43       dsl 
    500  1.76    rillig     arg->parse_buf = cmds_str;
    501  1.76    rillig     return cmds_str;
    502  1.43       dsl }
    503   1.7  christos 
    504  1.60    rillig /* Run the for loop, imitating the actions of an include file. */
    505  1.43       dsl void
    506  1.43       dsl For_Run(int lineno)
    507  1.54    rillig {
    508  1.43       dsl     For *arg;
    509  1.54    rillig 
    510  1.43       dsl     arg = accumFor;
    511  1.43       dsl     accumFor = NULL;
    512   1.1       cgd 
    513  1.96    rillig     if (arg->items.len == 0) {
    514  1.58    rillig 	/* Nothing to expand - possibly due to an earlier syntax error. */
    515  1.58    rillig 	For_Free(arg);
    516  1.58    rillig 	return;
    517  1.43       dsl     }
    518  1.54    rillig 
    519  1.68    rillig     Parse_SetInput(NULL, lineno, -1, ForIterate, arg);
    520   1.1       cgd }
    521