2005-02-01 10:50:37 +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.
|
2005-02-01 10:50:37 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00: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.
|
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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-02-01 10:50:37 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "buf.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "for.h"
|
|
|
|
#include "globals.h"
|
|
|
|
#include "lst.h"
|
|
|
|
#include "make.h"
|
|
|
|
#include "parse.h"
|
2008-03-24 12:33:28 +00:00
|
|
|
#include "str.h"
|
2005-02-01 10:50:37 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "var.h"
|
1994-05-27 12:33:43 +00: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
|
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.
|
|
|
|
*/
|
|
|
|
|
2005-02-23 13:58:56 +00:00
|
|
|
static int forLevel = 0; /* Nesting level */
|
|
|
|
static char *forVar; /* Iteration variable */
|
|
|
|
static Buffer *forBuf; /* Commands in loop */
|
|
|
|
static Lst forLst; /* List of items */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-04-11 07:20:10 +00:00
|
|
|
/**
|
|
|
|
* For_For
|
1994-05-27 12:33:43 +00:00
|
|
|
* Evaluate the for loop in the passed line. The line
|
|
|
|
* looks like this:
|
|
|
|
* .for <variable> in <varlist>
|
2005-04-11 07:20:10 +00:00
|
|
|
* The line pointer points just behind the for.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Results:
|
2005-04-11 07:20:10 +00:00
|
|
|
* TRUE: Syntax ok.
|
|
|
|
* FALSE: Syntax error.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-04-11 07:20:10 +00:00
|
|
|
Boolean
|
|
|
|
For_For(char *line)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-02-23 13:58:56 +00:00
|
|
|
char *ptr;
|
|
|
|
char *wrd;
|
2005-04-11 07:20:10 +00:00
|
|
|
char *sub;
|
|
|
|
Buffer *buf;
|
|
|
|
size_t varlen;
|
2008-03-24 12:33:28 +00:00
|
|
|
int i;
|
|
|
|
ArgArray words;
|
2005-02-23 13:58:56 +00:00
|
|
|
|
|
|
|
ptr = line;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-04-11 07:20:10 +00:00
|
|
|
/*
|
|
|
|
* Skip space between for and the variable.
|
|
|
|
*/
|
|
|
|
for (ptr++; *ptr && isspace((u_char)*ptr); ptr++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grab the variable
|
|
|
|
*/
|
|
|
|
for (wrd = ptr; *ptr && !isspace((u_char)*ptr); ptr++)
|
|
|
|
;
|
|
|
|
|
|
|
|
buf = Buf_Init(0);
|
|
|
|
Buf_AppendRange(buf, wrd, ptr);
|
|
|
|
forVar = Buf_GetAll(buf, &varlen);
|
|
|
|
|
|
|
|
if (varlen == 0) {
|
|
|
|
Buf_Destroy(buf, TRUE);
|
|
|
|
Parse_Error(PARSE_FATAL, "missing variable in for");
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip to 'in'.
|
|
|
|
*/
|
|
|
|
while (*ptr && isspace((u_char)*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grab the `in'
|
|
|
|
*/
|
|
|
|
if (ptr[0] != 'i' || ptr[1] != 'n' || !isspace((u_char)ptr[2])) {
|
|
|
|
free(forVar);
|
|
|
|
Parse_Error(PARSE_FATAL, "missing `in' in for");
|
|
|
|
fprintf(stderr, "%s\n", ptr);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
ptr += 3;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip to values
|
|
|
|
*/
|
|
|
|
while (*ptr && isspace((u_char)*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a list with the remaining words
|
|
|
|
*/
|
2005-05-09 14:06:04 +00:00
|
|
|
sub = Buf_Peel(Var_Subst(ptr, VAR_CMD, FALSE));
|
2008-03-24 12:33:28 +00:00
|
|
|
brk_string(&words, sub, FALSE);
|
2005-04-11 07:20:10 +00:00
|
|
|
Lst_Init(&forLst);
|
2008-03-24 12:33:28 +00:00
|
|
|
for (i = 1; i < words.argc; i++) {
|
|
|
|
if (words.argv[i][0] != '\0')
|
|
|
|
Lst_AtFront(&forLst, estrdup(words.argv[i]));
|
2005-04-11 07:20:10 +00:00
|
|
|
}
|
2008-03-24 12:33:28 +00:00
|
|
|
ArgArray_Done(&words);
|
2005-04-11 07:20:10 +00:00
|
|
|
DEBUGF(FOR, ("For: Iterator %s List %s\n", forVar, sub));
|
|
|
|
free(sub);
|
2005-02-23 13:58:56 +00:00
|
|
|
|
2005-04-11 07:20:10 +00:00
|
|
|
forBuf = Buf_Init(0);
|
|
|
|
forLevel++;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
2005-02-23 13:58:56 +00:00
|
|
|
|
2005-04-11 07:20:10 +00:00
|
|
|
/**
|
|
|
|
* For_Eval
|
|
|
|
* Eat a line of the .for body looking for embedded .for loops
|
|
|
|
* and the .endfor
|
|
|
|
*/
|
|
|
|
Boolean
|
|
|
|
For_Eval(char *line)
|
|
|
|
{
|
|
|
|
char *ptr;
|
2005-02-23 13:58:56 +00:00
|
|
|
|
2005-04-11 07:20:10 +00:00
|
|
|
ptr = line;
|
2005-02-23 13:58:56 +00:00
|
|
|
|
|
|
|
if (*ptr == '.') {
|
|
|
|
/*
|
|
|
|
* Need to check for 'endfor' and 'for' to find the end
|
|
|
|
* of our loop or to find embedded for loops.
|
|
|
|
*/
|
2005-04-11 07:20:10 +00:00
|
|
|
for (ptr++; *ptr != '\0' && isspace((u_char)*ptr); ptr++)
|
2005-02-23 13:58:56 +00:00
|
|
|
;
|
|
|
|
|
2005-04-11 07:20:10 +00:00
|
|
|
/* XXX the isspace is wrong */
|
2005-02-23 13:58:56 +00:00
|
|
|
if (strncmp(ptr, "endfor", 6) == 0 &&
|
2005-04-11 07:20:10 +00:00
|
|
|
(isspace((u_char)ptr[6]) || ptr[6] == '\0')) {
|
2005-02-23 13:58:56 +00:00
|
|
|
DEBUGF(FOR, ("For: end for %d\n", forLevel));
|
2005-04-11 07:20:10 +00:00
|
|
|
if (forLevel == 0) {
|
|
|
|
/* should not be here */
|
|
|
|
abort();
|
2005-02-23 13:58:56 +00:00
|
|
|
}
|
2005-04-11 07:20:10 +00:00
|
|
|
forLevel--;
|
2005-02-23 13:58:56 +00:00
|
|
|
|
|
|
|
} else if (strncmp(ptr, "for", 3) == 0 &&
|
2005-04-11 07:20:10 +00:00
|
|
|
isspace((u_char)ptr[3])) {
|
2005-02-23 13:58:56 +00:00
|
|
|
forLevel++;
|
|
|
|
DEBUGF(FOR, ("For: new loop %d\n", forLevel));
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-02-23 13:58:56 +00:00
|
|
|
|
|
|
|
if (forLevel != 0) {
|
|
|
|
/*
|
|
|
|
* Still in loop - append the line
|
|
|
|
*/
|
|
|
|
Buf_Append(forBuf, line);
|
|
|
|
Buf_AddByte(forBuf, (Byte)'\n');
|
2005-04-11 07:20:10 +00:00
|
|
|
return (TRUE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-02-23 13:58:56 +00:00
|
|
|
|
2005-04-11 07:20:10 +00:00
|
|
|
return (FALSE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* For_Run --
|
|
|
|
* Run the for loop, immitating the actions of an include file
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
2005-02-23 14:03:18 +00:00
|
|
|
* The values of the variables forLst, forVar and forBuf are freed.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2004-03-10 21:51:06 +00:00
|
|
|
For_Run(int lineno)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-02-23 14:03:18 +00:00
|
|
|
Lst values; /* list of values for the variable */
|
|
|
|
char *var; /* the variable's name */
|
|
|
|
Buffer *buf; /* the contents of the for loop */
|
|
|
|
const char *val; /* current value of loop variable */
|
|
|
|
LstNode *ln;
|
2005-02-25 13:16:56 +00:00
|
|
|
char *str;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-23 13:58:56 +00:00
|
|
|
if (forVar == NULL || forBuf == NULL)
|
|
|
|
return;
|
2004-12-16 16:14:16 +00:00
|
|
|
|
2005-02-23 14:03:18 +00:00
|
|
|
/* copy the global variables to have them free for embedded fors */
|
|
|
|
var = forVar;
|
|
|
|
buf = forBuf;
|
|
|
|
Lst_Init(&values);
|
|
|
|
Lst_Concat(&values, &forLst, LST_CONCLINK);
|
2004-12-16 16:14:16 +00:00
|
|
|
|
2005-02-23 13:58:56 +00:00
|
|
|
forVar = NULL;
|
|
|
|
forBuf = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-23 14:03:18 +00:00
|
|
|
LST_FOREACH(ln, &values) {
|
|
|
|
val = Lst_Datum(ln);
|
|
|
|
Var_Set(var, val, VAR_GLOBAL);
|
|
|
|
|
|
|
|
DEBUGF(FOR, ("--- %s = %s\n", var, val));
|
2005-05-10 12:02:15 +00:00
|
|
|
str = Buf_Peel(Var_SubstOnly(var, Buf_Data(buf), FALSE));
|
2005-02-23 14:03:18 +00:00
|
|
|
|
2005-02-25 13:16:56 +00:00
|
|
|
Parse_FromString(str, lineno);
|
2005-02-23 14:03:18 +00:00
|
|
|
Var_Delete(var, VAR_GLOBAL);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-23 14:03:18 +00:00
|
|
|
free(var);
|
|
|
|
Lst_Destroy(&values, free);
|
|
|
|
Buf_Destroy(buf, TRUE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|