1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* Default path to search for executables. */
26 static char default_path
[] = ":/bin:/usr/bin";
28 /* Default shell to use. */
29 char default_shell
[] = "/bin/sh";
31 /* If NGROUPS_MAX == 0 then try other methods for finding a real value. */
32 #if defined (NGROUPS_MAX) && NGROUPS_MAX == 0
34 #endif /* NGROUPS_MAX == 0 */
38 #define GET_NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
39 #else /* Not POSIX. */
40 #define NGROUPS_MAX NGROUPS
44 #ifdef HAVE_SYS_WAIT_H
49 #define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
50 #else /* Don't have waitpid. */
55 #define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
56 #endif /* Have wait3. */
57 #endif /* Have waitpid. */
59 #if !defined (wait) && !defined (POSIX)
63 #ifndef HAVE_UNION_WAIT
68 #define WTERMSIG(x) ((x) & 0x7f)
71 #define WCOREDUMP(x) ((x) & 0x80)
74 #define WEXITSTATUS(x) (((x) >> 8) & 0xff)
77 #define WIFSIGNALED(x) (WTERMSIG (x) != 0)
80 #define WIFEXITED(x) (WTERMSIG (x) == 0)
83 #else /* Have `union wait'. */
85 #define WAIT_T union wait
87 #define WTERMSIG(x) ((x).w_termsig)
90 #define WCOREDUMP(x) ((x).w_coredump)
93 #define WEXITSTATUS(x) ((x).w_retcode)
96 #define WIFSIGNALED(x) (WTERMSIG(x) != 0)
99 #define WIFEXITED(x) (WTERMSIG(x) == 0)
102 #endif /* Don't have `union wait'. */
105 #ifndef HAVE_UNISTD_H
107 extern int execve ();
108 extern void _exit ();
109 extern int geteuid (), getegid ();
110 extern int setgid (), getgid ();
113 #ifndef getdtablesize
114 #ifdef HAVE_GETDTABLESIZE
115 extern int getdtablesize ();
117 #include <sys/param.h>
118 #define getdtablesize() NOFILE
119 #if !defined (NOFILE) && defined (NOFILES_MAX)
120 /* SCO 3.2 "devsys 4.2" defines NOFILES_{MIN,MAX} in lieu of NOFILE. */
121 #define NOFILE NOFILES_MAX
126 extern int getloadavg ();
127 extern int start_remote_job_p ();
128 extern int start_remote_job (), remote_status ();
130 RETSIGTYPE
child_handler ();
131 static void free_child (), start_job_command ();
132 static int load_too_high (), job_next_command ();
134 /* Chain of all live (or recently deceased) children. */
136 struct child
*children
= 0;
138 /* Number of children currently running. */
140 unsigned int job_slots_used
= 0;
142 /* Nonzero if the `good' standard input is in use. */
144 static int good_stdin_used
= 0;
146 /* Chain of children waiting to run until the load average goes down. */
148 static struct child
*waiting_jobs
= 0;
150 /* Write an error message describing the exit status given in
151 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
152 Append "(ignored)" if IGNORED is nonzero. */
155 child_error (target_name
, exit_code
, exit_sig
, coredump
, ignored
)
157 int exit_code
, exit_sig
, coredump
;
161 error (ignored
? "[%s] Error %d (ignored)" :
163 target_name
, exit_code
);
166 char *coredump_string
= coredump
? " (core dumped)" : "";
167 if (exit_sig
> 0 && exit_sig
< NSIG
)
168 error ("*** [%s] %s%s",
169 target_name
, sys_siglist
[exit_sig
], coredump_string
);
171 error ("*** [%s] Signal %d%s", target_name
, exit_sig
, coredump_string
);
175 static unsigned int dead_children
= 0;
177 /* Notice that a child died.
178 reap_children should be called when convenient. */
186 printf ("Got a SIGCHLD; %d unreaped children.\n", dead_children
);
189 extern int shell_function_pid
, shell_function_completed
;
191 /* Reap dead children, storing the returned status and the new command
192 state (`cs_finished') in the `file' member of the `struct child' for the
193 dead child, and removing the child from the chain. If BLOCK nonzero,
194 reap at least one child, waiting for it to die if necessary. If ERR is
195 nonzero, print an error message first. */
198 reap_children (block
, err
)
203 while ((children
!= 0 || shell_function_pid
!= 0) &&
204 (block
|| dead_children
> 0))
208 int exit_code
, exit_sig
, coredump
;
209 register struct child
*lastc
, *c
;
211 int any_remote
, any_local
;
213 if (err
&& dead_children
== 0)
215 /* We might block for a while, so let the user know why. */
217 error ("*** Waiting for unfinished jobs....");
220 /* We have one less dead child to reap.
221 The test and decrement are not atomic; if it is compiled into:
222 register = dead_children - 1;
223 dead_children = register;
224 a SIGCHLD could come between the two instructions.
225 child_handler increments dead_children.
226 The second instruction here would lose that increment. But the
227 only effect of dead_children being wrong is that we might wait
228 longer than necessary to reap a child, and lose some parallelism;
229 and we might print the "Waiting for unfinished jobs" message above
230 when not necessary. */
232 if (dead_children
!= 0)
236 any_local
= shell_function_pid
!= -1;
237 for (c
= children
; c
!= 0; c
= c
->next
)
239 any_remote
|= c
->remote
;
240 any_local
|= ! c
->remote
;
242 printf ("Live child 0x%08lx PID %d%s\n",
243 (unsigned long int) c
,
244 c
->pid
, c
->remote
? " (remote)" : "");
247 /* First, check for remote children. */
249 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 0);
259 pfatal_with_name ("remote_status");
263 /* No remote children. Check for local children. */
269 pid
= WAIT_NOHANG (&status
);
272 pid
= wait (&status
);
283 pfatal_with_name ("wait");
287 /* No local children. */
288 if (block
&& any_remote
)
290 /* Now try a blocking wait for a remote child. */
291 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 1);
293 goto remote_status_lose
;
295 /* No remote children either. Finally give up. */
298 /* We got a remote child. */
306 /* Chop the status word up. */
307 exit_code
= WEXITSTATUS (status
);
308 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
309 coredump
= WCOREDUMP (status
);
313 /* We got a remote child. */
316 /* Check if this is the child of the `shell' function. */
317 if (!remote
&& pid
== shell_function_pid
)
319 /* It is. Leave an indicator for the `shell' function. */
320 if (exit_sig
== 0 && exit_code
== 127)
321 shell_function_completed
= -1;
323 shell_function_completed
= 1;
327 child_failed
= exit_sig
!= 0 || exit_code
!= 0;
329 /* Search for a child matching the deceased one. */
331 for (c
= children
; c
!= 0; lastc
= c
, c
= c
->next
)
332 if (c
->remote
== remote
&& c
->pid
== pid
)
337 /* An unknown child died. */
339 sprintf (buf
, "Unknown%s job %d", remote
? " remote" : "", pid
);
341 child_error (buf
, exit_code
, exit_sig
, coredump
,
344 error ("%s finished.", buf
);
349 printf ("Reaping %s child 0x%08lx PID %d%s\n",
350 child_failed
? "losing" : "winning",
351 (unsigned long int) c
,
352 c
->pid
, c
->remote
? " (remote)" : "");
354 /* If this child had the good stdin, say it is now free. */
358 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
360 /* The commands failed. Write an error message,
361 delete non-precious targets, and abort. */
362 child_error (c
->file
->name
, exit_code
, exit_sig
, coredump
, 0);
363 c
->file
->update_status
= 1;
365 delete_child_targets (c
);
371 /* The commands failed, but we don't care. */
372 child_error (c
->file
->name
,
373 exit_code
, exit_sig
, coredump
, 1);
377 /* If there are more commands to run, try to start them. */
378 if (job_next_command (c
))
380 if (handling_fatal_signal
)
382 /* Never start new commands while we are dying.
383 Since there are more commands that wanted to be run,
384 the target was not completely remade. So we treat
385 this as if a command had failed. */
386 c
->file
->command_state
= cs_finished
;
387 c
->file
->update_status
= 1;
391 /* Check again whether to start remotely.
392 Whether or not we want to changes over time.
393 Also, start_remote_job may need state set up
394 by start_remote_job_p. */
395 c
->remote
= start_remote_job_p ();
396 start_job_command (c
);
400 switch (c
->file
->command_state
)
403 /* Successfully started. Loop to reap more children. */
407 if (c
->file
->update_status
!= 0)
408 /* We failed to start the commands. */
409 delete_child_targets (c
);
413 error ("internal error: `%s' has bogus command_state \
414 %d in reap_children",
415 c
->file
->name
, (int) c
->file
->command_state
);
421 if (! handling_fatal_signal
)
422 /* Notice if the target of the commands has been changed. */
423 notice_finished_file (c
->file
);
426 printf ("Removing child 0x%08lx PID %d%s from chain.\n",
427 (unsigned long int) c
,
428 c
->pid
, c
->remote
? " (remote)" : "");
430 /* Remove the child from the chain and free it. */
434 lastc
->next
= c
->next
;
435 if (! handling_fatal_signal
) /* Avoid nonreentrancy. */
438 /* There is now another slot open. */
441 /* If the job failed, and the -k flag was not given, die,
442 unless we are already in the process of dying. */
443 if (!err
&& child_failed
&& !keep_going_flag
)
447 /* Only block for one child. */
452 /* Free the storage allocated for CHILD. */
456 register struct child
*child
;
458 if (child
->command_lines
!= 0)
460 register unsigned int i
;
461 for (i
= 0; i
< child
->file
->cmds
->ncommand_lines
; ++i
)
462 free (child
->command_lines
[i
]);
463 free ((char *) child
->command_lines
);
466 if (child
->environment
!= 0)
468 register char **ep
= child
->environment
;
471 free ((char *) child
->environment
);
474 free ((char *) child
);
478 extern sigset_t fatal_signal_set
;
484 sigemptyset (&empty
);
485 sigprocmask (SIG_SETMASK
, &empty
, (sigset_t
*) 0);
489 /* Start a job to run the commands specified in CHILD.
490 CHILD is updated to reflect the commands and ID of the child process. */
493 start_job_command (child
)
494 register struct child
*child
;
496 static int bad_stdin
= -1;
498 int flags
= child
->file
->cmds
->lines_flags
[child
->command_line
- 1];
501 p
= child
->command_ptr
;
502 child
->noerror
= flags
& COMMANDS_NOERROR
;
506 flags
|= COMMANDS_SILENT
;
508 flags
|= COMMANDS_RECURSE
;
511 else if (!isblank (*p
) && *p
!= '+')
516 /* If -q was given, just say that updating `failed'. The exit status of
517 1 tells the user that -q is saying `something to do'; the exit status
518 for a random error is 2. */
519 if (question_flag
&& !(flags
& COMMANDS_RECURSE
))
521 child
->file
->update_status
= 1;
522 child
->file
->command_state
= cs_finished
;
526 /* There may be some preceding whitespace left if there
527 was nothing but a backslash on the first line. */
530 /* Figure out an argument list from this command line. */
534 argv
= construct_command_argv (p
, &end
, child
->file
);
536 child
->command_ptr
= NULL
;
540 child
->command_ptr
= end
;
544 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
546 /* Go on to the next command. It might be the recursive one.
547 We construct ARGV only to find the end of the command line. */
549 free ((char *) argv
);
555 /* This line has no commands. Go to the next. */
556 if (job_next_command (child
))
557 start_job_command (child
);
561 /* Print out the command. */
563 if (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
566 /* Tell update_goal_chain that a command has been started on behalf of
567 this target. It is important that this happens here and not in
568 reap_children (where we used to do it), because reap_children might be
569 reaping children from a different target. We want this increment to
570 guaranteedly indicate that a command was started for the dependency
571 chain (i.e., update_file recursion chain) we are processing. */
575 /* If -n was given, recurse to get the next line in the sequence. */
577 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
580 free ((char *) argv
);
581 if (job_next_command (child
))
582 start_job_command (child
);
586 /* Flush the output streams so they won't have things written twice. */
591 /* Set up a bad standard input that reads from a broken pipe. */
595 /* Make a file descriptor that is the read end of a broken pipe.
596 This will be used for some children's standard inputs. */
600 /* Close the write side. */
601 (void) close (pd
[1]);
602 /* Save the read side. */
607 /* Decide whether to give this child the `good' standard input
608 (one that points to the terminal or whatever), or the `bad' one
609 that points to the read side of a broken pipe. */
611 child
->good_stdin
= !good_stdin_used
;
612 if (child
->good_stdin
)
617 /* Set up the environment for the child. */
618 if (child
->environment
== 0)
619 child
->environment
= target_environment (child
->file
);
621 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
624 int is_remote
, id
, used_stdin
;
625 if (start_remote_job (argv
, child
->environment
,
626 child
->good_stdin
? 0 : bad_stdin
,
627 &is_remote
, &id
, &used_stdin
))
631 if (child
->good_stdin
&& !used_stdin
)
633 child
->good_stdin
= 0;
636 child
->remote
= is_remote
;
642 /* Fork the child process. */
645 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
647 #ifdef HAVE_SIGSETMASK
648 (void) sigblock (fatal_signal_mask
);
653 child
->pid
= vfork ();
656 /* We are the child side. */
658 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
659 argv
, child
->environment
);
661 else if (child
->pid
< 0)
665 perror_with_name ("vfork", "");
670 /* We are the parent side. Set the state to
671 say the commands are running and return. */
673 child
->file
->command_state
= cs_running
;
675 /* Free the storage used by the child's argument list. */
678 free ((char *) argv
);
683 child
->file
->update_status
= 2;
684 child
->file
->command_state
= cs_finished
;
687 /* Try to start a child running.
688 Returns nonzero if the child was started (and maybe finished), or zero if
689 the load was too high and the child was put on the `waiting_jobs' chain. */
692 start_waiting_job (c
)
695 /* If we can start a job remotely, we always want to, and don't care about
696 the local load average. We record that the job should be started
697 remotely in C->remote for start_job_command to test. */
699 c
->remote
= start_remote_job_p ();
701 /* If this job is to be started locally, and we are already running
702 some jobs, make this one wait if the load average is too high. */
703 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
705 /* Put this child on the chain of children waiting
706 for the load average to go down. */
707 c
->file
->command_state
= cs_running
;
708 c
->next
= waiting_jobs
;
713 /* Start the first command; reap_children will run later command lines. */
714 start_job_command (c
);
716 switch (c
->file
->command_state
)
721 printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
722 (unsigned long int) c
,
723 c
->pid
, c
->remote
? " (remote)" : "");
725 /* One more job slot is in use. */
731 notice_finished_file (c
->file
);
736 error ("internal error: `%s' command_state == %d in new_job",
737 c
->file
->name
, (int) c
->file
->command_state
);
745 /* Create a `struct child' for FILE and start its commands running. */
749 register struct file
*file
;
751 register struct commands
*cmds
= file
->cmds
;
752 register struct child
*c
;
754 register unsigned int i
;
756 /* Let any previously decided-upon jobs that are waiting
757 for the load to go down start before this new one. */
758 start_waiting_jobs ();
760 /* Reap any children that might have finished recently. */
761 reap_children (0, 0);
763 /* Chop the commands up into lines if they aren't already. */
764 chop_commands (cmds
);
767 /* Wait for a job slot to be freed up. */
768 while (job_slots_used
== job_slots
)
769 reap_children (1, 0);
771 /* Expand the command lines and store the results in LINES. */
772 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
773 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
775 /* Collapse backslash-newline combinations that are inside variable
776 or function references. These are left alone by the parser so
777 that they will appear in the echoing of commands (where they look
778 nice); and collapsed by construct_command_argv when it tokenizes.
779 But letting them survive inside function invocations loses because
780 we don't want the functions to see them as part of the text. */
782 char *in
, *out
, *ref
;
784 /* IN points to where in the line we are scanning.
785 OUT points to where in the line we are writing.
786 When we collapse a backslash-newline combination,
787 IN gets ahead out OUT. */
789 in
= out
= cmds
->command_lines
[i
];
790 while ((ref
= index (in
, '$')) != 0)
792 ++ref
; /* Move past the $. */
795 /* Copy the text between the end of the last chunk
796 we processed (where IN points) and the new chunk
797 we are about to process (where REF points). */
798 bcopy (in
, out
, ref
- in
);
800 /* Move both pointers past the boring stuff. */
804 if (*ref
== '(' || *ref
== '{')
806 char openparen
= *ref
;
807 char closeparen
= openparen
== '(' ? ')' : '}';
811 *out
++ = *in
++; /* Copy OPENPAREN. */
812 /* IN now points past the opening paren or brace.
813 Count parens or braces until it is matched. */
817 if (*in
== closeparen
&& --count
< 0)
819 else if (*in
== '\\' && in
[1] == '\n')
821 /* We have found a backslash-newline inside a
822 variable or function reference. Eat it and
823 any following whitespace. */
826 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
830 /* There were two or more backslashes, so this is
831 not really a continuation line. We don't collapse
832 the quoting backslashes here as is done in
833 collapse_continuations, because the line will
834 be collapsed again after expansion. */
838 /* Skip the backslash, newline and
839 any following whitespace. */
840 in
= next_token (in
+ 2);
842 /* Discard any preceding whitespace that has
843 already been written to the output. */
844 while (out
> ref
&& isblank (out
[-1]))
847 /* Replace it all with a single space. */
853 if (*in
== openparen
)
862 /* There are no more references in this line to worry about.
863 Copy the remaining uninteresting text to the output. */
867 /* Finally, expand the line. */
868 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
872 /* Start the command sequence, record it in a new
873 `struct child', and add that to the chain. */
875 c
= (struct child
*) xmalloc (sizeof (struct child
));
877 c
->command_lines
= lines
;
882 /* Fetch the first command line to be run. */
883 if (! job_next_command (c
))
884 /* There were no commands! */
888 /* The job is now primed. Start it running. */
889 start_waiting_job (c
);
892 /* Since there is only one job slot, make things run linearly.
893 Wait for the child to die, setting the state to `cs_finished'. */
894 while (file
->command_state
== cs_running
)
895 reap_children (1, 0);
899 /* Move CHILD's pointers to the next command for it to execute.
900 Returns nonzero if there is another command. */
903 job_next_command (child
)
906 if (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
908 /* There are no more lines in the expansion of this line. */
909 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
911 /* There are no more lines to be expanded. */
912 child
->command_ptr
= 0;
913 child
->file
->command_state
= cs_finished
;
914 child
->file
->update_status
= 0;
918 /* Get the next line to run. */
919 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
927 extern int getloadavg ();
930 if (max_load_average
< 0)
934 if (getloadavg (&load
, 1) != 1)
936 static int lossage
= -1;
937 /* Complain only once for the same error. */
938 if (lossage
== -1 || errno
!= lossage
)
941 /* An errno value of zero means getloadavg is just unsupported. */
942 error ("cannot enforce load limits on this operating system");
944 perror_with_name ("cannot enforce load limit: ", "getloadavg");
951 return load
>= max_load_average
;
954 /* Start jobs that are waiting for the load to be lower. */
957 start_waiting_jobs ()
961 if (waiting_jobs
== 0)
966 /* Check for recently deceased descendants. */
967 reap_children (0, 0);
969 /* Take a job off the waiting list. */
971 waiting_jobs
= job
->next
;
973 /* Try to start that job. We break out of the loop as soon
974 as start_waiting_job puts one back on the waiting list. */
975 } while (start_waiting_job (job
) && waiting_jobs
!= 0);
978 /* Replace the current process with one executing the command in ARGV.
979 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
980 the environment of the new program. This function does not return. */
983 child_execute_job (stdin_fd
, stdout_fd
, argv
, envp
)
984 int stdin_fd
, stdout_fd
;
988 (void) dup2 (stdin_fd
, 0);
990 (void) dup2 (stdout_fd
, 1);
992 /* Free up file descriptors. */
995 int max
= getdtablesize ();
996 for (d
= 3; d
< max
; ++d
)
1000 /* Run the command. */
1001 exec_command (argv
, envp
);
1004 /* Search PATH for FILE.
1005 If successful, store the full pathname in PROGRAM and return 1.
1006 If not sucessful, return zero. */
1009 search_path (file
, path
, program
)
1010 char *file
, *path
, *program
;
1012 if (path
== 0 || path
[0] == '\0')
1013 path
= default_path
;
1015 if (index (file
, '/') != 0)
1017 strcpy (program
, file
);
1024 #ifdef HAVE_GETGROUPS
1025 #ifndef HAVE_UNISTD_H
1026 extern int getgroups ();
1028 static int ngroups
= -1;
1030 static GETGROUPS_T groups
[NGROUPS_MAX
];
1031 #define ngroups_max NGROUPS_MAX
1033 static GETGROUPS_T
*groups
= 0;
1034 static int ngroups_max
;
1037 ngroups_max
= GET_NGROUPS_MAX
;
1038 groups
= (GETGROUPS_T
*) malloc (ngroups_max
* sizeof (GETGROUPS_T
));
1041 if (groups
!= 0 && ngroups
== -1)
1042 ngroups
= getgroups (ngroups_max
, groups
);
1043 #endif /* Have getgroups. */
1045 len
= strlen (file
) + 1;
1052 p
= index (path
, ':');
1054 p
= path
+ strlen (path
);
1057 bcopy (file
, program
, len
);
1060 bcopy (path
, program
, p
- path
);
1061 program
[p
- path
] = '/';
1062 bcopy (file
, program
+ (p
- path
) + 1, len
);
1065 if (stat (program
, &st
) == 0
1066 && S_ISREG (st
.st_mode
))
1068 if (st
.st_uid
== geteuid ())
1069 perm
= (st
.st_mode
& 0100);
1070 else if (st
.st_gid
== getegid ())
1071 perm
= (st
.st_mode
& 0010);
1074 #ifdef HAVE_GETGROUPS
1076 for (i
= 0; i
< ngroups
; ++i
)
1077 if (groups
[i
] == st
.st_gid
)
1080 perm
= (st
.st_mode
& 0010);
1082 #endif /* Have getgroups. */
1083 perm
= (st
.st_mode
& 0001);
1091 } while (*path
!= '\0');
1097 /* Replace the current process with one running the command in ARGV,
1098 with environment ENVP. This function does not return. */
1101 exec_command (argv
, envp
)
1102 char **argv
, **envp
;
1109 for (ep
= envp
; *ep
!= 0; ++ep
)
1111 if (shell
== 0 && !strncmp(*ep
, "SHELL=", 6))
1113 else if (path
== 0 && !strncmp(*ep
, "PATH=", 5))
1115 else if (path
!= 0 && shell
!= 0)
1119 /* Be the user, permanently. */
1122 if (!search_path (argv
[0], path
, program
))
1123 error ("%s: Command not found", argv
[0]);
1126 /* Run the program. */
1127 execve (program
, argv
, envp
);
1129 if (errno
== ENOEXEC
)
1131 PATH_VAR (shell_program
);
1134 shell_path
= default_shell
;
1137 if (search_path (shell
, path
, shell_program
))
1138 shell_path
= shell_program
;
1142 error ("%s: Shell program not found", shell
);
1146 if (shell_path
!= 0)
1152 while (argv
[argc
] != 0)
1155 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
1156 new_argv
[0] = shell_path
;
1157 new_argv
[1] = program
;
1160 new_argv
[1 + argc
] = argv
[argc
];
1164 execve (shell_path
, new_argv
, envp
);
1165 perror_with_name ("execve: ", shell_path
);
1169 perror_with_name ("execve: ", program
);
1175 /* Figure out the argument list necessary to run LINE as a command.
1176 Try to avoid using a shell. This routine handles only ' quoting.
1177 Starting quotes may be escaped with a backslash. If any of the
1178 characters in sh_chars[] is seen, or any of the builtin commands
1179 listed in sh_cmds[] is the first word of a line, the shell is used.
1181 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1182 If *RESTP is NULL, newlines will be ignored.
1184 SHELL is the shell to use, or nil to use the default shell.
1185 IFS is the value of $IFS, or nil (meaning the default). */
1188 construct_command_argv_internal (line
, restp
, shell
, ifs
)
1189 char *line
, **restp
;
1192 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^";
1193 static char *sh_cmds
[] = { "cd", "eval", "exec", "exit", "login",
1194 "logout", "set", "umask", "wait", "while", "for",
1195 "case", "if", ":", ".", "break", "continue",
1196 "export", "read", "readonly", "shift", "times",
1197 "trap", "switch", 0 };
1202 int instring
, word_has_equals
, seen_nonequals
;
1203 char **new_argv
= 0;
1208 /* Make sure not to bother processing an empty line. */
1209 while (isblank (*line
))
1214 /* See if it is safe to parse commands internally. */
1216 shell
= default_shell
;
1217 else if (strcmp (shell
, default_shell
))
1221 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
1222 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
1225 i
= strlen (line
) + 1;
1227 /* More than 1 arg per character is impossible. */
1228 new_argv
= (char **) xmalloc (i
* sizeof (char *));
1230 /* All the args can fit in a buffer as big as LINE is. */
1231 ap
= new_argv
[0] = (char *) xmalloc (i
);
1234 /* I is how many complete arguments have been found. */
1236 instring
= word_has_equals
= seen_nonequals
= 0;
1237 for (p
= line
; *p
!= '\0'; ++p
)
1245 /* Inside a string, just copy any char except a closing quote
1246 or a backslash-newline combination. */
1249 else if (*p
== '\\' && p
[1] == '\n')
1250 goto swallow_escaped_newline
;
1251 else if (*p
== '\n' && restp
!= NULL
)
1253 /* End of the command line. */
1260 else if (index (sh_chars
, *p
) != 0)
1261 /* Not inside a string, but it's a special char. */
1264 /* Not a special char. */
1268 /* Equals is a special character in leading words before the
1269 first word with no equals sign in it. This is not the case
1270 with sh -k, but we never get here when using nonstandard
1272 if (! seen_nonequals
)
1274 word_has_equals
= 1;
1279 /* Backslash-newline combinations are eaten. */
1282 swallow_escaped_newline
:
1284 /* Eat the backslash, the newline, and following whitespace,
1285 replacing it all with a single space. */
1288 /* If there is a tab after a backslash-newline,
1289 remove it from the source line which will be echoed,
1290 since it was most likely used to line
1291 up the continued line with the previous one. */
1299 if (ap
!= new_argv
[i
])
1300 /* Treat this as a space, ending the arg.
1301 But if it's at the beginning of the arg, it should
1302 just get eaten, rather than becoming an empty arg. */
1305 p
= next_token (p
) - 1;
1308 else if (p
[1] != '\0')
1309 /* Copy and skip the following char. */
1320 /* End of the command line. */
1325 /* Newlines are not special. */
1332 /* We have the end of an argument.
1333 Terminate the text of the argument. */
1337 /* Update SEEN_NONEQUALS, which tells us if every word
1338 heretofore has contained an `='. */
1339 seen_nonequals
|= ! word_has_equals
;
1340 if (word_has_equals
&& ! seen_nonequals
)
1341 /* An `=' in a word before the first
1342 word without one is magical. */
1344 word_has_equals
= 0; /* Prepare for the next word. */
1346 /* If this argument is the command name,
1347 see if it is a built-in shell command.
1348 If so, have the shell handle it. */
1352 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1353 if (streq (sh_cmds
[j
], new_argv
[0]))
1357 /* Ignore multiple whitespace chars. */
1359 /* Next iteration should examine the first nonwhite char. */
1371 /* Let the shell deal with an unterminated quote. */
1374 /* Terminate the last argument and the argument list. */
1377 if (new_argv
[i
][0] != '\0')
1384 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1385 if (streq (sh_cmds
[j
], new_argv
[0]))
1389 if (new_argv
[0] == 0)
1390 /* Line was empty. */
1396 /* We must use the shell. */
1400 /* Free the old argument list we were working on. */
1406 /* SHELL may be a multi-word command. Construct a command line
1407 "SHELL -c LINE", with all special chars in LINE escaped.
1408 Then recurse, expanding this command line to get the final
1411 unsigned int shell_len
= strlen (shell
);
1412 static char minus_c
[] = " -c ";
1413 unsigned int line_len
= strlen (line
);
1415 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
1416 + (line_len
* 2) + 1);
1419 bcopy (shell
, ap
, shell_len
);
1421 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
1422 ap
+= sizeof (minus_c
) - 1;
1423 for (p
= line
; *p
!= '\0'; ++p
)
1425 if (restp
!= NULL
&& *p
== '\n')
1430 else if (*p
== '\\' && p
[1] == '\n')
1432 /* Eat the backslash, the newline, and following whitespace,
1433 replacing it all with a single space (which is escaped
1437 /* If there is a tab after a backslash-newline,
1438 remove it from the source line which will be echoed,
1439 since it was most likely used to line
1440 up the continued line with the previous one. */
1451 if (*p
== '\\' || *p
== '\''
1453 || index (sh_chars
, *p
) != 0)
1459 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
1460 (char *) 0, (char *) 0);
1466 /* Figure out the argument list necessary to run LINE as a command.
1467 Try to avoid using a shell. This routine handles only ' quoting.
1468 Starting quotes may be escaped with a backslash. If any of the
1469 characters in sh_chars[] is seen, or any of the builtin commands
1470 listed in sh_cmds[] is the first word of a line, the shell is used.
1472 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1473 If *RESTP is NULL, newlines will be ignored.
1475 FILE is the target whose commands these are. It is used for
1476 variable expansion for $(SHELL) and $(IFS). */
1479 construct_command_argv (line
, restp
, file
)
1480 char *line
, **restp
;
1487 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
1488 int save
= warn_undefined_variables_flag
;
1489 warn_undefined_variables_flag
= 0;
1491 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
1492 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
1494 warn_undefined_variables_flag
= save
;
1497 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
);