for.c revision 1.33 1 1.33 dsl /* $NetBSD: for.c,v 1.33 2008/11/30 22:37:55 dsl Exp $ */
2 1.3 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1992, The Regents of the University of California.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.15 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.17 ross #ifndef MAKE_NATIVE
33 1.33 dsl static char rcsid[] = "$NetBSD: for.c,v 1.33 2008/11/30 22:37:55 dsl Exp $";
34 1.6 lukem #else
35 1.5 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.3 christos #if 0
38 1.4 christos static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93";
39 1.3 christos #else
40 1.33 dsl __RCSID("$NetBSD: for.c,v 1.33 2008/11/30 22:37:55 dsl Exp $");
41 1.3 christos #endif
42 1.1 cgd #endif /* not lint */
43 1.6 lukem #endif
44 1.1 cgd
45 1.1 cgd /*-
46 1.1 cgd * for.c --
47 1.1 cgd * Functions to handle loops in a makefile.
48 1.1 cgd *
49 1.1 cgd * Interface:
50 1.1 cgd * For_Eval Evaluate the loop in the passed line.
51 1.1 cgd * For_Run Run accumulated loop
52 1.1 cgd *
53 1.1 cgd */
54 1.1 cgd
55 1.13 wiz #include <assert.h>
56 1.1 cgd #include <ctype.h>
57 1.13 wiz
58 1.1 cgd #include "make.h"
59 1.1 cgd #include "hash.h"
60 1.1 cgd #include "dir.h"
61 1.1 cgd #include "buf.h"
62 1.1 cgd
63 1.1 cgd /*
64 1.1 cgd * For statements are of the form:
65 1.1 cgd *
66 1.1 cgd * .for <variable> in <varlist>
67 1.1 cgd * ...
68 1.1 cgd * .endfor
69 1.1 cgd *
70 1.1 cgd * The trick is to look for the matching end inside for for loop
71 1.1 cgd * To do that, we count the current nesting level of the for loops.
72 1.1 cgd * and the .endfor statements, accumulating all the statements between
73 1.4 christos * the initial .for loop and the matching .endfor;
74 1.1 cgd * then we evaluate the for loop for each variable in the varlist.
75 1.7 christos *
76 1.7 christos * Note that any nested fors are just passed through; they get handled
77 1.7 christos * recursively in For_Eval when we're expanding the enclosing for in
78 1.7 christos * For_Run.
79 1.1 cgd */
80 1.1 cgd
81 1.1 cgd static int forLevel = 0; /* Nesting level */
82 1.1 cgd
83 1.1 cgd /*
84 1.1 cgd * State of a for loop.
85 1.1 cgd */
86 1.2 jtc typedef struct _For {
87 1.7 christos Buffer buf; /* Body of loop */
88 1.7 christos char **vars; /* Iteration variables */
89 1.7 christos int nvars; /* # of iteration vars */
90 1.33 dsl int nitem; /* # of substitution items */
91 1.7 christos Lst lst; /* List of items */
92 1.2 jtc } For;
93 1.1 cgd
94 1.7 christos static For accumFor; /* Loop being accumulated */
95 1.1 cgd
96 1.13 wiz static void ForAddVar(const char *, size_t);
97 1.1 cgd
98 1.1 cgd
99 1.1 cgd
100 1.7 christos
102 1.33 dsl static char *
103 1.33 dsl make_str(const char *ptr, int len)
104 1.33 dsl {
105 1.33 dsl char *new_ptr;
106 1.33 dsl
107 1.33 dsl new_ptr = bmake_malloc(len + 1);
108 1.33 dsl memcpy(new_ptr, ptr, len);
109 1.33 dsl new_ptr[len] = 0;
110 1.33 dsl return new_ptr;
111 1.33 dsl }
112 1.7 christos
113 1.7 christos /*-
114 1.7 christos *-----------------------------------------------------------------------
115 1.7 christos * ForAddVar --
116 1.7 christos * Add an iteration variable to the currently accumulating for.
117 1.7 christos *
118 1.7 christos * Results: none
119 1.7 christos * Side effects: no additional side effects.
120 1.7 christos *-----------------------------------------------------------------------
121 1.7 christos */
122 1.13 wiz static void
123 1.7 christos ForAddVar(const char *data, size_t len)
124 1.33 dsl {
125 1.7 christos int nvars;
126 1.33 dsl
127 1.33 dsl nvars = accumFor.nvars;
128 1.33 dsl accumFor.nvars = nvars + 1;
129 1.7 christos accumFor.vars = bmake_realloc(accumFor.vars, nvars * sizeof(char *));
130 1.33 dsl
131 1.7 christos accumFor.vars[nvars] = make_str(data, len);
132 1.7 christos }
133 1.1 cgd
134 1.1 cgd /*-
135 1.1 cgd *-----------------------------------------------------------------------
136 1.1 cgd * For_Eval --
137 1.1 cgd * Evaluate the for loop in the passed line. The line
138 1.1 cgd * looks like this:
139 1.1 cgd * .for <variable> in <varlist>
140 1.13 wiz *
141 1.13 wiz * Input:
142 1.13 wiz * line Line to parse
143 1.1 cgd *
144 1.1 cgd * Results:
145 1.1 cgd * TRUE: We found a for loop, or we are inside a for loop
146 1.1 cgd * FALSE: We did not find a for loop, or we found the end of the for
147 1.1 cgd * for loop.
148 1.1 cgd *
149 1.1 cgd * Side Effects:
150 1.1 cgd * None.
151 1.1 cgd *
152 1.1 cgd *-----------------------------------------------------------------------
153 1.1 cgd */
154 1.13 wiz int
155 1.1 cgd For_Eval(char *line)
156 1.33 dsl {
157 1.33 dsl char *ptr = line, *sub;
158 1.33 dsl int len;
159 1.33 dsl
160 1.33 dsl /* Forget anything we previously knew about - it cannot be useful */
161 1.32 dsl memset(&accumFor, 0, sizeof accumFor);
162 1.32 dsl
163 1.32 dsl forLevel = 0;
164 1.32 dsl for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
165 1.32 dsl continue;
166 1.32 dsl /*
167 1.32 dsl * If we are not in a for loop quickly determine if the statement is
168 1.32 dsl * a for.
169 1.32 dsl */
170 1.32 dsl if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
171 1.32 dsl !isspace((unsigned char) ptr[3])) {
172 1.32 dsl if (ptr[0] == 'e' && strncmp(ptr+1, "ndfor", 5) == 0) {
173 1.32 dsl Parse_Error(PARSE_FATAL, "for-less endfor");
174 1.32 dsl return -1;
175 1.32 dsl }
176 1.32 dsl return 0;
177 1.32 dsl }
178 1.1 cgd ptr += 3;
179 1.32 dsl
180 1.32 dsl /*
181 1.32 dsl * we found a for loop, and now we are going to parse it.
182 1.1 cgd */
183 1.33 dsl
184 1.33 dsl /* Grab the variables. Terminate on "in". */
185 1.32 dsl for (;; ptr += len) {
186 1.32 dsl while (*ptr && isspace((unsigned char) *ptr))
187 1.33 dsl ptr++;
188 1.33 dsl if (*ptr == '\0') {
189 1.33 dsl Parse_Error(PARSE_FATAL, "missing `in' in for");
190 1.33 dsl return -1;
191 1.33 dsl }
192 1.33 dsl for (len = 1; ptr[len] && !isspace((unsigned char)ptr[len]); len++)
193 1.33 dsl continue;
194 1.33 dsl if (len == 2 && ptr[0] == 'i' && ptr[1] == 'n') {
195 1.33 dsl ptr += 2;
196 1.33 dsl break;
197 1.33 dsl }
198 1.32 dsl ForAddVar(ptr, len);
199 1.1 cgd }
200 1.32 dsl
201 1.32 dsl if (accumFor.nvars == 0) {
202 1.32 dsl Parse_Error(PARSE_FATAL, "no iteration variables in for");
203 1.32 dsl return -1;
204 1.4 christos }
205 1.32 dsl
206 1.32 dsl while (*ptr && isspace((unsigned char) *ptr))
207 1.32 dsl ptr++;
208 1.32 dsl
209 1.32 dsl /*
210 1.32 dsl * Make a list with the remaining words
211 1.32 dsl */
212 1.32 dsl accumFor.lst = Lst_Init(FALSE);
213 1.4 christos sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
214 1.33 dsl
215 1.33 dsl for (ptr = sub;; ptr += len, accumFor.nitem++) {
216 1.33 dsl while (*ptr && isspace((unsigned char)*ptr))
217 1.33 dsl ptr++;
218 1.33 dsl if (*ptr == 0)
219 1.33 dsl break;
220 1.33 dsl for (len = 1; ptr[len] && !isspace((unsigned char)ptr[len]); len++)
221 1.33 dsl continue;
222 1.33 dsl Lst_AtFront(accumFor.lst, make_str(ptr, len));
223 1.1 cgd }
224 1.33 dsl
225 1.7 christos free(sub);
226 1.33 dsl
227 1.33 dsl if (accumFor.nitem % accumFor.nvars) {
228 1.33 dsl Parse_Error(PARSE_FATAL,
229 1.33 dsl "Wrong number of words in .for substitution list %d %d",
230 1.33 dsl accumFor.nitem, accumFor.nvars);
231 1.33 dsl /*
232 1.33 dsl * Return 'success' so that the body of the .for loop is accumulated.
233 1.33 dsl * The loop will have zero iterations expanded due a later test.
234 1.32 dsl */
235 1.32 dsl }
236 1.32 dsl
237 1.32 dsl accumFor.buf = Buf_Init(0);
238 1.32 dsl forLevel = 1;
239 1.32 dsl return 1;
240 1.7 christos }
241 1.32 dsl
242 1.32 dsl int
243 1.32 dsl For_Accum(char *line)
244 1.32 dsl {
245 1.26 dsl char *ptr = line;
246 1.26 dsl
247 1.1 cgd if (*ptr == '.') {
248 1.2 jtc
249 1.1 cgd for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
250 1.1 cgd continue;
251 1.2 jtc
252 1.26 dsl if (strncmp(ptr, "endfor", 6) == 0 &&
253 1.1 cgd (isspace((unsigned char) ptr[6]) || !ptr[6])) {
254 1.23 dsl if (DEBUG(FOR))
255 1.32 dsl (void)fprintf(debug_file, "For: end for %d\n", forLevel);
256 1.1 cgd if (--forLevel <= 0)
257 1.26 dsl return 0;
258 1.2 jtc } else if (strncmp(ptr, "for", 3) == 0 &&
259 1.1 cgd isspace((unsigned char) ptr[3])) {
260 1.1 cgd forLevel++;
261 1.23 dsl if (DEBUG(FOR))
262 1.1 cgd (void)fprintf(debug_file, "For: new loop %d\n", forLevel);
263 1.1 cgd }
264 1.1 cgd }
265 1.32 dsl
266 1.32 dsl Buf_AddBytes(accumFor.buf, strlen(line), (Byte *)line);
267 1.32 dsl Buf_AddByte(accumFor.buf, (Byte)'\n');
268 1.1 cgd return 1;
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd
272 1.1 cgd /*-
274 1.7 christos *-----------------------------------------------------------------------
275 1.1 cgd * For_Run --
276 1.1 cgd * Run the for loop, imitating the actions of an include file
277 1.1 cgd *
278 1.1 cgd * Results:
279 1.1 cgd * None.
280 1.1 cgd *
281 1.1 cgd * Side Effects:
282 1.1 cgd * None.
283 1.1 cgd *
284 1.1 cgd *-----------------------------------------------------------------------
285 1.16 enami */
286 1.1 cgd void
287 1.2 jtc For_Run(int lineno)
288 1.7 christos {
289 1.7 christos For arg;
290 1.28 christos LstNode ln;
291 1.10 mycroft char **values;
292 1.7 christos int i, done = 0, len;
293 1.7 christos char *guy, *orig_guy, *old_guy;
294 1.7 christos
295 1.7 christos arg = accumFor;
296 1.7 christos accumFor.buf = NULL;
297 1.7 christos accumFor.vars = NULL;
298 1.1 cgd accumFor.nvars = 0;
299 1.33 dsl accumFor.lst = NULL;
300 1.33 dsl
301 1.33 dsl if (arg.nitem % arg.nvars)
302 1.33 dsl /* Error message already printed */
303 1.7 christos return;
304 1.1 cgd
305 1.1 cgd if (Lst_Open(arg.lst) != SUCCESS)
306 1.30 joerg return;
307 1.7 christos
308 1.7 christos values = bmake_malloc(arg.nvars * sizeof(char *));
309 1.7 christos
310 1.7 christos while (!done) {
311 1.7 christos /*
312 1.7 christos * due to the dumb way this is set up, this loop must run
313 1.7 christos * backwards.
314 1.7 christos */
315 1.7 christos for (i = arg.nvars - 1; i >= 0; i--) {
316 1.7 christos ln = Lst_Next(arg.lst);
317 1.7 christos if (ln == NILLNODE) {
318 1.7 christos done = 1;
319 1.19 christos break;
320 1.7 christos } else {
321 1.7 christos values[i] = (char *)Lst_Datum(ln);
322 1.7 christos }
323 1.7 christos }
324 1.7 christos if (done)
325 1.7 christos break;
326 1.11 sjg
327 1.7 christos for (i = 0; i < arg.nvars; i++) {
328 1.23 dsl Var_Set(arg.vars[i], values[i], VAR_GLOBAL, 0);
329 1.7 christos if (DEBUG(FOR))
330 1.7 christos (void)fprintf(debug_file, "--- %s = %s\n", arg.vars[i],
331 1.7 christos values[i]);
332 1.7 christos }
333 1.7 christos
334 1.7 christos /*
335 1.7 christos * Hack, hack, kludge.
336 1.7 christos * This is really ugly, but to do it any better way would require
337 1.7 christos * making major changes to var.c, which I don't want to get into
338 1.7 christos * yet. There is no mechanism for expanding some variables, only
339 1.7 christos * for expanding a single variable. That should be corrected, but
340 1.7 christos * not right away. (XXX)
341 1.19 christos */
342 1.10 mycroft
343 1.10 mycroft guy = (char *)Buf_GetAll(arg.buf, &len);
344 1.10 mycroft orig_guy = guy;
345 1.10 mycroft for (i = 0; i < arg.nvars; i++) {
346 1.10 mycroft old_guy = guy;
347 1.10 mycroft guy = Var_Subst(arg.vars[i], guy, VAR_GLOBAL, FALSE);
348 1.10 mycroft if (old_guy != orig_guy)
349 1.25 dsl free(old_guy);
350 1.7 christos }
351 1.7 christos Parse_SetInput(NULL, lineno, -1, guy);
352 1.10 mycroft
353 1.7 christos for (i = 0; i < arg.nvars; i++)
354 1.7 christos Var_Delete(arg.vars[i], VAR_GLOBAL);
355 1.7 christos }
356 1.7 christos
357 1.7 christos free(values);
358 1.7 christos
359 1.7 christos Lst_Close(arg.lst);
360 1.7 christos
361 1.7 christos for (i=0; i<arg.nvars; i++) {
362 1.7 christos free(arg.vars[i]);
363 1.1 cgd }
364 1.22 christos free(arg.vars);
365 1.1 cgd
366 1.1 cgd Lst_Destroy(arg.lst, (FreeProc *)free);
367 Buf_Destroy(arg.buf, TRUE);
368 }
369