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