1 /* $NetBSD: for.c,v 1.45 2009/01/14 22:54:10 dsl Exp $ */
4 * Copyright (c) 1992, The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
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
33 static char rcsid
[] = "$NetBSD: for.c,v 1.45 2009/01/14 22:54:10 dsl Exp $";
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)for.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: for.c,v 1.45 2009/01/14 22:54:10 dsl Exp $");
47 * Functions to handle loops in a makefile.
50 * For_Eval Evaluate the loop in the passed line.
51 * For_Run Run accumulated loop
64 #define FOR_SUB_ESCAPE_CHAR 1
65 #define FOR_SUB_ESCAPE_BRACE 2
66 #define FOR_SUB_ESCAPE_PAREN 4
69 * For statements are of the form:
71 * .for <variable> in <varlist>
75 * The trick is to look for the matching end inside for for loop
76 * To do that, we count the current nesting level of the for loops.
77 * and the .endfor statements, accumulating all the statements between
78 * the initial .for loop and the matching .endfor;
79 * then we evaluate the for loop for each variable in the varlist.
81 * Note that any nested fors are just passed through; they get handled
82 * recursively in For_Eval when we're expanding the enclosing for in
86 static int forLevel
= 0; /* Nesting level */
89 * State of a for loop.
92 Buffer buf
; /* Body of loop */
93 strlist_t vars
; /* Iteration variables */
94 strlist_t items
; /* Substitution items */
100 static For
*accumFor
; /* Loop being accumulated */
105 make_str(const char *ptr
, int len
)
109 new_ptr
= bmake_malloc(len
+ 1);
110 memcpy(new_ptr
, ptr
, len
);
118 Buf_Destroy(&arg
->buf
, TRUE
);
119 strlist_clean(&arg
->vars
);
120 strlist_clean(&arg
->items
);
121 free(arg
->parse_buf
);
127 *-----------------------------------------------------------------------
129 * Evaluate the for loop in the passed line. The line
131 * .for <variable> in <varlist>
137 * 0: Not a .for statement, parse the line
138 * 1: We found a for loop
139 * -1: A .for statement with a bad syntax error, discard.
144 *-----------------------------------------------------------------------
150 char *ptr
= line
, *sub
;
155 /* Skip the '.' and any following whitespace */
156 for (ptr
++; *ptr
&& isspace((unsigned char) *ptr
); ptr
++)
160 * If we are not in a for loop quickly determine if the statement is
163 if (ptr
[0] != 'f' || ptr
[1] != 'o' || ptr
[2] != 'r' ||
164 !isspace((unsigned char) ptr
[3])) {
165 if (ptr
[0] == 'e' && strncmp(ptr
+1, "ndfor", 5) == 0) {
166 Parse_Error(PARSE_FATAL
, "for-less endfor");
174 * we found a for loop, and now we are going to parse it.
177 new_for
= bmake_malloc(sizeof *new_for
);
178 memset(new_for
, 0, sizeof *new_for
);
180 /* Grab the variables. Terminate on "in". */
181 for (;; ptr
+= len
) {
182 while (*ptr
&& isspace((unsigned char) *ptr
))
185 Parse_Error(PARSE_FATAL
, "missing `in' in for");
189 for (len
= 1; ptr
[len
] && !isspace((unsigned char)ptr
[len
]); len
++)
191 if (len
== 2 && ptr
[0] == 'i' && ptr
[1] == 'n') {
196 new_for
->short_var
= 1;
197 strlist_add_str(&new_for
->vars
, make_str(ptr
, len
), len
);
200 if (strlist_num(&new_for
->vars
) == 0) {
201 Parse_Error(PARSE_FATAL
, "no iteration variables in for");
206 while (*ptr
&& isspace((unsigned char) *ptr
))
210 * Make a list with the remaining words
211 * The values are substituted as ${:U<value>...} so we must \ escape
212 * characters that break that syntax.
213 * Variables are fully expanded - so it is safe for escape $.
214 * We can't do the escapes here - because we don't know whether
215 * we are substuting into ${...} or $(...).
217 sub
= Var_Subst(NULL
, ptr
, VAR_GLOBAL
, FALSE
);
219 for (ptr
= sub
;; ptr
+= len
) {
220 while (*ptr
&& isspace((unsigned char)*ptr
))
225 for (len
= 0; (ch
= ptr
[len
]) != 0 && !isspace(ch
); len
++) {
226 if (ch
== ':' || ch
== '$' || ch
== '\\')
227 escapes
|= FOR_SUB_ESCAPE_CHAR
;
229 escapes
|= FOR_SUB_ESCAPE_PAREN
;
230 else if (ch
== /*{*/ '}')
231 escapes
|= FOR_SUB_ESCAPE_BRACE
;
233 strlist_add_str(&new_for
->items
, make_str(ptr
, len
), escapes
);
238 if (strlist_num(&new_for
->items
) % strlist_num(&new_for
->vars
)) {
239 Parse_Error(PARSE_FATAL
,
240 "Wrong number of words in .for substitution list %d %d",
241 strlist_num(&new_for
->items
), strlist_num(&new_for
->vars
));
243 * Return 'success' so that the body of the .for loop is accumulated.
244 * Remove all items so that the loop doesn't iterate.
246 strlist_clean(&new_for
->items
);
249 Buf_Init(&new_for
->buf
, 0);
256 * Add another line to a .for loop.
257 * Returns 0 when the matching .endfor is reached.
261 For_Accum(char *line
)
267 for (ptr
++; *ptr
&& isspace((unsigned char) *ptr
); ptr
++)
270 if (strncmp(ptr
, "endfor", 6) == 0 &&
271 (isspace((unsigned char) ptr
[6]) || !ptr
[6])) {
273 (void)fprintf(debug_file
, "For: end for %d\n", forLevel
);
276 } else if (strncmp(ptr
, "for", 3) == 0 &&
277 isspace((unsigned char) ptr
[3])) {
280 (void)fprintf(debug_file
, "For: new loop %d\n", forLevel
);
284 Buf_AddBytes(&accumFor
->buf
, strlen(line
), line
);
285 Buf_AddByte(&accumFor
->buf
, '\n');
291 *-----------------------------------------------------------------------
293 * Run the for loop, imitating the actions of an include file
301 *-----------------------------------------------------------------------
305 for_var_len(const char *var
)
307 char ch
, var_start
, var_end
;
313 /* just escape the $ */
316 if (var_start
== '(')
318 else if (var_start
== '{')
321 /* Single char variable */
325 for (len
= 1; (ch
= var
[len
++]) != 0;) {
328 else if (ch
== var_end
&& --depth
== 0)
332 /* Variable end not found, escape the $ */
337 for_substitute(Buffer
*cmds
, strlist_t
*items
, unsigned int item_no
, char ech
)
339 const char *item
= strlist_str(items
, item_no
);
343 /* If there were no escapes, or the only escape is the other variable
344 * terminator, then just substitute the full string */
345 if (!(strlist_info(items
, item_no
) &
346 (ech
== ')' ? ~FOR_SUB_ESCAPE_BRACE
: ~FOR_SUB_ESCAPE_PAREN
))) {
347 Buf_AddBytes(cmds
, strlen(item
), item
);
351 /* Escape ':', '$', '\\' and 'ech' - removed by :U processing */
352 while ((ch
= *item
++) != 0) {
354 len
= for_var_len(item
);
356 Buf_AddBytes(cmds
, len
+ 1, item
- 1);
360 Buf_AddByte(cmds
, '\\');
361 } else if (ch
== ':' || ch
== '\\' || ch
== ech
)
362 Buf_AddByte(cmds
, '\\');
363 Buf_AddByte(cmds
, ch
);
368 For_Iterate(void *v_arg
)
379 if (arg
->sub_next
+ strlist_num(&arg
->vars
) > strlist_num(&arg
->items
)) {
380 /* No more iterations */
385 free(arg
->parse_buf
);
386 arg
->parse_buf
= NULL
;
389 * Scan the for loop body and replace references to the loop variables
390 * with variable references that expand to the required text.
391 * Using variable expansions ensures that the .for loop can't generate
392 * syntax, and that the later parsing will still see a variable.
393 * We assume that the null variable will never be defined.
395 * The detection of substitions of the loop control variable is naive.
396 * Many of the modifiers use \ to escape $ (not $) so it is possible
397 * to contrive a makefile where an unwanted substitution happens.
400 cmd_cp
= Buf_GetAll(&arg
->buf
, &len
);
401 body_end
= cmd_cp
+ len
;
402 Buf_Init(&cmds
, len
+ 256);
403 for (cp
= cmd_cp
; (cp
= strchr(cp
, '$')) != NULL
;) {
406 if ((ch
== '(' && (ech
= ')')) || (ch
== '{' && (ech
= '}'))) {
408 /* Check variable name against the .for loop variables */
409 STRLIST_FOREACH(var
, &arg
->vars
, i
) {
410 len
= strlist_info(&arg
->vars
, i
);
411 if (memcmp(cp
, var
, len
) != 0)
413 if (cp
[len
] != ':' && cp
[len
] != ech
&& cp
[len
] != '\\')
415 /* Found a variable match. Replace with :U<value> */
416 Buf_AddBytes(&cmds
, cp
- cmd_cp
, cmd_cp
);
417 Buf_AddBytes(&cmds
, 2, ":U");
420 for_substitute(&cmds
, &arg
->items
, arg
->sub_next
+ i
, ech
);
427 /* Probably a single character name, ignore $$ and stupid ones. {*/
428 if (!arg
->short_var
|| strchr("}):$", ch
) != NULL
) {
432 STRLIST_FOREACH(var
, &arg
->vars
, i
) {
433 if (var
[0] != ch
|| var
[1] != 0)
435 /* Found a variable match. Replace with ${:U<value>} */
436 Buf_AddBytes(&cmds
, cp
- cmd_cp
, cmd_cp
);
437 Buf_AddBytes(&cmds
, 3, "{:U");
439 for_substitute(&cmds
, &arg
->items
, arg
->sub_next
+ i
, /*{*/ '}');
440 Buf_AddBytes(&cmds
, 1, "}");
444 Buf_AddBytes(&cmds
, body_end
- cmd_cp
, cmd_cp
);
446 cp
= Buf_Destroy(&cmds
, FALSE
);
448 (void)fprintf(debug_file
, "For: loop body:\n%s", cp
);
450 arg
->sub_next
+= strlist_num(&arg
->vars
);
464 if (strlist_num(&arg
->items
) == 0) {
465 /* Nothing to expand - possibly due to an earlier syntax error. */
470 Parse_SetInput(NULL
, lineno
, -1, For_Iterate
, arg
);