#line 33 "wait.def"
#line 43 "wait.def"
#include <config.h>
#include "../bashtypes.h"
#include <signal.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <chartypes.h>
#include "../bashansi.h"
#include "../shell.h"
#include "../jobs.h"
#include "common.h"
#include "bashgetopt.h"
extern int wait_signal_received;
procenv_t wait_intr_buf;
#define WAIT_RETURN(s) \
do \
{ \
interrupt_immediately = old_interrupt_immediately;\
return (s);\
} \
while (0)
int
wait_builtin (list)
WORD_LIST *list;
{
int status, code;
volatile int old_interrupt_immediately;
USE_VAR(list);
if (no_options (list))
return (EX_USAGE);
list = loptend;
old_interrupt_immediately = interrupt_immediately;
interrupt_immediately++;
code = setjmp (wait_intr_buf);
if (code)
{
status = 128 + wait_signal_received;
WAIT_RETURN (status);
}
if (list == 0)
{
wait_for_background_pids ();
WAIT_RETURN (EXECUTION_SUCCESS);
}
status = EXECUTION_SUCCESS;
while (list)
{
pid_t pid;
char *w;
intmax_t pid_value;
w = list->word->word;
if (DIGIT (*w))
{
if (legal_number (w, &pid_value) && pid_value == (pid_t)pid_value)
{
pid = (pid_t)pid_value;
status = wait_for_single_pid (pid);
}
else
{
sh_badpid (w);
WAIT_RETURN (EXECUTION_FAILURE);
}
}
#if defined (JOB_CONTROL)
else if (*w && *w == '%')
{
int job;
sigset_t set, oset;
BLOCK_CHILD (set, oset);
job = get_job_spec (list);
if (INVALID_JOB (job))
{
if (job != DUP_JOB)
sh_badjob (list->word->word);
UNBLOCK_CHILD (oset);
status = 127;
list = list->next;
continue;
}
UNBLOCK_CHILD (oset);
status = wait_for_job (job);
}
#endif
else
{
sh_badpid (w);
status = EXECUTION_FAILURE;
}
list = list->next;
}
WAIT_RETURN (status);
}