Annotation of src/usr.bin/make/for.c, Revision 1.35
1.35 ! dsl 1: /* $NetBSD: for.c,v 1.34 2008/12/01 19:35:55 christos 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.35 ! dsl 33: static char rcsid[] = "$NetBSD: for.c,v 1.34 2008/12/01 19:35:55 christos 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.35 ! dsl 40: __RCSID("$NetBSD: for.c,v 1.34 2008/12/01 19:35:55 christos 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:
1.35 ! dsl 144: * 0: Not a .for statement, parse the line
! 145: * 1: We found a for loop
! 146: * -1: A .for statement with a bad syntax error, discard.
1.1 cgd 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.35 ! dsl 241: /*
! 242: * Add another line to a .for loop.
! 243: * Returns 0 when the matching .enfor is reached.
! 244: */
! 245:
1.32 dsl 246: int
247: For_Accum(char *line)
248: {
249: char *ptr = line;
1.26 dsl 250:
251: if (*ptr == '.') {
1.1 cgd 252:
1.2 jtc 253: for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
1.1 cgd 254: continue;
255:
1.2 jtc 256: if (strncmp(ptr, "endfor", 6) == 0 &&
1.26 dsl 257: (isspace((unsigned char) ptr[6]) || !ptr[6])) {
1.1 cgd 258: if (DEBUG(FOR))
1.23 dsl 259: (void)fprintf(debug_file, "For: end for %d\n", forLevel);
1.32 dsl 260: if (--forLevel <= 0)
1.1 cgd 261: return 0;
1.26 dsl 262: } else if (strncmp(ptr, "for", 3) == 0 &&
1.2 jtc 263: isspace((unsigned char) ptr[3])) {
1.1 cgd 264: forLevel++;
265: if (DEBUG(FOR))
1.23 dsl 266: (void)fprintf(debug_file, "For: new loop %d\n", forLevel);
1.1 cgd 267: }
268: }
269:
1.32 dsl 270: Buf_AddBytes(accumFor.buf, strlen(line), (Byte *)line);
271: Buf_AddByte(accumFor.buf, (Byte)'\n');
272: return 1;
1.1 cgd 273: }
274:
275:
276: /*-
277: *-----------------------------------------------------------------------
278: * For_Run --
1.7 christos 279: * Run the for loop, imitating the actions of an include file
1.1 cgd 280: *
281: * Results:
282: * None.
283: *
284: * Side Effects:
285: * None.
286: *
287: *-----------------------------------------------------------------------
288: */
289: void
1.16 enami 290: For_Run(int lineno)
1.1 cgd 291: {
1.2 jtc 292: For arg;
1.7 christos 293: LstNode ln;
294: char **values;
1.28 christos 295: int i, done = 0, len;
1.10 mycroft 296: char *guy, *orig_guy, *old_guy;
1.7 christos 297:
298: arg = accumFor;
299: accumFor.buf = NULL;
300: accumFor.vars = NULL;
301: accumFor.nvars = 0;
302: accumFor.lst = NULL;
1.1 cgd 303:
1.33 dsl 304: if (arg.nitem % arg.nvars)
305: /* Error message already printed */
306: return;
307:
1.7 christos 308: if (Lst_Open(arg.lst) != SUCCESS)
1.1 cgd 309: return;
310:
1.30 joerg 311: values = bmake_malloc(arg.nvars * sizeof(char *));
1.7 christos 312:
313: while (!done) {
314: /*
315: * due to the dumb way this is set up, this loop must run
316: * backwards.
317: */
318: for (i = arg.nvars - 1; i >= 0; i--) {
319: ln = Lst_Next(arg.lst);
320: if (ln == NILLNODE) {
321: done = 1;
322: break;
323: } else {
1.19 christos 324: values[i] = (char *)Lst_Datum(ln);
1.7 christos 325: }
326: }
327: if (done)
328: break;
329:
330: for (i = 0; i < arg.nvars; i++) {
1.11 sjg 331: Var_Set(arg.vars[i], values[i], VAR_GLOBAL, 0);
1.7 christos 332: if (DEBUG(FOR))
1.23 dsl 333: (void)fprintf(debug_file, "--- %s = %s\n", arg.vars[i],
1.7 christos 334: values[i]);
335: }
336:
337: /*
338: * Hack, hack, kludge.
339: * This is really ugly, but to do it any better way would require
340: * making major changes to var.c, which I don't want to get into
341: * yet. There is no mechanism for expanding some variables, only
342: * for expanding a single variable. That should be corrected, but
343: * not right away. (XXX)
344: */
345:
1.19 christos 346: guy = (char *)Buf_GetAll(arg.buf, &len);
1.10 mycroft 347: orig_guy = guy;
348: for (i = 0; i < arg.nvars; i++) {
349: old_guy = guy;
350: guy = Var_Subst(arg.vars[i], guy, VAR_GLOBAL, FALSE);
351: if (old_guy != orig_guy)
352: free(old_guy);
353: }
1.25 dsl 354: Parse_SetInput(NULL, lineno, -1, guy);
1.7 christos 355:
356: for (i = 0; i < arg.nvars; i++)
1.10 mycroft 357: Var_Delete(arg.vars[i], VAR_GLOBAL);
1.7 christos 358: }
359:
360: free(values);
361:
362: Lst_Close(arg.lst);
363:
364: for (i=0; i<arg.nvars; i++) {
365: free(arg.vars[i]);
366: }
367: free(arg.vars);
1.1 cgd 368:
1.22 christos 369: Lst_Destroy(arg.lst, (FreeProc *)free);
1.1 cgd 370: Buf_Destroy(arg.buf, TRUE);
371: }
CVSweb <webmaster@jp.NetBSD.org>