[BACK]Return to engine.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/engine.c, Revision 1.13

1.10      espie       1: /*     $OpenBSD$ */
1.1       espie       2: /*
                      3:  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
                      4:  * Copyright (c) 1988, 1989 by Adam de Boor
                      5:  * Copyright (c) 1989 by Berkeley Softworks
                      6:  * All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Adam de Boor.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
1.10      espie      36: #include <sys/types.h>
                     37: #include <sys/wait.h>
1.1       espie      38: #include <limits.h>
                     39: #include <stdio.h>
1.10      espie      40: #include <stdlib.h>
                     41: #include <ctype.h>
1.1       espie      42: #include <errno.h>
                     43: #include <fcntl.h>
                     44: #include <unistd.h>
                     45: #include <string.h>
1.10      espie      46: #include <signal.h>
1.1       espie      47: #include "config.h"
                     48: #include "defines.h"
                     49: #include "dir.h"
                     50: #include "engine.h"
                     51: #include "arch.h"
                     52: #include "gnode.h"
                     53: #include "targ.h"
                     54: #include "var.h"
                     55: #include "extern.h"
                     56: #include "lst.h"
                     57: #include "timestamp.h"
                     58: #include "make.h"
1.10      espie      59: #include "pathnames.h"
                     60: #include "error.h"
                     61: #include "str.h"
                     62: #include "memory.h"
1.1       espie      63:
                     64: static void MakeTimeStamp(void *, void *);
                     65: static void MakeAddAllSrc(void *, void *);
1.6       espie      66: static int rewrite_time(const char *);
1.10      espie      67: static void setup_signal(int);
                     68: static void setup_all_signals(void);
                     69: static void setup_meta(void);
                     70: static char **recheck_command_for_shell(char **);
                     71:
                     72: static int setup_and_run_command(char *, GNode *, int);
                     73: static void run_command(const char *, bool);
                     74: static void handle_compat_interrupts(GNode *);
1.1       espie      75:
                     76: bool
1.3       espie      77: Job_CheckCommands(GNode *gn, void (*abortProc)(char *, ...))
1.1       espie      78: {
1.11      espie      79:        /* Alter our type to tell if errors should be ignored or things
                     80:         * should not be printed so CompatRunCommand knows what to do.
                     81:         */
                     82:        if (Targ_Ignore(gn))
                     83:                gn->type |= OP_IGNORE;
                     84:        if (Targ_Silent(gn))
                     85:                gn->type |= OP_SILENT;
                     86:
1.3       espie      87:        if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) &&
1.9       espie      88:            (gn->type & (OP_NODEFAULT | OP_LIB)) == 0) {
1.3       espie      89:                /*
                     90:                 * No commands. Look for .DEFAULT rule from which we might infer
                     91:                 * commands
                     92:                 */
1.9       espie      93:                if ((DEFAULT->type & OP_DUMMY) == 0 &&
                     94:                    !Lst_IsEmpty(&DEFAULT->commands)) {
1.3       espie      95:                        /*
                     96:                         * Make only looks for a .DEFAULT if the node was never
                     97:                         * the target of an operator, so that's what we do too.
                     98:                         * If a .DEFAULT was given, we substitute its commands
                     99:                         * for gn's commands and set the IMPSRC variable to be
                    100:                         * the target's name The DEFAULT node acts like a
                    101:                         * transformation rule, in that gn also inherits any
                    102:                         * attributes or sources attached to .DEFAULT itself.
                    103:                         */
                    104:                        Make_HandleUse(DEFAULT, gn);
                    105:                        Varq_Set(IMPSRC_INDEX, Varq_Value(TARGET_INDEX, gn), gn);
                    106:                } else if (is_out_of_date(Dir_MTime(gn))) {
                    107:                        /*
                    108:                         * The node wasn't the target of an operator we have no
                    109:                         * .DEFAULT rule to go on and the target doesn't
                    110:                         * already exist. There's nothing more we can do for
                    111:                         * this branch. If the -k flag wasn't given, we stop in
                    112:                         * our tracks, otherwise we just don't update this
                    113:                         * node's parents so they never get examined.
                    114:                         */
1.5       espie     115:                        static const char msg[] =
1.3       espie     116:                            "make: don't know how to make";
                    117:
                    118:                        if (gn->type & OP_OPTIONAL) {
1.5       espie     119:                                (void)fprintf(stdout, "%s %s(ignored)\n", msg,
1.3       espie     120:                                    gn->name);
                    121:                                (void)fflush(stdout);
                    122:                        } else if (keepgoing) {
1.5       espie     123:                                (void)fprintf(stdout, "%s %s(continuing)\n",
1.3       espie     124:                                    msg, gn->name);
                    125:                                (void)fflush(stdout);
                    126:                                return false;
                    127:                        } else {
1.5       espie     128:                                (*abortProc)("%s %s. Stop in %s.", msg,
1.3       espie     129:                                    gn->name, Var_Value(".CURDIR"));
                    130:                                return false;
                    131:                        }
                    132:                }
1.1       espie     133:        }
1.3       espie     134:        return true;
1.1       espie     135: }
                    136:
