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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)for.c 8.1 (Berkeley) 6/6/93
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 * Functions to handle loops in a makefile.
43 * For_Eval Evaluate the loop in the passed line.
44 * For_Run Run accumulated loop
64 * For statements are of the form:
66 * .for <variable> in <varlist>
70 * The trick is to look for the matching end inside for for loop
71 * To do that, we count the current nesting level of the for loops.
72 * and the .endfor statements, accumulating all the statements between
73 * the initial .for loop and the matching .endfor;
74 * then we evaluate the for loop for each variable in the varlist.
77 static int forLevel = 0; /* Nesting level */
78 static char *forVar; /* Iteration variable */
79 static Buffer *forBuf; /* Commands in loop */
80 static Lst forLst; /* List of items */
84 * Evaluate the for loop in the passed line. The line
86 * .for <variable> in <varlist>
87 * The line pointer points just behind the for.
91 * FALSE: Syntax error.
107 * Skip space between for and the variable.
109 for (ptr++; *ptr && isspace((u_char)*ptr); ptr++)
115 for (wrd = ptr; *ptr && !isspace((u_char)*ptr); ptr++)
119 Buf_AppendRange(buf, wrd, ptr);
120 forVar = Buf_GetAll(buf, &varlen);
123 Buf_Destroy(buf, TRUE);
124 Parse_Error(PARSE_FATAL, "missing variable in for");
127 Buf_Destroy(buf, FALSE);
132 while (*ptr && isspace((u_char)*ptr))
138 if (ptr[0] != 'i' || ptr[1] != 'n' || !isspace((u_char)ptr[2])) {
140 Parse_Error(PARSE_FATAL, "missing `in' in for");
141 fprintf(stderr, "%s\n", ptr);
149 while (*ptr && isspace((u_char)*ptr))
153 * Make a list with the remaining words
155 sub = Buf_Peel(Var_Subst(ptr, VAR_CMD, FALSE));
156 brk_string(&words, sub, FALSE);
158 for (i = 1; i < words.argc; i++) {
159 if (words.argv[i][0] != '\0')
160 Lst_AtFront(&forLst, estrdup(words.argv[i]));
162 ArgArray_Done(&words);
163 DEBUGF(FOR, ("For: Iterator %s List %s\n", forVar, sub));
166 forBuf = Buf_Init(0);
173 * Eat a line of the .for body looking for embedded .for loops
185 * Need to check for 'endfor' and 'for' to find the end
186 * of our loop or to find embedded for loops.
188 for (ptr++; *ptr != '\0' && isspace((u_char)*ptr); ptr++)
191 /* XXX the isspace is wrong */
192 if (strncmp(ptr, "endfor", 6) == 0 &&
193 (isspace((u_char)ptr[6]) || ptr[6] == '\0')) {
194 DEBUGF(FOR, ("For: end for %d\n", forLevel));
196 /* should not be here */
201 } else if (strncmp(ptr, "for", 3) == 0 &&
202 isspace((u_char)ptr[3])) {
204 DEBUGF(FOR, ("For: new loop %d\n", forLevel));
210 * Still in loop - append the line
212 Buf_Append(forBuf, line);
213 Buf_AddByte(forBuf, (Byte)'\n');
221 *-----------------------------------------------------------------------
223 * Run the for loop, immitating the actions of an include file
229 * The values of the variables forLst, forVar and forBuf are freed.
231 *-----------------------------------------------------------------------
236 Lst values; /* list of values for the variable */
237 char *var; /* the variable's name */
238 Buffer *buf; /* the contents of the for loop */
239 const char *val; /* current value of loop variable */
243 if (forVar == NULL || forBuf == NULL)
246 /* copy the global variables to have them free for embedded fors */
250 Lst_Concat(&values, &forLst, LST_CONCLINK);
255 LST_FOREACH(ln, &values) {
257 Var_SetGlobal(var, val);
259 DEBUGF(FOR, ("--- %s = %s\n", var, val));
260 str = Buf_Peel(Var_SubstOnly(var, Buf_Data(buf), FALSE));
262 Parse_FromString(str, lineno);
263 Var_Delete(var, VAR_GLOBAL);
267 Lst_Destroy(&values, free);
268 Buf_Destroy(buf, TRUE);