Annotation of src/usr.bin/make/compat.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: compat.c,v 1.5 1997/04/01 07:28:09 millert Exp $ */
1.5 millert 2: /* $NetBSD: compat.c,v 1.18 1997/03/28 22:31:22 christos Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
6: * Copyright (c) 1988, 1989 by Adam de Boor
7: * Copyright (c) 1989 by Berkeley Softworks
8: * All rights reserved.
9: *
10: * This code is derived from software contributed to Berkeley by
11: * Adam de Boor.
12: *
13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
16: * 1. Redistributions of source code must retain the above copyright
17: * notice, this list of conditions and the following disclaimer.
18: * 2. Redistributions in binary form must reproduce the above copyright
19: * notice, this list of conditions and the following disclaimer in the
20: * documentation and/or other materials provided with the distribution.
21: * 3. All advertising materials mentioning features or use of this software
22: * must display the following acknowledgement:
23: * This product includes software developed by the University of
24: * California, Berkeley and its contributors.
25: * 4. Neither the name of the University nor the names of its contributors
26: * may be used to endorse or promote products derived from this software
27: * without specific prior written permission.
28: *
29: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39: * SUCH DAMAGE.
40: */
41:
42: #ifndef lint
43: #if 0
1.4 millert 44: static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
1.1 deraadt 45: #else
1.6 ! millert 46: static char rcsid[] = "$OpenBSD: compat.c,v 1.5 1997/04/01 07:28:09 millert Exp $";
1.1 deraadt 47: #endif
48: #endif /* not lint */
49:
50: /*-
51: * compat.c --
52: * The routines in this file implement the full-compatibility
53: * mode of PMake. Most of the special functionality of PMake
54: * is available in this mode. Things not supported:
55: * - different shells.
56: * - friendly variable substitution.
57: *
58: * Interface:
59: * Compat_Run Initialize things for this module and recreate
60: * thems as need creatin'
61: */
62:
63: #include <stdio.h>
64: #include <sys/types.h>
65: #include <sys/stat.h>
66: #include <sys/wait.h>
67: #include <ctype.h>
68: #include <errno.h>
69: #include <signal.h>
70: #include "make.h"
71: #include "hash.h"
72: #include "dir.h"
73: #include "job.h"
74: extern int errno;
75:
76: /*
77: * The following array is used to make a fast determination of which
78: * characters are interpreted specially by the shell. If a command
79: * contains any of these characters, it is executed by the shell, not
80: * directly by us.
81: */
82:
83: static char meta[256];
84:
85: static GNode *curTarg = NILGNODE;
86: static GNode *ENDNode;
87: static void CompatInterrupt __P((int));
88: static int CompatRunCommand __P((ClientData, ClientData));
89: static int CompatMake __P((ClientData, ClientData));
90:
91: /*-
92: *-----------------------------------------------------------------------
93: * CompatInterrupt --
94: * Interrupt the creation of the current target and remove it if
95: * it ain't precious.
96: *
97: * Results:
98: * None.
99: *
100: * Side Effects:
101: * The target is removed and the process exits. If .INTERRUPT exists,
102: * its commands are run first WITH INTERRUPTS IGNORED..
103: *
104: *-----------------------------------------------------------------------
105: */
106: static void
107: CompatInterrupt (signo)
108: int signo;
109: {
110: GNode *gn;
1.4 millert 111:
1.1 deraadt 112: if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
113: char *p1;
114: char *file = Var_Value (TARGET, curTarg, &p1);
115:
1.2 deraadt 116: if (!noExecute && eunlink(file) != -1) {
1.1 deraadt 117: printf ("*** %s removed\n", file);
118: }
119: if (p1)
120: free(p1);
121:
122: /*
123: * Run .INTERRUPT only if hit with interrupt signal
124: */
125: if (signo == SIGINT) {
126: gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
127: if (gn != NILGNODE) {
128: Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
129: }
130: }
131:
132: }
133: exit (signo);
134: }
135:
136: /*-
137: *-----------------------------------------------------------------------
138: * CompatRunCommand --
139: * Execute the next command for a target. If the command returns an
140: * error, the node's made field is set to ERROR and creation stops.
141: *
142: * Results:
143: * 0 if the command succeeded, 1 if an error occurred.
144: *
145: * Side Effects:
146: * The node's 'made' field may be set to ERROR.
147: *
148: *-----------------------------------------------------------------------
149: */
150: static int
151: CompatRunCommand (cmdp, gnp)
152: ClientData cmdp; /* Command to execute */
153: ClientData gnp; /* Node from which the command came */
154: {
155: char *cmdStart; /* Start of expanded command */
156: register char *cp;
157: Boolean silent, /* Don't print command */
158: errCheck; /* Check errors */
1.2 deraadt 159: int reason; /* Reason for child's death */
1.1 deraadt 160: int status; /* Description of child's death */
161: int cpid; /* Child actually found */
162: ReturnStatus stat; /* Status of fork */
163: LstNode cmdNode; /* Node where current command is located */
164: char **av; /* Argument vector for thing to exec */
165: int argc; /* Number of arguments in av or 0 if not
166: * dynamically allocated */
167: Boolean local; /* TRUE if command should be executed
168: * locally */
169: char *cmd = (char *) cmdp;
170: GNode *gn = (GNode *) gnp;
171:
1.4 millert 172: /*
1.1 deraadt 173: * Avoid clobbered variable warnings by forcing the compiler
174: * to ``unregister'' variables
175: */
176: #if __GNUC__
177: (void) &av;
178: (void) &errCheck;
179: #endif
180: silent = gn->type & OP_SILENT;
181: errCheck = !(gn->type & OP_IGNORE);
182:
183: cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
184: cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
185:
186: /*
187: * brk_string will return an argv with a NULL in av[1], thus causing
188: * execvp to choke and die horribly. Besides, how can we execute a null
189: * command? In any case, we warn the user that the command expanded to
190: * nothing (is this the right thing to do?).
191: */
1.4 millert 192:
1.1 deraadt 193: if (*cmdStart == '\0') {
194: free(cmdStart);
195: Error("%s expands to empty string", cmd);
196: return(0);
197: } else {
198: cmd = cmdStart;
199: }
200: Lst_Replace (cmdNode, (ClientData)cmdStart);
201:
202: if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
203: (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
204: return(0);
205: } else if (strcmp(cmdStart, "...") == 0) {
206: gn->type |= OP_SAVE_CMDS;
207: return(0);
208: }
209:
210: while ((*cmd == '@') || (*cmd == '-')) {
211: if (*cmd == '@') {
212: silent = TRUE;
213: } else {
214: errCheck = FALSE;
215: }
216: cmd++;
217: }
218:
219: while (isspace((unsigned char)*cmd))
220: cmd++;
1.4 millert 221:
1.1 deraadt 222: /*
223: * Search for meta characters in the command. If there are no meta
224: * characters, there's no need to execute a shell to execute the
225: * command.
226: */
227: for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
228: continue;
229: }
230:
231: /*
232: * Print the command before echoing if we're not supposed to be quiet for
233: * this one. We also print the command if -n given.
234: */
235: if (!silent || noExecute) {
236: printf ("%s\n", cmd);
237: fflush(stdout);
238: }
239:
240: /*
241: * If we're not supposed to execute any commands, this is as far as
242: * we go...
243: */
244: if (noExecute) {
245: return (0);
246: }
1.4 millert 247:
1.1 deraadt 248: if (*cp != '\0') {
249: /*
250: * If *cp isn't the null character, we hit a "meta" character and
251: * need to pass the command off to the shell. We give the shell the
252: * -e flag as well as -c if it's supposed to exit when it hits an
253: * error.
254: */
255: static char *shargv[4] = { "/bin/sh" };
256:
257: shargv[1] = (errCheck ? "-ec" : "-c");
258: shargv[2] = cmd;
259: shargv[3] = (char *)NULL;
260: av = shargv;
261: argc = 0;
262: } else {
263: /*
264: * No meta-characters, so no need to exec a shell. Break the command
265: * into words to form an argument vector we can execute.
266: * brk_string sticks our name in av[0], so we have to
267: * skip over it...
268: */
269: av = brk_string(cmd, &argc, TRUE);
270: av += 1;
271: }
1.4 millert 272:
1.1 deraadt 273: local = TRUE;
274:
275: /*
276: * Fork and execute the single command. If the fork fails, we abort.
277: */
278: cpid = vfork();
279: if (cpid < 0) {
280: Fatal("Could not fork");
281: }
282: if (cpid == 0) {
283: if (local) {
284: execvp(av[0], av);
285: (void) write (2, av[0], strlen (av[0]));
286: (void) write (2, ": not found\n", sizeof(": not found"));
287: } else {
288: (void)execv(av[0], av);
289: }
290: exit(1);
291: }
292: free(cmdStart);
293: Lst_Replace (cmdNode, (ClientData) NULL);
1.4 millert 294:
1.1 deraadt 295: /*
296: * The child is off and running. Now all we can do is wait...
297: */
298: while (1) {
299:
1.2 deraadt 300: while ((stat = wait(&reason)) != cpid) {
1.1 deraadt 301: if (stat == -1 && errno != EINTR) {
302: break;
303: }
304: }
1.4 millert 305:
1.1 deraadt 306: if (stat > -1) {
307: if (WIFSTOPPED(reason)) {
1.2 deraadt 308: status = WSTOPSIG(reason); /* stopped */
1.1 deraadt 309: } else if (WIFEXITED(reason)) {
1.2 deraadt 310: status = WEXITSTATUS(reason); /* exited */
1.1 deraadt 311: if (status != 0) {
312: printf ("*** Error code %d", status);
313: }
314: } else {
1.2 deraadt 315: status = WTERMSIG(reason); /* signaled */
1.1 deraadt 316: printf ("*** Signal %d", status);
1.4 millert 317: }
318:
1.1 deraadt 319:
320: if (!WIFEXITED(reason) || (status != 0)) {
321: if (errCheck) {
322: gn->made = ERROR;
323: if (keepgoing) {
324: /*
325: * Abort the current target, but let others
326: * continue.
327: */
328: printf (" (continuing)\n");
329: }
330: } else {
331: /*
332: * Continue executing commands for this target.
333: * If we return 0, this will happen...
334: */
335: printf (" (ignored)\n");
336: status = 0;
337: }
338: }
339: break;
340: } else {
341: Fatal ("error in wait: %d", stat);
342: /*NOTREACHED*/
343: }
344: }
345:
346: return (status);
347: }
348:
349: /*-
350: *-----------------------------------------------------------------------
351: * CompatMake --
352: * Make a target.
353: *
354: * Results:
355: * 0
356: *
357: * Side Effects:
358: * If an error is detected and not being ignored, the process exits.
359: *
360: *-----------------------------------------------------------------------
361: */
362: static int
363: CompatMake (gnp, pgnp)
364: ClientData gnp; /* The node to make */
365: ClientData pgnp; /* Parent to abort if necessary */
366: {
367: GNode *gn = (GNode *) gnp;
368: GNode *pgn = (GNode *) pgnp;
1.5 millert 369:
1.6 ! millert 370: if (gn->type & OP_USE) {
! 371: Make_HandleUse(gn, pgn);
! 372: } else if (gn->made == UNMADE) {
1.1 deraadt 373: /*
374: * First mark ourselves to be made, then apply whatever transformations
375: * the suffix module thinks are necessary. Once that's done, we can
376: * descend and make all our children. If any of them has an error
377: * but the -k flag was given, our 'make' field will be set FALSE again.
378: * This is our signal to not attempt to do anything but abort our
379: * parent as well.
380: */
381: gn->make = TRUE;
382: gn->made = BEINGMADE;
383: Suff_FindDeps (gn);
384: Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
385: if (!gn->make) {
386: gn->made = ABORTED;
387: pgn->make = FALSE;
388: return (0);
389: }
390:
391: if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
392: char *p1;
393: Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
394: if (p1)
395: free(p1);
396: }
1.4 millert 397:
1.1 deraadt 398: /*
399: * All the children were made ok. Now cmtime contains the modification
400: * time of the newest child, we need to find out if we exist and when
401: * we were modified last. The criteria for datedness are defined by the
402: * Make_OODate function.
403: */
404: if (DEBUG(MAKE)) {
405: printf("Examining %s...", gn->name);
406: }
407: if (! Make_OODate(gn)) {
408: gn->made = UPTODATE;
409: if (DEBUG(MAKE)) {
410: printf("up-to-date.\n");
411: }
412: return (0);
413: } else if (DEBUG(MAKE)) {
414: printf("out-of-date.\n");
415: }
416:
417: /*
418: * If the user is just seeing if something is out-of-date, exit now
419: * to tell him/her "yes".
420: */
421: if (queryFlag) {
422: exit (-1);
423: }
424:
425: /*
426: * We need to be re-made. We also have to make sure we've got a $?
427: * variable. To be nice, we also define the $> variable using
428: * Make_DoAllVar().
429: */
430: Make_DoAllVar(gn);
1.4 millert 431:
1.1 deraadt 432: /*
433: * Alter our type to tell if errors should be ignored or things
434: * should not be printed so CompatRunCommand knows what to do.
435: */
436: if (Targ_Ignore (gn)) {
437: gn->type |= OP_IGNORE;
438: }
439: if (Targ_Silent (gn)) {
440: gn->type |= OP_SILENT;
441: }
442:
443: if (Job_CheckCommands (gn, Fatal)) {
444: /*
445: * Our commands are ok, but we still have to worry about the -t
446: * flag...
447: */
448: if (!touchFlag) {
449: curTarg = gn;
450: Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
451: curTarg = NILGNODE;
452: } else {
453: Job_Touch (gn, gn->type & OP_SILENT);
454: }
455: } else {
456: gn->made = ERROR;
457: }
458:
459: if (gn->made != ERROR) {
460: /*
461: * If the node was made successfully, mark it so, update
462: * its modification time and timestamp all its parents. Note
463: * that for .ZEROTIME targets, the timestamping isn't done.
464: * This is to keep its state from affecting that of its parent.
465: */
466: gn->made = MADE;
467: #ifndef RECHECK
468: /*
469: * We can't re-stat the thing, but we can at least take care of
470: * rules where a target depends on a source that actually creates
471: * the target, but only if it has changed, e.g.
472: *
473: * parse.h : parse.o
474: *
475: * parse.o : parse.y
476: * yacc -d parse.y
477: * cc -c y.tab.c
478: * mv y.tab.o parse.o
479: * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
480: *
481: * In this case, if the definitions produced by yacc haven't
482: * changed from before, parse.h won't have been updated and
483: * gn->mtime will reflect the current modification time for
484: * parse.h. This is something of a kludge, I admit, but it's a
485: * useful one..
486: *
487: * XXX: People like to use a rule like
488: *
489: * FRC:
490: *
491: * To force things that depend on FRC to be made, so we have to
492: * check for gn->children being empty as well...
493: */
494: if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
495: gn->mtime = now;
496: }
497: #else
498: /*
499: * This is what Make does and it's actually a good thing, as it
500: * allows rules like
501: *
502: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
503: *
504: * to function as intended. Unfortunately, thanks to the stateless
505: * nature of NFS (and the speed of this program), there are times
506: * when the modification time of a file created on a remote
507: * machine will not be modified before the stat() implied by
508: * the Dir_MTime occurs, thus leading us to believe that the file
509: * is unchanged, wreaking havoc with files that depend on this one.
510: *
511: * I have decided it is better to make too much than to make too
512: * little, so this stuff is commented out unless you're sure it's
513: * ok.
514: * -- ardeb 1/12/88
515: */
516: if (noExecute || Dir_MTime(gn) == 0) {
517: gn->mtime = now;
518: }
519: if (gn->cmtime > gn->mtime)
520: gn->mtime = gn->cmtime;
521: if (DEBUG(MAKE)) {
522: printf("update time: %s\n", Targ_FmtTime(gn->mtime));
523: }
524: #endif
525: if (!(gn->type & OP_EXEC)) {
526: pgn->childMade = TRUE;
527: Make_TimeStamp(pgn, gn);
528: }
529: } else if (keepgoing) {
530: pgn->make = FALSE;
531: } else {
532: printf ("\n\nStop.\n");
533: exit (1);
534: }
535: } else if (gn->made == ERROR) {
536: /*
537: * Already had an error when making this beastie. Tell the parent
538: * to abort.
539: */
540: pgn->make = FALSE;
541: } else {
542: if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
543: char *p1;
544: Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
545: if (p1)
546: free(p1);
547: }
548: switch(gn->made) {
549: case BEINGMADE:
550: Error("Graph cycles through %s\n", gn->name);
551: gn->made = ERROR;
552: pgn->make = FALSE;
553: break;
554: case MADE:
555: if ((gn->type & OP_EXEC) == 0) {
556: pgn->childMade = TRUE;
557: Make_TimeStamp(pgn, gn);
558: }
559: break;
560: case UPTODATE:
561: if ((gn->type & OP_EXEC) == 0) {
562: Make_TimeStamp(pgn, gn);
563: }
564: break;
565: default:
566: break;
567: }
568: }
569:
570: return (0);
571: }
1.4 millert 572:
1.1 deraadt 573: /*-
574: *-----------------------------------------------------------------------
575: * Compat_Run --
576: * Initialize this mode and start making.
577: *
578: * Results:
579: * None.
580: *
581: * Side Effects:
582: * Guess what?
583: *
584: *-----------------------------------------------------------------------
585: */
586: void
587: Compat_Run(targs)
588: Lst targs; /* List of target nodes to re-create */
589: {
590: char *cp; /* Pointer to string of shell meta-characters */
591: GNode *gn = NULL;/* Current root target */
592: int errors; /* Number of targets not remade due to errors */
593:
594: if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
595: signal(SIGINT, CompatInterrupt);
596: }
597: if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
598: signal(SIGTERM, CompatInterrupt);
599: }
600: if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
601: signal(SIGHUP, CompatInterrupt);
602: }
603: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
604: signal(SIGQUIT, CompatInterrupt);
605: }
606:
607: for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
608: meta[(unsigned char) *cp] = 1;
609: }
610: /*
611: * The null character serves as a sentinel in the string.
612: */
613: meta[0] = 1;
614:
615: ENDNode = Targ_FindNode(".END", TARG_CREATE);
616: /*
617: * If the user has defined a .BEGIN target, execute the commands attached
618: * to it.
619: */
620: if (!queryFlag) {
621: gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
622: if (gn != NILGNODE) {
623: Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
1.4 millert 624: if (gn->made == ERROR) {
625: printf("\n\nStop.\n");
626: exit(1);
627: }
1.1 deraadt 628: }
629: }
630:
631: /*
632: * For each entry in the list of targets to create, call CompatMake on
633: * it to create the thing. CompatMake will leave the 'made' field of gn
634: * in one of several states:
635: * UPTODATE gn was already up-to-date
636: * MADE gn was recreated successfully
637: * ERROR An error occurred while gn was being created
638: * ABORTED gn was not remade because one of its inferiors
639: * could not be made due to errors.
640: */
641: errors = 0;
642: while (!Lst_IsEmpty (targs)) {
643: gn = (GNode *) Lst_DeQueue (targs);
644: CompatMake (gn, gn);
645:
646: if (gn->made == UPTODATE) {
647: printf ("`%s' is up to date.\n", gn->name);
648: } else if (gn->made == ABORTED) {
649: printf ("`%s' not remade because of errors.\n", gn->name);
650: errors += 1;
651: }
652: }
653:
654: /*
655: * If the user has defined a .END target, run its commands.
656: */
657: if (errors == 0) {
658: Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);
659: }
660: }