1.6       espie     137: /* touch files the hard way, by writing stuff to them */
                    138: static int
                    139: rewrite_time(const char *name)
                    140: {
                    141:        int fd;
                    142:        char c;
                    143:
                    144:        fd = open(name, O_RDWR | O_CREAT, 0666);
                    145:        if (fd < 0)
                    146:                return -1;
                    147:        /*
                    148:         * Read and write a byte to the file to change
                    149:         * the modification time.
                    150:         */
                    151:        if (read(fd, &c, 1) == 1) {
                    152:                (void)lseek(fd, 0, SEEK_SET);
                    153:                (void)write(fd, &c, 1);
                    154:        }
                    155:
                    156:        (void)close(fd);
                    157:        return 0;
                    158: }
                    159:
1.1       espie     160: void
1.12      espie     161: Job_Touch(GNode *gn)
1.1       espie     162: {
1.3       espie     163:        if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL)) {
                    164:                /*
1.9       espie     165:                 * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets
                    166:                 * and, as such, shouldn't really be created.
1.3       espie     167:                 */
                    168:                return;
                    169:        }
1.1       espie     170:
1.12      espie     171:        if (!(gn->type & OP_SILENT)) {
1.3       espie     172:                (void)fprintf(stdout, "touch %s\n", gn->name);
                    173:                (void)fflush(stdout);
                    174:        }
1.1       espie     175:
1.3       espie     176:        if (noExecute) {
                    177:                return;
                    178:        }
1.1       espie     179:
1.3       espie     180:        if (gn->type & OP_ARCHV) {
                    181:                Arch_Touch(gn);
                    182:        } else if (gn->type & OP_LIB) {
                    183:                Arch_TouchLib(gn);
                    184:        } else {
                    185:                const char *file = gn->path != NULL ? gn->path : gn->name;
                    186:
                    187:                if (set_times(file) == -1){
1.6       espie     188:                        if (rewrite_time(file) == -1) {
1.5       espie     189:                                (void)fprintf(stdout,
                    190:                                    "*** couldn't touch %s: %s", file,
1.3       espie     191:                                    strerror(errno));
                    192:                                (void)fflush(stdout);
1.9       espie     193:                        }
1.1       espie     194:                }
                    195:        }
                    196: }
                    197:
                    198: void
1.7       espie     199: Make_TimeStamp(GNode *parent, GNode *child)
1.1       espie     200: {
1.8       espie     201:        if (is_strictly_before(parent->cmtime, child->mtime))
                    202:                parent->cmtime = child->mtime;
1.1       espie     203: }
                    204:
                    205: void
1.9       espie     206: Make_HandleUse(GNode   *cgn,   /* The .USE node */
1.1       espie     207:     GNode      *pgn)   /* The target of the .USE node */
                    208: {
1.3       espie     209:        GNode   *gn;    /* A child of the .USE node */
                    210:        LstNode ln;     /* An element in the children list */
1.1       espie     211:
1.3       espie     212:        if (cgn->type & (OP_USE|OP_TRANSFORM)) {
                    213:                if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
1.9       espie     214:                        /* .USE or transformation and target has no commands
                    215:                         * -- append the child's commands to the parent.  */
1.3       espie     216:                        Lst_Concat(&pgn->commands, &cgn->commands);
                    217:                }
1.1       espie     218:
1.5       espie     219:                for (ln = Lst_First(&cgn->children); ln != NULL;
1.3       espie     220:                    ln = Lst_Adv(ln)) {
                    221:                        gn = (GNode *)Lst_Datum(ln);
                    222:
                    223:                        if (Lst_AddNew(&pgn->children, gn)) {
                    224:                                Lst_AtEnd(&gn->parents, pgn);
1.4       espie     225:                                pgn->unmade++;
1.3       espie     226:                        }
                    227:                }
1.1       espie     228:
1.3       espie     229:                pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
1.1       espie     230:
1.3       espie     231:                /*
                    232:                 * This child node is now "made", so we decrement the count of
                    233:                 * unmade children in the parent... We also remove the child
                    234:                 * from the parent's list to accurately reflect the number of
                    235:                 * decent children the parent has. This is used by Make_Run to
1.9       espie     236:                 * decide whether to queue the parent or examine its children...
1.3       espie     237:                 */
1.9       espie     238:                if (cgn->type & OP_USE)
1.3       espie     239:                        pgn->unmade--;
1.1       espie     240:        }
                    241: }
                    242:
                    243: static void
