VirtualBox

source: kBuild/trunk/src/kmk/compat.c@ 35

Last change on this file since 35 was 35, checked in by bird, 22 years ago

emx is kind of working again...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.0 KB
Line 
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#if 0
41static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
42#else
43static const char rcsid[] =
44 "$FreeBSD: src/usr.bin/make/compat.c,v 1.16.2.2 2000/07/01 12:24:21 ps Exp $";
45#endif
46#endif /* not lint */
47
48/*-
49 * compat.c --
50 * The routines in this file implement the full-compatibility
51 * mode of PMake. Most of the special functionality of PMake
52 * is available in this mode. Things not supported:
53 * - different shells.
54 * - friendly variable substitution.
55 *
56 * Interface:
57 * Compat_Run Initialize things for this module and recreate
58 * thems as need creatin'
59 */
60
61#include <stdio.h>
62#include <sys/types.h>
63#include <sys/stat.h>
64#ifdef __IBMC__
65#else
66#include <sys/wait.h>
67#endif
68#if defined(__EMX__)
69#include <sys/process.h>
70#endif
71#if defined(OS2) && defined(__IBMC__)
72#include <process.h>
73#endif
74#include <ctype.h>
75#include <errno.h>
76#include <signal.h>
77#include "make.h"
78#include "hash.h"
79#include "dir.h"
80#include "job.h"
81
82/*
83 * The following array is used to make a fast determination of which
84 * characters are interpreted specially by the shell. If a command
85 * contains any of these characters, it is executed by the shell, not
86 * directly by us.
87 */
88
89static char meta[256];
90
91static GNode *curTarg = NILGNODE;
92static GNode *ENDNode;
93static void CompatInterrupt __P((int));
94static int CompatRunCommand __P((ClientData, ClientData));
95static int CompatMake __P((ClientData, ClientData));
96
97static char *sh_builtin[] = {
98 "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
99 "unalias", "umask", "unset", "wait", ":", 0};
100
101/*-
102 *-----------------------------------------------------------------------
103 * CompatInterrupt --
104 * Interrupt the creation of the current target and remove it if
105 * it ain't precious.
106 *
107 * Results:
108 * None.
109 *
110 * Side Effects:
111 * The target is removed and the process exits. If .INTERRUPT exists,
112 * its commands are run first WITH INTERRUPTS IGNORED..
113 *
114 *-----------------------------------------------------------------------
115 */
116static void
117CompatInterrupt (signo)
118 int signo;
119{
120 GNode *gn;
121
122 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
123 char *p1;
124 char *file = Var_Value (TARGET, curTarg, &p1);
125
126 if (!noExecute && eunlink(file) != -1) {
127 printf ("*** %s removed\n", file);
128 }
129 efree(p1);
130
131 /*
132 * Run .INTERRUPT only if hit with interrupt signal
133 */
134 if (signo == SIGINT) {
135 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
136 if (gn != NILGNODE) {
137 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
138 }
139 }
140
141 }
142 #if !(defined(OS2) && defined(__IBMC__))
143 if (signo == SIGQUIT)
144 exit(signo);
145 #endif
146 (void) signal(signo, SIG_DFL);
147 (void) kill(getpid(), signo);
148}
149
150
151/*-
152 *-----------------------------------------------------------------------
153 * shellneed --
154 *
155 * Results:
156 * Returns 1 if a specified line must be executed by the shell,
157 * 0 if it can be run via execve, and -1 if the command is a no-op.
158 *
159 * Side Effects:
160 * None.
161 *
162 *-----------------------------------------------------------------------
163 */
164static int
165shellneed (cmd)
166 char *cmd;
167{
168 char **av, **p;
169 int ac;
170
171 av = brk_string(cmd, &ac, TRUE);
172 for(p = sh_builtin; *p != 0; p++)
173 if (strcmp(av[1], *p) == 0)
174 return (1);
175 return (0);
176}
177
178
179/*-
180 *-----------------------------------------------------------------------
181 * CompatRunCommand --
182 * Execute the next command for a target. If the command returns an
183 * error, the node's made field is set to ERROR and creation stops.
184 *
185 * Results:
186 * 0 if the command succeeded, 1 if an error occurred.
187 *
188 * Side Effects:
189 * The node's 'made' field may be set to ERROR.
190 *
191 *-----------------------------------------------------------------------
192 */
193static int
194CompatRunCommand (cmdp, gnp)
195 ClientData cmdp; /* Command to execute */
196 ClientData gnp; /* Node from which the command came */
197{
198 char *cmdStart; /* Start of expanded command */
199 register char *cp;
200 Boolean silent, /* Don't print command */
201 errCheck; /* Check errors */
202 int reason; /* Reason for child's death */
203 int status; /* Description of child's death */
204 int cpid; /* Child actually found */
205 ReturnStatus stat; /* Status of fork */
206 LstNode cmdNode; /* Node where current command is located */
207 char **av; /* Argument vector for thing to exec */
208 int argc; /* Number of arguments in av or 0 if not
209 * dynamically allocated */
210 Boolean local; /* TRUE if command should be executed
211 * locally */
212 int internal; /* Various values.. */
213 char *cmd = (char *) cmdp;
214 GNode *gn = (GNode *) gnp;
215
216 /*
217 * Avoid clobbered variable warnings by forcing the compiler
218 * to ``unregister'' variables
219 */
220#if __GNUC__
221 (void) &av;
222 (void) &errCheck;
223#endif
224 silent = gn->type & OP_SILENT;
225 errCheck = !(gn->type & OP_IGNORE);
226
227 cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
228 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
229
230 /*
231 * brk_string will return an argv with a NULL in av[0], thus causing
232 * execvp to choke and die horribly. Besides, how can we execute a null
233 * command? In any case, we warn the user that the command expanded to
234 * nothing (is this the right thing to do?).
235 */
236
237 if (*cmdStart == '\0') {
238 efree(cmdStart);
239 Error("%s expands to empty string", cmd);
240 return(0);
241 } else {
242 cmd = cmdStart;
243 }
244 Lst_Replace (cmdNode, (ClientData)cmdStart);
245
246 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
247 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
248 return(0);
249 } else if (strcmp(cmdStart, "...") == 0) {
250 gn->type |= OP_SAVE_CMDS;
251 return(0);
252 }
253
254 while ((*cmd == '@') || (*cmd == '-')) {
255 if (*cmd == '@') {
256 silent = DEBUG(LOUD) ? FALSE : TRUE;
257 } else {
258 errCheck = FALSE;
259 }
260 cmd++;
261 }
262
263 while (isspace((unsigned char)*cmd))
264 cmd++;
265
266 /*
267 * Search for meta characters in the command. If there are no meta
268 * characters, there's no need to execute a shell to execute the
269 * command.
270 */
271 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
272 continue;
273 }
274
275 /*
276 * Print the command before echoing if we're not supposed to be quiet for
277 * this one. We also print the command if -n given.
278 */
279 if (!silent || noExecute) {
280 printf ("%s\n", cmd);
281 fflush(stdout);
282 }
283
284 /*
285 * If we're not supposed to execute any commands, this is as far as
286 * we go...
287 */
288 if (noExecute) {
289 return (0);
290 }
291
292 if (*cp != '\0') {
293 /*
294 * If *cp isn't the null character, we hit a "meta" character and
295 * need to pass the command off to the shell. We give the shell the
296 * -e flag as well as -c if it's supposed to exit when it hits an
297 * error.
298 */
299 static char *shargv[4] = { "/bin/sh" };
300
301 shargv[1] = (errCheck ? "-ec" : "-c");
302 shargv[2] = cmd;
303 shargv[3] = (char *)NULL;
304 av = shargv;
305 argc = 0;
306 } else if ((internal = shellneed(cmd))) {
307 /*
308 * This command must be passed by the shell for other reasons..
309 * or.. possibly not at all.
310 */
311 static char *shargv[4] = { "/bin/sh" };
312
313 if (internal == -1) {
314 /* Command does not need to be executed */
315 return (0);
316 }
317
318 shargv[1] = (errCheck ? "-ec" : "-c");
319 shargv[2] = cmd;
320 shargv[3] = (char *)NULL;
321 av = shargv;
322 argc = 0;
323 } else {
324 /*
325 * No meta-characters, so no need to exec a shell. Break the command
326 * into words to form an argument vector we can execute.
327 * brk_string sticks our name in av[0], so we have to
328 * skip over it...
329 */
330 av = brk_string(cmd, &argc, TRUE);
331 av += 1;
332 }
333
334 local = TRUE;
335
336 /*
337 * Fork and execute the single command. If the fork fails, we abort.
338 */
339#ifdef OS2 //@todo use klib later!
340 cpid = _spawnvp(P_NOWAIT, av[0], av);
341#else
342 cpid = vfork();
343 if (cpid < 0) {
344 Fatal("Could not fork");
345 }
346 if (cpid == 0) {
347 if (local) {
348 execvp(av[0], av);
349 (void) write (2, av[0], strlen (av[0]));
350 (void) write (2, ":", 1);
351 (void) write (2, strerror(errno), strlen(strerror(errno)));
352 (void) write (2, "\n", 1);
353 } else {
354 (void)execv(av[0], av);
355 }
356 exit(1);
357 }
358#endif
359
360 /*
361 * we need to print out the command associated with this Gnode in
362 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
363 * in main(), Fatal() and DieHorribly(), therefore do not efree it
364 * when debugging.
365 */
366 if (!DEBUG(GRAPH2)) {
367 efree(cmdStart);
368 Lst_Replace (cmdNode, cmdp);
369 }
370
371 /*
372 * The child is off and running. Now all we can do is wait...
373 */
374 while (1) {
375
376 while ((stat = wait(&reason)) != cpid) {
377 if (stat == -1 && errno != EINTR) {
378 break;
379 }
380 }
381
382 if (stat > -1) {
383 if (WIFSTOPPED(reason)) {
384 status = WSTOPSIG(reason); /* stopped */
385 } else if (WIFEXITED(reason)) {
386 status = WEXITSTATUS(reason); /* exited */
387 if (status != 0) {
388 printf ("*** Error code %d", status);
389 }
390 } else {
391 status = WTERMSIG(reason); /* signaled */
392 printf ("*** Signal %d", status);
393 }
394
395
396 if (!WIFEXITED(reason) || (status != 0)) {
397 if (errCheck) {
398 gn->made = ERROR;
399 if (keepgoing) {
400 /*
401 * Abort the current target, but let others
402 * continue.
403 */
404 printf (" (continuing)\n");
405 }
406 } else {
407 /*
408 * Continue executing commands for this target.
409 * If we return 0, this will happen...
410 */
411 printf (" (ignored)\n");
412 status = 0;
413 }
414 }
415 break;
416 } else {
417 Fatal ("error in wait: %d", stat);
418 /*NOTREACHED*/
419 }
420 }
421
422 return (status);
423}
424
425
426/*-
427 *-----------------------------------------------------------------------
428 * CompatMake --
429 * Make a target.
430 *
431 * Results:
432 * 0
433 *
434 * Side Effects:
435 * If an error is detected and not being ignored, the process exits.
436 *
437 *-----------------------------------------------------------------------
438 */
439static int
440CompatMake (gnp, pgnp)
441 ClientData gnp; /* The node to make */
442 ClientData pgnp; /* Parent to abort if necessary */
443{
444 GNode *gn = (GNode *) gnp;
445 GNode *pgn = (GNode *) pgnp;
446 if (gn->type & OP_USE) {
447 Make_HandleUse(gn, pgn);
448 } else if (gn->made == UNMADE) {
449 /*
450 * First mark ourselves to be made, then apply whatever transformations
451 * the suffix module thinks are necessary. Once that's done, we can
452 * descend and make all our children. If any of them has an error
453 * but the -k flag was given, our 'make' field will be set FALSE again.
454 * This is our signal to not attempt to do anything but abort our
455 * parent as well.
456 */
457 gn->make = TRUE;
458 gn->made = BEINGMADE;
459 Suff_FindDeps (gn);
460 Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
461 if (!gn->make) {
462 gn->made = ABORTED;
463 pgn->make = FALSE;
464 return (0);
465 }
466
467 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
468 char *p1;
469 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
470 efree(p1);
471 }
472
473 /*
474 * All the children were made ok. Now cmtime contains the modification
475 * time of the newest child, we need to find out if we exist and when
476 * we were modified last. The criteria for datedness are defined by the
477 * Make_OODate function.
478 */
479 if (DEBUG(MAKE)) {
480 printf("Examining %s...", gn->name);
481 }
482 if (! Make_OODate(gn)) {
483 gn->made = UPTODATE;
484 if (DEBUG(MAKE)) {
485 printf("up-to-date.\n");
486 }
487 return (0);
488 } else if (DEBUG(MAKE)) {
489 printf("out-of-date.\n");
490 }
491
492 /*
493 * If the user is just seeing if something is out-of-date, exit now
494 * to tell him/her "yes".
495 */
496 if (queryFlag) {
497 exit (-1);
498 }
499
500 /*
501 * We need to be re-made. We also have to make sure we've got a $?
502 * variable. To be nice, we also define the $> variable using
503 * Make_DoAllVar().
504 */
505 Make_DoAllVar(gn);
506
507 /*
508 * Alter our type to tell if errors should be ignored or things
509 * should not be printed so CompatRunCommand knows what to do.
510 */
511 if (Targ_Ignore (gn)) {
512 gn->type |= OP_IGNORE;
513 }
514 if (Targ_Silent (gn)) {
515 gn->type |= OP_SILENT;
516 }
517
518 if (Job_CheckCommands (gn, Fatal)) {
519 /*
520 * Our commands are ok, but we still have to worry about the -t
521 * flag...
522 */
523 if (!touchFlag) {
524 curTarg = gn;
525 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
526 curTarg = NILGNODE;
527 } else {
528 Job_Touch (gn, gn->type & OP_SILENT);
529 }
530 } else {
531 gn->made = ERROR;
532 }
533
534 if (gn->made != ERROR) {
535 /*
536 * If the node was made successfully, mark it so, update
537 * its modification time and timestamp all its parents. Note
538 * that for .ZEROTIME targets, the timestamping isn't done.
539 * This is to keep its state from affecting that of its parent.
540 */
541 gn->made = MADE;
542#ifndef RECHECK
543 /*
544 * We can't re-stat the thing, but we can at least take care of
545 * rules where a target depends on a source that actually creates
546 * the target, but only if it has changed, e.g.
547 *
548 * parse.h : parse.o
549 *
550 * parse.o : parse.y
551 * yacc -d parse.y
552 * cc -c y.tab.c
553 * mv y.tab.o parse.o
554 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
555 *
556 * In this case, if the definitions produced by yacc haven't
557 * changed from before, parse.h won't have been updated and
558 * gn->mtime will reflect the current modification time for
559 * parse.h. This is something of a kludge, I admit, but it's a
560 * useful one..
561 *
562 * XXX: People like to use a rule like
563 *
564 * FRC:
565 *
566 * To force things that depend on FRC to be made, so we have to
567 * check for gn->children being empty as well...
568 */
569 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
570 gn->mtime = now;
571 }
572#else
573 /*
574 * This is what Make does and it's actually a good thing, as it
575 * allows rules like
576 *
577 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
578 *
579 * to function as intended. Unfortunately, thanks to the stateless
580 * nature of NFS (and the speed of this program), there are times
581 * when the modification time of a file created on a remote
582 * machine will not be modified before the stat() implied by
583 * the Dir_MTime occurs, thus leading us to believe that the file
584 * is unchanged, wreaking havoc with files that depend on this one.
585 *
586 * I have decided it is better to make too much than to make too
587 * little, so this stuff is commented out unless you're sure it's
588 * ok.
589 * -- ardeb 1/12/88
590 */
591 if (noExecute || Dir_MTime(gn) == 0) {
592 gn->mtime = now;
593 }
594 if (gn->cmtime > gn->mtime)
595 gn->mtime = gn->cmtime;
596 if (DEBUG(MAKE)) {
597 printf("update time: %s\n", Targ_FmtTime(gn->mtime));
598 }
599#endif
600 if (!(gn->type & OP_EXEC)) {
601 pgn->childMade = TRUE;
602 Make_TimeStamp(pgn, gn);
603 }
604 } else if (keepgoing) {
605 pgn->make = FALSE;
606 } else {
607 char *p1;
608
609 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
610 efree(p1);
611 exit (1);
612 }
613 } else if (gn->made == ERROR) {
614 /*
615 * Already had an error when making this beastie. Tell the parent
616 * to abort.
617 */
618 pgn->make = FALSE;
619 } else {
620 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
621 char *p1;
622 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
623 efree(p1);
624 }
625 switch(gn->made) {
626 case BEINGMADE:
627 Error("Graph cycles through %s\n", gn->name);
628 gn->made = ERROR;
629 pgn->make = FALSE;
630 break;
631 case MADE:
632 if ((gn->type & OP_EXEC) == 0) {
633 pgn->childMade = TRUE;
634 Make_TimeStamp(pgn, gn);
635 }
636 break;
637 case UPTODATE:
638 if ((gn->type & OP_EXEC) == 0) {
639 Make_TimeStamp(pgn, gn);
640 }
641 break;
642 default:
643 break;
644 }
645 }
646
647 return (0);
648}
649
650
651/*-
652 *-----------------------------------------------------------------------
653 * Compat_Run --
654 * Initialize this mode and start making.
655 *
656 * Results:
657 * None.
658 *
659 * Side Effects:
660 * Guess what?
661 *
662 *-----------------------------------------------------------------------
663 */
664void
665Compat_Run(targs)
666 Lst targs; /* List of target nodes to re-create */
667{
668 char *cp; /* Pointer to string of shell meta-characters */
669 GNode *gn = NULL;/* Current root target */
670 int errors; /* Number of targets not remade due to errors */
671
672 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
673 signal(SIGINT, CompatInterrupt);
674 }
675 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
676 signal(SIGTERM, CompatInterrupt);
677 }
678 #if !(defined(OS2) && defined(__IBMC__))
679 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
680 signal(SIGHUP, CompatInterrupt);
681 }
682 #endif
683 #if !(defined(OS2) && defined(__IBMC__))
684 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
685 signal(SIGQUIT, CompatInterrupt);
686 }
687 #endif
688
689 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
690 meta[(unsigned char) *cp] = 1;
691 }
692 /*
693 * The null character serves as a sentinel in the string.
694 */
695 meta[0] = 1;
696
697 ENDNode = Targ_FindNode(".END", TARG_CREATE);
698 /*
699 * If the user has defined a .BEGIN target, execute the commands attached
700 * to it.
701 */
702 if (!queryFlag) {
703 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
704 if (gn != NILGNODE) {
705 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
706 if (gn->made == ERROR) {
707 printf("\n\nStop.\n");
708 exit(1);
709 }
710 }
711 }
712
713 /*
714 * For each entry in the list of targets to create, call CompatMake on
715 * it to create the thing. CompatMake will leave the 'made' field of gn
716 * in one of several states:
717 * UPTODATE gn was already up-to-date
718 * MADE gn was recreated successfully
719 * ERROR An error occurred while gn was being created
720 * ABORTED gn was not remade because one of its inferiors
721 * could not be made due to errors.
722 */
723 errors = 0;
724 while (!Lst_IsEmpty (targs)) {
725 gn = (GNode *) Lst_DeQueue (targs);
726 CompatMake (gn, gn);
727
728 if (gn->made == UPTODATE) {
729 printf ("`%s' is up to date.\n", gn->name);
730 } else if (gn->made == ABORTED) {
731 printf ("`%s' not remade because of errors.\n", gn->name);
732 errors += 1;
733 }
734 }
735
736 /*
737 * If the user has defined a .END target, run its commands.
738 */
739 if (errors == 0) {
740 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);
741 }
742}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette