3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)for.c 8.1 (Berkeley) 6/6/93
37 * $FreeBSD: src/usr.bin/make/for.c,v 1.10 1999/09/11 13:08:01 hoek Exp $
38 * $DragonFly: src/usr.bin/make/for.c,v 1.23 2005/01/24 05:13:58 okumoto Exp $
43 * Functions to handle loops in a makefile.
46 * For_Eval Evaluate the loop in the passed line.
47 * For_Run Run accumulated loop
66 * For statements are of the form:
68 * .for <variable> in <varlist>
72 * The trick is to look for the matching end inside for for loop
73 * To do that, we count the current nesting level of the for loops.
74 * and the .endfor statements, accumulating all the statements between
75 * the initial .for loop and the matching .endfor;
76 * then we evaluate the for loop for each variable in the varlist.
79 static int forLevel = 0; /* Nesting level */
80 static char *forVar; /* Iteration variable */
81 static Buffer *forBuf; /* Commands in loop */
82 static Lst forLst; /* List of items */
85 * State of a for loop.
88 Buffer *buf; /* Unexpanded buffer */
89 char* var; /* Index name */
90 Lst lst; /* List of variables */
91 int lineno; /* Line # */
94 static int ForExec(void *, void *);
97 *-----------------------------------------------------------------------
99 * Evaluate the for loop in the passed line. The line
101 * .for <variable> in <varlist>
104 * TRUE: We found a for loop, or we are inside a for loop
105 * FALSE: We did not find a for loop, or we found the end of the for
111 *-----------------------------------------------------------------------
116 char *ptr = line, *sub, *wrd;
117 int level; /* Level at which to report errors. */
126 for (ptr++; *ptr && isspace((unsigned char)*ptr); ptr++)
129 * If we are not in a for loop quickly determine if the statement is
132 if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
133 !isspace((unsigned char)ptr[3]))
138 * we found a for loop, and now we are going to parse it.
140 while (*ptr && isspace((unsigned char)*ptr))
147 for (wrd = ptr; *ptr && !isspace((unsigned char)*ptr); ptr++)
149 Buf_AppendRange(buf, wrd, ptr);
151 forVar = (char *)Buf_GetAll(buf, &varlen);
153 Parse_Error(level, "missing variable in for");
156 Buf_Destroy(buf, FALSE);
158 while (*ptr && isspace((unsigned char)*ptr))
164 if (ptr[0] != 'i' || ptr[1] != 'n' ||
165 !isspace((unsigned char)ptr[2])) {
166 Parse_Error(level, "missing `in' in for");
172 while (*ptr && isspace((unsigned char)*ptr))
176 * Make a list with the remaining words
180 sub = Var_Subst(NULL, ptr, VAR_CMD, FALSE);
182 for (ptr = sub; *ptr && isspace((unsigned char)*ptr); ptr++)
185 for (wrd = ptr; *ptr; ptr++)
186 if (isspace((unsigned char)*ptr)) {
187 Buf_AppendRange(buf, wrd, ptr);
188 Lst_AtFront(&forLst, Buf_GetAll(buf, &varlen));
189 Buf_Destroy(buf, FALSE);
191 while (*ptr && isspace((unsigned char)*ptr))
195 DEBUGF(FOR, ("For: Iterator %s List %s\n", forVar, sub));
197 Buf_AppendRange(buf, wrd, ptr);
198 Lst_AtFront(&forLst, Buf_GetAll(buf, &varlen));
199 Buf_Destroy(buf, FALSE);
201 Buf_Destroy(buf, TRUE);
205 forBuf = Buf_Init(0);
209 else if (*ptr == '.') {
211 for (ptr++; *ptr && isspace((unsigned char)*ptr); ptr++)
214 if (strncmp(ptr, "endfor", 6) == 0 &&
215 (isspace((unsigned char)ptr[6]) || !ptr[6])) {
216 DEBUGF(FOR, ("For: end for %d\n", forLevel));
217 if (--forLevel < 0) {
218 Parse_Error(level, "for-less endfor");
222 else if (strncmp(ptr, "for", 3) == 0 &&
223 isspace((unsigned char)ptr[3])) {
225 DEBUGF(FOR, ("For: new loop %d\n", forLevel));
230 Buf_Append(forBuf, line);
231 Buf_AddByte(forBuf, (Byte)'\n');
240 *-----------------------------------------------------------------------
242 * Expand the for loop for this index and push it in the Makefile
250 *-----------------------------------------------------------------------
253 ForExec(void *namep, void *argp)
258 Var_Set(arg->var, name, VAR_GLOBAL);
259 DEBUGF(FOR, ("--- %s = %s\n", arg->var, name));
260 Parse_FromString(Var_Subst(arg->var, (char *)Buf_GetAll(arg->buf, NULL),
261 VAR_GLOBAL, FALSE), arg->lineno);
262 Var_Delete(arg->var, VAR_GLOBAL);
268 *-----------------------------------------------------------------------
270 * Run the for loop, immitating the actions of an include file
278 *-----------------------------------------------------------------------
285 if (forVar == NULL || forBuf == NULL)
290 /* move the forLst to the arg to get it free for nested for's */
292 Lst_Concat(&arg.lst, &forLst, LST_CONCLINK);
298 Lst_ForEach(&arg.lst, ForExec, &arg);
301 Lst_Destroy(&arg.lst, free);
302 Buf_Destroy(arg.buf, TRUE);