Renamed env.sh to common.sh
[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. 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.
19  *
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.
31  *
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.32 2005/02/23 21:07:43 okumoto Exp $
35  */
36
37 /*-
38  * for.c --
39  *      Functions to handle loops in a makefile.
40  *
41  * Interface:
42  *      For_Eval        Evaluate the loop in the passed line.
43  *      For_Run         Run accumulated loop
44  *
45  */
46
47 #include <ctype.h>
48 #include <stdlib.h>
49 #include <string.h>
50
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"
60
61 /*
62  * For statements are of the form:
63  *
64  * .for <variable> in <varlist>
65  * ...
66  * .endfor
67  *
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.
73  */
74
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 */
79
80 /*
81  * State of a for loop.
82  */
83 typedef struct _For {
84         Buffer  *buf;           /* Unexpanded buffer */
85         char    *var;           /* Index name */
86         Lst     lst;            /* List of variables */
87         int     lineno;         /* Line # */
88 } For;
89
90 /*-
91  *-----------------------------------------------------------------------
92  * For_Eval --
93  *      Evaluate the for loop in the passed line. The line
94  *      looks like this:
95  *          .for <variable> in <varlist>
96  *
97  * Results:
98  *      TRUE: We found a for loop, or we are inside a for loop
99  *      FALSE: We did not find a for loop, or we found the end of the for
100  *             for loop.
101  *
102  * Side Effects:
103  *      None.
104  *
105  *-----------------------------------------------------------------------
106  */
107 int
108 For_Eval(char *line)
109 {
110         char    *ptr;
111         char    *sub;
112         char    *wrd;
113         int     level;  /* Level at which to report errors. */
114
115         ptr = line;
116         level = PARSE_FATAL;
117
118         if (forLevel == 0) {
119                 /*
120                  * maybe start of a for loop
121                  */
122                 Buffer  *buf;
123                 size_t  varlen;
124
125                 for (ptr++; *ptr && isspace((unsigned char)*ptr); ptr++)
126                         ;
127                 /*
128                  * If we are not in a for loop quickly determine if
129                  * the statement is a for.
130                  */
131                 if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
132                     !isspace((unsigned char)ptr[3]))
133                         return (FALSE);
134                 ptr += 3;
135
136                 /*
137                  * we found a for loop, and now we are going to parse it.
138                  */
139                 while (*ptr && isspace((unsigned char)*ptr))
140                         ptr++;
141
142                 /*
143                  * Grab the variable
144                  */
145                 buf = Buf_Init(0);
146                 for (wrd = ptr; *ptr && !isspace((unsigned char)*ptr); ptr++)
147                         ;
148                 Buf_AppendRange(buf, wrd, ptr);
149
150                 forVar = (char *)Buf_GetAll(buf, &varlen);
151                 if (varlen == 0) {
152                         /* XXXHB Buf_Destroy(buf, TRUE) */
153                         Parse_Error(level, "missing variable in for");
154                         return (0);
155                 }
156                 Buf_Destroy(buf, FALSE);
157
158                 while (*ptr && isspace((unsigned char)*ptr))
159                         ptr++;
160
161                 /*
162                  * Grab the `in'
163                  */
164                 if (ptr[0] != 'i' || ptr[1] != 'n' ||
165                     !isspace((unsigned char)ptr[2])) {
166                         /* XXXHB free(forVar) */
167                         Parse_Error(level, "missing `in' in for");
168                         printf("%s\n", ptr);
169                         return (0);
170                 }
171                 ptr += 3;
172
173                 while (*ptr && isspace((unsigned char)*ptr))
174                         ptr++;
175
176                 /*
177                  * Make a list with the remaining words
178                  */
179                 Lst_Init(&forLst);
180                 buf = Buf_Init(0);
181                 {
182                         Buffer *buf1;
183
184                         buf1 = Var_Subst(NULL, ptr, VAR_CMD, FALSE);
185                         sub = Buf_GetAll(buf1, NULL);
186                         Buf_Destroy(buf1, FALSE);
187                 }
188
189                 for (ptr = sub; *ptr && isspace((unsigned char)*ptr); ptr++)
190                         ;
191
192                 for (wrd = ptr; *ptr; ptr++) {
193                         if (isspace((unsigned char)*ptr)) {
194                                 Buf_AppendRange(buf, wrd, ptr);
195                                 Buf_AddByte(buf, (Byte)'\0');
196                                 Lst_AtFront(&forLst, Buf_GetAll(buf, &varlen));
197                                 Buf_Destroy(buf, FALSE);
198                                 buf = Buf_Init(0);
199                                 while (*ptr && isspace((unsigned char)*ptr))
200                                         ptr++;
201                                 wrd = ptr--;
202                         }
203                 }
204                 DEBUGF(FOR, ("For: Iterator %s List %s\n", forVar, sub));
205                 if (ptr - wrd > 0) {
206                         Buf_AppendRange(buf, wrd, ptr);
207                         Buf_AddByte(buf, (Byte)'\0');
208                         Lst_AtFront(&forLst, Buf_GetAll(buf, &varlen));
209                         Buf_Destroy(buf, FALSE);
210                 } else {
211                         Buf_Destroy(buf, TRUE);
212                 }
213                 free(sub);
214
215                 forBuf = Buf_Init(0);
216                 forLevel++;
217                 return (1);
218         }
219
220         if (*ptr == '.') {
221                 /*
222                  * Need to check for 'endfor' and 'for' to find the end
223                  * of our loop or to find embedded for loops.
224                  */
225                 for (ptr++; *ptr && isspace((unsigned char)*ptr); ptr++)
226                         ;
227
228                 if (strncmp(ptr, "endfor", 6) == 0 &&
229                     (isspace((unsigned char)ptr[6]) || !ptr[6])) {
230                         DEBUGF(FOR, ("For: end for %d\n", forLevel));
231                         if (--forLevel < 0) {
232                                 Parse_Error(level, "for-less endfor");
233                                 return (0);
234                         }
235
236                 } else if (strncmp(ptr, "for", 3) == 0 &&
237                     isspace((unsigned char)ptr[3])) {
238                         forLevel++;
239                         DEBUGF(FOR, ("For: new loop %d\n", forLevel));
240                 }
241         }
242
243         if (forLevel != 0) {
244                 /*
245                  * Still in loop - append the line
246                  */
247                 Buf_Append(forBuf, line);
248                 Buf_AddByte(forBuf, (Byte)'\n');
249                 return (1);
250         }
251
252         return (0);
253 }
254
255 /*-
256  *-----------------------------------------------------------------------
257  * For_Run --
258  *      Run the for loop, immitating the actions of an include file
259  *
260  * Results:
261  *      None.
262  *
263  * Side Effects:
264  *      The values of the variables forLst, forVar and forBuf are freed.
265  *
266  *-----------------------------------------------------------------------
267  */
268 void
269 For_Run(int lineno)
270 {
271         Lst             values; /* list of values for the variable */
272         char            *var;   /* the variable's name */
273         Buffer          *buf;   /* the contents of the for loop */
274         const char      *val;   /* current value of loop variable */
275         LstNode         *ln;
276         Buffer          *buf2;
277         char            *str;
278
279
280         if (forVar == NULL || forBuf == NULL)
281                 return;
282
283         /* copy the global variables to have them free for embedded fors */
284         var = forVar;
285         buf = forBuf;
286         Lst_Init(&values);
287         Lst_Concat(&values, &forLst, LST_CONCLINK);
288
289         forVar = NULL;
290         forBuf = NULL;
291
292         LST_FOREACH(ln, &values) {
293                 val = Lst_Datum(ln);
294                 Var_Set(var, val, VAR_GLOBAL);
295
296                 DEBUGF(FOR, ("--- %s = %s\n", var, val));
297
298                 buf2 = Var_Subst(var,
299                     (char *)Buf_GetAll(buf, NULL),
300                     VAR_GLOBAL, FALSE);
301                 str = Buf_GetAll(buf2, NULL);
302                 Buf_Destroy(buf2, FALSE);
303
304                 Parse_FromString(str, lineno);
305                 Var_Delete(var, VAR_GLOBAL);
306         }
307
308         free(var);
309         Lst_Destroy(&values, free);
310         Buf_Destroy(buf, TRUE);
311 }