1 /* $OpenBSD: for.c,v 1.48 2023/09/04 11:35:11 espie Exp $ */ 2 /* $NetBSD: for.c,v 1.4 1996/11/06 17:59:05 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1999 Marc Espie. 6 * 7 * Extensive code modifications for the OpenBSD project. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 22 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * Copyright (c) 1992, The Regents of the University of California. 33 * All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 */ 59 60 #include <assert.h> 61 #include <ctype.h> 62 #include <stddef.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include "defines.h" 67 #include "buf.h" 68 #include "for.h" 69 #include "lst.h" 70 #include "error.h" 71 #include "var.h" 72 #include "lowparse.h" 73 #include "str.h" 74 #include "memory.h" 75 76 /* 77 * For statements are of the form: 78 * 79 * .for <variable> [variable...] in <varlist> 80 * ... 81 * .endfor 82 * 83 * The trick is to look for the matching .end inside .for loops. 84 * To do that, we keep track of the nesting level of .for loops 85 * and matching .endfor statements, accumulating all statements between 86 * the initial .for loop and the matching .endfor, 87 * then we evaluate the .for loop for each variable in the varlist. 88 */ 89 90 /* State of a for loop. */ 91 struct For_ { 92 char *text; /* Unexpanded text */ 93 LIST vars; /* List of variables */ 94 LstNode var; /* Current var */ 95 int nvars; /* Total number of vars */ 96 LIST lst; /* List of items */ 97 size_t guess; /* Estimated expansion size */ 98 BUFFER buf; /* Accumulating text */ 99 unsigned long lineno; /* Line number at start of loop */ 100 unsigned long level; /* Nesting level */ 101 bool freeold; 102 }; 103 104 /* ForExec(value, handle); 105 * Expands next variable in loop sequence described by handle to value. */ 106 static void ForExec(void *, void *); 107 108 /* n = build_words_list(lst, s); 109 * Cuts string into words, pushes words into list, in reverse order, 110 * because Parse_FromString works as a stack. 111 * Returns the number of words. */ 112 static unsigned long build_words_list(Lst, const char *); 113 114 static unsigned long 115 build_words_list(Lst lst, const char *s) 116 { 117 const char *end, *wrd; 118 unsigned long n; 119 120 n = 0; 121 end = s; 122 123 while ((wrd = iterate_words(&end)) != NULL) { 124 Lst_AtFront(lst, escape_dupi(wrd, end, "\"'")); 125 n++; 126 } 127 return n; 128 } 129 130 For * 131 For_Eval(const char *line) 132 { 133 const char *ptr = line; 134 const char *wrd; 135 char *sub; 136 const char *endVar; 137 For *arg; 138 unsigned long n; 139 140 while (ISSPACE(*ptr)) 141 ptr++; 142 143 /* Parse loop. */ 144 145 arg = emalloc(sizeof(*arg)); 146 arg->nvars = 0; 147 Lst_Init(&arg->vars); 148 149 for (;;) { 150 /* Grab the variables. */ 151 for (wrd = ptr; *ptr && !ISSPACE(*ptr); ptr++) 152 continue; 153 if (ptr - wrd == 0) { 154 Parse_Error(PARSE_FATAL, "Syntax error in for"); 155 return 0; 156 } 157 endVar = ptr; 158 if (*ptr) { 159 ptr++; 160 while (ISSPACE(*ptr)) 161 ptr++; 162 } 163 /* End of variable list ? */ 164 if (endVar - wrd == 2 && wrd[0] == 'i' && wrd[1] == 'n') 165 break; 166 Lst_AtEnd(&arg->vars, Var_NewLoopVar(wrd, endVar)); 167 arg->nvars++; 168 } 169 if (arg->nvars == 0) { 170 Parse_Error(PARSE_FATAL, "Missing variable in for"); 171 return 0; 172 } 173 174 /* Make a list with the remaining words. */ 175 sub = Var_Subst(ptr, NULL, false); 176 if (DEBUG(FOR)) { 177 LstNode ln; 178 (void)fprintf(stderr, "For: Iterator "); 179 for (ln = Lst_First(&arg->vars); ln != NULL; ln = Lst_Adv(ln)) 180 (void)fprintf(stderr, "%s ", 181 Var_LoopVarName(Lst_Datum(ln))); 182 (void)fprintf(stderr, "List %s\n", sub); 183 } 184 185 Lst_Init(&arg->lst); 186 n = build_words_list(&arg->lst, sub); 187 free(sub); 188 if (arg->nvars != 1 && n % arg->nvars != 0) { 189 LstNode ln; 190 191 Parse_Error(PARSE_FATAL, "Wrong number of items in for loop"); 192 (void)fprintf(stderr, "%lu items for %d variables:", 193 n, arg->nvars); 194 for (ln = Lst_First(&arg->lst); ln != NULL; ln = Lst_Adv(ln)) { 195 char *p = Lst_Datum(ln); 196 197 (void)fprintf(stderr, " %s", p); 198 } 199 (void)fprintf(stderr, "\n"); 200 return 0; 201 } 202 arg->lineno = Parse_Getlineno(); 203 arg->level = 1; 204 Buf_Init(&arg->buf, 0); 205 206 return arg; 207 } 208 209 210 bool 211 For_Accumulate(For *arg, const char *line) 212 { 213 const char *ptr = line; 214 215 assert(arg->level > 0); 216 217 if (*ptr == '.') { 218 219 for (ptr++; ISSPACE(*ptr); ptr++) 220 continue; 221 222 if (strncmp(ptr, "endfor", 6) == 0 && 223 (ISSPACE(ptr[6]) || !ptr[6])) { 224 if (DEBUG(FOR)) 225 (void)fprintf(stderr, "For: end for %lu\n", 226 arg->level); 227 /* If matching endfor, don't add line to buffer. */ 228 if (--arg->level == 0) 229 return false; 230 } 231 else if (strncmp(ptr, "for", 3) == 0 && 232 ISSPACE(ptr[3])) { 233 arg->level++; 234 if (DEBUG(FOR)) 235 (void)fprintf(stderr, "For: new loop %lu\n", 236 arg->level); 237 } 238 } 239 Buf_AddString(&arg->buf, line); 240 Buf_AddChar(&arg->buf, '\n'); 241 return true; 242 } 243 244 245 #define GUESS_EXPANSION 32 246 static void 247 ForExec(void *valuep, void *argp) 248 { 249 char *value = valuep; 250 For *arg = argp; 251 BUFFER buf; 252 253 /* Parse_FromString pushes stuff back, so we need to go over vars in 254 reverse. */ 255 if (arg->var == NULL) { 256 arg->var = Lst_Last(&arg->vars); 257 arg->text = Buf_Retrieve(&arg->buf); 258 arg->freeold = false; 259 } 260 261 if (DEBUG(FOR)) 262 (void)fprintf(stderr, "--- %s = %s\n", 263 Var_LoopVarName(Lst_Datum(arg->var)), value); 264 Buf_Init(&buf, arg->guess); 265 Var_SubstVar(&buf, arg->text, Lst_Datum(arg->var), value); 266 if (arg->freeold) 267 free(arg->text); 268 arg->text = Buf_Retrieve(&buf); 269 arg->freeold = true; 270 arg->var = Lst_Rev(arg->var); 271 if (arg->var == NULL) 272 Parse_FromString(arg->text, arg->lineno); 273 } 274 275 276 void 277 For_Run(For *arg) 278 { 279 arg->text = Buf_Retrieve(&arg->buf); 280 arg->guess = Buf_Size(&arg->buf) + GUESS_EXPANSION; 281 282 arg->var = NULL; 283 Lst_ForEach(&arg->lst, ForExec, arg); 284 Buf_Destroy(&arg->buf); 285 Lst_Destroy(&arg->vars, (SimpleProc)Var_DeleteLoopVar); 286 Lst_Destroy(&arg->lst, (SimpleProc)free); 287 free(arg); 288 } 289