compat.c revision 1.14 1 /* $NetBSD: compat.c,v 1.14 1996/11/06 17:59:01 christos Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * Copyright (c) 1988, 1989 by Adam de Boor
6 * Copyright (c) 1989 by Berkeley Softworks
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Adam de Boor.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
44 #else
45 static char rcsid[] = "$NetBSD: compat.c,v 1.14 1996/11/06 17:59:01 christos Exp $";
46 #endif
47 #endif /* not lint */
48
49 /*-
50 * compat.c --
51 * The routines in this file implement the full-compatibility
52 * mode of PMake. Most of the special functionality of PMake
53 * is available in this mode. Things not supported:
54 * - different shells.
55 * - friendly variable substitution.
56 *
57 * Interface:
58 * Compat_Run Initialize things for this module and recreate
59 * thems as need creatin'
60 */
61
62 #include <stdio.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <sys/wait.h>
66 #include <ctype.h>
67 #include <errno.h>
68 #include <signal.h>
69 #include "make.h"
70 #include "hash.h"
71 #include "dir.h"
72 #include "job.h"
73 extern int errno;
74
75 /*
76 * The following array is used to make a fast determination of which
77 * characters are interpreted specially by the shell. If a command
78 * contains any of these characters, it is executed by the shell, not
79 * directly by us.
80 */
81
82 static char meta[256];
83
84 static GNode *curTarg = NILGNODE;
85 static GNode *ENDNode;
86 static void CompatInterrupt __P((int));
87 static int CompatRunCommand __P((ClientData, ClientData));
88 static int CompatMake __P((ClientData, ClientData));
89
90 /*-
91 *-----------------------------------------------------------------------
92 * CompatInterrupt --
93 * Interrupt the creation of the current target and remove it if
94 * it ain't precious.
95 *
96 * Results:
97 * None.
98 *
99 * Side Effects:
100 * The target is removed and the process exits. If .INTERRUPT exists,
101 * its commands are run first WITH INTERRUPTS IGNORED..
102 *
103 *-----------------------------------------------------------------------
104 */
105 static void
106 CompatInterrupt (signo)
107 int signo;
108 {
109 GNode *gn;
110
111 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
112 char *p1;
113 char *file = Var_Value (TARGET, curTarg, &p1);
114
115 if (!noExecute && eunlink(file) != -1) {
116 printf ("*** %s removed\n", file);
117 }
118 if (p1)
119 free(p1);
120
121 /*
122 * Run .INTERRUPT only if hit with interrupt signal
123 */
124 if (signo == SIGINT) {
125 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
126 if (gn != NILGNODE) {
127 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
128 }
129 }
130
131 }
132 exit (signo);
133 }
134
135 /*-
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 */
159 int reason; /* Reason for child's death */
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
172 /*
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 */
192
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++;
221
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 }
247
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 }
272
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);
294
295 /*
296 * The child is off and running. Now all we can do is wait...
297 */
298 while (1) {
299
300 while ((stat = wait(&reason)) != cpid) {
301 if (stat == -1 && errno != EINTR) {
302 break;
303 }
304 }
305
306 if (stat > -1) {
307 if (WIFSTOPPED(reason)) {
308 status = WSTOPSIG(reason); /* stopped */
309 } else if (WIFEXITED(reason)) {
310 status = WEXITSTATUS(reason); /* exited */
311 if (status != 0) {
312 printf ("*** Error code %d", status);
313 }
314 } else {
315 status = WTERMSIG(reason); /* signaled */
316 printf ("*** Signal %d", status);
317 }
318
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 /*-
351 *-----------------------------------------------------------------------
352 * CompatMake --
353 * Make a target.
354 *
355 * Results:
356 * 0
357 *
358 * Side Effects:
359 * If an error is detected and not being ignored, the process exits.
360 *
361 *-----------------------------------------------------------------------
362 */
363 static int
364 CompatMake (gnp, pgnp)
365 ClientData gnp; /* The node to make */
366 ClientData pgnp; /* Parent to abort if necessary */
367 {
368 GNode *gn = (GNode *) gnp;
369 GNode *pgn = (GNode *) pgnp;
370 if (gn->type & OP_USE) {
371 Make_HandleUse(gn, pgn);
372 } else if (gn->made == UNMADE) {
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 }
397
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);
431
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 }
572
573 /*-
575 *-----------------------------------------------------------------------
576 * Compat_Run --
577 * Initialize this mode and start making.
578 *
579 * Results:
580 * None.
581 *
582 * Side Effects:
583 * Guess what?
584 *
585 *-----------------------------------------------------------------------
586 */
587 void
588 Compat_Run(targs)
589 Lst targs; /* List of target nodes to re-create */
590 {
591 char *cp; /* Pointer to string of shell meta-characters */
592 GNode *gn = NULL;/* Current root target */
593 int errors; /* Number of targets not remade due to errors */
594
595 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
596 signal(SIGINT, CompatInterrupt);
597 }
598 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
599 signal(SIGTERM, CompatInterrupt);
600 }
601 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
602 signal(SIGHUP, CompatInterrupt);
603 }
604 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
605 signal(SIGQUIT, CompatInterrupt);
606 }
607
608 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
609 meta[(unsigned char) *cp] = 1;
610 }
611 /*
612 * The null character serves as a sentinel in the string.
613 */
614 meta[0] = 1;
615
616 ENDNode = Targ_FindNode(".END", TARG_CREATE);
617 /*
618 * If the user has defined a .BEGIN target, execute the commands attached
619 * to it.
620 */
621 if (!queryFlag) {
622 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
623 if (gn != NILGNODE) {
624 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
625 if (gn->made == ERROR) {
626 printf("\n\nStop.\n");
627 exit(1);
628 }
629 }
630 }
631
632 /*
633 * For each entry in the list of targets to create, call CompatMake on
634 * it to create the thing. CompatMake will leave the 'made' field of gn
635 * in one of several states:
636 * UPTODATE gn was already up-to-date
637 * MADE gn was recreated successfully
638 * ERROR An error occurred while gn was being created
639 * ABORTED gn was not remade because one of its inferiors
640 * could not be made due to errors.
641 */
642 errors = 0;
643 while (!Lst_IsEmpty (targs)) {
644 gn = (GNode *) Lst_DeQueue (targs);
645 CompatMake (gn, gn);
646
647 if (gn->made == UPTODATE) {
648 printf ("`%s' is up to date.\n", gn->name);
649 } else if (gn->made == ABORTED) {
650 printf ("`%s' not remade because of errors.\n", gn->name);
651 errors += 1;
652 }
653 }
654
655 /*
656 * If the user has defined a .END target, run its commands.
657 */
658 if (errors == 0) {
659 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);
660 }
661 }
662