]> git.ipfire.org Git - thirdparty/bash.git/commitdiff
commit bash-20090416 snapshot
authorChet Ramey <chet.ramey@case.edu>
Fri, 9 Dec 2011 01:08:14 +0000 (20:08 -0500)
committerChet Ramey <chet.ramey@case.edu>
Fri, 9 Dec 2011 01:08:14 +0000 (20:08 -0500)
25 files changed:
CWRU/CWRU.chlog
CWRU/CWRU.chlog~
MANIFEST
MANIFEST~
doc/bashref.texi
include/posixselect.h [new file with mode: 0644]
include/posixselect.h~ [new file with mode: 0644]
jobs.c
jobs.c~
jobs.h
jobs.h~ [new file with mode: 0644]
lib/readline/input.c
lib/readline/input.c~ [new file with mode: 0644]
lib/readline/parens.c
lib/readline/parens.c~ [new file with mode: 0644]
lib/readline/posixselect.h [new symlink]
lib/sh/fpurge.c
lib/sh/fpurge.c.old [new file with mode: 0644]
lib/sh/fpurge.c~ [new file with mode: 0644]
lib/sh/input_avail.c
lib/sh/input_avail.c~ [new file with mode: 0644]
tests/RUN-ONE-TEST
trap.c
trap.c.save1 [new file with mode: 0644]
trap.c~

index 6e710684e8cfc1b03814c2249a50b3c07b25c9ea..940675b8c3d519fe82b10a388865ff2bc4b781ab 100644 (file)
@@ -7934,3 +7934,34 @@ parse.y
          character can change to a state where a reserved word is legal,
          since it is not a shell meta character.  Fixes bug reported by
          Bernd Eggink <monoped@sudrala.de>.
+
+                                  4/17
+                                  ----
+jobs.c
+       - new functions to save and restore the pgrp_pipe (since there's only
+         one): save_pgrp_pipe and restore_pgrp_pipe
+
+trap.c
+       - run_debug_trap now saves and restores the pgrp_pipe before and
+         after calling the debug trap
+       - run_debug_trap now makes sure the terminal is owned by the pipeline
+         pgrp after the debug trap runs.  Rest of fix for bug reported by
+         Oleksly Melnyk <o.melnyk@upc.ua> (lex@upc.ca)
+
+                                  4/19
+                                  ----
+include/posixselect.h
+       - new include file, encapsulates select(2) includes and defines for
+         bash and readline.  Inspired by patch from Mike Frysinger
+         <vapier@gentoo.org>
+
+lib/sh/input_avail.c
+        - include "posixselect.h"
+
+lib/readline/{input,parens}.c
+       - include "posixselect.h" instead of using inline includes
+       - use new USEC_TO_TIMEVAL define to make sure that values for timeouts
+         greater than one second are handled properly
+
+lib/sh/fpurge.c
+       - updated implementation, taken from gnulib
index c7950e41a1ce791db0795bd13322266bf4fdc849..5ffb8c49bfec06fd1293bc4237b3d72be4c8f297 100644 (file)
@@ -7926,3 +7926,39 @@ lib/readline/terminal.c
        - in rl_resize_terminal, don't call rl_redisplay_after_sigwinch() if
          we're already in the middle of redisplay (RL_STATE_REDISPLAYING).
          Fix for bug reported by Nicolai Lissner <nlissne@linux01.org>
+
+                                  4/15
+                                  ----
+parse.y
+       - fix parse_comsub to add check for \n when seeing whether the current
+         character can change to a state where a reserved word is legal,
+         since it is not a shell meta character.  Fixes bug reported by
+         Bernd Eggink <monoped@sudrala.de>.
+
+                                  4/17
+                                  ----
+jobs.c
+       - new functions to save and restore the pgrp_pipe (since there's only
+         one): save_pgrp_pipe and restore_pgrp_pipe
+
+trap.c
+       - run_debug_trap now saves and restores the pgrp_pipe before and
+         after calling the debug trap
+       - run_debug_trap now makes sure the terminal is owned by the pipeline
+         pgrp after the debug trap runs.  Rest of fix for bug reported by
+         Oleksly Melnyk <o.melnyk@upc.ua> (lex@upc.ca)
+
+                                  4/19
+                                  ----
+include/posixselect.h
+       - new include file, encapsulates select(2) includes and defines for
+         bash and readline.  Inspired by patch from Mike Frysinger
+         <vapier@gentoo.org>
+
+lib/sh/input_avail.c
+        - include "posixselect.h"
+
+lib/readline/{input,parens}.c
+       - include "posixselect.h" instead of using inline includes
+       - use new USEC_TO_TIMEVAL define to make sure that values for timeouts
+         greater than one second are handled properly
index a724e35109829d05905d7334ef7e7f6c958937c9..2bf59d9b2592c1665d244d6856a268b8e6aa7560 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -215,6 +215,7 @@ include/memalloc.h  f
 include/ocache.h       f
 include/posixdir.h     f
 include/posixjmp.h     f
+include/posixselect.h  f
 include/posixstat.h    f
 include/posixtime.h    f
 include/posixwait.h    f
index 0e6ceb60a586d1c819be32e043e16bd7717e5513..a724e35109829d05905d7334ef7e7f6c958937c9 100644 (file)
--- a/MANIFEST~
+++ b/MANIFEST~
@@ -1036,6 +1036,7 @@ tests/trap.right  f
 tests/trap1.sub                f       755
 tests/trap2.sub                f       755
 tests/trap2a.sub       f       755
+tests/trap3.sub                f
 tests/type.tests       f
 tests/type.right       f
 tests/type1.sub                f
index bc8e1c8af57683a80c0b97c9f6c35e543499a321..ca9b8467e493d515e2b93793b87206409f4c1b92 100644 (file)
@@ -7323,7 +7323,7 @@ But first, you should
 make sure that it really is a bug, and that it appears in the latest
 version of Bash.
 The latest version of Bash is always available for FTP from
-@uref{ftp://ftp.gnu.org/pub/bash/}.
+@uref{ftp://ftp.gnu.org/pub/gnu/bash/}.
 
 Once you have determined that a bug actually exists, use the
 @code{bashbug} command to submit a bug report.
diff --git a/include/posixselect.h b/include/posixselect.h
new file mode 100644 (file)
index 0000000..da6a1ac
--- /dev/null
@@ -0,0 +1,47 @@
+/* posixselect.h -- wrapper for select(2) includes and definitions */
+
+/* Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This file is part of GNU Bash, the Bourne Again SHell.
+
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _POSIXSELECT_H_
+#define _POSIXSELECT_H_
+
+#if defined (FD_SET) && !defined (HAVE_SELECT)
+#  define HAVE_SELECT 1
+#endif
+
+#if defined (HAVE_SELECT)
+#  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
+#    include <sys/time.h>
+#  endif
+#endif /* HAVE_SELECT */
+#if defined (HAVE_SYS_SELECT_H)
+#  include <sys/select.h>
+#endif
+
+#ifndef USEC_PER_SEC
+#  define USEC_PER_SEC 1000000
+#endif
+
+#define USEC_TO_TIMEVAL(us, tv) \
+do { \
+  (tv).tv_sec = (us) / USEC_PER_SEC; \
+  (tv).tv_usec = (us) % USEC_PER_SEC; \
+} while (0)
+
+#endif /* _POSIXSELECT_H_ */
diff --git a/include/posixselect.h~ b/include/posixselect.h~
new file mode 100644 (file)
index 0000000..471c6d7
--- /dev/null
@@ -0,0 +1,24 @@
+/* posixselect.h -- wrapper for select(2) includes and definitions */
+
+/* Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This file is part of GNU Bash, the Bourne Again SHell.
+
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _POSIXSELECT_H_
+#define _POSIXSELECT_H_
+
+#endif /* _POSIXSELECT_H_ */
diff --git a/jobs.c b/jobs.c
index 7c82c536dc661195a73a3d6858076a20db43a4f9..dcc1fefba5b9cc1fcdac861fe3ff576340fa081f 100644 (file)
--- a/jobs.c
+++ b/jobs.c
@@ -4203,4 +4203,23 @@ close_pgrp_pipe ()
   sh_closepipe (pgrp_pipe);
 }
 
+void
+save_pgrp_pipe (p, clear)
+     int *p;
+     int clear;
+{
+  p[0] = pgrp_pipe[0];
+  p[1] = pgrp_pipe[1];
+  if (clear)
+    pgrp_pipe[0] = pgrp_pipe[1] = -1;
+}
+
+void
+restore_pgrp_pipe (p)
+     int *p;
+{
+  pgrp_pipe[0] = p[0];
+  pgrp_pipe[1] = p[1];
+}
+
 #endif /* PGRP_PIPE */
diff --git a/jobs.c~ b/jobs.c~
index e3e514bd69749f9353373d8e018850631b4e609e..10614d3cb06c70cbbd282e5d3aa82e2ef1375b1c 100644 (file)
--- a/jobs.c~
+++ b/jobs.c~
@@ -427,7 +427,6 @@ void
 save_pipeline (clear)
      int clear;
 {
-itrace("save_pipeline (%d)", clear);
   saved_pipeline = the_pipeline;
   if (clear)
     the_pipeline = (PROCESS *)NULL;
@@ -440,7 +439,6 @@ restore_pipeline (discard)
 {
   PROCESS *old_pipeline;
 
-itrace("restore_pipeline (%d)", discard);
   old_pipeline = the_pipeline;
   the_pipeline = saved_pipeline;
   already_making_children = saved_already_making_children;
@@ -457,6 +455,7 @@ start_pipeline ()
       cleanup_the_pipeline ();
       pipeline_pgrp = 0;
 #if defined (PGRP_PIPE)
+itrace("start_pipeline: cleaning up existing pipeline: closing %d %d", pgrp_pipe[0], pgrp_pipe[1]);
       sh_closepipe (pgrp_pipe);
 #endif
     }
@@ -466,6 +465,7 @@ start_pipeline ()
     {
       if (pipe (pgrp_pipe) == -1)
        sys_error (_("start_pipeline: pgrp pipe"));
+itrace("start_pipeline: pgrp_pipe: %d %d", pgrp_pipe[0], pgrp_pipe[1]);
     }
 #endif
 }
@@ -487,6 +487,7 @@ stop_pipeline (async, deferred)
 
 #if defined (PGRP_PIPE)
   /* The parent closes the process group synchronization pipe. */
+itrace("stop_pipeline: close pgrp_pipe %d %d", pgrp_pipe[0], pgrp_pipe[1]);
   sh_closepipe (pgrp_pipe);
 #endif
 
@@ -594,7 +595,6 @@ stop_pipeline (async, deferred)
       newjob->j_cleanup = (sh_vptrfunc_t *)NULL;
       newjob->cleanarg = (PTR_T) NULL;
 
-itrace("adding the_pipeline (%s) as job index %d", newjob->pipe->command, i);
       jobs[i] = newjob;
       if (newjob->state == JDEAD && (newjob->flags & J_FOREGROUND))
        setjstatus (i);
@@ -4206,4 +4206,23 @@ close_pgrp_pipe ()
   sh_closepipe (pgrp_pipe);
 }
 
+void
+save_pgrp_pipe (p, clear)
+     int *p;
+     int clear;
+{
+  p[0] = pgrp_pipe[0];
+  p[1] = pgrp_pipe[1];
+  if (clear)
+    pgrp_pipe[0] = pgrp_pipe[1] = -1;
+}
+
+void
+restore_pgrp_pipe (p)
+     int *p;
+{
+  pgrp_pipe[0] = p[0];
+  pgrp_pipe[1] = p[1];
+}
+
 #endif /* PGRP_PIPE */
diff --git a/jobs.h b/jobs.h
index 6e82f762c09bb001f4ca7e2bb1512d11c20b2289..65de773b856e669c2e45265124d194fb17035860 100644 (file)
--- a/jobs.h
+++ b/jobs.h
@@ -235,6 +235,8 @@ extern void default_tty_job_signals __P((void));
 extern void init_job_stats __P((void));
 
 extern void close_pgrp_pipe __P((void));
+extern void save_pgrp_pipe __P((int *, int));
+extern void restore_pgrp_pipe __P((int *));
 
 #if defined (JOB_CONTROL)
 extern int job_control;
