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

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