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