diff --git a/jobs.h~ b/jobs.h~
new file mode 100644 (file)
index 0000000..6e82f76
--- /dev/null
+++ b/jobs.h~
@@ -0,0 +1,243 @@
+/* jobs.h -- structures and definitions used by the jobs.c file. */
+
+/* Copyright (C) 1993-2009 Free Software Foundation, Inc.
+
+   This file is part of GNU Bash, the Bourne Again SHell.
+
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#if !defined (_JOBS_H_)
+#  define _JOBS_H_
+
+#include "quit.h"
+#include "siglist.h"
+
+#include "stdc.h"
+
+#include "posixwait.h"
+
+/* Defines controlling the fashion in which jobs are listed. */
+#define JLIST_STANDARD       0
+#define JLIST_LONG          1
+#define JLIST_PID_ONLY      2
+#define JLIST_CHANGED_ONLY   3
+#define JLIST_NONINTERACTIVE 4
+
+/* I looked it up.  For pretty_print_job ().  The real answer is 24. */
+#define LONGEST_SIGNAL_DESC 24
+
+/* The max time to sleep while retrying fork() on EAGAIN failure */
+#define FORKSLEEP_MAX  16
+
+/* We keep an array of jobs.  Each entry in the array is a linked list
+   of processes that are piped together.  The first process encountered is
+   the group leader. */
+
+/* Values for the `running' field of a struct process. */
+#define PS_DONE                0
+#define PS_RUNNING     1
+#define PS_STOPPED     2
+#define PS_RECYCLED    4
+
+/* Each child of the shell is remembered in a STRUCT PROCESS.  A circular
+   chain of such structures is a pipeline. */
+typedef struct process {
+  struct process *next;        /* Next process in the pipeline.  A circular chain. */
+  pid_t pid;           /* Process ID. */
+  WAIT status;         /* The status of this command as returned by wait. */
+  int running;         /* Non-zero if this process is running. */
+  char *command;       /* The particular program that is running. */
+} PROCESS;
+
+/* PALIVE really means `not exited' */
+#define PSTOPPED(p)    (WIFSTOPPED((p)->status))
+#define PRUNNING(p)    ((p)->running == PS_RUNNING)
+#define PALIVE(p)      (PRUNNING(p) || PSTOPPED(p))
+
+#define PEXITED(p)     ((p)->running == PS_DONE)
+#if defined (RECYCLES_PIDS)
+#  define PRECYCLED(p) ((p)->running == PS_RECYCLED)
+#else
+#  define PRECYCLED(p) (0)
+#endif
+#define PDEADPROC(p)   (PEXITED(p) || PRECYCLED(p))
+
+#define get_job_by_jid(ind)    (jobs[(ind)])
+
+/* A description of a pipeline's state. */
+typedef enum { JNONE = -1, JRUNNING = 1, JSTOPPED = 2, JDEAD = 4, JMIXED = 8 } JOB_STATE;
+#define JOBSTATE(job)  (jobs[(job)]->state)
+#define J_JOBSTATE(j)  ((j)->state)
+
+#define STOPPED(j)     (jobs[(j)]->state == JSTOPPED)
+#define RUNNING(j)     (jobs[(j)]->state == JRUNNING)
+#define DEADJOB(j)     (jobs[(j)]->state == JDEAD)
+
+#define INVALID_JOB(j) ((j) < 0 || (j) >= js.j_jobslots || get_job_by_jid(j) == 0)
+
+/* Values for the FLAGS field in the JOB struct below. */
+#define J_FOREGROUND 0x01 /* Non-zero if this is running in the foreground.  */
+#define J_NOTIFIED   0x02 /* Non-zero if already notified about job state.   */
+#define J_JOBCONTROL 0x04 /* Non-zero if this job started under job control. */
+#define J_NOHUP      0x08 /* Don't send SIGHUP to job if shell gets SIGHUP. */
+#define J_STATSAVED  0x10 /* A process in this job had had status saved via $! */
+#define J_ASYNC             0x20 /* Job was started asynchronously */
+
+#define IS_FOREGROUND(j)       ((jobs[j]->flags & J_FOREGROUND) != 0)
+#define IS_NOTIFIED(j)         ((jobs[j]->flags & J_NOTIFIED) != 0)
+#define IS_JOBCONTROL(j)       ((jobs[j]->flags & J_JOBCONTROL) != 0)
+#define IS_ASYNC(j)            ((jobs[j]->flags & J_ASYNC) != 0)
+
+typedef struct job {
+  char *wd;       /* The working directory at time of invocation. */
+  PROCESS *pipe;   /* The pipeline of processes that make up this job. */
+  pid_t pgrp;     /* The process ID of the process group (necessary). */
+  JOB_STATE state; /* The state that this job is in. */
+  int flags;      /* Flags word: J_NOTIFIED, J_FOREGROUND, or J_JOBCONTROL. */
+#if defined (JOB_CONTROL)
+  COMMAND *deferred;   /* Commands that will execute when this job is done. */
+  sh_vptrfunc_t *j_cleanup; /* Cleanup function to call when job marked JDEAD */
+  PTR_T cleanarg;      /* Argument passed to (*j_cleanup)() */
+#endif /* JOB_CONTROL */
+} JOB;
+
+struct jobstats {
+  /* limits */
+  long c_childmax;
+  /* child process statistics */
+  int c_living;                /* running or stopped child processes */
+  int c_reaped;                /* exited child processes still in jobs list */
+  int c_injobs;                /* total number of child processes in jobs list */
+  /* child process totals */
+  int c_totforked;     /* total number of children this shell has forked */
+  int c_totreaped;     /* total number of children this shell has reaped */
+  /* job counters and indices */
+  int j_jobslots;      /* total size of jobs array */
+  int j_lastj;         /* last (newest) job allocated */
+  int j_firstj;                /* first (oldest) job allocated */
+  int j_njobs;         /* number of non-NULL jobs in jobs array */
+  int j_ndead;         /* number of JDEAD jobs in jobs array */
+  /* */
+  int j_current;       /* current job */
+  int j_previous;      /* previous job */
+  /* */
+  JOB *j_lastmade;     /* last job allocated by stop_pipeline */
+  JOB *j_lastasync;    /* last async job allocated by stop_pipeline */
+};
+
+struct pidstat {
+ struct pidstat *next;
+ pid_t pid;
+ int status;
+};
+
+struct bgpids {
+  struct pidstat *list;
+  struct pidstat *end;
+  int npid;
+};
+
+#define NO_JOB  -1     /* An impossible job array index. */
+#define DUP_JOB -2     /* A possible return value for get_job_spec (). */
+#define BAD_JOBSPEC -3 /* Bad syntax for job spec. */
+
+/* A value which cannot be a process ID. */
+#define NO_PID (pid_t)-1
+
+/* System calls. */
+#if !defined (HAVE_UNISTD_H)
+extern pid_t fork (), getpid (), getpgrp ();
+#endif /* !HAVE_UNISTD_H */
+
+/* Stuff from the jobs.c file. */
+extern struct jobstats js;
+
+extern pid_t original_pgrp, shell_pgrp, pipeline_pgrp;
+extern pid_t last_made_pid, last_asynchronous_pid;
+extern int asynchronous_notification;
+
+extern JOB **jobs;
+
+extern void making_children __P((void));
+extern void stop_making_children __P((void));
+extern void cleanup_the_pipeline __P((void));
+extern void save_pipeline __P((int));
+extern void restore_pipeline __P((int));
+extern void start_pipeline __P((void));
+extern int stop_pipeline __P((int, COMMAND *));
+
+extern void delete_job __P((int, int));
+extern void nohup_job __P((int));
+extern void delete_all_jobs __P((int));
+extern void nohup_all_jobs __P((int));
+
+extern int count_all_jobs __P((void));
+
+extern void terminate_current_pipeline __P((void));
+extern void terminate_stopped_jobs __P((void));
+extern void hangup_all_jobs __P((void));
+extern void kill_current_pipeline __P((void));
+
+#if defined (__STDC__) && defined (pid_t)
+extern int get_job_by_pid __P((int, int));
+extern void describe_pid __P((int));
+#else
+extern int get_job_by_pid __P((pid_t, int));
+extern void describe_pid __P((pid_t));
+#endif
+
+extern void list_one_job __P((JOB *, int, int, int));
+extern void list_all_jobs __P((int));
+extern void list_stopped_jobs __P((int));
+extern void list_running_jobs __P((int));
+
+extern pid_t make_child __P((char *, int));
+
+extern int get_tty_state __P((void));
+extern int set_tty_state __P((void));
+
+extern int wait_for_single_pid __P((pid_t));
+extern void wait_for_background_pids __P((void));
+extern int wait_for __P((pid_t));
+extern int wait_for_job __P((int));
+
+extern void notify_and_cleanup __P((void));
+extern void reap_dead_jobs __P((void));
+extern int start_job __P((int, int));
+extern int kill_pid __P((pid_t, int, int));
+extern int initialize_job_control __P((int));
+extern void initialize_job_signals __P((void));
+extern int give_terminal_to __P((pid_t, int));
+
+extern void run_sigchld_trap __P((int));
+
+extern void unfreeze_jobs_list __P((void));
+extern int set_job_control __P((int));
+extern void without_job_control __P((void));
+extern void end_job_control __P((void));
+extern void restart_job_control __P((void));
+extern void set_sigchld_handler __P((void));
+extern void ignore_tty_job_signals __P((void));
+extern void default_tty_job_signals __P((void));
+
+extern void init_job_stats __P((void));
+
+extern void close_pgrp_pipe __P((void));
+
+#if defined (JOB_CONTROL)
+extern int job_control;
+#endif
+
+#endif /* _JOBS_H_ */
index 320c4bc7f06bbd9f4242bcd44028d0c7f140e908..b5876da9b40fd153e731f38f4617df8ddbee56c7 100644 (file)
 #  include "ansi_stdlib.h"
 #endif /* HAVE_STDLIB_H */
 
-#if defined (HAVE_SELECT)
-#  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
-#    include <sys/time.h>
-#  endif
-#endif /* HAVE_SELECT */
-#if defined (HAVE_SYS_SELECT_H)
-#  include <sys/select.h>
-#endif
+#include "posixselect.h"
 
 #if defined (FIONREAD_IN_SYS_IOCTL)
 #  include <sys/ioctl.h>
@@ -190,8 +183,7 @@ rl_gather_tyi ()
   FD_ZERO (&exceptfds);
   FD_SET (tty, &readfds);
   FD_SET (tty, &exceptfds);
-  timeout.tv_sec = 0;
-  timeout.tv_usec = _keyboard_input_timeout;
+  USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
   result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
   if (result <= 0)
     return 0;  /* Nothing to read. */
diff --git a/lib/readline/input.c~ b/lib/readline/input.c~
new file mode 100644 (file)
index 0000000..65b7726
--- /dev/null
@@ -0,0 +1,595 @@
+/* input.c -- character input functions for readline. */
+
+/* Copyright (C) 1994-2009 Free Software Foundation, Inc.
+
+   This file is part of the GNU Readline Library (Readline), a library
+   for reading lines of text with interactive input and history editing.      
+
+   Readline is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Readline is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Readline.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#define READLINE_LIBRARY
+
+#if defined (__TANDEM)
+#  include <floss.h>
+#endif
+
+#if defined (HAVE_CONFIG_H)
+#  include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <fcntl.h>
+#if defined (HAVE_SYS_FILE_H)
+#  include <sys/file.h>
+#endif /* HAVE_SYS_FILE_H */
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif /* HAVE_UNISTD_H */
+
+#if defined (HAVE_STDLIB_H)
+#  include <stdlib.h>
+#else
+#  include "ansi_stdlib.h"
+#endif /* HAVE_STDLIB_H */
+
+#include "posixselect.h"
+
+#if defined (FIONREAD_IN_SYS_IOCTL)
+#  include <sys/ioctl.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+
+#if !defined (errno)
+extern int errno;
+#endif /* !errno */
+
+/* System-specific feature definitions and include files. */
+#include "rldefs.h"
+#include "rlmbutil.h"
+
+/* Some standard library routines. */
+#include "readline.h"
+
+#include "rlprivate.h"
+#include "rlshell.h"
+#include "xmalloc.h"
+
+/* What kind of non-blocking I/O do we have? */
+#if !defined (O_NDELAY) && defined (O_NONBLOCK)
+#  define O_NDELAY O_NONBLOCK  /* Posix style */
+#endif
+
+/* Non-null means it is a pointer to a function to run while waiting for
+   character input. */
+rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
+
+rl_getc_func_t *rl_getc_function = rl_getc;
+
+static int _keyboard_input_timeout = 100000;           /* 0.1 seconds; it's in usec */
+
+static int ibuffer_space PARAMS((void));
+static int rl_get_char PARAMS((int *));
+static int rl_gather_tyi PARAMS((void));
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Character Input Buffering                   */
+/*                                                                 */
+/* **************************************************************** */
+
+static int pop_index, push_index;
+static unsigned char ibuffer[512];
+static int ibuffer_len = sizeof (ibuffer) - 1;
+
+#define any_typein (push_index != pop_index)
+
+int
+_rl_any_typein ()
+{
+  return any_typein;
+}
+
+/* Return the amount of space available in the buffer for stuffing
+   characters. */
+static int
+ibuffer_space ()
+{
+  if (pop_index > push_index)
+    return (pop_index - push_index - 1);
+  else
+    return (ibuffer_len - (push_index - pop_index));
+}
+
+/* Get a key from the buffer of characters to be read.
+   Return the key in KEY.
+   Result is KEY if there was a key, or 0 if there wasn't. */
+static int
+rl_get_char (key)
+     int *key;
+{
+  if (push_index == pop_index)
+    return (0);
+
+  *key = ibuffer[pop_index++];
+#if 0
+  if (pop_index >= ibuffer_len)
+#else
+  if (pop_index > ibuffer_len)
+#endif
+    pop_index = 0;
+
+  return (1);
+}
+
+/* Stuff KEY into the *front* of the input buffer.
+   Returns non-zero if successful, zero if there is
+   no space left in the buffer. */
+int
+_rl_unget_char (key)
+     int key;
+{
+  if (ibuffer_space ())
+    {
+      pop_index--;
+      if (pop_index < 0)
+       pop_index = ibuffer_len;
+      ibuffer[pop_index] = key;
+      return (1);
+    }
+  return (0);
+}
+
+int
+_rl_pushed_input_available ()
+{
+  return (push_index != pop_index);
+}
+
+/* If a character is available to be read, then read it and stuff it into
+   IBUFFER.  Otherwise, just return.  Returns number of characters read
+   (0 if none available) and -1 on error (EIO). */
+static int
+rl_gather_tyi ()
+{
+  int tty;
+  register int tem, result;
+  int chars_avail, k;
+  char input;
+#if defined(HAVE_SELECT)
+  fd_set readfds, exceptfds;
+  struct timeval timeout;
+#endif
+
+  chars_avail = 0;
+  tty = fileno (rl_instream);
+
+#if defined (HAVE_SELECT)
+  FD_ZERO (&readfds);
+  FD_ZERO (&exceptfds);
+  FD_SET (tty, &readfds);
+  FD_SET (tty, &exceptfds);
+  timeout.tv_sec = 0;
+  timeout.tv_usec = _keyboard_input_timeout;
+  result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
+  if (result <= 0)
+    return 0;  /* Nothing to read. */
+#endif
+
+  result = -1;
+#if defined (FIONREAD)
+  errno = 0;
+  result = ioctl (tty, FIONREAD, &chars_avail);
+  if (result == -1 && errno == EIO)
+    return -1;
+#endif
+
+#if defined (O_NDELAY)
+  if (result == -1)
+    {
+      tem = fcntl (tty, F_GETFL, 0);
+
+      fcntl (tty, F_SETFL, (tem | O_NDELAY));
+      chars_avail = read (tty, &input, 1);
+
+      fcntl (tty, F_SETFL, tem);
+      if (chars_avail == -1 && errno == EAGAIN)
+       return 0;
+      if (chars_avail == 0)    /* EOF */
+       {
+         rl_stuff_char (EOF);
+         return (0);
+       }
+    }
+#endif /* O_NDELAY */
+
+#if defined (__MINGW32__)
+  /* Use getch/_kbhit to check for available console input, in the same way
+     that we read it normally. */
+   chars_avail = isatty (tty) ? _kbhit () : 0;
+   result = 0;
+#endif
+
+  /* If there's nothing available, don't waste time trying to read
+     something. */
+  if (chars_avail <= 0)
+    return 0;
+
+  tem = ibuffer_space ();
+
+  if (chars_avail > tem)
+    chars_avail = tem;
+
+  /* One cannot read all of the available input.  I can only read a single
+     character at a time, or else programs which require input can be
+     thwarted.  If the buffer is larger than one character, I lose.
+     Damn! */
+  if (tem < ibuffer_len)
+    chars_avail = 0;
+
+  if (result != -1)
+    {
+      while (chars_avail--)
+       {
+         RL_CHECK_SIGNALS ();
+         k = (*rl_getc_function) (rl_instream);
+         if (rl_stuff_char (k) == 0)
+           break;                      /* some problem; no more room */
+         if (k == NEWLINE || k == RETURN)
+           break;
+       }
+    }
+  else
+    {
+      if (chars_avail)
+       rl_stuff_char (input);
+    }
+
+  return 1;
+}
+
+int
+rl_set_keyboard_input_timeout (u)
+     int u;
+{
+  int o;
+
+  o = _keyboard_input_timeout;
+  if (u >= 0)
+    _keyboard_input_timeout = u;
+  return (o);
+}
+
+/* Is there input available to be read on the readline input file
+   descriptor?  Only works if the system has select(2) or FIONREAD.
+   Uses the value of _keyboard_input_timeout as the timeout; if another
+   readline function wants to specify a timeout and not leave it up to
+   the user, it should use _rl_input_queued(timeout_value_in_microseconds)
+   instead. */
+int
+_rl_input_available ()
+{
+#if defined(HAVE_SELECT)
+  fd_set readfds, exceptfds;
+  struct timeval timeout;
+#endif
+#if !defined (HAVE_SELECT) && defined(FIONREAD)
+  int chars_avail;
+#endif
+  int tty;
+
+  tty = fileno (rl_instream);
+
+#if defined (HAVE_SELECT)
+  FD_ZERO (&readfds);
+  FD_ZERO (&exceptfds);
+  FD_SET (tty, &readfds);
+  FD_SET (tty, &exceptfds);
+  timeout.tv_sec = 0;
+  timeout.tv_usec = _keyboard_input_timeout;
+  return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
+#else
+
+#if defined (FIONREAD)
+  if (ioctl (tty, FIONREAD, &chars_avail) == 0)
+    return (chars_avail);
+#endif
+
+#endif
+
+#if defined (__MINGW32__)
+  if (isatty (tty))
+    return (_kbhit ());
+#endif
+
+  return 0;
+}
+
+int
+_rl_input_queued (t)
+     int t;
+{
+  int old_timeout, r;
+
+  old_timeout = rl_set_keyboard_input_timeout (t);
+  r = _rl_input_available ();
+  rl_set_keyboard_input_timeout (old_timeout);
+  return r;
+}
+
+void
+_rl_insert_typein (c)
+     int c;     
+{      
+  int key, t, i;
+  char *string;
+
+  i = key = 0;
+  string = (char *)xmalloc (ibuffer_len + 1);
+  string[i++] = (char) c;
+
+  while ((t = rl_get_char (&key)) &&
+        _rl_keymap[key].type == ISFUNC &&
+        _rl_keymap[key].function == rl_insert)
+    string[i++] = key;
+
+  if (t)
+    _rl_unget_char (key);
+
+  string[i] = '\0';
+  rl_insert_text (string);
+  xfree (string);
+}
+
+/* Add KEY to the buffer of characters to be read.  Returns 1 if the
+   character was stuffed correctly; 0 otherwise. */
+int
+rl_stuff_char (key)
+     int key;
+{
+  if (ibuffer_space () == 0)
+    return 0;
+
+  if (key == EOF)
+    {
+      key = NEWLINE;
+      rl_pending_input = EOF;
+      RL_SETSTATE (RL_STATE_INPUTPENDING);
+    }
+  ibuffer[push_index++] = key;
+#if 0
+  if (push_index >= ibuffer_len)
+#else
+  if (push_index > ibuffer_len)
+#endif
+    push_index = 0;
+
+  return 1;
+}
+
+/* Make C be the next command to be executed. */
+int
+rl_execute_next (c)
+     int c;
+{
+  rl_pending_input = c;
+  RL_SETSTATE (RL_STATE_INPUTPENDING);
+  return 0;
+}
+
+/* Clear any pending input pushed with rl_execute_next() */
+int
+rl_clear_pending_input ()
+{
+  rl_pending_input = 0;
+  RL_UNSETSTATE (RL_STATE_INPUTPENDING);
+  return 0;
+}
+
+/* **************************************************************** */
+/*                                                                 */
+/*                          Character Input                        */
+/*                                                                 */
+/* **************************************************************** */
+
+/* Read a key, including pending input. */
+int
+rl_read_key ()
+{
+  int c;
+
+  rl_key_sequence_length++;
+
+  if (rl_pending_input)
+    {
+      c = rl_pending_input;
+      rl_clear_pending_input ();
+    }
+  else
+    {
+      /* If input is coming from a macro, then use that. */
+      if (c = _rl_next_macro_key ())
+       return (c);
+
+      /* If the user has an event function, then call it periodically. */
+      if (rl_event_hook)
+       {
+         while (rl_event_hook && rl_get_char (&c) == 0)
+           {
+             (*rl_event_hook) ();
+             RL_CHECK_SIGNALS ();
+             if (rl_done)              /* XXX - experimental */
+               return ('\n');
+             if (rl_gather_tyi () < 0) /* XXX - EIO */
+               {
+                 rl_done = 1;
+                 return ('\n');
+               }
+           }
+       }
+      else
+       {
+         if (rl_get_char (&c) == 0)
+           c = (*rl_getc_function) (rl_instream);
+         RL_CHECK_SIGNALS ();
+       }
+    }
+
+  return (c);
+}
+
+int
+rl_getc (stream)
+     FILE *stream;
+{
+  int result;
+  unsigned char c;
+
+  while (1)
+    {
+      RL_CHECK_SIGNALS ();
+
+#if defined (__MINGW32__)
+      if (isatty (fileno (stream)))
+       return (getch ());
+#endif
+      result = read (fileno (stream), &c, sizeof (unsigned char));
+
+      if (result == sizeof (unsigned char))
+       return (c);
+
+      /* If zero characters are returned, then the file that we are
+        reading from is empty!  Return EOF in that case. */
+      if (result == 0)
+       return (EOF);
+
+#if defined (__BEOS__)
+      if (errno == EINTR)
+       continue;
+#endif
+
+#if defined (EWOULDBLOCK)
+#  define X_EWOULDBLOCK EWOULDBLOCK
+#else
+#  define X_EWOULDBLOCK -99
+#endif
+
+#if defined (EAGAIN)
+#  define X_EAGAIN EAGAIN
+#else
+#  define X_EAGAIN -99
+#endif
+
+      if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
+       {
+         if (sh_unset_nodelay_mode (fileno (stream)) < 0)
+           return (EOF);
+         continue;
+       }
+
+#undef X_EWOULDBLOCK
+#undef X_EAGAIN
+
+      /* If the error that we received was SIGINT, then try again,
+        this is simply an interrupted system call to read ().
+        Otherwise, some error ocurred, also signifying EOF. */
+      if (errno != EINTR)
+       return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
+    }
+}
+
+#if defined (HANDLE_MULTIBYTE)
+/* read multibyte char */
+int
+_rl_read_mbchar (mbchar, size)
+     char *mbchar;
+     int size;
+{
+  int mb_len, c;
+  size_t mbchar_bytes_length;
+  wchar_t wc;
+  mbstate_t ps, ps_back;
+
+  memset(&ps, 0, sizeof (mbstate_t));
+  memset(&ps_back, 0, sizeof (mbstate_t));
+
+  mb_len = 0;  
+  while (mb_len < size)
+    {
+      RL_SETSTATE(RL_STATE_MOREINPUT);
+      c = rl_read_key ();
+      RL_UNSETSTATE(RL_STATE_MOREINPUT);
+
+      if (c < 0)
+       break;
+
+      mbchar[mb_len++] = c;
+
+      mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
+      if (mbchar_bytes_length == (size_t)(-1))
+       break;          /* invalid byte sequence for the current locale */
+      else if (mbchar_bytes_length == (size_t)(-2))
+       {
+         /* shorted bytes */
+         ps = ps_back;
+         continue;
+       } 
+      else if (mbchar_bytes_length == 0)
+       {
+         mbchar[0] = '\0';     /* null wide character */
+         mb_len = 1;
+         break;
+       }
+      else if (mbchar_bytes_length > (size_t)(0))
+       break;
+    }
+
+  return mb_len;
+}
+
+/* Read a multibyte-character string whose first character is FIRST into
+   the buffer MB of length MLEN.  Returns the last character read, which
+   may be FIRST.  Used by the search functions, among others.  Very similar
+   to _rl_read_mbchar. */
+int
+_rl_read_mbstring (first, mb, mlen)
+     int first;
+     char *mb;
+     int mlen;
+{
+  int i, c;
+  mbstate_t ps;
+
+  c = first;
+  memset (mb, 0, mlen);
+  for (i = 0; c >= 0 && i < mlen; i++)
+    {
+      mb[i] = (char)c;
+      memset (&ps, 0, sizeof (mbstate_t));
+      if (_rl_get_char_len (mb, &ps) == -2)
+       {
+         /* Read more for multibyte character */
+         RL_SETSTATE (RL_STATE_MOREINPUT);
+         c = rl_read_key ();
+         RL_UNSETSTATE (RL_STATE_MOREINPUT);
+       }
+      else
+       break;
+    }
+  return c;
+}
+#endif /* HANDLE_MULTIBYTE */
index f15d26f16e6189061fdf5e7dca79503bb2501f18..9c9848838fdfd6c38f22a8cace78308b54ac6b42 100644 (file)
 #  include <unistd.h>
 #endif
 
