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

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