Home | History | Annotate | Line # | Download | only in make
compat.c revision 1.55
      1  1.55       jmc /*	$NetBSD: compat.c,v 1.55 2004/07/01 04:39:30 jmc Exp $	*/
      2  1.10  christos 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  1.47       agc  * All rights reserved.
      6  1.47       agc  *
      7  1.47       agc  * This code is derived from software contributed to Berkeley by
      8  1.47       agc  * Adam de Boor.
      9  1.47       agc  *
     10  1.47       agc  * Redistribution and use in source and binary forms, with or without
     11  1.47       agc  * modification, are permitted provided that the following conditions
     12  1.47       agc  * are met:
     13  1.47       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.47       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.47       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.47       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.47       agc  *    documentation and/or other materials provided with the distribution.
     18  1.47       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.47       agc  *    may be used to endorse or promote products derived from this software
     20  1.47       agc  *    without specific prior written permission.
     21  1.47       agc  *
     22  1.47       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.47       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.47       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.47       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.47       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.47       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.47       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.47       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.47       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.47       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.47       agc  * SUCH DAMAGE.
     33  1.47       agc  */
     34  1.47       agc 
     35  1.47       agc /*
     36   1.1       cgd  * Copyright (c) 1988, 1989 by Adam de Boor
     37   1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     38   1.1       cgd  * All rights reserved.
     39   1.1       cgd  *
     40   1.1       cgd  * This code is derived from software contributed to Berkeley by
     41   1.1       cgd  * Adam de Boor.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.1       cgd  *    must display the following acknowledgement:
     53   1.1       cgd  *	This product includes software developed by the University of
     54   1.1       cgd  *	California, Berkeley and its contributors.
     55   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.1       cgd  *    may be used to endorse or promote products derived from this software
     57   1.1       cgd  *    without specific prior written permission.
     58   1.1       cgd  *
     59   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1       cgd  * SUCH DAMAGE.
     70   1.1       cgd  */
     71   1.1       cgd 
     72  1.53      ross #ifndef MAKE_NATIVE
     73  1.55       jmc static char rcsid[] = "$NetBSD: compat.c,v 1.55 2004/07/01 04:39:30 jmc Exp $";
     74  1.22     lukem #else
     75  1.21  christos #include <sys/cdefs.h>
     76   1.1       cgd #ifndef lint
     77  1.10  christos #if 0
     78  1.14  christos static char sccsid[] = "@(#)compat.c	8.2 (Berkeley) 3/19/94";
     79  1.10  christos #else
     80  1.55       jmc __RCSID("$NetBSD: compat.c,v 1.55 2004/07/01 04:39:30 jmc Exp $");
     81  1.10  christos #endif
     82   1.1       cgd #endif /* not lint */
     83  1.22     lukem #endif
     84   1.1       cgd 
     85   1.1       cgd /*-
     86   1.1       cgd  * compat.c --
     87   1.1       cgd  *	The routines in this file implement the full-compatibility
     88   1.1       cgd  *	mode of PMake. Most of the special functionality of PMake
     89   1.1       cgd  *	is available in this mode. Things not supported:
     90   1.1       cgd  *	    - different shells.
     91   1.1       cgd  *	    - friendly variable substitution.
     92   1.1       cgd  *
     93   1.1       cgd  * Interface:
     94   1.1       cgd  *	Compat_Run	    Initialize things for this module and recreate
     95   1.1       cgd  *	    	  	    thems as need creatin'
     96   1.1       cgd  */
     97   1.1       cgd 
     98   1.1       cgd #include    <sys/types.h>
     99  1.11       jtc #include    <sys/stat.h>
    100   1.1       cgd #include    <sys/wait.h>
    101  1.44       wiz 
    102   1.1       cgd #include    <ctype.h>
    103  1.11       jtc #include    <errno.h>
    104  1.11       jtc #include    <signal.h>
    105  1.44       wiz #include    <stdio.h>
    106  1.44       wiz 
    107   1.1       cgd #include    "make.h"
    108   1.5       cgd #include    "hash.h"
    109   1.5       cgd #include    "dir.h"
    110   1.5       cgd #include    "job.h"
    111  1.43     bjh21 #include    "pathnames.h"
    112   1.1       cgd 
    113   1.1       cgd /*
    114   1.1       cgd  * The following array is used to make a fast determination of which
    115   1.1       cgd  * characters are interpreted specially by the shell.  If a command
    116   1.1       cgd  * contains any of these characters, it is executed by the shell, not
    117   1.1       cgd  * directly by us.
    118   1.1       cgd  */
    119   1.1       cgd 
    120   1.1       cgd static char 	    meta[256];
    121   1.1       cgd 
    122   1.1       cgd static GNode	    *curTarg = NILGNODE;
    123   1.1       cgd static GNode	    *ENDNode;
    124  1.44       wiz static void CompatInterrupt(int);
    125  1.44       wiz static int CompatMake(ClientData, ClientData);
    126   1.1       cgd 
    127  1.54       sjg static void
    128  1.54       sjg Compat_Init(void)
    129  1.54       sjg {
    130  1.54       sjg     const char *cp;
    131  1.54       sjg 
    132  1.54       sjg     Shell_Init();		/* setup default shell */
    133  1.54       sjg 
    134  1.54       sjg     for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
    135  1.54       sjg 	meta[(unsigned char) *cp] = 1;
    136  1.54       sjg     }
    137  1.54       sjg     /*
    138  1.54       sjg      * The null character serves as a sentinel in the string.
    139  1.54       sjg      */
    140  1.54       sjg     meta[0] = 1;
    141  1.54       sjg }
    142  1.54       sjg 
    143   1.1       cgd /*-
    144   1.1       cgd  *-----------------------------------------------------------------------
    145   1.1       cgd  * CompatInterrupt --
    146   1.1       cgd  *	Interrupt the creation of the current target and remove it if
    147   1.1       cgd  *	it ain't precious.
    148   1.1       cgd  *
    149   1.1       cgd  * Results:
    150   1.1       cgd  *	None.
    151   1.1       cgd  *
    152   1.1       cgd  * Side Effects:
    153   1.1       cgd  *	The target is removed and the process exits. If .INTERRUPT exists,
    154   1.1       cgd  *	its commands are run first WITH INTERRUPTS IGNORED..
    155   1.1       cgd  *
    156   1.1       cgd  *-----------------------------------------------------------------------
    157   1.1       cgd  */
    158   1.1       cgd static void
    159  1.44       wiz CompatInterrupt(int signo)
    160   1.1       cgd {
    161   1.1       cgd     GNode   *gn;
    162  1.14  christos 
    163   1.1       cgd     if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
    164   1.7       jtc 	char	  *p1;
    165   1.7       jtc 	char 	  *file = Var_Value (TARGET, curTarg, &p1);
    166   1.1       cgd 
    167  1.12  christos 	if (!noExecute && eunlink(file) != -1) {
    168  1.42  christos 	    Error("*** %s removed", file);
    169   1.1       cgd 	}
    170   1.7       jtc 	if (p1)
    171   1.7       jtc 	    free(p1);
    172   1.1       cgd 
    173   1.1       cgd 	/*
    174   1.1       cgd 	 * Run .INTERRUPT only if hit with interrupt signal
    175   1.1       cgd 	 */
    176   1.1       cgd 	if (signo == SIGINT) {
    177   1.1       cgd 	    gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
    178   1.1       cgd 	    if (gn != NILGNODE) {
    179   1.1       cgd 		Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
    180   1.1       cgd 	    }
    181   1.1       cgd 	}
    182   1.7       jtc 
    183   1.1       cgd     }
    184   1.7       jtc     exit (signo);
    185   1.1       cgd }
    186   1.1       cgd 
    187   1.1       cgd /*-
    189   1.1       cgd  *-----------------------------------------------------------------------
    190   1.1       cgd  * CompatRunCommand --
    191   1.1       cgd  *	Execute the next command for a target. If the command returns an
    192   1.1       cgd  *	error, the node's made field is set to ERROR and creation stops.
    193  1.44       wiz  *
    194  1.44       wiz  * Input:
    195  1.44       wiz  *	cmdp		Command to execute
    196  1.44       wiz  *	gnp		Node from which the command came
    197   1.1       cgd  *
    198   1.1       cgd  * Results:
    199   1.1       cgd  *	0 if the command succeeded, 1 if an error occurred.
    200   1.1       cgd  *
    201   1.1       cgd  * Side Effects:
    202   1.1       cgd  *	The node's 'made' field may be set to ERROR.
    203   1.1       cgd  *
    204   1.1       cgd  *-----------------------------------------------------------------------
    205  1.54       sjg  */
    206  1.44       wiz int
    207   1.1       cgd CompatRunCommand(ClientData cmdp, ClientData gnp)
    208   1.1       cgd {
    209  1.24  christos     char    	  *cmdStart;	/* Start of expanded command */
    210   1.1       cgd     char 	  *cp, *bp;
    211  1.54       sjg     Boolean 	  silent,   	/* Don't print command */
    212   1.1       cgd 	    	  doIt,		/* Execute even if -n */
    213  1.13  christos 		  errCheck; 	/* Check errors */
    214   1.1       cgd     int 	  reason;   	/* Reason for child's death */
    215   1.1       cgd     int	    	  status;   	/* Description of child's death */
    216  1.37   reinoud     int	    	  cpid;	    	/* Child actually found */
    217   1.1       cgd     ReturnStatus  retstat;    	/* Status of fork */
    218  1.45  christos     LstNode 	  cmdNode;  	/* Node where current command is located */
    219   1.1       cgd     const char  **av;	    	/* Argument vector for thing to exec */
    220   1.1       cgd     int	    	  argc;	    	/* Number of arguments in av or 0 if not
    221   1.1       cgd 				 * dynamically allocated */
    222   1.1       cgd     Boolean 	  local;    	/* TRUE if command should be executed
    223   1.7       jtc 				 * locally */
    224   1.7       jtc     char	  *cmd = (char *) cmdp;
    225   1.1       cgd     GNode	  *gn = (GNode *) gnp;
    226  1.14  christos 
    227   1.6       cgd     /*
    228   1.6       cgd      * Avoid clobbered variable warnings by forcing the compiler
    229   1.6       cgd      * to ``unregister'' variables
    230   1.6       cgd      */
    231   1.6       cgd #if __GNUC__
    232   1.6       cgd     (void) &av;
    233  1.55       jmc     (void) &errCheck;
    234   1.6       cgd     (void) &cmd;
    235   1.1       cgd #endif
    236   1.1       cgd     silent = gn->type & OP_SILENT;
    237  1.54       sjg     errCheck = !(gn->type & OP_IGNORE);
    238  1.54       sjg     doIt = FALSE;
    239   1.1       cgd 
    240   1.5       cgd     cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
    241   1.1       cgd     cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
    242   1.1       cgd 
    243  1.24  christos     /*
    244   1.1       cgd      * brk_string will return an argv with a NULL in av[0], thus causing
    245   1.1       cgd      * execvp to choke and die horribly. Besides, how can we execute a null
    246   1.1       cgd      * command? In any case, we warn the user that the command expanded to
    247   1.1       cgd      * nothing (is this the right thing to do?).
    248  1.14  christos      */
    249   1.1       cgd 
    250   1.7       jtc     if (*cmdStart == '\0') {
    251   1.1       cgd 	free(cmdStart);
    252   1.1       cgd 	Error("%s expands to empty string", cmd);
    253   1.1       cgd 	return(0);
    254   1.1       cgd     } else {
    255   1.1       cgd 	cmd = cmdStart;
    256   1.1       cgd     }
    257   1.1       cgd     Lst_Replace (cmdNode, (ClientData)cmdStart);
    258   1.1       cgd 
    259   1.1       cgd     if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
    260   1.1       cgd 	(void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
    261   1.1       cgd 	return(0);
    262   1.1       cgd     } else if (strcmp(cmdStart, "...") == 0) {
    263   1.1       cgd 	gn->type |= OP_SAVE_CMDS;
    264   1.1       cgd 	return(0);
    265   1.1       cgd     }
    266  1.54       sjg 
    267  1.54       sjg     while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
    268  1.54       sjg 	switch (*cmd) {
    269   1.1       cgd 	case '@':
    270  1.54       sjg 	    silent = TRUE;
    271  1.54       sjg 	    break;
    272   1.1       cgd 	case '-':
    273  1.54       sjg 	    errCheck = FALSE;
    274  1.54       sjg 	    break;
    275  1.54       sjg 	case '+':
    276  1.54       sjg 	    doIt = TRUE;
    277  1.54       sjg 	    if (!meta[0])		/* we came here from jobs */
    278  1.54       sjg 		Compat_Init();
    279   1.1       cgd 	    break;
    280   1.1       cgd 	}
    281   1.1       cgd 	cmd++;
    282   1.1       cgd     }
    283   1.5       cgd 
    284   1.5       cgd     while (isspace((unsigned char)*cmd))
    285  1.14  christos 	cmd++;
    286   1.1       cgd 
    287   1.1       cgd     /*
    288   1.1       cgd      * Search for meta characters in the command. If there are no meta
    289   1.1       cgd      * characters, there's no need to execute a shell to execute the
    290   1.1       cgd      * command.
    291   1.5       cgd      */
    292   1.1       cgd     for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
    293   1.1       cgd 	continue;
    294   1.1       cgd     }
    295   1.1       cgd 
    296   1.1       cgd     /*
    297   1.1       cgd      * Print the command before echoing if we're not supposed to be quiet for
    298   1.1       cgd      * this one. We also print the command if -n given.
    299  1.31  sommerfe      */
    300   1.1       cgd     if (!silent || NoExecute(gn)) {
    301   1.1       cgd 	printf ("%s\n", cmd);
    302   1.1       cgd 	fflush(stdout);
    303   1.1       cgd     }
    304   1.1       cgd 
    305   1.1       cgd     /*
    306   1.1       cgd      * If we're not supposed to execute any commands, this is as far as
    307   1.1       cgd      * we go...
    308  1.54       sjg      */
    309   1.1       cgd     if (!doIt && NoExecute(gn)) {
    310   1.1       cgd 	return (0);
    311  1.14  christos     }
    312   1.1       cgd 
    313   1.1       cgd     if (*cp != '\0') {
    314   1.1       cgd 	/*
    315  1.54       sjg 	 * If *cp isn't the null character, we hit a "meta" character and
    316   1.1       cgd 	 * need to pass the command off to the shell.
    317  1.46       sjg 	 */
    318   1.1       cgd 	static const char *shargv[4];
    319  1.46       sjg 
    320  1.46       sjg 	shargv[0] = shellPath;
    321  1.46       sjg 	/*
    322  1.46       sjg 	 * The following work for any of the builtin shell specs.
    323  1.34       sjg 	 */
    324  1.54       sjg 	if (DEBUG(SHELL))
    325  1.34       sjg 		shargv[1] = "-xc";
    326  1.54       sjg 	else
    327   1.1       cgd 		shargv[1] = "-c";
    328   1.1       cgd 	shargv[2] = cmd;
    329   1.1       cgd 	shargv[3] = (char *)NULL;
    330   1.1       cgd 	av = shargv;
    331  1.24  christos 	argc = 0;
    332   1.1       cgd 	bp = NULL;
    333   1.1       cgd     } else {
    334   1.1       cgd 	/*
    335   1.1       cgd 	 * No meta-characters, so no need to exec a shell. Break the command
    336   1.1       cgd 	 * into words to form an argument vector we can execute.
    337  1.45  christos 	 */
    338   1.1       cgd 	av = (const char **)brk_string(cmd, &argc, TRUE, &bp);
    339  1.14  christos     }
    340   1.1       cgd 
    341   1.1       cgd     local = TRUE;
    342   1.1       cgd 
    343   1.1       cgd     /*
    344   1.1       cgd      * Fork and execute the single command. If the fork fails, we abort.
    345   1.1       cgd      */
    346   1.1       cgd     cpid = vfork();
    347   1.1       cgd     if (cpid < 0) {
    348   1.1       cgd 	Fatal("Could not fork");
    349   1.1       cgd     }
    350  1.30       sjg     if (cpid == 0) {
    351  1.33  christos 	Check_Cwd(av);
    352  1.45  christos 	if (local)
    353  1.33  christos 	    (void)execvp(av[0], (char *const *)UNCONST(av));
    354  1.45  christos 	else
    355  1.41        pk 	    (void)execv(av[0], (char *const *)UNCONST(av));
    356  1.23   thorpej 	execError("exec", av[0]);
    357   1.1       cgd 	_exit(1);
    358  1.25     itohy     }
    359  1.25     itohy     if (bp) {
    360  1.25     itohy 	free(av);
    361  1.25     itohy 	free(bp);
    362   1.7       jtc     }
    363  1.14  christos     Lst_Replace (cmdNode, (ClientData) NULL);
    364   1.1       cgd 
    365   1.1       cgd     /*
    366   1.1       cgd      * The child is off and running. Now all we can do is wait...
    367   1.1       cgd      */
    368   1.1       cgd     while (1) {
    369  1.37   reinoud 
    370  1.37   reinoud 	while ((retstat = wait(&reason)) != cpid) {
    371   1.1       cgd 	    if (retstat == -1 && errno != EINTR) {
    372   1.1       cgd 		break;
    373   1.1       cgd 	    }
    374  1.14  christos 	}
    375  1.37   reinoud 
    376   1.1       cgd 	if (retstat > -1) {
    377  1.13  christos 	    if (WIFSTOPPED(reason)) {
    378   1.1       cgd 		status = WSTOPSIG(reason);		/* stopped */
    379  1.13  christos 	    } else if (WIFEXITED(reason)) {
    380   1.1       cgd 		status = WEXITSTATUS(reason);		/* exited */
    381  1.52      jmmv 		if (status != 0) {
    382  1.52      jmmv 		    if (DEBUG(ERROR)) {
    383  1.52      jmmv 		        printf("\n*** Failed target:  %s\n*** Failed command: ",
    384  1.52      jmmv 			    gn->name);
    385  1.52      jmmv 		        for (cp = cmd; *cp; ) {
    386  1.52      jmmv     			    if (isspace((unsigned char)*cp)) {
    387  1.52      jmmv 			        putchar(' ');
    388  1.52      jmmv 			        while (isspace((unsigned char)*cp))
    389  1.52      jmmv 				    cp++;
    390  1.52      jmmv 			    } else {
    391  1.52      jmmv 			        putchar(*cp);
    392  1.52      jmmv 			        cp++;
    393  1.52      jmmv 			    }
    394  1.52      jmmv 		        }
    395  1.51     lukem 			printf ("\n");
    396  1.52      jmmv 		    }
    397   1.1       cgd 		    printf ("*** Error code %d", status);
    398   1.1       cgd 		}
    399  1.13  christos 	    } else {
    400   1.1       cgd 		status = WTERMSIG(reason);		/* signaled */
    401  1.14  christos 		printf ("*** Signal %d", status);
    402  1.14  christos 	    }
    403   1.1       cgd 
    404   1.1       cgd 
    405   1.1       cgd 	    if (!WIFEXITED(reason) || (status != 0)) {
    406   1.1       cgd 		if (errCheck) {
    407   1.1       cgd 		    gn->made = ERROR;
    408   1.1       cgd 		    if (keepgoing) {
    409   1.1       cgd 			/*
    410   1.1       cgd 			 * Abort the current target, but let others
    411   1.1       cgd 			 * continue.
    412   1.1       cgd 			 */
    413   1.1       cgd 			printf (" (continuing)\n");
    414   1.1       cgd 		    }
    415   1.1       cgd 		} else {
    416   1.1       cgd 		    /*
    417   1.1       cgd 		     * Continue executing commands for this target.
    418   1.1       cgd 		     * If we return 0, this will happen...
    419   1.1       cgd 		     */
    420   1.1       cgd 		    printf (" (ignored)\n");
    421   1.1       cgd 		    status = 0;
    422   1.1       cgd 		}
    423   1.1       cgd 	    }
    424   1.1       cgd 	    break;
    425  1.37   reinoud 	} else {
    426   1.1       cgd 	    Fatal ("error in wait: %d: %s", retstat, strerror(errno));
    427   1.1       cgd 	    /*NOTREACHED*/
    428   1.1       cgd 	}
    429  1.50     lukem     }
    430   1.1       cgd     free(cmdStart);
    431   1.1       cgd 
    432   1.1       cgd     return (status);
    433   1.1       cgd }
    434   1.1       cgd 
    435   1.1       cgd /*-
    437   1.1       cgd  *-----------------------------------------------------------------------
    438   1.1       cgd  * CompatMake --
    439  1.44       wiz  *	Make a target.
    440  1.44       wiz  *
    441  1.44       wiz  * Input:
    442  1.44       wiz  *	gnp		The node to make
    443   1.1       cgd  *	pgnp		Parent to abort if necessary
    444   1.1       cgd  *
    445   1.1       cgd  * Results:
    446   1.1       cgd  *	0
    447   1.1       cgd  *
    448   1.1       cgd  * Side Effects:
    449   1.1       cgd  *	If an error is detected and not being ignored, the process exits.
    450   1.1       cgd  *
    451   1.1       cgd  *-----------------------------------------------------------------------
    452  1.44       wiz  */
    453   1.1       cgd static int
    454   1.7       jtc CompatMake(ClientData gnp, ClientData pgnp)
    455   1.7       jtc {
    456  1.16  christos     GNode *gn = (GNode *) gnp;
    457  1.40        pk     GNode *pgn = (GNode *) pgnp;
    458   1.1       cgd 
    459   1.1       cgd     if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
    460   1.1       cgd 	/*
    461   1.1       cgd 	 * First mark ourselves to be made, then apply whatever transformations
    462   1.1       cgd 	 * the suffix module thinks are necessary. Once that's done, we can
    463   1.1       cgd 	 * descend and make all our children. If any of them has an error
    464   1.1       cgd 	 * but the -k flag was given, our 'make' field will be set FALSE again.
    465   1.1       cgd 	 * This is our signal to not attempt to do anything but abort our
    466  1.26  christos 	 * parent as well.
    467   1.1       cgd 	 */
    468  1.38        pk 	gn->flags |= REMAKE;
    469  1.38        pk 	gn->made = BEINGMADE;
    470   1.1       cgd 	if ((gn->type & OP_MADE) == 0)
    471  1.26  christos 	    Suff_FindDeps (gn);
    472   1.1       cgd 	Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
    473  1.26  christos 	if ((gn->flags & REMAKE) == 0) {
    474  1.28   mycroft 	    gn->made = ABORTED;
    475   1.1       cgd 	    pgn->flags &= ~REMAKE;
    476   1.1       cgd 	    goto cohorts;
    477   1.1       cgd 	}
    478   1.7       jtc 
    479  1.35       sjg 	if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
    480   1.7       jtc 	    char *p1;
    481   1.7       jtc 	    Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
    482   1.1       cgd 	    if (p1)
    483  1.14  christos 		free(p1);
    484   1.1       cgd 	}
    485   1.1       cgd 
    486   1.1       cgd 	/*
    487   1.1       cgd 	 * All the children were made ok. Now cmtime contains the modification
    488   1.1       cgd 	 * time of the newest child, we need to find out if we exist and when
    489   1.1       cgd 	 * we were modified last. The criteria for datedness are defined by the
    490   1.1       cgd 	 * Make_OODate function.
    491   1.1       cgd 	 */
    492   1.1       cgd 	if (DEBUG(MAKE)) {
    493   1.1       cgd 	    printf("Examining %s...", gn->name);
    494   1.1       cgd 	}
    495   1.1       cgd 	if (! Make_OODate(gn)) {
    496   1.1       cgd 	    gn->made = UPTODATE;
    497   1.1       cgd 	    if (DEBUG(MAKE)) {
    498  1.28   mycroft 		printf("up-to-date.\n");
    499   1.1       cgd 	    }
    500   1.1       cgd 	    goto cohorts;
    501   1.1       cgd 	} else if (DEBUG(MAKE)) {
    502   1.1       cgd 	    printf("out-of-date.\n");
    503   1.1       cgd 	}
    504   1.1       cgd 
    505   1.1       cgd 	/*
    506   1.1       cgd 	 * If the user is just seeing if something is out-of-date, exit now
    507   1.1       cgd 	 * to tell him/her "yes".
    508  1.32       wiz 	 */
    509   1.1       cgd 	if (queryFlag) {
    510   1.1       cgd 	    exit (1);
    511   1.1       cgd 	}
    512   1.1       cgd 
    513   1.1       cgd 	/*
    514   1.1       cgd 	 * We need to be re-made. We also have to make sure we've got a $?
    515   1.1       cgd 	 * variable. To be nice, we also define the $> variable using
    516   1.1       cgd 	 * Make_DoAllVar().
    517  1.14  christos 	 */
    518   1.1       cgd 	Make_DoAllVar(gn);
    519   1.1       cgd 
    520   1.1       cgd 	/*
    521   1.1       cgd 	 * Alter our type to tell if errors should be ignored or things
    522   1.1       cgd 	 * should not be printed so CompatRunCommand knows what to do.
    523   1.1       cgd 	 */
    524   1.1       cgd 	if (Targ_Ignore (gn)) {
    525   1.1       cgd 	    gn->type |= OP_IGNORE;
    526   1.1       cgd 	}
    527   1.1       cgd 	if (Targ_Silent (gn)) {
    528   1.1       cgd 	    gn->type |= OP_SILENT;
    529   1.1       cgd 	}
    530   1.1       cgd 
    531   1.1       cgd 	if (Job_CheckCommands (gn, Fatal)) {
    532   1.1       cgd 	    /*
    533   1.1       cgd 	     * Our commands are ok, but we still have to worry about the -t
    534  1.20   mycroft 	     * flag...
    535   1.1       cgd 	     */
    536   1.1       cgd 	    if (!touchFlag || (gn->type & OP_MAKE)) {
    537   1.1       cgd 		curTarg = gn;
    538   1.1       cgd 		Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
    539   1.1       cgd 		curTarg = NILGNODE;
    540   1.1       cgd 	    } else {
    541   1.1       cgd 		Job_Touch (gn, gn->type & OP_SILENT);
    542   1.1       cgd 	    }
    543   1.1       cgd 	} else {
    544   1.1       cgd 	    gn->made = ERROR;
    545   1.1       cgd 	}
    546   1.1       cgd 
    547   1.1       cgd 	if (gn->made != ERROR) {
    548   1.1       cgd 	    /*
    549   1.1       cgd 	     * If the node was made successfully, mark it so, update
    550   1.1       cgd 	     * its modification time and timestamp all its parents. Note
    551   1.1       cgd 	     * that for .ZEROTIME targets, the timestamping isn't done.
    552   1.1       cgd 	     * This is to keep its state from affecting that of its parent.
    553  1.26  christos 	     */
    554   1.1       cgd 	    gn->made = MADE;
    555  1.26  christos 	    pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
    556   1.1       cgd 	    if (!(gn->type & OP_EXEC)) {
    557   1.1       cgd 		pgn->flags |= CHILDMADE;
    558   1.1       cgd 		Make_TimeStamp(pgn, gn);
    559  1.26  christos 	    }
    560   1.1       cgd 	} else if (keepgoing) {
    561  1.34       sjg 	    pgn->flags &= ~REMAKE;
    562   1.1       cgd 	} else {
    563   1.1       cgd 	    PrintOnError("\n\nStop.");
    564   1.1       cgd 	    exit (1);
    565   1.1       cgd 	}
    566   1.1       cgd     } else if (gn->made == ERROR) {
    567   1.1       cgd 	/*
    568   1.1       cgd 	 * Already had an error when making this beastie. Tell the parent
    569  1.26  christos 	 * to abort.
    570   1.1       cgd 	 */
    571   1.1       cgd 	pgn->flags &= ~REMAKE;
    572   1.7       jtc     } else {
    573  1.35       sjg 	if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
    574   1.7       jtc 	    char *p1;
    575   1.7       jtc 	    Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
    576   1.1       cgd 	    if (p1)
    577   1.1       cgd 		free(p1);
    578   1.1       cgd 	}
    579  1.42  christos 	switch(gn->made) {
    580   1.1       cgd 	    case BEINGMADE:
    581  1.26  christos 		Error("Graph cycles through %s", gn->name);
    582   1.1       cgd 		gn->made = ERROR;
    583   1.1       cgd 		pgn->flags &= ~REMAKE;
    584   1.1       cgd 		break;
    585  1.26  christos 	    case MADE:
    586   1.1       cgd 		if ((gn->type & OP_EXEC) == 0) {
    587   1.1       cgd 		    pgn->flags |= CHILDMADE;
    588   1.1       cgd 		    Make_TimeStamp(pgn, gn);
    589   1.1       cgd 		}
    590   1.1       cgd 		break;
    591   1.1       cgd 	    case UPTODATE:
    592   1.1       cgd 		if ((gn->type & OP_EXEC) == 0) {
    593   1.1       cgd 		    Make_TimeStamp(pgn, gn);
    594   1.5       cgd 		}
    595   1.5       cgd 		break;
    596   1.1       cgd 	    default:
    597   1.1       cgd 		break;
    598   1.1       cgd 	}
    599  1.28   mycroft     }
    600  1.28   mycroft 
    601   1.1       cgd cohorts:
    602   1.1       cgd     Lst_ForEach (gn->cohorts, CompatMake, pgnp);
    603  1.14  christos     return (0);
    604   1.1       cgd }
    605   1.1       cgd 
    606   1.1       cgd /*-
    608   1.1       cgd  *-----------------------------------------------------------------------
    609  1.44       wiz  * Compat_Run --
    610  1.44       wiz  *	Initialize this mode and start making.
    611  1.44       wiz  *
    612   1.1       cgd  * Input:
    613   1.1       cgd  *	targs		List of target nodes to re-create
    614   1.1       cgd  *
    615   1.1       cgd  * Results:
    616   1.1       cgd  *	None.
    617   1.1       cgd  *
    618   1.1       cgd  * Side Effects:
    619   1.1       cgd  *	Guess what?
    620   1.1       cgd  *
    621  1.44       wiz  *-----------------------------------------------------------------------
    622   1.1       cgd  */
    623   1.5       cgd void
    624   1.1       cgd Compat_Run(Lst targs)
    625  1.46       sjg {
    626  1.54       sjg     GNode   	  *gn = NULL;/* Current root target */
    627   1.1       cgd     int	    	  errors;   /* Number of targets not remade due to errors */
    628   1.1       cgd 
    629   1.1       cgd     Compat_Init();
    630   1.1       cgd 
    631   1.1       cgd     if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
    632   1.1       cgd 	signal(SIGINT, CompatInterrupt);
    633   1.1       cgd     }
    634   1.1       cgd     if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
    635   1.1       cgd 	signal(SIGTERM, CompatInterrupt);
    636   1.1       cgd     }
    637   1.1       cgd     if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
    638   1.1       cgd 	signal(SIGHUP, CompatInterrupt);
    639   1.1       cgd     }
    640   1.1       cgd     if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
    641   1.1       cgd 	signal(SIGQUIT, CompatInterrupt);
    642   1.1       cgd     }
    643   1.1       cgd 
    644   1.1       cgd     ENDNode = Targ_FindNode(".END", TARG_CREATE);
    645   1.1       cgd     /*
    646   1.1       cgd      * If the user has defined a .BEGIN target, execute the commands attached
    647   1.1       cgd      * to it.
    648   1.1       cgd      */
    649   1.1       cgd     if (!queryFlag) {
    650  1.14  christos 	gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
    651  1.34       sjg 	if (gn != NILGNODE) {
    652  1.14  christos 	    Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
    653  1.14  christos             if (gn->made == ERROR) {
    654   1.1       cgd                 PrintOnError("\n\nStop.");
    655   1.1       cgd                 exit(1);
    656  1.15  christos             }
    657  1.15  christos 	}
    658  1.15  christos     }
    659  1.15  christos 
    660  1.15  christos     /*
    661  1.15  christos      * Expand .USE nodes right now, because they can modify the structure
    662   1.1       cgd      * of the tree.
    663   1.1       cgd      */
    664   1.1       cgd     Lst_Destroy(Make_ExpandUse(targs), NOFREE);
    665   1.1       cgd 
    666   1.1       cgd     /*
    667   1.1       cgd      * For each entry in the list of targets to create, call CompatMake on
    668   1.1       cgd      * it to create the thing. CompatMake will leave the 'made' field of gn
    669   1.1       cgd      * in one of several states:
    670   1.1       cgd      *	    UPTODATE	    gn was already up-to-date
    671   1.1       cgd      *	    MADE  	    gn was recreated successfully
    672   1.1       cgd      *	    ERROR 	    An error occurred while gn was being created
    673   1.1       cgd      *	    ABORTED	    gn was not remade because one of its inferiors
    674   1.1       cgd      *	    	  	    could not be made due to errors.
    675   1.1       cgd      */
    676   1.1       cgd     errors = 0;
    677   1.1       cgd     while (!Lst_IsEmpty (targs)) {
    678   1.1       cgd 	gn = (GNode *) Lst_DeQueue (targs);
    679   1.1       cgd 	CompatMake (gn, gn);
    680   1.1       cgd 
    681   1.1       cgd 	if (gn->made == UPTODATE) {
    682   1.1       cgd 	    printf ("`%s' is up to date.\n", gn->name);
    683   1.1       cgd 	} else if (gn->made == ABORTED) {
    684   1.1       cgd 	    printf ("`%s' not remade because of errors.\n", gn->name);
    685   1.1       cgd 	    errors += 1;
    686   1.1       cgd 	}
    687   1.1       cgd     }
    688   1.1       cgd 
    689   1.1       cgd     /*
    690   1.1       cgd      * If the user has defined a .END target, run its commands.
    691  1.36       sjg      */
    692  1.36       sjg     if (errors == 0) {
    693  1.36       sjg 	Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);
    694  1.36       sjg 	if (gn->made == ERROR) {
    695   1.1       cgd 	    PrintOnError("\n\nStop.");
    696   1.1       cgd 	    exit(1);
    697                 	}
    698                     }
    699                 }
    700