Merge from vendor branch DHCP:
[dragonfly.git] / usr.bin / make / for.c
1 /*
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Christos Zoulas.
7  *
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. 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.
23  *
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
34  * SUCH DAMAGE.
35  *
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.16 2004/12/17 08:13:30 okumoto Exp $
39  */
40
41 /*-
42  * for.c --
43  *      Functions to handle loops in a makefile.
44  *
45  * Interface:
46  *      For_Eval        Evaluate the loop in the passed line.
47  *      For_Run         Run accumulated loop
48  *
49  */
50
51 #include    <ctype.h>
52 #include    "make.h"
53 #include    "hash.h"
54 #include    "dir.h"
55 #include    "buf.h"
56
57 /*
58  * For statements are of the form:
59  *
60  * .for <variable> in <varlist>
61  * ...
62  * .endfor
63  *
64  * The trick is to look for the matching end inside for for loop
65  * To do that, we count the current nesting level of the for loops.
66  * and the .endfor statements, accumulating all the statements between
67  * the initial .for loop and the matching .endfor;
68  * then we evaluate the for loop for each variable in the varlist.
69  */
70
71 static int        forLevel = 0;         /* Nesting level        */
72 static char      *forVar;               /* Iteration variable   */
73 static Buffer     forBuf;               /* Commands in loop     */
74 static Lst      forLst;         /* List of items        */
75
76 /*
77  * State of a for loop.
78  */
79 typedef struct _For {
80     Buffer        buf;                  /* Unexpanded buffer    */
81     char*         var;                  /* Index name           */
82     Lst           lst;                  /* List of variables    */
83     int           lineno;               /* Line #               */
84 } For;
85
86 static int ForExec(void *, void *);
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(char *line)
107 {
108     char            *ptr = line, *sub, *wrd;
109     int             level;      /* Level at which to report errors. */
110
111     level = PARSE_FATAL;
112
113
114     if (forLevel == 0) {
115         Buffer      buf;
116         size_t varlen;
117
118         for (ptr++; *ptr && isspace((unsigned char)*ptr); ptr++)
119             continue;
120         /*
121          * If we are not in a for loop quickly determine if the statement is
122          * a for.
123          */
124         if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
125             !isspace((unsigned char)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((unsigned char)*ptr))
133             ptr++;
134
135         /*
136          * Grab the variable
137          */
138         buf = Buf_Init(0);
139         for (wrd = ptr; *ptr && !isspace((unsigned char)*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((unsigned char)*ptr))
151             ptr++;
152
153         /*
154          * Grab the `in'
155          */
156         if (ptr[0] != 'i' || ptr[1] != 'n' ||
157             !isspace((unsigned char)ptr[2])) {
158             Parse_Error(level, "missing `in' in for");
159             printf("%s\n", ptr);
160             return (0);
161         }
162         ptr += 3;
163
164         while (*ptr && isspace((unsigned char)*ptr))
165             ptr++;
166
167         /*
168          * Make a list with the remaining words
169          */
170         Lst_Init(&forLst);
171         buf = Buf_Init(0);
172         sub = Var_Subst(NULL, ptr, VAR_CMD, FALSE);
173
174 #define ADDWORD() \
175         Buf_AddBytes(buf, ptr - wrd, (Byte *)wrd), \
176         Buf_AddByte(buf, (Byte)'\0'), \
177         Lst_AtFront(&forLst, Buf_GetAll(buf, &varlen)), \
178         Buf_Destroy(buf, FALSE)
179
180         for (ptr = sub; *ptr && isspace((unsigned char)*ptr); ptr++)
181             continue;
182
183         for (wrd = ptr; *ptr; ptr++)
184             if (isspace((unsigned char)*ptr)) {
185                 ADDWORD();
186                 buf = Buf_Init(0);
187                 while (*ptr && isspace((unsigned char)*ptr))
188                     ptr++;
189                 wrd = ptr--;
190             }
191         DEBUGF(FOR, ("For: Iterator %s List %s\n", forVar, sub));
192         if (ptr - wrd > 0)
193             ADDWORD();
194         else
195             Buf_Destroy(buf, TRUE);
196         free(sub);
197
198         forBuf = Buf_Init(0);
199         forLevel++;
200         return (1);
201     }
202     else if (*ptr == '.') {
203
204         for (ptr++; *ptr && isspace((unsigned char)*ptr); ptr++)
205             continue;
206
207         if (strncmp(ptr, "endfor", 6) == 0 &&
208             (isspace((unsigned char)ptr[6]) || !ptr[6])) {
209             DEBUGF(FOR, ("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 &&
216                  isspace((unsigned char)ptr[3])) {
217             forLevel++;
218             DEBUGF(FOR, ("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(void *namep, void *argp)
247 {
248     char *name = namep;
249     For *arg = argp;
250     size_t len;
251
252     Var_Set(arg->var, name, VAR_GLOBAL);
253     DEBUGF(FOR, ("--- %s = %s\n", arg->var, name));
254     Parse_FromString(Var_Subst(arg->var, (char *)Buf_GetAll(arg->buf, &len),
255                                VAR_GLOBAL, FALSE), arg->lineno);
256     Var_Delete(arg->var, VAR_GLOBAL);
257
258     return (0);
259 }
260
261 /*-
262  *-----------------------------------------------------------------------
263  * For_Run --
264  *      Run the for loop, immitating the actions of an include file
265  *
266  * Results:
267  *      None.
268  *
269  * Side Effects:
270  *      None.
271  *
272  *-----------------------------------------------------------------------
273  */
274 void
275 For_Run(int lineno)
276 {
277     For arg;
278
279     if (forVar == NULL || forBuf == NULL)
280         return;
281     arg.var = forVar;
282     arg.buf = forBuf;
283
284     /* move the forLst to the arg to get it free for nested for's */
285     Lst_Init(&arg.lst);
286     Lst_Concat(&arg.lst, &forLst, LST_CONCLINK);
287
288     arg.lineno = lineno;
289     forVar = NULL;
290     forBuf = NULL;
291
292     Lst_ForEach(&arg.lst, ForExec, &arg);
293
294     free(arg.var);
295     Lst_Destroy(&arg.lst, free);
296     Buf_Destroy(arg.buf, TRUE);
297 }