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

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