Changeset 51 in kBuild for trunk/src/kmk/for.c
- Timestamp:
- Apr 7, 2003 1:30:32 AM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/for.c
r35 r51 13 13 * 3. All advertising materials mentioning features or use of this software 14 14 * must display the following acknowledgement: 15 * 16 * 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 17 * 4. Neither the name of the University nor the names of its contributors 18 18 * may be used to endorse or promote products derived from this software … … 34 34 #ifndef lint 35 35 #if 0 36 static char sccsid[] = "@(#)for.c 36 static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93"; 37 37 #else 38 38 static const char rcsid[] = 39 39 "$FreeBSD: src/usr.bin/make/for.c,v 1.10 1999/09/11 13:08:01 hoek Exp $"; 40 40 #endif 41 #define KLIBFILEDEF rcsid 41 42 #endif /* not lint */ 42 43 43 44 /*- 44 45 * for.c -- 45 * 46 * Functions to handle loops in a makefile. 46 47 * 47 48 * Interface: 48 * For_EvalEvaluate the loop in the passed line.49 * For_RunRun accumulated loop49 * For_Eval Evaluate the loop in the passed line. 50 * For_Run Run accumulated loop 50 51 * 51 52 */ … … 71 72 */ 72 73 73 static int forLevel = 0; /* Nesting level*/74 static char *forVar; /* Iteration variable*/75 static Buffer forBuf; /* Commands in loop*/76 static Lst forLst; /* List of items*/74 static int forLevel = 0; /* Nesting level */ 75 static char *forVar; /* Iteration variable */ 76 static Buffer forBuf; /* Commands in loop */ 77 static Lst forLst; /* List of items */ 77 78 78 79 /* … … 80 81 */ 81 82 typedef struct _For { 82 Buffer buf; /* Unexpanded buffer*/83 char* var; /* Index name*/84 Lst lst; /* List of variables*/83 Buffer buf; /* Unexpanded buffer */ 84 char* var; /* Index name */ 85 Lst lst; /* List of variables */ 85 86 } For; 86 87 87 static int ForExec 88 static int ForExec __P((ClientData, ClientData)); 88 89 89 90 … … 94 95 *----------------------------------------------------------------------- 95 96 * For_Eval -- 96 * 97 * 98 * 97 * Evaluate the for loop in the passed line. The line 98 * looks like this: 99 * .for <variable> in <varlist> 99 100 * 100 101 * Results: 101 * 102 * 103 * 102 * TRUE: We found a for loop, or we are inside a for loop 103 * FALSE: We did not find a for loop, or we found the end of the for 104 * for loop. 104 105 * 105 106 * Side Effects: 106 * 107 * None. 107 108 * 108 109 *----------------------------------------------------------------------- … … 110 111 int 111 112 For_Eval (line) 112 char 113 char *line; /* Line to parse */ 113 114 { 114 char 115 int level;/* Level at which to report errors. */115 char *ptr = line, *sub, *wrd; 116 int level; /* Level at which to report errors. */ 116 117 117 118 level = PARSE_FATAL; … … 119 120 120 121 if (forLevel == 0) { 121 Bufferbuf;122 intvarlen;123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 122 Buffer buf; 123 int varlen; 124 125 for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++) 126 continue; 127 /* 128 * If we are not in a for loop quickly determine if the statement is 129 * a for. 130 */ 131 if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' || 132 !isspace((unsigned char) ptr[3])) 133 return FALSE; 134 ptr += 3; 135 136 /* 137 * we found a for loop, and now we are going to parse it. 138 */ 139 while (*ptr && isspace((unsigned char) *ptr)) 140 ptr++; 141 142 /* 143 * Grab the variable 144 */ 145 buf = Buf_Init(0); 146 for (wrd = ptr; *ptr && !isspace((unsigned char) *ptr); ptr++) 147 continue; 148 Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd); 149 150 forVar = (char *) Buf_GetAll(buf, &varlen); 151 if (varlen == 0) { 152 Parse_Error (level, "missing variable in for"); 153 return 0; 154 } 155 Buf_Destroy(buf, FALSE); 156 157 while (*ptr && isspace((unsigned char) *ptr)) 158 ptr++; 159 160 /* 161 * Grab the `in' 162 */ 163 if (ptr[0] != 'i' || ptr[1] != 'n' || 164 !isspace((unsigned char) ptr[2])) { 165 Parse_Error (level, "missing `in' in for"); 166 printf("%s\n", ptr); 167 return 0; 168 } 169 ptr += 3; 170 171 while (*ptr && isspace((unsigned char) *ptr)) 172 ptr++; 173 174 /* 175 * Make a list with the remaining words 176 */ 177 forLst = Lst_Init(FALSE); 178 buf = Buf_Init(0); 179 sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE); 179 180 180 181 #define ADDWORD() \ 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 182 Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \ 183 Buf_AddByte(buf, (Byte) '\0'), \ 184 Lst_AtFront(forLst, (ClientData) Buf_GetAll(buf, &varlen)), \ 185 Buf_Destroy(buf, FALSE) 186 187 for (ptr = sub; *ptr && isspace((unsigned char) *ptr); ptr++) 188 continue; 189 190 for (wrd = ptr; *ptr; ptr++) 191 if (isspace((unsigned char) *ptr)) { 192 ADDWORD(); 193 buf = Buf_Init(0); 194 while (*ptr && isspace((unsigned char) *ptr)) 195 ptr++; 196 wrd = ptr--; 197 } 198 if (DEBUG(FOR)) 199 (void) fprintf(stderr, "For: Iterator %s List %s\n", forVar, sub); 200 if (ptr - wrd > 0) 201 ADDWORD(); 202 else 203 Buf_Destroy(buf, TRUE); 204 efree((Address) sub); 205 206 forBuf = Buf_Init(0); 207 forLevel++; 208 return 1; 208 209 } 209 210 else if (*ptr == '.') { 210 211 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 212 for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++) 213 continue; 214 215 if (strncmp(ptr, "endfor", 6) == 0 && 216 (isspace((unsigned char) ptr[6]) || !ptr[6])) { 217 if (DEBUG(FOR)) 218 (void) fprintf(stderr, "For: end for %d\n", forLevel); 219 if (--forLevel < 0) { 220 Parse_Error (level, "for-less endfor"); 221 return 0; 222 } 223 } 224 else if (strncmp(ptr, "for", 3) == 0 && 225 isspace((unsigned char) ptr[3])) { 226 forLevel++; 227 if (DEBUG(FOR)) 228 (void) fprintf(stderr, "For: new loop %d\n", forLevel); 229 } 229 230 } 230 231 231 232 if (forLevel != 0) { 232 233 234 233 Buf_AddBytes(forBuf, strlen(line), (Byte *) line); 234 Buf_AddByte(forBuf, (Byte) '\n'); 235 return 1; 235 236 } 236 237 else { 237 238 return 0; 238 239 } 239 240 } … … 242 243 *----------------------------------------------------------------------- 243 244 * ForExec -- 244 * 245 * Expand the for loop for this index and push it in the Makefile 245 246 * 246 247 * Results: 247 * 248 * None. 248 249 * 249 250 * Side Effects: 250 * 251 * None. 251 252 * 252 253 *----------------------------------------------------------------------- … … 262 263 Var_Set(arg->var, name, VAR_GLOBAL); 263 264 if (DEBUG(FOR)) 264 265 (void) fprintf(stderr, "--- %s = %s\n", arg->var, name); 265 266 Parse_FromString(Var_Subst(arg->var, (char *) Buf_GetAll(arg->buf, &len), 266 267 VAR_GLOBAL, FALSE)); 267 268 Var_Delete(arg->var, VAR_GLOBAL); 268 269 … … 275 276 *----------------------------------------------------------------------- 276 277 * For_Run -- 277 * 278 * Run the for loop, immitating the actions of an include file 278 279 * 279 280 * Results: 280 * 281 * None. 281 282 * 282 283 * Side Effects: 283 * 284 * None. 284 285 * 285 286 *----------------------------------------------------------------------- … … 291 292 292 293 if (forVar == NULL || forBuf == NULL || forLst == NULL) 293 294 return; 294 295 arg.var = forVar; 295 296 arg.buf = forBuf;
Note:
See TracChangeset
for help on using the changeset viewer.