Home | History | Annotate | Line # | Download | only in make
for.c revision 1.49
      1  1.49       sjg /*	$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg 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.17      ross #ifndef MAKE_NATIVE
     33  1.49       sjg static char rcsid[] = "$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $";
     34   1.6     lukem #else
     35   1.5  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37   1.3  christos #if 0
     38   1.4  christos static char sccsid[] = "@(#)for.c	8.1 (Berkeley) 6/6/93";
     39   1.3  christos #else
     40  1.49       sjg __RCSID("$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $");
     41   1.3  christos #endif
     42   1.1       cgd #endif /* not lint */
     43   1.6     lukem #endif
     44   1.1       cgd 
     45   1.1       cgd /*-
     46   1.1       cgd  * for.c --
     47   1.1       cgd  *	Functions to handle loops in a makefile.
     48   1.1       cgd  *
     49   1.1       cgd  * Interface:
     50   1.1       cgd  *	For_Eval 	Evaluate the loop in the passed line.
     51   1.1       cgd  *	For_Run		Run accumulated loop
     52   1.1       cgd  *
     53   1.1       cgd  */
     54   1.1       cgd 
     55  1.13       wiz #include    <assert.h>
     56   1.1       cgd #include    <ctype.h>
     57  1.13       wiz 
     58   1.1       cgd #include    "make.h"
     59   1.1       cgd #include    "hash.h"
     60   1.1       cgd #include    "dir.h"
     61   1.1       cgd #include    "buf.h"
     62  1.38       dsl #include    "strlist.h"
     63   1.1       cgd 
     64  1.44       dsl #define FOR_SUB_ESCAPE_CHAR  1
     65  1.42       dsl #define FOR_SUB_ESCAPE_BRACE 2
     66  1.42       dsl #define FOR_SUB_ESCAPE_PAREN 4
     67  1.42       dsl 
     68   1.1       cgd /*
     69   1.1       cgd  * For statements are of the form:
     70   1.1       cgd  *
     71   1.1       cgd  * .for <variable> in <varlist>
     72   1.1       cgd  * ...
     73   1.1       cgd  * .endfor
     74   1.1       cgd  *
     75   1.1       cgd  * The trick is to look for the matching end inside for for loop
     76   1.1       cgd  * To do that, we count the current nesting level of the for loops.
     77   1.1       cgd  * and the .endfor statements, accumulating all the statements between
     78   1.4  christos  * the initial .for loop and the matching .endfor;
     79   1.1       cgd  * then we evaluate the for loop for each variable in the varlist.
     80   1.7  christos  *
     81   1.7  christos  * Note that any nested fors are just passed through; they get handled
     82   1.7  christos  * recursively in For_Eval when we're expanding the enclosing for in
     83   1.7  christos  * For_Run.
     84   1.1       cgd  */
     85   1.1       cgd 
     86   1.1       cgd static int  	  forLevel = 0;  	/* Nesting level	*/
     87   1.1       cgd 
     88   1.1       cgd /*
     89   1.1       cgd  * State of a for loop.
     90   1.1       cgd  */
     91   1.2       jtc typedef struct _For {
     92   1.7  christos     Buffer	  buf;			/* Body of loop		*/
     93  1.38       dsl     strlist_t     vars;			/* Iteration variables	*/
     94  1.38       dsl     strlist_t     items;		/* Substitution items */
     95  1.43       dsl     char          *parse_buf;
     96  1.43       dsl     int           short_var;
     97  1.43       dsl     int           sub_next;
     98   1.2       jtc } For;
     99   1.1       cgd 
    100  1.43       dsl static For        *accumFor;            /* Loop being accumulated */
    101   1.1       cgd 
    102   1.1       cgd 
    103   1.7  christos 
    105  1.33       dsl static char *
    106  1.33       dsl make_str(const char *ptr, int len)
    107  1.33       dsl {
    108  1.33       dsl 	char *new_ptr;
    109  1.33       dsl 
    110  1.33       dsl 	new_ptr = bmake_malloc(len + 1);
    111  1.33       dsl 	memcpy(new_ptr, ptr, len);
    112  1.33       dsl 	new_ptr[len] = 0;
    113  1.33       dsl 	return new_ptr;
    114  1.33       dsl }
    115  1.43       dsl 
    116  1.43       dsl static void
    117  1.43       dsl For_Free(For *arg)
    118  1.46       dsl {
    119  1.43       dsl     Buf_Destroy(&arg->buf, TRUE);
    120  1.43       dsl     strlist_clean(&arg->vars);
    121  1.43       dsl     strlist_clean(&arg->items);
    122  1.43       dsl     free(arg->parse_buf);
    123  1.43       dsl 
    124  1.43       dsl     free(arg);
    125  1.43       dsl }
    126   1.7  christos 
    127   1.7  christos /*-
    128   1.1       cgd  *-----------------------------------------------------------------------
    129   1.1       cgd  * For_Eval --
    130   1.1       cgd  *	Evaluate the for loop in the passed line. The line
    131   1.1       cgd  *	looks like this:
    132   1.1       cgd  *	    .for <variable> in <varlist>
    133  1.13       wiz  *
    134  1.13       wiz  * Input:
    135  1.13       wiz  *	line		Line to parse
    136   1.1       cgd  *
    137  1.35       dsl  * Results:
    138  1.35       dsl  *      0: Not a .for statement, parse the line
    139  1.35       dsl  *	1: We found a for loop
    140   1.1       cgd  *     -1: A .for statement with a bad syntax error, discard.
    141   1.1       cgd  *
    142   1.1       cgd  * Side Effects:
    143   1.1       cgd  *	None.
    144   1.1       cgd  *
    145   1.1       cgd  *-----------------------------------------------------------------------
    146   1.1       cgd  */
    147  1.13       wiz int
    148   1.1       cgd For_Eval(char *line)
    149  1.43       dsl {
    150  1.33       dsl     For *new_for;
    151  1.33       dsl     char *ptr = line, *sub;
    152  1.39       dsl     int len;
    153  1.42       dsl     int escapes;
    154  1.49       sjg     unsigned char ch;
    155  1.49       sjg     char **words, *word_buf;
    156  1.33       dsl     int n, nwords;
    157  1.43       dsl 
    158  1.32       dsl     /* Skip the '.' and any following whitespace */
    159  1.32       dsl     for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
    160  1.43       dsl 	continue;
    161  1.32       dsl 
    162  1.32       dsl     /*
    163  1.32       dsl      * If we are not in a for loop quickly determine if the statement is
    164  1.32       dsl      * a for.
    165  1.32       dsl      */
    166  1.32       dsl     if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
    167  1.32       dsl 	    !isspace((unsigned char) ptr[3])) {
    168  1.32       dsl 	if (ptr[0] == 'e' && strncmp(ptr+1, "ndfor", 5) == 0) {
    169  1.32       dsl 	    Parse_Error(PARSE_FATAL, "for-less endfor");
    170  1.32       dsl 	    return -1;
    171  1.32       dsl 	}
    172  1.32       dsl 	return 0;
    173  1.32       dsl     }
    174   1.1       cgd     ptr += 3;
    175  1.32       dsl 
    176  1.32       dsl     /*
    177  1.32       dsl      * we found a for loop, and now we are going to parse it.
    178   1.1       cgd      */
    179  1.43       dsl 
    180  1.43       dsl     new_for = bmake_malloc(sizeof *new_for);
    181  1.43       dsl     memset(new_for, 0, sizeof *new_for);
    182  1.33       dsl 
    183  1.33       dsl     /* Grab the variables. Terminate on "in". */
    184  1.32       dsl     for (;; ptr += len) {
    185  1.32       dsl 	while (*ptr && isspace((unsigned char) *ptr))
    186  1.33       dsl 	    ptr++;
    187  1.33       dsl 	if (*ptr == '\0') {
    188  1.43       dsl 	    Parse_Error(PARSE_FATAL, "missing `in' in for");
    189  1.33       dsl 	    For_Free(new_for);
    190  1.33       dsl 	    return -1;
    191  1.33       dsl 	}
    192  1.33       dsl 	for (len = 1; ptr[len] && !isspace((unsigned char)ptr[len]); len++)
    193  1.33       dsl 	    continue;
    194  1.33       dsl 	if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
    195  1.33       dsl 	    ptr += 2;
    196  1.33       dsl 	    break;
    197  1.43       dsl 	}
    198  1.43       dsl 	if (len == 1)
    199  1.43       dsl 	    new_for->short_var = 1;
    200  1.32       dsl 	strlist_add_str(&new_for->vars, make_str(ptr, len), len);
    201   1.1       cgd     }
    202  1.43       dsl 
    203  1.32       dsl     if (strlist_num(&new_for->vars) == 0) {
    204  1.43       dsl 	Parse_Error(PARSE_FATAL, "no iteration variables in for");
    205  1.32       dsl 	For_Free(new_for);
    206  1.32       dsl 	return -1;
    207   1.4  christos     }
    208  1.32       dsl 
    209  1.32       dsl     while (*ptr && isspace((unsigned char) *ptr))
    210  1.32       dsl 	ptr++;
    211  1.32       dsl 
    212  1.32       dsl     /*
    213  1.42       dsl      * Make a list with the remaining words
    214  1.44       dsl      * The values are substituted as ${:U<value>...} so we must \ escape
    215  1.44       dsl      * characters that break that syntax.
    216  1.44       dsl      * Variables are fully expanded - so it is safe for escape $.
    217  1.44       dsl      * We can't do the escapes here - because we don't know whether
    218  1.32       dsl      * we are substuting into ${...} or $(...).
    219  1.32       dsl      */
    220   1.4  christos     sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
    221  1.49       sjg 
    222  1.49       sjg     /*
    223  1.49       sjg      * Split into words allowing for quoted strings.
    224  1.49       sjg      */
    225  1.49       sjg     words = brk_string(sub, &nwords, FALSE, &word_buf);
    226  1.49       sjg 
    227  1.49       sjg     free(sub);
    228  1.49       sjg 
    229  1.49       sjg     if (words != NULL) {
    230  1.49       sjg 	for (n = 0; n < nwords; n++) {
    231  1.49       sjg 	    ptr = words[n];
    232  1.49       sjg 	    if (!*ptr)
    233  1.49       sjg 		continue;
    234  1.49       sjg 	    escapes = 0;
    235  1.49       sjg 	    while ((ch = *ptr++)) {
    236  1.49       sjg 		switch(ch) {
    237  1.49       sjg 		case ':':
    238  1.49       sjg 		case '$':
    239  1.49       sjg 		case '\\':
    240  1.49       sjg 		    escapes |= FOR_SUB_ESCAPE_CHAR;
    241  1.49       sjg 		    break;
    242  1.49       sjg 		case ')':
    243  1.49       sjg 		    escapes |= FOR_SUB_ESCAPE_PAREN;
    244  1.49       sjg 		    break;
    245  1.49       sjg 		case /*{*/ '}':
    246  1.49       sjg 		    escapes |= FOR_SUB_ESCAPE_BRACE;
    247  1.49       sjg 		    break;
    248  1.49       sjg 		}
    249  1.49       sjg 	    }
    250  1.49       sjg 	    /*
    251  1.49       sjg 	     * We have to dup words[n] to maintain the semantics of
    252  1.49       sjg 	     * strlist.
    253  1.49       sjg 	     */
    254  1.39       dsl 	    strlist_add_str(&new_for->items, bmake_strdup(words[n]), escapes);
    255   1.1       cgd 	}
    256  1.49       sjg 
    257  1.49       sjg 	free(words);
    258   1.7  christos 	free(word_buf);
    259  1.49       sjg 
    260  1.49       sjg 	if ((len = strlist_num(&new_for->items)) > 0 &&
    261  1.49       sjg 	    len % (n = strlist_num(&new_for->vars))) {
    262  1.49       sjg 	    Parse_Error(PARSE_FATAL,
    263  1.49       sjg 			"Wrong number of words (%d) in .for substitution list"
    264  1.49       sjg 			" with %d vars", len, n);
    265  1.49       sjg 	    /*
    266  1.49       sjg 	     * Return 'success' so that the body of the .for loop is
    267  1.49       sjg 	     * accumulated.
    268  1.49       sjg 	     * Remove all items so that the loop doesn't iterate.
    269  1.49       sjg 	     */
    270  1.49       sjg 	    strlist_clean(&new_for->items);
    271  1.32       dsl 	}
    272  1.32       dsl     }
    273  1.46       dsl 
    274  1.43       dsl     Buf_Init(&new_for->buf, 0);
    275  1.32       dsl     accumFor = new_for;
    276  1.32       dsl     forLevel = 1;
    277  1.32       dsl     return 1;
    278   1.7  christos }
    279  1.35       dsl 
    280  1.35       dsl /*
    281  1.43       dsl  * Add another line to a .for loop.
    282  1.35       dsl  * Returns 0 when the matching .endfor is reached.
    283  1.35       dsl  */
    284  1.32       dsl 
    285  1.32       dsl int
    286  1.32       dsl For_Accum(char *line)
    287  1.32       dsl {
    288  1.26       dsl     char *ptr = line;
    289  1.26       dsl 
    290   1.1       cgd     if (*ptr == '.') {
    291   1.2       jtc 
    292   1.1       cgd 	for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
    293   1.1       cgd 	    continue;
    294   1.2       jtc 
    295  1.26       dsl 	if (strncmp(ptr, "endfor", 6) == 0 &&
    296   1.1       cgd 		(isspace((unsigned char) ptr[6]) || !ptr[6])) {
    297  1.23       dsl 	    if (DEBUG(FOR))
    298  1.32       dsl 		(void)fprintf(debug_file, "For: end for %d\n", forLevel);
    299   1.1       cgd 	    if (--forLevel <= 0)
    300  1.26       dsl 		return 0;
    301   1.2       jtc 	} else if (strncmp(ptr, "for", 3) == 0 &&
    302   1.1       cgd 		 isspace((unsigned char) ptr[3])) {
    303   1.1       cgd 	    forLevel++;
    304  1.23       dsl 	    if (DEBUG(FOR))
    305   1.1       cgd 		(void)fprintf(debug_file, "For: new loop %d\n", forLevel);
    306   1.1       cgd 	}
    307   1.1       cgd     }
    308  1.46       dsl 
    309  1.46       dsl     Buf_AddBytes(&accumFor->buf, strlen(line), line);
    310  1.32       dsl     Buf_AddByte(&accumFor->buf, '\n');
    311   1.1       cgd     return 1;
    312   1.1       cgd }
    313   1.1       cgd 
    314   1.1       cgd 
    315   1.1       cgd /*-
    317   1.7  christos  *-----------------------------------------------------------------------
    318   1.1       cgd  * For_Run --
    319   1.1       cgd  *	Run the for loop, imitating the actions of an include file
    320   1.1       cgd  *
    321   1.1       cgd  * Results:
    322   1.1       cgd  *	None.
    323   1.1       cgd  *
    324   1.1       cgd  * Side Effects:
    325   1.1       cgd  *	None.
    326   1.1       cgd  *
    327  1.42       dsl  *-----------------------------------------------------------------------
    328  1.45       dsl  */
    329  1.45       dsl 
    330  1.45       dsl static int
    331  1.45       dsl for_var_len(const char *var)
    332  1.45       dsl {
    333  1.45       dsl     char ch, var_start, var_end;
    334  1.45       dsl     int depth;
    335  1.45       dsl     int len;
    336  1.45       dsl 
    337  1.45       dsl     var_start = *var;
    338  1.45       dsl     if (var_start == 0)
    339  1.45       dsl 	/* just escape the $ */
    340  1.45       dsl 	return 0;
    341  1.45       dsl 
    342  1.45       dsl     if (var_start == '(')
    343  1.45       dsl 	var_end = ')';
    344  1.45       dsl     else if (var_start == '{')
    345  1.45       dsl 	var_end = '}';
    346  1.45       dsl     else
    347  1.45       dsl 	/* Single char variable */
    348  1.45       dsl 	return 1;
    349  1.45       dsl 
    350  1.45       dsl     depth = 1;
    351  1.45       dsl     for (len = 1; (ch = var[len++]) != 0;) {
    352  1.45       dsl 	if (ch == var_start)
    353  1.45       dsl 	    depth++;
    354  1.45       dsl 	else if (ch == var_end && --depth == 0)
    355  1.45       dsl 	    return len;
    356  1.45       dsl     }
    357  1.45       dsl 
    358  1.45       dsl     /* Variable end not found, escape the $ */
    359  1.45       dsl     return 0;
    360  1.42       dsl }
    361  1.46       dsl 
    362  1.42       dsl static void
    363  1.42       dsl for_substitute(Buffer *cmds, strlist_t *items, unsigned int item_no, char ech)
    364  1.45       dsl {
    365  1.42       dsl     const char *item = strlist_str(items, item_no);
    366  1.42       dsl     int len;
    367  1.42       dsl     char ch;
    368  1.42       dsl 
    369  1.42       dsl     /* If there were no escapes, or the only escape is the other variable
    370  1.42       dsl      * terminator, then just substitute the full string */
    371  1.42       dsl     if (!(strlist_info(items, item_no) &
    372  1.42       dsl 	    (ech == ')' ? ~FOR_SUB_ESCAPE_BRACE : ~FOR_SUB_ESCAPE_PAREN))) {
    373  1.42       dsl 	Buf_AddBytes(cmds, strlen(item), item);
    374  1.42       dsl 	return;
    375  1.44       dsl     }
    376  1.45       dsl 
    377  1.45       dsl     /* Escape ':', '$', '\\' and 'ech' - removed by :U processing */
    378  1.45       dsl     while ((ch = *item++) != 0) {
    379  1.45       dsl 	if (ch == '$') {
    380  1.45       dsl 	    len = for_var_len(item);
    381  1.45       dsl 	    if (len != 0) {
    382  1.45       dsl 		Buf_AddBytes(cmds, len + 1, item - 1);
    383  1.45       dsl 		item += len;
    384  1.45       dsl 		continue;
    385  1.45       dsl 	    }
    386  1.42       dsl 	    Buf_AddByte(cmds, '\\');
    387  1.42       dsl 	} else if (ch == ':' || ch == '\\' || ch == ech)
    388  1.42       dsl 	    Buf_AddByte(cmds, '\\');
    389  1.42       dsl 	Buf_AddByte(cmds, ch);
    390  1.42       dsl     }
    391  1.43       dsl }
    392  1.48  dholland 
    393   1.1       cgd static char *
    394  1.43       dsl For_Iterate(void *v_arg, size_t *ret_len)
    395  1.38       dsl {
    396  1.42       dsl     For *arg = v_arg;
    397  1.39       dsl     int i, len;
    398  1.39       dsl     char *var;
    399  1.39       dsl     char *cp;
    400  1.39       dsl     char *cmd_cp;
    401  1.39       dsl     char *body_end;
    402   1.7  christos     char ch;
    403  1.43       dsl     Buffer cmds;
    404  1.43       dsl 
    405  1.43       dsl     if (arg->sub_next + strlist_num(&arg->vars) > strlist_num(&arg->items)) {
    406  1.43       dsl 	/* No more iterations */
    407  1.43       dsl 	For_Free(arg);
    408   1.1       cgd 	return NULL;
    409  1.43       dsl     }
    410  1.43       dsl 
    411   1.7  christos     free(arg->parse_buf);
    412  1.39       dsl     arg->parse_buf = NULL;
    413  1.39       dsl 
    414  1.39       dsl     /*
    415  1.39       dsl      * Scan the for loop body and replace references to the loop variables
    416  1.39       dsl      * with variable references that expand to the required text.
    417  1.39       dsl      * Using variable expansions ensures that the .for loop can't generate
    418  1.39       dsl      * syntax, and that the later parsing will still see a variable.
    419  1.39       dsl      * We assume that the null variable will never be defined.
    420  1.39       dsl      *
    421  1.39       dsl      * The detection of substitions of the loop control variable is naive.
    422  1.39       dsl      * Many of the modifiers use \ to escape $ (not $) so it is possible
    423  1.43       dsl      * to contrive a makefile where an unwanted substitution happens.
    424  1.46       dsl      */
    425  1.43       dsl 
    426  1.46       dsl     cmd_cp = Buf_GetAll(&arg->buf, &len);
    427  1.43       dsl     body_end = cmd_cp + len;
    428  1.43       dsl     Buf_Init(&cmds, len + 256);
    429  1.43       dsl     for (cp = cmd_cp; (cp = strchr(cp, '$')) != NULL;) {
    430  1.43       dsl 	char ech;
    431  1.43       dsl 	ch = *++cp;
    432  1.43       dsl 	if ((ch == '(' && (ech = ')')) || (ch == '{' && (ech = '}'))) {
    433  1.43       dsl 	    cp++;
    434  1.43       dsl 	    /* Check variable name against the .for loop variables */
    435  1.43       dsl 	    STRLIST_FOREACH(var, &arg->vars, i) {
    436  1.43       dsl 		len = strlist_info(&arg->vars, i);
    437  1.43       dsl 		if (memcmp(cp, var, len) != 0)
    438  1.39       dsl 		    continue;
    439  1.43       dsl 		if (cp[len] != ':' && cp[len] != ech && cp[len] != '\\')
    440  1.46       dsl 		    continue;
    441  1.46       dsl 		/* Found a variable match. Replace with :U<value> */
    442  1.43       dsl 		Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
    443  1.43       dsl 		Buf_AddBytes(&cmds, 2, ":U");
    444  1.46       dsl 		cp += len;
    445  1.39       dsl 		cmd_cp = cp;
    446  1.39       dsl 		for_substitute(&cmds, &arg->items, arg->sub_next + i, ech);
    447  1.43       dsl 		break;
    448  1.43       dsl 	    }
    449  1.43       dsl 	    continue;
    450  1.43       dsl 	}
    451  1.43       dsl 	if (ch == 0)
    452  1.43       dsl 	    break;
    453  1.43       dsl 	/* Probably a single character name, ignore $$ and stupid ones. {*/
    454  1.43       dsl 	if (!arg->short_var || strchr("}):$", ch) != NULL) {
    455  1.43       dsl 	    cp++;
    456  1.43       dsl 	    continue;
    457  1.43       dsl 	}
    458  1.43       dsl 	STRLIST_FOREACH(var, &arg->vars, i) {
    459  1.43       dsl 	    if (var[0] != ch || var[1] != 0)
    460  1.46       dsl 		continue;
    461  1.46       dsl 	    /* Found a variable match. Replace with ${:U<value>} */
    462  1.43       dsl 	    Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
    463  1.46       dsl 	    Buf_AddBytes(&cmds, 3, "{:U");
    464  1.46       dsl 	    cmd_cp = ++cp;
    465  1.43       dsl 	    for_substitute(&cmds, &arg->items, arg->sub_next + i, /*{*/ '}');
    466  1.39       dsl 	    Buf_AddBytes(&cmds, 1, "}");
    467  1.43       dsl 	    break;
    468  1.46       dsl 	}
    469  1.43       dsl     }
    470  1.46       dsl     Buf_AddBytes(&cmds, body_end - cmd_cp, cmd_cp);
    471  1.43       dsl 
    472  1.43       dsl     cp = Buf_Destroy(&cmds, FALSE);
    473  1.39       dsl     if (DEBUG(FOR))
    474  1.43       dsl 	(void)fprintf(debug_file, "For: loop body:\n%s", cp);
    475  1.43       dsl 
    476  1.43       dsl     arg->sub_next += strlist_num(&arg->vars);
    477  1.48  dholland 
    478  1.43       dsl     arg->parse_buf = cp;
    479  1.43       dsl     *ret_len = strlen(cp);
    480   1.7  christos     return cp;
    481  1.43       dsl }
    482  1.43       dsl 
    483  1.43       dsl void
    484  1.43       dsl For_Run(int lineno)
    485  1.43       dsl {
    486  1.43       dsl     For *arg;
    487  1.43       dsl 
    488   1.1       cgd     arg = accumFor;
    489  1.43       dsl     accumFor = NULL;
    490  1.43       dsl 
    491  1.43       dsl     if (strlist_num(&arg->items) == 0) {
    492  1.43       dsl         /* Nothing to expand - possibly due to an earlier syntax error. */
    493  1.43       dsl         For_Free(arg);
    494  1.43       dsl         return;
    495  1.43       dsl     }
    496   1.1       cgd 
    497                     Parse_SetInput(NULL, lineno, -1, For_Iterate, arg);
    498                 }
    499