[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.16

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.16    ! espie      63: #include "buf.h"
1.1       espie      64:
                     65: static void MakeTimeStamp(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);
1.16    ! espie     105:                        Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn);
1.3       espie     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:
1.16    ! espie     243: void
        !           244: Make_DoAllVar(GNode *gn)
1.1       espie     245: {
1.16    ! espie     246:        GNode *child;
        !           247:        LstNode ln;
        !           248:        BUFFER allsrc, oodate;
        !           249:        char *target;
        !           250:        bool do_oodate;
        !           251:        int oodate_count, allsrc_count = 0;
        !           252:
        !           253:        oodate_count = 0;
        !           254:        allsrc_count = 0;
        !           255:
        !           256:        for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
        !           257:                child = (GNode *)Lst_Datum(ln);
        !           258:                if ((child->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) != 0)
        !           259:                        continue;
1.9       espie     260:                if (OP_NOP(child->type) ||
1.16    ! espie     261:                    (target = Var(TARGET_INDEX, child)) == NULL) {
1.3       espie     262:                        /*
                    263:                         * this node is only source; use the specific pathname
                    264:                         * for it
                    265:                         */
1.9       espie     266:                        target = child->path != NULL ? child->path :
                    267:                            child->name;
1.3       espie     268:                }
1.1       espie     269:
1.16    ! espie     270:                /*
        !           271:                 * It goes in the OODATE variable if the parent is younger than
        !           272:                 * the child or if the child has been modified more recently
        !           273:                 * than the start of the make.  This is to keep make from
        !           274:                 * getting confused if something else updates the parent after
        !           275:                 * the make starts (shouldn't happen, I know, but sometimes it
        !           276:                 * does). In such a case, if we've updated the kid, the parent
        !           277:                 * is likely to have a modification time later than that of the
        !           278:                 * kid and anything that relies on the OODATE variable will be
        !           279:                 * hosed.
        !           280:                 */
        !           281:                do_oodate = false;
        !           282:                if (gn->type & OP_JOIN) {
1.14      espie     283:                        if (child->built_status == MADE)
1.16    ! espie     284:                                do_oodate = true;
        !           285:                } else if (is_strictly_before(gn->mtime, child->mtime) ||
1.9       espie     286:                   (!is_strictly_before(child->mtime, now) &&
1.16    ! espie     287:                   child->built_status == MADE))
        !           288:                        do_oodate = true;
        !           289:                if (do_oodate) {
        !           290:                        oodate_count++;
        !           291:                        if (oodate_count == 1)
        !           292:                                Var(OODATE_INDEX, gn) = target;
        !           293:                        else {
        !           294:                                if (oodate_count == 2) {
        !           295:                                        Buf_Init(&oodate, 0);
        !           296:                                        Buf_AddString(&oodate,
        !           297:                                            Var(OODATE_INDEX, gn));
        !           298:                                }
        !           299:                                Buf_AddSpace(&oodate);
        !           300:                                Buf_AddString(&oodate, target);
        !           301:                        }
        !           302:                }
        !           303:                allsrc_count++;
        !           304:                if (allsrc_count == 1)
        !           305:                        Var(ALLSRC_INDEX, gn) = target;
        !           306:                else {
        !           307:                        if (allsrc_count == 2) {
        !           308:                                Buf_Init(&allsrc, 0);
        !           309:                                Buf_AddString(&allsrc,
        !           310:                                    Var(ALLSRC_INDEX, gn));
        !           311:                        }
        !           312:                        Buf_AddSpace(&allsrc);
        !           313:                        Buf_AddString(&allsrc, target);
1.3       espie     314:                }
1.1       espie     315:        }
                    316:
1.16    ! espie     317:        if (allsrc_count > 1)
        !           318:                Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc);
        !           319:        if (oodate_count > 1)
        !           320:                Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate);
1.1       espie     321:
1.13      espie     322:        if (gn->impliedsrc)
1.16    ! espie     323:                Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
1.13      espie     324:
1.3       espie     325:        if (gn->type & OP_JOIN)
1.16    ! espie     326:                Var(TARGET_INDEX, gn) = Var(ALLSRC_INDEX, gn);
1.1       espie     327: }
                    328:
                    329: /* Wrapper to call Make_TimeStamp from a forEach loop. */
                    330: static void
1.9       espie     331: MakeTimeStamp(void *parent, void *child)
1.1       espie     332: {
1.9       espie     333:     Make_TimeStamp((GNode *)parent, (GNode *)child);
1.1       espie     334: }
                    335:
                    336: bool
