Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / nojobs.c
index ca9b9fa..7763283 100644 (file)
--- a/nojobs.c
+++ b/nojobs.c
@@ -57,9 +57,7 @@
 
 #include "builtins/builtext.h" /* for wait_builtin */
 
-#if !defined (CHILD_MAX)
-#  define CHILD_MAX 32
-#endif
+#define DEFAULT_CHILD_MAX 32
 
 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
 #  define killpg(pg, sig)              kill(-(pg),(sig))
@@ -90,11 +88,12 @@ extern int interactive, interactive_shell, login_shell;
 extern int subshell_environment;
 extern int last_command_exit_value;
 extern int interrupt_immediately;
-extern Function *this_shell_builtin;
+extern sh_builtin_func_t *this_shell_builtin;
 #if defined (HAVE_POSIX_SIGNALS)
 extern sigset_t top_level_mask;
 #endif
 extern procenv_t wait_intr_buf;
+extern int wait_signal_received;
 
 pid_t last_made_pid = NO_PID;
 pid_t last_asynchronous_pid = NO_PID;
@@ -109,12 +108,6 @@ int shell_tty = -1;
    exits from get_tty_state(). */
 int check_window_size;
 
-#if defined (HAVE_WAITPID)
-static void reap_zombie_children ();
-#endif
-
-static int wait_sigint_received;
-
 /* STATUS and FLAGS are only valid if pid != NO_PID
    STATUS is only valid if (flags & PROC_RUNNING) == 0 */
 struct proc_status {
@@ -128,13 +121,37 @@ struct proc_status {
 #define PROC_NOTIFIED  0x02
 #define PROC_ASYNC     0x04
 
-static struct proc_status *pid_list = (struct proc_status *)NULL;
-static int pid_list_size;
-
 /* Return values from find_status_by_pid */
 #define PROC_BAD        -1
 #define PROC_STILL_ALIVE -2
 
+static struct proc_status *pid_list = (struct proc_status *)NULL;
+static int pid_list_size;
+static int wait_sigint_received;
+
+static long child_max = -1L;
+
+static void alloc_pid_list __P((void));
+static int find_proc_slot __P((void));
+static int find_index_by_pid __P((pid_t));
+static int find_status_by_pid __P((pid_t));
+static int process_exit_status __P((WAIT));
+static void set_pid_status __P((pid_t, WAIT));
+static void set_pid_flags __P((pid_t, int));
+static void unset_pid_flags __P((pid_t, int));
+static void add_pid __P((pid_t, int));
+static void mark_dead_jobs_as_notified __P((int));
+
+static void get_new_window_size __P((int));
+static sighandler sigwinch_sighandler __P((int));
+static sighandler wait_sigint_handler __P((int));
+
+#if defined (HAVE_WAITPID)
+static void reap_zombie_children __P((void));
+#endif
+
+static void restore_sigint_handler __P((void));
+
 /* Allocate new, or grow existing PID_LIST. */
 static void
 alloc_pid_list ()
@@ -143,8 +160,7 @@ alloc_pid_list ()
   int old = pid_list_size;
 
   pid_list_size += 10;
-  pid_list = (struct proc_status *)
-    xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
+  pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
 
   /* None of the newly allocated slots have process id's yet. */
   for (i = old; i < pid_list_size; i++)
@@ -291,7 +307,12 @@ mark_dead_jobs_as_notified (force)
        ndead++;
     }
 
-  if (force == 0 && ndead <= CHILD_MAX)
+  if (child_max < 0)
+    child_max = getmaxchild ();
+  if (child_max < 0)
+    child_max = DEFAULT_CHILD_MAX;
+
+  if (force == 0 && ndead <= child_max)
     return;
 
   /* If FORCE == 0, we just mark as many non-running async jobs as notified
@@ -304,7 +325,7 @@ mark_dead_jobs_as_notified (force)
           pid_list[i].pid != last_asynchronous_pid)
        {
          pid_list[i].flags |= PROC_NOTIFIED;
-         if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= CHILD_MAX)
+         if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= child_max)
            break;
        }
     }
@@ -538,6 +559,7 @@ default_tty_job_signals ()
 
 /* Wait for a single pid (PID) and return its exit status.  Called by
    the wait builtin. */
+int
 wait_for_single_pid (pid)
      pid_t pid;
 {
@@ -549,7 +571,7 @@ wait_for_single_pid (pid)
 
   if (pstatus == PROC_BAD)
     {
-      internal_error ("wait: pid %d is not a child of this shell", pid);
+      internal_error ("wait: pid %ld is not a child of this shell", (long)pid);
       return (127);
     }
 
@@ -572,13 +594,16 @@ wait_for_single_pid (pid)
        set_pid_status (got_pid, status);
     }
 
-  set_pid_status (got_pid, status);
-  set_pid_flags (got_pid, PROC_NOTIFIED);
+  if (got_pid > 0)
+    {
+      set_pid_status (got_pid, status);
+      set_pid_flags (got_pid, PROC_NOTIFIED);
+    }
 
   siginterrupt (SIGINT, 0);
   QUIT;
 
-  return (process_exit_status (status));
+  return (got_pid > 0 ? process_exit_status (status) : -1);
 }
 
 /* Wait for all of the shell's children to exit.  Called by the `wait'
@@ -646,6 +671,7 @@ wait_sigint_handler (sig)
       restore_sigint_handler ();
       interrupt_immediately = 0;
       trap_handler (SIGINT);   /* set pending_traps[SIGINT] */
+      wait_signal_received = SIGINT;
       longjmp (wait_intr_buf, 1);
     }
 
@@ -698,12 +724,13 @@ wait_for (pid)
          break;
        }
       else if (got_pid < 0 && errno != EINTR)
-       programming_error ("wait_for(%d): %s", pid, strerror(errno));
+       programming_error ("wait_for(%ld): %s", (long)pid, strerror(errno));
       else if (got_pid > 0)
        set_pid_status (got_pid, status);
     }
 
-  set_pid_status (got_pid, status);
+  if (got_pid > 0)
+    set_pid_status (got_pid, status);
 
 #if defined (HAVE_WAITPID)
   if (got_pid >= 0)
@@ -818,11 +845,13 @@ give_terminal_to (pgrp, force)
 }
 
 /* Stop a pipeline. */
+int
 stop_pipeline (async, ignore)
      int async;
      COMMAND *ignore;
 {
   already_making_children = 0;
+  return 0;
 }
 
 void
@@ -831,6 +860,12 @@ start_pipeline ()
   already_making_children = 1;
 }
 
+void
+stop_making_children ()
+{
+  already_making_children = 0;
+}
+
 int
 get_job_by_pid (pid, block)
      pid_t pid;
@@ -847,7 +882,7 @@ void
 describe_pid (pid)
      pid_t pid;
 {
-  fprintf (stderr, "%d\n", (int) pid);
+  fprintf (stderr, "%ld\n", (long) pid);
 }
 
 void