1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 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'. */
517 if (question_flag
&& !(flags
& COMMANDS_RECURSE
))
520 /* There may be some preceding whitespace left if there
521 was nothing but a backslash on the first line. */
524 /* Figure out an argument list from this command line. */
528 argv
= construct_command_argv (p
, &end
, child
->file
);
530 child
->command_ptr
= NULL
;
534 child
->command_ptr
= end
;
538 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
540 /* Go on to the next command. It might be the recursive one.
541 We construct ARGV only to find the end of the command line. */
543 free ((char *) argv
);
549 /* This line has no commands. Go to the next. */
550 if (job_next_command (child
))
551 start_job_command (child
);
555 /* Print out the command. */
557 if (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
560 /* Tell update_goal_chain that a command has been started on behalf of
561 this target. It is important that this happens here and not in
562 reap_children (where we used to do it), because reap_children might be
563 reaping children from a different target. We want this increment to
564 guaranteedly indicate that a command was started for the dependency
565 chain (i.e., update_file recursion chain) we are processing. */
569 /* If -n was given, recurse to get the next line in the sequence. */
571 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
574 free ((char *) argv
);
575 if (job_next_command (child
))
576 start_job_command (child
);
580 /* Flush the output streams so they won't have things written twice. */
585 /* Set up a bad standard input that reads from a broken pipe. */
589 /* Make a file descriptor that is the read end of a broken pipe.
590 This will be used for some children's standard inputs. */
594 /* Close the write side. */
595 (void) close (pd
[1]);
596 /* Save the read side. */
601 /* Decide whether to give this child the `good' standard input
602 (one that points to the terminal or whatever), or the `bad' one
603 that points to the read side of a broken pipe. */
605 child
->good_stdin
= !good_stdin_used
;
606 if (child
->good_stdin
)
611 /* Set up the environment for the child. */
612 if (child
->environment
== 0)
613 child
->environment
= target_environment (child
->file
);
615 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
618 int is_remote
, id
, used_stdin
;
619 if (start_remote_job (argv
, child
->environment
,
620 child
->good_stdin
? 0 : bad_stdin
,
621 &is_remote
, &id
, &used_stdin
))
625 if (child
->good_stdin
&& !used_stdin
)
627 child
->good_stdin
= 0;
630 child
->remote
= is_remote
;
636 /* Fork the child process. */
639 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
641 #ifdef HAVE_SIGSETMASK
642 (void) sigblock (fatal_signal_mask
);
647 child
->pid
= vfork ();
650 /* We are the child side. */
652 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
653 argv
, child
->environment
);
655 else if (child
->pid
< 0)
659 perror_with_name ("vfork", "");
664 /* We are the parent side. Set the state to
665 say the commands are running and return. */
667 child
->file
->command_state
= cs_running
;
669 /* Free the storage used by the child's argument list. */
672 free ((char *) argv
);
677 child
->file
->update_status
= 1;
678 child
->file
->command_state
= cs_finished
;
681 /* Try to start a child running.
682 Returns nonzero if the child was started (and maybe finished), or zero if
683 the load was too high and the child was put on the `waiting_jobs' chain. */
686 start_waiting_job (c
)
689 /* If we can start a job remotely, we always want to, and don't care about
690 the local load average. We record that the job should be started
691 remotely in C->remote for start_job_command to test. */
693 c
->remote
= start_remote_job_p ();
695 /* If this job is to be started locally, and we are already running
696 some jobs, make this one wait if the load average is too high. */
697 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
699 /* Put this child on the chain of children waiting
700 for the load average to go down. */
701 c
->file
->command_state
= cs_running
;
702 c
->next
= waiting_jobs
;
707 /* Start the first command; reap_children will run later command lines. */
708 start_job_command (c
);
710 switch (c
->file
->command_state
)
715 printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
716 (unsigned long int) c
,
717 c
->pid
, c
->remote
? " (remote)" : "");
719 /* One more job slot is in use. */
725 notice_finished_file (c
->file
);
730 error ("internal error: `%s' command_state == %d in new_job",
731 c
->file
->name
, (int) c
->file
->command_state
);
739 /* Create a `struct child' for FILE and start its commands running. */
743 register struct file
*file
;
745 register struct commands
*cmds
= file
->cmds
;
746 register struct child
*c
;
748 register unsigned int i
;
750 /* Let any previously decided-upon jobs that are waiting
751 for the load to go down start before this new one. */
752 start_waiting_jobs ();
754 /* Reap any children that might have finished recently. */
755 reap_children (0, 0);
757 /* Chop the commands up into lines if they aren't already. */
758 chop_commands (cmds
);
761 /* Wait for a job slot to be freed up. */
762 while (job_slots_used
== job_slots
)
763 reap_children (1, 0);
765 /* Expand the command lines and store the results in LINES. */
766 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
767 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
769 /* Collapse backslash-newline combinations that are inside variable
770 or function references. These are left alone by the parser so
771 that they will appear in the echoing of commands (where they look
772 nice); and collapsed by construct_command_argv when it tokenizes.
773 But letting them survive inside function invocations loses because
774 we don't want the functions to see them as part of the text. */
776 char *in
, *out
, *ref
;
778 /* IN points to where in the line we are scanning.
779 OUT points to where in the line we are writing.
780 When we collapse a backslash-newline combination,
781 IN gets ahead out OUT. */
783 in
= out
= cmds
->command_lines
[i
];
784 while ((ref
= index (in
, '$')) != 0)
786 ++ref
; /* Move past the $. */
789 /* Copy the text between the end of the last chunk
790 we processed (where IN points) and the new chunk
791 we are about to process (where REF points). */
792 bcopy (in
, out
, ref
- in
);
794 /* Move both pointers past the boring stuff. */
798 if (*ref
== '(' || *ref
== '{')
800 char openparen
= *ref
;
801 char closeparen
= openparen
== '(' ? ')' : '}';
805 *out
++ = *in
++; /* Copy OPENPAREN. */
806 /* IN now points past the opening paren or brace.
807 Count parens or braces until it is matched. */
811 if (*in
== closeparen
&& --count
< 0)
813 else if (*in
== '\\' && in
[1] == '\n')
815 /* We have found a backslash-newline inside a
816 variable or function reference. Eat it and
817 any following whitespace. */
820 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
824 /* There were two or more backslashes, so this is
825 not really a continuation line. We don't collapse
826 the quoting backslashes here as is done in
827 collapse_continuations, because the line will
828 be collapsed again after expansion. */
832 /* Skip the backslash, newline and
833 any following whitespace. */
834 in
= next_token (in
+ 2);
836 /* Discard any preceding whitespace that has
837 already been written to the output. */
838 while (out
> ref
&& isblank (out
[-1]))
841 /* Replace it all with a single space. */
847 if (*in
== openparen
)
856 /* There are no more references in this line to worry about.
857 Copy the remaining uninteresting text to the output. */
861 /* Finally, expand the line. */
862 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
866 /* Start the command sequence, record it in a new
867 `struct child', and add that to the chain. */
869 c
= (struct child
*) xmalloc (sizeof (struct child
));
871 c
->command_lines
= lines
;
876 /* Fetch the first command line to be run. */
877 if (! job_next_command (c
))
878 /* There were no commands! */
882 /* The job is now primed. Start it running. */
883 start_waiting_job (c
);
886 /* Since there is only one job slot, make things run linearly.
887 Wait for the child to die, setting the state to `cs_finished'. */
888 while (file
->command_state
== cs_running
)
889 reap_children (1, 0);
893 /* Move CHILD's pointers to the next command for it to execute.
894 Returns nonzero if there is another command. */
897 job_next_command (child
)
900 if (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
902 /* There are no more lines in the expansion of this line. */
903 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
905 /* There are no more lines to be expanded. */
906 child
->command_ptr
= 0;
907 child
->file
->command_state
= cs_finished
;
908 child
->file
->update_status
= 0;
912 /* Get the next line to run. */
913 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
921 extern int getloadavg ();
924 if (max_load_average
< 0)
928 if (getloadavg (&load
, 1) != 1)
930 static int lossage
= -1;
931 /* Complain only once for the same error. */
932 if (lossage
== -1 || errno
!= lossage
)
935 /* An errno value of zero means getloadavg is just unsupported. */
936 error ("cannot enforce load limits on this operating system");
938 perror_with_name ("cannot enforce load limit: ", "getloadavg");
945 return load
>= max_load_average
;
948 /* Start jobs that are waiting for the load to be lower. */
951 start_waiting_jobs ()
955 if (waiting_jobs
== 0)
960 /* Check for recently deceased descendants. */
961 reap_children (0, 0);
963 /* Take a job off the waiting list. */
965 waiting_jobs
= job
->next
;
967 /* Try to start that job. We break out of the loop as soon
968 as start_waiting_job puts one back on the waiting list. */
969 } while (start_waiting_job (job
) && waiting_jobs
!= 0);
972 /* Replace the current process with one executing the command in ARGV.
973 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
974 the environment of the new program. This function does not return. */
977 child_execute_job (stdin_fd
, stdout_fd
, argv
, envp
)
978 int stdin_fd
, stdout_fd
;
982 (void) dup2 (stdin_fd
, 0);
984 (void) dup2 (stdout_fd
, 1);
986 /* Free up file descriptors. */
989 int max
= getdtablesize ();
990 for (d
= 3; d
< max
; ++d
)
994 /* Run the command. */
995 exec_command (argv
, envp
);
998 /* Search PATH for FILE.
999 If successful, store the full pathname in PROGRAM and return 1.
1000 If not sucessful, return zero. */
1003 search_path (file
, path
, program
)
1004 char *file
, *path
, *program
;
1006 if (path
== 0 || path
[0] == '\0')
1007 path
= default_path
;
1009 if (index (file
, '/') != 0)
1011 strcpy (program
, file
);
1018 #ifdef HAVE_GETGROUPS
1019 #ifndef HAVE_UNISTD_H
1020 extern int getgroups ();
1022 static int ngroups
= -1;
1024 static GETGROUPS_T groups
[NGROUPS_MAX
];
1025 #define ngroups_max NGROUPS_MAX
1027 static GETGROUPS_T
*groups
= 0;
1028 static int ngroups_max
;
1031 ngroups_max
= GET_NGROUPS_MAX
;
1032 groups
= (GETGROUPS_T
*) malloc (ngroups_max
* sizeof (GETGROUPS_T
));
1035 if (groups
!= 0 && ngroups
== -1)
1036 ngroups
= getgroups (ngroups_max
, groups
);
1037 #endif /* Have getgroups. */
1039 len
= strlen (file
) + 1;
1046 p
= index (path
, ':');
1048 p
= path
+ strlen (path
);
1051 bcopy (file
, program
, len
);
1054 bcopy (path
, program
, p
- path
);
1055 program
[p
- path
] = '/';
1056 bcopy (file
, program
+ (p
- path
) + 1, len
);
1059 if (stat (program
, &st
) == 0
1060 && S_ISREG (st
.st_mode
))
1062 if (st
.st_uid
== geteuid ())
1063 perm
= (st
.st_mode
& 0100);
1064 else if (st
.st_gid
== getegid ())
1065 perm
= (st
.st_mode
& 0010);
1068 #ifdef HAVE_GETGROUPS
1070 for (i
= 0; i
< ngroups
; ++i
)
1071 if (groups
[i
] == st
.st_gid
)
1074 perm
= (st
.st_mode
& 0010);
1076 #endif /* Have getgroups. */
1077 perm
= (st
.st_mode
& 0001);
1085 } while (*path
!= '\0');
1091 /* Replace the current process with one running the command in ARGV,
1092 with environment ENVP. This function does not return. */
1095 exec_command (argv
, envp
)
1096 char **argv
, **envp
;
1103 for (ep
= envp
; *ep
!= 0; ++ep
)
1105 if (shell
== 0 && !strncmp(*ep
, "SHELL=", 6))
1107 else if (path
== 0 && !strncmp(*ep
, "PATH=", 5))
1109 else if (path
!= 0 && shell
!= 0)
1113 /* Be the user, permanently. */
1116 if (!search_path (argv
[0], path
, program
))
1117 error ("%s: Command not found", argv
[0]);
1120 /* Run the program. */
1121 execve (program
, argv
, envp
);
1123 if (errno
== ENOEXEC
)
1125 PATH_VAR (shell_program
);
1128 shell_path
= default_shell
;
1131 if (search_path (shell
, path
, shell_program
))
1132 shell_path
= shell_program
;
1136 error ("%s: Shell program not found", shell
);
1140 if (shell_path
!= 0)
1146 while (argv
[argc
] != 0)
1149 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
1150 new_argv
[0] = shell_path
;
1151 new_argv
[1] = program
;
1154 new_argv
[1 + argc
] = argv
[argc
];
1158 execve (shell_path
, new_argv
, envp
);
1159 perror_with_name ("execve: ", shell_path
);
1163 perror_with_name ("execve: ", program
);
1169 /* Figure out the argument list necessary to run LINE as a command.
1170 Try to avoid using a shell. This routine handles only ' quoting.
1171 Starting quotes may be escaped with a backslash. If any of the
1172 characters in sh_chars[] is seen, or any of the builtin commands
1173 listed in sh_cmds[] is the first word of a line, the shell is used.
1175 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1176 If *RESTP is NULL, newlines will be ignored.
1178 SHELL is the shell to use, or nil to use the default shell.
1179 IFS is the value of $IFS, or nil (meaning the default). */
1182 construct_command_argv_internal (line
, restp
, shell
, ifs
)
1183 char *line
, **restp
;
1186 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^";
1187 static char *sh_cmds
[] = { "cd", "eval", "exec", "exit", "login",
1188 "logout", "set", "umask", "wait", "while", "for",
1189 "case", "if", ":", ".", "break", "continue",
1190 "export", "read", "readonly", "shift", "times",
1191 "trap", "switch", 0 };
1196 int instring
, word_has_equals
, seen_nonequals
;
1197 char **new_argv
= 0;
1202 /* Make sure not to bother processing an empty line. */
1203 while (isblank (*line
))
1208 /* See if it is safe to parse commands internally. */
1210 shell
= default_shell
;
1211 else if (strcmp (shell
, default_shell
))
1215 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
1216 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
1219 i
= strlen (line
) + 1;
1221 /* More than 1 arg per character is impossible. */
1222 new_argv
= (char **) xmalloc (i
* sizeof (char *));
1224 /* All the args can fit in a buffer as big as LINE is. */
1225 ap
= new_argv
[0] = (char *) xmalloc (i
);
1228 /* I is how many complete arguments have been found. */
1230 instring
= word_has_equals
= seen_nonequals
= 0;
1231 for (p
= line
; *p
!= '\0'; ++p
)
1238 /* Inside a string, just copy any char except a closing quote. */
1244 else if (index (sh_chars
, *p
) != 0)
1245 /* Not inside a string, but it's a special char. */
1248 /* Not a special char. */
1252 /* Equals is a special character in leading words before the
1253 first word with no equals sign in it. This is not the case
1254 with sh -k, but we never get here when using nonstandard
1256 if (! seen_nonequals
)
1258 word_has_equals
= 1;
1263 /* Backslash-newline combinations are eaten. */
1266 /* Eat the backslash, the newline, and following whitespace,
1267 replacing it all with a single space. */
1270 /* If there is a tab after a backslash-newline,
1271 remove it from the source line which will be echoed,
1272 since it was most likely used to line
1273 up the continued line with the previous one. */
1277 if (ap
!= new_argv
[i
])
1278 /* Treat this as a space, ending the arg.
1279 But if it's at the beginning of the arg, it should
1280 just get eaten, rather than becoming an empty arg. */
1283 p
= next_token (p
) - 1;
1285 else if (p
[1] != '\0')
1286 /* Copy and skip the following char. */
1297 /* End of the command line. */
1302 /* Newlines are not special. */
1309 /* We have the end of an argument.
1310 Terminate the text of the argument. */
1314 /* Update SEEN_NONEQUALS, which tells us if every word
1315 heretofore has contained an `='. */
1316 seen_nonequals
|= ! word_has_equals
;
1317 if (word_has_equals
&& ! seen_nonequals
)
1318 /* An `=' in a word before the first
1319 word without one is magical. */
1321 word_has_equals
= 0; /* Prepare for the next word. */
1323 /* If this argument is the command name,
1324 see if it is a built-in shell command.
1325 If so, have the shell handle it. */
1329 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1330 if (streq (sh_cmds
[j
], new_argv
[0]))
1334 /* Ignore multiple whitespace chars. */
1336 /* Next iteration should examine the first nonwhite char. */
1348 /* Let the shell deal with an unterminated quote. */
1351 /* Terminate the last argument and the argument list. */
1354 if (new_argv
[i
][0] != '\0')
1361 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1362 if (streq (sh_cmds
[j
], new_argv
[0]))
1366 if (new_argv
[0] == 0)
1367 /* Line was empty. */
1373 /* We must use the shell. */
1377 /* Free the old argument list we were working on. */
1383 /* SHELL may be a multi-word command. Construct a command line
1384 "SHELL -c LINE", with all special chars in LINE escaped.
1385 Then recurse, expanding this command line to get the final
1388 unsigned int shell_len
= strlen (shell
);
1389 static char minus_c
[] = " -c ";
1390 unsigned int line_len
= strlen (line
);
1392 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
1393 + (line_len
* 2) + 1);
1396 bcopy (shell
, ap
, shell_len
);
1398 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
1399 ap
+= sizeof (minus_c
) - 1;
1400 for (p
= line
; *p
!= '\0'; ++p
)
1402 if (restp
!= NULL
&& *p
== '\n')
1407 else if (*p
== '\\' && p
[1] == '\n')
1409 /* Eat the backslash, the newline, and following whitespace,
1410 replacing it all with a single space (which is escaped
1414 /* If there is a tab after a backslash-newline,
1415 remove it from the source line which will be echoed,
1416 since it was most likely used to line
1417 up the continued line with the previous one. */
1428 if (*p
== '\\' || *p
== '\''
1430 || index (sh_chars
, *p
) != 0)
1436 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
1437 (char *) 0, (char *) 0);
1443 /* Figure out the argument list necessary to run LINE as a command.
1444 Try to avoid using a shell. This routine handles only ' quoting.
1445 Starting quotes may be escaped with a backslash. If any of the
1446 characters in sh_chars[] is seen, or any of the builtin commands
1447 listed in sh_cmds[] is the first word of a line, the shell is used.
1449 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1450 If *RESTP is NULL, newlines will be ignored.
1452 FILE is the target whose commands these are. It is used for
1453 variable expansion for $(SHELL) and $(IFS). */
1456 construct_command_argv (line
, restp
, file
)
1457 char *line
, **restp
;
1464 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
1465 int save
= warn_undefined_variables_flag
;
1466 warn_undefined_variables_flag
= 0;
1468 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
1469 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
1471 warn_undefined_variables_flag
= save
;
1474 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
);