isblank() implementation.
[minix.git] / commands / make / compat.c
blob944ed366bd7de8b5997001d704930afeba3d1e89
1 /* $NetBSD: compat.c,v 1.76 2009/02/22 07:33:00 dholland Exp $ */
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
41 * Adam de Boor.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
72 #ifndef MAKE_NATIVE
73 static char rcsid[] = "$NetBSD: compat.c,v 1.76 2009/02/22 07:33:00 dholland Exp $";
74 #else
75 #include <sys/cdefs.h>
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
79 #else
80 __RCSID("$NetBSD: compat.c,v 1.76 2009/02/22 07:33:00 dholland Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
85 /*-
86 * compat.c --
87 * The routines in this file implement the full-compatibility
88 * mode of PMake. Most of the special functionality of PMake
89 * is available in this mode. Things not supported:
90 * - different shells.
91 * - friendly variable substitution.
93 * Interface:
94 * Compat_Run Initialize things for this module and recreate
95 * thems as need creatin'
98 #include <sys/types.h>
99 #include <sys/stat.h>
100 #include <sys/wait.h>
102 #include <ctype.h>
103 #include <errno.h>
104 #include <signal.h>
105 #include <stdio.h>
107 #include "make.h"
108 #include "hash.h"
109 #include "dir.h"
110 #include "job.h"
111 #include "pathnames.h"
114 * The following array is used to make a fast determination of which
115 * characters are interpreted specially by the shell. If a command
116 * contains any of these characters, it is executed by the shell, not
117 * directly by us.
120 static char meta[256];
122 static GNode *curTarg = NULL;
123 static GNode *ENDNode;
124 static void CompatInterrupt(int);
126 static void
127 Compat_Init(void)
129 const char *cp;
131 Shell_Init(); /* setup default shell */
133 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
134 meta[(unsigned char) *cp] = 1;
137 * The null character serves as a sentinel in the string.
139 meta[0] = 1;
143 *-----------------------------------------------------------------------
144 * CompatInterrupt --
145 * Interrupt the creation of the current target and remove it if
146 * it ain't precious.
148 * Results:
149 * None.
151 * Side Effects:
152 * The target is removed and the process exits. If .INTERRUPT exists,
153 * its commands are run first WITH INTERRUPTS IGNORED..
155 *-----------------------------------------------------------------------
157 static void
158 CompatInterrupt(int signo)
160 GNode *gn;
162 if ((curTarg != NULL) && !Targ_Precious (curTarg)) {
163 char *p1;
164 char *file = Var_Value(TARGET, curTarg, &p1);
166 if (!noExecute && eunlink(file) != -1) {
167 Error("*** %s removed", file);
169 if (p1)
170 free(p1);
173 * Run .INTERRUPT only if hit with interrupt signal
175 if (signo == SIGINT) {
176 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
177 if (gn != NULL) {
178 Compat_Make(gn, gn);
183 exit(signo);
187 *-----------------------------------------------------------------------
188 * CompatRunCommand --
189 * Execute the next command for a target. If the command returns an
190 * error, the node's made field is set to ERROR and creation stops.
192 * Input:
193 * cmdp Command to execute
194 * gnp Node from which the command came
196 * Results:
197 * 0 if the command succeeded, 1 if an error occurred.
199 * Side Effects:
200 * The node's 'made' field may be set to ERROR.
202 *-----------------------------------------------------------------------
205 CompatRunCommand(void *cmdp, void *gnp)
207 char *cmdStart; /* Start of expanded command */
208 char *cp, *bp;
209 Boolean silent, /* Don't print command */
210 doIt; /* Execute even if -n */
211 volatile Boolean errCheck; /* Check errors */
212 int reason; /* Reason for child's death */
213 int status; /* Description of child's death */
214 pid_t cpid; /* Child actually found */
215 pid_t retstat; /* Result of wait */
216 LstNode cmdNode; /* Node where current command is located */
217 const char ** volatile av; /* Argument vector for thing to exec */
218 char ** volatile mav;/* Copy of the argument vector for freeing */
219 int argc; /* Number of arguments in av or 0 if not
220 * dynamically allocated */
221 Boolean local; /* TRUE if command should be executed
222 * locally */
223 Boolean useShell; /* TRUE if command should be executed
224 * using a shell */
225 char * volatile cmd = (char *)cmdp;
226 GNode *gn = (GNode *)gnp;
228 silent = gn->type & OP_SILENT;
229 errCheck = !(gn->type & OP_IGNORE);
230 doIt = FALSE;
232 cmdNode = Lst_Member(gn->commands, cmd);
233 cmdStart = Var_Subst(NULL, cmd, gn, FALSE);
236 * brk_string will return an argv with a NULL in av[0], thus causing
237 * execvp to choke and die horribly. Besides, how can we execute a null
238 * command? In any case, we warn the user that the command expanded to
239 * nothing (is this the right thing to do?).
242 if (*cmdStart == '\0') {
243 free(cmdStart);
244 Error("%s expands to empty string", cmd);
245 return(0);
247 cmd = cmdStart;
248 Lst_Replace(cmdNode, cmdStart);
250 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
251 (void)Lst_AtEnd(ENDNode->commands, cmdStart);
252 return(0);
254 if (strcmp(cmdStart, "...") == 0) {
255 gn->type |= OP_SAVE_CMDS;
256 return(0);
259 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
260 switch (*cmd) {
261 case '@':
262 silent = DEBUG(LOUD) ? FALSE : TRUE;
263 break;
264 case '-':
265 errCheck = FALSE;
266 break;
267 case '+':
268 doIt = TRUE;
269 if (!meta[0]) /* we came here from jobs */
270 Compat_Init();
271 break;
273 cmd++;
276 while (isspace((unsigned char)*cmd))
277 cmd++;
279 #if !defined(MAKE_NATIVE)
281 * In a non-native build, the host environment might be weird enough
282 * that it's necessary to go through a shell to get the correct
283 * behaviour. Or perhaps the shell has been replaced with something
284 * that does extra logging, and that should not be bypassed.
286 useShell = TRUE;
287 #else
289 * Search for meta characters in the command. If there are no meta
290 * characters, there's no need to execute a shell to execute the
291 * command.
293 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
294 continue;
296 useShell = (*cp != '\0');
297 #endif
300 * Print the command before echoing if we're not supposed to be quiet for
301 * this one. We also print the command if -n given.
303 if (!silent || NoExecute(gn)) {
304 printf("%s\n", cmd);
305 fflush(stdout);
309 * If we're not supposed to execute any commands, this is as far as
310 * we go...
312 if (!doIt && NoExecute(gn)) {
313 return (0);
315 if (DEBUG(JOB))
316 fprintf(debug_file, "Execute: '%s'\n", cmd);
318 again:
319 if (useShell) {
321 * We need to pass the command off to the shell, typically
322 * because the command contains a "meta" character.
324 static const char *shargv[4];
326 shargv[0] = shellPath;
328 * The following work for any of the builtin shell specs.
330 if (DEBUG(SHELL))
331 shargv[1] = "-xc";
332 else
333 shargv[1] = "-c";
334 shargv[2] = cmd;
335 shargv[3] = NULL;
336 av = shargv;
337 argc = 0;
338 bp = NULL;
339 mav = NULL;
340 } else {
342 * No meta-characters, so no need to exec a shell. Break the command
343 * into words to form an argument vector we can execute.
345 mav = brk_string(cmd, &argc, TRUE, &bp);
346 if (mav == NULL) {
347 useShell = 1;
348 goto again;
350 av = (const char **)mav;
353 local = TRUE;
356 * Fork and execute the single command. If the fork fails, we abort.
358 #if defined(__minix)
359 cpid = fork();
360 #else
361 cpid = vfork();
362 #endif
363 if (cpid < 0) {
364 Fatal("Could not fork");
366 if (cpid == 0) {
367 Check_Cwd(av);
368 Var_ExportVars();
369 if (local)
370 (void)execvp(av[0], (char *const *)UNCONST(av));
371 else
372 (void)execv(av[0], (char *const *)UNCONST(av));
373 execError("exec", av[0]);
374 _exit(1);
376 if (mav)
377 free(mav);
378 if (bp)
379 free(bp);
380 Lst_Replace(cmdNode, NULL);
383 * The child is off and running. Now all we can do is wait...
385 while (1) {
387 while ((retstat = wait(&reason)) != cpid) {
388 if (retstat == -1 && errno != EINTR) {
389 break;
393 if (retstat > -1) {
394 if (WIFSTOPPED(reason)) {
395 status = WSTOPSIG(reason); /* stopped */
396 } else if (WIFEXITED(reason)) {
397 status = WEXITSTATUS(reason); /* exited */
398 if (status != 0) {
399 if (DEBUG(ERROR)) {
400 fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ",
401 gn->name);
402 for (cp = cmd; *cp; ) {
403 if (isspace((unsigned char)*cp)) {
404 fprintf(debug_file, " ");
405 while (isspace((unsigned char)*cp))
406 cp++;
407 } else {
408 fprintf(debug_file, "%c", *cp);
409 cp++;
412 fprintf(debug_file, "\n");
414 fprintf(debug_file, "*** Error code %d", status);
416 } else {
417 status = WTERMSIG(reason); /* signaled */
418 printf("*** Signal %d", status);
422 if (!WIFEXITED(reason) || (status != 0)) {
423 if (errCheck) {
424 gn->made = ERROR;
425 if (keepgoing) {
427 * Abort the current target, but let others
428 * continue.
430 printf(" (continuing)\n");
432 } else {
434 * Continue executing commands for this target.
435 * If we return 0, this will happen...
437 printf(" (ignored)\n");
438 status = 0;
441 break;
442 } else {
443 Fatal("error in wait: %d: %s", retstat, strerror(errno));
444 /*NOTREACHED*/
447 free(cmdStart);
449 return (status);
453 *-----------------------------------------------------------------------
454 * Compat_Make --
455 * Make a target.
457 * Input:
458 * gnp The node to make
459 * pgnp Parent to abort if necessary
461 * Results:
464 * Side Effects:
465 * If an error is detected and not being ignored, the process exits.
467 *-----------------------------------------------------------------------
470 Compat_Make(void *gnp, void *pgnp)
472 GNode *gn = (GNode *)gnp;
473 GNode *pgn = (GNode *)pgnp;
475 if (!meta[0]) /* we came here from jobs */
476 Compat_Init();
477 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
479 * First mark ourselves to be made, then apply whatever transformations
480 * the suffix module thinks are necessary. Once that's done, we can
481 * descend and make all our children. If any of them has an error
482 * but the -k flag was given, our 'make' field will be set FALSE again.
483 * This is our signal to not attempt to do anything but abort our
484 * parent as well.
486 gn->flags |= REMAKE;
487 gn->made = BEINGMADE;
488 if ((gn->type & OP_MADE) == 0)
489 Suff_FindDeps(gn);
490 Lst_ForEach(gn->children, Compat_Make, gn);
491 if ((gn->flags & REMAKE) == 0) {
492 gn->made = ABORTED;
493 pgn->flags &= ~REMAKE;
494 goto cohorts;
497 if (Lst_Member(gn->iParents, pgn) != NULL) {
498 char *p1;
499 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
500 if (p1)
501 free(p1);
505 * All the children were made ok. Now cmtime contains the modification
506 * time of the newest child, we need to find out if we exist and when
507 * we were modified last. The criteria for datedness are defined by the
508 * Make_OODate function.
510 if (DEBUG(MAKE)) {
511 fprintf(debug_file, "Examining %s...", gn->name);
513 if (! Make_OODate(gn)) {
514 gn->made = UPTODATE;
515 if (DEBUG(MAKE)) {
516 fprintf(debug_file, "up-to-date.\n");
518 goto cohorts;
519 } else if (DEBUG(MAKE)) {
520 fprintf(debug_file, "out-of-date.\n");
524 * If the user is just seeing if something is out-of-date, exit now
525 * to tell him/her "yes".
527 if (queryFlag) {
528 exit(1);
532 * We need to be re-made. We also have to make sure we've got a $?
533 * variable. To be nice, we also define the $> variable using
534 * Make_DoAllVar().
536 Make_DoAllVar(gn);
539 * Alter our type to tell if errors should be ignored or things
540 * should not be printed so CompatRunCommand knows what to do.
542 if (Targ_Ignore(gn)) {
543 gn->type |= OP_IGNORE;
545 if (Targ_Silent(gn)) {
546 gn->type |= OP_SILENT;
549 if (Job_CheckCommands(gn, Fatal)) {
551 * Our commands are ok, but we still have to worry about the -t
552 * flag...
554 if (!touchFlag || (gn->type & OP_MAKE)) {
555 curTarg = gn;
556 Lst_ForEach(gn->commands, CompatRunCommand, gn);
557 curTarg = NULL;
558 } else {
559 Job_Touch(gn, gn->type & OP_SILENT);
561 } else {
562 gn->made = ERROR;
565 if (gn->made != ERROR) {
567 * If the node was made successfully, mark it so, update
568 * its modification time and timestamp all its parents. Note
569 * that for .ZEROTIME targets, the timestamping isn't done.
570 * This is to keep its state from affecting that of its parent.
572 gn->made = MADE;
573 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
574 if (!(gn->type & OP_EXEC)) {
575 pgn->flags |= CHILDMADE;
576 Make_TimeStamp(pgn, gn);
578 } else if (keepgoing) {
579 pgn->flags &= ~REMAKE;
580 } else {
581 PrintOnError("\n\nStop.");
582 exit(1);
584 } else if (gn->made == ERROR) {
586 * Already had an error when making this beastie. Tell the parent
587 * to abort.
589 pgn->flags &= ~REMAKE;
590 } else {
591 if (Lst_Member(gn->iParents, pgn) != NULL) {
592 char *p1;
593 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
594 if (p1)
595 free(p1);
597 switch(gn->made) {
598 case BEINGMADE:
599 Error("Graph cycles through %s", gn->name);
600 gn->made = ERROR;
601 pgn->flags &= ~REMAKE;
602 break;
603 case MADE:
604 if ((gn->type & OP_EXEC) == 0) {
605 pgn->flags |= CHILDMADE;
606 Make_TimeStamp(pgn, gn);
608 break;
609 case UPTODATE:
610 if ((gn->type & OP_EXEC) == 0) {
611 Make_TimeStamp(pgn, gn);
613 break;
614 default:
615 break;
619 cohorts:
620 Lst_ForEach(gn->cohorts, Compat_Make, pgnp);
621 return (0);
625 *-----------------------------------------------------------------------
626 * Compat_Run --
627 * Initialize this mode and start making.
629 * Input:
630 * targs List of target nodes to re-create
632 * Results:
633 * None.
635 * Side Effects:
636 * Guess what?
638 *-----------------------------------------------------------------------
640 void
641 Compat_Run(Lst targs)
643 GNode *gn = NULL;/* Current root target */
644 int errors; /* Number of targets not remade due to errors */
646 Compat_Init();
648 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
649 signal(SIGINT, CompatInterrupt);
651 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
652 signal(SIGTERM, CompatInterrupt);
654 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
655 signal(SIGHUP, CompatInterrupt);
657 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
658 signal(SIGQUIT, CompatInterrupt);
661 ENDNode = Targ_FindNode(".END", TARG_CREATE);
662 ENDNode->type = OP_SPECIAL;
664 * If the user has defined a .BEGIN target, execute the commands attached
665 * to it.
667 if (!queryFlag) {
668 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
669 if (gn != NULL) {
670 Compat_Make(gn, gn);
671 if (gn->made == ERROR) {
672 PrintOnError("\n\nStop.");
673 exit(1);
679 * Expand .USE nodes right now, because they can modify the structure
680 * of the tree.
682 Make_ExpandUse(targs);
685 * For each entry in the list of targets to create, call Compat_Make on
686 * it to create the thing. Compat_Make will leave the 'made' field of gn
687 * in one of several states:
688 * UPTODATE gn was already up-to-date
689 * MADE gn was recreated successfully
690 * ERROR An error occurred while gn was being created
691 * ABORTED gn was not remade because one of its inferiors
692 * could not be made due to errors.
694 errors = 0;
695 while (!Lst_IsEmpty (targs)) {
696 gn = (GNode *)Lst_DeQueue(targs);
697 Compat_Make(gn, gn);
699 if (gn->made == UPTODATE) {
700 printf("`%s' is up to date.\n", gn->name);
701 } else if (gn->made == ABORTED) {
702 printf("`%s' not remade because of errors.\n", gn->name);
703 errors += 1;
708 * If the user has defined a .END target, run its commands.
710 if (errors == 0) {
711 Compat_Make(ENDNode, ENDNode);
712 if (gn->made == ERROR) {
713 PrintOnError("\n\nStop.");
714 exit(1);