1.9       espie     244: MakeAddAllSrc(void *cgnp, void *pgnp)
1.1       espie     245: {
1.9       espie     246:        GNode   *child = (GNode *)cgnp;
                    247:        GNode   *parent = (GNode *)pgnp;
                    248:        if ((child->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) == 0) {
                    249:                const char *target;
1.3       espie     250:
1.9       espie     251:                if (OP_NOP(child->type) ||
                    252:                    (target = Varq_Value(TARGET_INDEX, child)) == NULL) {
1.3       espie     253:                        /*
                    254:                         * this node is only source; use the specific pathname
                    255:                         * for it
                    256:                         */
1.9       espie     257:                        target = child->path != NULL ? child->path :
                    258:                            child->name;
1.3       espie     259:                }
1.1       espie     260:
1.9       espie     261:                Varq_Append(ALLSRC_INDEX, target, parent);
                    262:                if (parent->type & OP_JOIN) {
                    263:                        if (child->made == MADE)
                    264:                                Varq_Append(OODATE_INDEX, target, parent);
                    265:                } else if (is_strictly_before(parent->mtime, child->mtime) ||
                    266:                   (!is_strictly_before(child->mtime, now) &&
                    267:                   child->made == MADE)) {
1.3       espie     268:                        /*
                    269:                         * It goes in the OODATE variable if the parent is
                    270:                         * younger than the child or if the child has been
                    271:                         * modified more recently than the start of the make.
1.9       espie     272:                         * This is to keep make from getting confused if
                    273:                         * something else updates the parent after the
                    274:                         * make starts (shouldn't happen, I know, but sometimes
                    275:                         * it does). In such a case, if we've updated the kid,
                    276:                         * the parent is likely to have a modification time
                    277:                         * later than that of the kid and anything that relies
                    278:                         * on the OODATE variable will be hosed.
1.3       espie     279:                         */
1.9       espie     280:                        Varq_Append(OODATE_INDEX, target, parent);
1.3       espie     281:                }
1.1       espie     282:        }
                    283: }
                    284:
                    285: void
                    286: Make_DoAllVar(GNode *gn)
                    287: {
1.3       espie     288:        Lst_ForEach(&gn->children, MakeAddAllSrc, gn);
1.1       espie     289:
1.13    ! espie     290:        if (gn->impliedsrc)
        !           291:                Varq_Set(IMPSRC_INDEX,
        !           292:                    Varq_Value(TARGET_INDEX, gn->impliedsrc), gn);
        !           293:
1.3       espie     294:        if (Varq_Value(OODATE_INDEX, gn) == NULL)
                    295:                Varq_Set(OODATE_INDEX, "", gn);
                    296:        if (Varq_Value(ALLSRC_INDEX, gn) == NULL)
                    297:                Varq_Set(ALLSRC_INDEX, "", gn);
1.1       espie     298:
1.3       espie     299:        if (gn->type & OP_JOIN)
                    300:                Varq_Set(TARGET_INDEX, Varq_Value(ALLSRC_INDEX, gn), gn);
1.1       espie     301: }
                    302:
                    303: /* Wrapper to call Make_TimeStamp from a forEach loop. */
                    304: static void
1.9       espie     305: MakeTimeStamp(void *parent, void *child)
1.1       espie     306: {
1.9       espie     307:     Make_TimeStamp((GNode *)parent, (GNode *)child);
1.1       espie     308: }
                    309:
                    310: bool