-#if defined (FD_SET) && !defined (HAVE_SELECT)
-#  define HAVE_SELECT
-#endif
-
-#if defined (HAVE_SELECT)
-#  include <sys/time.h>
-#endif /* HAVE_SELECT */
-#if defined (HAVE_SYS_SELECT_H)
-#  include <sys/select.h>
-#endif
+#include "posixselect.h"
 
 #if defined (HAVE_STRING_H)
 #  include <string.h>
@@ -130,8 +121,7 @@ rl_insert_close (count, invoking_key)
 
       FD_ZERO (&readfds);
       FD_SET (fileno (rl_instream), &readfds);
-      timer.tv_sec = 0;
-      timer.tv_usec = _paren_blink_usec;
+      USEC_TO_TIMEVAL (_paren_blink_usec, timer);
 
       orig_point = rl_point;
       rl_point = match_point;
diff --git a/lib/readline/parens.c~ b/lib/readline/parens.c~
new file mode 100644 (file)
index 0000000..d6f7a1b
--- /dev/null
@@ -0,0 +1,174 @@
+/* parens.c -- implementation of matching parentheses feature. */
+
+/* Copyright (C) 1987, 1989, 1992-2009 Free Software Foundation, Inc.
+
+   This file is part of the GNU Readline Library (Readline), a library
+   for reading lines of text with interactive input and history editing.      
+
+   Readline is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Readline is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Readline.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#define READLINE_LIBRARY
+
+#if defined (__TANDEM)
+#  include <floss.h>
+#endif
+
+#include "rlconf.h"
+
+#if defined (HAVE_CONFIG_H)
+#  include <config.h>
+#endif
+
+#include <stdio.h>
+#include <sys/types.h>
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif
+
+#include "posixselect.h"
+
+#if defined (HAVE_STRING_H)
+#  include <string.h>
+#else /* !HAVE_STRING_H */
+#  include <strings.h>
+#endif /* !HAVE_STRING_H */
+
+#if !defined (strchr) && !defined (__STDC__)
+extern char *strchr (), *strrchr ();
+#endif /* !strchr && !__STDC__ */
+
+#include "readline.h"
+#include "rlprivate.h"
+
+static int find_matching_open PARAMS((char *, int, int));
+
+/* Non-zero means try to blink the matching open parenthesis when the
+   close parenthesis is inserted. */
+#if defined (HAVE_SELECT)
+int rl_blink_matching_paren = 1;
+#else /* !HAVE_SELECT */
+int rl_blink_matching_paren = 0;
+#endif /* !HAVE_SELECT */
+
+static int _paren_blink_usec = 500000;
+
+/* Change emacs_standard_keymap to have bindings for paren matching when
+   ON_OR_OFF is 1, change them back to self_insert when ON_OR_OFF == 0. */
+void
+_rl_enable_paren_matching (on_or_off)
+     int on_or_off;
+{
+  if (on_or_off)
+    {  /* ([{ */
+      rl_bind_key_in_map (')', rl_insert_close, emacs_standard_keymap);
+      rl_bind_key_in_map (']', rl_insert_close, emacs_standard_keymap);
+      rl_bind_key_in_map ('}', rl_insert_close, emacs_standard_keymap);
+    }
+  else
+    {  /* ([{ */
+      rl_bind_key_in_map (')', rl_insert, emacs_standard_keymap);
+      rl_bind_key_in_map (']', rl_insert, emacs_standard_keymap);
+      rl_bind_key_in_map ('}', rl_insert, emacs_standard_keymap);
+    }
+}
+
+int
+rl_set_paren_blink_timeout (u)
+     int u;
+{
+  int o;
+
+  o = _paren_blink_usec;
+  if (u > 0)
+    _paren_blink_usec = u;
+  return (o);
+}
+
+int
+rl_insert_close (count, invoking_key)
+     int count, invoking_key;
+{
+  if (rl_explicit_arg || !rl_blink_matching_paren)
+    _rl_insert_char (count, invoking_key);
+  else
+    {
+#if defined (HAVE_SELECT)
+      int orig_point, match_point, ready;
+      struct timeval timer;
+      fd_set readfds;
+
+      _rl_insert_char (1, invoking_key);
+      (*rl_redisplay_function) ();
+      match_point =
+       find_matching_open (rl_line_buffer, rl_point - 2, invoking_key);
+
+      /* Emacs might message or ring the bell here, but I don't. */
+      if (match_point < 0)
+       return -1;
+
+      FD_ZERO (&readfds);
+      FD_SET (fileno (rl_instream), &readfds);
+      timer.tv_sec = 0;
+      timer.tv_usec = _paren_blink_usec;
+
+      orig_point = rl_point;
+      rl_point = match_point;
+      (*rl_redisplay_function) ();
+      ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer);
+      rl_point = orig_point;
+#else /* !HAVE_SELECT */
+      _rl_insert_char (count, invoking_key);
+#endif /* !HAVE_SELECT */
+    }
+  return 0;
+}
+
+static int
+find_matching_open (string, from, closer)
+     char *string;
+     int from, closer;
+{
+  register int i;
+  int opener, level, delimiter;
+
+  switch (closer)
+    {
+    case ']': opener = '['; break;
+    case '}': opener = '{'; break;
+    case ')': opener = '('; break;
+    default:
+      return (-1);
+    }
+
+  level = 1;                   /* The closer passed in counts as 1. */
+  delimiter = 0;               /* Delimited state unknown. */
+
+  for (i = from; i > -1; i--)
+    {
+      if (delimiter && (string[i] == delimiter))
+       delimiter = 0;
+      else if (rl_basic_quote_characters && strchr (rl_basic_quote_characters, string[i]))
+       delimiter = string[i];
+      else if (!delimiter && (string[i] == closer))
+       level++;
+      else if (!delimiter && (string[i] == opener))
+       level--;
+
+      if (!level)
+       break;
+    }
+  return (i);
+}
diff --git a/lib/readline/posixselect.h b/lib/readline/posixselect.h
new file mode 120000 (symlink)
index 0000000..56a0a2c
--- /dev/null
@@ -0,0 +1 @@
+../../include/posixselect.h
\ No newline at end of file
index fd093b8f16c0760a969fea0a340f295da2d5b15f..f9e1b9dcd0d8bd7411bdaeee42eee01051aa6329 100644 (file)
@@ -31,7 +31,7 @@
 #endif
 extern int fpurge __P((FILE *stream));
 
-#if HAVE___FPURGE                   /* glibc >= 2.2, Solaris >= 7 */
+#if HAVE___FPURGE                   /* glibc >= 2.2, Haiku, Solaris >= 7 */
 # include <stdio_ext.h>
 #endif
 #include <stdlib.h>
@@ -39,13 +39,13 @@ extern int fpurge __P((FILE *stream));
 int
 fpurge (FILE *fp)
 {
-#if HAVE___FPURGE                   /* glibc >= 2.2, Solaris >= 7 */
+#if HAVE___FPURGE                   /* glibc >= 2.2, Haiku, Solaris >= 7 */
 
   __fpurge (fp);
   /* The __fpurge function does not have a return value.  */
   return 0;
 
-#elif HAVE_FPURGE                   /* FreeBSD, NetBSD, OpenBSD, MacOS X */
+#elif HAVE_FPURGE                   /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X */
 
   /* Call the system's fpurge function.  */
 # undef fpurge
@@ -53,7 +53,7 @@ fpurge (FILE *fp)
   extern int fpurge (FILE *);
 # endif
   int result = fpurge (fp);
-# if defined __sferror              /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
+# if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
   if (result == 0)
     /* Correct the invariants that fpurge broke.
        <stdio.h> on BSD systems says:
@@ -71,7 +71,7 @@ fpurge (FILE *fp)
   /* Most systems provide FILE as a struct and the necessary bitmask in
      <stdio.h>, because they need it for implementing getc() and putc() as
      fast macros.  */
-# if defined _IO_ferror_unlocked    /* GNU libc, BeOS */
+# if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
   fp->_IO_read_end = fp->_IO_read_ptr;
   fp->_IO_write_ptr = fp->_IO_write_base;
   /* Avoid memory leak when there is an active ungetc buffer.  */
@@ -81,28 +81,27 @@ fpurge (FILE *fp)
       fp->_IO_save_base = NULL;
     }
   return 0;
-# elif defined __sferror            /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
-  fp->_p = fp->_bf._base;
-  fp->_r = 0;
-  fp->_w = ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
-           ? fp->_bf._size
-           : 0);
+# elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+  fp_->_p = fp_->_bf._base;
+  fp_->_r = 0;
+  fp_->_w = ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
+            ? fp_->_bf._size
+            : 0);
   /* Avoid memory leak when there is an active ungetc buffer.  */
-#  if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
-   /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
-      and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
-#   define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
-#  else                                         /* FreeBSD, MacOS X, Cygwin */
-#   define fp_ub fp->_ub
-#  endif
   if (fp_ub._base != NULL)
     {
-      if (fp_ub._base != fp->_ubuf)
+      if (fp_ub._base != fp_->_ubuf)
        free (fp_ub._base);
       fp_ub._base = NULL;
     }
   return 0;
-# elif defined _IOERR               /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
+# elif defined __EMX__              /* emx+gcc */
+  fp->_ptr = fp->_buffer;
+  fp->_rcount = 0;
+  fp->_wcount = 0;
+  fp->_ungetc_count = 0;
+  return 0;
+# elif defined _IOERR               /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
   fp->_ptr = fp->_base;
   if (fp->_ptr != NULL)
     fp->_cnt = 0;
@@ -115,8 +114,34 @@ fpurge (FILE *fp)
     fp->__bufpos = fp->__bufread;
 #  endif
   return 0;
+# elif defined __QNX__              /* QNX */
+  fp->_Rback = fp->_Back + sizeof (fp->_Back);
+  fp->_Rsave = NULL;
+  if (fp->_Mode & 0x2000 /* _MWRITE */)
+    /* fp->_Buf <= fp->_Next <= fp->_Wend */
+    fp->_Next = fp->_Buf;
+  else
+    /* fp->_Buf <= fp->_Next <= fp->_Rend */
+    fp->_Rend = fp->_Next;
+  return 0;
+# elif defined __MINT__             /* Atari FreeMiNT */
+  if (fp->__pushed_back)
+    {
+      fp->__bufp = fp->__pushback_bufp;
+      fp->__pushed_back = 0;
+    }
+  /* Preserve the current file position.  */
+  if (fp->__target != -1)
+    fp->__target += fp->__bufp - fp->__buffer;
+  fp->__bufp = fp->__buffer;
+  /* Nothing in the buffer, next getc is nontrivial.  */
+  fp->__get_limit = fp->__bufp;
+  /* Nothing in the buffer, next putc is nontrivial.  */
+  fp->__put_limit = fp->__buffer;
+  return 0;
 # else
- #error "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."
+# warning "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."
+  return 0;
 # endif
 
 #endif
