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