1.9       espie     311: Make_OODate(GNode *gn)
1.1       espie     312: {
1.9       espie     313:        bool        oodate;
1.1       espie     314:
                    315:        /*
1.3       espie     316:         * Certain types of targets needn't even be sought as their datedness
                    317:         * doesn't depend on their modification time...
1.1       espie     318:         */
1.3       espie     319:        if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC)) == 0) {
                    320:                (void)Dir_MTime(gn);
                    321:                if (DEBUG(MAKE)) {
1.9       espie     322:                        if (!is_out_of_date(gn->mtime))
1.5       espie     323:                                printf("modified %s...",
1.3       espie     324:                                    time_to_string(gn->mtime));
1.9       espie     325:                        else
1.3       espie     326:                                printf("non-existent...");
                    327:                }
1.1       espie     328:        }
                    329:
                    330:        /*
1.3       espie     331:         * A target is remade in one of the following circumstances:
1.9       espie     332:         * - its modification time is smaller than that of its youngest child
                    333:         *   and it would actually be run (has commands or type OP_NOP)
                    334:         * - it's the object of a force operator
                    335:         * - it has no children, was on the lhs of an operator and doesn't
                    336:         *   exist already.
1.3       espie     337:         *
                    338:         * Libraries are only considered out-of-date if the archive module says
                    339:         * they are.
1.1       espie     340:         */
1.3       espie     341:        if (gn->type & OP_USE) {
                    342:                /*
                    343:                 * If the node is a USE node it is *never* out of date
                    344:                 * no matter *what*.
                    345:                 */
1.9       espie     346:                if (DEBUG(MAKE))
1.3       espie     347:                        printf(".USE node...");
                    348:                oodate = false;
                    349:        } else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
1.9       espie     350:                if (DEBUG(MAKE))
                    351:                    printf("library...");
