#include "make.h"
MAKE_RCSID("$NetBSD: for.c,v 1.187 2026/04/06 17:13:54 rillig Exp $");
typedef struct ForLoop {
Vector vars;
SubstringWords items;
Buffer body;
unsigned nextItem;
} ForLoop;
static ForLoop *accumFor;
static void
skip_whitespace_or_line_continuation(const char **pp)
{
const char *p = *pp;
for (;;) {
if (ch_isspace(*p))
p++;
else if (p[0] == '\\' && p[1] == '\n')
p += 2;
else
break;
}
*pp = p;
}
static ForLoop *
ForLoop_New(void)
{
ForLoop *f = bmake_malloc(sizeof *f);
Vector_Init(&f->vars, sizeof(char *));
SubstringWords_Init(&f->items);
Buf_Init(&f->body);
f->nextItem = 0;
return f;
}
void
ForLoop_Free(ForLoop *f)
{
while (f->vars.len > 0)
free(*(char **)Vector_Pop(&f->vars));
Vector_Done(&f->vars);
SubstringWords_Free(f->items);
Buf_Done(&f->body);
free(f);
}
char *
ForLoop_Details(const ForLoop *f)
{
size_t i, n;
const char **vars;
const Substring *items;
Buffer buf;
n = f->vars.len;
vars = f->vars.items;
assert(f->nextItem >= n);
items = f->items.words + f->nextItem - n;
Buf_Init(&buf);
for (i = 0; i < n; i++) {
if (i > 0)
Buf_AddStr(&buf, ", ");
Buf_AddStr(&buf, vars[i]);
Buf_AddStr(&buf, " = ");
Buf_AddRange(&buf, items[i].start, items[i].end);
}
return Buf_DoneData(&buf);
}
static bool
IsValidInVarname(char c)
{
return c != '$' && c != ':' && c != '\\' &&
c != '(' && c != '{' && c != ')' && c != '}';
}
static void
ForLoop_ParseVarnames(ForLoop *f, const char **pp)
{
const char *p = *pp, *start;
for (;;) {
cpp_skip_whitespace(&p);
if (*p == '\0') {
Parse_Error(PARSE_FATAL,
"Missing \"in\" in .for loop");
goto cleanup;
}
for (start = p; *p != '\0' && !ch_isspace(*p); p++)
if (!IsValidInVarname(*p))
goto invalid_variable_name;
if (p - start == 2 && memcmp(start, "in", 2) == 0)
break;
*(char **)Vector_Push(&f->vars) = bmake_strsedup(start, p);
}
if (f->vars.len == 0) {
Parse_Error(PARSE_FATAL,
"Missing iteration variables in .for loop");
return;
}
*pp = p;
return;
invalid_variable_name:
Parse_Error(PARSE_FATAL,
"Invalid character \"%c\" in .for loop variable name", *p);
cleanup:
while (f->vars.len > 0)
free(*(char **)Vector_Pop(&f->vars));
}
static bool
ForLoop_ParseItems(ForLoop *f, const char *p)
{
char *items;
int parseErrorsBefore = parseErrors;
cpp_skip_whitespace(&p);
items = Var_Subst(p, SCOPE_GLOBAL, VARE_EVAL);
f->items = Substring_Words(
parseErrors == parseErrorsBefore ? items : "", false);
free(items);
if (f->items.len == 1 && Substring_IsEmpty(f->items.words[0]))
f->items.len = 0;
if (f->items.len % f->vars.len != 0) {
Parse_Error(PARSE_FATAL,
"Wrong number of words (%u) in .for "
"substitution list with %u variables",
(unsigned)f->items.len, (unsigned)f->vars.len);
return false;
}
return true;
}
static bool
IsFor(const char *p)
{
return p[0] == 'f' && p[1] == 'o' && p[2] == 'r' && ch_isspace(p[3]);
}
static bool
IsEndfor(const char *p)
{
return p[0] == 'e' && strncmp(p, "endfor", 6) == 0 &&
(p[6] == '\0' || ch_isspace(p[6]));
}
int
For_Eval(const char *line)
{
const char *p;
ForLoop *f;
p = line + 1;
skip_whitespace_or_line_continuation(&p);
if (IsFor(p)) {
p += 3;
f = ForLoop_New();
ForLoop_ParseVarnames(f, &p);
if (f->vars.len > 0 && !ForLoop_ParseItems(f, p))
f->items.len = 0;
accumFor = f;
return 1;
} else if (IsEndfor(p)) {
Parse_Error(PARSE_FATAL, "for-less endfor");
return -1;
} else
return 0;
}
bool
For_Accum(const char *line, int *forLevel)
{
const char *p = line;
if (*p == '.') {
p++;
skip_whitespace_or_line_continuation(&p);
if (IsEndfor(p)) {
DEBUG1(FOR, "For: end for %d\n", *forLevel);
if (--*forLevel == 0)
return false;
} else if (IsFor(p)) {
(*forLevel)++;
DEBUG1(FOR, "For: new loop %d\n", *forLevel);
}
}
Buf_AddStr(&accumFor->body, line);
Buf_AddByte(&accumFor->body, '\n');
return true;
}
static size_t
ExprLen(const char *s, const char *e)
{
char expr_open, expr_close;
int depth;
const char *p;
if (s == e)
return 0;
expr_open = s[0];
if (expr_open == '(')
expr_close = ')';
else if (expr_open == '{')
expr_close = '}';
else
return 1;
depth = 1;
for (p = s + 1; p != e; p++) {
if (*p == expr_open)
depth++;
else if (*p == expr_close && --depth == 0)
return (size_t)(p + 1 - s);
}
return 0;
}
static void
AddEscaped(Buffer *body, Substring item, char endc)
{
const char *p;
char ch;
for (p = item.start; p != item.end;) {
ch = *p;
if (ch == '$') {
size_t len = ExprLen(p + 1, item.end);
if (len != 0) {
Buf_AddBytes(body, p, 1 + len);
p += 1 + len;
continue;
}
Buf_AddByte(body, '\\');
} else if (ch == ':' || ch == '\\' || ch == endc)
Buf_AddByte(body, '\\');
else if (ch == '\n') {
Parse_Error(PARSE_FATAL, "newline in .for value");
ch = ' ';
}
Buf_AddByte(body, ch);
p++;
}
}
static void
ForLoop_SubstVarLong(ForLoop *f, unsigned firstItem, Buffer *body,
const char **pp, char endc, const char **inout_mark)
{
size_t i;
const char *start = *pp;
const char **varnames = Vector_Get(&f->vars, 0);
for (i = 0; i < f->vars.len; i++) {
const char *p = start;
if (!cpp_skip_string(&p, varnames[i]))
continue;
if (*p != ':' && *p != endc && *p != '\\')
continue;
Buf_AddRange(body, *inout_mark, start);
Buf_AddStr(body, ":U");
AddEscaped(body, f->items.words[firstItem + i], endc);
*inout_mark = p;
*pp = p;
return;
}
}
static void
ForLoop_SubstVarShort(ForLoop *f, unsigned firstItem, Buffer *body,
const char *p, const char **inout_mark)
{
char ch = *p;
const char **vars;
size_t i;
if (ch == '}' || ch == ')' || ch == ':' || ch == '$')
return;
vars = Vector_Get(&f->vars, 0);
for (i = 0; i < f->vars.len; i++) {
const char *varname = vars[i];
if (varname[0] == ch && varname[1] == '\0')
goto found;
}
return;
found:
Buf_AddRange(body, *inout_mark, p);
*inout_mark = p + 1;
Buf_AddStr(body, "{:U");
AddEscaped(body, f->items.words[firstItem + i], '}');
Buf_AddByte(body, '}');
}
static void
ForLoop_SubstBody(ForLoop *f, unsigned firstItem, Buffer *body)
{
const char *p, *end;
const char *mark;
Buf_Clear(body);
mark = f->body.data;
end = f->body.data + f->body.len;
for (p = mark; (p = strchr(p, '$')) != NULL;) {
if (p[1] == '{' || p[1] == '(') {
char endc = p[1] == '{' ? '}' : ')';
p += 2;
ForLoop_SubstVarLong(f, firstItem, body,
&p, endc, &mark);
} else {
ForLoop_SubstVarShort(f, firstItem, body,
p + 1, &mark);
p += 2;
}
}
Buf_AddRange(body, mark, end);
}
bool
For_NextIteration(ForLoop *f, Buffer *body)
{
if (f->nextItem == f->items.len)
return false;
f->nextItem += (unsigned)f->vars.len;
ForLoop_SubstBody(f, f->nextItem - (unsigned)f->vars.len, body);
if (DEBUG(FOR)) {
char *details = ForLoop_Details(f);
debug_printf("For: loop body with %s:\n%s",
details, body->data);
free(details);
}
return true;
}
void
For_Break(ForLoop *f)
{
f->nextItem = (unsigned)f->items.len;
}
void
For_Run(unsigned headLineno, unsigned bodyReadLines)
{
Buffer buf;
ForLoop *f = accumFor;
accumFor = NULL;
if (f->items.len > 0) {
Buf_Init(&buf);
Parse_PushInput(NULL, headLineno, bodyReadLines, buf, f);
} else
ForLoop_Free(f);
}