1 /* $NetBSD: for.c,v 1.6 1997/09/28 03:31:03 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1992, The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifdef MAKE_BOOTSTRAP 37 static char rcsid[] = "$NetBSD: for.c,v 1.6 1997/09/28 03:31:03 lukem Exp $"; 38 #else 39 #include <sys/cdefs.h> 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93"; 43 #else 44 __RCSID("$NetBSD: for.c,v 1.6 1997/09/28 03:31:03 lukem Exp $"); 45 #endif 46 #endif /* not lint */ 47 #endif 48 49 /*- 50 * for.c -- 51 * Functions to handle loops in a makefile. 52 * 53 * Interface: 54 * For_Eval Evaluate the loop in the passed line. 55 * For_Run Run accumulated loop 56 * 57 */ 58 59 #include <ctype.h> 60 #include "make.h" 61 #include "hash.h" 62 #include "dir.h" 63 #include "buf.h" 64 65 /* 66 * For statements are of the form: 67 * 68 * .for <variable> in <varlist> 69 * ... 70 * .endfor 71 * 72 * The trick is to look for the matching end inside for for loop 73 * To do that, we count the current nesting level of the for loops. 74 * and the .endfor statements, accumulating all the statements between 75 * the initial .for loop and the matching .endfor; 76 * then we evaluate the for loop for each variable in the varlist. 77 */ 78 79 static int forLevel = 0; /* Nesting level */ 80 static char *forVar; /* Iteration variable */ 81 static Buffer forBuf; /* Commands in loop */ 82 static Lst forLst; /* List of items */ 83 84 /* 85 * State of a for loop. 86 */ 87 typedef struct _For { 88 Buffer buf; /* Unexpanded buffer */ 89 char* var; /* Index name */ 90 Lst lst; /* List of variables */ 91 } For; 92 93 static int ForExec __P((ClientData, ClientData)); 94 95 96 97 98 /*- 99 *----------------------------------------------------------------------- 100 * For_Eval -- 101 * Evaluate the for loop in the passed line. The line 102 * looks like this: 103 * .for <variable> in <varlist> 104 * 105 * Results: 106 * TRUE: We found a for loop, or we are inside a for loop 107 * FALSE: We did not find a for loop, or we found the end of the for 108 * for loop. 109 * 110 * Side Effects: 111 * None. 112 * 113 *----------------------------------------------------------------------- 114 */ 115 int 116 For_Eval (line) 117 char *line; /* Line to parse */ 118 { 119 char *ptr = line, *sub, *wrd; 120 int level; /* Level at which to report errors. */ 121 122 level = PARSE_FATAL; 123 124 125 if (forLevel == 0) { 126 Buffer buf; 127 int varlen; 128 129 for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++) 130 continue; 131 /* 132 * If we are not in a for loop quickly determine if the statement is 133 * a for. 134 */ 135 if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' || 136 !isspace((unsigned char) ptr[3])) 137 return FALSE; 138 ptr += 3; 139 140 /* 141 * we found a for loop, and now we are going to parse it. 142 */ 143 while (*ptr && isspace((unsigned char) *ptr)) 144 ptr++; 145 146 /* 147 * Grab the variable 148 */ 149 buf = Buf_Init(0); 150 for (wrd = ptr; *ptr && !isspace((unsigned char) *ptr); ptr++) 151 continue; 152 Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd); 153 154 forVar = (char *) Buf_GetAll(buf, &varlen); 155 if (varlen == 0) { 156 Parse_Error (level, "missing variable in for"); 157 return 0; 158 } 159 Buf_Destroy(buf, FALSE); 160 161 while (*ptr && isspace((unsigned char) *ptr)) 162 ptr++; 163 164 /* 165 * Grab the `in' 166 */ 167 if (ptr[0] != 'i' || ptr[1] != 'n' || 168 !isspace((unsigned char) ptr[2])) { 169 Parse_Error (level, "missing `in' in for"); 170 printf("%s\n", ptr); 171 return 0; 172 } 173 ptr += 3; 174 175 while (*ptr && isspace((unsigned char) *ptr)) 176 ptr++; 177 178 /* 179 * Make a list with the remaining words 180 */ 181 forLst = Lst_Init(FALSE); 182 buf = Buf_Init(0); 183 sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE); 184 185 #define ADDWORD() \ 186 Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \ 187 Buf_AddByte(buf, (Byte) '\0'), \ 188 Lst_AtFront(forLst, (ClientData) Buf_GetAll(buf, &varlen)), \ 189 Buf_Destroy(buf, FALSE) 190 191 for (ptr = sub; *ptr && isspace((unsigned char) *ptr); ptr++) 192 continue; 193 194 for (wrd = ptr; *ptr; ptr++) 195 if (isspace((unsigned char) *ptr)) { 196 ADDWORD(); 197 buf = Buf_Init(0); 198 while (*ptr && isspace((unsigned char) *ptr)) 199 ptr++; 200 wrd = ptr--; 201 } 202 if (DEBUG(FOR)) 203 (void) fprintf(stderr, "For: Iterator %s List %s\n", forVar, sub); 204 if (ptr - wrd > 0) 205 ADDWORD(); 206 else 207 Buf_Destroy(buf, TRUE); 208 free((Address) sub); 209 210 forBuf = Buf_Init(0); 211 forLevel++; 212 return 1; 213 } 214 else if (*ptr == '.') { 215 216 for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++) 217 continue; 218 219 if (strncmp(ptr, "endfor", 6) == 0 && 220 (isspace((unsigned char) ptr[6]) || !ptr[6])) { 221 if (DEBUG(FOR)) 222 (void) fprintf(stderr, "For: end for %d\n", forLevel); 223 if (--forLevel < 0) { 224 Parse_Error (level, "for-less endfor"); 225 return 0; 226 } 227 } 228 else if (strncmp(ptr, "for", 3) == 0 && 229 isspace((unsigned char) ptr[3])) { 230 forLevel++; 231 if (DEBUG(FOR)) 232 (void) fprintf(stderr, "For: new loop %d\n", forLevel); 233 } 234 } 235 236 if (forLevel != 0) { 237 Buf_AddBytes(forBuf, strlen(line), (Byte *) line); 238 Buf_AddByte(forBuf, (Byte) '\n'); 239 return 1; 240 } 241 else { 242 return 0; 243 } 244 } 245 246 /*- 247 *----------------------------------------------------------------------- 248 * ForExec -- 249 * Expand the for loop for this index and push it in the Makefile 250 * 251 * Results: 252 * None. 253 * 254 * Side Effects: 255 * None. 256 * 257 *----------------------------------------------------------------------- 258 */ 259 static int 260 ForExec(namep, argp) 261 ClientData namep; 262 ClientData argp; 263 { 264 char *name = (char *) namep; 265 For *arg = (For *) argp; 266 int len; 267 Var_Set(arg->var, name, VAR_GLOBAL); 268 if (DEBUG(FOR)) 269 (void) fprintf(stderr, "--- %s = %s\n", arg->var, name); 270 Parse_FromString(Var_Subst(arg->var, (char *) Buf_GetAll(arg->buf, &len), 271 VAR_GLOBAL, FALSE)); 272 Var_Delete(arg->var, VAR_GLOBAL); 273 274 return 0; 275 } 276 277 278 /*- 279 *----------------------------------------------------------------------- 280 * For_Run -- 281 * Run the for loop, immitating the actions of an include file 282 * 283 * Results: 284 * None. 285 * 286 * Side Effects: 287 * None. 288 * 289 *----------------------------------------------------------------------- 290 */ 291 void 292 For_Run() 293 { 294 For arg; 295 296 if (forVar == NULL || forBuf == NULL || forLst == NULL) 297 return; 298 arg.var = forVar; 299 arg.buf = forBuf; 300 arg.lst = forLst; 301 forVar = NULL; 302 forBuf = NULL; 303 forLst = NULL; 304 305 Lst_ForEach(arg.lst, ForExec, (ClientData) &arg); 306 307 free((Address)arg.var); 308 Lst_Destroy(arg.lst, (void (*) __P((ClientData))) free); 309 Buf_Destroy(arg.buf, TRUE); 310 } 311