1.9       espie     337: Make_OODate(GNode *gn)
1.1       espie     338: {
1.9       espie     339:        bool        oodate;
1.1       espie     340:
                    341:        /*
1.3       espie     342:         * Certain types of targets needn't even be sought as their datedness
                    343:         * doesn't depend on their modification time...
1.1       espie     344:         */
1.15      espie     345:        if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_PHONY)) == 0) {
1.3       espie     346:                (void)Dir_MTime(gn);
                    347:                if (DEBUG(MAKE)) {
1.9       espie     348:                        if (!is_out_of_date(gn->mtime))
1.5       espie     349:                                printf("modified %s...",
1.3       espie     350:                                    time_to_string(gn->mtime));
1.9       espie     351:                        else
1.3       espie     352:                                printf("non-existent...");
                    353:                }
1.1       espie     354:        }
                    355:
                    356:        /*
1.3       espie     357:         * A target is remade in one of the following circumstances:
1.9       espie     358:         * - its modification time is smaller than that of its youngest child
                    359:         *   and it would actually be run (has commands or type OP_NOP)
                    360:         * - it's the object of a force operator
                    361:         * - it has no children, was on the lhs of an operator and doesn't
                    362:         *   exist already.
1.3       espie     363:         *
                    364:         * Libraries are only considered out-of-date if the archive module says
                    365:         * they are.
1.1       espie     366:         */
1.3       espie     367:        if (gn->type & OP_USE) {
                    368:                /*
                    369:                 * If the node is a USE node it is *never* out of date
                    370:                 * no matter *what*.
                    371:                 */
1.9       espie     372:                if (DEBUG(MAKE))
1.3       espie     373:                        printf(".USE node...");
                    374:                oodate = false;
                    375:        } else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
1.9       espie     376:                if (DEBUG(MAKE))
                    377:                    printf("library...");
