2009-01-17 16:29:37 +03:00
|
|
|
|
/* $NetBSD: for.c,v 1.46 2009/01/17 13:29:37 dsl Exp $ */
|
1995-06-14 19:18:37 +04:00
|
|
|
|
|
1994-03-05 03:34:29 +03:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 1992, The Regents of the University of California.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-03-05 03:34:29 +03:00
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-05-07 04:04:38 +04:00
|
|
|
|
#ifndef MAKE_NATIVE
|
2009-01-17 16:29:37 +03:00
|
|
|
|
static char rcsid[] = "$NetBSD: for.c,v 1.46 2009/01/17 13:29:37 dsl Exp $";
|
1997-09-28 07:30:58 +04:00
|
|
|
|
#else
|
1997-07-02 01:17:00 +04:00
|
|
|
|
#include <sys/cdefs.h>
|
1994-03-05 03:34:29 +03:00
|
|
|
|
#ifndef lint
|
1995-06-14 19:18:37 +04:00
|
|
|
|
#if 0
|
1996-11-06 20:58:58 +03:00
|
|
|
|
static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93";
|
1995-06-14 19:18:37 +04:00
|
|
|
|
#else
|
2009-01-17 16:29:37 +03:00
|
|
|
|
__RCSID("$NetBSD: for.c,v 1.46 2009/01/17 13:29:37 dsl Exp $");
|
1995-06-14 19:18:37 +04:00
|
|
|
|
#endif
|
1994-03-05 03:34:29 +03:00
|
|
|
|
#endif /* not lint */
|
1997-09-28 07:30:58 +04:00
|
|
|
|
#endif
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
* for.c --
|
|
|
|
|
* Functions to handle loops in a makefile.
|
|
|
|
|
*
|
|
|
|
|
* Interface:
|
|
|
|
|
* For_Eval Evaluate the loop in the passed line.
|
|
|
|
|
* For_Run Run accumulated loop
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2000-04-17 02:08:06 +04:00
|
|
|
|
#include <assert.h>
|
2002-06-15 22:24:55 +04:00
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
1994-03-05 03:34:29 +03:00
|
|
|
|
#include "make.h"
|
|
|
|
|
#include "hash.h"
|
|
|
|
|
#include "dir.h"
|
|
|
|
|
#include "buf.h"
|
2008-12-21 01:41:53 +03:00
|
|
|
|
#include "strlist.h"
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
2009-01-13 21:30:00 +03:00
|
|
|
|
#define FOR_SUB_ESCAPE_CHAR 1
|
2009-01-10 19:59:02 +03:00
|
|
|
|
#define FOR_SUB_ESCAPE_BRACE 2
|
|
|
|
|
#define FOR_SUB_ESCAPE_PAREN 4
|
|
|
|
|
|
1994-03-05 03:34:29 +03:00
|
|
|
|
/*
|
|
|
|
|
* For statements are of the form:
|
|
|
|
|
*
|
|
|
|
|
* .for <variable> in <varlist>
|
|
|
|
|
* ...
|
|
|
|
|
* .endfor
|
|
|
|
|
*
|
|
|
|
|
* The trick is to look for the matching end inside for for loop
|
|
|
|
|
* To do that, we count the current nesting level of the for loops.
|
|
|
|
|
* and the .endfor statements, accumulating all the statements between
|
1996-11-06 20:58:58 +03:00
|
|
|
|
* the initial .for loop and the matching .endfor;
|
1994-03-05 03:34:29 +03:00
|
|
|
|
* then we evaluate the for loop for each variable in the varlist.
|
2000-04-17 02:08:06 +04:00
|
|
|
|
*
|
|
|
|
|
* Note that any nested fors are just passed through; they get handled
|
|
|
|
|
* recursively in For_Eval when we're expanding the enclosing for in
|
|
|
|
|
* For_Run.
|
1994-03-05 03:34:29 +03:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int forLevel = 0; /* Nesting level */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* State of a for loop.
|
|
|
|
|
*/
|
1994-06-07 02:45:17 +04:00
|
|
|
|
typedef struct _For {
|
2000-04-17 02:08:06 +04:00
|
|
|
|
Buffer buf; /* Body of loop */
|
2008-12-21 01:41:53 +03:00
|
|
|
|
strlist_t vars; /* Iteration variables */
|
|
|
|
|
strlist_t items; /* Substitution items */
|
2009-01-11 18:50:06 +03:00
|
|
|
|
char *parse_buf;
|
|
|
|
|
int short_var;
|
|
|
|
|
int sub_next;
|
1994-06-07 02:45:17 +04:00
|
|
|
|
} For;
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
static For *accumFor; /* Loop being accumulated */
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
|
|
|
|
|
2000-04-17 02:08:06 +04:00
|
|
|
|
|
2008-12-01 01:37:55 +03:00
|
|
|
|
static char *
|
|
|
|
|
make_str(const char *ptr, int len)
|
|
|
|
|
{
|
|
|
|
|
char *new_ptr;
|
|
|
|
|
|
|
|
|
|
new_ptr = bmake_malloc(len + 1);
|
|
|
|
|
memcpy(new_ptr, ptr, len);
|
|
|
|
|
new_ptr[len] = 0;
|
|
|
|
|
return new_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
static void
|
|
|
|
|
For_Free(For *arg)
|
|
|
|
|
{
|
2009-01-17 16:29:37 +03:00
|
|
|
|
Buf_Destroy(&arg->buf, TRUE);
|
2009-01-11 18:50:06 +03:00
|
|
|
|
strlist_clean(&arg->vars);
|
|
|
|
|
strlist_clean(&arg->items);
|
|
|
|
|
free(arg->parse_buf);
|
|
|
|
|
|
|
|
|
|
free(arg);
|
|
|
|
|
}
|
|
|
|
|
|
1994-03-05 03:34:29 +03:00
|
|
|
|
/*-
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
* For_Eval --
|
|
|
|
|
* Evaluate the for loop in the passed line. The line
|
|
|
|
|
* looks like this:
|
|
|
|
|
* .for <variable> in <varlist>
|
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
|
* Input:
|
|
|
|
|
* line Line to parse
|
|
|
|
|
*
|
1994-03-05 03:34:29 +03:00
|
|
|
|
* Results:
|
2008-12-02 00:05:21 +03:00
|
|
|
|
* 0: Not a .for statement, parse the line
|
|
|
|
|
* 1: We found a for loop
|
|
|
|
|
* -1: A .for statement with a bad syntax error, discard.
|
1994-03-05 03:34:29 +03:00
|
|
|
|
*
|
|
|
|
|
* Side Effects:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
int
|
2002-06-15 22:24:55 +04:00
|
|
|
|
For_Eval(char *line)
|
1994-03-05 03:34:29 +03:00
|
|
|
|
{
|
2009-01-11 18:50:06 +03:00
|
|
|
|
For *new_for;
|
2008-12-01 01:37:55 +03:00
|
|
|
|
char *ptr = line, *sub;
|
|
|
|
|
int len;
|
2008-12-21 21:06:53 +03:00
|
|
|
|
int escapes;
|
2009-01-10 19:59:02 +03:00
|
|
|
|
unsigned char ch;
|
2008-12-01 01:37:55 +03:00
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
/* Skip the '.' and any following whitespace */
|
2008-11-29 20:50:11 +03:00
|
|
|
|
for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
|
|
|
|
|
continue;
|
2009-01-11 18:50:06 +03:00
|
|
|
|
|
2008-11-29 20:50:11 +03:00
|
|
|
|
/*
|
|
|
|
|
* If we are not in a for loop quickly determine if the statement is
|
|
|
|
|
* a for.
|
|
|
|
|
*/
|
|
|
|
|
if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
|
|
|
|
|
!isspace((unsigned char) ptr[3])) {
|
|
|
|
|
if (ptr[0] == 'e' && strncmp(ptr+1, "ndfor", 5) == 0) {
|
|
|
|
|
Parse_Error(PARSE_FATAL, "for-less endfor");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
ptr += 3;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* we found a for loop, and now we are going to parse it.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
new_for = bmake_malloc(sizeof *new_for);
|
|
|
|
|
memset(new_for, 0, sizeof *new_for);
|
|
|
|
|
|
2008-12-01 01:37:55 +03:00
|
|
|
|
/* Grab the variables. Terminate on "in". */
|
|
|
|
|
for (;; ptr += len) {
|
1994-06-07 02:45:17 +04:00
|
|
|
|
while (*ptr && isspace((unsigned char) *ptr))
|
1994-03-05 03:34:29 +03:00
|
|
|
|
ptr++;
|
2008-12-01 01:37:55 +03:00
|
|
|
|
if (*ptr == '\0') {
|
|
|
|
|
Parse_Error(PARSE_FATAL, "missing `in' in for");
|
2009-01-11 18:50:06 +03:00
|
|
|
|
For_Free(new_for);
|
2008-12-01 01:37:55 +03:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
for (len = 1; ptr[len] && !isspace((unsigned char)ptr[len]); len++)
|
|
|
|
|
continue;
|
|
|
|
|
if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
|
|
|
|
|
ptr += 2;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-01-11 18:50:06 +03:00
|
|
|
|
if (len == 1)
|
|
|
|
|
new_for->short_var = 1;
|
|
|
|
|
strlist_add_str(&new_for->vars, make_str(ptr, len), len);
|
2008-11-29 20:50:11 +03:00
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
if (strlist_num(&new_for->vars) == 0) {
|
2008-11-29 20:50:11 +03:00
|
|
|
|
Parse_Error(PARSE_FATAL, "no iteration variables in for");
|
2009-01-11 18:50:06 +03:00
|
|
|
|
For_Free(new_for);
|
2008-11-29 20:50:11 +03:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2000-04-17 02:08:06 +04:00
|
|
|
|
|
2008-11-29 20:50:11 +03:00
|
|
|
|
while (*ptr && isspace((unsigned char) *ptr))
|
|
|
|
|
ptr++;
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
2008-11-29 20:50:11 +03:00
|
|
|
|
/*
|
|
|
|
|
* Make a list with the remaining words
|
2009-01-10 19:59:02 +03:00
|
|
|
|
* The values are substituted as ${:U<value>...} so we must \ escape
|
2009-01-13 21:30:00 +03:00
|
|
|
|
* characters that break that syntax.
|
|
|
|
|
* Variables are fully expanded - so it is safe for escape $.
|
|
|
|
|
* We can't do the escapes here - because we don't know whether
|
|
|
|
|
* we are substuting into ${...} or $(...).
|
2008-11-29 20:50:11 +03:00
|
|
|
|
*/
|
|
|
|
|
sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
2008-12-21 01:41:53 +03:00
|
|
|
|
for (ptr = sub;; ptr += len) {
|
2008-12-01 01:37:55 +03:00
|
|
|
|
while (*ptr && isspace((unsigned char)*ptr))
|
|
|
|
|
ptr++;
|
|
|
|
|
if (*ptr == 0)
|
|
|
|
|
break;
|
2008-12-21 21:06:53 +03:00
|
|
|
|
escapes = 0;
|
2009-01-10 19:59:02 +03:00
|
|
|
|
for (len = 0; (ch = ptr[len]) != 0 && !isspace(ch); len++) {
|
2009-01-13 21:30:00 +03:00
|
|
|
|
if (ch == ':' || ch == '$' || ch == '\\')
|
|
|
|
|
escapes |= FOR_SUB_ESCAPE_CHAR;
|
2009-01-10 19:59:02 +03:00
|
|
|
|
else if (ch == ')')
|
|
|
|
|
escapes |= FOR_SUB_ESCAPE_PAREN;
|
|
|
|
|
else if (ch == /*{*/ '}')
|
|
|
|
|
escapes |= FOR_SUB_ESCAPE_BRACE;
|
2008-12-21 21:06:53 +03:00
|
|
|
|
}
|
2009-01-11 18:50:06 +03:00
|
|
|
|
strlist_add_str(&new_for->items, make_str(ptr, len), escapes);
|
2008-12-01 01:37:55 +03:00
|
|
|
|
}
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
2008-12-01 01:37:55 +03:00
|
|
|
|
free(sub);
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
if (strlist_num(&new_for->items) % strlist_num(&new_for->vars)) {
|
2008-12-01 01:37:55 +03:00
|
|
|
|
Parse_Error(PARSE_FATAL,
|
|
|
|
|
"Wrong number of words in .for substitution list %d %d",
|
2009-01-11 18:50:06 +03:00
|
|
|
|
strlist_num(&new_for->items), strlist_num(&new_for->vars));
|
2008-12-01 01:37:55 +03:00
|
|
|
|
/*
|
|
|
|
|
* Return 'success' so that the body of the .for loop is accumulated.
|
2009-01-11 18:50:06 +03:00
|
|
|
|
* Remove all items so that the loop doesn't iterate.
|
2008-12-01 01:37:55 +03:00
|
|
|
|
*/
|
2009-01-11 18:50:06 +03:00
|
|
|
|
strlist_clean(&new_for->items);
|
1994-03-05 03:34:29 +03:00
|
|
|
|
}
|
2008-11-29 20:50:11 +03:00
|
|
|
|
|
2009-01-17 16:29:37 +03:00
|
|
|
|
Buf_Init(&new_for->buf, 0);
|
2009-01-11 18:50:06 +03:00
|
|
|
|
accumFor = new_for;
|
2008-11-29 20:50:11 +03:00
|
|
|
|
forLevel = 1;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 00:05:21 +03:00
|
|
|
|
/*
|
|
|
|
|
* Add another line to a .for loop.
|
2009-01-11 18:50:06 +03:00
|
|
|
|
* Returns 0 when the matching .endfor is reached.
|
2008-12-02 00:05:21 +03:00
|
|
|
|
*/
|
|
|
|
|
|
2008-11-29 20:50:11 +03:00
|
|
|
|
int
|
|
|
|
|
For_Accum(char *line)
|
|
|
|
|
{
|
|
|
|
|
char *ptr = line;
|
2007-01-02 00:31:51 +03:00
|
|
|
|
|
|
|
|
|
if (*ptr == '.') {
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
1994-06-07 02:45:17 +04:00
|
|
|
|
for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
|
1994-03-05 03:34:29 +03:00
|
|
|
|
continue;
|
|
|
|
|
|
1994-06-07 02:45:17 +04:00
|
|
|
|
if (strncmp(ptr, "endfor", 6) == 0 &&
|
2007-01-02 00:31:51 +03:00
|
|
|
|
(isspace((unsigned char) ptr[6]) || !ptr[6])) {
|
1994-03-05 03:34:29 +03:00
|
|
|
|
if (DEBUG(FOR))
|
2006-10-15 12:38:21 +04:00
|
|
|
|
(void)fprintf(debug_file, "For: end for %d\n", forLevel);
|
2008-11-29 20:50:11 +03:00
|
|
|
|
if (--forLevel <= 0)
|
1994-03-05 03:34:29 +03:00
|
|
|
|
return 0;
|
2007-01-02 00:31:51 +03:00
|
|
|
|
} else if (strncmp(ptr, "for", 3) == 0 &&
|
1994-06-07 02:45:17 +04:00
|
|
|
|
isspace((unsigned char) ptr[3])) {
|
1994-03-05 03:34:29 +03:00
|
|
|
|
forLevel++;
|
|
|
|
|
if (DEBUG(FOR))
|
2006-10-15 12:38:21 +04:00
|
|
|
|
(void)fprintf(debug_file, "For: new loop %d\n", forLevel);
|
1994-03-05 03:34:29 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-17 16:29:37 +03:00
|
|
|
|
Buf_AddBytes(&accumFor->buf, strlen(line), line);
|
|
|
|
|
Buf_AddByte(&accumFor->buf, '\n');
|
2008-11-29 20:50:11 +03:00
|
|
|
|
return 1;
|
1994-03-05 03:34:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
* For_Run --
|
2000-04-17 02:08:06 +04:00
|
|
|
|
* Run the for loop, imitating the actions of an include file
|
1994-03-05 03:34:29 +03:00
|
|
|
|
*
|
|
|
|
|
* Results:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
* Side Effects:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
*/
|
2009-01-10 19:59:02 +03:00
|
|
|
|
|
2009-01-15 01:54:10 +03:00
|
|
|
|
static int
|
|
|
|
|
for_var_len(const char *var)
|
|
|
|
|
{
|
|
|
|
|
char ch, var_start, var_end;
|
|
|
|
|
int depth;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
var_start = *var;
|
|
|
|
|
if (var_start == 0)
|
|
|
|
|
/* just escape the $ */
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (var_start == '(')
|
|
|
|
|
var_end = ')';
|
|
|
|
|
else if (var_start == '{')
|
|
|
|
|
var_end = '}';
|
|
|
|
|
else
|
|
|
|
|
/* Single char variable */
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
depth = 1;
|
|
|
|
|
for (len = 1; (ch = var[len++]) != 0;) {
|
|
|
|
|
if (ch == var_start)
|
|
|
|
|
depth++;
|
|
|
|
|
else if (ch == var_end && --depth == 0)
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Variable end not found, escape the $ */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-10 19:59:02 +03:00
|
|
|
|
static void
|
2009-01-17 16:29:37 +03:00
|
|
|
|
for_substitute(Buffer *cmds, strlist_t *items, unsigned int item_no, char ech)
|
2009-01-10 19:59:02 +03:00
|
|
|
|
{
|
|
|
|
|
const char *item = strlist_str(items, item_no);
|
2009-01-15 01:54:10 +03:00
|
|
|
|
int len;
|
2009-01-10 19:59:02 +03:00
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
|
|
/* If there were no escapes, or the only escape is the other variable
|
|
|
|
|
* terminator, then just substitute the full string */
|
|
|
|
|
if (!(strlist_info(items, item_no) &
|
|
|
|
|
(ech == ')' ? ~FOR_SUB_ESCAPE_BRACE : ~FOR_SUB_ESCAPE_PAREN))) {
|
|
|
|
|
Buf_AddBytes(cmds, strlen(item), item);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-13 21:30:00 +03:00
|
|
|
|
/* Escape ':', '$', '\\' and 'ech' - removed by :U processing */
|
2009-01-15 01:54:10 +03:00
|
|
|
|
while ((ch = *item++) != 0) {
|
|
|
|
|
if (ch == '$') {
|
|
|
|
|
len = for_var_len(item);
|
|
|
|
|
if (len != 0) {
|
|
|
|
|
Buf_AddBytes(cmds, len + 1, item - 1);
|
|
|
|
|
item += len;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Buf_AddByte(cmds, '\\');
|
|
|
|
|
} else if (ch == ':' || ch == '\\' || ch == ech)
|
2009-01-10 19:59:02 +03:00
|
|
|
|
Buf_AddByte(cmds, '\\');
|
|
|
|
|
Buf_AddByte(cmds, ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
static char *
|
|
|
|
|
For_Iterate(void *v_arg)
|
1994-03-05 03:34:29 +03:00
|
|
|
|
{
|
2009-01-11 18:50:06 +03:00
|
|
|
|
For *arg = v_arg;
|
2008-12-21 01:41:53 +03:00
|
|
|
|
int i, len;
|
2009-01-10 19:59:02 +03:00
|
|
|
|
char *var;
|
2008-12-21 21:06:53 +03:00
|
|
|
|
char *cp;
|
|
|
|
|
char *cmd_cp;
|
|
|
|
|
char *body_end;
|
|
|
|
|
char ch;
|
|
|
|
|
Buffer cmds;
|
2000-04-17 02:08:06 +04:00
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
if (arg->sub_next + strlist_num(&arg->vars) > strlist_num(&arg->items)) {
|
|
|
|
|
/* No more iterations */
|
|
|
|
|
For_Free(arg);
|
|
|
|
|
return NULL;
|
2000-04-17 02:08:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
free(arg->parse_buf);
|
|
|
|
|
arg->parse_buf = NULL;
|
|
|
|
|
|
2008-12-21 21:06:53 +03:00
|
|
|
|
/*
|
|
|
|
|
* Scan the for loop body and replace references to the loop variables
|
|
|
|
|
* with variable references that expand to the required text.
|
|
|
|
|
* Using variable expansions ensures that the .for loop can't generate
|
|
|
|
|
* syntax, and that the later parsing will still see a variable.
|
|
|
|
|
* We assume that the null variable will never be defined.
|
|
|
|
|
*
|
|
|
|
|
* The detection of substitions of the loop control variable is naive.
|
|
|
|
|
* Many of the modifiers use \ to escape $ (not $) so it is possible
|
|
|
|
|
* to contrive a makefile where an unwanted substitution happens.
|
|
|
|
|
*/
|
2009-01-11 18:50:06 +03:00
|
|
|
|
|
2009-01-17 16:29:37 +03:00
|
|
|
|
cmd_cp = Buf_GetAll(&arg->buf, &len);
|
2009-01-11 18:50:06 +03:00
|
|
|
|
body_end = cmd_cp + len;
|
2009-01-17 16:29:37 +03:00
|
|
|
|
Buf_Init(&cmds, len + 256);
|
2009-01-11 18:50:06 +03:00
|
|
|
|
for (cp = cmd_cp; (cp = strchr(cp, '$')) != NULL;) {
|
|
|
|
|
char ech;
|
|
|
|
|
ch = *++cp;
|
|
|
|
|
if ((ch == '(' && (ech = ')')) || (ch == '{' && (ech = '}'))) {
|
|
|
|
|
cp++;
|
|
|
|
|
/* Check variable name against the .for loop variables */
|
|
|
|
|
STRLIST_FOREACH(var, &arg->vars, i) {
|
|
|
|
|
len = strlist_info(&arg->vars, i);
|
|
|
|
|
if (memcmp(cp, var, len) != 0)
|
2008-12-21 21:06:53 +03:00
|
|
|
|
continue;
|
2009-01-11 18:50:06 +03:00
|
|
|
|
if (cp[len] != ':' && cp[len] != ech && cp[len] != '\\')
|
|
|
|
|
continue;
|
|
|
|
|
/* Found a variable match. Replace with :U<value> */
|
2009-01-17 16:29:37 +03:00
|
|
|
|
Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
|
|
|
|
|
Buf_AddBytes(&cmds, 2, ":U");
|
2009-01-11 18:50:06 +03:00
|
|
|
|
cp += len;
|
|
|
|
|
cmd_cp = cp;
|
2009-01-17 16:29:37 +03:00
|
|
|
|
for_substitute(&cmds, &arg->items, arg->sub_next + i, ech);
|
2008-12-21 21:06:53 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
2009-01-11 18:50:06 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (ch == 0)
|
|
|
|
|
break;
|
|
|
|
|
/* Probably a single character name, ignore $$ and stupid ones. {*/
|
|
|
|
|
if (!arg->short_var || strchr("}):$", ch) != NULL) {
|
|
|
|
|
cp++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
STRLIST_FOREACH(var, &arg->vars, i) {
|
|
|
|
|
if (var[0] != ch || var[1] != 0)
|
|
|
|
|
continue;
|
|
|
|
|
/* Found a variable match. Replace with ${:U<value>} */
|
2009-01-17 16:29:37 +03:00
|
|
|
|
Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
|
|
|
|
|
Buf_AddBytes(&cmds, 3, "{:U");
|
2009-01-11 18:50:06 +03:00
|
|
|
|
cmd_cp = ++cp;
|
2009-01-17 16:29:37 +03:00
|
|
|
|
for_substitute(&cmds, &arg->items, arg->sub_next + i, /*{*/ '}');
|
|
|
|
|
Buf_AddBytes(&cmds, 1, "}");
|
2009-01-11 18:50:06 +03:00
|
|
|
|
break;
|
2008-12-21 21:06:53 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
|
Buf_AddBytes(&cmds, body_end - cmd_cp, cmd_cp);
|
2009-01-11 18:50:06 +03:00
|
|
|
|
|
2009-01-17 16:29:37 +03:00
|
|
|
|
cp = Buf_Destroy(&cmds, FALSE);
|
2009-01-11 18:50:06 +03:00
|
|
|
|
if (DEBUG(FOR))
|
|
|
|
|
(void)fprintf(debug_file, "For: loop body:\n%s", cp);
|
|
|
|
|
|
|
|
|
|
arg->sub_next += strlist_num(&arg->vars);
|
|
|
|
|
|
|
|
|
|
arg->parse_buf = cp;
|
|
|
|
|
return cp;
|
|
|
|
|
}
|
2000-04-17 02:08:06 +04:00
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
void
|
|
|
|
|
For_Run(int lineno)
|
|
|
|
|
{
|
|
|
|
|
For *arg;
|
|
|
|
|
|
|
|
|
|
arg = accumFor;
|
|
|
|
|
accumFor = NULL;
|
1994-03-05 03:34:29 +03:00
|
|
|
|
|
2009-01-11 18:50:06 +03:00
|
|
|
|
if (strlist_num(&arg->items) == 0) {
|
|
|
|
|
/* Nothing to expand - possibly due to an earlier syntax error. */
|
|
|
|
|
For_Free(arg);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Parse_SetInput(NULL, lineno, -1, For_Iterate, arg);
|
1994-03-05 03:34:29 +03:00
|
|
|
|
}
|