xref: /netbsd-src/usr.bin/make/for.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /*
2  * Copyright (c) 1992, The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /* from: static char sccsid[] = "@(#)for.c	5.6 (Berkeley) 6/1/90"; */
36 static char *rcsid = "$Id: for.c,v 1.1 1994/03/05 00:34:44 cgd Exp $";
37 #endif /* not lint */
38 
39 /*-
40  * for.c --
41  *	Functions to handle loops in a makefile.
42  *
43  * Interface:
44  *	For_Eval 	Evaluate the loop in the passed line.
45  *	For_Run		Run accumulated loop
46  *
47  */
48 
49 #include    <ctype.h>
50 #include    "make.h"
51 #include    "hash.h"
52 #include    "dir.h"
53 #include    "buf.h"
54 
55 /*
56  * For statements are of the form:
57  *
58  * .for <variable> in <varlist>
59  * ...
60  * .endfor
61  *
62  * The trick is to look for the matching end inside for for loop
63  * To do that, we count the current nesting level of the for loops.
64  * and the .endfor statements, accumulating all the statements between
65  * the initial .for loop and the matching .endfor;
66  * then we evaluate the for loop for each variable in the varlist.
67  */
68 
69 static int  	  forLevel = 0;  	/* Nesting level	*/
70 static char	 *forVar;		/* Iteration variable	*/
71 static Buffer	  forBuf;		/* Commands in loop	*/
72 static Lst	  forLst;		/* List of items	*/
73 
74 /*
75  * State of a for loop.
76  */
77 struct For {
78     Buffer	  buf;			/* Unexpanded buffer	*/
79     char*	  var;			/* Index name		*/
80     Lst  	  lst;			/* List of variables	*/
81 };
82 
83 static int ForExec	__P((char *, struct For *));
84 
85 
86 
87 
88 /*-
89  *-----------------------------------------------------------------------
90  * For_Eval --
91  *	Evaluate the for loop in the passed line. The line
92  *	looks like this:
93  *	    .for <variable> in <varlist>
94  *
95  * Results:
96  *	TRUE: We found a for loop, or we are inside a for loop
97  *	FALSE: We did not find a for loop, or we found the end of the for
98  *	       for loop.
99  *
100  * Side Effects:
101  *	None.
102  *
103  *-----------------------------------------------------------------------
104  */
105 int
106 For_Eval (line)
107     char    	    *line;    /* Line to parse */
108 {
109     char	    *ptr = line, *sub, *wrd;
110     int	    	    level;  	/* Level at which to report errors. */
111 
112     level = PARSE_FATAL;
113 
114 
115     if (forLevel == 0) {
116 	Buffer	    buf;
117 	int	    varlen;
118 
119 	for (ptr++; *ptr && isspace(*ptr); ptr++)
120 	    continue;
121 	/*
122 	 * If we are not in a for loop quickly determine if the statement is
123 	 * a for.
124 	 */
125 	if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' || !isspace(ptr[3]))
126 	    return FALSE;
127 	ptr += 3;
128 
129 	/*
130 	 * we found a for loop, and now we are going to parse it.
131 	 */
132 	while (*ptr && isspace(*ptr))
133 	    ptr++;
134 
135 	/*
136 	 * Grab the variable
137 	 */
138 	buf = Buf_Init(0);
139 	for (wrd = ptr; *ptr && !isspace(*ptr); ptr++)
140 	    continue;
141 	Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd);
142 
143 	forVar = (char *) Buf_GetAll(buf, &varlen);
144 	if (varlen == 0) {
145 	    Parse_Error (level, "missing variable in for");
146 	    return 0;
147 	}
148 	Buf_Destroy(buf, FALSE);
149 
150 	while (*ptr && isspace(*ptr))
151 	    ptr++;
152 
153 	/*
154 	 * Grab the `in'
155 	 */
156 	if (ptr[0] != 'i' || ptr[1] != 'n' || !isspace(ptr[2])) {
157 	    Parse_Error (level, "missing `in' in for");
158 	    printf("%s\n", ptr);
159 	    return 0;
160 	}
161 	ptr += 3;
162 
163 	while (*ptr && isspace(*ptr))
164 	    ptr++;
165 
166 	/*
167 	 * Make a list with the remaining words
168 	 */
169 	forLst = Lst_Init(FALSE);
170 	buf = Buf_Init(0);
171 	sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
172 
173 #define ADDWORD() \
174 	Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \
175 	Buf_AddByte(buf, (Byte) '\0'), \
176 	Lst_AtEnd(forLst, (ClientData) Buf_GetAll(buf, &varlen)), \
177 	Buf_Destroy(buf, FALSE)
178 
179 	for (ptr = sub; *ptr && isspace(*ptr); ptr++)
180 	    continue;
181 
182 	for (wrd = ptr; *ptr; ptr++)
183 	    if (isspace(*ptr)) {
184 		ADDWORD();
185 		buf = Buf_Init(0);
186 		while (*ptr && isspace(*ptr))
187 		    ptr++;
188 		wrd = ptr--;
189 	    }
190 	if (DEBUG(FOR))
191 	    (void) fprintf(stderr, "For: Iterator %s List %s\n", forVar, sub);
192 	if (ptr - wrd > 0)
193 	    ADDWORD();
194 	else
195 	    Buf_Destroy(buf, TRUE);
196 	free((Address) sub);
197 
198 	forBuf = Buf_Init(0);
199 	forLevel++;
200 	return 1;
201     }
202     else if (*ptr == '.') {
203 
204 	for (ptr++; *ptr && isspace(*ptr); ptr++)
205 	    continue;
206 
207 	if (strncmp(ptr, "endfor", 6) == 0 && (isspace(ptr[6]) || !ptr[6])) {
208 	    if (DEBUG(FOR))
209 		(void) fprintf(stderr, "For: end for %d\n", forLevel);
210 	    if (--forLevel < 0) {
211 		Parse_Error (level, "for-less endfor");
212 		return 0;
213 	    }
214 	}
215 	else if (strncmp(ptr, "for", 3) == 0 && isspace(ptr[3])) {
216 	    forLevel++;
217 	    if (DEBUG(FOR))
218 		(void) fprintf(stderr, "For: new loop %d\n", forLevel);
219 	}
220     }
221 
222     if (forLevel != 0) {
223 	Buf_AddBytes(forBuf, strlen(line), (Byte *) line);
224 	Buf_AddByte(forBuf, (Byte) '\n');
225 	return 1;
226     }
227     else {
228 	return 0;
229     }
230 }
231 
232 /*-
233  *-----------------------------------------------------------------------
234  * ForExec --
235  *	Expand the for loop for this index and push it in the Makefile
236  *
237  * Results:
238  *	None.
239  *
240  * Side Effects:
241  *	None.
242  *
243  *-----------------------------------------------------------------------
244  */
245 static int
246 ForExec(name, arg)
247     char *name;
248     struct For *arg;
249 {
250     int len;
251     Var_Set(arg->var, name, VAR_GLOBAL);
252     if (DEBUG(FOR))
253 	(void) fprintf(stderr, "--- %s = %s\n", arg->var, name);
254     Parse_FromString(Var_Subst(arg->var, (char *) Buf_GetAll(arg->buf, &len),
255 			       VAR_GLOBAL, FALSE));
256     Var_Delete(arg->var, VAR_GLOBAL);
257 
258     return 0;
259 }
260 
261 
262 /*-
263  *-----------------------------------------------------------------------
264  * For_Run --
265  *	Run the for loop, immitating the actions of an include file
266  *
267  * Results:
268  *	None.
269  *
270  * Side Effects:
271  *	None.
272  *
273  *-----------------------------------------------------------------------
274  */
275 void
276 For_Run()
277 {
278     struct For arg;
279 
280     if (forVar == NULL || forBuf == NULL || forLst == NULL)
281 	return;
282     arg.var = forVar;
283     arg.buf = forBuf;
284     arg.lst = forLst;
285     forVar = NULL;
286     forBuf = NULL;
287     forLst = NULL;
288 
289     Lst_ForEach(arg.lst, ForExec, (ClientData) &arg);
290 
291     free((Address)arg.var);
292     Lst_Destroy(arg.lst, free);
293     Buf_Destroy(arg.buf, TRUE);
294 }
295