xref: /original-bsd/usr.bin/make/for.c (revision bbd64002)
15e351555Sbostic /*
233717ddfSbostic  * Copyright (c) 1993
333717ddfSbostic  *	The Regents of the University of California.  All rights reserved.
45e351555Sbostic  *
55e351555Sbostic  * This code is derived from software contributed to Berkeley by
65e351555Sbostic  * Christos Zoulas.
75e351555Sbostic  *
85e351555Sbostic  * %sccs.include.redist.c%
95e351555Sbostic  */
105e351555Sbostic 
115e351555Sbostic #ifndef lint
12*bbd64002Schristos static char sccsid[] = "@(#)for.c	8.2 (Berkeley) 04/28/95";
135e351555Sbostic #endif /* not lint */
145e351555Sbostic 
155e351555Sbostic /*-
165e351555Sbostic  * for.c --
175e351555Sbostic  *	Functions to handle loops in a makefile.
185e351555Sbostic  *
195e351555Sbostic  * Interface:
205e351555Sbostic  *	For_Eval 	Evaluate the loop in the passed line.
215e351555Sbostic  *	For_Run		Run accumulated loop
225e351555Sbostic  *
235e351555Sbostic  */
245e351555Sbostic 
255e351555Sbostic #include    <ctype.h>
265e351555Sbostic #include    "make.h"
275e351555Sbostic #include    "hash.h"
285e351555Sbostic #include    "dir.h"
295e351555Sbostic #include    "buf.h"
305e351555Sbostic 
315e351555Sbostic /*
325e351555Sbostic  * For statements are of the form:
335e351555Sbostic  *
345e351555Sbostic  * .for <variable> in <varlist>
355e351555Sbostic  * ...
365e351555Sbostic  * .endfor
375e351555Sbostic  *
385e351555Sbostic  * The trick is to look for the matching end inside for for loop
395e351555Sbostic  * To do that, we count the current nesting level of the for loops.
405e351555Sbostic  * and the .endfor statements, accumulating all the statements between
415e351555Sbostic  * the initial .for loop and the matching .endfor;
425e351555Sbostic  * then we evaluate the for loop for each variable in the varlist.
435e351555Sbostic  */
445e351555Sbostic 
455e351555Sbostic static int  	  forLevel = 0;  	/* Nesting level	*/
465e351555Sbostic static char	 *forVar;		/* Iteration variable	*/
475e351555Sbostic static Buffer	  forBuf;		/* Commands in loop	*/
485e351555Sbostic static Lst	  forLst;		/* List of items	*/
495e351555Sbostic 
505e351555Sbostic /*
515e351555Sbostic  * State of a for loop.
525e351555Sbostic  */
53*bbd64002Schristos typedef struct _For {
545e351555Sbostic     Buffer	  buf;			/* Unexpanded buffer	*/
555e351555Sbostic     char*	  var;			/* Index name		*/
565e351555Sbostic     Lst  	  lst;			/* List of variables	*/
57*bbd64002Schristos } For;
585e351555Sbostic 
59*bbd64002Schristos static int ForExec	__P((ClientData, ClientData));
605e351555Sbostic 
615e351555Sbostic 
625e351555Sbostic 
635e351555Sbostic 
645e351555Sbostic /*-
655e351555Sbostic  *-----------------------------------------------------------------------
665e351555Sbostic  * For_Eval --
675e351555Sbostic  *	Evaluate the for loop in the passed line. The line
685e351555Sbostic  *	looks like this:
695e351555Sbostic  *	    .for <variable> in <varlist>
705e351555Sbostic  *
715e351555Sbostic  * Results:
725e351555Sbostic  *	TRUE: We found a for loop, or we are inside a for loop
735e351555Sbostic  *	FALSE: We did not find a for loop, or we found the end of the for
745e351555Sbostic  *	       for loop.
755e351555Sbostic  *
765e351555Sbostic  * Side Effects:
775e351555Sbostic  *	None.
785e351555Sbostic  *
795e351555Sbostic  *-----------------------------------------------------------------------
805e351555Sbostic  */
815e351555Sbostic int
For_Eval(line)825e351555Sbostic For_Eval (line)
835e351555Sbostic     char    	    *line;    /* Line to parse */
845e351555Sbostic {
855e351555Sbostic     char	    *ptr = line, *sub, *wrd;
865e351555Sbostic     int	    	    level;  	/* Level at which to report errors. */
875e351555Sbostic 
885e351555Sbostic     level = PARSE_FATAL;
895e351555Sbostic 
905e351555Sbostic 
915e351555Sbostic     if (forLevel == 0) {
925e351555Sbostic 	Buffer	    buf;
935e351555Sbostic 	int	    varlen;
945e351555Sbostic 
95*bbd64002Schristos 	for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
965e351555Sbostic 	    continue;
975e351555Sbostic 	/*
985e351555Sbostic 	 * If we are not in a for loop quickly determine if the statement is
995e351555Sbostic 	 * a for.
1005e351555Sbostic 	 */
101*bbd64002Schristos 	if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
102*bbd64002Schristos 	    !isspace((unsigned char) ptr[3]))
1035e351555Sbostic 	    return FALSE;
1045e351555Sbostic 	ptr += 3;
1055e351555Sbostic 
1065e351555Sbostic 	/*
1075e351555Sbostic 	 * we found a for loop, and now we are going to parse it.
1085e351555Sbostic 	 */
109*bbd64002Schristos 	while (*ptr && isspace((unsigned char) *ptr))
1105e351555Sbostic 	    ptr++;
1115e351555Sbostic 
1125e351555Sbostic 	/*
1135e351555Sbostic 	 * Grab the variable
1145e351555Sbostic 	 */
1155e351555Sbostic 	buf = Buf_Init(0);
116*bbd64002Schristos 	for (wrd = ptr; *ptr && !isspace((unsigned char) *ptr); ptr++)
1175e351555Sbostic 	    continue;
1185e351555Sbostic 	Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd);
1195e351555Sbostic 
1205e351555Sbostic 	forVar = (char *) Buf_GetAll(buf, &varlen);
1215e351555Sbostic 	if (varlen == 0) {
1225e351555Sbostic 	    Parse_Error (level, "missing variable in for");
1235e351555Sbostic 	    return 0;
1245e351555Sbostic 	}
1255e351555Sbostic 	Buf_Destroy(buf, FALSE);
1265e351555Sbostic 
127*bbd64002Schristos 	while (*ptr && isspace((unsigned char) *ptr))
1285e351555Sbostic 	    ptr++;
1295e351555Sbostic 
1305e351555Sbostic 	/*
1315e351555Sbostic 	 * Grab the `in'
1325e351555Sbostic 	 */
133*bbd64002Schristos 	if (ptr[0] != 'i' || ptr[1] != 'n' ||
134*bbd64002Schristos 	    !isspace((unsigned char) ptr[2])) {
1355e351555Sbostic 	    Parse_Error (level, "missing `in' in for");
1365e351555Sbostic 	    printf("%s\n", ptr);
1375e351555Sbostic 	    return 0;
1385e351555Sbostic 	}
1395e351555Sbostic 	ptr += 3;
1405e351555Sbostic 
141*bbd64002Schristos 	while (*ptr && isspace((unsigned char) *ptr))
1425e351555Sbostic 	    ptr++;
1435e351555Sbostic 
1445e351555Sbostic 	/*
1455e351555Sbostic 	 * Make a list with the remaining words
1465e351555Sbostic 	 */
1475e351555Sbostic 	forLst = Lst_Init(FALSE);
1485e351555Sbostic 	buf = Buf_Init(0);
1495e351555Sbostic 	sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
1505e351555Sbostic 
1515e351555Sbostic #define ADDWORD() \
1525e351555Sbostic 	Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \
1535e351555Sbostic 	Buf_AddByte(buf, (Byte) '\0'), \
1545e351555Sbostic 	Lst_AtEnd(forLst, (ClientData) Buf_GetAll(buf, &varlen)), \
1555e351555Sbostic 	Buf_Destroy(buf, FALSE)
1565e351555Sbostic 
157*bbd64002Schristos 	for (ptr = sub; *ptr && isspace((unsigned char) *ptr); ptr++)
1585e351555Sbostic 	    continue;
1595e351555Sbostic 
1605e351555Sbostic 	for (wrd = ptr; *ptr; ptr++)
161*bbd64002Schristos 	    if (isspace((unsigned char) *ptr)) {
1625e351555Sbostic 		ADDWORD();
1635e351555Sbostic 		buf = Buf_Init(0);
164*bbd64002Schristos 		while (*ptr && isspace((unsigned char) *ptr))
1655e351555Sbostic 		    ptr++;
1665e351555Sbostic 		wrd = ptr--;
1675e351555Sbostic 	    }
1685e351555Sbostic 	if (DEBUG(FOR))
1695e351555Sbostic 	    (void) fprintf(stderr, "For: Iterator %s List %s\n", forVar, sub);
1705e351555Sbostic 	if (ptr - wrd > 0)
1715e351555Sbostic 	    ADDWORD();
1725e351555Sbostic 	else
1735e351555Sbostic 	    Buf_Destroy(buf, TRUE);
1745e351555Sbostic 	free((Address) sub);
1755e351555Sbostic 
1765e351555Sbostic 	forBuf = Buf_Init(0);
1775e351555Sbostic 	forLevel++;
1785e351555Sbostic 	return 1;
1795e351555Sbostic     }
1805e351555Sbostic     else if (*ptr == '.') {
1815e351555Sbostic 
182*bbd64002Schristos 	for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
1835e351555Sbostic 	    continue;
1845e351555Sbostic 
185*bbd64002Schristos 	if (strncmp(ptr, "endfor", 6) == 0 &&
186*bbd64002Schristos 	    (isspace((unsigned char) ptr[6]) || !ptr[6])) {
1875e351555Sbostic 	    if (DEBUG(FOR))
1885e351555Sbostic 		(void) fprintf(stderr, "For: end for %d\n", forLevel);
1895e351555Sbostic 	    if (--forLevel < 0) {
1905e351555Sbostic 		Parse_Error (level, "for-less endfor");
1915e351555Sbostic 		return 0;
1925e351555Sbostic 	    }
1935e351555Sbostic 	}
194*bbd64002Schristos 	else if (strncmp(ptr, "for", 3) == 0 &&
195*bbd64002Schristos 		 isspace((unsigned char) ptr[3])) {
1965e351555Sbostic 	    forLevel++;
1975e351555Sbostic 	    if (DEBUG(FOR))
1985e351555Sbostic 		(void) fprintf(stderr, "For: new loop %d\n", forLevel);
1995e351555Sbostic 	}
2005e351555Sbostic     }
2015e351555Sbostic 
2025e351555Sbostic     if (forLevel != 0) {
2035e351555Sbostic 	Buf_AddBytes(forBuf, strlen(line), (Byte *) line);
2045e351555Sbostic 	Buf_AddByte(forBuf, (Byte) '\n');
2055e351555Sbostic 	return 1;
2065e351555Sbostic     }
2075e351555Sbostic     else {
2085e351555Sbostic 	return 0;
2095e351555Sbostic     }
2105e351555Sbostic }
2115e351555Sbostic 
2125e351555Sbostic /*-
2135e351555Sbostic  *-----------------------------------------------------------------------
2145e351555Sbostic  * ForExec --
2155e351555Sbostic  *	Expand the for loop for this index and push it in the Makefile
2165e351555Sbostic  *
2175e351555Sbostic  * Results:
2185e351555Sbostic  *	None.
2195e351555Sbostic  *
2205e351555Sbostic  * Side Effects:
2215e351555Sbostic  *	None.
2225e351555Sbostic  *
2235e351555Sbostic  *-----------------------------------------------------------------------
2245e351555Sbostic  */
2255e351555Sbostic static int
ForExec(namep,argp)226*bbd64002Schristos ForExec(namep, argp)
227*bbd64002Schristos     ClientData namep;
228*bbd64002Schristos     ClientData argp;
2295e351555Sbostic {
230*bbd64002Schristos     char *name = (char *) namep;
231*bbd64002Schristos     For *arg = (For *) argp;
2325e351555Sbostic     int len;
2335e351555Sbostic     Var_Set(arg->var, name, VAR_GLOBAL);
2345e351555Sbostic     if (DEBUG(FOR))
2355e351555Sbostic 	(void) fprintf(stderr, "--- %s = %s\n", arg->var, name);
2365e351555Sbostic     Parse_FromString(Var_Subst(arg->var, (char *) Buf_GetAll(arg->buf, &len),
2375e351555Sbostic 			       VAR_GLOBAL, FALSE));
2385e351555Sbostic     Var_Delete(arg->var, VAR_GLOBAL);
2395e351555Sbostic 
2405e351555Sbostic     return 0;
2415e351555Sbostic }
2425e351555Sbostic 
2435e351555Sbostic 
2445e351555Sbostic /*-
2455e351555Sbostic  *-----------------------------------------------------------------------
2465e351555Sbostic  * For_Run --
2475e351555Sbostic  *	Run the for loop, immitating the actions of an include file
2485e351555Sbostic  *
2495e351555Sbostic  * Results:
2505e351555Sbostic  *	None.
2515e351555Sbostic  *
2525e351555Sbostic  * Side Effects:
2535e351555Sbostic  *	None.
2545e351555Sbostic  *
2555e351555Sbostic  *-----------------------------------------------------------------------
2565e351555Sbostic  */
2575e351555Sbostic void
For_Run()2585e351555Sbostic For_Run()
2595e351555Sbostic {
260*bbd64002Schristos     For arg;
2615e351555Sbostic 
2625e351555Sbostic     if (forVar == NULL || forBuf == NULL || forLst == NULL)
2635e351555Sbostic 	return;
2645e351555Sbostic     arg.var = forVar;
2655e351555Sbostic     arg.buf = forBuf;
2665e351555Sbostic     arg.lst = forLst;
2675e351555Sbostic     forVar = NULL;
2685e351555Sbostic     forBuf = NULL;
2695e351555Sbostic     forLst = NULL;
2705e351555Sbostic 
2715e351555Sbostic     Lst_ForEach(arg.lst, ForExec, (ClientData) &arg);
2725e351555Sbostic 
2735e351555Sbostic     free((Address)arg.var);
274*bbd64002Schristos     Lst_Destroy(arg.lst, (void (*) __P((ClientData))) free);
2755e351555Sbostic     Buf_Destroy(arg.buf, TRUE);
2765e351555Sbostic }
277