diff --git a/lib/sh/fpurge.c.old b/lib/sh/fpurge.c.old
new file mode 100644 (file)
index 0000000..a01c1bf
--- /dev/null
@@ -0,0 +1,123 @@
+/* fpurge - Flushing buffers of a FILE stream. */
+
+/* Copyright (C) 2007 Free Software Foundation, Inc.
+
+   This file is part of GNU Bash, the Bourne Again SHell.
+
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <config.h>
+
+#include "stdc.h"
+
+#include <stdio.h>
+
+/* Specification.  Same as in ../../externs.h.  */
+#define NEED_FPURGE_DECL
+#if HAVE_FPURGE
+#  define fpurge _bash_fpurge
+#endif
+extern int fpurge __P((FILE *stream));
+
+#if HAVE___FPURGE                   /* glibc >= 2.2, Solaris >= 7 */
+# include <stdio_ext.h>
+#endif
+#include <stdlib.h>
+
+int
+fpurge (FILE *fp)
+{
+#if HAVE___FPURGE                   /* glibc >= 2.2, Solaris >= 7 */
+
+  __fpurge (fp);
+  /* The __fpurge function does not have a return value.  */
+  return 0;
+
+#elif HAVE_FPURGE                   /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X */
+
+  /* Call the system's fpurge function.  */
+# undef fpurge
+# if !HAVE_DECL_FPURGE
+  extern int fpurge (FILE *);
+# endif
+  int result = fpurge (fp);
+# if defined __sferror || defined __DragonFly__        /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+  if (result == 0)
+    /* Correct the invariants that fpurge broke.
+       <stdio.h> on BSD systems says:
+         "The following always hold: if _flags & __SRD, _w is 0."
+       If this invariant is not fulfilled and the stream is read-write but
+       currently writing, subsequent putc or fputc calls will write directly
+       into the buffer, although they shouldn't be allowed to.  */
+    if ((fp->_flags & __SRD) != 0)
+      fp->_w = 0;
+# endif
+  return result;
+
+#else
+
+  /* Most systems provide FILE as a struct and the necessary bitmask in
+     <stdio.h>, because they need it for implementing getc() and putc() as
+     fast macros.  */
+# if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Linux libc5 */
+  fp->_IO_read_end = fp->_IO_read_ptr;
+  fp->_IO_write_ptr = fp->_IO_write_base;
+  /* Avoid memory leak when there is an active ungetc buffer.  */
+  if (fp->_IO_save_base != NULL)
+    {
+      free (fp->_IO_save_base);
+      fp->_IO_save_base = NULL;
+    }
+  return 0;
+# elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+  fp->_p = fp->_bf._base;
+  fp->_r = 0;
+  fp->_w = ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
+           ? fp->_bf._size
+           : 0);
+  /* Avoid memory leak when there is an active ungetc buffer.  */
+#  if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
+   /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
+      and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
+#   define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
+#  else                                         /* FreeBSD, MacOS X, Cygwin */
+#   define fp_ub fp->_ub
+#  endif
+  if (fp_ub._base != NULL)
+    {
+      if (fp_ub._base != fp->_ubuf)
+       free (fp_ub._base);
+      fp_ub._base = NULL;
+    }
+  return 0;
+# elif defined _IOERR               /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
+  fp->_ptr = fp->_base;
+  if (fp->_ptr != NULL)
+    fp->_cnt = 0;
+  return 0;
+# elif defined __UCLIBC__           /* uClibc */
+#  ifdef __STDIO_BUFFERS
+  if (fp->__modeflags & __FLAG_WRITING)
+    fp->__bufpos = fp->__bufstart;
+  else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
+    fp->__bufpos = fp->__bufread;
+#  endif
+  return 0;
+# else
+ #error "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."
+# endif
+
+#endif
+}
diff --git a/lib/sh/fpurge.c~ b/lib/sh/fpurge.c~
new file mode 100644 (file)
index 0000000..fa2ccf9
--- /dev/null
@@ -0,0 +1,147 @@
+/* fpurge - Flushing buffers of a FILE stream. */
+
+/* Copyright (C) 2007 Free Software Foundation, Inc.
+
+   This file is part of GNU Bash, the Bourne Again SHell.
+
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <config.h>
+
+#include "stdc.h"
+
+#include <stdio.h>
+
+/* Specification.  Same as in ../../externs.h.  */
+#define NEED_FPURGE_DECL
+#if HAVE_FPURGE
+#  define fpurge _bash_fpurge
+#endif
+extern int fpurge __P((FILE *stream));
+
+#if HAVE___FPURGE                   /* glibc >= 2.2, Haiku, Solaris >= 7 */
+# include <stdio_ext.h>
+#endif
+#include <stdlib.h>
+
+int
+fpurge (FILE *fp)
+{
+#if HAVE___FPURGE                   /* glibc >= 2.2, Haiku, Solaris >= 7 */
+
+  __fpurge (fp);
+  /* The __fpurge function does not have a return value.  */
+  return 0;
+
+#elif HAVE_FPURGE                   /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X */
+
+  /* Call the system's fpurge function.  */
+# undef fpurge
+# if !HAVE_DECL_FPURGE
+  extern int fpurge (FILE *);
+# endif
+  int result = fpurge (fp);
+# if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+  if (result == 0)
+    /* Correct the invariants that fpurge broke.
+       <stdio.h> on BSD systems says:
+         "The following always hold: if _flags & __SRD, _w is 0."
+       If this invariant is not fulfilled and the stream is read-write but
+       currently writing, subsequent putc or fputc calls will write directly
+       into the buffer, although they shouldn't be allowed to.  */
+    if ((fp->_flags & __SRD) != 0)
+      fp->_w = 0;
+# endif
+  return result;
+
+#else
+
+  /* Most systems provide FILE as a struct and the necessary bitmask in
+     <stdio.h>, because they need it for implementing getc() and putc() as
+     fast macros.  */
+# if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
+  fp->_IO_read_end = fp->_IO_read_ptr;
+  fp->_IO_write_ptr = fp->_IO_write_base;
+  /* Avoid memory leak when there is an active ungetc buffer.  */
+  if (fp->_IO_save_base != NULL)
+    {
+      free (fp->_IO_save_base);
+      fp->_IO_save_base = NULL;
+    }
+  return 0;
+# elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+  fp_->_p = fp_->_bf._base;
+  fp_->_r = 0;
+  fp_->_w = ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
+            ? fp_->_bf._size
+            : 0);
+  /* Avoid memory leak when there is an active ungetc buffer.  */
+  if (fp_ub._base != NULL)
+    {
+      if (fp_ub._base != fp_->_ubuf)
+       free (fp_ub._base);
+      fp_ub._base = NULL;
+    }
+  return 0;
+# elif defined __EMX__              /* emx+gcc */
+  fp->_ptr = fp->_buffer;
+  fp->_rcount = 0;
+  fp->_wcount = 0;
+  fp->_ungetc_count = 0;
+  return 0;
+# elif defined _IOERR               /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
+  fp->_ptr = fp->_base;
+  if (fp->_ptr != NULL)
+    fp->_cnt = 0;
+  return 0;
+# elif defined __UCLIBC__           /* uClibc */
+#  ifdef __STDIO_BUFFERS
+  if (fp->__modeflags & __FLAG_WRITING)
+    fp->__bufpos = fp->__bufstart;
+  else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
+    fp->__bufpos = fp->__bufread;
+#  endif
+  return 0;
+# elif defined __QNX__              /* QNX */
+  fp->_Rback = fp->_Back + sizeof (fp->_Back);
+  fp->_Rsave = NULL;
+  if (fp->_Mode & 0x2000 /* _MWRITE */)
+    /* fp->_Buf <= fp->_Next <= fp->_Wend */
+    fp->_Next = fp->_Buf;
+  else
+    /* fp->_Buf <= fp->_Next <= fp->_Rend */
+    fp->_Rend = fp->_Next;
+  return 0;
+# elif defined __MINT__             /* Atari FreeMiNT */
+  if (fp->__pushed_back)
+    {
+      fp->__bufp = fp->__pushback_bufp;
+      fp->__pushed_back = 0;
+    }
+  /* Preserve the current file position.  */
+  if (fp->__target != -1)
+    fp->__target += fp->__bufp - fp->__buffer;
+  fp->__bufp = fp->__buffer;
+  /* Nothing in the buffer, next getc is nontrivial.  */
+  fp->__get_limit = fp->__bufp;
+  /* Nothing in the buffer, next putc is nontrivial.  */
+  fp->__put_limit = fp->__buffer;
+  return 0;
+# else
+ #error "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."
+# endif
+
+#endif
+}
index 5e01f8f75e31c5126eb99190cce5be382eb51ea5..ac157a9f7412d1135a66604ace02551e9c95bb6f 100644 (file)
 
 #include "bashansi.h"
 
-#if defined (HAVE_SELECT)
-#  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
-#    include <sys/time.h>
-#  endif
-#endif /* HAVE_SELECT */
-#if defined (HAVE_SYS_SELECT_H)
-#  include <sys/select.h>
-#endif
+#include "posixselect.h"
 
 #if defined (FIONREAD_IN_SYS_IOCTL)
 #  include <sys/ioctl.h>
