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