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