I missed this file. We are running 9.3.4-P1 now, not 9.3.4.
[dfdiff.git] / usr.bin / make / for.c
blob3dabb0b515aaed9466195f8ee8f8adb079812e65
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
32 * @(#)for.c 8.1 (Berkeley) 6/6/93
33 * $FreeBSD: src/usr.bin/make/for.c,v 1.35 2005/02/10 14:39:05 harti Exp $
34 * $DragonFly: src/usr.bin/make/for.c,v 1.47 2005/09/24 07:38:03 okumoto Exp $
37 /*-
38 * for.c --
39 * Functions to handle loops in a makefile.
41 * Interface:
42 * For_Eval Evaluate the loop in the passed line.
43 * For_Run Run accumulated loop
47 #include <ctype.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include "buf.h"
52 #include "dir.h"
53 #include "for.h"
54 #include "globals.h"
55 #include "lst.h"
56 #include "make.h"
57 #include "parse.h"
58 #include "util.h"
59 #include "var.h"
62 * For statements are of the form:
64 * .for <variable> in <varlist>
65 * ...
66 * .endfor
68 * The trick is to look for the matching end inside for for loop
69 * To do that, we count the current nesting level of the for loops.
70 * and the .endfor statements, accumulating all the statements between
71 * the initial .for loop and the matching .endfor;
72 * then we evaluate the for loop for each variable in the varlist.
75 static int forLevel = 0; /* Nesting level */
76 static char *forVar; /* Iteration variable */
77 static Buffer *forBuf; /* Commands in loop */
78 static Lst forLst; /* List of items */
80 /**
81 * For_For
82 * Evaluate the for loop in the passed line. The line
83 * looks like this:
84 * .for <variable> in <varlist>
85 * The line pointer points just behind the for.
87 * Results:
88 * true: Syntax ok.
89 * false: Syntax error.
91 bool
92 For_For(char *line)
94 char *ptr;
95 char *wrd;
96 char *sub;
97 Buffer *buf;
99 ptr = line;
102 * Skip space between for and the variable.
104 for (ptr++; *ptr && isspace((u_char)*ptr); ptr++)
108 * Grab the variable
110 for (wrd = ptr; *ptr && !isspace((u_char)*ptr); ptr++)
113 buf = Buf_Init(0);
114 Buf_AppendRange(buf, wrd, ptr);
115 forVar = Buf_Data(buf);
117 if (Buf_Size(buf) == 0) {
118 Buf_Destroy(buf, true);
119 Parse_Error(PARSE_FATAL, "missing variable in for");
120 return (false);
122 Buf_Destroy(buf, false);
125 * Skip to 'in'.
127 while (*ptr && isspace((u_char)*ptr))
128 ptr++;
131 * Grab the `in'
133 if (ptr[0] != 'i' || ptr[1] != 'n' || !isspace((u_char)ptr[2])) {
134 free(forVar);
135 Parse_Error(PARSE_FATAL, "missing `in' in for");
136 fprintf(stderr, "%s\n", ptr);
137 return (false);
139 ptr += 3;
142 * Skip to values
144 while (*ptr && isspace((u_char)*ptr))
145 ptr++;
148 * Make a list with the remaining words
150 sub = Buf_Peel(Var_Subst(ptr, VAR_CMD, false));
151 for (ptr = sub; *ptr != '\0' && isspace((u_char)*ptr); ptr++)
154 Lst_Init(&forLst);
155 buf = Buf_Init(0);
156 for (wrd = ptr; *ptr != '\0'; ptr++) {
157 if (isspace((u_char)*ptr)) {
158 Buf_AppendRange(buf, wrd, ptr);
159 Lst_AtFront(&forLst, Buf_Peel(buf));
161 buf = Buf_Init(0);
162 while (*ptr != '\0' && isspace((u_char)*ptr))
163 ptr++;
164 wrd = ptr--;
167 DEBUGF(FOR, ("For: Iterator %s List %s\n", forVar, sub));
169 if (ptr - wrd > 0) {
170 Buf_AppendRange(buf, wrd, ptr);
171 Lst_AtFront(&forLst, Buf_Peel(buf));
172 } else {
173 Buf_Destroy(buf, true);
175 free(sub);
177 forBuf = Buf_Init(0);
178 forLevel++;
179 return (true);
183 * For_Eval
184 * Eat a line of the .for body looking for embedded .for loops
185 * and the .endfor
187 bool
188 For_Eval(char *line)
190 char *ptr;
192 ptr = line;
194 if (*ptr == '.') {
196 * Need to check for 'endfor' and 'for' to find the end
197 * of our loop or to find embedded for loops.
199 for (ptr++; *ptr != '\0' && isspace((u_char)*ptr); ptr++)
202 /* XXX the isspace is wrong */
203 if (strncmp(ptr, "endfor", 6) == 0 &&
204 (isspace((u_char)ptr[6]) || ptr[6] == '\0')) {
205 DEBUGF(FOR, ("For: end for %d\n", forLevel));
206 if (forLevel == 0) {
207 /* should not be here */
208 abort();
210 forLevel--;
212 } else if (strncmp(ptr, "for", 3) == 0 &&
213 isspace((u_char)ptr[3])) {
214 forLevel++;
215 DEBUGF(FOR, ("For: new loop %d\n", forLevel));
219 if (forLevel != 0) {
221 * Still in loop - append the line
223 Buf_Append(forBuf, line);
224 Buf_AddByte(forBuf, '\n');
225 return (true);
228 return (false);
232 *-----------------------------------------------------------------------
233 * For_Run --
234 * Run the for loop, immitating the actions of an include file
236 * Results:
237 * None.
239 * Side Effects:
240 * The values of the variables forLst, forVar and forBuf are freed.
242 *-----------------------------------------------------------------------
244 void
245 For_Run(int lineno)
247 Lst values; /* list of values for the variable */
248 char *var; /* the variable's name */
249 Buffer *buf; /* the contents of the for loop */
250 const char *val; /* current value of loop variable */
251 LstNode *ln;
252 char *str;
254 if (forVar == NULL || forBuf == NULL)
255 return;
257 /* copy the global variables to have them free for embedded fors */
258 var = forVar;
259 buf = forBuf;
260 Lst_Init(&values);
261 Lst_Concat(&values, &forLst, LST_CONCLINK);
263 forVar = NULL;
264 forBuf = NULL;
266 LST_FOREACH(ln, &values) {
267 val = Lst_Datum(ln);
268 Var_SetGlobal(var, val);
270 DEBUGF(FOR, ("--- %s = %s\n", var, val));
271 str = Buf_Peel(Var_SubstOnly(var, Buf_Data(buf), false));
273 Parse_FromString(str, lineno);
274 Var_Delete(var, VAR_GLOBAL);
277 free(var);
278 Lst_Destroy(&values, free);
279 Buf_Destroy(buf, true);