1.3       espie     378:
1.9       espie     379:                /* always out of date if no children and :: target */
1.3       espie     380:                oodate = Arch_LibOODate(gn) ||
                    381:                    (is_out_of_date(gn->cmtime) && (gn->type & OP_DOUBLEDEP));
                    382:        } else if (gn->type & OP_JOIN) {
                    383:                /*
                    384:                 * A target with the .JOIN attribute is only considered
                    385:                 * out-of-date if any of its children was out-of-date.
                    386:                 */
1.9       espie     387:                if (DEBUG(MAKE))
1.3       espie     388:                        printf(".JOIN node...");
                    389:                oodate = gn->childMade;
                    390:        } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
                    391:                /*
1.9       espie     392:                 * A node which is the object of the force (!) operator or which
                    393:                 * has the .EXEC attribute is always considered out-of-date.
1.3       espie     394:                 */
                    395:                if (DEBUG(MAKE)) {
1.9       espie     396:                        if (gn->type & OP_FORCE)
1.3       espie     397:                                printf("! operator...");
1.9       espie     398:                        else if (gn->type & OP_PHONY)
1.3       espie     399:                                printf(".PHONY node...");
1.9       espie     400:                        else
1.3       espie     401:                                printf(".EXEC node...");
                    402:                }
                    403:                oodate = true;
                    404:        } else if (is_strictly_before(gn->mtime, gn->cmtime) ||
1.9       espie     405:           (is_out_of_date(gn->cmtime) &&
1.3       espie     406:            (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
                    407:                /*
                    408:                 * A node whose modification time is less than that of its
                    409:                 * youngest child or that has no children (cmtime ==
                    410:                 * OUT_OF_DATE) and either doesn't exist (mtime == OUT_OF_DATE)
1.9       espie     411:                 * or was the object of a :: operator is out-of-date.
1.3       espie     412:                 */
                    413:                if (DEBUG(MAKE)) {
1.9       espie     414:                        if (is_strictly_before(gn->mtime, gn->cmtime))
1.3       espie     415:                                printf("modified before source...");
1.9       espie     416:                        else if (is_out_of_date(gn->mtime))
1.3       espie     417:                                printf("non-existent and no sources...");
1.9       espie     418:                        else
1.3       espie     419:                                printf(":: operator and no sources...");
                    420:                }
                    421:                oodate = true;
                    422:        } else {
                    423:                oodate = false;
                    424:        }
1.1       espie     425:
1.3       espie     426:        /*
                    427:         * If the target isn't out-of-date, the parents need to know its
                    428:         * modification time. Note that targets that appear to be out-of-date
                    429:         * but aren't, because they have no commands and aren't of type OP_NOP,
                    430:         * have their mtime stay below their children's mtime to keep parents
                    431:         * from thinking they're out-of-date.
                    432:         */
                    433:        if (!oodate)
                    434:                Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
1.1       espie     435:
1.3       espie     436:        return oodate;
1.1       espie     437: }
                    438:
1.10      espie     439: volatile sig_atomic_t got_signal;
                    440:
                    441: volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT,
                    442:     got_SIGTERM, got_SIGTSTP, got_SIGTTOU, got_SIGTTIN, got_SIGWINCH;
                    443:
                    444: static void
                    445: setup_signal(int sig)
                    446: {
                    447:        if (signal(sig, SIG_IGN) != SIG_IGN) {
                    448:                (void)signal(sig, SigHandler);
                    449:        }
                    450: }
                    451:
                    452: void
                    453: setup_all_signals()
                    454: {
                    455:        /*
                    456:         * Catch the four signals that POSIX specifies if they aren't ignored.
                    457:         * handle_signal will take care of calling JobInterrupt if appropriate.
                    458:         */
                    459:        setup_signal(SIGINT);
                    460:        setup_signal(SIGHUP);
                    461:        setup_signal(SIGQUIT);
                    462:        setup_signal(SIGTERM);
                    463:        /*
                    464:         * There are additional signals that need to be caught and passed if
                    465:         * either the export system wants to be told directly of signals or if
                    466:         * we're giving each job its own process group (since then it won't get
                    467:         * signals from the terminal driver as we own the terminal)
                    468:         */
                    469: #if defined(USE_PGRP)
                    470:        setup_signal(SIGTSTP);
                    471:        setup_signal(SIGTTOU);
                    472:        setup_signal(SIGTTIN);
                    473:        setup_signal(SIGWINCH);
                    474: #endif
                    475: }
                    476:
                    477: void
                    478: SigHandler(int sig)
                    479: {
                    480:        switch(sig) {
                    481:        case SIGINT:
                    482:                got_SIGINT++;
                    483:                got_signal = 1;
                    484:                break;
                    485:        case SIGHUP:
                    486:                got_SIGHUP++;
                    487:                got_signal = 1;
                    488:                break;
                    489:        case SIGQUIT:
                    490:                got_SIGQUIT++;
                    491:                got_signal = 1;
                    492:                break;
                    493:        case SIGTERM:
                    494:                got_SIGTERM++;
                    495:                got_signal = 1;
                    496:                break;
                    497: #ifdef USE_PGRP
                    498:        case SIGTSTP:
                    499:                got_SIGTSTP++;
                    500:                got_signal = 1;
                    501:                break;
                    502:        case SIGTTOU:
                    503:                got_SIGTTOU++;
                    504:                got_signal = 1;
                    505:                break;
                    506:        case SIGTTIN:
                    507:                got_SIGTTIN++;
                    508:                got_signal = 1;
                    509:                break;
                    510:        case SIGWINCH:
                    511:                got_SIGWINCH++;
                    512:                got_signal = 1;
                    513:                break;
                    514: #endif
                    515:        }
                    516: }
                    517:
                    518: /* The following array is used to make a fast determination of which
                    519:  * characters are interpreted specially by the shell.  If a command
                    520:  * contains any of these characters, it is executed by the shell, not
                    521:  * directly by us.  */
                    522: static char        meta[256];
                    523:
                    524: void
                    525: setup_meta(void)
                    526: {
                    527:        char *p;
                    528:
                    529:        for (p = "#=|^(){};&<>*?[]:$`\\\n"; *p != '\0'; p++)
                    530:                meta[(unsigned char) *p] = 1;
                    531:        /* The null character serves as a sentinel in the string.  */
                    532:        meta[0] = 1;
                    533: }
                    534:
                    535: static char **
                    536: recheck_command_for_shell(char **av)
                    537: {
                    538:        char *runsh[] = {
                    539:                "alias", "cd", "eval", "exit", "read", "set", "ulimit",
                    540:                "unalias", "unset", "wait", "umask", NULL
                    541:        };
                    542:
                    543:        char **p;
                    544:
                    545:        /* optimization: if exec cmd, we avoid the intermediate shell */
                    546:        if (strcmp(av[0], "exec") == 0)
                    547:                av++;
                    548:
                    549:        for (p = runsh; *p; p++)
                    550:                if (strcmp(av[0], *p) == 0)
                    551:                        return NULL;
                    552:
                    553:        return av;
                    554: }
                    555:
                    556: static void
                    557: run_command(const char *cmd, bool errCheck)
                    558: {
                    559:        const char *p;
                    560:        char *shargv[4];
                    561:        char **todo;
                    562:
                    563:        shargv[0] = _PATH_BSHELL;
                    564:
                    565:        shargv[1] = errCheck ? "-ec" : "-c";
                    566:        shargv[2] = (char *)cmd;
                    567:        shargv[3] = NULL;
                    568:
                    569:        todo = shargv;
                    570:
                    571:
                    572:        /* Search for meta characters in the command. If there are no meta
                    573:         * characters, there's no need to execute a shell to execute the
                    574:         * command.  */
                    575:        for (p = cmd; !meta[(unsigned char)*p]; p++)
                    576:                continue;
                    577:        if (*p == '\0') {
                    578:                char *bp;
                    579:                char **av;
                    580:                int argc;
                    581:                /* No meta-characters, so probably no need to exec a shell.
                    582:                 * Break the command into words to form an argument vector
                    583:                 * we can execute.  */
                    584:                av = brk_string(cmd, &argc, &bp);
                    585:                av = recheck_command_for_shell(av);
                    586:                if (av != NULL)
                    587:                        todo = av;
                    588:        }
                    589:        execvp(todo[0], todo);
                    590:
                    591:        if (errno == ENOENT)
                    592:                fprintf(stderr, "%s: not found\n", todo[0]);
                    593:        else
                    594:                perror(todo[0]);
                    595:        _exit(1);
                    596: }
                    597:
                    598: /*-
                    599:  *-----------------------------------------------------------------------
                    600:  * setup_and_run_command --
                    601:  *     Execute the next command for a target. If the command returns an
1.14      espie     602:  *     error, the node's built_status field is set to ERROR and creation stops.
1.10      espie     603:  *
                    604:  * Results:
                    605:  *     0 in case of error, 1 if ok.
                    606:  *
                    607:  * Side Effects:
1.14      espie     608:  *     The node's 'built_status' field may be set to ERROR.
1.10      espie     609:  *-----------------------------------------------------------------------
                    610:  */
                    611: static int
                    612: setup_and_run_command(char *cmd, GNode *gn, int dont_fork)
                    613: {
                    614:        char *cmdStart; /* Start of expanded command */
                    615:        bool silent;    /* Don't print command */
                    616:        bool doExecute; /* Execute the command */
                    617:        bool errCheck;  /* Check errors */
                    618:        int reason;     /* Reason for child's death */
                    619:        int status;     /* Description of child's death */
                    620:        pid_t cpid;     /* Child actually found */
                    621:        pid_t stat;     /* Status of fork */
                    622:
                    623:        silent = gn->type & OP_SILENT;
                    624:        errCheck = !(gn->type & OP_IGNORE);
                    625:        doExecute = !noExecute;
                    626:
                    627:        cmdStart = Var_Subst(cmd, &gn->context, false);
                    628:
                    629:        /* How can we execute a null command ? we warn the user that the
                    630:         * command expanded to nothing (is this the right thing to do?).  */
                    631:        if (*cmdStart == '\0') {
                    632:                free(cmdStart);
                    633:                Error("%s expands to empty string", cmd);
                    634:                return 1;
                    635:        } else
                    636:                cmd = cmdStart;
                    637:
                    638:        for (;; cmd++) {
                    639:                if (*cmd == '@')
                    640:                        silent = DEBUG(LOUD) ? false : true;
                    641:                else if (*cmd == '-')
                    642:                        errCheck = false;
                    643:                else if (*cmd == '+')
                    644:                        doExecute = true;
                    645:                else
                    646:                        break;
                    647:        }
                    648:        while (isspace(*cmd))
                    649:                cmd++;
                    650:        /* Print the command before echoing if we're not supposed to be quiet
                    651:         * for this one. We also print the command if -n given.  */
                    652:        if (!silent || noExecute) {
                    653:                printf("%s\n", cmd);
                    654:                fflush(stdout);
                    655:        }
                    656:        /* If we're not supposed to execute any commands, this is as far as
                    657:         * we go...  */
                    658:        if (!doExecute)
                    659:                return 1;
                    660:
                    661:        /* if we're running in parallel mode, we try not to fork the last
                    662:         * command, since it's exit status will be just fine... unless
                    663:         * errCheck is not set, in which case we must deal with the
                    664:         * status ourselves.
                    665:         */
                    666:        if (dont_fork && errCheck)
                    667:                run_command(cmd, errCheck);
                    668:                /*NOTREACHED*/
                    669:
                    670:        /* Fork and execute the single command. If the fork fails, we abort.  */
                    671:        switch (cpid = fork()) {
                    672:        case -1:
                    673:                Fatal("Could not fork");
                    674:                /*NOTREACHED*/
                    675:        case 0:
                    676:                run_command(cmd, errCheck);
                    677:                /*NOTREACHED*/
                    678:        default:
                    679:                break;
                    680:        }
                    681:        free(cmdStart);
                    682:
                    683:        /* The child is off and running. Now all we can do is wait...  */
                    684:        while (1) {
                    685:
                    686:                while ((stat = wait(&reason)) != cpid) {
                    687:                        if (stat == -1 && errno != EINTR)
                    688:                                break;
                    689:                }
                    690:
                    691:                if (got_signal)
                    692:                        break;
                    693:
                    694:                if (stat != -1) {
                    695:                        if (WIFSTOPPED(reason))
                    696:                                status = WSTOPSIG(reason);      /* stopped */
                    697:                        else if (WIFEXITED(reason)) {
                    698:                                status = WEXITSTATUS(reason);   /* exited */
                    699:                                if (status != 0)
                    700:                                    printf("*** Error code %d", status);
                    701:                        } else {
                    702:                                status = WTERMSIG(reason);      /* signaled */
                    703:                                printf("*** Signal %d", status);
                    704:                        }
                    705:
                    706:
                    707:                        if (!WIFEXITED(reason) || status != 0) {
                    708:                                if (errCheck) {
1.14      espie     709:                                        gn->built_status = ERROR;
1.10      espie     710:                                        if (keepgoing)
                    711:                                                /* Abort the current target,
                    712:                                                 * but let others continue.  */
                    713:                                                printf(" (continuing)\n");
                    714:                                } else {
                    715:                                        /* Continue executing commands for
                    716:                                         * this target.  If we return 0,
                    717:                                         * this will happen...  */
                    718:                                        printf(" (ignored)\n");
                    719:                                        status = 0;
                    720:                                }
                    721:                        }
                    722:                        return !status;
                    723:                } else
                    724:                        Fatal("error in wait: %d", stat);
                    725:                        /*NOTREACHED*/
                    726:        }
                    727:        return 0;
                    728: }
                    729:
                    730: static void
                    731: handle_compat_interrupts(GNode *gn)
                    732: {
                    733:        if (!Targ_Precious(gn)) {
1.16    ! espie     734:                char *file = Var(TARGET_INDEX, gn);
1.10      espie     735:
                    736:                if (!noExecute && eunlink(file) != -1)
                    737:                        Error("*** %s removed\n", file);
                    738:        }
                    739:        if (got_SIGINT) {
                    740:                signal(SIGINT, SIG_IGN);
                    741:                signal(SIGTERM, SIG_IGN);
                    742:                signal(SIGHUP, SIG_IGN);
                    743:                signal(SIGQUIT, SIG_IGN);
                    744:                got_signal = 0;
                    745:                got_SIGINT = 0;
                    746:                run_gnode(interrupt_node, 0);
                    747:                exit(255);
                    748:        }
                    749:        exit(255);
                    750: }
                    751:
                    752: int
                    753: run_gnode(GNode *gn, int parallel)
                    754: {
                    755:        LstNode ln, nln;
                    756:
                    757:        if (gn != NULL && (gn->type & OP_DUMMY) == 0) {
1.14      espie     758:                gn->built_status = MADE;
1.10      espie     759:                for (ln = Lst_First(&gn->commands); ln != NULL; ln = nln) {
                    760:                        nln = Lst_Adv(ln);
                    761:                        if (setup_and_run_command(Lst_Datum(ln), gn,
                    762:                            parallel && nln == NULL) == 0)
                    763:                                break;
                    764:                }
                    765:                if (got_signal && !parallel)
                    766:                        handle_compat_interrupts(gn);
1.14      espie     767:                return gn->built_status;
1.10      espie     768:        } else
                    769:                return NOSUCHNODE;
                    770: }
                    771:
                    772: void
                    773: setup_engine()
                    774: {
                    775:        static int already_setup = 0;
                    776:
                    777:        if (!already_setup) {
                    778:                setup_meta();
                    779:                setup_all_signals();
                    780:                already_setup = 1;
                    781:        }
                    782: }