1.3       espie     352:
1.9       espie     353:                /* always out of date if no children and :: target */
1.3       espie     354:                oodate = Arch_LibOODate(gn) ||
                    355:                    (is_out_of_date(gn->cmtime) && (gn->type & OP_DOUBLEDEP));
                    356:        } else if (gn->type & OP_JOIN) {
                    357:                /*
                    358:                 * A target with the .JOIN attribute is only considered
                    359:                 * out-of-date if any of its children was out-of-date.
                    360:                 */
1.9       espie     361:                if (DEBUG(MAKE))
1.3       espie     362:                        printf(".JOIN node...");
                    363:                oodate = gn->childMade;
                    364:        } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
                    365:                /*
1.9       espie     366:                 * A node which is the object of the force (!) operator or which
                    367:                 * has the .EXEC attribute is always considered out-of-date.
1.3       espie     368:                 */
                    369:                if (DEBUG(MAKE)) {
1.9       espie     370:                        if (gn->type & OP_FORCE)
1.3       espie     371:                                printf("! operator...");
1.9       espie     372:                        else if (gn->type & OP_PHONY)
1.3       espie     373:                                printf(".PHONY node...");
1.9       espie     374:                        else
1.3       espie     375:                                printf(".EXEC node...");
                    376:                }
                    377:                oodate = true;
                    378:        } else if (is_strictly_before(gn->mtime, gn->cmtime) ||
1.9       espie     379:           (is_out_of_date(gn->cmtime) &&
1.3       espie     380:            (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
                    381:                /*
                    382:                 * A node whose modification time is less than that of its
                    383:                 * youngest child or that has no children (cmtime ==
                    384:                 * OUT_OF_DATE) and either doesn't exist (mtime == OUT_OF_DATE)
1.9       espie     385:                 * or was the object of a :: operator is out-of-date.
1.3       espie     386:                 */
                    387:                if (DEBUG(MAKE)) {
1.9       espie     388:                        if (is_strictly_before(gn->mtime, gn->cmtime))
1.3       espie     389:                                printf("modified before source...");
1.9       espie     390:                        else if (is_out_of_date(gn->mtime))
1.3       espie     391:                                printf("non-existent and no sources...");
1.9       espie     392:                        else
1.3       espie     393:                                printf(":: operator and no sources...");
                    394:                }
                    395:                oodate = true;
                    396:        } else {
                    397:                oodate = false;
                    398:        }
1.1       espie     399:
1.3       espie     400:        /*
                    401:         * If the target isn't out-of-date, the parents need to know its
                    402:         * modification time. Note that targets that appear to be out-of-date
                    403:         * but aren't, because they have no commands and aren't of type OP_NOP,
                    404:         * have their mtime stay below their children's mtime to keep parents
                    405:         * from thinking they're out-of-date.
                    406:         */
                    407:        if (!oodate)
                    408:                Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
1.1       espie     409:
1.3       espie     410:        return oodate;
1.1       espie     411: }
                    412:
1.10      espie     413: volatile sig_atomic_t got_signal;
                    414:
                    415: volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT,
                    416:     got_SIGTERM, got_SIGTSTP, got_SIGTTOU, got_SIGTTIN, got_SIGWINCH;
                    417:
                    418: static void
                    419: setup_signal(int sig)
                    420: {
                    421:        if (signal(sig, SIG_IGN) != SIG_IGN) {
                    422:                (void)signal(sig, SigHandler);
                    423:        }
                    424: }
                    425:
                    426: void
                    427: setup_all_signals()
                    428: {
                    429:        /*
                    430:         * Catch the four signals that POSIX specifies if they aren't ignored.
                    431:         * handle_signal will take care of calling JobInterrupt if appropriate.
                    432:         */
                    433:        setup_signal(SIGINT);
                    434:        setup_signal(SIGHUP);
                    435:        setup_signal(SIGQUIT);
                    436:        setup_signal(SIGTERM);
                    437:        /*
                    438:         * There are additional signals that need to be caught and passed if
                    439:         * either the export system wants to be told directly of signals or if
                    440:         * we're giving each job its own process group (since then it won't get
                    441:         * signals from the terminal driver as we own the terminal)
                    442:         */
                    443: #if defined(USE_PGRP)
                    444:        setup_signal(SIGTSTP);
                    445:        setup_signal(SIGTTOU);
                    446:        setup_signal(SIGTTIN);
                    447:        setup_signal(SIGWINCH);
                    448: #endif
                    449: }
                    450:
                    451: void
                    452: SigHandler(int sig)
                    453: {
                    454:        switch(sig) {
                    455:        case SIGINT:
                    456:                got_SIGINT++;
                    457:                got_signal = 1;
                    458:                break;
                    459:        case SIGHUP:
                    460:                got_SIGHUP++;
                    461:                got_signal = 1;
                    462:                break;
                    463:        case SIGQUIT:
                    464:                got_SIGQUIT++;
                    465:                got_signal = 1;
                    466:                break;
                    467:        case SIGTERM:
                    468:                got_SIGTERM++;
                    469:                got_signal = 1;
                    470:                break;
                    471: #ifdef USE_PGRP
                    472:        case SIGTSTP:
                    473:                got_SIGTSTP++;
                    474:                got_signal = 1;
                    475:                break;
                    476:        case SIGTTOU:
                    477:                got_SIGTTOU++;
                    478:                got_signal = 1;
                    479:                break;
                    480:        case SIGTTIN:
                    481:                got_SIGTTIN++;
                    482:                got_signal = 1;
                    483:                break;
                    484:        case SIGWINCH:
                    485:                got_SIGWINCH++;
                    486:                got_signal = 1;
                    487:                break;
                    488: #endif
                    489:        }
                    490: }
                    491:
                    492: /* The following array is used to make a fast determination of which
                    493:  * characters are interpreted specially by the shell.  If a command
                    494:  * contains any of these characters, it is executed by the shell, not
                    495:  * directly by us.  */
                    496: static char        meta[256];
                    497:
                    498: void
                    499: setup_meta(void)
                    500: {
                    501:        char *p;
                    502:
                    503:        for (p = "#=|^(){};&<>*?[]:$`\\\n"; *p != '\0'; p++)
                    504:                meta[(unsigned char) *p] = 1;
                    505:        /* The null character serves as a sentinel in the string.  */
                    506:        meta[0] = 1;
                    507: }
                    508:
                    509: static char **
                    510: recheck_command_for_shell(char **av)
                    511: {
                    512:        char *runsh[] = {
                    513:                "alias", "cd", "eval", "exit", "read", "set", "ulimit",
                    514:                "unalias", "unset", "wait", "umask", NULL
                    515:        };
                    516:
                    517:        char **p;
                    518:
                    519:        /* optimization: if exec cmd, we avoid the intermediate shell */
                    520:        if (strcmp(av[0], "exec") == 0)
                    521:                av++;
                    522:
                    523:        for (p = runsh; *p; p++)
                    524:                if (strcmp(av[0], *p) == 0)
                    525:                        return NULL;
                    526:
                    527:        return av;
                    528: }
                    529:
                    530: static void
                    531: run_command(const char *cmd, bool errCheck)
                    532: {
                    533:        const char *p;
                    534:        char *shargv[4];
                    535:        char **todo;
                    536:
                    537:        shargv[0] = _PATH_BSHELL;
                    538:
                    539:        shargv[1] = errCheck ? "-ec" : "-c";
                    540:        shargv[2] = (char *)cmd;
                    541:        shargv[3] = NULL;
                    542:
                    543:        todo = shargv;
                    544:
                    545:
                    546:        /* Search for meta characters in the command. If there are no meta
                    547:         * characters, there's no need to execute a shell to execute the
                    548:         * command.  */
                    549:        for (p = cmd; !meta[(unsigned char)*p]; p++)
                    550:                continue;
                    551:        if (*p == '\0') {
                    552:                char *bp;
                    553:                char **av;
                    554:                int argc;
                    555:                /* No meta-characters, so probably no need to exec a shell.
                    556:                 * Break the command into words to form an argument vector
                    557:                 * we can execute.  */
                    558:                av = brk_string(cmd, &argc, &bp);
                    559:                av = recheck_command_for_shell(av);
                    560:                if (av != NULL)
                    561:                        todo = av;
                    562:        }
                    563:        execvp(todo[0], todo);
                    564:
                    565:        if (errno == ENOENT)
                    566:                fprintf(stderr, "%s: not found\n", todo[0]);
                    567:        else
                    568:                perror(todo[0]);
                    569:        _exit(1);
                    570: }
                    571:
                    572: /*-
                    573:  *-----------------------------------------------------------------------
                    574:  * setup_and_run_command --
                    575:  *     Execute the next command for a target. If the command returns an
                    576:  *     error, the node's made field is set to ERROR and creation stops.
                    577:  *
                    578:  * Results:
                    579:  *     0 in case of error, 1 if ok.
                    580:  *
                    581:  * Side Effects:
                    582:  *     The node's 'made' field may be set to ERROR.
                    583:  *-----------------------------------------------------------------------
                    584:  */
                    585: static int
                    586: setup_and_run_command(char *cmd, GNode *gn, int dont_fork)
                    587: {
                    588:        char *cmdStart; /* Start of expanded command */
                    589:        bool silent;    /* Don't print command */
                    590:        bool doExecute; /* Execute the command */
                    591:        bool errCheck;  /* Check errors */
                    592:        int reason;     /* Reason for child's death */
                    593:        int status;     /* Description of child's death */
                    594:        pid_t cpid;     /* Child actually found */
                    595:        pid_t stat;     /* Status of fork */
                    596:
                    597:        silent = gn->type & OP_SILENT;
                    598:        errCheck = !(gn->type & OP_IGNORE);
                    599:        doExecute = !noExecute;
                    600:
                    601:        cmdStart = Var_Subst(cmd, &gn->context, false);
                    602:
                    603:        /* How can we execute a null command ? we warn the user that the
                    604:         * command expanded to nothing (is this the right thing to do?).  */
                    605:        if (*cmdStart == '\0') {
                    606:                free(cmdStart);
                    607:                Error("%s expands to empty string", cmd);
                    608:                return 1;
                    609:        } else
                    610:                cmd = cmdStart;
                    611:
                    612:        for (;; cmd++) {
                    613:                if (*cmd == '@')
                    614:                        silent = DEBUG(LOUD) ? false : true;
                    615:                else if (*cmd == '-')
                    616:                        errCheck = false;
                    617:                else if (*cmd == '+')
                    618:                        doExecute = true;
                    619:                else
                    620:                        break;
                    621:        }
                    622:        while (isspace(*cmd))
                    623:                cmd++;
                    624:        /* Print the command before echoing if we're not supposed to be quiet
                    625:         * for this one. We also print the command if -n given.  */
                    626:        if (!silent || noExecute) {
                    627:                printf("%s\n", cmd);
                    628:                fflush(stdout);
                    629:        }
                    630:        /* If we're not supposed to execute any commands, this is as far as
                    631:         * we go...  */
                    632:        if (!doExecute)
                    633:                return 1;
                    634:
                    635:        /* if we're running in parallel mode, we try not to fork the last
                    636:         * command, since it's exit status will be just fine... unless
                    637:         * errCheck is not set, in which case we must deal with the
                    638:         * status ourselves.
                    639:         */
                    640:        if (dont_fork && errCheck)
                    641:                run_command(cmd, errCheck);
                    642:                /*NOTREACHED*/
                    643:
                    644:        /* Fork and execute the single command. If the fork fails, we abort.  */
                    645:        switch (cpid = fork()) {
                    646:        case -1:
                    647:                Fatal("Could not fork");
                    648:                /*NOTREACHED*/
                    649:        case 0:
                    650:                run_command(cmd, errCheck);
                    651:                /*NOTREACHED*/
                    652:        default:
                    653:                break;
                    654:        }
                    655:        free(cmdStart);
                    656:
                    657:        /* The child is off and running. Now all we can do is wait...  */
                    658:        while (1) {
                    659:
                    660:                while ((stat = wait(&reason)) != cpid) {
                    661:                        if (stat == -1 && errno != EINTR)
                    662:                                break;
                    663:                }
                    664:
                    665:                if (got_signal)
                    666:                        break;
                    667:
                    668:                if (stat != -1) {
                    669:                        if (WIFSTOPPED(reason))
                    670:                                status = WSTOPSIG(reason);      /* stopped */
                    671:                        else if (WIFEXITED(reason)) {
                    672:                                status = WEXITSTATUS(reason);   /* exited */
                    673:                                if (status != 0)
                    674:                                    printf("*** Error code %d", status);
                    675:                        } else {
                    676:                                status = WTERMSIG(reason);      /* signaled */
                    677:                                printf("*** Signal %d", status);
                    678:                        }
                    679:
                    680:
                    681:                        if (!WIFEXITED(reason) || status != 0) {
                    682:                                if (errCheck) {
                    683:                                        gn->made = ERROR;
                    684:                                        if (keepgoing)
                    685:                                                /* Abort the current target,
                    686:                                                 * but let others continue.  */
                    687:                                                printf(" (continuing)\n");
                    688:                                } else {
                    689:                                        /* Continue executing commands for
                    690:                                         * this target.  If we return 0,
                    691:                                         * this will happen...  */
                    692:                                        printf(" (ignored)\n");
                    693:                                        status = 0;
                    694:                                }
                    695:                        }
                    696:                        return !status;
                    697:                } else
                    698:                        Fatal("error in wait: %d", stat);
                    699:                        /*NOTREACHED*/
                    700:        }
                    701:        return 0;
                    702: }
                    703:
                    704: static void
                    705: handle_compat_interrupts(GNode *gn)
                    706: {
                    707:        if (!Targ_Precious(gn)) {
                    708:                char      *file = Varq_Value(TARGET_INDEX, gn);
                    709:
                    710:                if (!noExecute && eunlink(file) != -1)
                    711:                        Error("*** %s removed\n", file);
                    712:        }
                    713:        if (got_SIGINT) {
                    714:                signal(SIGINT, SIG_IGN);
                    715:                signal(SIGTERM, SIG_IGN);
                    716:                signal(SIGHUP, SIG_IGN);
                    717:                signal(SIGQUIT, SIG_IGN);
                    718:                got_signal = 0;
                    719:                got_SIGINT = 0;
                    720:                run_gnode(interrupt_node, 0);
                    721:                exit(255);
                    722:        }
                    723:        exit(255);
                    724: }
                    725:
                    726: int
                    727: run_gnode(GNode *gn, int parallel)
                    728: {
                    729:        LstNode ln, nln;
                    730:
                    731:        if (gn != NULL && (gn->type & OP_DUMMY) == 0) {
                    732:                gn->made = MADE;
                    733:                for (ln = Lst_First(&gn->commands); ln != NULL; ln = nln) {
                    734:                        nln = Lst_Adv(ln);
                    735:                        if (setup_and_run_command(Lst_Datum(ln), gn,
                    736:                            parallel && nln == NULL) == 0)
                    737:                                break;
                    738:                }
                    739:                if (got_signal && !parallel)
                    740:                        handle_compat_interrupts(gn);
                    741:                return gn->made;
                    742:        } else
                    743:                return NOSUCHNODE;
                    744: }
                    745:
                    746: void
                    747: setup_engine()
                    748: {
                    749:        static int already_setup = 0;
                    750:
                    751:        if (!already_setup) {
                    752:                setup_meta();
                    753:                setup_all_signals();
                    754:                already_setup = 1;
                    755:        }
                    756: }