diff --git a/lib/sh/input_avail.c~ b/lib/sh/input_avail.c~
new file mode 100644 (file)
index 0000000..5e01f8f
--- /dev/null
@@ -0,0 +1,105 @@
+/* input_avail.c -- check whether or not data is available for reading on a
+                   specified file descriptor. */
+
+/* Copyright (C) 2008,2009 Free Software Foundation, Inc.
+
+   This file is part of GNU Bash, the Bourne Again SHell.
+
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#if defined (__TANDEM)
+#  include <floss.h>
+#endif
+
+#if defined (HAVE_CONFIG_H)
+#  include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <fcntl.h>
+#if defined (HAVE_SYS_FILE_H)
+#  include <sys/file.h>
+#endif /* HAVE_SYS_FILE_H */
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif /* HAVE_UNISTD_H */
+
+#include "bashansi.h"
+
+#if defined (HAVE_SELECT)
+#  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
+#    include <sys/time.h>
+#  endif
+#endif /* HAVE_SELECT */
+#if defined (HAVE_SYS_SELECT_H)
+#  include <sys/select.h>
+#endif
+
+#if defined (FIONREAD_IN_SYS_IOCTL)
+#  include <sys/ioctl.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+
+#if !defined (errno)
+extern int errno;
+#endif /* !errno */
+
+#if !defined (O_NDELAY) && defined (O_NONBLOCK)
+#  define O_NDELAY O_NONBLOCK  /* Posix style */
+#endif
+
+/* Return >= 1 if select/FIONREAD indicates data available for reading on
+   file descriptor FD; 0 if no data available.  Return -1 on error. */
+int
+input_avail (fd)
+     int fd;
+{
+  int result, chars_avail;
+#if defined(HAVE_SELECT)
+  fd_set readfds, exceptfds;
+  struct timeval timeout;
+#endif
+
+  if (fd < 0)
+    return -1;
+
+  chars_avail = 0;
+
+#if defined (HAVE_SELECT)
+  FD_ZERO (&readfds);
+  FD_ZERO (&exceptfds);
+  FD_SET (fd, &readfds);
+  FD_SET (fd, &exceptfds);
+  timeout.tv_sec = 0;
+  timeout.tv_usec = 0;
+  result = select (fd + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
+  return ((result <= 0) ? 0 : 1);
+
+#endif
+
+  result = -1;
+#if defined (FIONREAD)
+  errno = 0;
+  result = ioctl (fd, FIONREAD, &chars_avail);
+  if (result == -1 && errno == EIO)
+    return -1;
+  return (chars_avail);
+#endif
+
+  return 0;
+}
index 72ec06a2c1fd8dde92acea5e8ac773e35f1d061b..3efcf32d68e9722024b6ca9d67f9e81b2aa5ac04 100755 (executable)
@@ -1,4 +1,4 @@
-BUILD_DIR=/usr/local/build/bash/bash-current
+BUILD_DIR=/usr/local/build/chet/bash/bash-current
 THIS_SH=$BUILD_DIR/bash
 PATH=$PATH:$BUILD_DIR
 
diff --git a/trap.c b/trap.c
index f4513e2c27b593228597744914928ccbc53f9625..940dcfdbccd5d9832c0778051b6391a52b540a7b 100644 (file)
--- a/trap.c
+++ b/trap.c
@@ -799,6 +799,7 @@ run_debug_trap ()
 {
   int trap_exit_value;
   pid_t save_pgrp;
+  int save_pipe[2];
 
   /* XXX - question:  should the DEBUG trap inherit the RETURN trap? */
   trap_exit_value = 0;
@@ -806,14 +807,25 @@ run_debug_trap ()
     {
 #if defined (JOB_CONTROL)
       save_pgrp = pipeline_pgrp;
-      pipeline_pgrp = shell_pgrp;
+      pipeline_pgrp = 0;
       save_pipeline (1);
+#  if defined (PGRP_PIPE)
+      save_pgrp_pipe (save_pipe, 1);
+#  endif
       stop_making_children ();
 #endif
+
       trap_exit_value = _run_trap_internal (DEBUG_TRAP, "debug trap");
+
 #if defined (JOB_CONTROL)
       pipeline_pgrp = save_pgrp;
       restore_pipeline (1);
+#  if defined (PGRP_PIPE)
+      close_pgrp_pipe ();
+      restore_pgrp_pipe (save_pipe);
+#  endif
+      if (pipeline_pgrp > 0)
+       give_terminal_to (pipeline_pgrp, 1);
       notify_and_cleanup ();
 #endif
       
diff --git a/trap.c.save1 b/trap.c.save1
new file mode 100644 (file)
index 0000000..b41304e
--- /dev/null
@@ -0,0 +1,1042 @@
+/* trap.c -- Not the trap command, but useful functions for manipulating
+   those objects.  The trap command is in builtins/trap.def. */
+
+/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
+
+   This file is part of GNU Bash, the Bourne Again SHell.
+
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "config.h"
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif
+
+#include "bashtypes.h"
+#include "bashansi.h"
+
+#include <stdio.h>
+#include <errno.h>
+
+#include "bashintl.h"
+
+#include "trap.h"
+
+#include "shell.h"
+#include "flags.h"
+#include "input.h"     /* for save_token_state, restore_token_state */
+#include "jobs.h"
+#include "signames.h"
+#include "builtins.h"
+#include "builtins/common.h"
+#include "builtins/builtext.h"
+
+#ifndef errno
+extern int errno;
+#endif
+
+/* Flags which describe the current handling state of a signal. */
+#define SIG_INHERITED   0x0    /* Value inherited from parent. */
+#define SIG_TRAPPED     0x1    /* Currently trapped. */
+#define SIG_HARD_IGNORE 0x2    /* Signal was ignored on shell entry. */
+#define SIG_SPECIAL     0x4    /* Treat this signal specially. */
+#define SIG_NO_TRAP     0x8    /* Signal cannot be trapped. */
+#define SIG_INPROGRESS 0x10    /* Signal handler currently executing. */
+#define SIG_CHANGED    0x20    /* Trap value changed in trap handler. */
+#define SIG_IGNORED    0x40    /* The signal is currently being ignored. */
+
+#define SPECIAL_TRAP(s)        ((s) == EXIT_TRAP || (s) == DEBUG_TRAP || (s) == ERROR_TRAP || (s) == RETURN_TRAP)
+
+/* An array of such flags, one for each signal, describing what the
+   shell will do with a signal.  DEBUG_TRAP == NSIG; some code below
+   assumes this. */
+static int sigmodes[BASH_NSIG];
+
+static void free_trap_command __P((int));
+static void change_signal __P((int, char *));
+
+static void get_original_signal __P((int));
+
+static int _run_trap_internal __P((int, char *));
+
+static void reset_signal __P((int));
+static void restore_signal __P((int));
+static void reset_or_restore_signal_handlers __P((sh_resetsig_func_t *));
+
+/* Variables used here but defined in other files. */
+extern int last_command_exit_value;
+extern int line_number;
+
+extern char *this_command_name;
+extern sh_builtin_func_t *this_shell_builtin;
+extern procenv_t wait_intr_buf;
+extern int return_catch_flag, return_catch_value;
+extern int subshell_level;
+
+/* The list of things to do originally, before we started trapping. */
+SigHandler *original_signals[NSIG];
+
+/* For each signal, a slot for a string, which is a command to be
+   executed when that signal is recieved.  The slot can also contain
+   DEFAULT_SIG, which means do whatever you were going to do before
+   you were so rudely interrupted, or IGNORE_SIG, which says ignore
+   this signal. */
+char *trap_list[BASH_NSIG];
+
+/* A bitmap of signals received for which we have trap handlers. */
+int pending_traps[NSIG];
+
+/* Set to the number of the signal we're running the trap for + 1.
+   Used in execute_cmd.c and builtins/common.c to clean up when
+   parse_and_execute does not return normally after executing the
+   trap command (e.g., when `return' is executed in the trap command). */
+int running_trap;
+
+/* Set to last_command_exit_value before running a trap. */
+int trap_saved_exit_value;
+
+/* The (trapped) signal received while executing in the `wait' builtin */
+int wait_signal_received;
+
+/* A value which can never be the target of a trap handler. */
+#define IMPOSSIBLE_TRAP_HANDLER (SigHandler *)initialize_traps
+
+#define GETORIGSIG(sig) \
+  do { \
+    original_signals[sig] = (SigHandler *)set_signal_handler (sig, SIG_DFL); \
+    set_signal_handler (sig, original_signals[sig]); \
+    if (original_signals[sig] == SIG_IGN) \
+      sigmodes[sig] |= SIG_HARD_IGNORE; \
+  } while (0)
+
+#define GET_ORIGINAL_SIGNAL(sig) \
+  if (sig && sig < NSIG && original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER) \
+    GETORIGSIG(sig)
+
+void
+initialize_traps ()
+{
+  register int i;
+
+  initialize_signames();
+
+  trap_list[EXIT_TRAP] = trap_list[DEBUG_TRAP] = trap_list[ERROR_TRAP] = trap_list[RETURN_TRAP] = (char *)NULL;
+  sigmodes[EXIT_TRAP] = sigmodes[DEBUG_TRAP] = sigmodes[ERROR_TRAP] = sigmodes[RETURN_TRAP] = SIG_INHERITED;
+  original_signals[EXIT_TRAP] = IMPOSSIBLE_TRAP_HANDLER;
+
+  for (i = 1; i < NSIG; i++)
+    {
+      pending_traps[i] = 0;
+      trap_list[i] = (char *)DEFAULT_SIG;
+      sigmodes[i] = SIG_INHERITED;
+      original_signals[i] = IMPOSSIBLE_TRAP_HANDLER;
+    }
+
+  /* Show which signals are treated specially by the shell. */
+#if defined (SIGCHLD)
+  GETORIGSIG (SIGCHLD);
+  sigmodes[SIGCHLD] |= (SIG_SPECIAL | SIG_NO_TRAP);
+#endif /* SIGCHLD */
+
+  GETORIGSIG (SIGINT);
+  sigmodes[SIGINT] |= SIG_SPECIAL;
+
+#if defined (__BEOS__)
+  /* BeOS sets SIGINT to SIG_IGN! */
+  original_signals[SIGINT] = SIG_DFL;
+  sigmodes[SIGINT] &= ~SIG_HARD_IGNORE;
+#endif
+
+  GETORIGSIG (SIGQUIT);
+  sigmodes[SIGQUIT] |= SIG_SPECIAL;
+
+  if (interactive)
+    {
+      GETORIGSIG (SIGTERM);
+      sigmodes[SIGTERM] |= SIG_SPECIAL;
+    }
+}
+
+#ifdef INCLUDE_UNUSED
+/* Return a printable representation of the trap handler for SIG. */
+static char *
+trap_handler_string (sig)
+     int sig;
+{
+  if (trap_list[sig] == (char *)DEFAULT_SIG)
+    return "DEFAULT_SIG";
+  else if (trap_list[sig] == (char *)IGNORE_SIG)
+    return "IGNORE_SIG";
+  else if (trap_list[sig] == (char *)IMPOSSIBLE_TRAP_HANDLER)
+    return "IMPOSSIBLE_TRAP_HANDLER";
+  else if (trap_list[sig])
+    return trap_list[sig];
+  else
+    return "NULL";
+}
+#endif
+
+/* Return the print name of this signal. */
+char *
+signal_name (sig)
+     int sig;
+{
+  char *ret;
+
+  /* on cygwin32, signal_names[sig] could be null */
+  ret = (sig >= BASH_NSIG || sig < 0 || signal_names[sig] == NULL)
+       ? _("invalid signal number")
+       : signal_names[sig];
+
+  return ret;
+}
+
+/* Turn a string into a signal number, or a number into
+   a signal number.  If STRING is "2", "SIGINT", or "INT",
+   then (int)2 is returned.  Return NO_SIG if STRING doesn't
+   contain a valid signal descriptor. */
+int
+decode_signal (string, flags)
+     char *string;
+     int flags;
+{
+  intmax_t sig;
+  char *name;
+
+  if (legal_number (string, &sig))
+    return ((sig >= 0 && sig < NSIG) ? (int)sig : NO_SIG);
+
+  /* A leading `SIG' may be omitted. */
+  for (sig = 0; sig < BASH_NSIG; sig++)
+    {
+      name = signal_names[sig];
+      if (name == 0 || name[0] == '\0')
+       continue;
+
+      /* Check name without the SIG prefix first case sensitivly or
+        insensitively depending on whether flags includes DSIG_NOCASE */
+      if (STREQN (name, "SIG", 3))
+       {
+         name += 3;
+
+         if ((flags & DSIG_NOCASE) && strcasecmp (string, name) == 0)
+           return ((int)sig);
+         else if ((flags & DSIG_NOCASE) == 0 && strcmp (string, name) == 0)
+           return ((int)sig);
+         /* If we can't use the `SIG' prefix to match, punt on this
+            name now. */
+         else if ((flags & DSIG_SIGPREFIX) == 0)
+           continue;
+       }
+
+      /* Check name with SIG prefix case sensitively or insensitively
+        depending on whether flags includes DSIG_NOCASE */
+      name = signal_names[sig];
+      if ((flags & DSIG_NOCASE) && strcasecmp (string, name) == 0)
+       return ((int)sig);
+      else if ((flags & DSIG_NOCASE) == 0 && strcmp (string, name) == 0)
+       return ((int)sig);
+    }
+
+  return (NO_SIG);
+}
+
+/* Non-zero when we catch a trapped signal. */
+static int catch_flag;
+
+void
+run_pending_traps ()
+{
+  register int sig;
+  int old_exit_value, *token_state;
+
+  if (catch_flag == 0)         /* simple optimization */
+    return;
+
+  catch_flag = 0;
+
+  /* Preserve $? when running trap. */
+  old_exit_value = last_command_exit_value;
+
+  for (sig = 1; sig < NSIG; sig++)
+    {
+      /* XXX this could be made into a counter by using
+        while (pending_traps[sig]--) instead of the if statement. */
+      if (pending_traps[sig])
+       {
+#if defined (HAVE_POSIX_SIGNALS)
+         sigset_t set, oset;
+
+         sigemptyset (&set);
+         sigemptyset (&oset);
+
+         sigaddset (&set, sig);
+         sigprocmask (SIG_BLOCK, &set, &oset);
+#else
+#  if defined (HAVE_BSD_SIGNALS)
+         int oldmask = sigblock (sigmask (sig));
+#  endif
+#endif /* HAVE_POSIX_SIGNALS */
+
+         if (sig == SIGINT)
+           {
+             run_interrupt_trap ();
+             CLRINTERRUPT;
+           }
+#if defined (JOB_CONTROL) && defined (SIGCHLD)
+         else if (sig == SIGCHLD &&
+                  trap_list[SIGCHLD] != (char *)IMPOSSIBLE_TRAP_HANDLER &&
+                  (sigmodes[SIGCHLD] & SIG_INPROGRESS) == 0)
+           {
+             run_sigchld_trap (pending_traps[sig]);    /* use as counter */
+           }
+#endif
+         else if (trap_list[sig] == (char *)DEFAULT_SIG ||
+                  trap_list[sig] == (char *)IGNORE_SIG ||
+                  trap_list[sig] == (char *)IMPOSSIBLE_TRAP_HANDLER)
+           {
+             /* This is possible due to a race condition.  Say a bash
+                process has SIGTERM trapped.  A subshell is spawned
+                using { list; } & and the parent does something and kills
+                the subshell with SIGTERM.  It's possible for the subshell
+                to set pending_traps[SIGTERM] to 1 before the code in
+                execute_cmd.c eventually calls restore_original_signals
+                to reset the SIGTERM signal handler in the subshell.  The
+                next time run_pending_traps is called, pending_traps[SIGTERM]
+                will be 1, but the trap handler in trap_list[SIGTERM] will
+                be invalid (probably DEFAULT_SIG, but it could be IGNORE_SIG).
+                Unless we catch this, the subshell will dump core when
+                trap_list[SIGTERM] == DEFAULT_SIG, because DEFAULT_SIG is
+                usually 0x0. */
+             internal_warning (_("run_pending_traps: bad value in trap_list[%d]: %p"),
+                               sig, trap_list[sig]);
+             if (trap_list[sig] == (char *)DEFAULT_SIG)
+               {
+                 internal_warning (_("run_pending_traps: signal handler is SIG_DFL, resending %d (%s) to myself"), sig, signal_name (sig));
+                 kill (getpid (), sig);
+               }
+           }
+         else
+           {
+             token_state = save_token_state ();
+             parse_and_execute (savestring (trap_list[sig]), "trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE);
+             restore_token_state (token_state);
+             free (token_state);
+           }
+
+         pending_traps[sig] = 0;
+
+#if defined (HAVE_POSIX_SIGNALS)
+         sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
+#else
+#  if defined (HAVE_BSD_SIGNALS)
+         sigsetmask (oldmask);
+#  endif
+#endif /* POSIX_VERSION */
+       }
+    }
+
+  last_command_exit_value = old_exit_value;
+}
+
+sighandler
+trap_handler (sig)
+     int sig;
+{
+  int oerrno;
+
+  if ((sigmodes[sig] & SIG_TRAPPED) == 0)
+    {
+#if defined (DEBUG)
+      internal_warning ("trap_handler: signal %d: signal not trapped", sig);
+#endif
+      SIGRETURN (0);
+    }
+
+  if ((sig >= NSIG) ||
+      (trap_list[sig] == (char *)DEFAULT_SIG) ||
+      (trap_list[sig] == (char *)IGNORE_SIG))
+    programming_error (_("trap_handler: bad signal %d"), sig);
+  else
+    {
+      oerrno = errno;
+#if defined (MUST_REINSTALL_SIGHANDLERS)
+#  if defined (JOB_CONTROL) && defined (SIGCHLD)
+      if (sig != SIGCHLD)
+#  endif /* JOB_CONTROL && SIGCHLD */
+      set_signal_handler (sig, trap_handler);
+#endif /* MUST_REINSTALL_SIGHANDLERS */
+
+      catch_flag = 1;
+      pending_traps[sig]++;
+
+      if (interrupt_immediately && this_shell_builtin && (this_shell_builtin == wait_builtin))
+       {
+         wait_signal_received = sig;
+         longjmp (wait_intr_buf, 1);
+       }
+
+      if (interrupt_immediately)
+       run_pending_traps ();
+
+      errno = oerrno;
+    }
+
+  SIGRETURN (0);
+}
+
+#if defined (JOB_CONTROL) && defined (SIGCHLD)
+
+#ifdef INCLUDE_UNUSED
+/* Make COMMAND_STRING be executed when SIGCHLD is caught. */
+void
+set_sigchld_trap (command_string)
+     char *command_string;
+{
+  set_signal (SIGCHLD, command_string);
+}
+#endif
+
+/* Make COMMAND_STRING be executed when SIGCHLD is caught iff SIGCHLD
+   is not already trapped.  IMPOSSIBLE_TRAP_HANDLER is used as a sentinel
+   to make sure that a SIGCHLD trap handler run via run_sigchld_trap can
+   reset the disposition to the default and not have the original signal
+   accidentally restored, undoing the user's command. */
+void
+maybe_set_sigchld_trap (command_string)
+     char *command_string;
+{
+  if ((sigmodes[SIGCHLD] & SIG_TRAPPED) == 0 && trap_list[SIGCHLD] == (char *)IMPOSSIBLE_TRAP_HANDLER)
+    set_signal (SIGCHLD, command_string);
+}
+
+/* Temporarily set the SIGCHLD trap string to IMPOSSIBLE_TRAP_HANDLER.  Used
+   as a sentinel in run_sigchld_trap and maybe_set_sigchld_trap to see whether
+   or not a SIGCHLD trap handler reset SIGCHLD disposition to the default. */
+void
+set_impossible_sigchld_trap ()
+{
+  restore_default_signal (SIGCHLD);
+  change_signal (SIGCHLD, (char *)IMPOSSIBLE_TRAP_HANDLER);
+  sigmodes[SIGCHLD] &= ~SIG_TRAPPED;   /* maybe_set_sigchld_trap checks this */
+}
+#endif /* JOB_CONTROL && SIGCHLD */
+
+void
+set_debug_trap (command)
+     char *command;
+{
+  set_signal (DEBUG_TRAP, command);
+}
+
+void
+set_error_trap (command)
+     char *command;
+{
+  set_signal (ERROR_TRAP, command);
+}
+
+void
+set_return_trap (command)
+     char *command;
+{
+  set_signal (RETURN_TRAP, command);
+}
+
+#ifdef INCLUDE_UNUSED
+void
+set_sigint_trap (command)
+     char *command;
+{
+  set_signal (SIGINT, command);
+}
+#endif
+
+/* Reset the SIGINT handler so that subshells that are doing `shellsy'
+   things, like waiting for command substitution or executing commands
+   in explicit subshells ( ( cmd ) ), can catch interrupts properly. */
+SigHandler *
+set_sigint_handler ()
+{
+  if (sigmodes[SIGINT] & SIG_HARD_IGNORE)
+    return ((SigHandler *)SIG_IGN);
+
+  else if (sigmodes[SIGINT] & SIG_IGNORED)
+    return ((SigHandler *)set_signal_handler (SIGINT, SIG_IGN)); /* XXX */
+
+  else if (sigmodes[SIGINT] & SIG_TRAPPED)
+    return ((SigHandler *)set_signal_handler (SIGINT, trap_handler));
+
+  /* The signal is not trapped, so set the handler to the shell's special
+     interrupt handler. */
+  else if (interactive)        /* XXX - was interactive_shell */
+    return (set_signal_handler (SIGINT, sigint_sighandler));
+  else
+    return (set_signal_handler (SIGINT, termsig_sighandler));
+}
+
+/* Return the correct handler for signal SIG according to the values in
+   sigmodes[SIG]. */
+SigHandler *
+trap_to_sighandler (sig)
+     int sig;
+{
+  if (sigmodes[sig] & (SIG_IGNORED|SIG_HARD_IGNORE))
+    return (SIG_IGN);
+  else if (sigmodes[sig] & SIG_TRAPPED)
+    return (trap_handler);
+  else
+    return (SIG_DFL);
+}
+
+/* Set SIG to call STRING as a command. */
+void
+set_signal (sig, string)
+     int sig;
+     char *string;
+{
+  if (SPECIAL_TRAP (sig))
+    {
+      change_signal (sig, savestring (string));
+      if (sig == EXIT_TRAP && interactive == 0)
+       initialize_terminating_signals ();
+      return;
+    }
+
+  /* A signal ignored on entry to the shell cannot be trapped or reset, but
+     no error is reported when attempting to do so.  -- Posix.2 */
+  if (sigmodes[sig] & SIG_HARD_IGNORE)
+    return;
+
+  /* Make sure we have original_signals[sig] if the signal has not yet
+     been trapped. */
+  if ((sigmodes[sig] & SIG_TRAPPED) == 0)
+    {
+      /* If we aren't sure of the original value, check it. */
+      if (original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER)
+        GETORIGSIG (sig);
+      if (original_signals[sig] == SIG_IGN)
+       return;
+    }
+
+  /* Only change the system signal handler if SIG_NO_TRAP is not set.
+     The trap command string is changed in either case.  The shell signal
+     handlers for SIGINT and SIGCHLD run the user specified traps in an
+     environment in which it is safe to do so. */
+  if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
+    {
+      set_signal_handler (sig, SIG_IGN);
+      change_signal (sig, savestring (string));
+      set_signal_handler (sig, trap_handler);
+    }
+  else
+    change_signal (sig, savestring (string));
+}
+
+static void
+free_trap_command (sig)
+     int sig;
+{
+  if ((sigmodes[sig] & SIG_TRAPPED) && trap_list[sig] &&
+      (trap_list[sig] != (char *)IGNORE_SIG) &&
+      (trap_list[sig] != (char *)DEFAULT_SIG) &&
+      (trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER))
+    free (trap_list[sig]);
+}
+
+/* If SIG has a string assigned to it, get rid of it.  Then give it
+   VALUE. */
+static void
+change_signal (sig, value)
+     int sig;
+     char *value;
+{
+  if ((sigmodes[sig] & SIG_INPROGRESS) == 0)
+    free_trap_command (sig);
+  trap_list[sig] = value;
+
+  sigmodes[sig] |= SIG_TRAPPED;
+  if (value == (char *)IGNORE_SIG)
+    sigmodes[sig] |= SIG_IGNORED;
+  else
+    sigmodes[sig] &= ~SIG_IGNORED;
+  if (sigmodes[sig] & SIG_INPROGRESS)
+    sigmodes[sig] |= SIG_CHANGED;
+}
+
+static void
+get_original_signal (sig)
+     int sig;
+{
+  /* If we aren't sure the of the original value, then get it. */
+  if (original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER)
+    GETORIGSIG (sig);
+}
+
+/* Restore the default action for SIG; i.e., the action the shell
+   would have taken before you used the trap command.  This is called
+   from trap_builtin (), which takes care to restore the handlers for
+   the signals the shell treats specially. */
+void
+restore_default_signal (sig)
+     int sig;
+{
+  if (SPECIAL_TRAP (sig))
+    {
+      if ((sig != DEBUG_TRAP && sig != ERROR_TRAP && sig != RETURN_TRAP) ||
+         (sigmodes[sig] & SIG_INPROGRESS) == 0)
+       free_trap_command (sig);
+      trap_list[sig] = (char *)NULL;
+      sigmodes[sig] &= ~SIG_TRAPPED;
+      if (sigmodes[sig] & SIG_INPROGRESS)
+       sigmodes[sig] |= SIG_CHANGED;
+      return;
+    }
+
+  GET_ORIGINAL_SIGNAL (sig);
+
+  /* A signal ignored on entry to the shell cannot be trapped or reset, but
+     no error is reported when attempting to do so.  Thanks Posix.2. */
+  if (sigmodes[sig] & SIG_HARD_IGNORE)
+    return;
+
+  /* If we aren't trapping this signal, don't bother doing anything else. */
+  if ((sigmodes[sig] & SIG_TRAPPED) == 0)
+    return;
+
+  /* Only change the signal handler for SIG if it allows it. */
+  if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
+    set_signal_handler (sig, original_signals[sig]);
+
+  /* Change the trap command in either case. */
+  change_signal (sig, (char *)DEFAULT_SIG);
+
+  /* Mark the signal as no longer trapped. */
+  sigmodes[sig] &= ~SIG_TRAPPED;
+}
+
+/* Make this signal be ignored. */
+void
+ignore_signal (sig)
+     int sig;
+{
+  if (SPECIAL_TRAP (sig) && ((sigmodes[sig] & SIG_IGNORED) == 0))
+    {
+      change_signal (sig, (char *)IGNORE_SIG);
+      return;
+    }
+
+  GET_ORIGINAL_SIGNAL (sig);
+
+  /* A signal ignored on entry to the shell cannot be trapped or reset.
+     No error is reported when the user attempts to do so. */
+  if (sigmodes[sig] & SIG_HARD_IGNORE)
+    return;
+
+  /* If already trapped and ignored, no change necessary. */
+  if (sigmodes[sig] & SIG_IGNORED)
+    return;
+
+  /* Only change the signal handler for SIG if it allows it. */
+  if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
+    set_signal_handler (sig, SIG_IGN);
+
+  /* Change the trap command in either case. */
+  change_signal (sig, (char *)IGNORE_SIG);
+}
+
+/* Handle the calling of "trap 0".  The only sticky situation is when
+   the command to be executed includes an "exit".  This is why we have
+   to provide our own place for top_level to jump to. */
+int
+run_exit_trap ()
+{
+  char *trap_command;
+  int code, function_code, retval;
+
+  trap_saved_exit_value = last_command_exit_value;
+  function_code = 0;
+
+  /* Run the trap only if signal 0 is trapped and not ignored, and we are not
+     currently running in the trap handler (call to exit in the list of
+     commands given to trap 0). */
+  if ((sigmodes[EXIT_TRAP] & SIG_TRAPPED) &&
+      (sigmodes[EXIT_TRAP] & (SIG_IGNORED|SIG_INPROGRESS)) == 0)
+    {
+      trap_command = savestring (trap_list[EXIT_TRAP]);
+      sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED;
+      sigmodes[EXIT_TRAP] |= SIG_INPROGRESS;
+
+      retval = trap_saved_exit_value;
+      running_trap = 1;
+
+      code = setjmp (top_level);
+
+      /* If we're in a function, make sure return longjmps come here, too. */
+      if (return_catch_flag)
+       function_code = setjmp (return_catch);
+
+      if (code == 0 && function_code == 0)
+       {
+         reset_parser ();
+         parse_and_execute (trap_command, "exit trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE);
+       }
+      else if (code == ERREXIT)
+       retval = last_command_exit_value;
+      else if (code == EXITPROG)
+       retval = last_command_exit_value;
+      else if (function_code != 0)
+        retval = return_catch_value;
+      else
+       retval = trap_saved_exit_value;
+
+      running_trap = 0;
+      return retval;
+    }
+
+  return (trap_saved_exit_value);
+}
+
+void
+run_trap_cleanup (sig)
+     int sig;
+{
+  sigmodes[sig] &= ~(SIG_INPROGRESS|SIG_CHANGED);
+}
+
+/* Run a trap command for SIG.  SIG is one of the signals the shell treats
+   specially.  Returns the exit status of the executed trap command list. */
+static int
+_run_trap_internal (sig, tag)
+     int sig;
+     char *tag;
+{
+  char *trap_command, *old_trap;
+  int trap_exit_value, *token_state;
+  int save_return_catch_flag, function_code, flags;
+  procenv_t save_return_catch;
+
+  trap_exit_value = function_code = 0;
+  /* Run the trap only if SIG is trapped and not ignored, and we are not
+     currently executing in the trap handler. */
+  if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0) &&
+      (trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER) &&
+      ((sigmodes[sig] & SIG_INPROGRESS) == 0))
+    {
+      old_trap = trap_list[sig];
+      sigmodes[sig] |= SIG_INPROGRESS;
+      sigmodes[sig] &= ~SIG_CHANGED;           /* just to be sure */
+      trap_command =  savestring (old_trap);
+
+      running_trap = sig + 1;
+      trap_saved_exit_value = last_command_exit_value;
+
+      token_state = save_token_state ();
+
+      /* If we're in a function, make sure return longjmps come here, too. */
+      save_return_catch_flag = return_catch_flag;
+      if (return_catch_flag)
+       {
+         COPY_PROCENV (return_catch, save_return_catch);
+         function_code = setjmp (return_catch);
+       }
+
+      flags = SEVAL_NONINT|SEVAL_NOHIST;
+      if (sig != DEBUG_TRAP && sig != RETURN_TRAP && sig != ERROR_TRAP)
+       flags |= SEVAL_RESETLINE;
+      if (function_code == 0)
+       parse_and_execute (trap_command, tag, flags);
+
+      restore_token_state (token_state);
+      free (token_state);
+
+      trap_exit_value = last_command_exit_value;
+      last_command_exit_value = trap_saved_exit_value;
+      running_trap = 0;
+
+      sigmodes[sig] &= ~SIG_INPROGRESS;
+
+      if (sigmodes[sig] & SIG_CHANGED)
+       {
+#if 0
+         /* Special traps like EXIT, DEBUG, RETURN are handled explicitly in
+            the places where they can be changed using unwind-protects.  For
+            example, look at execute_cmd.c:execute_function(). */
+         if (SPECIAL_TRAP (sig) == 0)
+#endif
+           free (old_trap);
+         sigmodes[sig] &= ~SIG_CHANGED;
+       }
+
+      if (save_return_catch_flag)
+       {
+         return_catch_flag = save_return_catch_flag;
+         return_catch_value = trap_exit_value;
+         COPY_PROCENV (save_return_catch, return_catch);
+         if (function_code)
+           longjmp (return_catch, 1);
+       }
+    }
+
+  return trap_exit_value;
+}
+
+int
+run_debug_trap ()
+{
+  int trap_exit_value;
+  pid_t save_pgrp;
+
+  /* XXX - question:  should the DEBUG trap inherit the RETURN trap? */
+  trap_exit_value = 0;
+  if ((sigmodes[DEBUG_TRAP] & SIG_TRAPPED) && ((sigmodes[DEBUG_TRAP] & SIG_IGNORED) == 0) && ((sigmodes[DEBUG_TRAP] & SIG_INPROGRESS) == 0))
+    {
+#if defined (JOB_CONTROL)
+      save_pgrp = pipeline_pgrp;
+itrace("run_debug_trap: save_pgrp = %d pipeline_pgrp -> 0", save_pgrp);
+      pipeline_pgrp = 0;
+      save_pipeline (1);
+      stop_making_children ();
+#  if defined (PGRP_PIPE)
+      close_pgrp_pipe ();
+#  endif
+#endif
+      trap_exit_value = _run_trap_internal (DEBUG_TRAP, "debug trap");
+#if defined (JOB_CONTROL)
+      pipeline_pgrp = save_pgrp;
+      restore_pipeline (1);
+itrace("run_debug_trap: after restore_pipeline");
+if (pipeline_pgrp > 0)
+  {
+itrace("run_debug_trap: pipeline_pgrp = %d give_terminal_to (%d, 1)", pipeline_pgrp, pipeline_pgrp);
+      give_terminal_to (pipeline_pgrp, 1);
+  }
+      notify_and_cleanup ();
+#endif
+      
+#if defined (DEBUGGER)
+      /* If we're in the debugger and the DEBUG trap returns 2 while we're in
+        a function or sourced script, we force a `return'. */
+      if (debugging_mode && trap_exit_value == 2 && return_catch_flag)
+       {
+         return_catch_value = trap_exit_value;
+         longjmp (return_catch, 1);
+       }
+#endif
+    }
+  return trap_exit_value;
+}
+
+void
+run_error_trap ()
+{
+  if ((sigmodes[ERROR_TRAP] & SIG_TRAPPED) && ((sigmodes[ERROR_TRAP] & SIG_IGNORED) == 0) && (sigmodes[ERROR_TRAP] & SIG_INPROGRESS) == 0)
+    _run_trap_internal (ERROR_TRAP, "error trap");
+}
+
+void
+run_return_trap ()
+{
+  int old_exit_value;
+
+#if 0
+  if ((sigmodes[DEBUG_TRAP] & SIG_TRAPPED) && (sigmodes[DEBUG_TRAP] & SIG_INPROGRESS))
+    return;
+#endif
+
+  if ((sigmodes[RETURN_TRAP] & SIG_TRAPPED) && ((sigmodes[RETURN_TRAP] & SIG_IGNORED) == 0) && (sigmodes[RETURN_TRAP] & SIG_INPROGRESS) == 0)
+    {
+      old_exit_value = last_command_exit_value;
+      _run_trap_internal (RETURN_TRAP, "return trap");
+      last_command_exit_value = old_exit_value;
+    }
+}
+
+/* Run a trap set on SIGINT.  This is called from throw_to_top_level (), and
+   declared here to localize the trap functions. */
+void
+run_interrupt_trap ()
+{
+  _run_trap_internal (SIGINT, "interrupt trap");
+}
+
+#ifdef INCLUDE_UNUSED
+/* Free all the allocated strings in the list of traps and reset the trap
+   values to the default. */
+void
+free_trap_strings ()
+{
+  register int i;
+
+  for (i = 0; i < BASH_NSIG; i++)
+    {
+      free_trap_command (i);
+      trap_list[i] = (char *)DEFAULT_SIG;
+      sigmodes[i] &= ~SIG_TRAPPED;
+    }
+  trap_list[DEBUG_TRAP] = trap_list[EXIT_TRAP] = trap_list[ERROR_TRAP] = trap_list[RETURN_TRAP] = (char *)NULL;
+}
+#endif
+
+/* Reset the handler for SIG to the original value. */
+static void
+reset_signal (sig)
+     int sig;
+{
+  set_signal_handler (sig, original_signals[sig]);
+  sigmodes[sig] &= ~SIG_TRAPPED;
+}
+
+/* Set the handler signal SIG to the original and free any trap
+   command associated with it. */
+static void
+restore_signal (sig)
+     int sig;
+{
+  set_signal_handler (sig, original_signals[sig]);
+  change_signal (sig, (char *)DEFAULT_SIG);
+  sigmodes[sig] &= ~SIG_TRAPPED;
+}
+
+static void
+reset_or_restore_signal_handlers (reset)
+     sh_resetsig_func_t *reset;
+{
+  register int i;
+
+  /* Take care of the exit trap first */
+  if (sigmodes[EXIT_TRAP] & SIG_TRAPPED)
+    {
+      sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED;
+      if (reset != reset_signal)
+       {
+         free_trap_command (EXIT_TRAP);
+         trap_list[EXIT_TRAP] = (char *)NULL;
+       }
+    }
+
+  for (i = 1; i < NSIG; i++)
+    {
+      if (sigmodes[i] & SIG_TRAPPED)
+       {
+         if (trap_list[i] == (char *)IGNORE_SIG)
+           set_signal_handler (i, SIG_IGN);
+         else
+           (*reset) (i);
+       }
+      else if (sigmodes[i] & SIG_SPECIAL)
+       (*reset) (i);
+    }
+
+  /* Command substitution and other child processes don't inherit the
+     debug, error, or return traps.  If we're in the debugger, and the
+     `functrace' or `errtrace' options have been set, then let command
+     substitutions inherit them.  Let command substitution inherit the
+     RETURN trap if we're in the debugger and tracing functions. */
+  if (function_trace_mode == 0)
+    {
+      sigmodes[DEBUG_TRAP] &= ~SIG_TRAPPED;
+      sigmodes[RETURN_TRAP] &= ~SIG_TRAPPED;
+    }
+  if (error_trace_mode == 0)
+    sigmodes[ERROR_TRAP] &= ~SIG_TRAPPED;
+}
+
+/* Reset trapped signals to their original values, but don't free the
+   trap strings.  Called by the command substitution code. */
+void
+reset_signal_handlers ()
+{
+  reset_or_restore_signal_handlers (reset_signal);
+}
+
+/* Reset all trapped signals to their original values.  Signals set to be
+   ignored with trap '' SIGNAL should be ignored, so we make sure that they
+   are.  Called by child processes after they are forked. */
+void
+restore_original_signals ()
+{
+  reset_or_restore_signal_handlers (restore_signal);
+}
+
+/* If a trap handler exists for signal SIG, then call it; otherwise just
+   return failure. */
+int
+maybe_call_trap_handler (sig)
+     int sig;
+{
+  /* Call the trap handler for SIG if the signal is trapped and not ignored. */
+  if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0))
+    {
+      switch (sig)
+       {
+       case SIGINT:
+         run_interrupt_trap ();
+         break;
+       case EXIT_TRAP:
+         run_exit_trap ();
+         break;
+       case DEBUG_TRAP:
+         run_debug_trap ();
+         break;
+       case ERROR_TRAP:
+         run_error_trap ();
+         break;
+       default:
+         trap_handler (sig);
+         break;
+       }
+      return (1);
+    }
+  else
+    return (0);
+}
+
+int
+signal_is_trapped (sig)
+     int sig;
+{
+  return (sigmodes[sig] & SIG_TRAPPED);
+}
+
+int
+signal_is_special (sig)
+     int sig;
+{
+  return (sigmodes[sig] & SIG_SPECIAL);
+}
+
+int
+signal_is_ignored (sig)
+     int sig;
+{
+  return (sigmodes[sig] & SIG_IGNORED);
+}
+
+void
+set_signal_ignored (sig)
+     int sig;
+{
+  sigmodes[sig] |= SIG_HARD_IGNORE;
+  original_signals[sig] = SIG_IGN;
+}
+
+int
+signal_in_progress (sig)
+     int sig;
+{
+  return (sigmodes[sig] & SIG_INPROGRESS);
+}
diff --git a/trap.c~ b/trap.c~
index 9c2ab164f5f9625e080e6dee05a69a9f2b2a0add..99fc50402ce02afb16b0e54eb9bbf9155f1827bf 100644 (file)
--- a/trap.c~
+++ b/trap.c~
@@ -86,6 +86,10 @@ extern procenv_t wait_intr_buf;
 extern int return_catch_flag, return_catch_value;
 extern int subshell_level;
 
+#if defined (PGRP_PIPE)
+extern int pgrp_pipe[2];
+#endif
+
 /* The list of things to do originally, before we started trapping. */
 SigHandler *original_signals[NSIG];
 
@@ -799,6 +803,7 @@ run_debug_trap ()
 {
   int trap_exit_value;
   pid_t save_pgrp;
+  int save_pipe[2];
 
   /* XXX - question:  should the DEBUG trap inherit the RETURN trap? */
   trap_exit_value = 0;
@@ -806,14 +811,26 @@ run_debug_trap ()
     {
 #if defined (JOB_CONTROL)
       save_pgrp = pipeline_pgrp;
-      pipeline_pgrp = shell_pgrp;
+      pipeline_pgrp = 0;
       save_pipeline (1);
+#  if defined (PGRP_PIPE)
+      save_pgrp_pipe (save_pipe, 1);
+#  endif
       stop_making_children ();
 #endif
+
       trap_exit_value = _run_trap_internal (DEBUG_TRAP, "debug trap");
+
 #if defined (JOB_CONTROL)
       pipeline_pgrp = save_pgrp;
       restore_pipeline (1);
+#  if defined (PGRP_PIPE)
+      close_pgrp_pipe ();
+      restore_pgrp_pipe (save_pipe);
+#  endif
+      if (pipeline_pgrp > 0)
+       give_terminal_to (pipeline_pgrp, 1);
+      notify_and_cleanup ();
 #endif
       
 #if defined (DEBUGGER)