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