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

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