Home | History | Annotate | Line # | Download | only in make
for.c revision 1.7
      1  1.7  christos /*	$NetBSD: for.c,v 1.7 2000/04/16 22:08:06 christos Exp $	*/
      2  1.3  christos 
      3  1.1       cgd /*
      4  1.1       cgd  * Copyright (c) 1992, The Regents of the University of California.
      5  1.1       cgd  * All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.6     lukem #ifdef MAKE_BOOTSTRAP
     37  1.7  christos static char rcsid[] = "$NetBSD: for.c,v 1.7 2000/04/16 22:08:06 christos Exp $";
     38  1.6     lukem #else
     39  1.5  christos #include <sys/cdefs.h>
     40  1.1       cgd #ifndef lint
     41  1.3  christos #if 0
     42  1.4  christos static char sccsid[] = "@(#)for.c	8.1 (Berkeley) 6/6/93";
     43  1.3  christos #else
     44  1.7  christos __RCSID("$NetBSD: for.c,v 1.7 2000/04/16 22:08:06 christos Exp $");
     45  1.3  christos #endif
     46  1.1       cgd #endif /* not lint */
     47  1.6     lukem #endif
     48  1.1       cgd 
     49  1.1       cgd /*-
     50  1.1       cgd  * for.c --
     51  1.1       cgd  *	Functions to handle loops in a makefile.
     52  1.1       cgd  *
     53  1.1       cgd  * Interface:
     54  1.1       cgd  *	For_Eval 	Evaluate the loop in the passed line.
     55  1.1       cgd  *	For_Run		Run accumulated loop
     56  1.1       cgd  *
     57  1.1       cgd  */
     58  1.1       cgd 
     59  1.1       cgd #include    <ctype.h>
     60  1.7  christos #include    <assert.h>
     61  1.1       cgd #include    "make.h"
     62  1.1       cgd #include    "hash.h"
     63  1.1       cgd #include    "dir.h"
     64  1.1       cgd #include    "buf.h"
     65  1.1       cgd 
     66  1.1       cgd /*
     67  1.1       cgd  * For statements are of the form:
     68  1.1       cgd  *
     69  1.1       cgd  * .for <variable> in <varlist>
     70  1.1       cgd  * ...
     71  1.1       cgd  * .endfor
     72  1.1       cgd  *
     73  1.1       cgd  * The trick is to look for the matching end inside for for loop
     74  1.1       cgd  * To do that, we count the current nesting level of the for loops.
     75  1.1       cgd  * and the .endfor statements, accumulating all the statements between
     76  1.4  christos  * the initial .for loop and the matching .endfor;
     77  1.1       cgd  * then we evaluate the for loop for each variable in the varlist.
     78  1.7  christos  *
     79  1.7  christos  * Note that any nested fors are just passed through; they get handled
     80  1.7  christos  * recursively in For_Eval when we're expanding the enclosing for in
     81  1.7  christos  * For_Run.
     82  1.1       cgd  */
     83  1.1       cgd 
     84  1.1       cgd static int  	  forLevel = 0;  	/* Nesting level	*/
     85  1.1       cgd 
     86  1.1       cgd /*
     87  1.1       cgd  * State of a for loop.
     88  1.1       cgd  */
     89  1.2       jtc typedef struct _For {
     90  1.7  christos     Buffer	  buf;			/* Body of loop		*/
     91  1.7  christos     char	**vars;			/* Iteration variables	*/
     92  1.7  christos     int           nvars;		/* # of iteration vars	*/
     93  1.7  christos     Lst  	  lst;			/* List of items	*/
     94  1.2       jtc } For;
     95  1.1       cgd 
     96  1.7  christos static For        accumFor;             /* Loop being accumulated */
     97  1.1       cgd 
     98  1.7  christos static void ForAddVar	__P((const char *, size_t));
     99  1.1       cgd 
    100  1.1       cgd 
    101  1.1       cgd 
    102  1.7  christos 
    104  1.7  christos /*-
    105  1.7  christos  *-----------------------------------------------------------------------
    106  1.7  christos  * ForAddVar --
    107  1.7  christos  *	Add an iteration variable to the currently accumulating for.
    108  1.7  christos  *
    109  1.7  christos  * Results: none
    110  1.7  christos  * Side effects: no additional side effects.
    111  1.7  christos  *-----------------------------------------------------------------------
    112  1.7  christos  */
    113  1.7  christos static void
    114  1.7  christos ForAddVar(data, len)
    115  1.7  christos 	const char *data;
    116  1.7  christos 	size_t len;
    117  1.7  christos {
    118  1.7  christos 	Buffer buf;
    119  1.7  christos 	size_t varlen;
    120  1.7  christos 
    121  1.7  christos 	buf = Buf_Init(0);
    122  1.7  christos 	Buf_AddBytes(buf, len, (Byte *) data);
    123  1.7  christos 
    124  1.7  christos 	accumFor.nvars++;
    125  1.7  christos 	accumFor.vars = erealloc(accumFor.vars, accumFor.nvars*sizeof(char *));
    126  1.7  christos 
    127  1.7  christos 	accumFor.vars[accumFor.nvars-1] = (char *) Buf_GetAll(buf, &varlen);
    128  1.7  christos 
    129  1.7  christos 	Buf_Destroy(buf, FALSE);
    130  1.7  christos }
    131  1.1       cgd 
    132  1.1       cgd /*-
    133  1.1       cgd  *-----------------------------------------------------------------------
    134  1.1       cgd  * For_Eval --
    135  1.1       cgd  *	Evaluate the for loop in the passed line. The line
    136  1.1       cgd  *	looks like this:
    137  1.1       cgd  *	    .for <variable> in <varlist>
    138  1.1       cgd  *
    139  1.1       cgd  * Results:
    140  1.1       cgd  *	TRUE: We found a for loop, or we are inside a for loop
    141  1.1       cgd  *	FALSE: We did not find a for loop, or we found the end of the for
    142  1.1       cgd  *	       for loop.
    143  1.1       cgd  *
    144  1.1       cgd  * Side Effects:
    145  1.1       cgd  *	None.
    146  1.1       cgd  *
    147  1.1       cgd  *-----------------------------------------------------------------------
    148  1.1       cgd  */
    149  1.1       cgd int
    150  1.1       cgd For_Eval (line)
    151  1.1       cgd     char    	    *line;    /* Line to parse */
    152  1.7  christos {
    153  1.1       cgd     char	    *ptr = line, *sub, *in, *wrd;
    154  1.1       cgd     int	    	    level;  	/* Level at which to report errors. */
    155  1.1       cgd 
    156  1.1       cgd     level = PARSE_FATAL;
    157  1.1       cgd 
    158  1.1       cgd 
    159  1.1       cgd     if (forLevel == 0) {
    160  1.1       cgd 	Buffer	    buf;
    161  1.1       cgd 	int	    varlen;
    162  1.2       jtc 
    163  1.1       cgd 	for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
    164  1.1       cgd 	    continue;
    165  1.1       cgd 	/*
    166  1.1       cgd 	 * If we are not in a for loop quickly determine if the statement is
    167  1.1       cgd 	 * a for.
    168  1.2       jtc 	 */
    169  1.2       jtc 	if (ptr[0] != 'f' || ptr[1] != 'o' || ptr[2] != 'r' ||
    170  1.1       cgd 	    !isspace((unsigned char) ptr[3]))
    171  1.1       cgd 	    return FALSE;
    172  1.4  christos 	ptr += 3;
    173  1.1       cgd 
    174  1.1       cgd 	/*
    175  1.1       cgd 	 * we found a for loop, and now we are going to parse it.
    176  1.2       jtc 	 */
    177  1.1       cgd 	while (*ptr && isspace((unsigned char) *ptr))
    178  1.4  christos 	    ptr++;
    179  1.1       cgd 
    180  1.7  christos 	/*
    181  1.1       cgd 	 * Find the "in".
    182  1.7  christos 	 */
    183  1.7  christos 	for (in = ptr; *in; in++) {
    184  1.7  christos 	    if (isspace((unsigned char) in[0]) && in[1]== 'i' &&
    185  1.7  christos 		in[2] == 'n' &&
    186  1.7  christos 		(in[3] == '\0' || isspace((unsigned char) in[3])))
    187  1.7  christos 		break;
    188  1.7  christos 	}
    189  1.7  christos 	if (*in == '\0') {
    190  1.1       cgd 	    Parse_Error(level, "missing `in' in for");
    191  1.1       cgd 	    return 0;
    192  1.1       cgd 	}
    193  1.1       cgd 
    194  1.7  christos 	/*
    195  1.1       cgd 	 * Grab the variables.
    196  1.7  christos 	 */
    197  1.7  christos 	accumFor.vars = NULL;
    198  1.7  christos 
    199  1.7  christos 	while (ptr < in) {
    200  1.7  christos 	    wrd = ptr;
    201  1.7  christos 	    while (*ptr && !isspace((unsigned char) *ptr))
    202  1.7  christos 	        ptr++;
    203  1.7  christos 	    ForAddVar(wrd, ptr - wrd);
    204  1.7  christos 	    while (*ptr && isspace((unsigned char) *ptr))
    205  1.7  christos 		ptr++;
    206  1.7  christos 	}
    207  1.7  christos 
    208  1.7  christos 	if (accumFor.nvars == 0) {
    209  1.1       cgd 	    Parse_Error(level, "no iteration variables in for");
    210  1.1       cgd 	    return 0;
    211  1.7  christos 	}
    212  1.7  christos 
    213  1.7  christos 	/* At this point we should be pointing right at the "in" */
    214  1.7  christos 	assert(!memcmp(ptr, "in", 2));
    215  1.1       cgd 	ptr += 2;
    216  1.2       jtc 
    217  1.1       cgd 	while (*ptr && isspace((unsigned char) *ptr))
    218  1.1       cgd 	    ptr++;
    219  1.1       cgd 
    220  1.1       cgd 	/*
    221  1.1       cgd 	 * Make a list with the remaining words
    222  1.7  christos 	 */
    223  1.1       cgd 	accumFor.lst = Lst_Init(FALSE);
    224  1.4  christos 	buf = Buf_Init(0);
    225  1.1       cgd 	sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
    226  1.1       cgd 
    227  1.1       cgd #define ADDWORD() \
    228  1.1       cgd 	Buf_AddBytes(buf, ptr - wrd, (Byte *) wrd), \
    229  1.7  christos 	Buf_AddByte(buf, (Byte) '\0'), \
    230  1.1       cgd 	Lst_AtFront(accumFor.lst, (ClientData) Buf_GetAll(buf, &varlen)), \
    231  1.1       cgd 	Buf_Destroy(buf, FALSE)
    232  1.2       jtc 
    233  1.1       cgd 	for (ptr = sub; *ptr && isspace((unsigned char) *ptr); ptr++)
    234  1.1       cgd 	    continue;
    235  1.1       cgd 
    236  1.2       jtc 	for (wrd = ptr; *ptr; ptr++)
    237  1.1       cgd 	    if (isspace((unsigned char) *ptr)) {
    238  1.1       cgd 		ADDWORD();
    239  1.2       jtc 		buf = Buf_Init(0);
    240  1.1       cgd 		while (*ptr && isspace((unsigned char) *ptr))
    241  1.1       cgd 		    ptr++;
    242  1.1       cgd 		wrd = ptr--;
    243  1.7  christos 	    }
    244  1.7  christos 	if (DEBUG(FOR)) {
    245  1.7  christos 	    int i;
    246  1.7  christos 	    for (i = 0; i < accumFor.nvars; i++) {
    247  1.7  christos 		(void) fprintf(stderr, "For: variable %s\n", accumFor.vars[i]);
    248  1.7  christos 	    }
    249  1.7  christos 	    (void) fprintf(stderr, "For: list %s\n", sub);
    250  1.4  christos 	}
    251  1.1       cgd 	if (ptr - wrd > 0)
    252  1.1       cgd 	    ADDWORD();
    253  1.1       cgd 	else
    254  1.1       cgd 	    Buf_Destroy(buf, TRUE);
    255  1.4  christos 	free((Address) sub);
    256  1.7  christos 
    257  1.1       cgd 	accumFor.buf = Buf_Init(0);
    258  1.1       cgd 	forLevel++;
    259  1.1       cgd 	return 1;
    260  1.1       cgd     }
    261  1.1       cgd     else if (*ptr == '.') {
    262  1.2       jtc 
    263  1.1       cgd 	for (ptr++; *ptr && isspace((unsigned char) *ptr); ptr++)
    264  1.1       cgd 	    continue;
    265  1.2       jtc 
    266  1.2       jtc 	if (strncmp(ptr, "endfor", 6) == 0 &&
    267  1.1       cgd 	    (isspace((unsigned char) ptr[6]) || !ptr[6])) {
    268  1.1       cgd 	    if (DEBUG(FOR))
    269  1.1       cgd 		(void) fprintf(stderr, "For: end for %d\n", forLevel);
    270  1.1       cgd 	    if (--forLevel < 0) {
    271  1.1       cgd 		Parse_Error (level, "for-less endfor");
    272  1.1       cgd 		return 0;
    273  1.1       cgd 	    }
    274  1.2       jtc 	}
    275  1.2       jtc 	else if (strncmp(ptr, "for", 3) == 0 &&
    276  1.1       cgd 		 isspace((unsigned char) ptr[3])) {
    277  1.1       cgd 	    forLevel++;
    278  1.1       cgd 	    if (DEBUG(FOR))
    279  1.1       cgd 		(void) fprintf(stderr, "For: new loop %d\n", forLevel);
    280  1.1       cgd 	}
    281  1.1       cgd     }
    282  1.1       cgd 
    283  1.7  christos     if (forLevel != 0) {
    284  1.7  christos 	Buf_AddBytes(accumFor.buf, strlen(line), (Byte *) line);
    285  1.1       cgd 	Buf_AddByte(accumFor.buf, (Byte) '\n');
    286  1.1       cgd 	return 1;
    287  1.1       cgd     }
    288  1.1       cgd     else {
    289  1.1       cgd 	return 0;
    290  1.1       cgd     }
    291  1.1       cgd }
    292  1.1       cgd 
    293  1.1       cgd 
    294  1.1       cgd /*-
    296  1.7  christos  *-----------------------------------------------------------------------
    297  1.1       cgd  * For_Run --
    298  1.1       cgd  *	Run the for loop, imitating the actions of an include file
    299  1.1       cgd  *
    300  1.1       cgd  * Results:
    301  1.1       cgd  *	None.
    302  1.1       cgd  *
    303  1.1       cgd  * Side Effects:
    304  1.1       cgd  *	None.
    305  1.1       cgd  *
    306  1.1       cgd  *-----------------------------------------------------------------------
    307  1.1       cgd  */
    308  1.1       cgd void
    309  1.2       jtc For_Run()
    310  1.7  christos {
    311  1.7  christos     For arg;
    312  1.7  christos     LstNode ln;
    313  1.7  christos     char **values;
    314  1.7  christos     int i, done = 0, len;
    315  1.7  christos     char *guy, *orig_guy, *old_guy;
    316  1.7  christos 
    317  1.7  christos     if (accumFor.buf == NULL || accumFor.vars == NULL || accumFor.lst == NULL)
    318  1.7  christos 	return;
    319  1.7  christos     arg = accumFor;
    320  1.7  christos     accumFor.buf = NULL;
    321  1.7  christos     accumFor.vars = NULL;
    322  1.1       cgd     accumFor.nvars = 0;
    323  1.7  christos     accumFor.lst = NULL;
    324  1.1       cgd 
    325  1.1       cgd     if (Lst_Open(arg.lst) != SUCCESS)
    326  1.7  christos 	return;
    327  1.7  christos 
    328  1.7  christos     values = emalloc(arg.nvars * sizeof(char *));
    329  1.7  christos 
    330  1.7  christos     while (!done) {
    331  1.7  christos 	/*
    332  1.7  christos 	 * due to the dumb way this is set up, this loop must run
    333  1.7  christos 	 * backwards.
    334  1.7  christos 	 */
    335  1.7  christos 	for (i = arg.nvars - 1; i >= 0; i--) {
    336  1.7  christos 	    ln = Lst_Next(arg.lst);
    337  1.7  christos 	    if (ln == NILLNODE) {
    338  1.7  christos 		if (i != arg.nvars-1) {
    339  1.7  christos 		    Parse_Error(PARSE_FATAL,
    340  1.7  christos 			"Not enough words in for substitution list");
    341  1.7  christos 		}
    342  1.7  christos 		done = 1;
    343  1.7  christos 		break;
    344  1.7  christos 	    } else {
    345  1.7  christos 		values[i] = (char *) Lst_Datum(ln);
    346  1.7  christos 	    }
    347  1.7  christos 	}
    348  1.7  christos 	if (done)
    349  1.7  christos 	    break;
    350  1.7  christos 
    351  1.7  christos 	for (i = 0; i < arg.nvars; i++) {
    352  1.7  christos 	    Var_Set(arg.vars[i], values[i], VAR_GLOBAL);
    353  1.7  christos 	    if (DEBUG(FOR))
    354  1.7  christos 		(void) fprintf(stderr, "--- %s = %s\n", arg.vars[i],
    355  1.7  christos 		    values[i]);
    356  1.7  christos 	}
    357  1.7  christos 
    358  1.7  christos 	/*
    359  1.7  christos 	 * Hack, hack, kludge.
    360  1.7  christos 	 * This is really ugly, but to do it any better way would require
    361  1.7  christos 	 * making major changes to var.c, which I don't want to get into
    362  1.7  christos 	 * yet. There is no mechanism for expanding some variables, only
    363  1.7  christos 	 * for expanding a single variable. That should be corrected, but
    364  1.7  christos 	 * not right away. (XXX)
    365  1.7  christos 	 */
    366  1.7  christos 
    367  1.7  christos 	guy = (char *) Buf_GetAll(arg.buf, &len);
    368  1.7  christos 	orig_guy = guy;
    369  1.7  christos 	for (i = 0; i < arg.nvars; i++) {
    370  1.7  christos 	    old_guy = guy;
    371  1.7  christos 	    guy = Var_Subst(arg.vars[i], guy, VAR_GLOBAL, FALSE);
    372  1.7  christos 	    if (old_guy != orig_guy)
    373  1.7  christos 		free(old_guy);
    374  1.7  christos 	}
    375  1.7  christos 	Parse_FromString(guy);
    376  1.7  christos 
    377  1.7  christos 	for (i = 0; i < arg.nvars; i++)
    378  1.7  christos 	    Var_Delete(arg.vars[i], VAR_GLOBAL);
    379  1.7  christos     }
    380  1.7  christos 
    381  1.7  christos     free(values);
    382  1.7  christos 
    383  1.7  christos     Lst_Close(arg.lst);
    384  1.7  christos 
    385  1.7  christos     for (i=0; i<arg.nvars; i++) {
    386  1.7  christos 	free(arg.vars[i]);
    387  1.1       cgd     }
    388  1.2       jtc     free(arg.vars);
    389  1.1       cgd 
    390  1.1       cgd     Lst_Destroy(arg.lst, (void (*) __P((ClientData))) free);
    391                    Buf_Destroy(arg.buf, TRUE);
    392                }
    393