1994-05-27 12:33:43 +00:00
|
|
|
|
/*
|
2002-04-13 10:57:56 +00:00
|
|
|
|
* Copyright (c) 1993
|
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
|
* Christos Zoulas.
|
1994-05-27 12:33:43 +00:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
|
* 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.
|
2000-07-09 02:54:54 +00:00
|
|
|
|
*
|
|
|
|
|
* @(#)for.c 8.1 (Berkeley) 6/6/93
|
1994-05-27 12:33:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2000-07-09 02:54:54 +00:00
|
|
|
|
#include <sys/cdefs.h>
|
2002-04-13 10:17:18 +00:00
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00: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
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include "make.h"
|
|
|
|
|
#include "hash.h"
|
|
|
|
|
#include "dir.h"
|
|
|
|
|
#include "buf.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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
|
1995-05-30 06:41:30 +00:00
|
|
|
|
* the initial .for loop and the matching .endfor;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
* then we evaluate the for loop for each variable in the varlist.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int forLevel = 0; /* Nesting level */
|
|
|
|
|
static char *forVar; /* Iteration variable */
|
|
|
|
|
static Buffer forBuf; /* Commands in loop */
|
|
|
|
|
static Lst forLst; /* List of items */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* State of a for loop.
|
|
|
|
|
*/
|
1995-01-23 21:03:17 +00:00
|
|
|
|
typedef struct _For {
|
1994-05-27 12:33:43 +00:00
|
|
|
|
Buffer buf; /* Unexpanded buffer */
|
|
|
|
|
char* var; /* Index name */
|
|
|
|
|
Lst lst; /* List of variables */
|
1995-01-23 21:03:17 +00:00
|
|
|
|
} For;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
2002-03-22 01:33:25 +00:00
|
|
|
|
static int ForExec(void *, void *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
* For_Eval --
|
|
|
|
|
* Evaluate the for loop in the passed line. The line
|
|
|
|
|
* looks like this:
|
|
|
|
|
* .for <variable> in <varlist>
|
|
|
|
|
*
|
|
|
|
|
* Results:
|
|
|
|
|
* TRUE: We found a for loop, or we are inside a for loop
|
|
|
|
|
* FALSE: We did not find a for loop, or we found the end of the for
|
|
|
|
|
* for loop.
|
|
|
|
|
*
|
|
|
|
|
* Side Effects:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
int
|
2002-10-09 03:42:10 +00:00
|
|
|
|
For_Eval (char *line)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
{
|
|
|
|
|
char *ptr = line, *sub, *wrd;
|
|
|
|
|
int level; /* Level at which to report errors. */
|
|
|
|
|
|
|
|
|
|
level = PARSE_FATAL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (forLevel == 0) {
|
|
|
|
|
Buffer buf;
|
|
|
|
|
int varlen;
|
|
|
|
|
|
1995-01-23 21:03:17 +00:00
|
|
|
|
for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
/*
|
|
|
|
|
* If we are not in a for loop quickly determine if the statement is
|
|
|
|
|
* a for.
|
|
|
|
|
*/
|
1995-01-23 21:03:17 +00:00
|
|
|
|
if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
|
|
|
|
|
!isspace((unsigned char) ptr[3]))
|
1994-05-27 12:33:43 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
ptr += 3;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
|
/*
|
|
|
|
|
* we found a for loop, and now we are going to parse it.
|
|
|
|
|
*/
|
1995-01-23 21:03:17 +00:00
|
|
|
|
while (*ptr && isspace((unsigned char) *ptr))
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ptr++;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
|
/*
|
|
|
|
|
* Grab the variable
|
|
|
|
|
*/
|
|
|
|
|
buf = Buf_Init(0);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
for (wrd = ptr; *ptr && !isspace((unsigned char) *ptr); ptr++)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd);
|
|
|
|
|
|
|
|
|
|
forVar = (char *) Buf_GetAll(buf, &varlen);
|
|
|
|
|
if (varlen == 0) {
|
|
|
|
|
Parse_Error (level, "missing variable in for");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
|
1995-01-23 21:03:17 +00:00
|
|
|
|
while (*ptr && isspace((unsigned char) *ptr))
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Grab the `in'
|
|
|
|
|
*/
|
1995-01-23 21:03:17 +00:00
|
|
|
|
if (ptr[0] != 'i' || ptr[1] != 'n' ||
|
|
|
|
|
!isspace((unsigned char) ptr[2])) {
|
1994-05-27 12:33:43 +00:00
|
|
|
|
Parse_Error (level, "missing `in' in for");
|
|
|
|
|
printf("%s\n", ptr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
ptr += 3;
|
|
|
|
|
|
1995-01-23 21:03:17 +00:00
|
|
|
|
while (*ptr && isspace((unsigned char) *ptr))
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make a list with the remaining words
|
|
|
|
|
*/
|
|
|
|
|
forLst = Lst_Init(FALSE);
|
|
|
|
|
buf = Buf_Init(0);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
2002-09-17 21:29:06 +00:00
|
|
|
|
#define ADDWORD() \
|
1994-05-27 12:33:43 +00:00
|
|
|
|
Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \
|
|
|
|
|
Buf_AddByte(buf, (Byte) '\0'), \
|
2000-12-02 20:24:42 +00:00
|
|
|
|
Lst_AtFront(forLst, (void *) Buf_GetAll(buf, &varlen)), \
|
1994-05-27 12:33:43 +00:00
|
|
|
|
Buf_Destroy(buf, FALSE)
|
|
|
|
|
|
1995-01-23 21:03:17 +00:00
|
|
|
|
for (ptr = sub; *ptr && isspace((unsigned char) *ptr); ptr++)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (wrd = ptr; *ptr; ptr++)
|
1995-01-23 21:03:17 +00:00
|
|
|
|
if (isspace((unsigned char) *ptr)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ADDWORD();
|
|
|
|
|
buf = Buf_Init(0);
|
1995-01-23 21:03:17 +00:00
|
|
|
|
while (*ptr && isspace((unsigned char) *ptr))
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ptr++;
|
|
|
|
|
wrd = ptr--;
|
|
|
|
|
}
|
2002-09-18 16:13:03 +00:00
|
|
|
|
DEBUGF(FOR, ("For: Iterator %s List %s\n", forVar, sub));
|
1995-05-30 06:41:30 +00:00
|
|
|
|
if (ptr - wrd > 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ADDWORD();
|
|
|
|
|
else
|
|
|
|
|
Buf_Destroy(buf, TRUE);
|
2000-12-02 20:24:42 +00:00
|
|
|
|
free(sub);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
|
forBuf = Buf_Init(0);
|
|
|
|
|
forLevel++;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (*ptr == '.') {
|
|
|
|
|
|
1995-01-23 21:03:17 +00:00
|
|
|
|
for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
1995-01-23 21:03:17 +00:00
|
|
|
|
if (strncmp(ptr, "endfor", 6) == 0 &&
|
|
|
|
|
(isspace((unsigned char) ptr[6]) || !ptr[6])) {
|
2002-09-18 16:13:03 +00:00
|
|
|
|
DEBUGF(FOR, ("For: end for %d\n", forLevel));
|
1994-05-27 12:33:43 +00:00
|
|
|
|
if (--forLevel < 0) {
|
|
|
|
|
Parse_Error (level, "for-less endfor");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
1995-01-23 21:03:17 +00:00
|
|
|
|
else if (strncmp(ptr, "for", 3) == 0 &&
|
|
|
|
|
isspace((unsigned char) ptr[3])) {
|
1994-05-27 12:33:43 +00:00
|
|
|
|
forLevel++;
|
2002-09-18 16:13:03 +00:00
|
|
|
|
DEBUGF(FOR, ("For: new loop %d\n", forLevel));
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (forLevel != 0) {
|
|
|
|
|
Buf_AddBytes(forBuf, strlen(line), (Byte *) line);
|
|
|
|
|
Buf_AddByte(forBuf, (Byte) '\n');
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
* ForExec --
|
|
|
|
|
* Expand the for loop for this index and push it in the Makefile
|
|
|
|
|
*
|
|
|
|
|
* Results:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
* Side Effects:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2002-10-09 03:42:10 +00:00
|
|
|
|
ForExec(void *namep, void *argp)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
{
|
1995-01-23 21:03:17 +00:00
|
|
|
|
char *name = (char *) namep;
|
|
|
|
|
For *arg = (For *) argp;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
int len;
|
|
|
|
|
Var_Set(arg->var, name, VAR_GLOBAL);
|
2002-09-18 16:13:03 +00:00
|
|
|
|
DEBUGF(FOR, ("--- %s = %s\n", arg->var, name));
|
1995-05-30 06:41:30 +00:00
|
|
|
|
Parse_FromString(Var_Subst(arg->var, (char *) Buf_GetAll(arg->buf, &len),
|
1994-05-27 12:33:43 +00:00
|
|
|
|
VAR_GLOBAL, FALSE));
|
|
|
|
|
Var_Delete(arg->var, VAR_GLOBAL);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
* For_Run --
|
|
|
|
|
* Run the for loop, immitating the actions of an include file
|
|
|
|
|
*
|
|
|
|
|
* Results:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
* Side Effects:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
void
|
2002-10-09 03:42:10 +00:00
|
|
|
|
For_Run(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
{
|
1995-01-23 21:03:17 +00:00
|
|
|
|
For arg;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
|
|
if (forVar == NULL || forBuf == NULL || forLst == NULL)
|
|
|
|
|
return;
|
|
|
|
|
arg.var = forVar;
|
|
|
|
|
arg.buf = forBuf;
|
|
|
|
|
arg.lst = forLst;
|
|
|
|
|
forVar = NULL;
|
|
|
|
|
forBuf = NULL;
|
|
|
|
|
forLst = NULL;
|
|
|
|
|
|
2000-12-02 20:24:42 +00:00
|
|
|
|
Lst_ForEach(arg.lst, ForExec, (void *) &arg);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
2000-12-02 20:24:42 +00:00
|
|
|
|
free(arg.var);
|
2002-03-22 01:33:25 +00:00
|
|
|
|
Lst_Destroy(arg.lst, (void (*)(void *)) free);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
Buf_Destroy(arg.buf, TRUE);
|
|
|
|
|
}
|