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

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