xref: /minix3/usr.bin/make/for.c (revision 2bc7c627ace37c52ea76fc2f2c700c563df69735)
1*2bc7c627SLionel Sambuc /*	$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $	*/
22e2caf59SThomas Veerman 
32e2caf59SThomas Veerman /*
42e2caf59SThomas Veerman  * Copyright (c) 1992, The Regents of the University of California.
52e2caf59SThomas Veerman  * All rights reserved.
62e2caf59SThomas Veerman  *
72e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
82e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
92e2caf59SThomas Veerman  * are met:
102e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
112e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
122e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
132e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
142e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
152e2caf59SThomas Veerman  * 3. Neither the name of the University nor the names of its contributors
162e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
172e2caf59SThomas Veerman  *    without specific prior written permission.
182e2caf59SThomas Veerman  *
192e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292e2caf59SThomas Veerman  * SUCH DAMAGE.
302e2caf59SThomas Veerman  */
312e2caf59SThomas Veerman 
322e2caf59SThomas Veerman #ifndef MAKE_NATIVE
33*2bc7c627SLionel Sambuc static char rcsid[] = "$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $";
342e2caf59SThomas Veerman #else
352e2caf59SThomas Veerman #include <sys/cdefs.h>
362e2caf59SThomas Veerman #ifndef lint
372e2caf59SThomas Veerman #if 0
382e2caf59SThomas Veerman static char sccsid[] = "@(#)for.c	8.1 (Berkeley) 6/6/93";
392e2caf59SThomas Veerman #else
40*2bc7c627SLionel Sambuc __RCSID("$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $");
412e2caf59SThomas Veerman #endif
422e2caf59SThomas Veerman #endif /* not lint */
432e2caf59SThomas Veerman #endif
442e2caf59SThomas Veerman 
452e2caf59SThomas Veerman /*-
462e2caf59SThomas Veerman  * for.c --
472e2caf59SThomas Veerman  *	Functions to handle loops in a makefile.
482e2caf59SThomas Veerman  *
492e2caf59SThomas Veerman  * Interface:
502e2caf59SThomas Veerman  *	For_Eval 	Evaluate the loop in the passed line.
512e2caf59SThomas Veerman  *	For_Run		Run accumulated loop
522e2caf59SThomas Veerman  *
532e2caf59SThomas Veerman  */
542e2caf59SThomas Veerman 
552e2caf59SThomas Veerman #include    <assert.h>
562e2caf59SThomas Veerman #include    <ctype.h>
572e2caf59SThomas Veerman 
582e2caf59SThomas Veerman #include    "make.h"
592e2caf59SThomas Veerman #include    "hash.h"
602e2caf59SThomas Veerman #include    "dir.h"
612e2caf59SThomas Veerman #include    "buf.h"
622e2caf59SThomas Veerman #include    "strlist.h"
632e2caf59SThomas Veerman 
642e2caf59SThomas Veerman #define FOR_SUB_ESCAPE_CHAR  1
652e2caf59SThomas Veerman #define FOR_SUB_ESCAPE_BRACE 2
662e2caf59SThomas Veerman #define FOR_SUB_ESCAPE_PAREN 4
672e2caf59SThomas Veerman 
682e2caf59SThomas Veerman /*
692e2caf59SThomas Veerman  * For statements are of the form:
702e2caf59SThomas Veerman  *
712e2caf59SThomas Veerman  * .for <variable> in <varlist>
722e2caf59SThomas Veerman  * ...
732e2caf59SThomas Veerman  * .endfor
742e2caf59SThomas Veerman  *
752e2caf59SThomas Veerman  * The trick is to look for the matching end inside for for loop
762e2caf59SThomas Veerman  * To do that, we count the current nesting level of the for loops.
772e2caf59SThomas Veerman  * and the .endfor statements, accumulating all the statements between
782e2caf59SThomas Veerman  * the initial .for loop and the matching .endfor;
792e2caf59SThomas Veerman  * then we evaluate the for loop for each variable in the varlist.
802e2caf59SThomas Veerman  *
812e2caf59SThomas Veerman  * Note that any nested fors are just passed through; they get handled
822e2caf59SThomas Veerman  * recursively in For_Eval when we're expanding the enclosing for in
832e2caf59SThomas Veerman  * For_Run.
842e2caf59SThomas Veerman  */
852e2caf59SThomas Veerman 
862e2caf59SThomas Veerman static int  	  forLevel = 0;  	/* Nesting level	*/
872e2caf59SThomas Veerman 
882e2caf59SThomas Veerman /*
892e2caf59SThomas Veerman  * State of a for loop.
902e2caf59SThomas Veerman  */
912e2caf59SThomas Veerman typedef struct _For {
922e2caf59SThomas Veerman     Buffer	  buf;			/* Body of loop		*/
932e2caf59SThomas Veerman     strlist_t     vars;			/* Iteration variables	*/
942e2caf59SThomas Veerman     strlist_t     items;		/* Substitution items */
952e2caf59SThomas Veerman     char          *parse_buf;
962e2caf59SThomas Veerman     int           short_var;
972e2caf59SThomas Veerman     int           sub_next;
982e2caf59SThomas Veerman } For;
992e2caf59SThomas Veerman 
1002e2caf59SThomas Veerman static For        *accumFor;            /* Loop being accumulated */
1012e2caf59SThomas Veerman 
1022e2caf59SThomas Veerman 
1032e2caf59SThomas Veerman 
1042e2caf59SThomas Veerman static char *
make_str(const char * ptr,int len)1052e2caf59SThomas Veerman make_str(const char *ptr, int len)
1062e2caf59SThomas Veerman {
1072e2caf59SThomas Veerman 	char *new_ptr;
1082e2caf59SThomas Veerman 
1092e2caf59SThomas Veerman 	new_ptr = bmake_malloc(len + 1);
1102e2caf59SThomas Veerman 	memcpy(new_ptr, ptr, len);
1112e2caf59SThomas Veerman 	new_ptr[len] = 0;
1122e2caf59SThomas Veerman 	return new_ptr;
1132e2caf59SThomas Veerman }
1142e2caf59SThomas Veerman 
1152e2caf59SThomas Veerman static void
For_Free(For * arg)1162e2caf59SThomas Veerman For_Free(For *arg)
1172e2caf59SThomas Veerman {
1182e2caf59SThomas Veerman     Buf_Destroy(&arg->buf, TRUE);
1192e2caf59SThomas Veerman     strlist_clean(&arg->vars);
1202e2caf59SThomas Veerman     strlist_clean(&arg->items);
1212e2caf59SThomas Veerman     free(arg->parse_buf);
1222e2caf59SThomas Veerman 
1232e2caf59SThomas Veerman     free(arg);
1242e2caf59SThomas Veerman }
1252e2caf59SThomas Veerman 
1262e2caf59SThomas Veerman /*-
1272e2caf59SThomas Veerman  *-----------------------------------------------------------------------
1282e2caf59SThomas Veerman  * For_Eval --
1292e2caf59SThomas Veerman  *	Evaluate the for loop in the passed line. The line
1302e2caf59SThomas Veerman  *	looks like this:
1312e2caf59SThomas Veerman  *	    .for <variable> in <varlist>
1322e2caf59SThomas Veerman  *
1332e2caf59SThomas Veerman  * Input:
1342e2caf59SThomas Veerman  *	line		Line to parse
1352e2caf59SThomas Veerman  *
1362e2caf59SThomas Veerman  * Results:
1372e2caf59SThomas Veerman  *      0: Not a .for statement, parse the line
1382e2caf59SThomas Veerman  *	1: We found a for loop
1392e2caf59SThomas Veerman  *     -1: A .for statement with a bad syntax error, discard.
1402e2caf59SThomas Veerman  *
1412e2caf59SThomas Veerman  * Side Effects:
1422e2caf59SThomas Veerman  *	None.
1432e2caf59SThomas Veerman  *
1442e2caf59SThomas Veerman  *-----------------------------------------------------------------------
1452e2caf59SThomas Veerman  */
1462e2caf59SThomas Veerman int
For_Eval(char * line)1472e2caf59SThomas Veerman For_Eval(char *line)
1482e2caf59SThomas Veerman {
1492e2caf59SThomas Veerman     For *new_for;
1502e2caf59SThomas Veerman     char *ptr = line, *sub;
1512e2caf59SThomas Veerman     int len;
1522e2caf59SThomas Veerman     int escapes;
1532e2caf59SThomas Veerman     unsigned char ch;
154*2bc7c627SLionel Sambuc     char **words, *word_buf;
155*2bc7c627SLionel Sambuc     int n, nwords;
1562e2caf59SThomas Veerman 
1572e2caf59SThomas Veerman     /* Skip the '.' and any following whitespace */
1582e2caf59SThomas Veerman     for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
1592e2caf59SThomas Veerman 	continue;
1602e2caf59SThomas Veerman 
1612e2caf59SThomas Veerman     /*
1622e2caf59SThomas Veerman      * If we are not in a for loop quickly determine if the statement is
1632e2caf59SThomas Veerman      * a for.
1642e2caf59SThomas Veerman      */
1652e2caf59SThomas Veerman     if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
1662e2caf59SThomas Veerman 	    !isspace((unsigned char) ptr[3])) {
1672e2caf59SThomas Veerman 	if (ptr[0] == 'e' && strncmp(ptr+1, "ndfor", 5) == 0) {
1682e2caf59SThomas Veerman 	    Parse_Error(PARSE_FATAL, "for-less endfor");
1692e2caf59SThomas Veerman 	    return -1;
1702e2caf59SThomas Veerman 	}
1712e2caf59SThomas Veerman 	return 0;
1722e2caf59SThomas Veerman     }
1732e2caf59SThomas Veerman     ptr += 3;
1742e2caf59SThomas Veerman 
1752e2caf59SThomas Veerman     /*
1762e2caf59SThomas Veerman      * we found a for loop, and now we are going to parse it.
1772e2caf59SThomas Veerman      */
1782e2caf59SThomas Veerman 
1792e2caf59SThomas Veerman     new_for = bmake_malloc(sizeof *new_for);
1802e2caf59SThomas Veerman     memset(new_for, 0, sizeof *new_for);
1812e2caf59SThomas Veerman 
1822e2caf59SThomas Veerman     /* Grab the variables. Terminate on "in". */
1832e2caf59SThomas Veerman     for (;; ptr += len) {
1842e2caf59SThomas Veerman 	while (*ptr && isspace((unsigned char) *ptr))
1852e2caf59SThomas Veerman 	    ptr++;
1862e2caf59SThomas Veerman 	if (*ptr == '\0') {
1872e2caf59SThomas Veerman 	    Parse_Error(PARSE_FATAL, "missing `in' in for");
1882e2caf59SThomas Veerman 	    For_Free(new_for);
1892e2caf59SThomas Veerman 	    return -1;
1902e2caf59SThomas Veerman 	}
1912e2caf59SThomas Veerman 	for (len = 1; ptr[len] && !isspace((unsigned char)ptr[len]); len++)
1922e2caf59SThomas Veerman 	    continue;
1932e2caf59SThomas Veerman 	if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
1942e2caf59SThomas Veerman 	    ptr += 2;
1952e2caf59SThomas Veerman 	    break;
1962e2caf59SThomas Veerman 	}
1972e2caf59SThomas Veerman 	if (len == 1)
1982e2caf59SThomas Veerman 	    new_for->short_var = 1;
1992e2caf59SThomas Veerman 	strlist_add_str(&new_for->vars, make_str(ptr, len), len);
2002e2caf59SThomas Veerman     }
2012e2caf59SThomas Veerman 
2022e2caf59SThomas Veerman     if (strlist_num(&new_for->vars) == 0) {
2032e2caf59SThomas Veerman 	Parse_Error(PARSE_FATAL, "no iteration variables in for");
2042e2caf59SThomas Veerman 	For_Free(new_for);
2052e2caf59SThomas Veerman 	return -1;
2062e2caf59SThomas Veerman     }
2072e2caf59SThomas Veerman 
2082e2caf59SThomas Veerman     while (*ptr && isspace((unsigned char) *ptr))
2092e2caf59SThomas Veerman 	ptr++;
2102e2caf59SThomas Veerman 
2112e2caf59SThomas Veerman     /*
2122e2caf59SThomas Veerman      * Make a list with the remaining words
2132e2caf59SThomas Veerman      * The values are substituted as ${:U<value>...} so we must \ escape
2142e2caf59SThomas Veerman      * characters that break that syntax.
2152e2caf59SThomas Veerman      * Variables are fully expanded - so it is safe for escape $.
2162e2caf59SThomas Veerman      * We can't do the escapes here - because we don't know whether
2172e2caf59SThomas Veerman      * we are substuting into ${...} or $(...).
2182e2caf59SThomas Veerman      */
2192e2caf59SThomas Veerman     sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
2202e2caf59SThomas Veerman 
221*2bc7c627SLionel Sambuc     /*
222*2bc7c627SLionel Sambuc      * Split into words allowing for quoted strings.
223*2bc7c627SLionel Sambuc      */
224*2bc7c627SLionel Sambuc     words = brk_string(sub, &nwords, FALSE, &word_buf);
2252e2caf59SThomas Veerman 
2262e2caf59SThomas Veerman     free(sub);
2272e2caf59SThomas Veerman 
228*2bc7c627SLionel Sambuc     if (words != NULL) {
229*2bc7c627SLionel Sambuc 	for (n = 0; n < nwords; n++) {
230*2bc7c627SLionel Sambuc 	    ptr = words[n];
231*2bc7c627SLionel Sambuc 	    if (!*ptr)
232*2bc7c627SLionel Sambuc 		continue;
233*2bc7c627SLionel Sambuc 	    escapes = 0;
234*2bc7c627SLionel Sambuc 	    while ((ch = *ptr++)) {
235*2bc7c627SLionel Sambuc 		switch(ch) {
236*2bc7c627SLionel Sambuc 		case ':':
237*2bc7c627SLionel Sambuc 		case '$':
238*2bc7c627SLionel Sambuc 		case '\\':
239*2bc7c627SLionel Sambuc 		    escapes |= FOR_SUB_ESCAPE_CHAR;
240*2bc7c627SLionel Sambuc 		    break;
241*2bc7c627SLionel Sambuc 		case ')':
242*2bc7c627SLionel Sambuc 		    escapes |= FOR_SUB_ESCAPE_PAREN;
243*2bc7c627SLionel Sambuc 		    break;
244*2bc7c627SLionel Sambuc 		case /*{*/ '}':
245*2bc7c627SLionel Sambuc 		    escapes |= FOR_SUB_ESCAPE_BRACE;
246*2bc7c627SLionel Sambuc 		    break;
247*2bc7c627SLionel Sambuc 		}
248*2bc7c627SLionel Sambuc 	    }
249*2bc7c627SLionel Sambuc 	    /*
250*2bc7c627SLionel Sambuc 	     * We have to dup words[n] to maintain the semantics of
251*2bc7c627SLionel Sambuc 	     * strlist.
252*2bc7c627SLionel Sambuc 	     */
253*2bc7c627SLionel Sambuc 	    strlist_add_str(&new_for->items, bmake_strdup(words[n]), escapes);
254*2bc7c627SLionel Sambuc 	}
255*2bc7c627SLionel Sambuc 
256*2bc7c627SLionel Sambuc 	free(words);
257*2bc7c627SLionel Sambuc 	free(word_buf);
258*2bc7c627SLionel Sambuc 
259*2bc7c627SLionel Sambuc 	if ((len = strlist_num(&new_for->items)) > 0 &&
260*2bc7c627SLionel Sambuc 	    len % (n = strlist_num(&new_for->vars))) {
2612e2caf59SThomas Veerman 	    Parse_Error(PARSE_FATAL,
2622e2caf59SThomas Veerman 			"Wrong number of words (%d) in .for substitution list"
263*2bc7c627SLionel Sambuc 			" with %d vars", len, n);
2642e2caf59SThomas Veerman 	    /*
265*2bc7c627SLionel Sambuc 	     * Return 'success' so that the body of the .for loop is
266*2bc7c627SLionel Sambuc 	     * accumulated.
2672e2caf59SThomas Veerman 	     * Remove all items so that the loop doesn't iterate.
2682e2caf59SThomas Veerman 	     */
2692e2caf59SThomas Veerman 	    strlist_clean(&new_for->items);
2702e2caf59SThomas Veerman 	}
271*2bc7c627SLionel Sambuc     }
2722e2caf59SThomas Veerman 
2732e2caf59SThomas Veerman     Buf_Init(&new_for->buf, 0);
2742e2caf59SThomas Veerman     accumFor = new_for;
2752e2caf59SThomas Veerman     forLevel = 1;
2762e2caf59SThomas Veerman     return 1;
2772e2caf59SThomas Veerman }
2782e2caf59SThomas Veerman 
2792e2caf59SThomas Veerman /*
2802e2caf59SThomas Veerman  * Add another line to a .for loop.
2812e2caf59SThomas Veerman  * Returns 0 when the matching .endfor is reached.
2822e2caf59SThomas Veerman  */
2832e2caf59SThomas Veerman 
2842e2caf59SThomas Veerman int
For_Accum(char * line)2852e2caf59SThomas Veerman For_Accum(char *line)
2862e2caf59SThomas Veerman {
2872e2caf59SThomas Veerman     char *ptr = line;
2882e2caf59SThomas Veerman 
2892e2caf59SThomas Veerman     if (*ptr == '.') {
2902e2caf59SThomas Veerman 
2912e2caf59SThomas Veerman 	for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
2922e2caf59SThomas Veerman 	    continue;
2932e2caf59SThomas Veerman 
2942e2caf59SThomas Veerman 	if (strncmp(ptr, "endfor", 6) == 0 &&
2952e2caf59SThomas Veerman 		(isspace((unsigned char) ptr[6]) || !ptr[6])) {
2962e2caf59SThomas Veerman 	    if (DEBUG(FOR))
2972e2caf59SThomas Veerman 		(void)fprintf(debug_file, "For: end for %d\n", forLevel);
2982e2caf59SThomas Veerman 	    if (--forLevel <= 0)
2992e2caf59SThomas Veerman 		return 0;
3002e2caf59SThomas Veerman 	} else if (strncmp(ptr, "for", 3) == 0 &&
3012e2caf59SThomas Veerman 		 isspace((unsigned char) ptr[3])) {
3022e2caf59SThomas Veerman 	    forLevel++;
3032e2caf59SThomas Veerman 	    if (DEBUG(FOR))
3042e2caf59SThomas Veerman 		(void)fprintf(debug_file, "For: new loop %d\n", forLevel);
3052e2caf59SThomas Veerman 	}
3062e2caf59SThomas Veerman     }
3072e2caf59SThomas Veerman 
3082e2caf59SThomas Veerman     Buf_AddBytes(&accumFor->buf, strlen(line), line);
3092e2caf59SThomas Veerman     Buf_AddByte(&accumFor->buf, '\n');
3102e2caf59SThomas Veerman     return 1;
3112e2caf59SThomas Veerman }
3122e2caf59SThomas Veerman 
3132e2caf59SThomas Veerman 
3142e2caf59SThomas Veerman /*-
3152e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3162e2caf59SThomas Veerman  * For_Run --
3172e2caf59SThomas Veerman  *	Run the for loop, imitating the actions of an include file
3182e2caf59SThomas Veerman  *
3192e2caf59SThomas Veerman  * Results:
3202e2caf59SThomas Veerman  *	None.
3212e2caf59SThomas Veerman  *
3222e2caf59SThomas Veerman  * Side Effects:
3232e2caf59SThomas Veerman  *	None.
3242e2caf59SThomas Veerman  *
3252e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3262e2caf59SThomas Veerman  */
3272e2caf59SThomas Veerman 
3282e2caf59SThomas Veerman static int
for_var_len(const char * var)3292e2caf59SThomas Veerman for_var_len(const char *var)
3302e2caf59SThomas Veerman {
3312e2caf59SThomas Veerman     char ch, var_start, var_end;
3322e2caf59SThomas Veerman     int depth;
3332e2caf59SThomas Veerman     int len;
3342e2caf59SThomas Veerman 
3352e2caf59SThomas Veerman     var_start = *var;
3362e2caf59SThomas Veerman     if (var_start == 0)
3372e2caf59SThomas Veerman 	/* just escape the $ */
3382e2caf59SThomas Veerman 	return 0;
3392e2caf59SThomas Veerman 
3402e2caf59SThomas Veerman     if (var_start == '(')
3412e2caf59SThomas Veerman 	var_end = ')';
3422e2caf59SThomas Veerman     else if (var_start == '{')
3432e2caf59SThomas Veerman 	var_end = '}';
3442e2caf59SThomas Veerman     else
3452e2caf59SThomas Veerman 	/* Single char variable */
3462e2caf59SThomas Veerman 	return 1;
3472e2caf59SThomas Veerman 
3482e2caf59SThomas Veerman     depth = 1;
3492e2caf59SThomas Veerman     for (len = 1; (ch = var[len++]) != 0;) {
3502e2caf59SThomas Veerman 	if (ch == var_start)
3512e2caf59SThomas Veerman 	    depth++;
3522e2caf59SThomas Veerman 	else if (ch == var_end && --depth == 0)
3532e2caf59SThomas Veerman 	    return len;
3542e2caf59SThomas Veerman     }
3552e2caf59SThomas Veerman 
3562e2caf59SThomas Veerman     /* Variable end not found, escape the $ */
3572e2caf59SThomas Veerman     return 0;
3582e2caf59SThomas Veerman }
3592e2caf59SThomas Veerman 
3602e2caf59SThomas Veerman static void
for_substitute(Buffer * cmds,strlist_t * items,unsigned int item_no,char ech)3612e2caf59SThomas Veerman for_substitute(Buffer *cmds, strlist_t *items, unsigned int item_no, char ech)
3622e2caf59SThomas Veerman {
3632e2caf59SThomas Veerman     const char *item = strlist_str(items, item_no);
3642e2caf59SThomas Veerman     int len;
3652e2caf59SThomas Veerman     char ch;
3662e2caf59SThomas Veerman 
3672e2caf59SThomas Veerman     /* If there were no escapes, or the only escape is the other variable
3682e2caf59SThomas Veerman      * terminator, then just substitute the full string */
3692e2caf59SThomas Veerman     if (!(strlist_info(items, item_no) &
3702e2caf59SThomas Veerman 	    (ech == ')' ? ~FOR_SUB_ESCAPE_BRACE : ~FOR_SUB_ESCAPE_PAREN))) {
3712e2caf59SThomas Veerman 	Buf_AddBytes(cmds, strlen(item), item);
3722e2caf59SThomas Veerman 	return;
3732e2caf59SThomas Veerman     }
3742e2caf59SThomas Veerman 
3752e2caf59SThomas Veerman     /* Escape ':', '$', '\\' and 'ech' - removed by :U processing */
3762e2caf59SThomas Veerman     while ((ch = *item++) != 0) {
3772e2caf59SThomas Veerman 	if (ch == '$') {
3782e2caf59SThomas Veerman 	    len = for_var_len(item);
3792e2caf59SThomas Veerman 	    if (len != 0) {
3802e2caf59SThomas Veerman 		Buf_AddBytes(cmds, len + 1, item - 1);
3812e2caf59SThomas Veerman 		item += len;
3822e2caf59SThomas Veerman 		continue;
3832e2caf59SThomas Veerman 	    }
3842e2caf59SThomas Veerman 	    Buf_AddByte(cmds, '\\');
3852e2caf59SThomas Veerman 	} else if (ch == ':' || ch == '\\' || ch == ech)
3862e2caf59SThomas Veerman 	    Buf_AddByte(cmds, '\\');
3872e2caf59SThomas Veerman 	Buf_AddByte(cmds, ch);
3882e2caf59SThomas Veerman     }
3892e2caf59SThomas Veerman }
3902e2caf59SThomas Veerman 
3912e2caf59SThomas Veerman static char *
For_Iterate(void * v_arg,size_t * ret_len)3922e2caf59SThomas Veerman For_Iterate(void *v_arg, size_t *ret_len)
3932e2caf59SThomas Veerman {
3942e2caf59SThomas Veerman     For *arg = v_arg;
3952e2caf59SThomas Veerman     int i, len;
3962e2caf59SThomas Veerman     char *var;
3972e2caf59SThomas Veerman     char *cp;
3982e2caf59SThomas Veerman     char *cmd_cp;
3992e2caf59SThomas Veerman     char *body_end;
4002e2caf59SThomas Veerman     char ch;
4012e2caf59SThomas Veerman     Buffer cmds;
4022e2caf59SThomas Veerman 
4032e2caf59SThomas Veerman     if (arg->sub_next + strlist_num(&arg->vars) > strlist_num(&arg->items)) {
4042e2caf59SThomas Veerman 	/* No more iterations */
4052e2caf59SThomas Veerman 	For_Free(arg);
4062e2caf59SThomas Veerman 	return NULL;
4072e2caf59SThomas Veerman     }
4082e2caf59SThomas Veerman 
4092e2caf59SThomas Veerman     free(arg->parse_buf);
4102e2caf59SThomas Veerman     arg->parse_buf = NULL;
4112e2caf59SThomas Veerman 
4122e2caf59SThomas Veerman     /*
4132e2caf59SThomas Veerman      * Scan the for loop body and replace references to the loop variables
4142e2caf59SThomas Veerman      * with variable references that expand to the required text.
4152e2caf59SThomas Veerman      * Using variable expansions ensures that the .for loop can't generate
4162e2caf59SThomas Veerman      * syntax, and that the later parsing will still see a variable.
4172e2caf59SThomas Veerman      * We assume that the null variable will never be defined.
4182e2caf59SThomas Veerman      *
4192e2caf59SThomas Veerman      * The detection of substitions of the loop control variable is naive.
4202e2caf59SThomas Veerman      * Many of the modifiers use \ to escape $ (not $) so it is possible
4212e2caf59SThomas Veerman      * to contrive a makefile where an unwanted substitution happens.
4222e2caf59SThomas Veerman      */
4232e2caf59SThomas Veerman 
4242e2caf59SThomas Veerman     cmd_cp = Buf_GetAll(&arg->buf, &len);
4252e2caf59SThomas Veerman     body_end = cmd_cp + len;
4262e2caf59SThomas Veerman     Buf_Init(&cmds, len + 256);
4272e2caf59SThomas Veerman     for (cp = cmd_cp; (cp = strchr(cp, '$')) != NULL;) {
4282e2caf59SThomas Veerman 	char ech;
4292e2caf59SThomas Veerman 	ch = *++cp;
4302e2caf59SThomas Veerman 	if ((ch == '(' && (ech = ')')) || (ch == '{' && (ech = '}'))) {
4312e2caf59SThomas Veerman 	    cp++;
4322e2caf59SThomas Veerman 	    /* Check variable name against the .for loop variables */
4332e2caf59SThomas Veerman 	    STRLIST_FOREACH(var, &arg->vars, i) {
4342e2caf59SThomas Veerman 		len = strlist_info(&arg->vars, i);
4352e2caf59SThomas Veerman 		if (memcmp(cp, var, len) != 0)
4362e2caf59SThomas Veerman 		    continue;
4372e2caf59SThomas Veerman 		if (cp[len] != ':' && cp[len] != ech && cp[len] != '\\')
4382e2caf59SThomas Veerman 		    continue;
4392e2caf59SThomas Veerman 		/* Found a variable match. Replace with :U<value> */
4402e2caf59SThomas Veerman 		Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
4412e2caf59SThomas Veerman 		Buf_AddBytes(&cmds, 2, ":U");
4422e2caf59SThomas Veerman 		cp += len;
4432e2caf59SThomas Veerman 		cmd_cp = cp;
4442e2caf59SThomas Veerman 		for_substitute(&cmds, &arg->items, arg->sub_next + i, ech);
4452e2caf59SThomas Veerman 		break;
4462e2caf59SThomas Veerman 	    }
4472e2caf59SThomas Veerman 	    continue;
4482e2caf59SThomas Veerman 	}
4492e2caf59SThomas Veerman 	if (ch == 0)
4502e2caf59SThomas Veerman 	    break;
4512e2caf59SThomas Veerman 	/* Probably a single character name, ignore $$ and stupid ones. {*/
4522e2caf59SThomas Veerman 	if (!arg->short_var || strchr("}):$", ch) != NULL) {
4532e2caf59SThomas Veerman 	    cp++;
4542e2caf59SThomas Veerman 	    continue;
4552e2caf59SThomas Veerman 	}
4562e2caf59SThomas Veerman 	STRLIST_FOREACH(var, &arg->vars, i) {
4572e2caf59SThomas Veerman 	    if (var[0] != ch || var[1] != 0)
4582e2caf59SThomas Veerman 		continue;
4592e2caf59SThomas Veerman 	    /* Found a variable match. Replace with ${:U<value>} */
4602e2caf59SThomas Veerman 	    Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
4612e2caf59SThomas Veerman 	    Buf_AddBytes(&cmds, 3, "{:U");
4622e2caf59SThomas Veerman 	    cmd_cp = ++cp;
4632e2caf59SThomas Veerman 	    for_substitute(&cmds, &arg->items, arg->sub_next + i, /*{*/ '}');
4642e2caf59SThomas Veerman 	    Buf_AddBytes(&cmds, 1, "}");
4652e2caf59SThomas Veerman 	    break;
4662e2caf59SThomas Veerman 	}
4672e2caf59SThomas Veerman     }
4682e2caf59SThomas Veerman     Buf_AddBytes(&cmds, body_end - cmd_cp, cmd_cp);
4692e2caf59SThomas Veerman 
4702e2caf59SThomas Veerman     cp = Buf_Destroy(&cmds, FALSE);
4712e2caf59SThomas Veerman     if (DEBUG(FOR))
4722e2caf59SThomas Veerman 	(void)fprintf(debug_file, "For: loop body:\n%s", cp);
4732e2caf59SThomas Veerman 
4742e2caf59SThomas Veerman     arg->sub_next += strlist_num(&arg->vars);
4752e2caf59SThomas Veerman 
4762e2caf59SThomas Veerman     arg->parse_buf = cp;
4772e2caf59SThomas Veerman     *ret_len = strlen(cp);
4782e2caf59SThomas Veerman     return cp;
4792e2caf59SThomas Veerman }
4802e2caf59SThomas Veerman 
4812e2caf59SThomas Veerman void
For_Run(int lineno)4822e2caf59SThomas Veerman For_Run(int lineno)
4832e2caf59SThomas Veerman {
4842e2caf59SThomas Veerman     For *arg;
4852e2caf59SThomas Veerman 
4862e2caf59SThomas Veerman     arg = accumFor;
4872e2caf59SThomas Veerman     accumFor = NULL;
4882e2caf59SThomas Veerman 
4892e2caf59SThomas Veerman     if (strlist_num(&arg->items) == 0) {
4902e2caf59SThomas Veerman         /* Nothing to expand - possibly due to an earlier syntax error. */
4912e2caf59SThomas Veerman         For_Free(arg);
4922e2caf59SThomas Veerman         return;
4932e2caf59SThomas Veerman     }
4942e2caf59SThomas Veerman 
4952e2caf59SThomas Veerman     Parse_SetInput(NULL, lineno, -1, For_Iterate, arg);
4962e2caf59SThomas Veerman }
497