1*8d5c8e21SSimon J. Gerraty /* $NetBSD: for.c,v 1.182 2024/06/07 18:57:30 rillig Exp $ */ 23955d011SMarcel Moolenaar 33955d011SMarcel Moolenaar /* 43955d011SMarcel Moolenaar * Copyright (c) 1992, The Regents of the University of California. 53955d011SMarcel Moolenaar * All rights reserved. 63955d011SMarcel Moolenaar * 73955d011SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 83955d011SMarcel Moolenaar * modification, are permitted provided that the following conditions 93955d011SMarcel Moolenaar * are met: 103955d011SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 113955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 123955d011SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 133955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 143955d011SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 153955d011SMarcel Moolenaar * 3. Neither the name of the University nor the names of its contributors 163955d011SMarcel Moolenaar * may be used to endorse or promote products derived from this software 173955d011SMarcel Moolenaar * without specific prior written permission. 183955d011SMarcel Moolenaar * 193955d011SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 203955d011SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 213955d011SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 223955d011SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 233955d011SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 243955d011SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 253955d011SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 263955d011SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 273955d011SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 283955d011SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 293955d011SMarcel Moolenaar * SUCH DAMAGE. 303955d011SMarcel Moolenaar */ 313955d011SMarcel Moolenaar 32dba7b0efSSimon J. Gerraty /* 33956e45f6SSimon J. Gerraty * Handling of .for/.endfor loops in a makefile. 343955d011SMarcel Moolenaar * 3506b9b3e0SSimon J. Gerraty * For loops have the form: 363955d011SMarcel Moolenaar * 37956e45f6SSimon J. Gerraty * .for <varname...> in <value...> 3806b9b3e0SSimon J. Gerraty * # the body 393955d011SMarcel Moolenaar * .endfor 403955d011SMarcel Moolenaar * 4106b9b3e0SSimon J. Gerraty * When a .for line is parsed, the following lines are copied to the body of 4206b9b3e0SSimon J. Gerraty * the .for loop, until the corresponding .endfor line is reached. In this 4306b9b3e0SSimon J. Gerraty * phase, the body is not yet evaluated. This also applies to any nested 4406b9b3e0SSimon J. Gerraty * .for loops. 453955d011SMarcel Moolenaar * 4606b9b3e0SSimon J. Gerraty * After reaching the .endfor, the values from the .for line are grouped 4706b9b3e0SSimon J. Gerraty * according to the number of variables. For each such group, the unexpanded 48d5e0a182SSimon J. Gerraty * body is scanned for expressions, and those that match the 4912904384SSimon J. Gerraty * variable names are replaced with expressions of the form ${:U...}. After 5012904384SSimon J. Gerraty * that, the body is treated like a file from an .include directive. 51956e45f6SSimon J. Gerraty * 52956e45f6SSimon J. Gerraty * Interface: 53956e45f6SSimon J. Gerraty * For_Eval Evaluate the loop in the passed line. 54956e45f6SSimon J. Gerraty * 55956e45f6SSimon J. Gerraty * For_Run Run accumulated loop 563955d011SMarcel Moolenaar */ 573955d011SMarcel Moolenaar 58956e45f6SSimon J. Gerraty #include "make.h" 59956e45f6SSimon J. Gerraty 60956e45f6SSimon J. Gerraty /* "@(#)for.c 8.1 (Berkeley) 6/6/93" */ 61*8d5c8e21SSimon J. Gerraty MAKE_RCSID("$NetBSD: for.c,v 1.182 2024/06/07 18:57:30 rillig Exp $"); 62956e45f6SSimon J. Gerraty 633955d011SMarcel Moolenaar 64dba7b0efSSimon J. Gerraty typedef struct ForLoop { 659f45a3c8SSimon J. Gerraty Vector /* of 'char *' */ vars; /* Iteration variables */ 6612904384SSimon J. Gerraty SubstringWords items; /* Substitution items */ 679f45a3c8SSimon J. Gerraty Buffer body; /* Unexpanded body of the loop */ 6812904384SSimon J. Gerraty unsigned int nextItem; /* Where to continue iterating */ 69dba7b0efSSimon J. Gerraty } ForLoop; 703955d011SMarcel Moolenaar 713955d011SMarcel Moolenaar 72dba7b0efSSimon J. Gerraty static ForLoop *accumFor; /* Loop being accumulated */ 73dba7b0efSSimon J. Gerraty 74dba7b0efSSimon J. Gerraty 75c1d01b5fSSimon J. Gerraty /* See LK_FOR_BODY. */ 76c1d01b5fSSimon J. Gerraty static void 77c1d01b5fSSimon J. Gerraty skip_whitespace_or_line_continuation(const char **pp) 78c1d01b5fSSimon J. Gerraty { 79c1d01b5fSSimon J. Gerraty const char *p = *pp; 80c1d01b5fSSimon J. Gerraty for (;;) { 81c1d01b5fSSimon J. Gerraty if (ch_isspace(*p)) 82c1d01b5fSSimon J. Gerraty p++; 83c1d01b5fSSimon J. Gerraty else if (p[0] == '\\' && p[1] == '\n') 84c1d01b5fSSimon J. Gerraty p += 2; 85c1d01b5fSSimon J. Gerraty else 86c1d01b5fSSimon J. Gerraty break; 87c1d01b5fSSimon J. Gerraty } 88c1d01b5fSSimon J. Gerraty *pp = p; 89c1d01b5fSSimon J. Gerraty } 90c1d01b5fSSimon J. Gerraty 91dba7b0efSSimon J. Gerraty static ForLoop * 92dba7b0efSSimon J. Gerraty ForLoop_New(void) 93956e45f6SSimon J. Gerraty { 94dba7b0efSSimon J. Gerraty ForLoop *f = bmake_malloc(sizeof *f); 95dba7b0efSSimon J. Gerraty 969f45a3c8SSimon J. Gerraty Vector_Init(&f->vars, sizeof(char *)); 9712904384SSimon J. Gerraty SubstringWords_Init(&f->items); 989f45a3c8SSimon J. Gerraty Buf_Init(&f->body); 9912904384SSimon J. Gerraty f->nextItem = 0; 100dba7b0efSSimon J. Gerraty 101dba7b0efSSimon J. Gerraty return f; 102956e45f6SSimon J. Gerraty } 1033955d011SMarcel Moolenaar 1049f45a3c8SSimon J. Gerraty void 105dba7b0efSSimon J. Gerraty ForLoop_Free(ForLoop *f) 1063955d011SMarcel Moolenaar { 1079f45a3c8SSimon J. Gerraty while (f->vars.len > 0) 1089f45a3c8SSimon J. Gerraty free(*(char **)Vector_Pop(&f->vars)); 109956e45f6SSimon J. Gerraty Vector_Done(&f->vars); 110956e45f6SSimon J. Gerraty 11112904384SSimon J. Gerraty SubstringWords_Free(f->items); 1129f45a3c8SSimon J. Gerraty Buf_Done(&f->body); 113956e45f6SSimon J. Gerraty 114956e45f6SSimon J. Gerraty free(f); 1153955d011SMarcel Moolenaar } 1163955d011SMarcel Moolenaar 1179f45a3c8SSimon J. Gerraty char * 118148ee845SSimon J. Gerraty ForLoop_Details(const ForLoop *f) 119dba7b0efSSimon J. Gerraty { 1209f45a3c8SSimon J. Gerraty size_t i, n; 1219f45a3c8SSimon J. Gerraty const char **vars; 1229f45a3c8SSimon J. Gerraty const Substring *items; 1239f45a3c8SSimon J. Gerraty Buffer buf; 1249f45a3c8SSimon J. Gerraty 1259f45a3c8SSimon J. Gerraty n = f->vars.len; 1269f45a3c8SSimon J. Gerraty vars = f->vars.items; 1279f45a3c8SSimon J. Gerraty assert(f->nextItem >= n); 1289f45a3c8SSimon J. Gerraty items = f->items.words + f->nextItem - n; 1299f45a3c8SSimon J. Gerraty 1309f45a3c8SSimon J. Gerraty Buf_Init(&buf); 1319f45a3c8SSimon J. Gerraty for (i = 0; i < n; i++) { 1329f45a3c8SSimon J. Gerraty if (i > 0) 1339f45a3c8SSimon J. Gerraty Buf_AddStr(&buf, ", "); 1349f45a3c8SSimon J. Gerraty Buf_AddStr(&buf, vars[i]); 1359f45a3c8SSimon J. Gerraty Buf_AddStr(&buf, " = "); 136148ee845SSimon J. Gerraty Buf_AddRange(&buf, items[i].start, items[i].end); 1379f45a3c8SSimon J. Gerraty } 1389f45a3c8SSimon J. Gerraty return Buf_DoneData(&buf); 139dba7b0efSSimon J. Gerraty } 140dba7b0efSSimon J. Gerraty 141b0c40a00SSimon J. Gerraty static bool 142c1d01b5fSSimon J. Gerraty IsValidInVarname(char c) 143c1d01b5fSSimon J. Gerraty { 144c1d01b5fSSimon J. Gerraty return c != '$' && c != ':' && c != '\\' && 145c1d01b5fSSimon J. Gerraty c != '(' && c != '{' && c != ')' && c != '}'; 146c1d01b5fSSimon J. Gerraty } 147c1d01b5fSSimon J. Gerraty 148c1d01b5fSSimon J. Gerraty static void 149dba7b0efSSimon J. Gerraty ForLoop_ParseVarnames(ForLoop *f, const char **pp) 150dba7b0efSSimon J. Gerraty { 151dba7b0efSSimon J. Gerraty const char *p = *pp; 152dba7b0efSSimon J. Gerraty 153dba7b0efSSimon J. Gerraty for (;;) { 154dba7b0efSSimon J. Gerraty size_t len; 155dba7b0efSSimon J. Gerraty 156dba7b0efSSimon J. Gerraty cpp_skip_whitespace(&p); 157dba7b0efSSimon J. Gerraty if (*p == '\0') { 158dba7b0efSSimon J. Gerraty Parse_Error(PARSE_FATAL, "missing `in' in for"); 159*8d5c8e21SSimon J. Gerraty while (f->vars.len > 0) 160*8d5c8e21SSimon J. Gerraty free(*(char **)Vector_Pop(&f->vars)); 161c1d01b5fSSimon J. Gerraty return; 162dba7b0efSSimon J. Gerraty } 163dba7b0efSSimon J. Gerraty 164c1d01b5fSSimon J. Gerraty for (len = 0; p[len] != '\0' && !ch_isspace(p[len]); len++) { 165c1d01b5fSSimon J. Gerraty if (!IsValidInVarname(p[len])) { 166c1d01b5fSSimon J. Gerraty Parse_Error(PARSE_FATAL, 167c1d01b5fSSimon J. Gerraty "invalid character '%c' " 168c1d01b5fSSimon J. Gerraty "in .for loop variable name", 169c1d01b5fSSimon J. Gerraty p[len]); 170*8d5c8e21SSimon J. Gerraty while (f->vars.len > 0) 171*8d5c8e21SSimon J. Gerraty free(*(char **)Vector_Pop(&f->vars)); 172c1d01b5fSSimon J. Gerraty return; 173c1d01b5fSSimon J. Gerraty } 174c1d01b5fSSimon J. Gerraty } 175dba7b0efSSimon J. Gerraty 176dba7b0efSSimon J. Gerraty if (len == 2 && p[0] == 'i' && p[1] == 'n') { 177dba7b0efSSimon J. Gerraty p += 2; 178dba7b0efSSimon J. Gerraty break; 179dba7b0efSSimon J. Gerraty } 180dba7b0efSSimon J. Gerraty 1819f45a3c8SSimon J. Gerraty *(char **)Vector_Push(&f->vars) = bmake_strldup(p, len); 182dba7b0efSSimon J. Gerraty p += len; 183dba7b0efSSimon J. Gerraty } 184dba7b0efSSimon J. Gerraty 185dba7b0efSSimon J. Gerraty if (f->vars.len == 0) { 186dba7b0efSSimon J. Gerraty Parse_Error(PARSE_FATAL, "no iteration variables in for"); 187c1d01b5fSSimon J. Gerraty return; 188dba7b0efSSimon J. Gerraty } 189dba7b0efSSimon J. Gerraty 190dba7b0efSSimon J. Gerraty *pp = p; 191dba7b0efSSimon J. Gerraty } 192dba7b0efSSimon J. Gerraty 193b0c40a00SSimon J. Gerraty static bool 194dba7b0efSSimon J. Gerraty ForLoop_ParseItems(ForLoop *f, const char *p) 195dba7b0efSSimon J. Gerraty { 196dba7b0efSSimon J. Gerraty char *items; 197dba7b0efSSimon J. Gerraty 198dba7b0efSSimon J. Gerraty cpp_skip_whitespace(&p); 199dba7b0efSSimon J. Gerraty 200*8d5c8e21SSimon J. Gerraty items = Var_Subst(p, SCOPE_GLOBAL, VARE_EVAL); 201548bfc56SSimon J. Gerraty /* TODO: handle errors */ 202dba7b0efSSimon J. Gerraty 20312904384SSimon J. Gerraty f->items = Substring_Words(items, false); 204dba7b0efSSimon J. Gerraty free(items); 205dba7b0efSSimon J. Gerraty 20612904384SSimon J. Gerraty if (f->items.len == 1 && Substring_IsEmpty(f->items.words[0])) 207dba7b0efSSimon J. Gerraty f->items.len = 0; /* .for var in ${:U} */ 208dba7b0efSSimon J. Gerraty 2099f45a3c8SSimon J. Gerraty if (f->items.len % f->vars.len != 0) { 210dba7b0efSSimon J. Gerraty Parse_Error(PARSE_FATAL, 211dba7b0efSSimon J. Gerraty "Wrong number of words (%u) in .for " 212dba7b0efSSimon J. Gerraty "substitution list with %u variables", 213dba7b0efSSimon J. Gerraty (unsigned)f->items.len, (unsigned)f->vars.len); 214b0c40a00SSimon J. Gerraty return false; 215dba7b0efSSimon J. Gerraty } 216dba7b0efSSimon J. Gerraty 217b0c40a00SSimon J. Gerraty return true; 218dba7b0efSSimon J. Gerraty } 219dba7b0efSSimon J. Gerraty 220b0c40a00SSimon J. Gerraty static bool 221956e45f6SSimon J. Gerraty IsFor(const char *p) 222956e45f6SSimon J. Gerraty { 223956e45f6SSimon J. Gerraty return p[0] == 'f' && p[1] == 'o' && p[2] == 'r' && ch_isspace(p[3]); 224956e45f6SSimon J. Gerraty } 2253955d011SMarcel Moolenaar 226b0c40a00SSimon J. Gerraty static bool 227956e45f6SSimon J. Gerraty IsEndfor(const char *p) 228956e45f6SSimon J. Gerraty { 229956e45f6SSimon J. Gerraty return p[0] == 'e' && strncmp(p, "endfor", 6) == 0 && 230956e45f6SSimon J. Gerraty (p[6] == '\0' || ch_isspace(p[6])); 231956e45f6SSimon J. Gerraty } 232956e45f6SSimon J. Gerraty 23306b9b3e0SSimon J. Gerraty /* 23406b9b3e0SSimon J. Gerraty * Evaluate the for loop in the passed line. The line looks like this: 235956e45f6SSimon J. Gerraty * .for <varname...> in <value...> 236956e45f6SSimon J. Gerraty * 237956e45f6SSimon J. Gerraty * Results: 2389f45a3c8SSimon J. Gerraty * 0 not a .for directive 2399f45a3c8SSimon J. Gerraty * 1 found a .for directive 2409f45a3c8SSimon J. Gerraty * -1 erroneous .for directive 241956e45f6SSimon J. Gerraty */ 242956e45f6SSimon J. Gerraty int 243956e45f6SSimon J. Gerraty For_Eval(const char *line) 244956e45f6SSimon J. Gerraty { 245956e45f6SSimon J. Gerraty const char *p; 2469f45a3c8SSimon J. Gerraty ForLoop *f; 247956e45f6SSimon J. Gerraty 248956e45f6SSimon J. Gerraty p = line + 1; /* skip the '.' */ 249c1d01b5fSSimon J. Gerraty skip_whitespace_or_line_continuation(&p); 250956e45f6SSimon J. Gerraty 2519f45a3c8SSimon J. Gerraty if (IsFor(p)) { 252956e45f6SSimon J. Gerraty p += 3; 253956e45f6SSimon J. Gerraty 254dba7b0efSSimon J. Gerraty f = ForLoop_New(); 255c1d01b5fSSimon J. Gerraty ForLoop_ParseVarnames(f, &p); 256c1d01b5fSSimon J. Gerraty if (f->vars.len > 0 && !ForLoop_ParseItems(f, p)) 2579f45a3c8SSimon J. Gerraty f->items.len = 0; /* don't iterate */ 2583955d011SMarcel Moolenaar 259956e45f6SSimon J. Gerraty accumFor = f; 2603955d011SMarcel Moolenaar return 1; 2619f45a3c8SSimon J. Gerraty } else if (IsEndfor(p)) { 2629f45a3c8SSimon J. Gerraty Parse_Error(PARSE_FATAL, "for-less endfor"); 2639f45a3c8SSimon J. Gerraty return -1; 2649f45a3c8SSimon J. Gerraty } else 2659f45a3c8SSimon J. Gerraty return 0; 2663955d011SMarcel Moolenaar } 2673955d011SMarcel Moolenaar 2683955d011SMarcel Moolenaar /* 269dba7b0efSSimon J. Gerraty * Add another line to the .for loop that is being built up. 270b0c40a00SSimon J. Gerraty * Returns false when the matching .endfor is reached. 2713955d011SMarcel Moolenaar */ 272b0c40a00SSimon J. Gerraty bool 2739f45a3c8SSimon J. Gerraty For_Accum(const char *line, int *forLevel) 2743955d011SMarcel Moolenaar { 275dba7b0efSSimon J. Gerraty const char *p = line; 2763955d011SMarcel Moolenaar 277dba7b0efSSimon J. Gerraty if (*p == '.') { 278dba7b0efSSimon J. Gerraty p++; 279c1d01b5fSSimon J. Gerraty skip_whitespace_or_line_continuation(&p); 2803955d011SMarcel Moolenaar 281dba7b0efSSimon J. Gerraty if (IsEndfor(p)) { 2829f45a3c8SSimon J. Gerraty DEBUG1(FOR, "For: end for %d\n", *forLevel); 2839f45a3c8SSimon J. Gerraty if (--*forLevel == 0) 284b0c40a00SSimon J. Gerraty return false; 285dba7b0efSSimon J. Gerraty } else if (IsFor(p)) { 2869f45a3c8SSimon J. Gerraty (*forLevel)++; 2879f45a3c8SSimon J. Gerraty DEBUG1(FOR, "For: new loop %d\n", *forLevel); 2883955d011SMarcel Moolenaar } 2893955d011SMarcel Moolenaar } 2903955d011SMarcel Moolenaar 291956e45f6SSimon J. Gerraty Buf_AddStr(&accumFor->body, line); 292956e45f6SSimon J. Gerraty Buf_AddByte(&accumFor->body, '\n'); 293b0c40a00SSimon J. Gerraty return true; 2943955d011SMarcel Moolenaar } 2953955d011SMarcel Moolenaar 296954401e6SSimon J. Gerraty /* 297954401e6SSimon J. Gerraty * When the body of a '.for i' loop is prepared for an iteration, each 298954401e6SSimon J. Gerraty * occurrence of $i in the body is replaced with ${:U...}, inserting the 299d5e0a182SSimon J. Gerraty * value of the item. If this item contains a '$', it may be the start of an 300d5e0a182SSimon J. Gerraty * expression. This expression is copied verbatim, its length is 301954401e6SSimon J. Gerraty * determined here, in a rather naive way, ignoring escape characters and 302954401e6SSimon J. Gerraty * funny delimiters in modifiers like ':S}from}to}'. 303954401e6SSimon J. Gerraty */ 3042c3632d1SSimon J. Gerraty static size_t 30512904384SSimon J. Gerraty ExprLen(const char *s, const char *e) 3063955d011SMarcel Moolenaar { 30712904384SSimon J. Gerraty char expr_open, expr_close; 3083955d011SMarcel Moolenaar int depth; 30912904384SSimon J. Gerraty const char *p; 3103955d011SMarcel Moolenaar 31112904384SSimon J. Gerraty if (s == e) 31212904384SSimon J. Gerraty return 0; /* just escape the '$' */ 3133955d011SMarcel Moolenaar 31412904384SSimon J. Gerraty expr_open = s[0]; 31512904384SSimon J. Gerraty if (expr_open == '(') 31612904384SSimon J. Gerraty expr_close = ')'; 31712904384SSimon J. Gerraty else if (expr_open == '{') 31812904384SSimon J. Gerraty expr_close = '}'; 3193955d011SMarcel Moolenaar else 32006b9b3e0SSimon J. Gerraty return 1; /* Single char variable */ 3213955d011SMarcel Moolenaar 3223955d011SMarcel Moolenaar depth = 1; 32312904384SSimon J. Gerraty for (p = s + 1; p != e; p++) { 32412904384SSimon J. Gerraty if (*p == expr_open) 3253955d011SMarcel Moolenaar depth++; 32612904384SSimon J. Gerraty else if (*p == expr_close && --depth == 0) 32712904384SSimon J. Gerraty return (size_t)(p + 1 - s); 3283955d011SMarcel Moolenaar } 3293955d011SMarcel Moolenaar 33012904384SSimon J. Gerraty /* Expression end not found, escape the $ */ 3313955d011SMarcel Moolenaar return 0; 3323955d011SMarcel Moolenaar } 3333955d011SMarcel Moolenaar 33406b9b3e0SSimon J. Gerraty /* 335148ee845SSimon J. Gerraty * While expanding the body of a .for loop, write the item as a ${:U...} 3369f45a3c8SSimon J. Gerraty * expression, escaping characters as needed. The result is later unescaped 3379f45a3c8SSimon J. Gerraty * by ApplyModifier_Defined. 33806b9b3e0SSimon J. Gerraty */ 3393955d011SMarcel Moolenaar static void 3409f45a3c8SSimon J. Gerraty AddEscaped(Buffer *cmds, Substring item, char endc) 3413955d011SMarcel Moolenaar { 34212904384SSimon J. Gerraty const char *p; 3433955d011SMarcel Moolenaar char ch; 3443955d011SMarcel Moolenaar 3459f45a3c8SSimon J. Gerraty for (p = item.start; p != item.end;) { 34612904384SSimon J. Gerraty ch = *p; 3473955d011SMarcel Moolenaar if (ch == '$') { 34812904384SSimon J. Gerraty size_t len = ExprLen(p + 1, item.end); 3493955d011SMarcel Moolenaar if (len != 0) { 35012904384SSimon J. Gerraty /* 35112904384SSimon J. Gerraty * XXX: Should a '\' be added here? 35212904384SSimon J. Gerraty * See directive-for-escape.mk, ExprLen. 35312904384SSimon J. Gerraty */ 35412904384SSimon J. Gerraty Buf_AddBytes(cmds, p, 1 + len); 3559f45a3c8SSimon J. Gerraty p += 1 + len; 3563955d011SMarcel Moolenaar continue; 3573955d011SMarcel Moolenaar } 3583955d011SMarcel Moolenaar Buf_AddByte(cmds, '\\'); 35906b9b3e0SSimon J. Gerraty } else if (ch == ':' || ch == '\\' || ch == endc) 3603955d011SMarcel Moolenaar Buf_AddByte(cmds, '\\'); 36112904384SSimon J. Gerraty else if (ch == '\n') { 36212904384SSimon J. Gerraty Parse_Error(PARSE_FATAL, "newline in .for value"); 36312904384SSimon J. Gerraty ch = ' '; /* prevent newline injection */ 36412904384SSimon J. Gerraty } 3653955d011SMarcel Moolenaar Buf_AddByte(cmds, ch); 3669f45a3c8SSimon J. Gerraty p++; 3673955d011SMarcel Moolenaar } 3683955d011SMarcel Moolenaar } 3693955d011SMarcel Moolenaar 37006b9b3e0SSimon J. Gerraty /* 371148ee845SSimon J. Gerraty * While expanding the body of a .for loop, replace the variable name of an 37206b9b3e0SSimon J. Gerraty * expression like ${i} or ${i:...} or $(i) or $(i:...) with ":Uvalue". 37306b9b3e0SSimon J. Gerraty */ 374956e45f6SSimon J. Gerraty static void 3759f45a3c8SSimon J. Gerraty ForLoop_SubstVarLong(ForLoop *f, unsigned int firstItem, Buffer *body, 3769f45a3c8SSimon J. Gerraty const char **pp, char endc, const char **inout_mark) 3773955d011SMarcel Moolenaar { 378956e45f6SSimon J. Gerraty size_t i; 3799f45a3c8SSimon J. Gerraty const char *start = *pp; 380148ee845SSimon J. Gerraty const char **varnames = Vector_Get(&f->vars, 0); 3813955d011SMarcel Moolenaar 382956e45f6SSimon J. Gerraty for (i = 0; i < f->vars.len; i++) { 3839f45a3c8SSimon J. Gerraty const char *p = start; 384956e45f6SSimon J. Gerraty 385148ee845SSimon J. Gerraty if (!cpp_skip_string(&p, varnames[i])) 386956e45f6SSimon J. Gerraty continue; 387956e45f6SSimon J. Gerraty /* XXX: why test for backslash here? */ 3889f45a3c8SSimon J. Gerraty if (*p != ':' && *p != endc && *p != '\\') 389956e45f6SSimon J. Gerraty continue; 390956e45f6SSimon J. Gerraty 39106b9b3e0SSimon J. Gerraty /* 39206b9b3e0SSimon J. Gerraty * Found a variable match. Skip over the variable name and 39306b9b3e0SSimon J. Gerraty * instead add ':U<value>' to the current body. 39406b9b3e0SSimon J. Gerraty */ 395148ee845SSimon J. Gerraty Buf_AddRange(body, *inout_mark, start); 3969f45a3c8SSimon J. Gerraty Buf_AddStr(body, ":U"); 3979f45a3c8SSimon J. Gerraty AddEscaped(body, f->items.words[firstItem + i], endc); 398956e45f6SSimon J. Gerraty 399956e45f6SSimon J. Gerraty *inout_mark = p; 400956e45f6SSimon J. Gerraty *pp = p; 401956e45f6SSimon J. Gerraty return; 402956e45f6SSimon J. Gerraty } 403956e45f6SSimon J. Gerraty } 4043955d011SMarcel Moolenaar 4053955d011SMarcel Moolenaar /* 406148ee845SSimon J. Gerraty * While expanding the body of a .for loop, replace single-character 407d5e0a182SSimon J. Gerraty * expressions like $i with their ${:U...} expansion. 40806b9b3e0SSimon J. Gerraty */ 40906b9b3e0SSimon J. Gerraty static void 4109f45a3c8SSimon J. Gerraty ForLoop_SubstVarShort(ForLoop *f, unsigned int firstItem, Buffer *body, 4119f45a3c8SSimon J. Gerraty const char *p, const char **inout_mark) 41206b9b3e0SSimon J. Gerraty { 413c59c3bf3SSimon J. Gerraty char ch = *p; 4149f45a3c8SSimon J. Gerraty const char **vars; 41506b9b3e0SSimon J. Gerraty size_t i; 41606b9b3e0SSimon J. Gerraty 41706b9b3e0SSimon J. Gerraty /* Skip $$ and stupid ones. */ 41812904384SSimon J. Gerraty if (ch == '}' || ch == ')' || ch == ':' || ch == '$') 41906b9b3e0SSimon J. Gerraty return; 42006b9b3e0SSimon J. Gerraty 42106b9b3e0SSimon J. Gerraty vars = Vector_Get(&f->vars, 0); 42206b9b3e0SSimon J. Gerraty for (i = 0; i < f->vars.len; i++) { 4239f45a3c8SSimon J. Gerraty const char *varname = vars[i]; 42406b9b3e0SSimon J. Gerraty if (varname[0] == ch && varname[1] == '\0') 42506b9b3e0SSimon J. Gerraty goto found; 42606b9b3e0SSimon J. Gerraty } 42706b9b3e0SSimon J. Gerraty return; 42806b9b3e0SSimon J. Gerraty 42906b9b3e0SSimon J. Gerraty found: 430148ee845SSimon J. Gerraty Buf_AddRange(body, *inout_mark, p); 43112904384SSimon J. Gerraty *inout_mark = p + 1; 43212904384SSimon J. Gerraty 43306b9b3e0SSimon J. Gerraty /* Replace $<ch> with ${:U<value>} */ 4349f45a3c8SSimon J. Gerraty Buf_AddStr(body, "{:U"); 4359f45a3c8SSimon J. Gerraty AddEscaped(body, f->items.words[firstItem + i], '}'); 4369f45a3c8SSimon J. Gerraty Buf_AddByte(body, '}'); 43706b9b3e0SSimon J. Gerraty } 43806b9b3e0SSimon J. Gerraty 43906b9b3e0SSimon J. Gerraty /* 44006b9b3e0SSimon J. Gerraty * Compute the body for the current iteration by copying the unexpanded body, 44106b9b3e0SSimon J. Gerraty * replacing the expressions for the iteration variables on the way. 442956e45f6SSimon J. Gerraty * 443d5e0a182SSimon J. Gerraty * Using expressions ensures that the .for loop can't generate 444148ee845SSimon J. Gerraty * syntax, and that the later parsing will still see an expression. 445148ee845SSimon J. Gerraty * This code assumes that the variable with the empty name is never defined, 446148ee845SSimon J. Gerraty * see unit-tests/varname-empty.mk. 4473955d011SMarcel Moolenaar * 448dba7b0efSSimon J. Gerraty * The detection of substitutions of the loop control variables is naive. 44912904384SSimon J. Gerraty * Many of the modifiers use '\$' instead of '$$' to escape '$', so it is 45012904384SSimon J. Gerraty * possible to contrive a makefile where an unwanted substitution happens. 451148ee845SSimon J. Gerraty * See unit-tests/directive-for-escape.mk. 4523955d011SMarcel Moolenaar */ 45306b9b3e0SSimon J. Gerraty static void 4549f45a3c8SSimon J. Gerraty ForLoop_SubstBody(ForLoop *f, unsigned int firstItem, Buffer *body) 45506b9b3e0SSimon J. Gerraty { 4569f45a3c8SSimon J. Gerraty const char *p, *end; 45712904384SSimon J. Gerraty const char *mark; /* where the last substitution left off */ 45806b9b3e0SSimon J. Gerraty 4599f45a3c8SSimon J. Gerraty Buf_Clear(body); 46006b9b3e0SSimon J. Gerraty 46106b9b3e0SSimon J. Gerraty mark = f->body.data; 4629f45a3c8SSimon J. Gerraty end = f->body.data + f->body.len; 46306b9b3e0SSimon J. Gerraty for (p = mark; (p = strchr(p, '$')) != NULL;) { 46406b9b3e0SSimon J. Gerraty if (p[1] == '{' || p[1] == '(') { 46512904384SSimon J. Gerraty char endc = p[1] == '{' ? '}' : ')'; 46606b9b3e0SSimon J. Gerraty p += 2; 4679f45a3c8SSimon J. Gerraty ForLoop_SubstVarLong(f, firstItem, body, 4689f45a3c8SSimon J. Gerraty &p, endc, &mark); 469548bfc56SSimon J. Gerraty } else { 4709f45a3c8SSimon J. Gerraty ForLoop_SubstVarShort(f, firstItem, body, 4719f45a3c8SSimon J. Gerraty p + 1, &mark); 47206b9b3e0SSimon J. Gerraty p += 2; 473548bfc56SSimon J. Gerraty } 47406b9b3e0SSimon J. Gerraty } 47506b9b3e0SSimon J. Gerraty 476148ee845SSimon J. Gerraty Buf_AddRange(body, mark, end); 47706b9b3e0SSimon J. Gerraty } 47806b9b3e0SSimon J. Gerraty 47906b9b3e0SSimon J. Gerraty /* 48006b9b3e0SSimon J. Gerraty * Compute the body for the current iteration by copying the unexpanded body, 48106b9b3e0SSimon J. Gerraty * replacing the expressions for the iteration variables on the way. 48206b9b3e0SSimon J. Gerraty */ 4839f45a3c8SSimon J. Gerraty bool 4849f45a3c8SSimon J. Gerraty For_NextIteration(ForLoop *f, Buffer *body) 485956e45f6SSimon J. Gerraty { 4869f45a3c8SSimon J. Gerraty if (f->nextItem == f->items.len) 4879f45a3c8SSimon J. Gerraty return false; 4883955d011SMarcel Moolenaar 48912904384SSimon J. Gerraty f->nextItem += (unsigned int)f->vars.len; 4909f45a3c8SSimon J. Gerraty ForLoop_SubstBody(f, f->nextItem - (unsigned int)f->vars.len, body); 491148ee845SSimon J. Gerraty if (DEBUG(FOR)) { 492148ee845SSimon J. Gerraty char *details = ForLoop_Details(f); 493148ee845SSimon J. Gerraty debug_printf("For: loop body with %s:\n%s", 494148ee845SSimon J. Gerraty details, body->data); 495148ee845SSimon J. Gerraty free(details); 496148ee845SSimon J. Gerraty } 4979f45a3c8SSimon J. Gerraty return true; 4983955d011SMarcel Moolenaar } 4993955d011SMarcel Moolenaar 5004fde40d9SSimon J. Gerraty /* Break out of the .for loop. */ 5014fde40d9SSimon J. Gerraty void 5024fde40d9SSimon J. Gerraty For_Break(ForLoop *f) 5034fde40d9SSimon J. Gerraty { 5044fde40d9SSimon J. Gerraty f->nextItem = (unsigned int)f->items.len; 5054fde40d9SSimon J. Gerraty } 5064fde40d9SSimon J. Gerraty 50706b9b3e0SSimon J. Gerraty /* Run the .for loop, imitating the actions of an include file. */ 5083955d011SMarcel Moolenaar void 5099f45a3c8SSimon J. Gerraty For_Run(unsigned headLineno, unsigned bodyReadLines) 5103955d011SMarcel Moolenaar { 5119f45a3c8SSimon J. Gerraty Buffer buf; 512dba7b0efSSimon J. Gerraty ForLoop *f = accumFor; 5133955d011SMarcel Moolenaar accumFor = NULL; 5143955d011SMarcel Moolenaar 5159f45a3c8SSimon J. Gerraty if (f->items.len > 0) { 5169f45a3c8SSimon J. Gerraty Buf_Init(&buf); 5179f45a3c8SSimon J. Gerraty Parse_PushInput(NULL, headLineno, bodyReadLines, buf, f); 5189f45a3c8SSimon J. Gerraty } else 519dba7b0efSSimon J. Gerraty ForLoop_Free(f); 5203955d011SMarcel Moolenaar } 521