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