commit bash-20041027 snapshot
authorChet Ramey <chet.ramey@case.edu>
Sat, 3 Dec 2011 18:38:37 +0000 (13:38 -0500)
committerChet Ramey <chet.ramey@case.edu>
Sat, 3 Dec 2011 18:38:37 +0000 (13:38 -0500)
54 files changed:
CWRU/CWRU.chlog
CWRU/CWRU.chlog~
MANIFEST
MANIFEST~
doc/bash.1
doc/bash.1~
doc/bashref.texi
doc/bashref.texi~
doc/version.texi
doc/version.texi~
lib/glob/glob.c
lib/glob/glob.c.save1 [new file with mode: 0644]
lib/glob/glob.c~ [new file with mode: 0644]
lib/readline/display.c
lib/readline/display.c~ [new file with mode: 0644]
lib/readline/histexpand.c
lib/readline/histexpand.c~ [new file with mode: 0644]
lib/readline/rlmbutil.h
lib/readline/text.c
lib/readline/text.c~ [new file with mode: 0644]
parse.y
shell.c
shell.c~ [new file with mode: 0644]
subst.c
subst.c~
tests/RUN-ONE-TEST
tests/array.right
tests/array.right~
tests/array.tests
tests/array.tests~
tests/array1.sub [new file with mode: 0644]
tests/array2.sub [new file with mode: 0644]
tests/exec.right
tests/exec.right~ [new file with mode: 0644]
tests/execscript
tests/execscript~ [new file with mode: 0644]
tests/exp-tests
tests/exp-tests~ [new file with mode: 0644]
tests/exp.right
tests/extglob3.right
tests/extglob3.right~ [new file with mode: 0644]
tests/extglob3.tests
tests/extglob3.tests~ [new file with mode: 0644]
tests/intl.right [new file with mode: 0644]
tests/intl.tests [new file with mode: 0644]
tests/intl.tests~ [new file with mode: 0644]
tests/redir.right
tests/redir.tests
tests/redir.tests~ [new file with mode: 0644]
tests/run-intl [new file with mode: 0644]
tests/run-intl~ [new file with mode: 0644]
tests/tilde2.right
tests/tilde2.tests
tests/tilde2.tests~ [new file with mode: 0644]

index 75a1cc2..5bdfceb 100644 (file)
@@ -10431,3 +10431,39 @@ lib/readline/text.c
          multibyte locale) using above utility functions
        - fix rl_backward_word to work with multibyte characters (or in a
          multibyte locale) using above utility functions
+
+                                  10/26
+                                  -----
+parse.y
+       - fix parse_matched_pair so that it doesn't swallow \<newline> when
+         parsing a $'...' construct (call shell_getc with different arg)
+
+                                  10/28
+                                  -----
+lib/glob/glob.c
+       - after some (compiled-in) threshold, glob_vector will stop using
+         alloca to allocate `struct globval's and will switch to using
+         malloc, with appropriate cleanup before returning
+
+subst.c
+       - don't expand tildes after `=' in expand_word_internal, even if the
+         W_TILDEEXP flag is set, unless it's the first tilde in a word
+         marked W_ASSIGNMENT
+
+                                  10/31
+                                  -----
+lib/readline/text.c
+       - make sure rl_point doesn't go below 0 in rl_delete_horizontal_space
+         (from SUSE, but not sent in)
+
+shell.c
+       - make sure shell_is_restricted skips over a single leading `-' in
+         the shell name (from SUSE, but not sent in)
+
+lib/readline/display.c
+       - disable `fast redisplay' at the end of the line if in a locale that
+         supports multibyte characters (from SUSE, but not sent in)
+
+lib/readline/histexpand.c
+       - fix a problem with finding the delimiter of a `?' substring when
+         compiled for multibyte characters (from SUSE, but not sent in)
index 1fbb6a9..b3ba6ee 100644 (file)
@@ -10422,7 +10422,44 @@ lib/readline/mbutil.c
 lib/readline/rlmbutil.h
        - extern defines for _rl_walphabetic and _rl_char_value for when
          multibyte chars are not being used
+       - new wrapper definitions for _rl_find_next_mbchar (MB_NEXTCHAR) and
+         _rl_find_prev_mbchar (MB_PREVCHAR) that try to avoid unneeded
+         function calls
 
 lib/readline/text.c
        - fix rl_foward_word to work with multibyte characters (or in a
          multibyte locale) using above utility functions
+       - fix rl_backward_word to work with multibyte characters (or in a
+         multibyte locale) using above utility functions
+
+                                  10/26
+                                  -----
+parse.y
+       - fix parse_matched_pair so that it doesn't swallow \<newline> when
+         parsing a $'...' construct (call shell_getc with different arg)
+
+                                  10/28
+                                  -----
+lib/glob/glob.c
+       - after some (compiled-in) threshold, glob_vector will stop using
+         alloca to allocate `struct globval's and will switch to using
+         malloc, with appropriate cleanup before returning
+
+subst.c
+       - don't expand tildes after `=' in expand_word_internal, even if the
+         W_TILDEEXP flag is set, unless it's the first tilde in a word
+         marked W_ASSIGNMENT
+
+                                  10/31
+                                  -----
+lib/readline/text.c
+       - make sure rl_point doesn't go below 0 in rl_delete_horizontal_space
+         (from SUSE, but not sent in)
+
+shell.c
+       - make sure shell_is_restricted skips over a single leading `-' in
+         the shell name (from SUSE, but not sent in)
+
+lib/readline/display.c
+       - disable `fast redisplay' at the end of the line if in a locale that
+         supports multibyte characters (from SUSE, but not sent in)
index 2985f54..0207ba5 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -683,6 +683,8 @@ tests/arith1.sub    f
 tests/arith2.sub       f
 tests/array.tests      f
 tests/array.right      f
+tests/array1.sub       f
+tests/array2.sub       f
 tests/array-at-star    f
 tests/array2.right     f
 tests/braces.tests     f
@@ -764,6 +766,8 @@ tests/ifs.right             f
 tests/input-line.sh    f
 tests/input-line.sub   f
 tests/input.right      f
+tests/intl.tests       f
+tests/intl.right       f
 tests/invert.tests     f
 tests/invert.right     f
 tests/jobs.tests       f
@@ -852,6 +856,7 @@ tests/run-histexpand        f
 tests/run-history      f
 tests/run-ifs          f
 tests/run-input-test   f
+tests/run-intl         f
 tests/run-invert       f
 tests/run-jobs         f
 tests/run-more-exp     f
index a7b7be3..4742287 100644 (file)
--- a/MANIFEST~
+++ b/MANIFEST~
@@ -408,6 +408,7 @@ lib/sh/strftime.c   f
 lib/sh/strindex.c      f
 lib/sh/stringlist.c    f
 lib/sh/stringvec.c     f
+lib/sh/strnlen.c       f
 lib/sh/strpbrk.c       f
 lib/sh/strstr.c                f
 lib/sh/strtod.c                f
@@ -730,6 +731,8 @@ tests/extglob.tests f
 tests/extglob.right    f
 tests/extglob2.tests   f
 tests/extglob2.right   f
+tests/extglob3.tests   f
+tests/extglob3.right   f
 tests/func.tests       f
 tests/func.right       f
 tests/func1.sub                f
@@ -761,6 +764,8 @@ tests/ifs.right             f
 tests/input-line.sh    f
 tests/input-line.sub   f
 tests/input.right      f
+tests/intl.tests       f
+tests/intl.right       f
 tests/invert.tests     f
 tests/invert.right     f
 tests/jobs.tests       f
@@ -839,6 +844,7 @@ tests/run-execscript        f
 tests/run-exp-tests    f
 tests/run-extglob      f
 tests/run-extglob2     f
+tests/run-extglob3     f
 tests/run-func         f
 tests/run-getopts      f
 tests/run-glob-test    f
@@ -848,6 +854,7 @@ tests/run-histexpand        f
 tests/run-history      f
 tests/run-ifs          f
 tests/run-input-test   f
+tests/run-intl         f
 tests/run-invert       f
 tests/run-jobs         f
 tests/run-more-exp     f
@@ -872,6 +879,7 @@ tests/run-shopt             f
 tests/run-strip                f
 tests/run-test         f
 tests/run-tilde                f
+tests/run-tilde2       f
 tests/run-trap         f
 tests/run-type         f
 tests/run-varenv       f
@@ -885,8 +893,10 @@ tests/strip.tests  f
 tests/strip.right      f
 tests/test.tests       f
 tests/test.right       f
-tests/tilde-tests      f
+tests/tilde.tests      f
 tests/tilde.right      f
+tests/tilde2.tests     f
+tests/tilde2.right     f
 tests/trap.tests       f
 tests/trap.right       f
 tests/trap1.sub                f       755
index 86504ef..e607d98 100644 (file)
@@ -6,12 +6,12 @@
 .\"    Case Western Reserve University
 .\"    chet@po.CWRU.Edu
 .\"
-.\"    Last Change: Sat Oct  2 18:05:57 EDT 2004
+.\"    Last Change: Sat Oct 30 22:24:07 EDT 2004
 .\"
 .\" bash_builtins, strip all but Built-Ins section
 .if \n(zZ=1 .ig zZ
 .if \n(zY=1 .ig zY
-.TH BASH 1 "2004 Oct 2" "GNU Bash-3.1-devel"
+.TH BASH 1 "2004 Oct 30" "GNU Bash-3.1-devel"
 .\"
 .\" There's some problem with having a `@'
 .\" in a tagged paragraph with the BSD man macros.
@@ -8541,9 +8541,9 @@ subsequently reset.  The exit status is true unless a
 .I name
 is readonly.
 .TP
-\fBwait\fP [\fIn\fP]
-Wait for the specified process and return its termination
-status.
+\fBwait\fP [\fIn ...\fP]
+Wait for each specified process and return its termination status.
+Each
 .I n
 may be a process
 ID or a job specification; if a job spec is given, all processes
index 7eba6f0..86504ef 100644 (file)
@@ -4516,6 +4516,11 @@ If set to \fBnone\fP, readline never rings the bell.  If set to
 \fBvisible\fP, readline uses a visible bell if one is available.
 If set to \fBaudible\fP, readline attempts to ring the terminal's bell.
 .TP
+.B bind\-tty\-special\-chars (On)
+If set to \fBOn\fP, readline attempts to bind the control characters
+treated specially by the kernel's terminal driver to their readline
+equivalents.
+.TP
 .B comment\-begin (``#'')
 The string that is inserted when the readline
 .B insert\-comment
index 94814d6..b581aaa 100644 (file)
@@ -6252,11 +6252,11 @@ or non-zero if an error occurs or an invalid option is encountered.
 @item wait
 @btindex wait
 @example
-wait [@var{jobspec} or @var{pid}]
+wait [@var{jobspec} or @var{pid} ...]
 @end example
-Wait until the child process specified by process @sc{id} @var{pid} or job
-specification @var{jobspec} exits and return the exit status of the last
-command waited for.
+Wait until the child process specified by each process @sc{id} @var{pid}
+or job specification @var{jobspec} exits and return the exit status of the
+last command waited for.
 If a job spec is given, all processes in the job are waited for.
 If no arguments are given, all currently active child processes are
 waited for, and the return status is zero.
index 9df7ead..94814d6 100644 (file)
@@ -2017,6 +2017,10 @@ connection to the corresponding socket.
 
 A failure to open or create a file causes the redirection to fail.
 
+Redirections using file descriptors greater than 9 should be used with
+care, as they may conflict with file descriptors the shell uses
+internally.
+
 @subsection Redirecting Input
 Redirection of input causes the file whose name results from
 the expansion of @var{word}
index 0490f6a..d26a6fa 100644 (file)
@@ -4,7 +4,7 @@ Copyright (C) 1988-2004 Free Software Foundation, Inc.
 
 @set EDITION 3.1-devel
 @set VERSION 3.1-devel
-@set UPDATED 20 October 2004
+@set UPDATED 30 October 2004
 @set UPDATED-MONTH October 2004
 
-@set LASTCHANGE Wed Oct 20 09:54:44 EDT 2004
+@set LASTCHANGE Sat Oct 30 22:24:26 EDT 2004
index fe49140..0490f6a 100644 (file)
@@ -4,7 +4,7 @@ Copyright (C) 1988-2004 Free Software Foundation, Inc.
 
 @set EDITION 3.1-devel
 @set VERSION 3.1-devel
-@set UPDATED 9 October 2004
+@set UPDATED 20 October 2004
 @set UPDATED-MONTH October 2004
 
-@set LASTCHANGE Sat Oct  9 18:40:05 EDT 2004
+@set LASTCHANGE Wed Oct 20 09:54:44 EDT 2004
index a62054e..69c4959 100644 (file)
 #  define FREE(x)      if (x) free (x)
 #endif
 
+/* Don't try to alloca() more than this much memory for `struct globval'
+   in glob_vector() */
+#ifndef ALLOCA_MAX
+#  define ALLOCA_MAX   100000
+#endif
+
 extern void throw_to_top_level __P((void));
 extern int test_eaccess __P((char *, int));
 
@@ -347,10 +353,14 @@ glob_vector (pat, dir, flags)
   register char **name_vector;
   register unsigned int i;
   int mflags;          /* Flags passed to strmatch (). */
+  int nalloca;
+  struct globval *firstmalloc, *tmplink;
 
   lastlink = 0;
   count = lose = skip = 0;
 
+  firstmalloc = 0;
+
   /* If PAT is empty, skip the loop, but return one (empty) filename. */
   if (pat == 0 || *pat == '\0')
     {
@@ -488,8 +498,18 @@ glob_vector (pat, dir, flags)
 
          if (strmatch (pat, dp->d_name, mflags) != FNM_NOMATCH)
            {
+             if (nalloca < ALLOCA_MAX)
+               {
+                 nextlink = (struct globval *) alloca (sizeof (struct globval));
+                 nalloca += sizeof (struct globval);
+               }
+             else
+               {
+                 nextlink = (struct globval *) malloc (sizeof (struct globval));
+                 if (firstmalloc == 0)
+                   firstmalloc = nextlink;
+               }
              nextname = (char *) malloc (D_NAMLEN (dp) + 1);
-             nextlink = (struct globval *) alloca (sizeof (struct globval));
              if (nextlink == 0 || nextname == 0)
                {
                  lose = 1;
@@ -515,11 +535,20 @@ glob_vector (pat, dir, flags)
   /* Have we run out of memory?         */
   if (lose)
     {
+      tmplink = 0;
+
       /* Here free the strings we have got.  */
       while (lastlink)
        {
+         if (firstmalloc)
+           {
+             if (lastlink == firstmalloc)
+               firstmalloc = 0;
+             tmplink = lastlink;
+           }
          free (lastlink->name);
          lastlink = lastlink->next;
+         FREE (tmplink);
        }
 
       QUIT;
@@ -528,13 +557,29 @@ glob_vector (pat, dir, flags)
     }
 
   /* Copy the name pointers from the linked list into the vector.  */
-  for (i = 0; i < count; ++i)
+  for (tmplink = lastlink, i = 0; i < count; ++i)
     {
-      name_vector[i] = lastlink->name;
-      lastlink = lastlink->next;
+      name_vector[i] = tmplink->name;
+      tmplink = tmplink->next;
     }
 
   name_vector[count] = NULL;
+
+  /* If we allocated some of the struct globvals, free them now. */
+  if (firstmalloc)
+    {
+      tmplink = 0;
+      while (lastlink)
+       {
+         tmplink = lastlink;
+         if (lastlink == firstmalloc)
+           lastlink = firstmalloc = 0;
+         else
+           lastlink = lastlink->next;
+         free (tmplink);
+       }
+    }
+       
   return (name_vector);
 }
 
diff --git a/lib/glob/glob.c.save1 b/lib/glob/glob.c.save1
new file mode 100644 (file)
index 0000000..953472e
--- /dev/null
@@ -0,0 +1,888 @@
+/* glob.c -- file-name wildcard pattern matching for Bash.
+
+   Copyright (C) 1985-2002 Free Software Foundation, Inc.
+
+   This program 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 2, or (at your option)
+   any later version.
+
+   This program 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 this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.  */
+
+/* To whomever it may concern: I have never seen the code which most
+   Unix programs use to perform this function.  I wrote this from scratch
+   based on specifications for the pattern matching.  --RMS.  */
+
+#include <config.h>
+
+#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
+  #pragma alloca
+#endif /* _AIX && RISC6000 && !__GNUC__ */
+
+#include "bashtypes.h"
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif
+
+#include "bashansi.h"
+#include "posixdir.h"
+#include "posixstat.h"
+#include "shmbutil.h"
+#include "xmalloc.h"
+
+#include "filecntl.h"
+#if !defined (F_OK)
+#  define F_OK 0
+#endif
+
+#include "stdc.h"
+#include "memalloc.h"
+#include "quit.h"
+
+#include "glob.h"
+#include "strmatch.h"
+
+#if !defined (HAVE_BCOPY) && !defined (bcopy)
+#  define bcopy(s, d, n) ((void) memcpy ((d), (s), (n)))
+#endif /* !HAVE_BCOPY && !bcopy */
+
+#if !defined (NULL)
+#  if defined (__STDC__)
+#    define NULL ((void *) 0)
+#  else
+#    define NULL 0x0
+#  endif /* __STDC__ */
+#endif /* !NULL */
+
+#if !defined (FREE)
+#  define FREE(x)      if (x) free (x)
+#endif
+
+/* Don't try to alloca() more than this much memory for `struct globval'
+   in glob_vector() */
+#ifndef ALLOCA_MAX
+#  define ALLOCA_MAX   128     /* Testing, was 100000 */
+#endif
+
+extern void throw_to_top_level __P((void));
+extern int test_eaccess __P((char *, int));
+
+extern int extended_glob;
+
+/* Global variable which controls whether or not * matches .*.
+   Non-zero means don't match .*.  */
+int noglob_dot_filenames = 1;
+
+/* Global variable which controls whether or not filename globbing
+   is done without regard to case. */
+int glob_ignore_case = 0;
+
+/* Global variable to return to signify an error in globbing. */
+char *glob_error_return;
+
+/* Some forward declarations. */
+static int skipname __P((char *, char *));
+#if HANDLE_MULTIBYTE
+static int mbskipname __P((char *, char *));
+#endif
+#if HANDLE_MULTIBYTE
+static void udequote_pathname __P((char *));
+static void wdequote_pathname __P((char *));
+#else
+#  define dequote_pathname udequote_pathname
+#endif
+static void dequote_pathname __P((char *));
+static int glob_testdir __P((char *));
+static char **glob_dir_to_array __P((char *, char **, int));
+
+/* Compile `glob_loop.c' for single-byte characters. */
+#define CHAR   unsigned char
+#define INT    int
+#define L(CS)  CS
+#define INTERNAL_GLOB_PATTERN_P internal_glob_pattern_p
+#include "glob_loop.c"
+
+/* Compile `glob_loop.c' again for multibyte characters. */
+#if HANDLE_MULTIBYTE
+
+#define CHAR   wchar_t
+#define INT    wint_t
+#define L(CS)  L##CS
+#define INTERNAL_GLOB_PATTERN_P internal_glob_wpattern_p
+#include "glob_loop.c"
+
+#endif /* HANDLE_MULTIBYTE */
+
+/* And now a function that calls either the single-byte or multibyte version
+   of internal_glob_pattern_p. */
+int
+glob_pattern_p (pattern)
+     const char *pattern;
+{
+#if HANDLE_MULTIBYTE
+  size_t n;
+  wchar_t *wpattern;
+  int r;
+
+  if (MB_CUR_MAX == 1)
+    return (internal_glob_pattern_p (pattern));
+
+  /* Convert strings to wide chars, and call the multibyte version. */
+  n = xdupmbstowcs (&wpattern, NULL, pattern);
+  if (n == (size_t)-1)
+    /* Oops.  Invalid multibyte sequence.  Try it as single-byte sequence. */
+    return (internal_glob_pattern_p (pattern));
+
+  r = internal_glob_wpattern_p (wpattern);
+  free (wpattern);
+
+  return r;
+#else
+  return (internal_glob_pattern_p (pattern));
+#endif
+}
+
+/* Return 1 if DNAME should be skipped according to PAT.  Mostly concerned
+   with matching leading `.'. */
+
+static int
+skipname (pat, dname)
+     char *pat;
+     char *dname;
+{
+  /* If a leading dot need not be explicitly matched, and the pattern
+     doesn't start with a `.', don't match `.' or `..' */
+  if (noglob_dot_filenames == 0 && pat[0] != '.' &&
+       (pat[0] != '\\' || pat[1] != '.') &&
+       (dname[0] == '.' &&
+         (dname[1] == '\0' || (dname[1] == '.' && dname[2] == '\0'))))
+    return 1;
+
+  /* If a dot must be explicity matched, check to see if they do. */
+  else if (noglob_dot_filenames && dname[0] == '.' && pat[0] != '.' &&
+       (pat[0] != '\\' || pat[1] != '.'))
+    return 1;
+
+  return 0;
+}
+
+#if HANDLE_MULTIBYTE
+/* Return 1 if DNAME should be skipped according to PAT.  Handles multibyte
+   characters in PAT and DNAME.  Mostly concerned with matching leading `.'. */
+
+static int
+mbskipname (pat, dname)
+     char *pat, *dname;
+{
+  int ret;
+  wchar_t *pat_wc, *dn_wc;
+  size_t pat_n, dn_n, n;
+
+  pat_n = xdupmbstowcs (&pat_wc, NULL, pat);
+  dn_n = xdupmbstowcs (&dn_wc, NULL, dname);
+
+  ret = 0;
+  if (pat_n != (size_t)-1 && dn_n !=(size_t)-1)
+    {
+      /* If a leading dot need not be explicitly matched, and the
+        pattern doesn't start with a `.', don't match `.' or `..' */
+      if (noglob_dot_filenames == 0 && pat_wc[0] != L'.' &&
+           (pat_wc[0] != L'\\' || pat_wc[1] != L'.') &&
+           (dn_wc[0] == L'.' &&
+             (dn_wc[1] == L'\0' || (dn_wc[1] == L'.' && dn_wc[2] == L'\0'))))
+       ret = 1;
+
+      /* If a leading dot must be explicity matched, check to see if the
+        pattern and dirname both have one. */
+     else if (noglob_dot_filenames && dn_wc[0] == L'.' &&
+          pat_wc[0] != L'.' &&
+          (pat_wc[0] != L'\\' || pat_wc[1] != L'.'))
+       ret = 1;
+    }
+
+  FREE (pat_wc);
+  FREE (dn_wc);
+
+  return ret;
+}
+#endif /* HANDLE_MULTIBYTE */
+
+/* Remove backslashes quoting characters in PATHNAME by modifying PATHNAME. */
+static void
+udequote_pathname (pathname)
+     char *pathname;
+{
+  register int i, j;
+
+  for (i = j = 0; pathname && pathname[i]; )
+    {
+      if (pathname[i] == '\\')
+       i++;
+
+      pathname[j++] = pathname[i++];
+
+      if (pathname[i - 1] == 0)
+       break;
+    }
+  pathname[j] = '\0';
+}
+
+#if HANDLE_MULTIBYTE
+/* Remove backslashes quoting characters in PATHNAME by modifying PATHNAME. */
+static void
+wdequote_pathname (pathname)
+     char *pathname;
+{
+  mbstate_t ps;
+  size_t len, n;
+  wchar_t *wpathname;
+  int i, j;
+  wchar_t *orig_wpathname;
+
+  len = strlen (pathname);
+  /* Convert the strings into wide characters.  */
+  n = xdupmbstowcs (&wpathname, NULL, pathname);
+  if (n == (size_t) -1)
+    /* Something wrong. */
+    return;
+  orig_wpathname = wpathname;
+
+  for (i = j = 0; wpathname && wpathname[i]; )
+    {
+      if (wpathname[i] == L'\\')
+       i++;
+
+      wpathname[j++] = wpathname[i++];
+
+      if (wpathname[i - 1] == L'\0')
+       break;
+    }
+  wpathname[j] = L'\0';
+
+  /* Convert the wide character string into unibyte character set. */
+  memset (&ps, '\0', sizeof(mbstate_t));
+  n = wcsrtombs(pathname, (const wchar_t **)&wpathname, len, &ps);
+  pathname[len] = '\0';
+
+  /* Can't just free wpathname here; wcsrtombs changes it in many cases. */
+  free (orig_wpathname);
+}
+
+static void
+dequote_pathname (pathname)
+     char *pathname;
+{
+  if (MB_CUR_MAX > 1)
+    wdequote_pathname (pathname);
+  else
+    udequote_pathname (pathname);
+}
+#endif /* HANDLE_MULTIBYTE */
+
+/* Test whether NAME exists. */
+
+#if defined (HAVE_LSTAT)
+#  define GLOB_TESTNAME(name)  (lstat (name, &finfo))
+#else /* !HAVE_LSTAT */
+#  if !defined (AFS)
+#    define GLOB_TESTNAME(name)  (test_eaccess (nextname, F_OK))
+#  else /* AFS */
+#    define GLOB_TESTNAME(name)  (access (nextname, F_OK))
+#  endif /* AFS */
+#endif /* !HAVE_LSTAT */
+
+/* Return 0 if DIR is a directory, -1 otherwise. */
+static int
+glob_testdir (dir)
+     char *dir;
+{
+  struct stat finfo;
+
+  if (stat (dir, &finfo) < 0)
+    return (-1);
+
+  if (S_ISDIR (finfo.st_mode) == 0)
+    return (-1);
+
+  return (0);
+}
+
+/* Return a vector of names of files in directory DIR
+   whose names match glob pattern PAT.
+   The names are not in any particular order.
+   Wildcards at the beginning of PAT do not match an initial period.
+
+   The vector is terminated by an element that is a null pointer.
+
+   To free the space allocated, first free the vector's elements,
+   then free the vector.
+
+   Return 0 if cannot get enough memory to hold the pointer
+   and the names.
+
+   Return -1 if cannot access directory DIR.
+   Look in errno for more information.  */
+
+char **
+glob_vector (pat, dir, flags)
+     char *pat;
+     char *dir;
+     int flags;
+{
+  struct globval
+    {
+      struct globval *next;
+      char *name;
+    };
+
+  DIR *d;
+  register struct dirent *dp;
+  struct globval *lastlink;
+  register struct globval *nextlink;
+  register char *nextname, *npat;
+  unsigned int count;
+  int lose, skip;
+  register char **name_vector;
+  register unsigned int i;
+  int mflags;          /* Flags passed to strmatch (). */
+  int nalloca;
+  struct globval *firstmalloc, *tmplink;
+
+  lastlink = 0;
+  count = lose = skip = 0;
+
+  firstmalloc = 0;
+
+  /* If PAT is empty, skip the loop, but return one (empty) filename. */
+  if (pat == 0 || *pat == '\0')
+    {
+      if (glob_testdir (dir) < 0)
+       return ((char **) &glob_error_return);
+
+      nextlink = (struct globval *)alloca (sizeof (struct globval));
+      if (nextlink == NULL)
+       return ((char **) NULL);
+
+      nextlink->next = (struct globval *)0;
+      nextname = (char *) malloc (1);
+      if (nextname == 0)
+       lose = 1;
+      else
+       {
+         lastlink = nextlink;
+         nextlink->name = nextname;
+         nextname[0] = '\0';
+         count = 1;
+       }
+
+      skip = 1;
+    }
+
+  /* If the filename pattern (PAT) does not contain any globbing characters,
+     we can dispense with reading the directory, and just see if there is
+     a filename `DIR/PAT'.  If there is, and we can access it, just make the
+     vector to return and bail immediately. */
+  if (skip == 0 && glob_pattern_p (pat) == 0)
+    {
+      int dirlen;
+      struct stat finfo;
+
+      if (glob_testdir (dir) < 0)
+       return ((char **) &glob_error_return);
+
+      dirlen = strlen (dir);
+      nextname = (char *)malloc (dirlen + strlen (pat) + 2);
+      npat = (char *)malloc (strlen (pat) + 1);
+      if (nextname == 0 || npat == 0)
+       lose = 1;
+      else
+       {
+         strcpy (npat, pat);
+         dequote_pathname (npat);
+
+         strcpy (nextname, dir);
+         nextname[dirlen++] = '/';
+         strcpy (nextname + dirlen, npat);
+
+         if (GLOB_TESTNAME (nextname) >= 0)
+           {
+             free (nextname);
+             nextlink = (struct globval *)alloca (sizeof (struct globval));
+             if (nextlink)
+               {
+                 nextlink->next = (struct globval *)0;
+                 lastlink = nextlink;
+                 nextlink->name = npat;
+                 count = 1;
+               }
+             else
+               lose = 1;
+           }
+         else
+           {
+             free (nextname);
+             free (npat);
+           }
+       }
+
+      skip = 1;
+    }
+
+  if (skip == 0)
+    {
+      /* Open the directory, punting immediately if we cannot.  If opendir
+        is not robust (i.e., it opens non-directories successfully), test
+        that DIR is a directory and punt if it's not. */
+#if defined (OPENDIR_NOT_ROBUST)
+      if (glob_testdir (dir) < 0)
+       return ((char **) &glob_error_return);
+#endif
+
+      d = opendir (dir);
+      if (d == NULL)
+       return ((char **) &glob_error_return);
+
+      /* Compute the flags that will be passed to strmatch().  We don't
+        need to do this every time through the loop. */
+      mflags = (noglob_dot_filenames ? FNM_PERIOD : 0) | FNM_PATHNAME;
+
+#ifdef FNM_CASEFOLD
+      if (glob_ignore_case)
+       mflags |= FNM_CASEFOLD;
+#endif
+
+      if (extended_glob)
+       mflags |= FNM_EXTMATCH;
+
+      /* Scan the directory, finding all names that match.
+        For each name that matches, allocate a struct globval
+        on the stack and store the name in it.
+        Chain those structs together; lastlink is the front of the chain.  */
+      while (1)
+       {
+         /* Make globbing interruptible in the shell. */
+         if (interrupt_state)
+           {
+             lose = 1;
+             break;
+           }
+         
+         dp = readdir (d);
+         if (dp == NULL)
+           break;
+
+         /* If this directory entry is not to be used, try again. */
+         if (REAL_DIR_ENTRY (dp) == 0)
+           continue;
+
+#if 0
+         if (dp->d_name == 0 || *dp->d_name == 0)
+           continue;
+#endif
+
+#if HANDLE_MULTIBYTE
+         if (MB_CUR_MAX > 1 && mbskipname (pat, dp->d_name))
+           continue;
+         else
+#endif
+         if (skipname (pat, dp->d_name))
+           continue;
+
+         if (strmatch (pat, dp->d_name, mflags) != FNM_NOMATCH)
+           {
+             if (nalloca < ALLOCA_MAX)
+               {
+                 nextlink = (struct globval *) alloca (sizeof (struct globval));
+                 nalloca += sizeof (struct globval);
+               }
+             else
+               {
+                 nextlink = (struct globval *) malloc (sizeof (struct globval));
+                 if (firstmalloc == 0)
+                   firstmalloc = nextlink;
+               }
+             nextname = (char *) malloc (D_NAMLEN (dp) + 1);
+             if (nextlink == 0 || nextname == 0)
+               {
+                 lose = 1;
+                 break;
+               }
+             nextlink->next = lastlink;
+             lastlink = nextlink;
+             nextlink->name = nextname;
+             bcopy (dp->d_name, nextname, D_NAMLEN (dp) + 1);
+             ++count;
+           }
+       }
+
+      (void) closedir (d);
+    }
+
+  if (lose == 0)
+    {
+      name_vector = (char **) malloc ((count + 1) * sizeof (char *));
+      lose |= name_vector == NULL;
+    }
+
+  /* Have we run out of memory?         */
+  if (lose)
+    {
+      tmplink = 0;
+
+      /* Here free the strings we have got.  */
+      while (lastlink)
+       {
+         if (firstmalloc)
+           {
+             if (lastlink == firstmalloc)
+               firstmalloc = 0;
+             tmplink = lastlink;
+           }
+         free (lastlink->name);
+         lastlink = lastlink->next;
+         FREE (tmplink);
+       }
+
+      QUIT;
+
+      return ((char **)NULL);
+    }
+
+
+  tmplink = lastlink;  /* save in case we need to free */
+  /* Copy the name pointers from the linked list into the vector.  */
+  for (i = 0; i < count; ++i)
+    {
+      name_vector[i] = lastlink->name;
+      lastlink = lastlink->next;
+    }
+  lastlink = tmplink;  /* restore in case we need to free */
+
+  name_vector[count] = NULL;
+
+  /* If we allocated some of the struct globvals, free them now. */
+  if (firstmalloc)
+    {
+      tmplink = 0;
+      while (lastlink)
+       {
+         tmplink = lastlink;
+         if (lastlink == firstmalloc)
+           lastlink = firstmalloc = 0;
+         else
+           lastlink = lastlink->next;
+         free (tmplink);
+       }
+    }
+       
+  return (name_vector);
+}
+
+/* Return a new array which is the concatenation of each string in ARRAY
+   to DIR.  This function expects you to pass in an allocated ARRAY, and
+   it takes care of free()ing that array.  Thus, you might think of this
+   function as side-effecting ARRAY.  This should handle GX_MARKDIRS. */
+static char **
+glob_dir_to_array (dir, array, flags)
+     char *dir, **array;
+     int flags;
+{
+  register unsigned int i, l;
+  int add_slash;
+  char **result, *new;
+  struct stat sb;
+
+  l = strlen (dir);
+  if (l == 0)
+    {
+      if (flags & GX_MARKDIRS)
+       for (i = 0; array[i]; i++)
+         {
+           if ((stat (array[i], &sb) == 0) && S_ISDIR (sb.st_mode))
+             {
+               l = strlen (array[i]);
+               new = (char *)realloc (array[i], l + 2);
+               if (new == 0)
+                 return NULL;
+               new[l] = '/';
+               new[l+1] = '\0';
+               array[i] = new;
+             }
+         }
+      return (array);
+    }
+
+  add_slash = dir[l - 1] != '/';
+
+  i = 0;
+  while (array[i] != NULL)
+    ++i;
+
+  result = (char **) malloc ((i + 1) * sizeof (char *));
+  if (result == NULL)
+    return (NULL);
+
+  for (i = 0; array[i] != NULL; i++)
+    {
+      /* 3 == 1 for NUL, 1 for slash at end of DIR, 1 for GX_MARKDIRS */
+      result[i] = (char *) malloc (l + strlen (array[i]) + 3);
+
+      if (result[i] == NULL)
+       return (NULL);
+
+      strcpy (result[i], dir);
+      if (add_slash)
+       result[i][l] = '/';
+      strcpy (result[i] + l + add_slash, array[i]);
+      if (flags & GX_MARKDIRS)
+       {
+         if ((stat (result[i], &sb) == 0) && S_ISDIR (sb.st_mode))
+           {
+             size_t rlen;
+             rlen = strlen (result[i]);
+             result[i][rlen] = '/';
+             result[i][rlen+1] = '\0';
+           }
+       }
+    }
+  result[i] = NULL;
+
+  /* Free the input array.  */
+  for (i = 0; array[i] != NULL; i++)
+    free (array[i]);
+  free ((char *) array);
+
+  return (result);
+}
+
+/* Do globbing on PATHNAME.  Return an array of pathnames that match,
+   marking the end of the array with a null-pointer as an element.
+   If no pathnames match, then the array is empty (first element is null).
+   If there isn't enough memory, then return NULL.
+   If a file system error occurs, return -1; `errno' has the error code.  */
+char **
+glob_filename (pathname, flags)
+     char *pathname;
+     int flags;
+{
+  char **result;
+  unsigned int result_size;
+  char *directory_name, *filename;
+  unsigned int directory_len;
+  int free_dirname;                    /* flag */
+
+  result = (char **) malloc (sizeof (char *));
+  result_size = 1;
+  if (result == NULL)
+    return (NULL);
+
+  result[0] = NULL;
+
+  directory_name = NULL;
+
+  /* Find the filename.  */
+  filename = strrchr (pathname, '/');
+  if (filename == NULL)
+    {
+      filename = pathname;
+      directory_name = "";
+      directory_len = 0;
+      free_dirname = 0;
+    }
+  else
+    {
+      directory_len = (filename - pathname) + 1;
+      directory_name = (char *) malloc (directory_len + 1);
+
+      if (directory_name == 0)         /* allocation failed? */
+       return (NULL);
+
+      bcopy (pathname, directory_name, directory_len);
+      directory_name[directory_len] = '\0';
+      ++filename;
+      free_dirname = 1;
+    }
+
+  /* If directory_name contains globbing characters, then we
+     have to expand the previous levels.  Just recurse. */
+  if (glob_pattern_p (directory_name))
+    {
+      char **directories;
+      register unsigned int i;
+
+      if (directory_name[directory_len - 1] == '/')
+       directory_name[directory_len - 1] = '\0';
+
+      directories = glob_filename (directory_name, flags & ~GX_MARKDIRS);
+
+      if (free_dirname)
+       {
+         free (directory_name);
+         directory_name = NULL;
+       }
+
+      if (directories == NULL)
+       goto memory_error;
+      else if (directories == (char **)&glob_error_return)
+       {
+         free ((char *) result);
+         return ((char **) &glob_error_return);
+       }
+      else if (*directories == NULL)
+       {
+         free ((char *) directories);
+         free ((char *) result);
+         return ((char **) &glob_error_return);
+       }
+
+      /* We have successfully globbed the preceding directory name.
+        For each name in DIRECTORIES, call glob_vector on it and
+        FILENAME.  Concatenate the results together.  */
+      for (i = 0; directories[i] != NULL; ++i)
+       {
+         char **temp_results;
+
+         /* Scan directory even on a NULL pathname.  That way, `*h/'
+            returns only directories ending in `h', instead of all
+            files ending in `h' with a `/' appended. */
+         temp_results = glob_vector (filename, directories[i], flags & ~GX_MARKDIRS);
+
+         /* Handle error cases. */
+         if (temp_results == NULL)
+           goto memory_error;
+         else if (temp_results == (char **)&glob_error_return)
+           /* This filename is probably not a directory.  Ignore it.  */
+           ;
+         else
+           {
+             char **array;
+             register unsigned int l;
+
+             array = glob_dir_to_array (directories[i], temp_results, flags);
+             l = 0;
+             while (array[l] != NULL)
+               ++l;
+
+             result =
+               (char **)realloc (result, (result_size + l) * sizeof (char *));
+
+             if (result == NULL)
+               goto memory_error;
+
+             for (l = 0; array[l] != NULL; ++l)
+               result[result_size++ - 1] = array[l];
+
+             result[result_size - 1] = NULL;
+
+             /* Note that the elements of ARRAY are not freed.  */
+             free ((char *) array);
+           }
+       }
+      /* Free the directories.  */
+      for (i = 0; directories[i]; i++)
+       free (directories[i]);
+
+      free ((char *) directories);
+
+      return (result);
+    }
+
+  /* If there is only a directory name, return it. */
+  if (*filename == '\0')
+    {
+      result = (char **) realloc ((char *) result, 2 * sizeof (char *));
+      if (result == NULL)
+       return (NULL);
+      /* Handle GX_MARKDIRS here. */
+      result[0] = (char *) malloc (directory_len + 1);
+      if (result[0] == NULL)
+       goto memory_error;
+      bcopy (directory_name, result[0], directory_len + 1);
+      if (free_dirname)
+       free (directory_name);
+      result[1] = NULL;
+      return (result);
+    }
+  else
+    {
+      char **temp_results;
+
+      /* There are no unquoted globbing characters in DIRECTORY_NAME.
+        Dequote it before we try to open the directory since there may
+        be quoted globbing characters which should be treated verbatim. */
+      if (directory_len > 0)
+       dequote_pathname (directory_name);
+
+      /* We allocated a small array called RESULT, which we won't be using.
+        Free that memory now. */
+      free (result);
+
+      /* Just return what glob_vector () returns appended to the
+        directory name. */
+      temp_results = glob_vector (filename,
+                                 (directory_len == 0 ? "." : directory_name),
+                                 flags & ~GX_MARKDIRS);
+
+      if (temp_results == NULL || temp_results == (char **)&glob_error_return)
+       {
+         if (free_dirname)
+           free (directory_name);
+         return (temp_results);
+       }
+
+      result = glob_dir_to_array (directory_name, temp_results, flags);
+      if (free_dirname)
+       free (directory_name);
+      return (result);
+    }
+
+  /* We get to memory_error if the program has run out of memory, or
+     if this is the shell, and we have been interrupted. */
+ memory_error:
+  if (result != NULL)
+    {
+      register unsigned int i;
+      for (i = 0; result[i] != NULL; ++i)
+       free (result[i]);
+      free ((char *) result);
+    }
+
+  if (free_dirname && directory_name)
+    free (directory_name);
+
+  QUIT;
+
+  return (NULL);
+}
+
+#if defined (TEST)
+
+main (argc, argv)
+     int argc;
+     char **argv;
+{
+  unsigned int i;
+
+  for (i = 1; i < argc; ++i)
+    {
+      char **value = glob_filename (argv[i], 0);
+      if (value == NULL)
+       puts ("Out of memory.");
+      else if (value == &glob_error_return)
+       perror (argv[i]);
+      else
+       for (i = 0; value[i] != NULL; i++)
+         puts (value[i]);
+    }
+
+  exit (0);
+}
+#endif /* TEST.  */
diff --git a/lib/glob/glob.c~ b/lib/glob/glob.c~
new file mode 100644 (file)
index 0000000..f50b596
--- /dev/null
@@ -0,0 +1,885 @@
+/* glob.c -- file-name wildcard pattern matching for Bash.
+
+   Copyright (C) 1985-2002 Free Software Foundation, Inc.
+
+   This program 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 2, or (at your option)
+   any later version.
+
+   This program 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 this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.  */
+
+/* To whomever it may concern: I have never seen the code which most
+   Unix programs use to perform this function.  I wrote this from scratch
+   based on specifications for the pattern matching.  --RMS.  */
+
+#include <config.h>
+
+#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
+  #pragma alloca
+#endif /* _AIX && RISC6000 && !__GNUC__ */
+
+#include "bashtypes.h"
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif
+
+#include "bashansi.h"
+#include "posixdir.h"
+#include "posixstat.h"
+#include "shmbutil.h"
+#include "xmalloc.h"
+
+#include "filecntl.h"
+#if !defined (F_OK)
+#  define F_OK 0
+#endif
+
+#include "stdc.h"
+#include "memalloc.h"
+#include "quit.h"
+
+#include "glob.h"
+#include "strmatch.h"
+
+#if !defined (HAVE_BCOPY) && !defined (bcopy)
+#  define bcopy(s, d, n) ((void) memcpy ((d), (s), (n)))
+#endif /* !HAVE_BCOPY && !bcopy */
+
+#if !defined (NULL)
+#  if defined (__STDC__)
+#    define NULL ((void *) 0)
+#  else
+#    define NULL 0x0
+#  endif /* __STDC__ */
+#endif /* !NULL */
+
+#if !defined (FREE)
+#  define FREE(x)      if (x) free (x)
+#endif
+
+/* Don't try to alloca() more than this much memory for `struct globval'
+   in glob_vector() */
+#ifndef ALLOCA_MAX
+#  define ALLOCA_MAX   128     /* Testing, was 100000 */
+#endif
+
+extern void throw_to_top_level __P((void));
+extern int test_eaccess __P((char *, int));
+
+extern int extended_glob;
+
+/* Global variable which controls whether or not * matches .*.
+   Non-zero means don't match .*.  */
+int noglob_dot_filenames = 1;
+
+/* Global variable which controls whether or not filename globbing
+   is done without regard to case. */
+int glob_ignore_case = 0;
+
+/* Global variable to return to signify an error in globbing. */
+char *glob_error_return;
+
+/* Some forward declarations. */
+static int skipname __P((char *, char *));
+#if HANDLE_MULTIBYTE
+static int mbskipname __P((char *, char *));
+#endif
+#if HANDLE_MULTIBYTE
+static void udequote_pathname __P((char *));
+static void wdequote_pathname __P((char *));
+#else
+#  define dequote_pathname udequote_pathname
+#endif
+static void dequote_pathname __P((char *));
+static int glob_testdir __P((char *));
+static char **glob_dir_to_array __P((char *, char **, int));
+
+/* Compile `glob_loop.c' for single-byte characters. */
+#define CHAR   unsigned char
+#define INT    int
+#define L(CS)  CS
+#define INTERNAL_GLOB_PATTERN_P internal_glob_pattern_p
+#include "glob_loop.c"
+
+/* Compile `glob_loop.c' again for multibyte characters. */
+#if HANDLE_MULTIBYTE
+
+#define CHAR   wchar_t
+#define INT    wint_t
+#define L(CS)  L##CS
+#define INTERNAL_GLOB_PATTERN_P internal_glob_wpattern_p
+#include "glob_loop.c"
+
+#endif /* HANDLE_MULTIBYTE */
+
+/* And now a function that calls either the single-byte or multibyte version
+   of internal_glob_pattern_p. */
+int
+glob_pattern_p (pattern)
+     const char *pattern;
+{
+#if HANDLE_MULTIBYTE
+  size_t n;
+  wchar_t *wpattern;
+  int r;
+
+  if (MB_CUR_MAX == 1)
+    return (internal_glob_pattern_p (pattern));
+
+  /* Convert strings to wide chars, and call the multibyte version. */
+  n = xdupmbstowcs (&wpattern, NULL, pattern);
+  if (n == (size_t)-1)
+    /* Oops.  Invalid multibyte sequence.  Try it as single-byte sequence. */
+    return (internal_glob_pattern_p (pattern));
+
+  r = internal_glob_wpattern_p (wpattern);
+  free (wpattern);
+
+  return r;
+#else
+  return (internal_glob_pattern_p (pattern));
+#endif
+}
+
+/* Return 1 if DNAME should be skipped according to PAT.  Mostly concerned
+   with matching leading `.'. */
+
+static int
+skipname (pat, dname)
+     char *pat;
+     char *dname;
+{
+  /* If a leading dot need not be explicitly matched, and the pattern
+     doesn't start with a `.', don't match `.' or `..' */
+  if (noglob_dot_filenames == 0 && pat[0] != '.' &&
+       (pat[0] != '\\' || pat[1] != '.') &&
+       (dname[0] == '.' &&
+         (dname[1] == '\0' || (dname[1] == '.' && dname[2] == '\0'))))
+    return 1;
+
+  /* If a dot must be explicity matched, check to see if they do. */
+  else if (noglob_dot_filenames && dname[0] == '.' && pat[0] != '.' &&
+       (pat[0] != '\\' || pat[1] != '.'))
+    return 1;
+
+  return 0;
+}
+
+#if HANDLE_MULTIBYTE
+/* Return 1 if DNAME should be skipped according to PAT.  Handles multibyte
+   characters in PAT and DNAME.  Mostly concerned with matching leading `.'. */
+
+static int
+mbskipname (pat, dname)
+     char *pat, *dname;
+{
+  int ret;
+  wchar_t *pat_wc, *dn_wc;
+  size_t pat_n, dn_n, n;
+
+  pat_n = xdupmbstowcs (&pat_wc, NULL, pat);
+  dn_n = xdupmbstowcs (&dn_wc, NULL, dname);
+
+  ret = 0;
+  if (pat_n != (size_t)-1 && dn_n !=(size_t)-1)
+    {
+      /* If a leading dot need not be explicitly matched, and the
+        pattern doesn't start with a `.', don't match `.' or `..' */
+      if (noglob_dot_filenames == 0 && pat_wc[0] != L'.' &&
+           (pat_wc[0] != L'\\' || pat_wc[1] != L'.') &&
+           (dn_wc[0] == L'.' &&
+             (dn_wc[1] == L'\0' || (dn_wc[1] == L'.' && dn_wc[2] == L'\0'))))
+       ret = 1;
+
+      /* If a leading dot must be explicity matched, check to see if the
+        pattern and dirname both have one. */
+     else if (noglob_dot_filenames && dn_wc[0] == L'.' &&
+          pat_wc[0] != L'.' &&
+          (pat_wc[0] != L'\\' || pat_wc[1] != L'.'))
+       ret = 1;
+    }
+
+  FREE (pat_wc);
+  FREE (dn_wc);
+
+  return ret;
+}
+#endif /* HANDLE_MULTIBYTE */
+
+/* Remove backslashes quoting characters in PATHNAME by modifying PATHNAME. */
+static void
+udequote_pathname (pathname)
+     char *pathname;
+{
+  register int i, j;
+
+  for (i = j = 0; pathname && pathname[i]; )
+    {
+      if (pathname[i] == '\\')
+       i++;
+
+      pathname[j++] = pathname[i++];
+
+      if (pathname[i - 1] == 0)
+       break;
+    }
+  pathname[j] = '\0';
+}
+
+#if HANDLE_MULTIBYTE
+/* Remove backslashes quoting characters in PATHNAME by modifying PATHNAME. */
+static void
+wdequote_pathname (pathname)
+     char *pathname;
+{
+  mbstate_t ps;
+  size_t len, n;
+  wchar_t *wpathname;
+  int i, j;
+  wchar_t *orig_wpathname;
+
+  len = strlen (pathname);
+  /* Convert the strings into wide characters.  */
+  n = xdupmbstowcs (&wpathname, NULL, pathname);
+  if (n == (size_t) -1)
+    /* Something wrong. */
+    return;
+  orig_wpathname = wpathname;
+
+  for (i = j = 0; wpathname && wpathname[i]; )
+    {
+      if (wpathname[i] == L'\\')
+       i++;
+
+      wpathname[j++] = wpathname[i++];
+
+      if (wpathname[i - 1] == L'\0')
+       break;
+    }
+  wpathname[j] = L'\0';
+
+  /* Convert the wide character string into unibyte character set. */
+  memset (&ps, '\0', sizeof(mbstate_t));
+  n = wcsrtombs(pathname, (const wchar_t **)&wpathname, len, &ps);
+  pathname[len] = '\0';
+
+  /* Can't just free wpathname here; wcsrtombs changes it in many cases. */
+  free (orig_wpathname);
+}
+
+static void
+dequote_pathname (pathname)
+     char *pathname;
+{
+  if (MB_CUR_MAX > 1)
+    wdequote_pathname (pathname);
+  else
+    udequote_pathname (pathname);
+}
+#endif /* HANDLE_MULTIBYTE */
+
+/* Test whether NAME exists. */
+
+#if defined (HAVE_LSTAT)
+#  define GLOB_TESTNAME(name)  (lstat (name, &finfo))
+#else /* !HAVE_LSTAT */
+#  if !defined (AFS)
+#    define GLOB_TESTNAME(name)  (test_eaccess (nextname, F_OK))
+#  else /* AFS */
+#    define GLOB_TESTNAME(name)  (access (nextname, F_OK))
+#  endif /* AFS */
+#endif /* !HAVE_LSTAT */
+
+/* Return 0 if DIR is a directory, -1 otherwise. */
+static int
+glob_testdir (dir)
+     char *dir;
+{
+  struct stat finfo;
+
+  if (stat (dir, &finfo) < 0)
+    return (-1);
+
+  if (S_ISDIR (finfo.st_mode) == 0)
+    return (-1);
+
+  return (0);
+}
+
+/* Return a vector of names of files in directory DIR
+   whose names match glob pattern PAT.
+   The names are not in any particular order.
+   Wildcards at the beginning of PAT do not match an initial period.
+
+   The vector is terminated by an element that is a null pointer.
+
+   To free the space allocated, first free the vector's elements,
+   then free the vector.
+
+   Return 0 if cannot get enough memory to hold the pointer
+   and the names.
+
+   Return -1 if cannot access directory DIR.
+   Look in errno for more information.  */
+
+char **
+glob_vector (pat, dir, flags)
+     char *pat;
+     char *dir;
+     int flags;
+{
+  struct globval
+    {
+      struct globval *next;
+      char *name;
+    };
+
+  DIR *d;
+  register struct dirent *dp;
+  struct globval *lastlink;
+  register struct globval *nextlink;
+  register char *nextname, *npat;
+  unsigned int count;
+  int lose, skip;
+  register char **name_vector;
+  register unsigned int i;
+  int mflags;          /* Flags passed to strmatch (). */
+  int nalloca;
+  struct globval *firstmalloc, *tmplink;
+
+  lastlink = 0;
+  count = lose = skip = 0;
+
+  firstmalloc = 0;
+
+  /* If PAT is empty, skip the loop, but return one (empty) filename. */
+  if (pat == 0 || *pat == '\0')
+    {
+      if (glob_testdir (dir) < 0)
+       return ((char **) &glob_error_return);
+
+      nextlink = (struct globval *)alloca (sizeof (struct globval));
+      if (nextlink == NULL)
+       return ((char **) NULL);
+
+      nextlink->next = (struct globval *)0;
+      nextname = (char *) malloc (1);
+      if (nextname == 0)
+       lose = 1;
+      else
+       {
+         lastlink = nextlink;
+         nextlink->name = nextname;
+         nextname[0] = '\0';
+         count = 1;
+       }
+
+      skip = 1;
+    }
+
+  /* If the filename pattern (PAT) does not contain any globbing characters,
+     we can dispense with reading the directory, and just see if there is
+     a filename `DIR/PAT'.  If there is, and we can access it, just make the
+     vector to return and bail immediately. */
+  if (skip == 0 && glob_pattern_p (pat) == 0)
+    {
+      int dirlen;
+      struct stat finfo;
+
+      if (glob_testdir (dir) < 0)
+       return ((char **) &glob_error_return);
+
+      dirlen = strlen (dir);
+      nextname = (char *)malloc (dirlen + strlen (pat) + 2);
+      npat = (char *)malloc (strlen (pat) + 1);
+      if (nextname == 0 || npat == 0)
+       lose = 1;
+      else
+       {
+         strcpy (npat, pat);
+         dequote_pathname (npat);
+
+         strcpy (nextname, dir);
+         nextname[dirlen++] = '/';
+         strcpy (nextname + dirlen, npat);
+
+         if (GLOB_TESTNAME (nextname) >= 0)
+           {
+             free (nextname);
+             nextlink = (struct globval *)alloca (sizeof (struct globval));
+             if (nextlink)
+               {
+                 nextlink->next = (struct globval *)0;
+                 lastlink = nextlink;
+                 nextlink->name = npat;
+                 count = 1;
+               }
+             else
+               lose = 1;
+           }
+         else
+           {
+             free (nextname);
+             free (npat);
+           }
+       }
+
+      skip = 1;
+    }
+
+  if (skip == 0)
+    {
+      /* Open the directory, punting immediately if we cannot.  If opendir
+        is not robust (i.e., it opens non-directories successfully), test
+        that DIR is a directory and punt if it's not. */
+#if defined (OPENDIR_NOT_ROBUST)
+      if (glob_testdir (dir) < 0)
+       return ((char **) &glob_error_return);
+#endif
+
+      d = opendir (dir);
+      if (d == NULL)
+       return ((char **) &glob_error_return);
+
+      /* Compute the flags that will be passed to strmatch().  We don't
+        need to do this every time through the loop. */
+      mflags = (noglob_dot_filenames ? FNM_PERIOD : 0) | FNM_PATHNAME;
+
+#ifdef FNM_CASEFOLD
+      if (glob_ignore_case)
+       mflags |= FNM_CASEFOLD;
+#endif
+
+      if (extended_glob)
+       mflags |= FNM_EXTMATCH;
+
+      /* Scan the directory, finding all names that match.
+        For each name that matches, allocate a struct globval
+        on the stack and store the name in it.
+        Chain those structs together; lastlink is the front of the chain.  */
+      while (1)
+       {
+         /* Make globbing interruptible in the shell. */
+         if (interrupt_state)
+           {
+             lose = 1;
+             break;
+           }
+         
+         dp = readdir (d);
+         if (dp == NULL)
+           break;
+
+         /* If this directory entry is not to be used, try again. */
+         if (REAL_DIR_ENTRY (dp) == 0)
+           continue;
+
+#if 0
+         if (dp->d_name == 0 || *dp->d_name == 0)
+           continue;
+#endif
+
+#if HANDLE_MULTIBYTE
+         if (MB_CUR_MAX > 1 && mbskipname (pat, dp->d_name))
+           continue;
+         else
+#endif
+         if (skipname (pat, dp->d_name))
+           continue;
+
+         if (strmatch (pat, dp->d_name, mflags) != FNM_NOMATCH)
+           {
+             if (nalloca < ALLOCA_MAX)
+               {
+                 nextlink = (struct globval *) alloca (sizeof (struct globval));
+                 nalloca += sizeof (struct globval);
+               }
+             else
+               {
+                 nextlink = (struct globval *) malloc (sizeof (struct globval));
+                 if (firstmalloc == 0)
+                   firstmalloc = nextlink;
+               }
+             nextname = (char *) malloc (D_NAMLEN (dp) + 1);
+             if (nextlink == 0 || nextname == 0)
+               {
+                 lose = 1;
+                 break;
+               }
+             nextlink->next = lastlink;
+             lastlink = nextlink;
+             nextlink->name = nextname;
+             bcopy (dp->d_name, nextname, D_NAMLEN (dp) + 1);
+             ++count;
+           }
+       }
+
+      (void) closedir (d);
+    }
+
+  if (lose == 0)
+    {
+      name_vector = (char **) malloc ((count + 1) * sizeof (char *));
+      lose |= name_vector == NULL;
+    }
+
+  /* Have we run out of memory?         */
+  if (lose)
+    {
+      tmplink = 0;
+
+      /* Here free the strings we have got.  */
+      while (lastlink)
+       {
+         if (firstmalloc)
+           {
+             if (lastlink == firstmalloc)
+               firstmalloc = 0;
+             tmplink = lastlink;
+           }
+         free (lastlink->name);
+         lastlink = lastlink->next;
+         FREE (tmplink);
+       }
+
+      QUIT;
+
+      return ((char **)NULL);
+    }
+
+  /* Copy the name pointers from the linked list into the vector.  */
+  for (tmplink = lastlink, i = 0; i < count; ++i)
+    {
+      name_vector[i] = tmplink->name;
+      tmplink = tmplink->next;
+    }
+
+  name_vector[count] = NULL;
+
+  /* If we allocated some of the struct globvals, free them now. */
+  if (firstmalloc)
+    {
+      tmplink = 0;
+      while (lastlink)
+       {
+         tmplink = lastlink;
+         if (lastlink == firstmalloc)
+           lastlink = firstmalloc = 0;
+         else
+           lastlink = lastlink->next;
+         free (tmplink);
+       }
+    }
+       
+  return (name_vector);
+}
+
+/* Return a new array which is the concatenation of each string in ARRAY
+   to DIR.  This function expects you to pass in an allocated ARRAY, and
+   it takes care of free()ing that array.  Thus, you might think of this
+   function as side-effecting ARRAY.  This should handle GX_MARKDIRS. */
+static char **
+glob_dir_to_array (dir, array, flags)
+     char *dir, **array;
+     int flags;
+{
+  register unsigned int i, l;
+  int add_slash;
+  char **result, *new;
+  struct stat sb;
+
+  l = strlen (dir);
+  if (l == 0)
+    {
+      if (flags & GX_MARKDIRS)
+       for (i = 0; array[i]; i++)
+         {
+           if ((stat (array[i], &sb) == 0) && S_ISDIR (sb.st_mode))
+             {
+               l = strlen (array[i]);
+               new = (char *)realloc (array[i], l + 2);
+               if (new == 0)
+                 return NULL;
+               new[l] = '/';
+               new[l+1] = '\0';
+               array[i] = new;
+             }
+         }
+      return (array);
+    }
+
+  add_slash = dir[l - 1] != '/';
+
+  i = 0;
+  while (array[i] != NULL)
+    ++i;
+
+  result = (char **) malloc ((i + 1) * sizeof (char *));
+  if (result == NULL)
+    return (NULL);
+
+  for (i = 0; array[i] != NULL; i++)
+    {
+      /* 3 == 1 for NUL, 1 for slash at end of DIR, 1 for GX_MARKDIRS */
+      result[i] = (char *) malloc (l + strlen (array[i]) + 3);
+
+      if (result[i] == NULL)
+       return (NULL);
+
+      strcpy (result[i], dir);
+      if (add_slash)
+       result[i][l] = '/';
+      strcpy (result[i] + l + add_slash, array[i]);
+      if (flags & GX_MARKDIRS)
+       {
+         if ((stat (result[i], &sb) == 0) && S_ISDIR (sb.st_mode))
+           {
+             size_t rlen;
+             rlen = strlen (result[i]);
+             result[i][rlen] = '/';
+             result[i][rlen+1] = '\0';
+           }
+       }
+    }
+  result[i] = NULL;
+
+  /* Free the input array.  */
+  for (i = 0; array[i] != NULL; i++)
+    free (array[i]);
+  free ((char *) array);
+
+  return (result);
+}
+
+/* Do globbing on PATHNAME.  Return an array of pathnames that match,
+   marking the end of the array with a null-pointer as an element.
+   If no pathnames match, then the array is empty (first element is null).
+   If there isn't enough memory, then return NULL.
+   If a file system error occurs, return -1; `errno' has the error code.  */
+char **
+glob_filename (pathname, flags)
+     char *pathname;
+     int flags;
+{
+  char **result;
+  unsigned int result_size;
+  char *directory_name, *filename;
+  unsigned int directory_len;
+  int free_dirname;                    /* flag */
+
+  result = (char **) malloc (sizeof (char *));
+  result_size = 1;
+  if (result == NULL)
+    return (NULL);
+
+  result[0] = NULL;
+
+  directory_name = NULL;
+
+  /* Find the filename.  */
+  filename = strrchr (pathname, '/');
+  if (filename == NULL)
+    {
+      filename = pathname;
+      directory_name = "";
+      directory_len = 0;
+      free_dirname = 0;
+    }
+  else
+    {
+      directory_len = (filename - pathname) + 1;
+      directory_name = (char *) malloc (directory_len + 1);
+
+      if (directory_name == 0)         /* allocation failed? */
+       return (NULL);
+
+      bcopy (pathname, directory_name, directory_len);
+      directory_name[directory_len] = '\0';
+      ++filename;
+      free_dirname = 1;
+    }
+
+  /* If directory_name contains globbing characters, then we
+     have to expand the previous levels.  Just recurse. */
+  if (glob_pattern_p (directory_name))
+    {
+      char **directories;
+      register unsigned int i;
+
+      if (directory_name[directory_len - 1] == '/')
+       directory_name[directory_len - 1] = '\0';
+
+      directories = glob_filename (directory_name, flags & ~GX_MARKDIRS);
+
+      if (free_dirname)
+       {
+         free (directory_name);
+         directory_name = NULL;
+       }
+
+      if (directories == NULL)
+       goto memory_error;
+      else if (directories == (char **)&glob_error_return)
+       {
+         free ((char *) result);
+         return ((char **) &glob_error_return);
+       }
+      else if (*directories == NULL)
+       {
+         free ((char *) directories);
+         free ((char *) result);
+         return ((char **) &glob_error_return);
+       }
+
+      /* We have successfully globbed the preceding directory name.
+        For each name in DIRECTORIES, call glob_vector on it and
+        FILENAME.  Concatenate the results together.  */
+      for (i = 0; directories[i] != NULL; ++i)
+       {
+         char **temp_results;
+
+         /* Scan directory even on a NULL pathname.  That way, `*h/'
+            returns only directories ending in `h', instead of all
+            files ending in `h' with a `/' appended. */
+         temp_results = glob_vector (filename, directories[i], flags & ~GX_MARKDIRS);
+
+         /* Handle error cases. */
+         if (temp_results == NULL)
+           goto memory_error;
+         else if (temp_results == (char **)&glob_error_return)
+           /* This filename is probably not a directory.  Ignore it.  */
+           ;
+         else
+           {
+             char **array;
+             register unsigned int l;
+
+             array = glob_dir_to_array (directories[i], temp_results, flags);
+             l = 0;
+             while (array[l] != NULL)
+               ++l;
+
+             result =
+               (char **)realloc (result, (result_size + l) * sizeof (char *));
+
+             if (result == NULL)
+               goto memory_error;
+
+             for (l = 0; array[l] != NULL; ++l)
+               result[result_size++ - 1] = array[l];
+
+             result[result_size - 1] = NULL;
+
+             /* Note that the elements of ARRAY are not freed.  */
+             free ((char *) array);
+           }
+       }
+      /* Free the directories.  */
+      for (i = 0; directories[i]; i++)
+       free (directories[i]);
+
+      free ((char *) directories);
+
+      return (result);
+    }
+
+  /* If there is only a directory name, return it. */
+  if (*filename == '\0')
+    {
+      result = (char **) realloc ((char *) result, 2 * sizeof (char *));
+      if (result == NULL)
+       return (NULL);
+      /* Handle GX_MARKDIRS here. */
+      result[0] = (char *) malloc (directory_len + 1);
+      if (result[0] == NULL)
+       goto memory_error;
+      bcopy (directory_name, result[0], directory_len + 1);
+      if (free_dirname)
+       free (directory_name);
+      result[1] = NULL;
+      return (result);
+    }
+  else
+    {
+      char **temp_results;
+
+      /* There are no unquoted globbing characters in DIRECTORY_NAME.
+        Dequote it before we try to open the directory since there may
+        be quoted globbing characters which should be treated verbatim. */
+      if (directory_len > 0)
+       dequote_pathname (directory_name);
+
+      /* We allocated a small array called RESULT, which we won't be using.
+        Free that memory now. */
+      free (result);
+
+      /* Just return what glob_vector () returns appended to the
+        directory name. */
+      temp_results = glob_vector (filename,
+                                 (directory_len == 0 ? "." : directory_name),
+                                 flags & ~GX_MARKDIRS);
+
+      if (temp_results == NULL || temp_results == (char **)&glob_error_return)
+       {
+         if (free_dirname)
+           free (directory_name);
+         return (temp_results);
+       }
+
+      result = glob_dir_to_array (directory_name, temp_results, flags);
+      if (free_dirname)
+       free (directory_name);
+      return (result);
+    }
+
+  /* We get to memory_error if the program has run out of memory, or
+     if this is the shell, and we have been interrupted. */
+ memory_error:
+  if (result != NULL)
+    {
+      register unsigned int i;
+      for (i = 0; result[i] != NULL; ++i)
+       free (result[i]);
+      free ((char *) result);
+    }
+
+  if (free_dirname && directory_name)
+    free (directory_name);
+
+  QUIT;
+
+  return (NULL);
+}
+
+#if defined (TEST)
+
+main (argc, argv)
+     int argc;
+     char **argv;
+{
+  unsigned int i;
+
+  for (i = 1; i < argc; ++i)
+    {
+      char **value = glob_filename (argv[i], 0);
+      if (value == NULL)
+       puts ("Out of memory.");
+      else if (value == &glob_error_return)
+       perror (argv[i]);
+      else
+       for (i = 0; value[i] != NULL; i++)
+         puts (value[i]);
+    }
+
+  exit (0);
+}
+#endif /* TEST.  */
index ff0cf5a..e0df965 100644 (file)
@@ -1414,7 +1414,7 @@ update_line (old, new, current_line, omax, nmax, inv_botlin)
              insert_some_chars (nfd, lendiff, col_lendiff);
              _rl_last_c_pos += col_lendiff;
            }
-         else if (*ols == 0 && lendiff > 0)
+         else if ((MB_CUR_MAX == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0)
            {
              /* At the end of a line the characters do not have to
                 be "inserted".  They can just be placed on the screen. */
diff --git a/lib/readline/display.c~ b/lib/readline/display.c~
new file mode 100644 (file)
index 0000000..ff0cf5a
--- /dev/null
@@ -0,0 +1,2309 @@
+/* display.c -- readline redisplay facility. */
+
+/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
+
+   This file is part of the GNU Readline Library, a library for
+   reading lines of text with interactive input and history editing.
+
+   The GNU Readline Library 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 2, or
+   (at your option) any later version.
+
+   The GNU Readline Library 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.
+
+   The GNU General Public License is often shipped with GNU software, and
+   is generally kept in a file called COPYING or LICENSE.  If you do not
+   have a copy of the license, write to the Free Software Foundation,
+   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+#define READLINE_LIBRARY
+
+#if defined (HAVE_CONFIG_H)
+#  include <config.h>
+#endif
+
+#include <sys/types.h>
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif /* HAVE_UNISTD_H */
+
+#include "posixstat.h"
+
+#if defined (HAVE_STDLIB_H)
+#  include <stdlib.h>
+#else
+#  include "ansi_stdlib.h"
+#endif /* HAVE_STDLIB_H */
+
+#include <stdio.h>
+
+/* System-specific feature definitions and include files. */
+#include "rldefs.h"
+#include "rlmbutil.h"
+
+/* Termcap library stuff. */
+#include "tcap.h"
+
+/* Some standard library routines. */
+#include "readline.h"
+#include "history.h"
+
+#include "rlprivate.h"
+#include "xmalloc.h"
+
+#if !defined (strchr) && !defined (__STDC__)
+extern char *strchr (), *strrchr ();
+#endif /* !strchr && !__STDC__ */
+
+#if defined (HACK_TERMCAP_MOTION)
+extern char *_rl_term_forward_char;
+#endif
+
+static void update_line PARAMS((char *, char *, int, int, int, int));
+static void space_to_eol PARAMS((int));
+static void delete_chars PARAMS((int));
+static void insert_some_chars PARAMS((char *, int, int));
+static void cr PARAMS((void));
+
+#if defined (HANDLE_MULTIBYTE)
+static int _rl_col_width PARAMS((const char *, int, int));
+static int *_rl_wrapped_line;
+#else
+#  define _rl_col_width(l, s, e)       (((e) <= (s)) ? 0 : (e) - (s))
+#endif
+
+static int *inv_lbreaks, *vis_lbreaks;
+static int inv_lbsize, vis_lbsize;
+
+/* Heuristic used to decide whether it is faster to move from CUR to NEW
+   by backing up or outputting a carriage return and moving forward. */
+#define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Display stuff                               */
+/*                                                                 */
+/* **************************************************************** */
+
+/* This is the stuff that is hard for me.  I never seem to write good
+   display routines in C.  Let's see how I do this time. */
+
+/* (PWP) Well... Good for a simple line updater, but totally ignores
+   the problems of input lines longer than the screen width.
+
+   update_line and the code that calls it makes a multiple line,
+   automatically wrapping line update.  Careful attention needs
+   to be paid to the vertical position variables. */
+
+/* Keep two buffers; one which reflects the current contents of the
+   screen, and the other to draw what we think the new contents should
+   be.  Then compare the buffers, and make whatever changes to the
+   screen itself that we should.  Finally, make the buffer that we
+   just drew into be the one which reflects the current contents of the
+   screen, and place the cursor where it belongs.
+
+   Commands that want to can fix the display themselves, and then let
+   this function know that the display has been fixed by setting the
+   RL_DISPLAY_FIXED variable.  This is good for efficiency. */
+
+/* Application-specific redisplay function. */
+rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
+
+/* Global variables declared here. */
+/* What YOU turn on when you have handled all redisplay yourself. */
+int rl_display_fixed = 0;
+
+int _rl_suppress_redisplay = 0;
+
+/* The stuff that gets printed out before the actual text of the line.
+   This is usually pointing to rl_prompt. */
+char *rl_display_prompt = (char *)NULL;
+
+/* Pseudo-global variables declared here. */
+/* The visible cursor position.  If you print some text, adjust this. */
+int _rl_last_c_pos = 0;
+int _rl_last_v_pos = 0;
+
+/* Number of lines currently on screen minus 1. */
+int _rl_vis_botlin = 0;
+
+/* Variables used only in this file. */
+/* The last left edge of text that was displayed.  This is used when
+   doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
+static int last_lmargin;
+
+/* The line display buffers.  One is the line currently displayed on
+   the screen.  The other is the line about to be displayed. */
+static char *visible_line = (char *)NULL;
+static char *invisible_line = (char *)NULL;
+
+/* A buffer for `modeline' messages. */
+static char msg_buf[128];
+
+/* Non-zero forces the redisplay even if we thought it was unnecessary. */
+static int forced_display;
+
+/* Default and initial buffer size.  Can grow. */
+static int line_size = 1024;
+
+/* Variables to keep track of the expanded prompt string, which may
+   include invisible characters. */
+
+static char *local_prompt, *local_prompt_prefix;
+static int prompt_visible_length, prompt_prefix_length;
+
+/* The number of invisible characters in the line currently being
+   displayed on the screen. */
+static int visible_wrap_offset;
+
+/* The number of invisible characters in the prompt string.  Static so it
+   can be shared between rl_redisplay and update_line */
+static int wrap_offset;
+
+/* The index of the last invisible character in the prompt string. */
+static int prompt_last_invisible;
+
+/* The length (buffer offset) of the first line of the last (possibly
+   multi-line) buffer displayed on the screen. */
+static int visible_first_line_len;
+
+/* Number of invisible characters on the first physical line of the prompt.
+   Only valid when the number of physical characters in the prompt exceeds
+   (or is equal to) _rl_screenwidth. */
+static int prompt_invis_chars_first_line;
+
+static int prompt_last_screen_line;
+
+static int prompt_physical_chars;
+
+/* Expand the prompt string S and return the number of visible
+   characters in *LP, if LP is not null.  This is currently more-or-less
+   a placeholder for expansion.  LIP, if non-null is a place to store the
+   index of the last invisible character in the returned string. NIFLP,
+   if non-zero, is a place to store the number of invisible characters in
+   the first prompt line.  The previous are used as byte counts -- indexes
+   into a character buffer. */
+
+/* Current implementation:
+       \001 (^A) start non-visible characters
+       \002 (^B) end non-visible characters
+   all characters except \001 and \002 (following a \001) are copied to
+   the returned string; all characters except those between \001 and
+   \002 are assumed to be `visible'. */        
+
+static char *
+expand_prompt (pmt, lp, lip, niflp, vlp)
+     char *pmt;
+     int *lp, *lip, *niflp, *vlp;
+{
+  char *r, *ret, *p;
+  int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
+
+  /* Short-circuit if we can. */
+  if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
+    {
+      r = savestring (pmt);
+      if (lp)
+       *lp = strlen (r);
+      if (lip)
+       *lip = 0;
+      if (niflp)
+       *niflp = 0;
+      if (vlp)
+       *vlp = lp ? *lp : strlen (r);
+      return r;
+    }
+
+  l = strlen (pmt);
+  r = ret = (char *)xmalloc (l + 1);
+
+  invfl = 0;   /* invisible chars in first line of prompt */
+  invflset = 0;        /* we only want to set invfl once */
+
+  for (rl = ignoring = last = ninvis = physchars = 0, p = pmt; p && *p; p++)
+    {
+      /* This code strips the invisible character string markers
+        RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
+      if (*p == RL_PROMPT_START_IGNORE)
+       {
+         ignoring++;
+         continue;
+       }
+      else if (ignoring && *p == RL_PROMPT_END_IGNORE)
+       {
+         ignoring = 0;
+         last = r - ret - 1;
+         continue;
+       }
+      else
+       {
+#if defined (HANDLE_MULTIBYTE)
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           {
+             pind = p - pmt;
+             ind = _rl_find_next_mbchar (pmt, pind, 1, MB_FIND_NONZERO);
+             l = ind - pind;
+             while (l--)
+               *r++ = *p++;
+             if (!ignoring)
+               {
+                 rl += ind - pind;
+                 physchars += _rl_col_width (pmt, pind, ind);
+               }
+             else
+               ninvis += ind - pind;
+             p--;                      /* compensate for later increment */
+           }
+         else
+#endif
+           {
+             *r++ = *p;
+             if (!ignoring)
+               {
+                 rl++;                 /* visible length byte counter */
+                 physchars++;
+               }
+             else
+               ninvis++;               /* invisible chars byte counter */
+           }
+
+         if (invflset == 0 && rl >= _rl_screenwidth)
+           {
+             invfl = ninvis;
+             invflset = 1;
+           }
+       }
+    }
+
+  if (rl < _rl_screenwidth)
+    invfl = ninvis;
+
+  *r = '\0';
+  if (lp)
+    *lp = rl;
+  if (lip)
+    *lip = last;
+  if (niflp)
+    *niflp = invfl;
+  if  (vlp)
+    *vlp = physchars;
+  return ret;
+}
+
+/* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
+   PMT and return the rest of PMT. */
+char *
+_rl_strip_prompt (pmt)
+     char *pmt;
+{
+  char *ret;
+
+  ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
+  return ret;
+}
+
+/*
+ * Expand the prompt string into the various display components, if
+ * necessary.
+ *
+ * local_prompt = expanded last line of string in rl_display_prompt
+ *               (portion after the final newline)
+ * local_prompt_prefix = portion before last newline of rl_display_prompt,
+ *                      expanded via expand_prompt
+ * prompt_visible_length = number of visible characters in local_prompt
+ * prompt_prefix_length = number of visible characters in local_prompt_prefix
+ *
+ * This function is called once per call to readline().  It may also be
+ * called arbitrarily to expand the primary prompt.
+ *
+ * The return value is the number of visible characters on the last line
+ * of the (possibly multi-line) prompt.
+ */
+int
+rl_expand_prompt (prompt)
+     char *prompt;
+{
+  char *p, *t;
+  int c;
+
+  /* Clear out any saved values. */
+  FREE (local_prompt);
+  FREE (local_prompt_prefix);
+
+  local_prompt = local_prompt_prefix = (char *)0;
+  prompt_last_invisible = prompt_visible_length = 0;
+
+  if (prompt == 0 || *prompt == 0)
+    return (0);
+
+  p = strrchr (prompt, '\n');
+  if (!p)
+    {
+      /* The prompt is only one logical line, though it might wrap. */
+      local_prompt = expand_prompt (prompt, &prompt_visible_length,
+                                           &prompt_last_invisible,
+                                           &prompt_invis_chars_first_line,
+                                           &prompt_physical_chars);
+      local_prompt_prefix = (char *)0;
+      return (prompt_visible_length);
+    }
+  else
+    {
+      /* The prompt spans multiple lines. */
+      t = ++p;
+      local_prompt = expand_prompt (p, &prompt_visible_length,
+                                      &prompt_last_invisible,
+                                      (int *)NULL,
+                                      &prompt_physical_chars);
+      c = *t; *t = '\0';
+      /* The portion of the prompt string up to and including the
+        final newline is now null-terminated. */
+      local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
+                                                  (int *)NULL,
+                                                  &prompt_invis_chars_first_line,
+                                                  (int *)NULL);
+      *t = c;
+      return (prompt_prefix_length);
+    }
+}
+
+/* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
+   arrays of line break markers.  MINSIZE is the minimum size of VISIBLE_LINE
+   and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
+   increased.  If the lines have already been allocated, this ensures that
+   they can hold at least MINSIZE characters. */
+static void
+init_line_structures (minsize)
+      int minsize;
+{
+  register int n;
+
+  if (invisible_line == 0)     /* initialize it */
+    {
+      if (line_size < minsize)
+       line_size = minsize;
+      visible_line = (char *)xmalloc (line_size);
+      invisible_line = (char *)xmalloc (line_size);
+    }
+  else if (line_size < minsize)        /* ensure it can hold MINSIZE chars */
+    {
+      line_size *= 2;
+      if (line_size < minsize)
+       line_size = minsize;
+      visible_line = (char *)xrealloc (visible_line, line_size);
+      invisible_line = (char *)xrealloc (invisible_line, line_size);
+    }
+
+  for (n = minsize; n < line_size; n++)
+    {
+      visible_line[n] = 0;
+      invisible_line[n] = 1;
+    }
+
+  if (vis_lbreaks == 0)
+    {
+      /* should be enough. */
+      inv_lbsize = vis_lbsize = 256;
+      inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
+      vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
+#if defined (HANDLE_MULTIBYTE)
+      _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
+#endif
+      inv_lbreaks[0] = vis_lbreaks[0] = 0;
+    }
+}
+  
+/* Basic redisplay algorithm. */
+void
+rl_redisplay ()
+{
+  register int in, out, c, linenum, cursor_linenum;
+  register char *line;
+  int c_pos, inv_botlin, lb_botlin, lb_linenum;
+  int newlines, lpos, temp, modmark, n0, num;
+  char *prompt_this_line;
+#if defined (HANDLE_MULTIBYTE)
+  wchar_t wc;
+  size_t wc_bytes;
+  int wc_width;
+  mbstate_t ps;
+  int _rl_wrapped_multicolumn = 0;
+#endif
+
+  if (!readline_echoing_p)
+    return;
+
+  if (!rl_display_prompt)
+    rl_display_prompt = "";
+
+  if (invisible_line == 0)
+    {
+      init_line_structures (0);
+      rl_on_new_line ();
+    }
+
+  /* Draw the line into the buffer. */
+  c_pos = -1;
+
+  line = invisible_line;
+  out = inv_botlin = 0;
+
+  /* Mark the line as modified or not.  We only do this for history
+     lines. */
+  modmark = 0;
+  if (_rl_mark_modified_lines && current_history () && rl_undo_list)
+    {
+      line[out++] = '*';
+      line[out] = '\0';
+      modmark = 1;
+    }
+
+  /* If someone thought that the redisplay was handled, but the currently
+     visible line has a different modification state than the one about
+     to become visible, then correct the caller's misconception. */
+  if (visible_line[0] != invisible_line[0])
+    rl_display_fixed = 0;
+
+  /* If the prompt to be displayed is the `primary' readline prompt (the
+     one passed to readline()), use the values we have already expanded.
+     If not, use what's already in rl_display_prompt.  WRAP_OFFSET is the
+     number of non-visible characters in the prompt string. */
+  if (rl_display_prompt == rl_prompt || local_prompt)
+    {
+      int local_len = local_prompt ? strlen (local_prompt) : 0;
+      if (local_prompt_prefix && forced_display)
+       _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
+
+      if (local_len > 0)
+       {
+         temp = local_len + out + 2;
+         if (temp >= line_size)
+           {
+             line_size = (temp + 1024) - (temp % 1024);
+             visible_line = (char *)xrealloc (visible_line, line_size);
+             line = invisible_line = (char *)xrealloc (invisible_line, line_size);
+           }
+         strncpy (line + out, local_prompt, local_len);
+         out += local_len;
+       }
+      line[out] = '\0';
+      wrap_offset = local_len - prompt_visible_length;
+    }
+  else
+    {
+      int pmtlen;
+      prompt_this_line = strrchr (rl_display_prompt, '\n');
+      if (!prompt_this_line)
+       prompt_this_line = rl_display_prompt;
+      else
+       {
+         prompt_this_line++;
+         pmtlen = prompt_this_line - rl_display_prompt;        /* temp var */
+         if (forced_display)
+           {
+             _rl_output_some_chars (rl_display_prompt, pmtlen);
+             /* Make sure we are at column zero even after a newline,
+                regardless of the state of terminal output processing. */
+             if (pmtlen < 2 || prompt_this_line[-2] != '\r')
+               cr ();
+           }
+       }
+
+      prompt_physical_chars = pmtlen = strlen (prompt_this_line);
+      temp = pmtlen + out + 2;
+      if (temp >= line_size)
+       {
+         line_size = (temp + 1024) - (temp % 1024);
+         visible_line = (char *)xrealloc (visible_line, line_size);
+         line = invisible_line = (char *)xrealloc (invisible_line, line_size);
+       }
+      strncpy (line + out,  prompt_this_line, pmtlen);
+      out += pmtlen;
+      line[out] = '\0';
+      wrap_offset = prompt_invis_chars_first_line = 0;
+    }
+
+#define CHECK_INV_LBREAKS() \
+      do { \
+       if (newlines >= (inv_lbsize - 2)) \
+         { \
+           inv_lbsize *= 2; \
+           inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
+         } \
+      } while (0)
+
+#if defined (HANDLE_MULTIBYTE)   
+#define CHECK_LPOS() \
+      do { \
+       lpos++; \
+       if (lpos >= _rl_screenwidth) \
+         { \
+           if (newlines >= (inv_lbsize - 2)) \
+             { \
+               inv_lbsize *= 2; \
+               inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
+               _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
+             } \
+           inv_lbreaks[++newlines] = out; \
+           _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
+           lpos = 0; \
+         } \
+      } while (0)
+#else
+#define CHECK_LPOS() \
+      do { \
+       lpos++; \
+       if (lpos >= _rl_screenwidth) \
+         { \
+           if (newlines >= (inv_lbsize - 2)) \
+             { \
+               inv_lbsize *= 2; \
+               inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
+             } \
+           inv_lbreaks[++newlines] = out; \
+           lpos = 0; \
+         } \
+      } while (0)
+#endif
+
+  /* inv_lbreaks[i] is where line i starts in the buffer. */
+  inv_lbreaks[newlines = 0] = 0;
+#if 0
+  lpos = out - wrap_offset;
+#else
+  lpos = prompt_physical_chars + modmark;
+#endif
+
+#if defined (HANDLE_MULTIBYTE)
+  memset (_rl_wrapped_line, 0, vis_lbsize);
+  num = 0;
+#endif
+
+  /* prompt_invis_chars_first_line is the number of invisible characters in
+     the first physical line of the prompt.
+     wrap_offset - prompt_invis_chars_first_line is the number of invis
+     chars on the second line. */
+
+  /* what if lpos is already >= _rl_screenwidth before we start drawing the
+     contents of the command line? */
+  while (lpos >= _rl_screenwidth)
+    {
+      /* fix from Darin Johnson <darin@acuson.com> for prompt string with
+         invisible characters that is longer than the screen width.  The
+         prompt_invis_chars_first_line variable could be made into an array
+         saying how many invisible characters there are per line, but that's
+         probably too much work for the benefit gained.  How many people have
+         prompts that exceed two physical lines?
+         Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
+#if defined (HANDLE_MULTIBYTE)
+      n0 = num;
+      temp = local_prompt ? strlen (local_prompt) : 0;
+      while (num < temp)
+       {
+         if (_rl_col_width  (local_prompt, n0, num) > _rl_screenwidth)
+           {
+             num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
+             break;
+           }
+         num++;
+       }
+      temp = num +
+#else
+      temp = ((newlines + 1) * _rl_screenwidth) +
+#endif /* !HANDLE_MULTIBYTE */
+             ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line
+                                                           : ((newlines == 1) ? wrap_offset : 0))
+                                        : ((newlines == 0) ? wrap_offset :0));
+             
+      inv_lbreaks[++newlines] = temp;
+#if defined (HANDLE_MULTIBYTE)
+      lpos -= _rl_col_width (local_prompt, n0, num);
+#else
+      lpos -= _rl_screenwidth;
+#endif
+    }
+
+  prompt_last_screen_line = newlines;
+
+  /* Draw the rest of the line (after the prompt) into invisible_line, keeping
+     track of where the cursor is (c_pos), the number of the line containing
+     the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
+     It maintains an array of line breaks for display (inv_lbreaks).
+     This handles expanding tabs for display and displaying meta characters. */
+  lb_linenum = 0;
+#if defined (HANDLE_MULTIBYTE)
+  in = 0;
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    {
+      memset (&ps, 0, sizeof (mbstate_t));
+      wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
+    }
+  else
+    wc_bytes = 1;
+  while (in < rl_end)
+#else
+  for (in = 0; in < rl_end; in++)
+#endif
+    {
+      c = (unsigned char)rl_line_buffer[in];
+
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         if (MB_INVALIDCH (wc_bytes))
+           {
+             /* Byte sequence is invalid or shortened.  Assume that the
+                first byte represents a character. */
+             wc_bytes = 1;
+             /* Assume that a character occupies a single column. */
+             wc_width = 1;
+             memset (&ps, 0, sizeof (mbstate_t));
+           }
+         else if (MB_NULLWCH (wc_bytes))
+           break;                      /* Found '\0' */
+         else
+           {
+             temp = wcwidth (wc);
+             wc_width = (temp >= 0) ? temp : 1;
+           }
+       }
+#endif
+
+      if (out + 8 >= line_size)                /* XXX - 8 for \t */
+       {
+         line_size *= 2;
+         visible_line = (char *)xrealloc (visible_line, line_size);
+         invisible_line = (char *)xrealloc (invisible_line, line_size);
+         line = invisible_line;
+       }
+
+      if (in == rl_point)
+       {
+         c_pos = out;
+         lb_linenum = newlines;
+       }
+
+#if defined (HANDLE_MULTIBYTE)
+      if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
+#else
+      if (META_CHAR (c))
+#endif
+       {
+         if (_rl_output_meta_chars == 0)
+           {
+             sprintf (line + out, "\\%o", c);
+
+             if (lpos + 4 >= _rl_screenwidth)
+               {
+                 temp = _rl_screenwidth - lpos;
+                 CHECK_INV_LBREAKS ();
+                 inv_lbreaks[++newlines] = out + temp;
+                 lpos = 4 - temp;
+               }
+             else
+               lpos += 4;
+
+             out += 4;
+           }
+         else
+           {
+             line[out++] = c;
+             CHECK_LPOS();
+           }
+       }
+#if defined (DISPLAY_TABS)
+      else if (c == '\t')
+       {
+         register int newout;
+
+#if 0
+         newout = (out | (int)7) + 1;
+#else
+         newout = out + 8 - lpos % 8;
+#endif
+         temp = newout - out;
+         if (lpos + temp >= _rl_screenwidth)
+           {
+             register int temp2;
+             temp2 = _rl_screenwidth - lpos;
+             CHECK_INV_LBREAKS ();
+             inv_lbreaks[++newlines] = out + temp2;
+             lpos = temp - temp2;
+             while (out < newout)
+               line[out++] = ' ';
+           }
+         else
+           {
+             while (out < newout)
+               line[out++] = ' ';
+             lpos += temp;
+           }
+       }
+#endif
+      else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
+       {
+         line[out++] = '\0';   /* XXX - sentinel */
+         CHECK_INV_LBREAKS ();
+         inv_lbreaks[++newlines] = out;
+         lpos = 0;
+       }
+      else if (CTRL_CHAR (c) || c == RUBOUT)
+       {
+         line[out++] = '^';
+         CHECK_LPOS();
+         line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
+         CHECK_LPOS();
+       }
+      else
+       {
+#if defined (HANDLE_MULTIBYTE)
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           {
+             register int i;
+
+             _rl_wrapped_multicolumn = 0;
+
+             if (_rl_screenwidth < lpos + wc_width)
+               for (i = lpos; i < _rl_screenwidth; i++)
+                 {
+                   /* The space will be removed in update_line() */
+                   line[out++] = ' ';
+                   _rl_wrapped_multicolumn++;
+                   CHECK_LPOS();
+                 }
+             if (in == rl_point)
+               {
+                 c_pos = out;
+                 lb_linenum = newlines;
+               }
+             for (i = in; i < in+wc_bytes; i++)
+               line[out++] = rl_line_buffer[i];
+             for (i = 0; i < wc_width; i++)
+               CHECK_LPOS();
+           }
+         else
+           {
+             line[out++] = c;
+             CHECK_LPOS();
+           }
+#else
+         line[out++] = c;
+         CHECK_LPOS();
+#endif
+       }
+
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         in += wc_bytes;
+         wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
+       }
+      else
+        in++;
+#endif
+
+    }
+  line[out] = '\0';
+  if (c_pos < 0)
+    {
+      c_pos = out;
+      lb_linenum = newlines;
+    }
+
+  inv_botlin = lb_botlin = newlines;
+  CHECK_INV_LBREAKS ();
+  inv_lbreaks[newlines+1] = out;
+  cursor_linenum = lb_linenum;
+
+  /* C_POS == position in buffer where cursor should be placed.
+     CURSOR_LINENUM == line number where the cursor should be placed. */
+
+  /* PWP: now is when things get a bit hairy.  The visible and invisible
+     line buffers are really multiple lines, which would wrap every
+     (screenwidth - 1) characters.  Go through each in turn, finding
+     the changed region and updating it.  The line order is top to bottom. */
+
+  /* If we can move the cursor up and down, then use multiple lines,
+     otherwise, let long lines display in a single terminal line, and
+     horizontally scroll it. */
+
+  if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
+    {
+      int nleft, pos, changed_screen_line;
+
+      if (!rl_display_fixed || forced_display)
+       {
+         forced_display = 0;
+
+         /* If we have more than a screenful of material to display, then
+            only display a screenful.  We should display the last screen,
+            not the first.  */
+         if (out >= _rl_screenchars)
+           {
+             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+               out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
+             else
+               out = _rl_screenchars - 1;
+           }
+
+         /* The first line is at character position 0 in the buffer.  The
+            second and subsequent lines start at inv_lbreaks[N], offset by
+            OFFSET (which has already been calculated above).  */
+
+#define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
+#define VIS_LLEN(l)    ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
+#define INV_LLEN(l)    (inv_lbreaks[l+1] - inv_lbreaks[l])
+#define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
+#define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
+#define INV_LINE(line) (invisible_line + inv_lbreaks[line])
+
+         /* For each line in the buffer, do the updating display. */
+         for (linenum = 0; linenum <= inv_botlin; linenum++)
+           {
+             update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
+                          VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
+
+             /* If this is the line with the prompt, we might need to
+                compensate for invisible characters in the new line. Do
+                this only if there is not more than one new line (which
+                implies that we completely overwrite the old visible line)
+                and the new line is shorter than the old.  Make sure we are
+                at the end of the new line before clearing. */
+             if (linenum == 0 &&
+                 inv_botlin == 0 && _rl_last_c_pos == out &&
+                 (wrap_offset > visible_wrap_offset) &&
+                 (_rl_last_c_pos < visible_first_line_len))
+               {
+                 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
+                 if (nleft)
+                   _rl_clear_to_eol (nleft);
+               }
+
+             /* Since the new first line is now visible, save its length. */
+             if (linenum == 0)
+               visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
+           }
+
+         /* We may have deleted some lines.  If so, clear the left over
+            blank ones at the bottom out. */
+         if (_rl_vis_botlin > inv_botlin)
+           {
+             char *tt;
+             for (; linenum <= _rl_vis_botlin; linenum++)
+               {
+                 tt = VIS_CHARS (linenum);
+                 _rl_move_vert (linenum);
+                 _rl_move_cursor_relative (0, tt);
+                 _rl_clear_to_eol
+                   ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
+               }
+           }
+         _rl_vis_botlin = inv_botlin;
+
+         /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
+            different screen line during this redisplay. */
+         changed_screen_line = _rl_last_v_pos != cursor_linenum;
+         if (changed_screen_line)
+           {
+             _rl_move_vert (cursor_linenum);
+             /* If we moved up to the line with the prompt using _rl_term_up,
+                the physical cursor position on the screen stays the same,
+                but the buffer position needs to be adjusted to account
+                for invisible characters. */
+             if (cursor_linenum == 0 && wrap_offset)
+               _rl_last_c_pos += wrap_offset;
+           }
+
+         /* We have to reprint the prompt if it contains invisible
+            characters, since it's not generally OK to just reprint
+            the characters from the current cursor position.  But we
+            only need to reprint it if the cursor is before the last
+            invisible character in the prompt string. */
+         nleft = prompt_visible_length + wrap_offset;
+         if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
+             _rl_last_c_pos <= prompt_last_invisible && local_prompt)
+           {
+#if defined (__MSDOS__)
+             putc ('\r', rl_outstream);
+#else
+             if (_rl_term_cr)
+               tputs (_rl_term_cr, 1, _rl_output_character_function);
+#endif
+             _rl_output_some_chars (local_prompt, nleft);
+             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+               _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft);
+             else
+               _rl_last_c_pos = nleft;
+           }
+
+         /* Where on that line?  And where does that line start
+            in the buffer? */
+         pos = inv_lbreaks[cursor_linenum];
+         /* nleft == number of characters in the line buffer between the
+            start of the line and the cursor position. */
+         nleft = c_pos - pos;
+
+         /* Since _rl_backspace() doesn't know about invisible characters in the
+            prompt, and there's no good way to tell it, we compensate for
+            those characters here and call _rl_backspace() directly. */
+         if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
+           {
+             _rl_backspace (_rl_last_c_pos - nleft);
+             if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+               _rl_last_c_pos = _rl_col_width (&visible_line[pos], 0, nleft);
+             else
+               _rl_last_c_pos = nleft;
+           }
+
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           _rl_move_cursor_relative (nleft, &invisible_line[pos]);
+         else if (nleft != _rl_last_c_pos)
+           _rl_move_cursor_relative (nleft, &invisible_line[pos]);
+       }
+    }
+  else                         /* Do horizontal scrolling. */
+    {
+#define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
+      int lmargin, ndisp, nleft, phys_c_pos, t;
+
+      /* Always at top line. */
+      _rl_last_v_pos = 0;
+
+      /* Compute where in the buffer the displayed line should start.  This
+        will be LMARGIN. */
+
+      /* The number of characters that will be displayed before the cursor. */
+      ndisp = c_pos - wrap_offset;
+      nleft  = prompt_visible_length + wrap_offset;
+      /* Where the new cursor position will be on the screen.  This can be
+        longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
+      phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
+      t = _rl_screenwidth / 3;
+
+      /* If the number of characters had already exceeded the screenwidth,
+        last_lmargin will be > 0. */
+
+      /* If the number of characters to be displayed is more than the screen
+        width, compute the starting offset so that the cursor is about
+        two-thirds of the way across the screen. */
+      if (phys_c_pos > _rl_screenwidth - 2)
+       {
+         lmargin = c_pos - (2 * t);
+         if (lmargin < 0)
+           lmargin = 0;
+         /* If the left margin would be in the middle of a prompt with
+            invisible characters, don't display the prompt at all. */
+         if (wrap_offset && lmargin > 0 && lmargin < nleft)
+           lmargin = nleft;
+       }
+      else if (ndisp < _rl_screenwidth - 2)            /* XXX - was -1 */
+       lmargin = 0;
+      else if (phys_c_pos < 1)
+       {
+         /* If we are moving back towards the beginning of the line and
+            the last margin is no longer correct, compute a new one. */
+         lmargin = ((c_pos - 1) / t) * t;      /* XXX */
+         if (wrap_offset && lmargin > 0 && lmargin < nleft)
+           lmargin = nleft;
+       }
+      else
+       lmargin = last_lmargin;
+
+      /* If the first character on the screen isn't the first character
+        in the display line, indicate this with a special character. */
+      if (lmargin > 0)
+       line[lmargin] = '<';
+
+      /* If SCREENWIDTH characters starting at LMARGIN do not encompass
+        the whole line, indicate that with a special character at the
+        right edge of the screen.  If LMARGIN is 0, we need to take the
+        wrap offset into account. */
+      t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
+      if (t < out)
+       line[t - 1] = '>';
+
+      if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
+       {
+         forced_display = 0;
+         update_line (&visible_line[last_lmargin],
+                      &invisible_line[lmargin],
+                      0,
+                      _rl_screenwidth + visible_wrap_offset,
+                      _rl_screenwidth + (lmargin ? 0 : wrap_offset),
+                      0);
+
+         /* If the visible new line is shorter than the old, but the number
+            of invisible characters is greater, and we are at the end of
+            the new line, we need to clear to eol. */
+         t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
+         if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
+             (_rl_last_c_pos == out) &&
+             t < visible_first_line_len)
+           {
+             nleft = _rl_screenwidth - t;
+             _rl_clear_to_eol (nleft);
+           }
+         visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
+         if (visible_first_line_len > _rl_screenwidth)
+           visible_first_line_len = _rl_screenwidth;
+
+         _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
+         last_lmargin = lmargin;
+       }
+    }
+  fflush (rl_outstream);
+
+  /* Swap visible and non-visible lines. */
+  {
+    char *vtemp = visible_line;
+    int *itemp = vis_lbreaks, ntemp = vis_lbsize;
+
+    visible_line = invisible_line;
+    invisible_line = vtemp;
+
+    vis_lbreaks = inv_lbreaks;
+    inv_lbreaks = itemp;
+
+    vis_lbsize = inv_lbsize;
+    inv_lbsize = ntemp;
+
+    rl_display_fixed = 0;
+    /* If we are displaying on a single line, and last_lmargin is > 0, we
+       are not displaying any invisible characters, so set visible_wrap_offset
+       to 0. */
+    if (_rl_horizontal_scroll_mode && last_lmargin)
+      visible_wrap_offset = 0;
+    else
+      visible_wrap_offset = wrap_offset;
+  }
+}
+
+/* PWP: update_line() is based on finding the middle difference of each
+   line on the screen; vis:
+
+                            /old first difference
+       /beginning of line   |        /old last same       /old EOL
+       v                    v        v             v
+old:   eddie> Oh, my little gruntle-buggy is to me, as lurgid as
+new:   eddie> Oh, my little buggy says to me, as lurgid as
+       ^                    ^  ^                          ^
+       \beginning of line   |  \new last same     \new end of line
+                            \new first difference
+
+   All are character pointers for the sake of speed.  Special cases for
+   no differences, as well as for end of line additions must be handled.
+
+   Could be made even smarter, but this works well enough */
+static void
+update_line (old, new, current_line, omax, nmax, inv_botlin)
+     register char *old, *new;
+     int current_line, omax, nmax, inv_botlin;
+{
+  register char *ofd, *ols, *oe, *nfd, *nls, *ne;
+  int temp, lendiff, wsatend, od, nd;
+  int current_invis_chars;
+  int col_lendiff, col_temp;
+#if defined (HANDLE_MULTIBYTE)
+  mbstate_t ps_new, ps_old;
+  int new_offset, old_offset, tmp;
+#endif
+
+  /* If we're at the right edge of a terminal that supports xn, we're
+     ready to wrap around, so do so.  This fixes problems with knowing
+     the exact cursor position and cut-and-paste with certain terminal
+     emulators.  In this calculation, TEMP is the physical screen
+     position of the cursor. */
+  temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
+  if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
+       && _rl_last_v_pos == current_line - 1)
+    {
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         wchar_t wc;
+         mbstate_t ps;
+         int tempwidth, bytes;
+         size_t ret;
+
+         /* This fixes only double-column characters, but if the wrapped
+            character comsumes more than three columns, spaces will be
+            inserted in the string buffer. */
+         if (_rl_wrapped_line[current_line] > 0)
+           _rl_clear_to_eol (_rl_wrapped_line[current_line]);
+
+         memset (&ps, 0, sizeof (mbstate_t));
+         ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
+         if (MB_INVALIDCH (ret))
+           {
+             tempwidth = 1;
+             ret = 1;
+           }
+         else if (MB_NULLWCH (ret))
+           tempwidth = 0;
+         else
+           tempwidth = wcwidth (wc);
+
+         if (tempwidth > 0)
+           {
+             int count;
+             bytes = ret;
+             for (count = 0; count < bytes; count++)
+               putc (new[count], rl_outstream);
+             _rl_last_c_pos = tempwidth;
+             _rl_last_v_pos++;
+             memset (&ps, 0, sizeof (mbstate_t));
+             ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
+             if (ret != 0 && bytes != 0)
+               {
+                 if (MB_INVALIDCH (ret))
+                   memmove (old+bytes, old+1, strlen (old+1));
+                 else
+                   memmove (old+bytes, old+ret, strlen (old+ret));
+                 memcpy (old, new, bytes);
+               }
+           }
+         else
+           {
+             putc (' ', rl_outstream);
+             _rl_last_c_pos = 1;
+             _rl_last_v_pos++;
+             if (old[0] && new[0])
+               old[0] = new[0];
+           }
+       }
+      else
+#endif
+       {
+         if (new[0])
+           putc (new[0], rl_outstream);
+         else
+           putc (' ', rl_outstream);
+         _rl_last_c_pos = 1;           /* XXX */
+         _rl_last_v_pos++;
+         if (old[0] && new[0])
+           old[0] = new[0];
+       }
+    }
+
+      
+  /* Find first difference. */
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    {
+      /* See if the old line is a subset of the new line, so that the
+        only change is adding characters. */
+      temp = (omax < nmax) ? omax : nmax;
+      if (memcmp (old, new, temp) == 0)
+       {
+         ofd = old + temp;
+         nfd = new + temp;
+       }
+      else
+       {      
+         memset (&ps_new, 0, sizeof(mbstate_t));
+         memset (&ps_old, 0, sizeof(mbstate_t));
+
+         if (omax == nmax && STREQN (new, old, omax))
+           {
+             ofd = old + omax;
+             nfd = new + nmax;
+           }
+         else
+           {
+             new_offset = old_offset = 0;
+             for (ofd = old, nfd = new;
+                   (ofd - old < omax) && *ofd &&
+                   _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
+               {
+                 old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
+                 new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
+                 ofd = old + old_offset;
+                 nfd = new + new_offset;
+               }
+           }
+       }
+    }
+  else
+#endif
+  for (ofd = old, nfd = new;
+       (ofd - old < omax) && *ofd && (*ofd == *nfd);
+       ofd++, nfd++)
+    ;
+
+  /* Move to the end of the screen line.  ND and OD are used to keep track
+     of the distance between ne and new and oe and old, respectively, to
+     move a subtraction out of each loop. */
+  for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
+  for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
+
+  /* If no difference, continue to next line. */
+  if (ofd == oe && nfd == ne)
+    return;
+
+  wsatend = 1;                 /* flag for trailing whitespace */
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    {
+      ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
+      nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
+      while ((ols > ofd) && (nls > nfd))
+       {
+         memset (&ps_old, 0, sizeof (mbstate_t));
+         memset (&ps_new, 0, sizeof (mbstate_t));
+
+#if 0
+         /* On advice from jir@yamato.ibm.com */
+         _rl_adjust_point (old, ols - old, &ps_old);
+         _rl_adjust_point (new, nls - new, &ps_new);
+#endif
+
+         if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
+           break;
+
+         if (*ols == ' ')
+           wsatend = 0;
+
+         ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
+         nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
+       }
+    }
+  else
+    {
+#endif /* HANDLE_MULTIBYTE */
+  ols = oe - 1;                        /* find last same */
+  nls = ne - 1;
+  while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
+    {
+      if (*ols != ' ')
+       wsatend = 0;
+      ols--;
+      nls--;
+    }
+#if defined (HANDLE_MULTIBYTE)
+    }
+#endif
+
+  if (wsatend)
+    {
+      ols = oe;
+      nls = ne;
+    }
+#if defined (HANDLE_MULTIBYTE)
+  /* This may not work for stateful encoding, but who cares?  To handle
+     stateful encoding properly, we have to scan each string from the
+     beginning and compare. */
+  else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
+#else
+  else if (*ols != *nls)
+#endif
+    {
+      if (*ols)                        /* don't step past the NUL */
+       {
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
+         else
+           ols++;
+       }
+      if (*nls)
+       {
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
+         else
+           nls++;
+       }
+    }
+
+  /* count of invisible characters in the current invisible line. */
+  current_invis_chars = W_OFFSET (current_line, wrap_offset);
+  if (_rl_last_v_pos != current_line)
+    {
+      _rl_move_vert (current_line);
+      if (current_line == 0 && visible_wrap_offset)
+       _rl_last_c_pos += visible_wrap_offset;
+    }
+
+  /* If this is the first line and there are invisible characters in the
+     prompt string, and the prompt string has not changed, and the current
+     cursor position is before the last invisible character in the prompt,
+     and the index of the character to move to is past the end of the prompt
+     string, then redraw the entire prompt string.  We can only do this
+     reliably if the terminal supports a `cr' capability.
+
+     This is not an efficiency hack -- there is a problem with redrawing
+     portions of the prompt string if they contain terminal escape
+     sequences (like drawing the `unbold' sequence without a corresponding
+     `bold') that manifests itself on certain terminals. */
+
+  lendiff = local_prompt ? strlen (local_prompt) : 0;
+  od = ofd - old;      /* index of first difference in visible line */
+  if (current_line == 0 && !_rl_horizontal_scroll_mode &&
+      _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
+      od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
+    {
+#if defined (__MSDOS__)
+      putc ('\r', rl_outstream);
+#else
+      tputs (_rl_term_cr, 1, _rl_output_character_function);
+#endif
+      _rl_output_some_chars (local_prompt, lendiff);
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
+      else
+       _rl_last_c_pos = lendiff;
+    }
+
+  _rl_move_cursor_relative (od, old);
+
+  /* if (len (new) > len (old))
+     lendiff == difference in buffer
+     col_lendiff == difference on screen
+     When not using multibyte characters, these are equal */
+  lendiff = (nls - nfd) - (ols - ofd);
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
+  else
+    col_lendiff = lendiff;
+
+  /* If we are changing the number of invisible characters in a line, and
+     the spot of first difference is before the end of the invisible chars,
+     lendiff needs to be adjusted. */
+  if (current_line == 0 && !_rl_horizontal_scroll_mode &&
+      current_invis_chars != visible_wrap_offset)
+    {
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         lendiff += visible_wrap_offset - current_invis_chars;
+         col_lendiff += visible_wrap_offset - current_invis_chars;
+       }
+      else
+       {
+         lendiff += visible_wrap_offset - current_invis_chars;
+         col_lendiff = lendiff;
+       }
+    }
+
+  /* Insert (diff (len (old), len (new)) ch. */
+  temp = ne - nfd;
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    col_temp = _rl_col_width (new, nfd - new, ne - new);
+  else
+    col_temp = temp;
+
+  if (col_lendiff > 0) /* XXX - was lendiff */
+    {
+      /* Non-zero if we're increasing the number of lines. */
+      int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
+      /* Sometimes it is cheaper to print the characters rather than
+        use the terminal's capabilities.  If we're growing the number
+        of lines, make sure we actually cause the new line to wrap
+        around on auto-wrapping terminals. */
+      if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
+       {
+         /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
+            _rl_horizontal_scroll_mode == 1, inserting the characters with
+            _rl_term_IC or _rl_term_ic will screw up the screen because of the
+            invisible characters.  We need to just draw them. */
+         if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
+                       lendiff <= prompt_visible_length || !current_invis_chars))
+           {
+             insert_some_chars (nfd, lendiff, col_lendiff);
+             _rl_last_c_pos += col_lendiff;
+           }
+         else if (*ols == 0 && lendiff > 0)
+           {
+             /* At the end of a line the characters do not have to
+                be "inserted".  They can just be placed on the screen. */
+             /* However, this screws up the rest of this block, which
+                assumes you've done the insert because you can. */
+             _rl_output_some_chars (nfd, lendiff);
+             _rl_last_c_pos += col_lendiff;
+           }
+         else
+           {
+             /* We have horizontal scrolling and we are not inserting at
+                the end.  We have invisible characters in this line.  This
+                is a dumb update. */
+             _rl_output_some_chars (nfd, temp);
+             _rl_last_c_pos += col_temp;
+             return;
+           }
+         /* Copy (new) chars to screen from first diff to last match. */
+         temp = nls - nfd;
+         if ((temp - lendiff) > 0)
+           {
+             _rl_output_some_chars (nfd + lendiff, temp - lendiff);
+#if 1
+            /* XXX -- this bears closer inspection.  Fixes a redisplay bug
+               reported against bash-3.0-alpha by Andreas Schwab involving
+               multibyte characters and prompt strings with invisible
+               characters, but was previously disabled. */
+             _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
+#else
+             _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff);
+#endif
+           }
+       }
+      else
+       {
+         /* cannot insert chars, write to EOL */
+         _rl_output_some_chars (nfd, temp);
+         _rl_last_c_pos += col_temp;
+       }
+    }
+  else                         /* Delete characters from line. */
+    {
+      /* If possible and inexpensive to use terminal deletion, then do so. */
+      if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
+       {
+         /* If all we're doing is erasing the invisible characters in the
+            prompt string, don't bother.  It screws up the assumptions
+            about what's on the screen. */
+         if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
+             -lendiff == visible_wrap_offset)
+           col_lendiff = 0;
+
+         if (col_lendiff)
+           delete_chars (-col_lendiff); /* delete (diff) characters */
+
+         /* Copy (new) chars to screen from first diff to last match */
+         temp = nls - nfd;
+         if (temp > 0)
+           {
+             _rl_output_some_chars (nfd, temp);
+             _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
+           }
+       }
+      /* Otherwise, print over the existing material. */
+      else
+       {
+         if (temp > 0)
+           {
+             _rl_output_some_chars (nfd, temp);
+             _rl_last_c_pos += col_temp;
+           }
+         lendiff = (oe - old) - (ne - new);
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
+         else
+           col_lendiff = lendiff;
+
+         if (col_lendiff)
+           {     
+             if (_rl_term_autowrap && current_line < inv_botlin)
+               space_to_eol (col_lendiff);
+             else
+               _rl_clear_to_eol (col_lendiff);
+           }
+       }
+    }
+}
+
+/* Tell the update routines that we have moved onto a new (empty) line. */
+int
+rl_on_new_line ()
+{
+  if (visible_line)
+    visible_line[0] = '\0';
+
+  _rl_last_c_pos = _rl_last_v_pos = 0;
+  _rl_vis_botlin = last_lmargin = 0;
+  if (vis_lbreaks)
+    vis_lbreaks[0] = vis_lbreaks[1] = 0;
+  visible_wrap_offset = 0;
+  return 0;
+}
+
+/* Tell the update routines that we have moved onto a new line with the
+   prompt already displayed.  Code originally from the version of readline
+   distributed with CLISP.  rl_expand_prompt must have already been called
+   (explicitly or implicitly).  This still doesn't work exactly right. */
+int
+rl_on_new_line_with_prompt ()
+{
+  int prompt_size, i, l, real_screenwidth, newlines;
+  char *prompt_last_line, *lprompt;
+
+  /* Initialize visible_line and invisible_line to ensure that they can hold
+     the already-displayed prompt. */
+  prompt_size = strlen (rl_prompt) + 1;
+  init_line_structures (prompt_size);
+
+  /* Make sure the line structures hold the already-displayed prompt for
+     redisplay. */
+  lprompt = local_prompt ? local_prompt : rl_prompt;
+  strcpy (visible_line, lprompt);
+  strcpy (invisible_line, lprompt);
+
+  /* If the prompt contains newlines, take the last tail. */
+  prompt_last_line = strrchr (rl_prompt, '\n');
+  if (!prompt_last_line)
+    prompt_last_line = rl_prompt;
+
+  l = strlen (prompt_last_line);
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);
+  else
+    _rl_last_c_pos = l;
+
+  /* Dissect prompt_last_line into screen lines. Note that here we have
+     to use the real screenwidth. Readline's notion of screenwidth might be
+     one less, see terminal.c. */
+  real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
+  _rl_last_v_pos = l / real_screenwidth;
+  /* If the prompt length is a multiple of real_screenwidth, we don't know
+     whether the cursor is at the end of the last line, or already at the
+     beginning of the next line. Output a newline just to be safe. */
+  if (l > 0 && (l % real_screenwidth) == 0)
+    _rl_output_some_chars ("\n", 1);
+  last_lmargin = 0;
+
+  newlines = 0; i = 0;
+  while (i <= l)
+    {
+      _rl_vis_botlin = newlines;
+      vis_lbreaks[newlines++] = i;
+      i += real_screenwidth;
+    }
+  vis_lbreaks[newlines] = l;
+  visible_wrap_offset = 0;
+
+  rl_display_prompt = rl_prompt;       /* XXX - make sure it's set */
+
+  return 0;
+}
+
+/* Actually update the display, period. */
+int
+rl_forced_update_display ()
+{
+  if (visible_line)
+    {
+      register char *temp = visible_line;
+
+      while (*temp)
+       *temp++ = '\0';
+    }
+  rl_on_new_line ();
+  forced_display++;
+  (*rl_redisplay_function) ();
+  return 0;
+}
+
+/* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
+   DATA is the contents of the screen line of interest; i.e., where
+   the movement is being done. */
+void
+_rl_move_cursor_relative (new, data)
+     int new;
+     const char *data;
+{
+  register int i;
+
+  /* If we don't have to do anything, then return. */
+#if defined (HANDLE_MULTIBYTE)
+  /* If we have multibyte characters, NEW is indexed by the buffer point in
+     a multibyte string, but _rl_last_c_pos is the display position.  In
+     this case, NEW's display position is not obvious and must be
+     calculated. */
+  if (MB_CUR_MAX == 1 || rl_byte_oriented)
+    {
+      if (_rl_last_c_pos == new)
+       return;
+    }
+  else if (_rl_last_c_pos == _rl_col_width (data, 0, new))
+    return;
+#else
+  if (_rl_last_c_pos == new) return;
+#endif
+
+  /* It may be faster to output a CR, and then move forwards instead
+     of moving backwards. */
+  /* i == current physical cursor position. */
+  i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
+  if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
+      (_rl_term_autowrap && i == _rl_screenwidth))
+    {
+#if defined (__MSDOS__)
+      putc ('\r', rl_outstream);
+#else
+      tputs (_rl_term_cr, 1, _rl_output_character_function);
+#endif /* !__MSDOS__ */
+      _rl_last_c_pos = 0;
+    }
+
+  if (_rl_last_c_pos < new)
+    {
+      /* Move the cursor forward.  We do it by printing the command
+        to move the cursor forward if there is one, else print that
+        portion of the output buffer again.  Which is cheaper? */
+
+      /* The above comment is left here for posterity.  It is faster
+        to print one character (non-control) than to print a control
+        sequence telling the terminal to move forward one character.
+        That kind of control is for people who don't know what the
+        data is underneath the cursor. */
+#if defined (HACK_TERMCAP_MOTION)
+      if (_rl_term_forward_char)
+       {
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           {
+             int width;
+             width = _rl_col_width (data, _rl_last_c_pos, new);
+             for (i = 0; i < width; i++)
+               tputs (_rl_term_forward_char, 1, _rl_output_character_function);
+           }
+         else
+           {
+             for (i = _rl_last_c_pos; i < new; i++)
+               tputs (_rl_term_forward_char, 1, _rl_output_character_function);
+           }
+       }
+      else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         tputs (_rl_term_cr, 1, _rl_output_character_function);
+         for (i = 0; i < new; i++)
+           putc (data[i], rl_outstream);
+       }
+      else
+       for (i = _rl_last_c_pos; i < new; i++)
+         putc (data[i], rl_outstream);
+
+#else /* !HACK_TERMCAP_MOTION */
+
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         tputs (_rl_term_cr, 1, _rl_output_character_function);
+         for (i = 0; i < new; i++)
+           putc (data[i], rl_outstream);
+       }
+      else
+       for (i = _rl_last_c_pos; i < new; i++)
+         putc (data[i], rl_outstream);
+
+#endif /* !HACK_TERMCAP_MOTION */
+
+    }
+#if defined (HANDLE_MULTIBYTE)
+  /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
+     The byte length of the string is probably bigger than the column width
+     of the string, which means that if NEW == _rl_last_c_pos, then NEW's
+     display point is less than _rl_last_c_pos. */
+  else if (_rl_last_c_pos >= new)
+#else
+  else if (_rl_last_c_pos > new)
+#endif
+    {
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       _rl_backspace (_rl_last_c_pos - _rl_col_width (data, 0, new));
+      else
+       _rl_backspace (_rl_last_c_pos - new);
+    }
+
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    _rl_last_c_pos =  _rl_col_width (data, 0, new);
+  else
+    _rl_last_c_pos = new;
+}
+
+/* PWP: move the cursor up or down. */
+void
+_rl_move_vert (to)
+     int to;
+{
+  register int delta, i;
+
+  if (_rl_last_v_pos == to || to > _rl_screenheight)
+    return;
+
+  if ((delta = to - _rl_last_v_pos) > 0)
+    {
+      for (i = 0; i < delta; i++)
+       putc ('\n', rl_outstream);
+#if defined (__MSDOS__)
+      putc ('\r', rl_outstream);
+#else
+      tputs (_rl_term_cr, 1, _rl_output_character_function);
+#endif
+      _rl_last_c_pos = 0;
+    }
+  else
+    {                  /* delta < 0 */
+      if (_rl_term_up && *_rl_term_up)
+       for (i = 0; i < -delta; i++)
+         tputs (_rl_term_up, 1, _rl_output_character_function);
+    }
+
+  _rl_last_v_pos = to;         /* Now TO is here */
+}
+
+/* Physically print C on rl_outstream.  This is for functions which know
+   how to optimize the display.  Return the number of characters output. */
+int
+rl_show_char (c)
+     int c;
+{
+  int n = 1;
+  if (META_CHAR (c) && (_rl_output_meta_chars == 0))
+    {
+      fprintf (rl_outstream, "M-");
+      n += 2;
+      c = UNMETA (c);
+    }
+
+#if defined (DISPLAY_TABS)
+  if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
+#else
+  if (CTRL_CHAR (c) || c == RUBOUT)
+#endif /* !DISPLAY_TABS */
+    {
+      fprintf (rl_outstream, "C-");
+      n += 2;
+      c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
+    }
+
+  putc (c, rl_outstream);
+  fflush (rl_outstream);
+  return n;
+}
+
+int
+rl_character_len (c, pos)
+     register int c, pos;
+{
+  unsigned char uc;
+
+  uc = (unsigned char)c;
+
+  if (META_CHAR (uc))
+    return ((_rl_output_meta_chars == 0) ? 4 : 1);
+
+  if (uc == '\t')
+    {
+#if defined (DISPLAY_TABS)
+      return (((pos | 7) + 1) - pos);
+#else
+      return (2);
+#endif /* !DISPLAY_TABS */
+    }
+
+  if (CTRL_CHAR (c) || c == RUBOUT)
+    return (2);
+
+  return ((ISPRINT (uc)) ? 1 : 2);
+}
+
+/* How to print things in the "echo-area".  The prompt is treated as a
+   mini-modeline. */
+
+#if defined (USE_VARARGS)
+int
+#if defined (PREFER_STDARG)
+rl_message (const char *format, ...)
+#else
+rl_message (va_alist)
+     va_dcl
+#endif
+{
+  va_list args;
+#if defined (PREFER_VARARGS)
+  char *format;
+#endif
+
+#if defined (PREFER_STDARG)
+  va_start (args, format);
+#else
+  va_start (args);
+  format = va_arg (args, char *);
+#endif
+
+#if defined (HAVE_VSNPRINTF)
+  vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
+#else
+  vsprintf (msg_buf, format, args);
+  msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
+#endif
+  va_end (args);
+
+  rl_display_prompt = msg_buf;
+  (*rl_redisplay_function) ();
+  return 0;
+}
+#else /* !USE_VARARGS */
+int
+rl_message (format, arg1, arg2)
+     char *format;
+{
+  sprintf (msg_buf, format, arg1, arg2);
+  msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
+  rl_display_prompt = msg_buf;
+  (*rl_redisplay_function) ();
+  return 0;
+}
+#endif /* !USE_VARARGS */
+
+/* How to clear things from the "echo-area". */
+int
+rl_clear_message ()
+{
+  rl_display_prompt = rl_prompt;
+  (*rl_redisplay_function) ();
+  return 0;
+}
+
+int
+rl_reset_line_state ()
+{
+  rl_on_new_line ();
+
+  rl_display_prompt = rl_prompt ? rl_prompt : "";
+  forced_display = 1;
+  return 0;
+}
+
+/* These are getting numerous enough that it's time to create a struct. */
+
+static char *saved_local_prompt;
+static char *saved_local_prefix;
+static int saved_last_invisible;
+static int saved_visible_length;
+static int saved_invis_chars_first_line;
+static int saved_physical_chars;
+
+void
+rl_save_prompt ()
+{
+  saved_local_prompt = local_prompt;
+  saved_local_prefix = local_prompt_prefix;
+  saved_last_invisible = prompt_last_invisible;
+  saved_visible_length = prompt_visible_length;
+  saved_invis_chars_first_line = prompt_invis_chars_first_line;
+  saved_physical_chars = prompt_physical_chars;
+
+  local_prompt = local_prompt_prefix = (char *)0;
+  prompt_last_invisible = prompt_visible_length = 0;
+  prompt_invis_chars_first_line = prompt_physical_chars = 0;
+}
+
+void
+rl_restore_prompt ()
+{
+  FREE (local_prompt);
+  FREE (local_prompt_prefix);
+
+  local_prompt = saved_local_prompt;
+  local_prompt_prefix = saved_local_prefix;
+  prompt_last_invisible = saved_last_invisible;
+  prompt_visible_length = saved_visible_length;
+  prompt_invis_chars_first_line = saved_invis_chars_first_line;
+  prompt_physical_chars = saved_physical_chars;
+}
+
+char *
+_rl_make_prompt_for_search (pchar)
+     int pchar;
+{
+  int len;
+  char *pmt;
+
+  rl_save_prompt ();
+
+  if (saved_local_prompt == 0)
+    {
+      len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
+      pmt = (char *)xmalloc (len + 2);
+      if (len)
+       strcpy (pmt, rl_prompt);
+      pmt[len] = pchar;
+      pmt[len+1] = '\0';
+    }
+  else
+    {
+      len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
+      pmt = (char *)xmalloc (len + 2);
+      if (len)
+       strcpy (pmt, saved_local_prompt);
+      pmt[len] = pchar;
+      pmt[len+1] = '\0';
+      local_prompt = savestring (pmt);
+      prompt_last_invisible = saved_last_invisible;
+      prompt_visible_length = saved_visible_length + 1;
+    }
+
+  return pmt;
+}
+
+/* Quick redisplay hack when erasing characters at the end of the line. */
+void
+_rl_erase_at_end_of_line (l)
+     int l;
+{
+  register int i;
+
+  _rl_backspace (l);
+  for (i = 0; i < l; i++)
+    putc (' ', rl_outstream);
+  _rl_backspace (l);
+  for (i = 0; i < l; i++)
+    visible_line[--_rl_last_c_pos] = '\0';
+  rl_display_fixed++;
+}
+
+/* Clear to the end of the line.  COUNT is the minimum
+   number of character spaces to clear, */
+void
+_rl_clear_to_eol (count)
+     int count;
+{
+  if (_rl_term_clreol)
+    tputs (_rl_term_clreol, 1, _rl_output_character_function);
+  else if (count)
+    space_to_eol (count);
+}
+
+/* Clear to the end of the line using spaces.  COUNT is the minimum
+   number of character spaces to clear, */
+static void
+space_to_eol (count)
+     int count;
+{
+  register int i;
+
+  for (i = 0; i < count; i++)
+   putc (' ', rl_outstream);
+
+  _rl_last_c_pos += count;
+}
+
+void
+_rl_clear_screen ()
+{
+  if (_rl_term_clrpag)
+    tputs (_rl_term_clrpag, 1, _rl_output_character_function);
+  else
+    rl_crlf ();
+}
+
+/* Insert COUNT characters from STRING to the output stream at column COL. */
+static void
+insert_some_chars (string, count, col)
+     char *string;
+     int count, col;
+{
+  /* DEBUGGING */
+  if (MB_CUR_MAX == 1 || rl_byte_oriented)
+    if (count != col)
+      fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
+
+  /* If IC is defined, then we do not have to "enter" insert mode. */
+  if (_rl_term_IC)
+    {
+      char *buffer;
+
+      buffer = tgoto (_rl_term_IC, 0, col);
+      tputs (buffer, 1, _rl_output_character_function);
+      _rl_output_some_chars (string, count);
+    }
+  else
+    {
+      register int i;
+
+      /* If we have to turn on insert-mode, then do so. */
+      if (_rl_term_im && *_rl_term_im)
+       tputs (_rl_term_im, 1, _rl_output_character_function);
+
+      /* If there is a special command for inserting characters, then
+        use that first to open up the space. */
+      if (_rl_term_ic && *_rl_term_ic)
+       {
+         for (i = col; i--; )
+           tputs (_rl_term_ic, 1, _rl_output_character_function);
+       }
+
+      /* Print the text. */
+      _rl_output_some_chars (string, count);
+
+      /* If there is a string to turn off insert mode, we had best use
+        it now. */
+      if (_rl_term_ei && *_rl_term_ei)
+       tputs (_rl_term_ei, 1, _rl_output_character_function);
+    }
+}
+
+/* Delete COUNT characters from the display line. */
+static void
+delete_chars (count)
+     int count;
+{
+  if (count > _rl_screenwidth) /* XXX */
+    return;
+
+  if (_rl_term_DC && *_rl_term_DC)
+    {
+      char *buffer;
+      buffer = tgoto (_rl_term_DC, count, count);
+      tputs (buffer, count, _rl_output_character_function);
+    }
+  else
+    {
+      if (_rl_term_dc && *_rl_term_dc)
+       while (count--)
+         tputs (_rl_term_dc, 1, _rl_output_character_function);
+    }
+}
+
+void
+_rl_update_final ()
+{
+  int full_lines;
+
+  full_lines = 0;
+  /* If the cursor is the only thing on an otherwise-blank last line,
+     compensate so we don't print an extra CRLF. */
+  if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
+       visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
+    {
+      _rl_vis_botlin--;
+      full_lines = 1;
+    }
+  _rl_move_vert (_rl_vis_botlin);
+  /* If we've wrapped lines, remove the final xterm line-wrap flag. */
+  if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
+    {
+      char *last_line;
+
+      last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
+      _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
+      _rl_clear_to_eol (0);
+      putc (last_line[_rl_screenwidth - 1], rl_outstream);
+    }
+  _rl_vis_botlin = 0;
+  rl_crlf ();
+  fflush (rl_outstream);
+  rl_display_fixed++;
+}
+
+/* Move to the start of the current line. */
+static void
+cr ()
+{
+  if (_rl_term_cr)
+    {
+#if defined (__MSDOS__)
+      putc ('\r', rl_outstream);
+#else
+      tputs (_rl_term_cr, 1, _rl_output_character_function);
+#endif
+      _rl_last_c_pos = 0;
+    }
+}
+
+/* Redraw the last line of a multi-line prompt that may possibly contain
+   terminal escape sequences.  Called with the cursor at column 0 of the
+   line to draw the prompt on. */
+static void
+redraw_prompt (t)
+     char *t;
+{
+  char *oldp, *oldl, *oldlprefix;
+  int oldlen, oldlast, oldplen, oldninvis, oldphyschars;
+
+  /* Geez, I should make this a struct. */
+  oldp = rl_display_prompt;
+  oldl = local_prompt;
+  oldlprefix = local_prompt_prefix;
+  oldlen = prompt_visible_length;
+  oldplen = prompt_prefix_length;
+  oldlast = prompt_last_invisible;
+  oldninvis = prompt_invis_chars_first_line;
+  oldphyschars = prompt_physical_chars;
+
+  rl_display_prompt = t;
+  local_prompt = expand_prompt (t, &prompt_visible_length,
+                                  &prompt_last_invisible,
+                                  &prompt_invis_chars_first_line,
+                                  &prompt_physical_chars);
+  local_prompt_prefix = (char *)NULL;
+  rl_forced_update_display ();
+
+  rl_display_prompt = oldp;
+  local_prompt = oldl;
+  local_prompt_prefix = oldlprefix;
+  prompt_visible_length = oldlen;
+  prompt_prefix_length = oldplen;
+  prompt_last_invisible = oldlast;
+  prompt_invis_chars_first_line = oldninvis;
+  prompt_physical_chars = oldphyschars;
+}
+      
+/* Redisplay the current line after a SIGWINCH is received. */
+void
+_rl_redisplay_after_sigwinch ()
+{
+  char *t;
+
+  /* Clear the current line and put the cursor at column 0.  Make sure
+     the right thing happens if we have wrapped to a new screen line. */
+  if (_rl_term_cr)
+    {
+#if defined (__MSDOS__)
+      putc ('\r', rl_outstream);
+#else
+      tputs (_rl_term_cr, 1, _rl_output_character_function);
+#endif
+      _rl_last_c_pos = 0;
+#if defined (__MSDOS__)
+      space_to_eol (_rl_screenwidth);
+      putc ('\r', rl_outstream);
+#else
+      if (_rl_term_clreol)
+       tputs (_rl_term_clreol, 1, _rl_output_character_function);
+      else
+       {
+         space_to_eol (_rl_screenwidth);
+         tputs (_rl_term_cr, 1, _rl_output_character_function);
+       }
+#endif
+      if (_rl_last_v_pos > 0)
+       _rl_move_vert (0);
+    }
+  else
+    rl_crlf ();
+
+  /* Redraw only the last line of a multi-line prompt. */
+  t = strrchr (rl_display_prompt, '\n');
+  if (t)
+    redraw_prompt (++t);
+  else
+    rl_forced_update_display ();
+}
+
+void
+_rl_clean_up_for_exit ()
+{
+  if (readline_echoing_p)
+    {
+      _rl_move_vert (_rl_vis_botlin);
+      _rl_vis_botlin = 0;
+      fflush (rl_outstream);
+      rl_restart_output (1, 0);
+    }
+}
+
+void
+_rl_erase_entire_line ()
+{
+  cr ();
+  _rl_clear_to_eol (0);
+  cr ();
+  fflush (rl_outstream);
+}
+
+/* return the `current display line' of the cursor -- the number of lines to
+   move up to get to the first screen line of the current readline line. */
+int
+_rl_current_display_line ()
+{
+  int ret, nleft;
+
+  /* Find out whether or not there might be invisible characters in the
+     editing buffer. */
+  if (rl_display_prompt == rl_prompt)
+    nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
+  else
+    nleft = _rl_last_c_pos - _rl_screenwidth;
+
+  if (nleft > 0)
+    ret = 1 + nleft / _rl_screenwidth;
+  else
+    ret = 0;
+
+  return ret;
+}
+
+#if defined (HANDLE_MULTIBYTE)
+/* Calculate the number of screen columns occupied by STR from START to END.
+   In the case of multibyte characters with stateful encoding, we have to
+   scan from the beginning of the string to take the state into account. */
+static int
+_rl_col_width (str, start, end)
+     const char *str;
+     int start, end;
+{
+  wchar_t wc;
+  mbstate_t ps = {0};
+  int tmp, point, width, max;
+
+  if (end <= start)
+    return 0;
+
+  point = 0;
+  max = end;
+
+  while (point < start)
+    {
+      tmp = mbrlen (str + point, max, &ps);
+      if (MB_INVALIDCH ((size_t)tmp))
+       {
+         /* In this case, the bytes are invalid or too short to compose a
+            multibyte character, so we assume that the first byte represents
+            a single character. */
+         point++;
+         max--;
+
+         /* Clear the state of the byte sequence, because in this case the
+            effect of mbstate is undefined. */
+         memset (&ps, 0, sizeof (mbstate_t));
+       }
+      else if (MB_NULLWCH (tmp))
+       break;          /* Found '\0' */
+      else
+       {
+         point += tmp;
+         max -= tmp;
+       }
+    }
+
+  /* If START is not a byte that starts a character, then POINT will be
+     greater than START.  In this case, assume that (POINT - START) gives
+     a byte count that is the number of columns of difference. */
+  width = point - start;
+
+  while (point < end)
+    {
+      tmp = mbrtowc (&wc, str + point, max, &ps);
+      if (MB_INVALIDCH ((size_t)tmp))
+       {
+         /* In this case, the bytes are invalid or too short to compose a
+            multibyte character, so we assume that the first byte represents
+            a single character. */
+         point++;
+         max--;
+
+         /* and assume that the byte occupies a single column. */
+         width++;
+
+         /* Clear the state of the byte sequence, because in this case the
+            effect of mbstate is undefined. */
+         memset (&ps, 0, sizeof (mbstate_t));
+       }
+      else if (MB_NULLWCH (tmp))
+       break;                  /* Found '\0' */
+      else
+       {
+         point += tmp;
+         max -= tmp;
+         tmp = wcwidth(wc);
+         width += (tmp >= 0) ? tmp : 1;
+       }
+    }
+
+  width += point - end;
+
+  return width;
+}
+#endif /* HANDLE_MULTIBYTE */
index 2ab34cb..6847014 100644 (file)
@@ -206,23 +206,24 @@ get_history_event (string, caller_index, delimiting_quote)
 
   /* Only a closing `?' or a newline delimit a substring search string. */
   for (local_index = i; c = string[i]; i++)
+    {
 #if defined (HANDLE_MULTIBYTE)
-    if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-      {
-       int v;
-       mbstate_t ps;
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         int v;
+         mbstate_t ps;
+
+         memset (&ps, 0, sizeof (mbstate_t));
+         /* These produce warnings because we're passing a const string to a
+            function that takes a non-const string. */
+         _rl_adjust_point ((char *)string, i, &ps);
+         if ((v = _rl_get_char_len ((char *)string + i, &ps)) > 1)
+           {
+             i += v - 1;
+             continue;
+           }
+        }
 
-       memset (&ps, 0, sizeof (mbstate_t));
-       /* These produce warnings because we're passing a const string to a
-          function that takes a non-const string. */
-       _rl_adjust_point ((char *)string, i, &ps);
-       if ((v = _rl_get_char_len ((char *)string + i, &ps)) > 1)
-         {
-           i += v - 1;
-           continue;
-         }
-      }
-    else
 #endif /* HANDLE_MULTIBYTE */
       if ((!substring_okay && (whitespace (c) || c == ':' ||
          (history_search_delimiter_chars && member (c, history_search_delimiter_chars)) ||
@@ -230,6 +231,7 @@ get_history_event (string, caller_index, delimiting_quote)
          string[i] == '\n' ||
          (substring_okay && string[i] == '?'))
        break;
+    }
 
   which = i - local_index;
   temp = (char *)xmalloc (1 + which);
diff --git a/lib/readline/histexpand.c~ b/lib/readline/histexpand.c~
new file mode 100644 (file)
index 0000000..2ab34cb
--- /dev/null
@@ -0,0 +1,1591 @@
+/* histexpand.c -- history expansion. */
+
+/* Copyright (C) 1989-2004 Free Software Foundation, Inc.
+
+   This file contains the GNU History Library (the Library), a set of
+   routines for managing the text of previously typed lines.
+
+   The Library 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 2, or (at your option)
+   any later version.
+
+   The Library 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.
+
+   The GNU General Public License is often shipped with GNU software, and
+   is generally kept in a file called COPYING or LICENSE.  If you do not
+   have a copy of the license, write to the Free Software Foundation,
+   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
+#define READLINE_LIBRARY
+
+#if defined (HAVE_CONFIG_H)
+#  include <config.h>
+#endif
+
+#include <stdio.h>
+
+#if defined (HAVE_STDLIB_H)
+#  include <stdlib.h>
+#else
+#  include "ansi_stdlib.h"
+#endif /* HAVE_STDLIB_H */
+
+#if defined (HAVE_UNISTD_H)
+#  ifndef _MINIX
+#    include <sys/types.h>
+#  endif
+#  include <unistd.h>
+#endif
+
+#include "rlmbutil.h"
+
+#include "history.h"
+#include "histlib.h"
+
+#include "rlshell.h"
+#include "xmalloc.h"
+
+#define HISTORY_WORD_DELIMITERS                " \t\n;&()|<>"
+#define HISTORY_QUOTE_CHARACTERS       "\"'`"
+
+#define slashify_in_quotes "\\`\"$"
+
+typedef int _hist_search_func_t PARAMS((const char *, int));
+
+extern int rl_byte_oriented;   /* declared in mbutil.c */
+
+static char error_pointer;
+
+static char *subst_lhs;
+static char *subst_rhs;
+static int subst_lhs_len;
+static int subst_rhs_len;
+
+static char *get_history_word_specifier PARAMS((char *, char *, int *));
+static char *history_find_word PARAMS((char *, int));
+static int history_tokenize_word PARAMS((const char *, int));
+static char *history_substring PARAMS((const char *, int, int));
+
+static char *quote_breaks PARAMS((char *));
+
+/* Variables exported by this file. */
+/* The character that represents the start of a history expansion
+   request.  This is usually `!'. */
+char history_expansion_char = '!';
+
+/* The character that invokes word substitution if found at the start of
+   a line.  This is usually `^'. */
+char history_subst_char = '^';
+
+/* During tokenization, if this character is seen as the first character
+   of a word, then it, and all subsequent characters upto a newline are
+   ignored.  For a Bourne shell, this should be '#'.  Bash special cases
+   the interactive comment character to not be a comment delimiter. */
+char history_comment_char = '\0';
+
+/* The list of characters which inhibit the expansion of text if found
+   immediately following history_expansion_char. */
+char *history_no_expand_chars = " \t\n\r=";
+
+/* If set to a non-zero value, single quotes inhibit history expansion.
+   The default is 0. */
+int history_quotes_inhibit_expansion = 0;
+
+/* Used to split words by history_tokenize_internal. */
+char *history_word_delimiters = HISTORY_WORD_DELIMITERS;
+
+/* If set, this points to a function that is called to verify that a
+   particular history expansion should be performed. */
+rl_linebuf_func_t *history_inhibit_expansion_function;
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     History Expansion                           */
+/*                                                                 */
+/* **************************************************************** */
+
+/* Hairy history expansion on text, not tokens.  This is of general
+   use, and thus belongs in this library. */
+
+/* The last string searched for by a !?string? search. */
+static char *search_string;
+
+/* The last string matched by a !?string? search. */
+static char *search_match;
+
+/* Return the event specified at TEXT + OFFSET modifying OFFSET to
+   point to after the event specifier.  Just a pointer to the history
+   line is returned; NULL is returned in the event of a bad specifier.
+   You pass STRING with *INDEX equal to the history_expansion_char that
+   begins this specification.
+   DELIMITING_QUOTE is a character that is allowed to end the string
+   specification for what to search for in addition to the normal
+   characters `:', ` ', `\t', `\n', and sometimes `?'.
+   So you might call this function like:
+   line = get_history_event ("!echo:p", &index, 0);  */
+char *
+get_history_event (string, caller_index, delimiting_quote)
+     const char *string;
+     int *caller_index;
+     int delimiting_quote;
+{
+  register int i;
+  register char c;
+  HIST_ENTRY *entry;
+  int which, sign, local_index, substring_okay;
+  _hist_search_func_t *search_func;
+  char *temp;
+
+  /* The event can be specified in a number of ways.
+
+     !!   the previous command
+     !n   command line N
+     !-n  current command-line minus N
+     !str the most recent command starting with STR
+     !?str[?]
+         the most recent command containing STR
+
+     All values N are determined via HISTORY_BASE. */
+
+  i = *caller_index;
+
+  if (string[i] != history_expansion_char)
+    return ((char *)NULL);
+
+  /* Move on to the specification. */
+  i++;
+
+  sign = 1;
+  substring_okay = 0;
+
+#define RETURN_ENTRY(e, w) \
+       return ((e = history_get (w)) ? e->line : (char *)NULL)
+
+  /* Handle !! case. */
+  if (string[i] == history_expansion_char)
+    {
+      i++;
+      which = history_base + (history_length - 1);
+      *caller_index = i;
+      RETURN_ENTRY (entry, which);
+    }
+
+  /* Hack case of numeric line specification. */
+  if (string[i] == '-')
+    {
+      sign = -1;
+      i++;
+    }
+
+  if (_rl_digit_p (string[i]))
+    {
+      /* Get the extent of the digits and compute the value. */
+      for (which = 0; _rl_digit_p (string[i]); i++)
+       which = (which * 10) + _rl_digit_value (string[i]);
+
+      *caller_index = i;
+
+      if (sign < 0)
+       which = (history_length + history_base) - which;
+
+      RETURN_ENTRY (entry, which);
+    }
+
+  /* This must be something to search for.  If the spec begins with
+     a '?', then the string may be anywhere on the line.  Otherwise,
+     the string must be found at the start of a line. */
+  if (string[i] == '?')
+    {
+      substring_okay++;
+      i++;
+    }
+
+  /* Only a closing `?' or a newline delimit a substring search string. */
+  for (local_index = i; c = string[i]; i++)
+#if defined (HANDLE_MULTIBYTE)
+    if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      {
+       int v;
+       mbstate_t ps;
+
+       memset (&ps, 0, sizeof (mbstate_t));
+       /* These produce warnings because we're passing a const string to a
+          function that takes a non-const string. */
+       _rl_adjust_point ((char *)string, i, &ps);
+       if ((v = _rl_get_char_len ((char *)string + i, &ps)) > 1)
+         {
+           i += v - 1;
+           continue;
+         }
+      }
+    else
+#endif /* HANDLE_MULTIBYTE */
+      if ((!substring_okay && (whitespace (c) || c == ':' ||
+         (history_search_delimiter_chars && member (c, history_search_delimiter_chars)) ||
+         string[i] == delimiting_quote)) ||
+         string[i] == '\n' ||
+         (substring_okay && string[i] == '?'))
+       break;
+
+  which = i - local_index;
+  temp = (char *)xmalloc (1 + which);
+  if (which)
+    strncpy (temp, string + local_index, which);
+  temp[which] = '\0';
+
+  if (substring_okay && string[i] == '?')
+    i++;
+
+  *caller_index = i;
+
+#define FAIL_SEARCH() \
+  do { \
+    history_offset = history_length; free (temp) ; return (char *)NULL; \
+  } while (0)
+
+  /* If there is no search string, try to use the previous search string,
+     if one exists.  If not, fail immediately. */
+  if (*temp == '\0' && substring_okay)
+    {
+      if (search_string)
+        {
+          free (temp);
+          temp = savestring (search_string);
+        }
+      else
+        FAIL_SEARCH ();
+    }
+
+  search_func = substring_okay ? history_search : history_search_prefix;
+  while (1)
+    {
+      local_index = (*search_func) (temp, -1);
+
+      if (local_index < 0)
+       FAIL_SEARCH ();
+
+      if (local_index == 0 || substring_okay)
+       {
+         entry = current_history ();
+         history_offset = history_length;
+       
+         /* If this was a substring search, then remember the
+            string that we matched for word substitution. */
+         if (substring_okay)
+           {
+             FREE (search_string);
+             search_string = temp;
+
+             FREE (search_match);
+             search_match = history_find_word (entry->line, local_index);
+           }
+         else
+           free (temp);
+
+         return (entry->line);
+       }
+
+      if (history_offset)
+       history_offset--;
+      else
+       FAIL_SEARCH ();
+    }
+#undef FAIL_SEARCH
+#undef RETURN_ENTRY
+}
+
+/* Function for extracting single-quoted strings.  Used for inhibiting
+   history expansion within single quotes. */
+
+/* Extract the contents of STRING as if it is enclosed in single quotes.
+   SINDEX, when passed in, is the offset of the character immediately
+   following the opening single quote; on exit, SINDEX is left pointing
+   to the closing single quote. */
+static void
+hist_string_extract_single_quoted (string, sindex)
+     char *string;
+     int *sindex;
+{
+  register int i;
+
+  for (i = *sindex; string[i] && string[i] != '\''; i++)
+    ;
+
+  *sindex = i;
+}
+
+static char *
+quote_breaks (s)
+     char *s;
+{
+  register char *p, *r;
+  char *ret;
+  int len = 3;
+
+  for (p = s; p && *p; p++, len++)
+    {
+      if (*p == '\'')
+       len += 3;
+      else if (whitespace (*p) || *p == '\n')
+       len += 2;
+    }
+
+  r = ret = (char *)xmalloc (len);
+  *r++ = '\'';
+  for (p = s; p && *p; )
+    {
+      if (*p == '\'')
+       {
+         *r++ = '\'';
+         *r++ = '\\';
+         *r++ = '\'';
+         *r++ = '\'';
+         p++;
+       }
+      else if (whitespace (*p) || *p == '\n')
+       {
+         *r++ = '\'';
+         *r++ = *p++;
+         *r++ = '\'';
+       }
+      else
+       *r++ = *p++;
+    }
+  *r++ = '\'';
+  *r = '\0';
+  return ret;
+}
+
+static char *
+hist_error(s, start, current, errtype)
+      char *s;
+      int start, current, errtype;
+{
+  char *temp;
+  const char *emsg;
+  int ll, elen;
+
+  ll = current - start;
+
+  switch (errtype)
+    {
+    case EVENT_NOT_FOUND:
+      emsg = "event not found";
+      elen = 15;
+      break;
+    case BAD_WORD_SPEC:
+      emsg = "bad word specifier";
+      elen = 18;
+      break;
+    case SUBST_FAILED:
+      emsg = "substitution failed";
+      elen = 19;
+      break;
+    case BAD_MODIFIER:
+      emsg = "unrecognized history modifier";
+      elen = 29;
+      break;
+    case NO_PREV_SUBST:
+      emsg = "no previous substitution";
+      elen = 24;
+      break;
+    default:
+      emsg = "unknown expansion error";
+      elen = 23;
+      break;
+    }
+
+  temp = (char *)xmalloc (ll + elen + 3);
+  strncpy (temp, s + start, ll);
+  temp[ll] = ':';
+  temp[ll + 1] = ' ';
+  strcpy (temp + ll + 2, emsg);
+  return (temp);
+}
+
+/* Get a history substitution string from STR starting at *IPTR
+   and return it.  The length is returned in LENPTR.
+
+   A backslash can quote the delimiter.  If the string is the
+   empty string, the previous pattern is used.  If there is
+   no previous pattern for the lhs, the last history search
+   string is used.
+
+   If IS_RHS is 1, we ignore empty strings and set the pattern
+   to "" anyway.  subst_lhs is not changed if the lhs is empty;
+   subst_rhs is allowed to be set to the empty string. */
+
+static char *
+get_subst_pattern (str, iptr, delimiter, is_rhs, lenptr)
+     char *str;
+     int *iptr, delimiter, is_rhs, *lenptr;
+{
+  register int si, i, j, k;
+  char *s;
+#if defined (HANDLE_MULTIBYTE)
+  mbstate_t ps;
+#endif
+
+  s = (char *)NULL;
+  i = *iptr;
+
+#if defined (HANDLE_MULTIBYTE)
+  memset (&ps, 0, sizeof (mbstate_t));
+  _rl_adjust_point (str, i, &ps);
+#endif
+
+  for (si = i; str[si] && str[si] != delimiter; si++)
+#if defined (HANDLE_MULTIBYTE)
+    if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+      {
+       int v;
+       if ((v = _rl_get_char_len (str + si, &ps)) > 1)
+         si += v - 1;
+       else if (str[si] == '\\' && str[si + 1] == delimiter)
+         si++;
+      }
+    else
+#endif /* HANDLE_MULTIBYTE */
+      if (str[si] == '\\' && str[si + 1] == delimiter)
+       si++;
+
+  if (si > i || is_rhs)
+    {
+      s = (char *)xmalloc (si - i + 1);
+      for (j = 0, k = i; k < si; j++, k++)
+       {
+         /* Remove a backslash quoting the search string delimiter. */
+         if (str[k] == '\\' && str[k + 1] == delimiter)
+           k++;
+         s[j] = str[k];
+       }
+      s[j] = '\0';
+      if (lenptr)
+       *lenptr = j;
+    }
+
+  i = si;
+  if (str[i])
+    i++;
+  *iptr = i;
+
+  return s;
+}
+
+static void
+postproc_subst_rhs ()
+{
+  char *new;
+  int i, j, new_size;
+
+  new = (char *)xmalloc (new_size = subst_rhs_len + subst_lhs_len);
+  for (i = j = 0; i < subst_rhs_len; i++)
+    {
+      if (subst_rhs[i] == '&')
+       {
+         if (j + subst_lhs_len >= new_size)
+           new = (char *)xrealloc (new, (new_size = new_size * 2 + subst_lhs_len));
+         strcpy (new + j, subst_lhs);
+         j += subst_lhs_len;
+       }
+      else
+       {
+         /* a single backslash protects the `&' from lhs interpolation */
+         if (subst_rhs[i] == '\\' && subst_rhs[i + 1] == '&')
+           i++;
+         if (j >= new_size)
+           new = (char *)xrealloc (new, new_size *= 2);
+         new[j++] = subst_rhs[i];
+       }
+    }
+  new[j] = '\0';
+  free (subst_rhs);
+  subst_rhs = new;
+  subst_rhs_len = j;
+}
+
+/* Expand the bulk of a history specifier starting at STRING[START].
+   Returns 0 if everything is OK, -1 if an error occurred, and 1
+   if the `p' modifier was supplied and the caller should just print
+   the returned string.  Returns the new index into string in
+   *END_INDEX_PTR, and the expanded specifier in *RET_STRING. */
+static int
+history_expand_internal (string, start, end_index_ptr, ret_string, current_line)
+     char *string;
+     int start, *end_index_ptr;
+     char **ret_string;
+     char *current_line;       /* for !# */
+{
+  int i, n, starting_index;
+  int substitute_globally, subst_bywords, want_quotes, print_only;
+  char *event, *temp, *result, *tstr, *t, c, *word_spec;
+  int result_len;
+#if defined (HANDLE_MULTIBYTE)
+  mbstate_t ps;
+
+  memset (&ps, 0, sizeof (mbstate_t));
+#endif
+
+  result = (char *)xmalloc (result_len = 128);
+
+  i = start;
+
+  /* If it is followed by something that starts a word specifier,
+     then !! is implied as the event specifier. */
+
+  if (member (string[i + 1], ":$*%^"))
+    {
+      char fake_s[3];
+      int fake_i = 0;
+      i++;
+      fake_s[0] = fake_s[1] = history_expansion_char;
+      fake_s[2] = '\0';
+      event = get_history_event (fake_s, &fake_i, 0);
+    }
+  else if (string[i + 1] == '#')
+    {
+      i += 2;
+      event = current_line;
+    }
+  else
+    {
+      int quoted_search_delimiter = 0;
+
+      /* If the character before this `!' is a double or single
+        quote, then this expansion takes place inside of the
+        quoted string.  If we have to search for some text ("!foo"),
+        allow the delimiter to end the search string. */
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         int c, l;
+         l = _rl_find_prev_mbchar (string, i, MB_FIND_ANY);
+         c = string[l];
+         /* XXX - original patch had i - 1 ???  If i == 0 it would fail. */
+         if (i && (c == '\'' || c == '"'))
+           quoted_search_delimiter = c;
+       }
+      else
+#endif /* HANDLE_MULTIBYTE */    
+       if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
+         quoted_search_delimiter = string[i - 1];
+
+      event = get_history_event (string, &i, quoted_search_delimiter);
+    }
+         
+  if (event == 0)
+    {
+      *ret_string = hist_error (string, start, i, EVENT_NOT_FOUND);
+      free (result);
+      return (-1);
+    }
+
+  /* If a word specifier is found, then do what that requires. */
+  starting_index = i;
+  word_spec = get_history_word_specifier (string, event, &i);
+
+  /* There is no such thing as a `malformed word specifier'.  However,
+     it is possible for a specifier that has no match.  In that case,
+     we complain. */
+  if (word_spec == (char *)&error_pointer)
+    {
+      *ret_string = hist_error (string, starting_index, i, BAD_WORD_SPEC);
+      free (result);
+      return (-1);
+    }
+
+  /* If no word specifier, than the thing of interest was the event. */
+  temp = word_spec ? savestring (word_spec) : savestring (event);
+  FREE (word_spec);
+
+  /* Perhaps there are other modifiers involved.  Do what they say. */
+  want_quotes = substitute_globally = subst_bywords = print_only = 0;
+  starting_index = i;
+
+  while (string[i] == ':')
+    {
+      c = string[i + 1];
+
+      if (c == 'g' || c == 'a')
+       {
+         substitute_globally = 1;
+         i++;
+         c = string[i + 1];
+       }
+      else if (c == 'G')
+       {
+         subst_bywords = 1;
+         i++;
+         c = string[i + 1];
+       }
+
+      switch (c)
+       {
+       default:
+         *ret_string = hist_error (string, i+1, i+2, BAD_MODIFIER);
+         free (result);
+         free (temp);
+         return -1;
+
+       case 'q':
+         want_quotes = 'q';
+         break;
+
+       case 'x':
+         want_quotes = 'x';
+         break;
+
+         /* :p means make this the last executed line.  So we
+            return an error state after adding this line to the
+            history. */
+       case 'p':
+         print_only++;
+         break;
+
+         /* :t discards all but the last part of the pathname. */
+       case 't':
+         tstr = strrchr (temp, '/');
+         if (tstr)
+           {
+             tstr++;
+             t = savestring (tstr);
+             free (temp);
+             temp = t;
+           }
+         break;
+
+         /* :h discards the last part of a pathname. */
+       case 'h':
+         tstr = strrchr (temp, '/');
+         if (tstr)
+           *tstr = '\0';
+         break;
+
+         /* :r discards the suffix. */
+       case 'r':
+         tstr = strrchr (temp, '.');
+         if (tstr)
+           *tstr = '\0';
+         break;
+
+         /* :e discards everything but the suffix. */
+       case 'e':
+         tstr = strrchr (temp, '.');
+         if (tstr)
+           {
+             t = savestring (tstr);
+             free (temp);
+             temp = t;
+           }
+         break;
+
+       /* :s/this/that substitutes `that' for the first
+          occurrence of `this'.  :gs/this/that substitutes `that'
+          for each occurrence of `this'.  :& repeats the last
+          substitution.  :g& repeats the last substitution
+          globally. */
+
+       case '&':
+       case 's':
+         {
+           char *new_event;
+           int delimiter, failed, si, l_temp, ws, we;
+
+           if (c == 's')
+             {
+               if (i + 2 < (int)strlen (string))
+                 {
+#if defined (HANDLE_MULTIBYTE)
+                   if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+                     {
+                       _rl_adjust_point (string, i + 2, &ps);
+                       if (_rl_get_char_len (string + i + 2, &ps) > 1)
+                         delimiter = 0;
+                       else
+                         delimiter = string[i + 2];
+                     }
+                   else
+#endif /* HANDLE_MULTIBYTE */
+                     delimiter = string[i + 2];
+                 }
+               else
+                 break;        /* no search delimiter */
+
+               i += 3;
+
+               t = get_subst_pattern (string, &i, delimiter, 0, &subst_lhs_len);
+               /* An empty substitution lhs with no previous substitution
+                  uses the last search string as the lhs. */
+               if (t)
+                 {
+                   FREE (subst_lhs);
+                   subst_lhs = t;
+                 }
+               else if (!subst_lhs)
+                 {
+                   if (search_string && *search_string)
+                     {
+                       subst_lhs = savestring (search_string);
+                       subst_lhs_len = strlen (subst_lhs);
+                     }
+                   else
+                     {
+                       subst_lhs = (char *) NULL;
+                       subst_lhs_len = 0;
+                     }
+                 }
+
+               FREE (subst_rhs);
+               subst_rhs = get_subst_pattern (string, &i, delimiter, 1, &subst_rhs_len);
+
+               /* If `&' appears in the rhs, it's supposed to be replaced
+                  with the lhs. */
+               if (member ('&', subst_rhs))
+                 postproc_subst_rhs ();
+             }
+           else
+             i += 2;
+
+           /* If there is no lhs, the substitution can't succeed. */
+           if (subst_lhs_len == 0)
+             {
+               *ret_string = hist_error (string, starting_index, i, NO_PREV_SUBST);
+               free (result);
+               free (temp);
+               return -1;
+             }
+
+           l_temp = strlen (temp);
+           /* Ignore impossible cases. */
+           if (subst_lhs_len > l_temp)
+             {
+               *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
+               free (result);
+               free (temp);
+               return (-1);
+             }
+
+           /* Find the first occurrence of THIS in TEMP. */
+           /* Substitute SUBST_RHS for SUBST_LHS in TEMP.  There are three
+              cases to consider:
+
+                1.  substitute_globally == subst_bywords == 0
+                2.  substitute_globally == 1 && subst_bywords == 0
+                3.  substitute_globally == 0 && subst_bywords == 1
+
+              In the first case, we substitute for the first occurrence only.
+              In the second case, we substitute for every occurrence.
+              In the third case, we tokenize into words and substitute the
+              first occurrence of each word. */
+
+           si = we = 0;
+           for (failed = 1; (si + subst_lhs_len) <= l_temp; si++)
+             {
+               /* First skip whitespace and find word boundaries if
+                  we're past the end of the word boundary we found
+                  the last time. */
+               if (subst_bywords && si > we)
+                 {
+                   for (; temp[si] && whitespace (temp[si]); si++)
+                     ;
+                   ws = si;
+                   we = history_tokenize_word (temp, si);
+                 }
+
+               if (STREQN (temp+si, subst_lhs, subst_lhs_len))
+                 {
+                   int len = subst_rhs_len - subst_lhs_len + l_temp;
+                   new_event = (char *)xmalloc (1 + len);
+                   strncpy (new_event, temp, si);
+                   strncpy (new_event + si, subst_rhs, subst_rhs_len);
+                   strncpy (new_event + si + subst_rhs_len,
+                            temp + si + subst_lhs_len,
+                            l_temp - (si + subst_lhs_len));
+                   new_event[len] = '\0';
+                   free (temp);
+                   temp = new_event;
+
+                   failed = 0;
+
+                   if (substitute_globally)
+                     {
+                       /* Reported to fix a bug that causes it to skip every
+                          other match when matching a single character.  Was
+                          si += subst_rhs_len previously. */
+                       si += subst_rhs_len - 1;
+                       l_temp = strlen (temp);
+                       substitute_globally++;
+                       continue;
+                     }
+                   else if (subst_bywords)
+                     {
+                       si = we;
+                       l_temp = strlen (temp);
+                       continue;
+                     }
+                   else
+                     break;
+                 }
+             }
+
+           if (substitute_globally > 1)
+             {
+               substitute_globally = 0;
+               continue;       /* don't want to increment i */
+             }
+
+           if (failed == 0)
+             continue;         /* don't want to increment i */
+
+           *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
+           free (result);
+           free (temp);
+           return (-1);
+         }
+       }
+      i += 2;
+    }
+  /* Done with modfiers. */
+  /* Believe it or not, we have to back the pointer up by one. */
+  --i;
+
+  if (want_quotes)
+    {
+      char *x;
+
+      if (want_quotes == 'q')
+       x = sh_single_quote (temp);
+      else if (want_quotes == 'x')
+       x = quote_breaks (temp);
+      else
+       x = savestring (temp);
+
+      free (temp);
+      temp = x;
+    }
+
+  n = strlen (temp);
+  if (n >= result_len)
+    result = (char *)xrealloc (result, n + 2);
+  strcpy (result, temp);
+  free (temp);
+
+  *end_index_ptr = i;
+  *ret_string = result;
+  return (print_only);
+}
+
+/* Expand the string STRING, placing the result into OUTPUT, a pointer
+   to a string.  Returns:
+
+  -1) If there was an error in expansion.
+   0) If no expansions took place (or, if the only change in
+      the text was the de-slashifying of the history expansion
+      character)
+   1) If expansions did take place
+   2) If the `p' modifier was given and the caller should print the result
+
+  If an error ocurred in expansion, then OUTPUT contains a descriptive
+  error message. */
+
+#define ADD_STRING(s) \
+       do \
+         { \
+           int sl = strlen (s); \
+           j += sl; \
+           if (j >= result_len) \
+             { \
+               while (j >= result_len) \
+                 result_len += 128; \
+               result = (char *)xrealloc (result, result_len); \
+             } \
+           strcpy (result + j - sl, s); \
+         } \
+       while (0)
+
+#define ADD_CHAR(c) \
+       do \
+         { \
+           if (j >= result_len - 1) \
+             result = (char *)xrealloc (result, result_len += 64); \
+           result[j++] = c; \
+           result[j] = '\0'; \
+         } \
+       while (0)
+
+int
+history_expand (hstring, output)
+     char *hstring;
+     char **output;
+{
+  register int j;
+  int i, r, l, passc, cc, modified, eindex, only_printing, dquote;
+  char *string;
+
+  /* The output string, and its length. */
+  int result_len;
+  char *result;
+
+#if defined (HANDLE_MULTIBYTE)
+  char mb[MB_LEN_MAX];
+  mbstate_t ps;
+#endif
+
+  /* Used when adding the string. */
+  char *temp;
+
+  if (output == 0)
+    return 0;
+
+  /* Setting the history expansion character to 0 inhibits all
+     history expansion. */
+  if (history_expansion_char == 0)
+    {
+      *output = savestring (hstring);
+      return (0);
+    }
+    
+  /* Prepare the buffer for printing error messages. */
+  result = (char *)xmalloc (result_len = 256);
+  result[0] = '\0';
+
+  only_printing = modified = 0;
+  l = strlen (hstring);
+
+  /* Grovel the string.  Only backslash and single quotes can quote the
+     history escape character.  We also handle arg specifiers. */
+
+  /* Before we grovel forever, see if the history_expansion_char appears
+     anywhere within the text. */
+
+  /* The quick substitution character is a history expansion all right.  That
+     is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
+     that is the substitution that we do. */
+  if (hstring[0] == history_subst_char)
+    {
+      string = (char *)xmalloc (l + 5);
+
+      string[0] = string[1] = history_expansion_char;
+      string[2] = ':';
+      string[3] = 's';
+      strcpy (string + 4, hstring);
+      l += 4;
+    }
+  else
+    {
+#if defined (HANDLE_MULTIBYTE)
+      memset (&ps, 0, sizeof (mbstate_t));
+#endif
+
+      string = hstring;
+      /* If not quick substitution, still maybe have to do expansion. */
+
+      /* `!' followed by one of the characters in history_no_expand_chars
+        is NOT an expansion. */
+      for (i = dquote = 0; string[i]; i++)
+       {
+#if defined (HANDLE_MULTIBYTE)
+         if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+           {
+             int v;
+             v = _rl_get_char_len (string + i, &ps);
+             if (v > 1)
+               {
+                 i += v - 1;
+                 continue;
+               }
+           }
+#endif /* HANDLE_MULTIBYTE */
+
+         cc = string[i + 1];
+         /* The history_comment_char, if set, appearing at the beginning
+            of a word signifies that the rest of the line should not have
+            history expansion performed on it.
+            Skip the rest of the line and break out of the loop. */
+         if (history_comment_char && string[i] == history_comment_char &&
+             (i == 0 || member (string[i - 1], history_word_delimiters)))
+           {
+             while (string[i])
+               i++;
+             break;
+           }
+         else if (string[i] == history_expansion_char)
+           {
+             if (!cc || member (cc, history_no_expand_chars))
+               continue;
+             /* If the calling application has set
+                history_inhibit_expansion_function to a function that checks
+                for special cases that should not be history expanded,
+                call the function and skip the expansion if it returns a
+                non-zero value. */
+             else if (history_inhibit_expansion_function &&
+                       (*history_inhibit_expansion_function) (string, i))
+               continue;
+             else
+               break;
+           }
+         /* Shell-like quoting: allow backslashes to quote double quotes
+            inside a double-quoted string. */
+         else if (dquote && string[i] == '\\' && cc == '"')
+           i++;
+         /* More shell-like quoting:  if we're paying attention to single
+            quotes and letting them quote the history expansion character,
+            then we need to pay attention to double quotes, because single
+            quotes are not special inside double-quoted strings. */
+         else if (history_quotes_inhibit_expansion && string[i] == '"')
+           {
+             dquote = 1 - dquote;
+           }
+         else if (dquote == 0 && history_quotes_inhibit_expansion && string[i] == '\'')
+           {
+             /* If this is bash, single quotes inhibit history expansion. */
+             i++;
+             hist_string_extract_single_quoted (string, &i);
+           }
+         else if (history_quotes_inhibit_expansion && string[i] == '\\')
+           {
+             /* If this is bash, allow backslashes to quote single
+                quotes and the history expansion character. */
+             if (cc == '\'' || cc == history_expansion_char)
+               i++;
+           }
+         
+       }
+         
+      if (string[i] != history_expansion_char)
+       {
+         free (result);
+         *output = savestring (string);
+         return (0);
+       }
+    }
+
+  /* Extract and perform the substitution. */
+  for (passc = dquote = i = j = 0; i < l; i++)
+    {
+      int tchar = string[i];
+
+      if (passc)
+       {
+         passc = 0;
+         ADD_CHAR (tchar);
+         continue;
+       }
+
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         int k, c;
+
+         c = tchar;
+         memset (mb, 0, sizeof (mb));
+         for (k = 0; k < MB_LEN_MAX; k++)
+           {
+             mb[k] = (char)c;
+             memset (&ps, 0, sizeof (mbstate_t));
+             if (_rl_get_char_len (mb, &ps) == -2)
+               c = string[++i];
+             else
+               break;
+           }
+         if (strlen (mb) > 1)
+           {
+             ADD_STRING (mb);
+             break;
+           }
+       }
+#endif /* HANDLE_MULTIBYTE */
+
+      if (tchar == history_expansion_char)
+       tchar = -3;
+      else if (tchar == history_comment_char)
+       tchar = -2;
+
+      switch (tchar)
+       {
+       default:
+         ADD_CHAR (string[i]);
+         break;
+
+       case '\\':
+         passc++;
+         ADD_CHAR (tchar);
+         break;
+
+       case '"':
+         dquote = 1 - dquote;
+         ADD_CHAR (tchar);
+         break;
+         
+       case '\'':
+         {
+           /* If history_quotes_inhibit_expansion is set, single quotes
+              inhibit history expansion. */
+           if (dquote == 0 && history_quotes_inhibit_expansion)
+             {
+               int quote, slen;
+
+               quote = i++;
+               hist_string_extract_single_quoted (string, &i);
+
+               slen = i - quote + 2;
+               temp = (char *)xmalloc (slen);
+               strncpy (temp, string + quote, slen);
+               temp[slen - 1] = '\0';
+               ADD_STRING (temp);
+               free (temp);
+             }
+           else
+             ADD_CHAR (string[i]);
+           break;
+         }
+
+       case -2:                /* history_comment_char */
+         if (i == 0 || member (string[i - 1], history_word_delimiters))
+           {
+             temp = (char *)xmalloc (l - i + 1);
+             strcpy (temp, string + i);
+             ADD_STRING (temp);
+             free (temp);
+             i = l;
+           }
+         else
+           ADD_CHAR (string[i]);
+         break;
+
+       case -3:                /* history_expansion_char */
+         cc = string[i + 1];
+
+         /* If the history_expansion_char is followed by one of the
+            characters in history_no_expand_chars, then it is not a
+            candidate for expansion of any kind. */
+         if (member (cc, history_no_expand_chars))
+           {
+             ADD_CHAR (string[i]);
+             break;
+           }
+
+#if defined (NO_BANG_HASH_MODIFIERS)
+         /* There is something that is listed as a `word specifier' in csh
+            documentation which means `the expanded text to this point'.
+            That is not a word specifier, it is an event specifier.  If we
+            don't want to allow modifiers with `!#', just stick the current
+            output line in again. */
+         if (cc == '#')
+           {
+             if (result)
+               {
+                 temp = (char *)xmalloc (1 + strlen (result));
+                 strcpy (temp, result);
+                 ADD_STRING (temp);
+                 free (temp);
+               }
+             i++;
+             break;
+           }
+#endif
+
+         r = history_expand_internal (string, i, &eindex, &temp, result);
+         if (r < 0)
+           {
+             *output = temp;
+             free (result);
+             if (string != hstring)
+               free (string);
+             return -1;
+           }
+         else
+           {
+             if (temp)
+               {
+                 modified++;
+                 if (*temp)
+                   ADD_STRING (temp);
+                 free (temp);
+               }
+             only_printing = r == 1;
+             i = eindex;
+           }
+         break;
+       }
+    }
+
+  *output = result;
+  if (string != hstring)
+    free (string);
+
+  if (only_printing)
+    {
+#if 0
+      add_history (result);
+#endif
+      return (2);
+    }
+
+  return (modified != 0);
+}
+
+/* Return a consed string which is the word specified in SPEC, and found
+   in FROM.  NULL is returned if there is no spec.  The address of
+   ERROR_POINTER is returned if the word specified cannot be found.
+   CALLER_INDEX is the offset in SPEC to start looking; it is updated
+   to point to just after the last character parsed. */
+static char *
+get_history_word_specifier (spec, from, caller_index)
+     char *spec, *from;
+     int *caller_index;
+{
+  register int i = *caller_index;
+  int first, last;
+  int expecting_word_spec = 0;
+  char *result;
+
+  /* The range of words to return doesn't exist yet. */
+  first = last = 0;
+  result = (char *)NULL;
+
+  /* If we found a colon, then this *must* be a word specification.  If
+     it isn't, then it is an error. */
+  if (spec[i] == ':')
+    {
+      i++;
+      expecting_word_spec++;
+    }
+
+  /* Handle special cases first. */
+
+  /* `%' is the word last searched for. */
+  if (spec[i] == '%')
+    {
+      *caller_index = i + 1;
+      return (search_match ? savestring (search_match) : savestring (""));
+    }
+
+  /* `*' matches all of the arguments, but not the command. */
+  if (spec[i] == '*')
+    {
+      *caller_index = i + 1;
+      result = history_arg_extract (1, '$', from);
+      return (result ? result : savestring (""));
+    }
+
+  /* `$' is last arg. */
+  if (spec[i] == '$')
+    {
+      *caller_index = i + 1;
+      return (history_arg_extract ('$', '$', from));
+    }
+
+  /* Try to get FIRST and LAST figured out. */
+
+  if (spec[i] == '-')
+    first = 0;
+  else if (spec[i] == '^')
+    {
+      first = 1;
+      i++;
+    }
+  else if (_rl_digit_p (spec[i]) && expecting_word_spec)
+    {
+      for (first = 0; _rl_digit_p (spec[i]); i++)
+       first = (first * 10) + _rl_digit_value (spec[i]);
+    }
+  else
+    return ((char *)NULL);     /* no valid `first' for word specifier */
+
+  if (spec[i] == '^' || spec[i] == '*')
+    {
+      last = (spec[i] == '^') ? 1 : '$';       /* x* abbreviates x-$ */
+      i++;
+    }
+  else if (spec[i] != '-')
+    last = first;
+  else
+    {
+      i++;
+
+      if (_rl_digit_p (spec[i]))
+       {
+         for (last = 0; _rl_digit_p (spec[i]); i++)
+           last = (last * 10) + _rl_digit_value (spec[i]);
+       }
+      else if (spec[i] == '$')
+       {
+         i++;
+         last = '$';
+       }
+#if 0
+      else if (!spec[i] || spec[i] == ':')
+       /* check against `:' because there could be a modifier separator */
+#else
+      else
+       /* csh seems to allow anything to terminate the word spec here,
+          leaving it as an abbreviation. */
+#endif
+       last = -1;              /* x- abbreviates x-$ omitting word `$' */
+    }
+
+  *caller_index = i;
+
+  if (last >= first || last == '$' || last < 0)
+    result = history_arg_extract (first, last, from);
+
+  return (result ? result : (char *)&error_pointer);
+}
+
+/* Extract the args specified, starting at FIRST, and ending at LAST.
+   The args are taken from STRING.  If either FIRST or LAST is < 0,
+   then make that arg count from the right (subtract from the number of
+   tokens, so that FIRST = -1 means the next to last token on the line).
+   If LAST is `$' the last arg from STRING is used. */
+char *
+history_arg_extract (first, last, string)
+     int first, last;
+     const char *string;
+{
+  register int i, len;
+  char *result;
+  int size, offset;
+  char **list;
+
+  /* XXX - think about making history_tokenize return a struct array,
+     each struct in array being a string and a length to avoid the
+     calls to strlen below. */
+  if ((list = history_tokenize (string)) == NULL)
+    return ((char *)NULL);
+
+  for (len = 0; list[len]; len++)
+    ;
+
+  if (last < 0)
+    last = len + last - 1;
+
+  if (first < 0)
+    first = len + first - 1;
+
+  if (last == '$')
+    last = len - 1;
+
+  if (first == '$')
+    first = len - 1;
+
+  last++;
+
+  if (first >= len || last > len || first < 0 || last < 0 || first > last)
+    result = ((char *)NULL);
+  else
+    {
+      for (size = 0, i = first; i < last; i++)
+       size += strlen (list[i]) + 1;
+      result = (char *)xmalloc (size + 1);
+      result[0] = '\0';
+
+      for (i = first, offset = 0; i < last; i++)
+       {
+         strcpy (result + offset, list[i]);
+         offset += strlen (list[i]);
+         if (i + 1 < last)
+           {
+             result[offset++] = ' ';
+             result[offset] = 0;
+           }
+       }
+    }
+
+  for (i = 0; i < len; i++)
+    free (list[i]);
+  free (list);
+
+  return (result);
+}
+
+static int
+history_tokenize_word (string, ind)
+     const char *string;
+     int ind;
+{
+  register int i;
+  int delimiter;
+
+  i = ind;
+  delimiter = 0;
+
+  if (member (string[i], "()\n"))
+    {
+      i++;
+      return i;
+    }
+
+  if (member (string[i], "<>;&|$"))
+    {
+      int peek = string[i + 1];
+
+      if (peek == string[i] && peek != '$')
+       {
+         if (peek == '<' && string[i + 2] == '-')
+           i++;
+         i += 2;
+         return i;
+       }
+      else
+       {
+         if ((peek == '&' && (string[i] == '>' || string[i] == '<')) ||
+             (peek == '>' && string[i] == '&') ||
+             (peek == '(' && (string[i] == '>' || string[i] == '<')) || /* ) */
+             (peek == '(' && string[i] == '$')) /* ) */
+           {
+             i += 2;
+             return i;
+           }
+       }
+
+      if (string[i] != '$')
+       {
+         i++;
+         return i;
+       }
+    }
+
+  /* Get word from string + i; */
+
+  if (member (string[i], HISTORY_QUOTE_CHARACTERS))
+    delimiter = string[i++];
+
+  for (; string[i]; i++)
+    {
+      if (string[i] == '\\' && string[i + 1] == '\n')
+       {
+         i++;
+         continue;
+       }
+
+      if (string[i] == '\\' && delimiter != '\'' &&
+         (delimiter != '"' || member (string[i], slashify_in_quotes)))
+       {
+         i++;
+         continue;
+       }
+
+      if (delimiter && string[i] == delimiter)
+       {
+         delimiter = 0;
+         continue;
+       }
+
+      if (!delimiter && (member (string[i], history_word_delimiters)))
+       break;
+
+      if (!delimiter && member (string[i], HISTORY_QUOTE_CHARACTERS))
+       delimiter = string[i];
+    }
+
+  return i;
+}
+
+static char *
+history_substring (string, start, end)
+     const char *string;
+     int start, end;
+{
+  register int len;
+  register char *result;
+
+  len = end - start;
+  result = (char *)xmalloc (len + 1);
+  strncpy (result, string + start, len);
+  result[len] = '\0';
+  return result;
+}
+
+/* Parse STRING into tokens and return an array of strings.  If WIND is
+   not -1 and INDP is not null, we also want the word surrounding index
+   WIND.  The position in the returned array of strings is returned in
+   *INDP. */
+static char **
+history_tokenize_internal (string, wind, indp)
+     const char *string;
+     int wind, *indp;
+{
+  char **result;
+  register int i, start, result_index, size;
+
+  /* If we're searching for a string that's not part of a word (e.g., " "),
+     make sure we set *INDP to a reasonable value. */
+  if (indp && wind != -1)
+    *indp = -1;
+
+  /* Get a token, and stuff it into RESULT.  The tokens are split
+     exactly where the shell would split them. */
+  for (i = result_index = size = 0, result = (char **)NULL; string[i]; )
+    {
+      /* Skip leading whitespace. */
+      for (; string[i] && whitespace (string[i]); i++)
+       ;
+      if (string[i] == 0 || string[i] == history_comment_char)
+       return (result);
+
+      start = i;
+
+      i = history_tokenize_word (string, start);
+
+      /* If we have a non-whitespace delimiter character (which would not be
+        skipped by the loop above), use it and any adjacent delimiters to
+        make a separate field.  Any adjacent white space will be skipped the
+        next time through the loop. */
+      if (i == start && history_word_delimiters)
+       {
+         i++;
+         while (string[i] && member (string[i], history_word_delimiters))
+           i++;
+       }
+
+      /* If we are looking for the word in which the character at a
+        particular index falls, remember it. */
+      if (indp && wind != -1 && wind >= start && wind < i)
+        *indp = result_index;
+
+      if (result_index + 2 >= size)
+       result = (char **)xrealloc (result, ((size += 10) * sizeof (char *)));
+
+      result[result_index++] = history_substring (string, start, i);
+      result[result_index] = (char *)NULL;
+    }
+
+  return (result);
+}
+
+/* Return an array of tokens, much as the shell might.  The tokens are
+   parsed out of STRING. */
+char **
+history_tokenize (string)
+     const char *string;
+{
+  return (history_tokenize_internal (string, -1, (int *)NULL));
+}
+
+/* Find and return the word which contains the character at index IND
+   in the history line LINE.  Used to save the word matched by the
+   last history !?string? search. */
+static char *
+history_find_word (line, ind)
+     char *line;
+     int ind;
+{
+  char **words, *s;
+  int i, wind;
+
+  words = history_tokenize_internal (line, ind, &wind);
+  if (wind == -1 || words == 0)
+    return ((char *)NULL);
+  s = words[wind];
+  for (i = 0; i < wind; i++)
+    free (words[i]);
+  for (i = wind + 1; words[i]; i++)
+    free (words[i]);
+  free (words);
+  return s;
+}
index 9cdd87c..d3628be 100644 (file)
@@ -97,7 +97,7 @@ extern int _rl_read_mbstring PARAMS((int, char *, int));
 
 extern int _rl_is_mbchar_matched PARAMS((char *, int, int, char *, int));
 
-extern int _rl_char_value PARAMS((char *, int));
+extern wchar_t _rl_char_value PARAMS((char *, int));
 extern int _rl_walphabetic PARAMS((wchar_t));
 
 #define MB_NEXTCHAR(b,s,c,f) \
index af33580..6d8ccfd 100644 (file)
@@ -1124,6 +1124,10 @@ rl_delete_horizontal_space (count, ignore)
       rl_delete_text (start, rl_point);
       rl_point = start;
     }
+
+  if (rl_point < 0)
+    rl_point = 0;
+
   return 0;
 }
 
diff --git a/lib/readline/text.c~ b/lib/readline/text.c~
new file mode 100644 (file)
index 0000000..af33580
--- /dev/null
@@ -0,0 +1,1558 @@
+/* text.c -- text handling commands for readline. */
+
+/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
+
+   This file is part of the GNU Readline Library, a library for
+   reading lines of text with interactive input and history editing.
+
+   The GNU Readline Library 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 2, or
+   (at your option) any later version.
+
+   The GNU Readline Library 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.
+
+   The GNU General Public License is often shipped with GNU software, and
+   is generally kept in a file called COPYING or LICENSE.  If you do not
+   have a copy of the license, write to the Free Software Foundation,
+   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+#define READLINE_LIBRARY
+
+#if defined (HAVE_CONFIG_H)
+#  include <config.h>
+#endif
+
+#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 */
+
+#if defined (HAVE_LOCALE_H)
+#  include <locale.h>
+#endif
+
+#include <stdio.h>
+
+/* System-specific feature definitions and include files. */
+#include "rldefs.h"
+#include "rlmbutil.h"
+
+#if defined (__EMX__)
+#  define INCL_DOSPROCESS
+#  include <os2.h>
+#endif /* __EMX__ */
+
+/* Some standard library routines. */
+#include "readline.h"
+#include "history.h"
+
+#include "rlprivate.h"
+#include "rlshell.h"
+#include "xmalloc.h"
+
+/* Forward declarations. */
+static int rl_change_case PARAMS((int, int));
+static int _rl_char_search PARAMS((int, int, int));
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Insert and Delete                           */
+/*                                                                 */
+/* **************************************************************** */
+
+/* Insert a string of text into the line at point.  This is the only
+   way that you should do insertion.  _rl_insert_char () calls this
+   function.  Returns the number of characters inserted. */
+int
+rl_insert_text (string)
+     const char *string;
+{
+  register int i, l;
+
+  l = (string && *string) ? strlen (string) : 0;
+  if (l == 0)
+    return 0;
+
+  if (rl_end + l >= rl_line_buffer_len)
+    rl_extend_line_buffer (rl_end + l);
+
+  for (i = rl_end; i >= rl_point; i--)
+    rl_line_buffer[i + l] = rl_line_buffer[i];
+  strncpy (rl_line_buffer + rl_point, string, l);
+
+  /* Remember how to undo this if we aren't undoing something. */
+  if (_rl_doing_an_undo == 0)
+    {
+      /* If possible and desirable, concatenate the undos. */
+      if ((l == 1) &&
+         rl_undo_list &&
+         (rl_undo_list->what == UNDO_INSERT) &&
+         (rl_undo_list->end == rl_point) &&
+         (rl_undo_list->end - rl_undo_list->start < 20))
+       rl_undo_list->end++;
+      else
+       rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
+    }
+  rl_point += l;
+  rl_end += l;
+  rl_line_buffer[rl_end] = '\0';
+  return l;
+}
+
+/* Delete the string between FROM and TO.  FROM is inclusive, TO is not.
+   Returns the number of characters deleted. */
+int
+rl_delete_text (from, to)
+     int from, to;
+{
+  register char *text;
+  register int diff, i;
+
+  /* Fix it if the caller is confused. */
+  if (from > to)
+    SWAP (from, to);
+
+  /* fix boundaries */
+  if (to > rl_end)
+    {
+      to = rl_end;
+      if (from > to)
+       from = to;
+    }
+  if (from < 0)
+    from = 0;
+
+  text = rl_copy_text (from, to);
+
+  /* Some versions of strncpy() can't handle overlapping arguments. */
+  diff = to - from;
+  for (i = from; i < rl_end - diff; i++)
+    rl_line_buffer[i] = rl_line_buffer[i + diff];
+
+  /* Remember how to undo this delete. */
+  if (_rl_doing_an_undo == 0)
+    rl_add_undo (UNDO_DELETE, from, to, text);
+  else
+    free (text);
+
+  rl_end -= diff;
+  rl_line_buffer[rl_end] = '\0';
+  return (diff);
+}
+
+/* Fix up point so that it is within the line boundaries after killing
+   text.  If FIX_MARK_TOO is non-zero, the mark is forced within line
+   boundaries also. */
+
+#define _RL_FIX_POINT(x) \
+       do { \
+       if (x > rl_end) \
+         x = rl_end; \
+       else if (x < 0) \
+         x = 0; \
+       } while (0)
+
+void
+_rl_fix_point (fix_mark_too)
+     int fix_mark_too;
+{
+  _RL_FIX_POINT (rl_point);
+  if (fix_mark_too)
+    _RL_FIX_POINT (rl_mark);
+}
+#undef _RL_FIX_POINT
+
+/* Replace the contents of the line buffer between START and END with
+   TEXT.  The operation is undoable.  To replace the entire line in an
+   undoable mode, use _rl_replace_text(text, 0, rl_end); */
+int
+_rl_replace_text (text, start, end)
+     const char *text;
+     int start, end;
+{
+  int n;
+
+  rl_begin_undo_group ();
+  rl_delete_text (start, end + 1);
+  rl_point = start;
+  n = rl_insert_text (text);
+  rl_end_undo_group ();
+
+  return n;
+}
+
+/* Replace the current line buffer contents with TEXT.  If CLEAR_UNDO is
+   non-zero, we free the current undo list. */
+void
+rl_replace_line (text, clear_undo)
+     const char *text;
+     int clear_undo;
+{
+  int len;
+
+  len = strlen (text);
+  if (len >= rl_line_buffer_len)
+    rl_extend_line_buffer (len);
+  strcpy (rl_line_buffer, text);
+  rl_end = len;
+
+  if (clear_undo)
+    rl_free_undo_list ();
+
+  _rl_fix_point (1);
+}
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Readline character functions                */
+/*                                                                 */
+/* **************************************************************** */
+
+/* This is not a gap editor, just a stupid line input routine.  No hair
+   is involved in writing any of the functions, and none should be. */
+
+/* Note that:
+
+   rl_end is the place in the string that we would place '\0';
+   i.e., it is always safe to place '\0' there.
+
+   rl_point is the place in the string where the cursor is.  Sometimes
+   this is the same as rl_end.
+
+   Any command that is called interactively receives two arguments.
+   The first is a count: the numeric arg pased to this command.
+   The second is the key which invoked this command.
+*/
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Movement Commands                           */
+/*                                                                 */
+/* **************************************************************** */
+
+/* Note that if you `optimize' the display for these functions, you cannot
+   use said functions in other functions which do not do optimizing display.
+   I.e., you will have to update the data base for rl_redisplay, and you
+   might as well let rl_redisplay do that job. */
+
+/* Move forward COUNT bytes. */
+int
+rl_forward_byte (count, key)
+     int count, key;
+{
+  if (count < 0)
+    return (rl_backward_byte (-count, key));
+
+  if (count > 0)
+    {
+      int end = rl_point + count;
+#if defined (VI_MODE)
+      int lend = rl_end > 0 ? rl_end - (rl_editing_mode == vi_mode) : rl_end;
+#else
+      int lend = rl_end;
+#endif
+
+      if (end > lend)
+       {
+         rl_point = lend;
+         rl_ding ();
+       }
+      else
+       rl_point = end;
+    }
+
+  if (rl_end < 0)
+    rl_end = 0;
+
+  return 0;
+}
+
+#if defined (HANDLE_MULTIBYTE)
+/* Move forward COUNT characters. */
+int
+rl_forward_char (count, key)
+     int count, key;
+{
+  int point;
+
+  if (MB_CUR_MAX == 1 || rl_byte_oriented)
+    return (rl_forward_byte (count, key));
+
+  if (count < 0)
+    return (rl_backward_char (-count, key));
+
+  if (count > 0)
+    {
+      point = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
+
+#if defined (VI_MODE)
+      if (rl_end <= point && rl_editing_mode == vi_mode)
+       point = _rl_find_prev_mbchar (rl_line_buffer, rl_end, MB_FIND_NONZERO);
+#endif
+
+      if (rl_point == point)
+       rl_ding ();
+
+      rl_point = point;
+
+      if (rl_end < 0)
+       rl_end = 0;
+    }
+
+  return 0;
+}
+#else /* !HANDLE_MULTIBYTE */
+int
+rl_forward_char (count, key)
+     int count, key;
+{
+  return (rl_forward_byte (count, key));
+}
+#endif /* !HANDLE_MULTIBYTE */
+  
+/* Backwards compatibility. */
+int
+rl_forward (count, key)
+     int count, key;
+{
+  return (rl_forward_char (count, key));
+}
+
+/* Move backward COUNT bytes. */
+int
+rl_backward_byte (count, key)
+     int count, key;
+{
+  if (count < 0)
+    return (rl_forward_byte (-count, key));
+
+  if (count > 0)
+    {
+      if (rl_point < count)
+       {
+         rl_point = 0;
+         rl_ding ();
+       }
+      else
+       rl_point -= count;
+    }
+
+  if (rl_point < 0)
+    rl_point = 0;
+
+  return 0;
+}
+
+#if defined (HANDLE_MULTIBYTE)
+/* Move backward COUNT characters. */
+int
+rl_backward_char (count, key)
+     int count, key;
+{
+  int point;
+
+  if (MB_CUR_MAX == 1 || rl_byte_oriented)
+    return (rl_backward_byte (count, key));
+
+  if (count < 0)
+    return (rl_forward_char (-count, key));
+
+  if (count > 0)
+    {
+      point = rl_point;
+
+      while (count > 0 && point > 0)
+       {
+         point = _rl_find_prev_mbchar (rl_line_buffer, point, MB_FIND_NONZERO);
+         count--;
+       }
+      if (count > 0)
+       {
+         rl_point = 0;
+         rl_ding ();
+       }
+      else
+        rl_point = point;
+    }
+
+  return 0;
+}
+#else
+int
+rl_backward_char (count, key)
+     int count, key;
+{
+  return (rl_backward_byte (count, key));
+}
+#endif
+
+/* Backwards compatibility. */
+int
+rl_backward (count, key)
+     int count, key;
+{
+  return (rl_backward_char (count, key));
+}
+
+/* Move to the beginning of the line. */
+int
+rl_beg_of_line (count, key)
+     int count, key;
+{
+  rl_point = 0;
+  return 0;
+}
+
+/* Move to the end of the line. */
+int
+rl_end_of_line (count, key)
+     int count, key;
+{
+  rl_point = rl_end;
+  return 0;
+}
+
+/* Move forward a word.  We do what Emacs does.  Handles multibyte chars. */
+int
+rl_forward_word (count, key)
+     int count, key;
+{
+  int c;
+
+  if (count < 0)
+    return (rl_backward_word (-count, key));
+
+  while (count)
+    {
+      if (rl_point == rl_end)
+       return 0;
+
+      /* If we are not in a word, move forward until we are in one.
+        Then, move forward until we hit a non-alphabetic character. */
+      c = _rl_char_value (rl_line_buffer, rl_point);
+
+      if (_rl_walphabetic (c) == 0)
+       {
+         rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
+         while (rl_point < rl_end)
+           {
+             c = _rl_char_value (rl_line_buffer, rl_point);
+             if (_rl_walphabetic (c))
+               break;
+             rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
+           }
+       }
+
+      if (rl_point == rl_end)
+       return 0;
+
+      rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
+      while (rl_point < rl_end)
+       {
+         c = _rl_char_value (rl_line_buffer, rl_point);
+         if (_rl_walphabetic (c) == 0)
+           break;
+         rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
+       }
+
+      --count;
+    }
+
+  return 0;
+}
+
+/* Move backward a word.  We do what Emacs does.  Handles multibyte chars. */
+int
+rl_backward_word (count, key)
+     int count, key;
+{
+  int c, p;
+
+  if (count < 0)
+    return (rl_forward_word (-count, key));
+
+  while (count)
+    {
+      if (rl_point == 0)
+       return 0;
+
+      /* Like rl_forward_word (), except that we look at the characters
+        just before point. */
+
+      p = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
+      c = _rl_char_value (rl_line_buffer, p);
+
+      if (_rl_walphabetic (c) == 0)
+       {
+         rl_point = p;
+         while (rl_point > 0)
+           {
+             p = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
+             c = _rl_char_value (rl_line_buffer, p);
+             if (_rl_walphabetic (c))
+               break;
+             rl_point = p;
+           }
+       }
+
+      while (rl_point)
+       {
+         p = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
+         c = _rl_char_value (rl_line_buffer, p);         
+         if (_rl_walphabetic (c) == 0)
+           break;
+         else
+           rl_point = p;
+       }
+
+      --count;
+    }
+
+  return 0;
+}
+
+/* Clear the current line.  Numeric argument to C-l does this. */
+int
+rl_refresh_line (ignore1, ignore2)
+     int ignore1, ignore2;
+{
+  int curr_line;
+
+  curr_line = _rl_current_display_line ();
+
+  _rl_move_vert (curr_line);
+  _rl_move_cursor_relative (0, rl_line_buffer);   /* XXX is this right */
+
+  _rl_clear_to_eol (0);                /* arg of 0 means to not use spaces */
+
+  rl_forced_update_display ();
+  rl_display_fixed = 1;
+
+  return 0;
+}
+
+/* C-l typed to a line without quoting clears the screen, and then reprints
+   the prompt and the current input line.  Given a numeric arg, redraw only
+   the current line. */
+int
+rl_clear_screen (count, key)
+     int count, key;
+{
+  if (rl_explicit_arg)
+    {
+      rl_refresh_line (count, key);
+      return 0;
+    }
+
+  _rl_clear_screen ();         /* calls termcap function to clear screen */
+  rl_forced_update_display ();
+  rl_display_fixed = 1;
+
+  return 0;
+}
+
+int
+rl_arrow_keys (count, c)
+     int count, c;
+{
+  int ch;
+
+  RL_SETSTATE(RL_STATE_MOREINPUT);
+  ch = rl_read_key ();
+  RL_UNSETSTATE(RL_STATE_MOREINPUT);
+
+  switch (_rl_to_upper (ch))
+    {
+    case 'A':
+      rl_get_previous_history (count, ch);
+      break;
+
+    case 'B':
+      rl_get_next_history (count, ch);
+      break;
+
+    case 'C':
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       rl_forward_char (count, ch);
+      else
+       rl_forward_byte (count, ch);
+      break;
+
+    case 'D':
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       rl_backward_char (count, ch);
+      else
+       rl_backward_byte (count, ch);
+      break;
+
+    default:
+      rl_ding ();
+    }
+
+  return 0;
+}
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Text commands                               */
+/*                                                                 */
+/* **************************************************************** */
+
+#ifdef HANDLE_MULTIBYTE
+static char pending_bytes[MB_LEN_MAX];
+static int pending_bytes_length = 0;
+static mbstate_t ps = {0};
+#endif
+
+/* Insert the character C at the current location, moving point forward.
+   If C introduces a multibyte sequence, we read the whole sequence and
+   then insert the multibyte char into the line buffer. */
+int
+_rl_insert_char (count, c)
+     int count, c;
+{
+  register int i;
+  char *string;
+#ifdef HANDLE_MULTIBYTE
+  int string_size;
+  char incoming[MB_LEN_MAX + 1];
+  int incoming_length = 0;
+  mbstate_t ps_back;
+  static int stored_count = 0;
+#endif
+
+  if (count <= 0)
+    return 0;
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX == 1 || rl_byte_oriented)
+    {
+      incoming[0] = c;
+      incoming[1] = '\0';
+      incoming_length = 1;
+    }
+  else
+    {
+      wchar_t wc;
+      size_t ret;
+
+      if (stored_count <= 0)
+       stored_count = count;
+      else
+       count = stored_count;
+
+      ps_back = ps;
+      pending_bytes[pending_bytes_length++] = c;
+      ret = mbrtowc (&wc, pending_bytes, pending_bytes_length, &ps);
+
+      if (ret == (size_t)-2)
+       {
+         /* Bytes too short to compose character, try to wait for next byte.
+            Restore the state of the byte sequence, because in this case the
+            effect of mbstate is undefined. */
+         ps = ps_back;
+         return 1;
+       }
+      else if (ret == (size_t)-1)
+       {
+         /* Invalid byte sequence for the current locale.  Treat first byte
+            as a single character. */
+         incoming[0] = pending_bytes[0];
+         incoming[1] = '\0';
+         incoming_length = 1;
+         pending_bytes_length--;
+         memmove (pending_bytes, pending_bytes + 1, pending_bytes_length);
+         /* Clear the state of the byte sequence, because in this case the
+            effect of mbstate is undefined. */
+         memset (&ps, 0, sizeof (mbstate_t));
+       }
+      else if (ret == (size_t)0)
+       {
+         incoming[0] = '\0';
+         incoming_length = 0;
+         pending_bytes_length--;
+         /* Clear the state of the byte sequence, because in this case the
+            effect of mbstate is undefined. */
+         memset (&ps, 0, sizeof (mbstate_t));
+       }
+      else
+       {
+         /* We successfully read a single multibyte character. */
+         memcpy (incoming, pending_bytes, pending_bytes_length);
+         incoming[pending_bytes_length] = '\0';
+         incoming_length = pending_bytes_length;
+         pending_bytes_length = 0;
+       }
+    }
+#endif /* HANDLE_MULTIBYTE */
+         
+  /* If we can optimize, then do it.  But don't let people crash
+     readline because of extra large arguments. */
+  if (count > 1 && count <= 1024)
+    {
+#if defined (HANDLE_MULTIBYTE)
+      string_size = count * incoming_length;
+      string = (char *)xmalloc (1 + string_size);
+
+      i = 0;
+      while (i < string_size)
+       {
+         strncpy (string + i, incoming, incoming_length);
+         i += incoming_length;
+       }
+      incoming_length = 0;
+      stored_count = 0;
+#else /* !HANDLE_MULTIBYTE */
+      string = (char *)xmalloc (1 + count);
+
+      for (i = 0; i < count; i++)
+       string[i] = c;
+#endif /* !HANDLE_MULTIBYTE */
+
+      string[i] = '\0';
+      rl_insert_text (string);
+      free (string);
+
+      return 0;
+    }
+
+  if (count > 1024)
+    {
+      int decreaser;
+#if defined (HANDLE_MULTIBYTE)
+      string_size = incoming_length * 1024;
+      string = (char *)xmalloc (1 + string_size);
+
+      i = 0;
+      while (i < string_size)
+       {
+         strncpy (string + i, incoming, incoming_length);
+         i += incoming_length;
+       }
+
+      while (count)
+       {
+         decreaser = (count > 1024) ? 1024 : count;
+         string[decreaser*incoming_length] = '\0';
+         rl_insert_text (string);
+         count -= decreaser;
+       }
+
+      free (string);
+      incoming_length = 0;
+      stored_count = 0;
+#else /* !HANDLE_MULTIBYTE */
+      char str[1024+1];
+
+      for (i = 0; i < 1024; i++)
+       str[i] = c;
+
+      while (count)
+       {
+         decreaser = (count > 1024 ? 1024 : count);
+         str[decreaser] = '\0';
+         rl_insert_text (str);
+         count -= decreaser;
+       }
+#endif /* !HANDLE_MULTIBYTE */
+
+      return 0;
+    }
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX == 1 || rl_byte_oriented)
+    {
+#endif
+      /* We are inserting a single character.
+        If there is pending input, then make a string of all of the
+        pending characters that are bound to rl_insert, and insert
+        them all. */
+      if (_rl_any_typein ())
+       _rl_insert_typein (c);
+      else
+       {
+         /* Inserting a single character. */
+         char str[2];
+
+         str[1] = '\0';
+         str[0] = c;
+         rl_insert_text (str);
+       }
+#if defined (HANDLE_MULTIBYTE)
+    }
+  else
+    {
+      rl_insert_text (incoming);
+      stored_count = 0;
+    }
+#endif
+
+  return 0;
+}
+
+/* Overwrite the character at point (or next COUNT characters) with C.
+   If C introduces a multibyte character sequence, read the entire sequence
+   before starting the overwrite loop. */
+int
+_rl_overwrite_char (count, c)
+     int count, c;
+{
+  int i;
+#if defined (HANDLE_MULTIBYTE)
+  char mbkey[MB_LEN_MAX];
+  int k;
+
+  /* Read an entire multibyte character sequence to insert COUNT times. */
+  if (count > 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    k = _rl_read_mbstring (c, mbkey, MB_LEN_MAX);
+#endif
+
+  rl_begin_undo_group ();
+
+  for (i = 0; i < count; i++)
+    {
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       rl_insert_text (mbkey);
+      else
+#endif
+       _rl_insert_char (1, c);
+
+      if (rl_point < rl_end)
+       rl_delete (1, c);
+    }
+
+  rl_end_undo_group ();
+
+  return 0;
+}
+
+int
+rl_insert (count, c)
+     int count, c;
+{
+  return (rl_insert_mode == RL_IM_INSERT ? _rl_insert_char (count, c)
+                                        : _rl_overwrite_char (count, c));
+}
+
+/* Insert the next typed character verbatim. */
+int
+rl_quoted_insert (count, key)
+     int count, key;
+{
+  int c;
+
+#if defined (HANDLE_SIGNALS)
+  _rl_disable_tty_signals ();
+#endif
+
+  RL_SETSTATE(RL_STATE_MOREINPUT);
+  c = rl_read_key ();
+  RL_UNSETSTATE(RL_STATE_MOREINPUT);
+
+#if defined (HANDLE_SIGNALS)
+  _rl_restore_tty_signals ();
+#endif
+
+  return (_rl_insert_char (count, c));  
+}
+
+/* Insert a tab character. */
+int
+rl_tab_insert (count, key)
+     int count, key;
+{
+  return (_rl_insert_char (count, '\t'));
+}
+
+/* What to do when a NEWLINE is pressed.  We accept the whole line.
+   KEY is the key that invoked this command.  I guess it could have
+   meaning in the future. */
+int
+rl_newline (count, key)
+     int count, key;
+{
+  rl_done = 1;
+
+  if (_rl_history_preserve_point)
+    _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
+
+  RL_SETSTATE(RL_STATE_DONE);
+
+#if defined (VI_MODE)
+  if (rl_editing_mode == vi_mode)
+    {
+      _rl_vi_done_inserting ();
+      if (_rl_vi_textmod_command (_rl_vi_last_command) == 0)   /* XXX */
+       _rl_vi_reset_last ();
+    }
+#endif /* VI_MODE */
+
+  /* If we've been asked to erase empty lines, suppress the final update,
+     since _rl_update_final calls rl_crlf(). */
+  if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
+    return 0;
+
+  if (readline_echoing_p)
+    _rl_update_final ();
+  return 0;
+}
+
+/* What to do for some uppercase characters, like meta characters,
+   and some characters appearing in emacs_ctlx_keymap.  This function
+   is just a stub, you bind keys to it and the code in _rl_dispatch ()
+   is special cased. */
+int
+rl_do_lowercase_version (ignore1, ignore2)
+     int ignore1, ignore2;
+{
+  return 0;
+}
+
+/* This is different from what vi does, so the code's not shared.  Emacs
+   rubout in overwrite mode has one oddity:  it replaces a control
+   character that's displayed as two characters (^X) with two spaces. */
+int
+_rl_overwrite_rubout (count, key)
+     int count, key;
+{
+  int opoint;
+  int i, l;
+
+  if (rl_point == 0)
+    {
+      rl_ding ();
+      return 1;
+    }
+
+  opoint = rl_point;
+
+  /* L == number of spaces to insert */
+  for (i = l = 0; i < count; i++)
+    {
+      rl_backward_char (1, key);
+      l += rl_character_len (rl_line_buffer[rl_point], rl_point);      /* not exactly right */
+    }
+
+  rl_begin_undo_group ();
+
+  if (count > 1 || rl_explicit_arg)
+    rl_kill_text (opoint, rl_point);
+  else
+    rl_delete_text (opoint, rl_point);
+
+  /* Emacs puts point at the beginning of the sequence of spaces. */
+  if (rl_point < rl_end)
+    {
+      opoint = rl_point;
+      _rl_insert_char (l, ' ');
+      rl_point = opoint;
+    }
+
+  rl_end_undo_group ();
+
+  return 0;
+}
+  
+/* Rubout the character behind point. */
+int
+rl_rubout (count, key)
+     int count, key;
+{
+  if (count < 0)
+    return (rl_delete (-count, key));
+
+  if (!rl_point)
+    {
+      rl_ding ();
+      return -1;
+    }
+
+  if (rl_insert_mode == RL_IM_OVERWRITE)
+    return (_rl_overwrite_rubout (count, key));
+
+  return (_rl_rubout_char (count, key));
+}
+
+int
+_rl_rubout_char (count, key)
+     int count, key;
+{
+  int orig_point;
+  unsigned char c;
+
+  /* Duplicated code because this is called from other parts of the library. */
+  if (count < 0)
+    return (rl_delete (-count, key));
+
+  if (rl_point == 0)
+    {
+      rl_ding ();
+      return -1;
+    }
+
+  if (count > 1 || rl_explicit_arg)
+    {
+      orig_point = rl_point;
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       rl_backward_char (count, key);
+      else
+#endif
+        rl_backward_byte (count, key);
+      rl_kill_text (orig_point, rl_point);
+    }
+  else
+    {
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX == 1 || rl_byte_oriented)
+       {
+#endif
+         c = rl_line_buffer[--rl_point];
+         rl_delete_text (rl_point, rl_point + 1);
+#if defined (HANDLE_MULTIBYTE)
+       }
+      else
+       {
+         int orig_point;
+
+         orig_point = rl_point;
+         rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
+         c = rl_line_buffer[rl_point];
+         rl_delete_text (rl_point, orig_point);
+       }
+#endif /* HANDLE_MULTIBYTE */
+
+      /* I don't think that the hack for end of line is needed for
+        multibyte chars. */
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX == 1 || rl_byte_oriented)
+#endif
+      if (rl_point == rl_end && ISPRINT (c) && _rl_last_c_pos)
+       {
+         int l;
+         l = rl_character_len (c, rl_point);
+         _rl_erase_at_end_of_line (l);
+       }
+    }
+
+  return 0;
+}
+
+/* Delete the character under the cursor.  Given a numeric argument,
+   kill that many characters instead. */
+int
+rl_delete (count, key)
+     int count, key;
+{
+  int r;
+
+  if (count < 0)
+    return (_rl_rubout_char (-count, key));
+
+  if (rl_point == rl_end)
+    {
+      rl_ding ();
+      return -1;
+    }
+
+  if (count > 1 || rl_explicit_arg)
+    {
+      int orig_point = rl_point;
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       rl_forward_char (count, key);
+      else
+#endif
+       rl_forward_byte (count, key);
+
+      r = rl_kill_text (orig_point, rl_point);
+      rl_point = orig_point;
+      return r;
+    }
+  else
+    {
+      int new_point;
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       new_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
+      else
+       new_point = rl_point + 1;
+       
+      return (rl_delete_text (rl_point, new_point));
+    }
+}
+
+/* Delete the character under the cursor, unless the insertion
+   point is at the end of the line, in which case the character
+   behind the cursor is deleted.  COUNT is obeyed and may be used
+   to delete forward or backward that many characters. */      
+int
+rl_rubout_or_delete (count, key)
+     int count, key;
+{
+  if (rl_end != 0 && rl_point == rl_end)
+    return (_rl_rubout_char (count, key));
+  else
+    return (rl_delete (count, key));
+}  
+
+/* Delete all spaces and tabs around point. */
+int
+rl_delete_horizontal_space (count, ignore)
+     int count, ignore;
+{
+  int start = rl_point;
+
+  while (rl_point && whitespace (rl_line_buffer[rl_point - 1]))
+    rl_point--;
+
+  start = rl_point;
+
+  while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
+    rl_point++;
+
+  if (start != rl_point)
+    {
+      rl_delete_text (start, rl_point);
+      rl_point = start;
+    }
+  return 0;
+}
+
+/* Like the tcsh editing function delete-char-or-list.  The eof character
+   is caught before this is invoked, so this really does the same thing as
+   delete-char-or-list-or-eof, as long as it's bound to the eof character. */
+int
+rl_delete_or_show_completions (count, key)
+     int count, key;
+{
+  if (rl_end != 0 && rl_point == rl_end)
+    return (rl_possible_completions (count, key));
+  else
+    return (rl_delete (count, key));
+}
+
+#ifndef RL_COMMENT_BEGIN_DEFAULT
+#define RL_COMMENT_BEGIN_DEFAULT "#"
+#endif
+
+/* Turn the current line into a comment in shell history.
+   A K*rn shell style function. */
+int
+rl_insert_comment (count, key)
+     int count, key;
+{
+  char *rl_comment_text;
+  int rl_comment_len;
+
+  rl_beg_of_line (1, key);
+  rl_comment_text = _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT;
+
+  if (rl_explicit_arg == 0)
+    rl_insert_text (rl_comment_text);
+  else
+    {
+      rl_comment_len = strlen (rl_comment_text);
+      if (STREQN (rl_comment_text, rl_line_buffer, rl_comment_len))
+       rl_delete_text (rl_point, rl_point + rl_comment_len);
+      else
+       rl_insert_text (rl_comment_text);
+    }
+
+  (*rl_redisplay_function) ();
+  rl_newline (1, '\n');
+
+  return (0);
+}
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Changing Case                               */
+/*                                                                 */
+/* **************************************************************** */
+
+/* The three kinds of things that we know how to do. */
+#define UpCase 1
+#define DownCase 2
+#define CapCase 3
+
+/* Uppercase the word at point. */
+int
+rl_upcase_word (count, key)
+     int count, key;
+{
+  return (rl_change_case (count, UpCase));
+}
+
+/* Lowercase the word at point. */
+int
+rl_downcase_word (count, key)
+     int count, key;
+{
+  return (rl_change_case (count, DownCase));
+}
+
+/* Upcase the first letter, downcase the rest. */
+int
+rl_capitalize_word (count, key)
+     int count, key;
+{
+ return (rl_change_case (count, CapCase));
+}
+
+/* The meaty function.
+   Change the case of COUNT words, performing OP on them.
+   OP is one of UpCase, DownCase, or CapCase.
+   If a negative argument is given, leave point where it started,
+   otherwise, leave it where it moves to. */
+static int
+rl_change_case (count, op)
+     int count, op;
+{
+  register int start, end;
+  int inword, c;
+
+  start = rl_point;
+  rl_forward_word (count, 0);
+  end = rl_point;
+
+  if (count < 0)
+    SWAP (start, end);
+
+  /* We are going to modify some text, so let's prepare to undo it. */
+  rl_modifying (start, end);
+
+  for (inword = 0; start < end; start++)
+    {
+      c = rl_line_buffer[start];
+      switch (op)
+       {
+       case UpCase:
+         rl_line_buffer[start] = _rl_to_upper (c);
+         break;
+
+       case DownCase:
+         rl_line_buffer[start] = _rl_to_lower (c);
+         break;
+
+       case CapCase:
+         rl_line_buffer[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
+         inword = rl_alphabetic (rl_line_buffer[start]);
+         break;
+
+       default:
+         rl_ding ();
+         return -1;
+       }
+    }
+  rl_point = end;
+  return 0;
+}
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Transposition                               */
+/*                                                                 */
+/* **************************************************************** */
+
+/* Transpose the words at point.  If point is at the end of the line,
+   transpose the two words before point. */
+int
+rl_transpose_words (count, key)
+     int count, key;
+{
+  char *word1, *word2;
+  int w1_beg, w1_end, w2_beg, w2_end;
+  int orig_point = rl_point;
+
+  if (!count)
+    return 0;
+
+  /* Find the two words. */
+  rl_forward_word (count, key);
+  w2_end = rl_point;
+  rl_backward_word (1, key);
+  w2_beg = rl_point;
+  rl_backward_word (count, key);
+  w1_beg = rl_point;
+  rl_forward_word (1, key);
+  w1_end = rl_point;
+
+  /* Do some check to make sure that there really are two words. */
+  if ((w1_beg == w2_beg) || (w2_beg < w1_end))
+    {
+      rl_ding ();
+      rl_point = orig_point;
+      return -1;
+    }
+
+  /* Get the text of the words. */
+  word1 = rl_copy_text (w1_beg, w1_end);
+  word2 = rl_copy_text (w2_beg, w2_end);
+
+  /* We are about to do many insertions and deletions.  Remember them
+     as one operation. */
+  rl_begin_undo_group ();
+
+  /* Do the stuff at word2 first, so that we don't have to worry
+     about word1 moving. */
+  rl_point = w2_beg;
+  rl_delete_text (w2_beg, w2_end);
+  rl_insert_text (word1);
+
+  rl_point = w1_beg;
+  rl_delete_text (w1_beg, w1_end);
+  rl_insert_text (word2);
+
+  /* This is exactly correct since the text before this point has not
+     changed in length. */
+  rl_point = w2_end;
+
+  /* I think that does it. */
+  rl_end_undo_group ();
+  free (word1);
+  free (word2);
+
+  return 0;
+}
+
+/* Transpose the characters at point.  If point is at the end of the line,
+   then transpose the characters before point. */
+int
+rl_transpose_chars (count, key)
+     int count, key;
+{
+#if defined (HANDLE_MULTIBYTE)
+  char *dummy;
+  int i, prev_point;
+#else
+  char dummy[2];
+#endif
+  int char_length;
+
+  if (count == 0)
+    return 0;
+
+  if (!rl_point || rl_end < 2)
+    {
+      rl_ding ();
+      return -1;
+    }
+
+  rl_begin_undo_group ();
+
+  if (rl_point == rl_end)
+    {
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
+      else
+       --rl_point;
+      count = 1;
+    }
+
+#if defined (HANDLE_MULTIBYTE)
+  prev_point = rl_point;
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
+  else
+#endif
+    rl_point--;
+
+#if defined (HANDLE_MULTIBYTE)
+  char_length = prev_point - rl_point;
+  dummy = (char *)xmalloc (char_length + 1);
+  for (i = 0; i < char_length; i++)
+    dummy[i] = rl_line_buffer[rl_point + i];
+  dummy[i] = '\0';
+#else
+  dummy[0] = rl_line_buffer[rl_point];
+  dummy[char_length = 1] = '\0';
+#endif
+
+  rl_delete_text (rl_point, rl_point + char_length);
+
+  rl_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
+
+  _rl_fix_point (0);
+  rl_insert_text (dummy);
+  rl_end_undo_group ();
+
+#if defined (HANDLE_MULTIBYTE)
+  free (dummy);
+#endif
+
+  return 0;
+}
+
+/* **************************************************************** */
+/*                                                                 */
+/*                     Character Searching                         */
+/*                                                                 */
+/* **************************************************************** */
+
+int
+#if defined (HANDLE_MULTIBYTE)
+_rl_char_search_internal (count, dir, smbchar, len)
+     int count, dir;
+     char *smbchar;
+     int len;
+#else
+_rl_char_search_internal (count, dir, schar)
+     int count, dir, schar;
+#endif
+{
+  int pos, inc;
+#if defined (HANDLE_MULTIBYTE)
+  int prepos;
+#endif
+
+  pos = rl_point;
+  inc = (dir < 0) ? -1 : 1;
+  while (count)
+    {
+      if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
+       {
+         rl_ding ();
+         return -1;
+       }
+
+#if defined (HANDLE_MULTIBYTE)
+      pos = (inc > 0) ? _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)
+                     : _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY);
+#else
+      pos += inc;
+#endif
+      do
+       {
+#if defined (HANDLE_MULTIBYTE)
+         if (_rl_is_mbchar_matched (rl_line_buffer, pos, rl_end, smbchar, len))
+#else
+         if (rl_line_buffer[pos] == schar)
+#endif
+           {
+             count--;
+             if (dir < 0)
+               rl_point = (dir == BTO) ? _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)
+                                       : pos;
+             else
+               rl_point = (dir == FTO) ? _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY)
+                                       : pos;
+             break;
+           }
+#if defined (HANDLE_MULTIBYTE)
+         prepos = pos;
+#endif
+       }
+#if defined (HANDLE_MULTIBYTE)
+      while ((dir < 0) ? (pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY)) != prepos
+                      : (pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)) != prepos);
+#else
+      while ((dir < 0) ? pos-- : ++pos < rl_end);
+#endif
+    }
+  return (0);
+}
+
+/* Search COUNT times for a character read from the current input stream.
+   FDIR is the direction to search if COUNT is non-negative; otherwise
+   the search goes in BDIR.  So much is dependent on HANDLE_MULTIBYTE
+   that there are two separate versions of this function. */
+#if defined (HANDLE_MULTIBYTE)
+static int
+_rl_char_search (count, fdir, bdir)
+     int count, fdir, bdir;
+{
+  char mbchar[MB_LEN_MAX];
+  int mb_len;
+
+  mb_len = _rl_read_mbchar (mbchar, MB_LEN_MAX);
+
+  if (count < 0)
+    return (_rl_char_search_internal (-count, bdir, mbchar, mb_len));
+  else
+    return (_rl_char_search_internal (count, fdir, mbchar, mb_len));
+}
+#else /* !HANDLE_MULTIBYTE */
+static int
+_rl_char_search (count, fdir, bdir)
+     int count, fdir, bdir;
+{
+  int c;
+
+  RL_SETSTATE(RL_STATE_MOREINPUT);
+  c = rl_read_key ();
+  RL_UNSETSTATE(RL_STATE_MOREINPUT);
+
+  if (count < 0)
+    return (_rl_char_search_internal (-count, bdir, c));
+  else
+    return (_rl_char_search_internal (count, fdir, c));
+}
+#endif /* !HANDLE_MULTIBYTE */
+
+int
+rl_char_search (count, key)
+     int count, key;
+{
+  return (_rl_char_search (count, FFIND, BFIND));
+}
+
+int
+rl_backward_char_search (count, key)
+     int count, key;
+{
+  return (_rl_char_search (count, BFIND, FFIND));
+}
+
+/* **************************************************************** */
+/*                                                                 */
+/*                The Mark and the Region.                         */
+/*                                                                 */
+/* **************************************************************** */
+
+/* Set the mark at POSITION. */
+int
+_rl_set_mark_at_pos (position)
+     int position;
+{
+  if (position > rl_end)
+    return -1;
+
+  rl_mark = position;
+  return 0;
+}
+
+/* A bindable command to set the mark. */
+int
+rl_set_mark (count, key)
+     int count, key;
+{
+  return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
+}
+
+/* Exchange the position of mark and point. */
+int
+rl_exchange_point_and_mark (count, key)
+     int count, key;
+{
+  if (rl_mark > rl_end)
+    rl_mark = -1;
+
+  if (rl_mark == -1)
+    {
+      rl_ding ();
+      return -1;
+    }
+  else
+    SWAP (rl_point, rl_mark);
+
+  return 0;
+}
diff --git a/parse.y b/parse.y
index 0d97b83..c03d19a 100644 (file)
--- a/parse.y
+++ b/parse.y
@@ -2730,7 +2730,11 @@ parse_matched_pair (qc, open, close, lenp, flags)
   start_lineno = line_number;
   while (count)
     {
+#if 0
       ch = shell_getc ((qc != '\'' || (flags & P_ALLOWESC)) && pass_next_character == 0);
+#else
+      ch = shell_getc (qc != '\'' && pass_next_character == 0);
+#endif
       if (ch == EOF)
        {
          free (ret);
diff --git a/shell.c b/shell.c
index 2e17baa..651ce1d 100644 (file)
--- a/shell.c
+++ b/shell.c
@@ -1089,6 +1089,8 @@ shell_is_restricted (name)
   if (restricted)
     return 1;
   temp = base_pathname (name);
+  if (*temp == '-')
+    temp++;
   return (STREQ (temp, RESTRICTED_SHELL_NAME));
 }
 
diff --git a/shell.c~ b/shell.c~
new file mode 100644 (file)
index 0000000..2e17baa
--- /dev/null
+++ b/shell.c~
@@ -0,0 +1,1786 @@
+/* shell.c -- GNU's idea of the POSIX shell specification. */
+
+/* Copyright (C) 1987-2004 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 2, 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; see the file COPYING.  If not, write to the Free
+   Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
+
+  Birthdate:
+  Sunday, January 10th, 1988.
+  Initial author: Brian Fox
+*/
+#define INSTALL_DEBUG_MODE
+
+#include "config.h"
+
+#include "bashtypes.h"
+#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
+#  include <sys/file.h>
+#endif
+#include "posixstat.h"
+#include "posixtime.h"
+#include "bashansi.h"
+#include <stdio.h>
+#include <signal.h>
+#include <errno.h>
+#include "filecntl.h"
+#include <pwd.h>
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif
+
+#include "bashintl.h"
+
+#define NEED_SH_SETLINEBUF_DECL                /* used in externs.h */
+
+#include "shell.h"
+#include "flags.h"
+#include "trap.h"
+#include "mailcheck.h"
+#include "builtins.h"
+#include "builtins/common.h"
+
+#if defined (JOB_CONTROL)
+#include "jobs.h"
+#endif /* JOB_CONTROL */
+
+#include "input.h"
+#include "execute_cmd.h"
+#include "findcmd.h"
+
+#if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
+#  include <malloc/shmalloc.h>
+#endif
+
+#if defined (HISTORY)
+#  include "bashhist.h"
+#  include <readline/history.h>
+#endif
+
+#include <tilde/tilde.h>
+#include <glob/strmatch.h>
+
+#if defined (__OPENNT)
+#  include <opennt/opennt.h>
+#endif
+
+#if !defined (HAVE_GETPW_DECLS)
+extern struct passwd *getpwuid ();
+#endif /* !HAVE_GETPW_DECLS */
+
+#if !defined (errno)
+extern int errno;
+#endif
+
+#if defined (NO_MAIN_ENV_ARG)
+extern char **environ; /* used if no third argument to main() */
+#endif
+
+extern char *dist_version, *release_status;
+extern int patch_level, build_version;
+extern int shell_level;
+extern int subshell_environment;
+extern int last_command_exit_value;
+extern int line_number;
+extern char *primary_prompt, *secondary_prompt;
+extern int expand_aliases;
+extern char *this_command_name;
+extern int array_needs_making;
+
+/* Non-zero means that this shell has already been run; i.e. you should
+   call shell_reinitialize () if you need to start afresh. */
+int shell_initialized = 0;
+
+COMMAND *global_command = (COMMAND *)NULL;
+
+/* Information about the current user. */
+struct user_info current_user =
+{
+  (uid_t)-1, (uid_t)-1, (gid_t)-1, (gid_t)-1,
+  (char *)NULL, (char *)NULL, (char *)NULL
+};
+
+/* The current host's name. */
+char *current_host_name = (char *)NULL;
+
+/* Non-zero means that this shell is a login shell.
+   Specifically:
+   0 = not login shell.
+   1 = login shell from getty (or equivalent fake out)
+  -1 = login shell from "--login" (or -l) flag.
+  -2 = both from getty, and from flag.
+ */
+int login_shell = 0;
+
+/* Non-zero means that at this moment, the shell is interactive.  In
+   general, this means that the shell is at this moment reading input
+   from the keyboard. */
+int interactive = 0;
+
+/* Non-zero means that the shell was started as an interactive shell. */
+int interactive_shell = 0;
+
+/* Non-zero means to send a SIGHUP to all jobs when an interactive login
+   shell exits. */
+int hup_on_exit = 0;
+
+/* Tells what state the shell was in when it started:
+       0 = non-interactive shell script
+       1 = interactive
+       2 = -c command
+       3 = wordexp evaluation
+   This is a superset of the information provided by interactive_shell.
+*/
+int startup_state = 0;
+
+/* Special debugging helper. */
+int debugging_login_shell = 0;
+
+/* The environment that the shell passes to other commands. */
+char **shell_environment;
+
+/* Non-zero when we are executing a top-level command. */
+int executing = 0;
+
+/* The number of commands executed so far. */
+int current_command_number = 1;
+
+/* Non-zero is the recursion depth for commands. */
+int indirection_level = 0;
+
+/* The name of this shell, as taken from argv[0]. */
+char *shell_name = (char *)NULL;
+
+/* time in seconds when the shell was started */
+time_t shell_start_time;
+
+/* Are we running in an emacs shell window? */
+int running_under_emacs;
+
+/* The name of the .(shell)rc file. */
+static char *bashrc_file = "~/.bashrc";
+
+/* Non-zero means to act more like the Bourne shell on startup. */
+static int act_like_sh;
+
+/* Non-zero if this shell is being run by `su'. */
+static int su_shell;
+
+/* Non-zero if we have already expanded and sourced $ENV. */
+static int sourced_env;
+
+/* Is this shell running setuid? */
+static int running_setuid;
+
+/* Values for the long-winded argument names. */
+static int debugging;                  /* Do debugging things. */
+static int no_rc;                      /* Don't execute ~/.bashrc */
+static int no_profile;                 /* Don't execute .profile */
+static int do_version;                 /* Display interesting version info. */
+static int make_login_shell;           /* Make this shell be a `-bash' shell. */
+static int want_initial_help;          /* --help option */
+
+int debugging_mode = 0;                /* In debugging mode with --debugger */
+int no_line_editing = 0;       /* Don't do fancy line editing. */
+int posixly_correct = 0;       /* Non-zero means posix.2 superset. */
+int dump_translatable_strings; /* Dump strings in $"...", don't execute. */
+int dump_po_strings;           /* Dump strings in $"..." in po format */
+int wordexp_only = 0;          /* Do word expansion only */
+int protected_mode = 0;                /* No command substitution with --wordexp */
+
+/* Some long-winded argument names.  These are obviously new. */
+#define Int 1
+#define Charp 2
+struct {
+  char *name;
+  int type;
+  int *int_value;
+  char **char_value;
+} long_args[] = {
+  { "debug", Int, &debugging, (char **)0x0 },
+#if defined (DEBUGGER)
+  { "debugger", Int, &debugging_mode, (char **)0x0 },
+#endif
+  { "dump-po-strings", Int, &dump_po_strings, (char **)0x0 },
+  { "dump-strings", Int, &dump_translatable_strings, (char **)0x0 },
+  { "help", Int, &want_initial_help, (char **)0x0 },
+  { "init-file", Charp, (int *)0x0, &bashrc_file },
+  { "login", Int, &make_login_shell, (char **)0x0 },
+  { "noediting", Int, &no_line_editing, (char **)0x0 },
+  { "noprofile", Int, &no_profile, (char **)0x0 },
+  { "norc", Int, &no_rc, (char **)0x0 },
+  { "posix", Int, &posixly_correct, (char **)0x0 },
+  { "protected", Int, &protected_mode, (char **)0x0 },
+  { "rcfile", Charp, (int *)0x0, &bashrc_file },
+#if defined (RESTRICTED_SHELL)
+  { "restricted", Int, &restricted, (char **)0x0 },
+#endif
+  { "verbose", Int, &echo_input_at_read, (char **)0x0 },
+  { "version", Int, &do_version, (char **)0x0 },
+  { "wordexp", Int, &wordexp_only, (char **)0x0 },
+  { (char *)0x0, Int, (int *)0x0, (char **)0x0 }
+};
+
+/* These are extern so execute_simple_command can set them, and then
+   longjmp back to main to execute a shell script, instead of calling
+   main () again and resulting in indefinite, possibly fatal, stack
+   growth. */
+procenv_t subshell_top_level;
+int subshell_argc;
+char **subshell_argv;
+char **subshell_envp;
+
+#if defined (BUFFERED_INPUT)
+/* The file descriptor from which the shell is reading input. */
+int default_buffered_input = -1;
+#endif
+
+/* The following two variables are not static so they can show up in $-. */
+int read_from_stdin;           /* -s flag supplied */
+int want_pending_command;      /* -c flag supplied */
+
+/* This variable is not static so it can be bound to $BASH_EXECUTION_STRING */
+char *command_execution_string;        /* argument to -c option */
+
+int malloc_trace_at_exit = 0;
+
+static int shell_reinitialized = 0;
+
+static FILE *default_input;
+
+static STRING_INT_ALIST *shopt_alist;
+static int shopt_ind = 0, shopt_len = 0;
+
+static int parse_long_options __P((char **, int, int));
+static int parse_shell_options __P((char **, int, int));
+static int bind_args __P((char **, int, int, int));
+
+static void start_debugger __P((void));
+
+static void add_shopt_to_alist __P((char *, int));
+static void run_shopt_alist __P((void));
+
+static void execute_env_file __P((char *));
+static void run_startup_files __P((void));
+static int open_shell_script __P((char *));
+static void set_bash_input __P((void));
+static int run_one_command __P((char *));
+static int run_wordexp __P((char *));
+
+static int uidget __P((void));
+
+static void init_interactive __P((void));
+static void init_noninteractive __P((void));
+
+static void set_shell_name __P((char *));
+static void shell_initialize __P((void));
+static void shell_reinitialize __P((void));
+
+static void show_shell_usage __P((FILE *, int));
+
+#ifdef __CYGWIN__
+static void
+_cygwin32_check_tmp ()
+{
+  struct stat sb;
+
+  if (stat ("/tmp", &sb) < 0)
+    internal_warning (_("could not find /tmp, please create!"));
+  else
+    {
+      if (S_ISDIR (sb.st_mode) == 0)
+       internal_warning (_("/tmp must be a valid directory name"));
+    }
+}
+#endif /* __CYGWIN__ */
+
+#if defined (NO_MAIN_ENV_ARG)
+/* systems without third argument to main() */
+int
+main (argc, argv)
+     int argc;
+     char **argv;
+#else /* !NO_MAIN_ENV_ARG */
+int
+main (argc, argv, env)
+     int argc;
+     char **argv, **env;
+#endif /* !NO_MAIN_ENV_ARG */
+{
+  register int i;
+  int code, old_errexit_flag;
+#if defined (RESTRICTED_SHELL)
+  int saverst;
+#endif
+  volatile int locally_skip_execution;
+  volatile int arg_index, top_level_arg_index;
+#ifdef __OPENNT
+  char **env;
+
+  env = environ;
+#endif /* __OPENNT */
+
+  USE_VAR(argc);
+  USE_VAR(argv);
+  USE_VAR(env);
+  USE_VAR(code);
+  USE_VAR(old_errexit_flag);
+#if defined (RESTRICTED_SHELL)
+  USE_VAR(saverst);
+#endif
+
+  /* Catch early SIGINTs. */
+  code = setjmp (top_level);
+  if (code)
+    exit (2);
+
+#if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
+#  if 1
+  malloc_set_register (1);
+#  endif
+#endif
+
+  check_dev_tty ();
+
+#ifdef __CYGWIN__
+  _cygwin32_check_tmp ();
+#endif /* __CYGWIN__ */
+
+  /* Wait forever if we are debugging a login shell. */
+  while (debugging_login_shell);
+
+  set_default_locale ();
+
+  running_setuid = uidget ();
+
+  if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
+    posixly_correct = 1;
+
+#if defined (USE_GNU_MALLOC_LIBRARY)
+  mcheck (programming_error, (void (*) ())0);
+#endif /* USE_GNU_MALLOC_LIBRARY */
+
+  if (setjmp (subshell_top_level))
+    {
+      argc = subshell_argc;
+      argv = subshell_argv;
+      env = subshell_envp;
+      sourced_env = 0;
+    }
+
+  shell_reinitialized = 0;
+
+  /* Initialize `local' variables for all `invocations' of main (). */
+  arg_index = 1;
+  command_execution_string = (char *)NULL;
+  want_pending_command = locally_skip_execution = read_from_stdin = 0;
+  default_input = stdin;
+#if defined (BUFFERED_INPUT)
+  default_buffered_input = -1;
+#endif
+
+  /* Fix for the `infinite process creation' bug when running shell scripts
+     from startup files on System V. */
+  login_shell = make_login_shell = 0;
+
+  /* If this shell has already been run, then reinitialize it to a
+     vanilla state. */
+  if (shell_initialized || shell_name)
+    {
+      /* Make sure that we do not infinitely recurse as a login shell. */
+      if (*shell_name == '-')
+       shell_name++;
+
+      shell_reinitialize ();
+      if (setjmp (top_level))
+       exit (2);
+    }
+
+  shell_environment = env;
+  set_shell_name (argv[0]);
+  shell_start_time = NOW;      /* NOW now defined in general.h */
+
+  /* Parse argument flags from the input line. */
+
+  /* Find full word arguments first. */
+  arg_index = parse_long_options (argv, arg_index, argc);
+
+  if (want_initial_help)
+    {
+      show_shell_usage (stdout, 1);
+      exit (EXECUTION_SUCCESS);
+    }
+
+  if (do_version)
+    {
+      show_shell_version (1);
+      exit (EXECUTION_SUCCESS);
+    }
+
+  /* All done with full word options; do standard shell option parsing.*/
+  this_command_name = shell_name;      /* for error reporting */
+  arg_index = parse_shell_options (argv, arg_index, argc);
+
+  /* If user supplied the "--login" (or -l) flag, then set and invert
+     LOGIN_SHELL. */
+  if (make_login_shell)
+    {
+      login_shell++;
+      login_shell = -login_shell;
+    }
+
+  set_login_shell (login_shell != 0);
+
+  if (dump_po_strings)
+    dump_translatable_strings = 1;
+
+  if (dump_translatable_strings)
+    read_but_dont_execute = 1;
+
+  if (running_setuid && privileged_mode == 0)
+    disable_priv_mode ();
+
+  /* Need to get the argument to a -c option processed in the
+     above loop.  The next arg is a command to execute, and the
+     following args are $0...$n respectively. */
+  if (want_pending_command)
+    {
+      command_execution_string = argv[arg_index];
+      if (command_execution_string == 0)
+       {
+         report_error (_("%s: option requires an argument"), "-c");
+         exit (EX_BADUSAGE);
+       }
+      arg_index++;
+    }
+  this_command_name = (char *)NULL;
+
+  cmd_init();          /* initialize the command object caches */
+
+  /* First, let the outside world know about our interactive status.
+     A shell is interactive if the `-i' flag was given, or if all of
+     the following conditions are met:
+       no -c command
+       no arguments remaining or the -s flag given
+       standard input is a terminal
+       standard error is a terminal
+     Refer to Posix.2, the description of the `sh' utility. */
+
+  if (forced_interactive ||            /* -i flag */
+      (!command_execution_string &&    /* No -c command and ... */
+       wordexp_only == 0 &&            /* No --wordexp and ... */
+       ((arg_index == argc) ||         /*   no remaining args or... */
+       read_from_stdin) &&             /*   -s flag with args, and */
+       isatty (fileno (stdin)) &&      /* Input is a terminal and */
+       isatty (fileno (stderr))))      /* error output is a terminal. */
+    init_interactive ();
+  else
+    init_noninteractive ();
+
+#define CLOSE_FDS_AT_LOGIN
+#if defined (CLOSE_FDS_AT_LOGIN)
+  /*
+   * Some systems have the bad habit of starting login shells with lots of open
+   * file descriptors.  For instance, most systems that have picked up the
+   * pre-4.0 Sun YP code leave a file descriptor open each time you call one
+   * of the getpw* functions, and it's set to be open across execs.  That
+   * means one for login, one for xterm, one for shelltool, etc.
+   */
+  if (login_shell && interactive_shell)
+    {
+      for (i = 3; i < 20; i++)
+       close (i);
+    }
+#endif /* CLOSE_FDS_AT_LOGIN */
+
+  /* If we're in a strict Posix.2 mode, turn on interactive comments,
+     alias expansion in non-interactive shells, and other Posix.2 things. */
+  if (posixly_correct)
+    {
+      bind_variable ("POSIXLY_CORRECT", "y");
+      sv_strict_posix ("POSIXLY_CORRECT");
+    }
+
+  /* Now we run the shopt_alist and process the options. */
+  if (shopt_alist)
+    run_shopt_alist ();
+
+  /* From here on in, the shell must be a normal functioning shell.
+     Variables from the environment are expected to be set, etc. */
+  shell_initialize ();
+
+  set_default_locale_vars ();
+
+  if (interactive_shell)
+    {
+      char *term, *emacs;
+
+      term = get_string_value ("TERM");
+      no_line_editing |= term && (STREQ (term, "emacs"));
+      emacs = get_string_value ("EMACS");
+      running_under_emacs = emacs ? ((strmatch ("*term*", emacs, 0) == 0) ? 2 : 1)
+                                : 0;
+#if 0
+      no_line_editing |= emacs && emacs[0] == 't' && emacs[1] == '\0';
+#else
+      no_line_editing |= emacs && emacs[0] == 't' && emacs[1] == '\0' && STREQ (term, "dumb");
+#endif
+    }
+
+  top_level_arg_index = arg_index;
+  old_errexit_flag = exit_immediately_on_error;
+
+  /* Give this shell a place to longjmp to before executing the
+     startup files.  This allows users to press C-c to abort the
+     lengthy startup. */
+  code = setjmp (top_level);
+  if (code)
+    {
+      if (code == EXITPROG || code == ERREXIT)
+       exit_shell (last_command_exit_value);
+      else
+       {
+#if defined (JOB_CONTROL)
+         /* Reset job control, since run_startup_files turned it off. */
+         set_job_control (interactive_shell);
+#endif
+         /* Reset value of `set -e', since it's turned off before running
+            the startup files. */
+         exit_immediately_on_error += old_errexit_flag;
+         locally_skip_execution++;
+       }
+    }
+
+  arg_index = top_level_arg_index;
+
+  /* Execute the start-up scripts. */
+
+  if (interactive_shell == 0)
+    {
+      unbind_variable ("PS1");
+      unbind_variable ("PS2");
+      interactive = 0;
+#if 0
+      /* This has already been done by init_noninteractive */
+      expand_aliases = posixly_correct;
+#endif
+    }
+  else
+    {
+      change_flag ('i', FLAG_ON);
+      interactive = 1;
+    }
+
+#if defined (RESTRICTED_SHELL)
+  /* Set restricted_shell based on whether the basename of $0 indicates that
+     the shell should be restricted or if the `-r' option was supplied at
+     startup. */
+  restricted_shell = shell_is_restricted (shell_name);
+
+  /* If the `-r' option is supplied at invocation, make sure that the shell
+     is not in restricted mode when running the startup files. */
+  saverst = restricted;
+  restricted = 0;
+#endif
+
+  /* The startup files are run with `set -e' temporarily disabled. */
+  if (locally_skip_execution == 0 && running_setuid == 0)
+    {
+      old_errexit_flag = exit_immediately_on_error;
+      exit_immediately_on_error = 0;
+
+      run_startup_files ();
+      exit_immediately_on_error += old_errexit_flag;
+    }
+
+  /* If we are invoked as `sh', turn on Posix mode. */
+  if (act_like_sh)
+    {
+      bind_variable ("POSIXLY_CORRECT", "y");
+      sv_strict_posix ("POSIXLY_CORRECT");
+    }
+
+#if defined (RESTRICTED_SHELL)
+  /* Turn on the restrictions after executing the startup files.  This
+     means that `bash -r' or `set -r' invoked from a startup file will
+     turn on the restrictions after the startup files are executed. */
+  restricted = saverst || restricted;
+  if (shell_reinitialized == 0)
+    maybe_make_restricted (shell_name);
+#endif /* RESTRICTED_SHELL */
+
+  if (wordexp_only)
+    {
+      startup_state = 3;
+      last_command_exit_value = run_wordexp (argv[arg_index]);
+      exit_shell (last_command_exit_value);
+    }
+
+  if (command_execution_string)
+    {
+      arg_index = bind_args (argv, arg_index, argc, 0);
+      startup_state = 2;
+
+      if (debugging_mode)
+       start_debugger ();
+
+#if defined (ONESHOT)
+      executing = 1;
+      run_one_command (command_execution_string);
+      exit_shell (last_command_exit_value);
+#else /* ONESHOT */
+      with_input_from_string (command_execution_string, "-c");
+      goto read_and_execute;
+#endif /* !ONESHOT */
+    }
+
+  /* Get possible input filename and set up default_buffered_input or
+     default_input as appropriate. */
+  if (arg_index != argc && read_from_stdin == 0)
+    {
+      open_shell_script (argv[arg_index]);
+      arg_index++;
+    }
+  else if (interactive == 0)
+    /* In this mode, bash is reading a script from stdin, which is a
+       pipe or redirected file. */
+#if defined (BUFFERED_INPUT)
+    default_buffered_input = fileno (stdin);   /* == 0 */
+#else
+    setbuf (default_input, (char *)NULL);
+#endif /* !BUFFERED_INPUT */
+
+  set_bash_input ();
+
+  /* Bind remaining args to $1 ... $n */
+  arg_index = bind_args (argv, arg_index, argc, 1);
+
+  if (debugging_mode && locally_skip_execution == 0 && running_setuid == 0)
+    start_debugger ();
+
+  /* Do the things that should be done only for interactive shells. */
+  if (interactive_shell)
+    {
+      /* Set up for checking for presence of mail. */
+      remember_mail_dates ();
+      reset_mail_timer ();
+
+#if defined (HISTORY)
+      /* Initialize the interactive history stuff. */
+      bash_initialize_history ();
+      /* Don't load the history from the history file if we've already
+        saved some lines in this session (e.g., by putting `history -s xx'
+        into one of the startup files). */
+      if (shell_initialized == 0 && history_lines_this_session == 0)
+       load_history ();
+#endif /* HISTORY */
+
+      /* Initialize terminal state for interactive shells after the
+        .bash_profile and .bashrc are interpreted. */
+      get_tty_state ();
+    }
+
+#if !defined (ONESHOT)
+ read_and_execute:
+#endif /* !ONESHOT */
+
+  shell_initialized = 1;
+
+  /* Read commands until exit condition. */
+  reader_loop ();
+  exit_shell (last_command_exit_value);
+}
+
+static int
+parse_long_options (argv, arg_start, arg_end)
+     char **argv;
+     int arg_start, arg_end;
+{
+  int arg_index, longarg, i;
+  char *arg_string;
+
+  arg_index = arg_start;
+  while ((arg_index != arg_end) && (arg_string = argv[arg_index]) &&
+        (*arg_string == '-'))
+    {
+      longarg = 0;
+
+      /* Make --login equivalent to -login. */
+      if (arg_string[1] == '-' && arg_string[2])
+       {
+         longarg = 1;
+         arg_string++;
+       }
+
+      for (i = 0; long_args[i].name; i++)
+       {
+         if (STREQ (arg_string + 1, long_args[i].name))
+           {
+             if (long_args[i].type == Int)
+               *long_args[i].int_value = 1;
+             else if (argv[++arg_index] == 0)
+               {
+                 report_error (_("%s: option requires an argument"), long_args[i].name);
+                 exit (EX_BADUSAGE);
+               }
+             else
+               *long_args[i].char_value = argv[arg_index];
+
+             break;
+           }
+       }
+      if (long_args[i].name == 0)
+       {
+         if (longarg)
+           {
+             report_error (_("%s: invalid option"), argv[arg_index]);
+             show_shell_usage (stderr, 0);
+             exit (EX_BADUSAGE);
+           }
+         break;                /* No such argument.  Maybe flag arg. */
+       }
+
+      arg_index++;
+    }
+
+  return (arg_index);
+}
+
+static int
+parse_shell_options (argv, arg_start, arg_end)
+     char **argv;
+     int arg_start, arg_end;
+{
+  int arg_index;
+  int arg_character, on_or_off, next_arg, i;
+  char *o_option, *arg_string;
+
+  arg_index = arg_start;
+  while (arg_index != arg_end && (arg_string = argv[arg_index]) &&
+        (*arg_string == '-' || *arg_string == '+'))
+    {
+      /* There are flag arguments, so parse them. */
+      next_arg = arg_index + 1;
+
+      /* A single `-' signals the end of options.  From the 4.3 BSD sh.
+        An option `--' means the same thing; this is the standard
+        getopt(3) meaning. */
+      if (arg_string[0] == '-' &&
+          (arg_string[1] == '\0' ||
+            (arg_string[1] == '-' && arg_string[2] == '\0')))
+       return (next_arg);
+
+      i = 1;
+      on_or_off = arg_string[0];
+      while (arg_character = arg_string[i++])
+       {
+         switch (arg_character)
+           {
+           case 'c':
+             want_pending_command = 1;
+             break;
+
+           case 'l':
+             make_login_shell = 1;
+             break;
+
+           case 's':
+             read_from_stdin = 1;
+             break;
+
+           case 'o':
+             o_option = argv[next_arg];
+             if (o_option == 0)
+               {
+                 list_minus_o_opts (-1, (on_or_off == '-') ? 0 : 1);
+                 break;
+               }
+             if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
+               exit (EX_BADUSAGE);
+             next_arg++;
+             break;
+
+           case 'O':
+             /* Since some of these can be overridden by the normal
+                interactive/non-interactive shell initialization or
+                initializing posix mode, we save the options and process
+                them after initialization. */
+             o_option = argv[next_arg];
+             if (o_option == 0)
+               {
+                 shopt_listopt (o_option, (on_or_off == '-') ? 0 : 1);
+                 break;
+               }
+             add_shopt_to_alist (o_option, on_or_off);
+             next_arg++;
+             break;
+
+           case 'D':
+             dump_translatable_strings = 1;
+             break;
+
+           default:
+             if (change_flag (arg_character, on_or_off) == FLAG_ERROR)
+               {
+                 report_error (_("%c%c: invalid option"), on_or_off, arg_character);
+                 show_shell_usage (stderr, 0);
+                 exit (EX_BADUSAGE);
+               }
+           }
+       }
+      /* Can't do just a simple increment anymore -- what about
+        "bash -abouo emacs ignoreeof -hP"? */
+      arg_index = next_arg;
+    }
+
+  return (arg_index);
+}
+
+/* Exit the shell with status S. */
+void
+exit_shell (s)
+     int s;
+{
+  /* Do trap[0] if defined.  Allow it to override the exit status
+     passed to us. */
+  if (signal_is_trapped (0))
+    s = run_exit_trap ();
+
+#if defined (PROCESS_SUBSTITUTION)
+  unlink_fifo_list ();
+#endif /* PROCESS_SUBSTITUTION */
+
+#if defined (HISTORY)
+  if (interactive_shell)
+    maybe_save_shell_history ();
+#endif /* HISTORY */
+
+#if defined (JOB_CONTROL)
+  /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
+     an interactive login shell.  ksh does this unconditionally. */
+  if (interactive_shell && login_shell && hup_on_exit)
+    hangup_all_jobs ();
+
+  /* If this shell is interactive, terminate all stopped jobs and
+     restore the original terminal process group.  Don't do this if we're
+     in a subshell and calling exit_shell after, for example, a failed
+     word expansion. */
+  if (subshell_environment == 0)
+    end_job_control ();
+#endif /* JOB_CONTROL */
+
+  /* Always return the exit status of the last command to our parent. */
+  sh_exit (s);
+}
+
+/* A wrapper for exit that (optionally) can do other things, like malloc
+   statistics tracing. */
+void
+sh_exit (s)
+     int s;
+{
+#if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
+  if (malloc_trace_at_exit)
+    trace_malloc_stats (get_name_for_error (), (char *)NULL);
+#endif
+
+  exit (s);
+}
+
+/* Source the bash startup files.  If POSIXLY_CORRECT is non-zero, we obey
+   the Posix.2 startup file rules:  $ENV is expanded, and if the file it
+   names exists, that file is sourced.  The Posix.2 rules are in effect
+   for interactive shells only. (section 4.56.5.3) */
+
+/* Execute ~/.bashrc for most shells.  Never execute it if
+   ACT_LIKE_SH is set, or if NO_RC is set.
+
+   If the executable file "/usr/gnu/src/bash/foo" contains:
+
+   #!/usr/gnu/bin/bash
+   echo hello
+
+   then:
+
+        COMMAND            EXECUTE BASHRC
+        --------------------------------
+        bash -c foo            NO
+        bash foo               NO
+        foo                    NO
+        rsh machine ls         YES (for rsh, which calls `bash -c')
+        rsh machine foo        YES (for shell started by rsh) NO (for foo!)
+        echo ls | bash         NO
+        login                  NO
+        bash                   YES
+*/
+
+static void
+execute_env_file (env_file)
+      char *env_file;
+{
+  char *fn;
+
+  if (env_file && *env_file)
+    {
+      fn = expand_string_unsplit_to_string (env_file, Q_DOUBLE_QUOTES);
+      if (fn && *fn)
+       maybe_execute_file (fn, 1);
+      FREE (fn);
+    }
+}
+
+static void
+run_startup_files ()
+{
+#if defined (JOB_CONTROL)
+  int old_job_control;
+#endif
+  int sourced_login, run_by_ssh;
+
+  /* get the rshd/sshd case out of the way first. */
+  if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&
+      act_like_sh == 0 && command_execution_string)
+    {
+#ifdef SSH_SOURCE_BASHRC
+      run_by_ssh = (find_variable ("SSH_CLIENT") != (SHELL_VAR *)0) ||
+                  (find_variable ("SSH2_CLIENT") != (SHELL_VAR *)0);
+#else
+      run_by_ssh = 0;
+#endif
+
+      /* If we were run by sshd or we think we were run by rshd, execute
+        ~/.bashrc if we are a top-level shell. */
+      if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2)
+       {
+#ifdef SYS_BASHRC
+#  if defined (__OPENNT)
+         maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
+#  else
+         maybe_execute_file (SYS_BASHRC, 1);
+#  endif
+#endif
+         maybe_execute_file (bashrc_file, 1);
+         return;
+       }
+    }
+
+#if defined (JOB_CONTROL)
+  /* Startup files should be run without job control enabled. */
+  old_job_control = interactive_shell ? set_job_control (0) : 0;
+#endif
+
+  sourced_login = 0;
+
+  /* A shell begun with the --login (or -l) flag that is not in posix mode
+     runs the login shell startup files, no matter whether or not it is
+     interactive.  If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
+     startup files if argv[0][0] == '-' as well. */
+#if defined (NON_INTERACTIVE_LOGIN_SHELLS)
+  if (login_shell && posixly_correct == 0)
+#else
+  if (login_shell < 0 && posixly_correct == 0)
+#endif
+    {
+      /* We don't execute .bashrc for login shells. */
+      no_rc++;
+
+      /* Execute /etc/profile and one of the personal login shell
+        initialization files. */
+      if (no_profile == 0)
+       {
+         maybe_execute_file (SYS_PROFILE, 1);
+
+         if (act_like_sh)      /* sh */
+           maybe_execute_file ("~/.profile", 1);
+         else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
+                  (maybe_execute_file ("~/.bash_login", 1) == 0))      /* bash */
+           maybe_execute_file ("~/.profile", 1);
+       }
+
+      sourced_login = 1;
+    }
+
+  /* A non-interactive shell not named `sh' and not in posix mode reads and
+     executes commands from $BASH_ENV.  If `su' starts a shell with `-c cmd'
+     and `-su' as the name of the shell, we want to read the startup files.
+     No other non-interactive shells read any startup files. */
+  if (interactive_shell == 0 && !(su_shell && login_shell))
+    {
+      if (posixly_correct == 0 && act_like_sh == 0 && privileged_mode == 0 &&
+           sourced_env++ == 0)
+       execute_env_file (get_string_value ("BASH_ENV"));
+      return;
+    }
+
+  /* Interactive shell or `-su' shell. */
+  if (posixly_correct == 0)              /* bash, sh */
+    {
+      if (login_shell && sourced_login++ == 0)
+       {
+         /* We don't execute .bashrc for login shells. */
+         no_rc++;
+
+         /* Execute /etc/profile and one of the personal login shell
+            initialization files. */
+         if (no_profile == 0)
+           {
+             maybe_execute_file (SYS_PROFILE, 1);
+
+             if (act_like_sh)  /* sh */
+               maybe_execute_file ("~/.profile", 1);
+             else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
+                      (maybe_execute_file ("~/.bash_login", 1) == 0))  /* bash */
+               maybe_execute_file ("~/.profile", 1);
+           }
+       }
+
+      /* bash */
+      if (act_like_sh == 0 && no_rc == 0)
+       {
+#ifdef SYS_BASHRC
+#  if defined (__OPENNT)
+         maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
+#  else
+         maybe_execute_file (SYS_BASHRC, 1);
+#  endif
+#endif
+         maybe_execute_file (bashrc_file, 1);
+       }
+      /* sh */
+      else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0)
+       execute_env_file (get_string_value ("ENV"));
+    }
+  else         /* bash --posix, sh --posix */
+    {
+      /* bash and sh */
+      if (interactive_shell && privileged_mode == 0 && sourced_env++ == 0)
+       execute_env_file (get_string_value ("ENV"));
+    }
+
+#if defined (JOB_CONTROL)
+  set_job_control (old_job_control);
+#endif
+}
+
+#if defined (RESTRICTED_SHELL)
+/* Return 1 if the shell should be a restricted one based on NAME or the
+   value of `restricted'.  Don't actually do anything, just return a
+   boolean value. */
+int
+shell_is_restricted (name)
+     char *name;
+{
+  char *temp;
+
+  if (restricted)
+    return 1;
+  temp = base_pathname (name);
+  return (STREQ (temp, RESTRICTED_SHELL_NAME));
+}
+
+/* Perhaps make this shell a `restricted' one, based on NAME.  If the
+   basename of NAME is "rbash", then this shell is restricted.  The
+   name of the restricted shell is a configurable option, see config.h.
+   In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
+   and non-unsettable.
+   Do this also if `restricted' is already set to 1; maybe the shell was
+   started with -r. */
+int
+maybe_make_restricted (name)
+     char *name;
+{
+  char *temp;
+
+  temp = base_pathname (name);
+  if (*temp == '-')
+    temp++;
+  if (restricted || (STREQ (temp, RESTRICTED_SHELL_NAME)))
+    {
+      set_var_read_only ("PATH");
+      set_var_read_only ("SHELL");
+      set_var_read_only ("ENV");
+      set_var_read_only ("BASH_ENV");
+      restricted = 1;
+    }
+  return (restricted);
+}
+#endif /* RESTRICTED_SHELL */
+
+/* Fetch the current set of uids and gids and return 1 if we're running
+   setuid or setgid. */
+static int
+uidget ()
+{
+  uid_t u;
+
+  u = getuid ();
+  if (current_user.uid != u)
+    {
+      FREE (current_user.user_name);
+      FREE (current_user.shell);
+      FREE (current_user.home_dir);
+      current_user.user_name = current_user.shell = current_user.home_dir = (char *)NULL;
+    }
+  current_user.uid = u;
+  current_user.gid = getgid ();
+  current_user.euid = geteuid ();
+  current_user.egid = getegid ();
+
+  /* See whether or not we are running setuid or setgid. */
+  return (current_user.uid != current_user.euid) ||
+          (current_user.gid != current_user.egid);
+}
+
+void
+disable_priv_mode ()
+{
+  setuid (current_user.uid);
+  setgid (current_user.gid);
+  current_user.euid = current_user.uid;
+  current_user.egid = current_user.gid;
+}
+
+static int
+run_wordexp (words)
+     char *words;
+{
+  int code, nw, nb;
+  WORD_LIST *wl, *tl, *result;
+
+  code = setjmp (top_level);
+
+  if (code != NOT_JUMPED)
+    {
+      switch (code)
+       {
+         /* Some kind of throw to top_level has occured. */
+       case FORCE_EOF:
+         return last_command_exit_value = 127;
+       case ERREXIT:
+       case EXITPROG:
+         return last_command_exit_value;
+       case DISCARD:
+         return last_command_exit_value = 1;
+       default:
+         command_error ("run_wordexp", CMDERR_BADJUMP, code, 0);
+       }
+    }
+
+  /* Run it through the parser to get a list of words and expand them */
+  if (words && *words)
+    {
+      with_input_from_string (words, "--wordexp");
+      if (parse_command () != 0)
+       return (126);
+      if (global_command == 0)
+       {
+         printf ("0\n0\n");
+         return (0);
+       }
+      if (global_command->type != cm_simple)
+       return (126);
+      wl = global_command->value.Simple->words;
+      if (protected_mode)
+       for (tl = wl; tl; tl = tl->next)
+         tl->word->flags |= W_NOCOMSUB;
+      result = wl ? expand_words_no_vars (wl) : (WORD_LIST *)0;
+    }
+  else
+    result = (WORD_LIST *)0;
+
+  last_command_exit_value = 0;
+
+  if (result == 0)
+    {
+      printf ("0\n0\n");
+      return (0);
+    }
+
+  /* Count up the number of words and bytes, and print them.  Don't count
+     the trailing NUL byte. */
+  for (nw = nb = 0, wl = result; wl; wl = wl->next)
+    {
+      nw++;
+      nb += strlen (wl->word->word);
+    }
+  printf ("%u\n%u\n", nw, nb);
+  /* Print each word on a separate line.  This will have to be changed when
+     the interface to glibc is completed. */
+  for (wl = result; wl; wl = wl->next)
+    printf ("%s\n", wl->word->word);
+
+  return (0);
+}
+
+#if defined (ONESHOT)
+/* Run one command, given as the argument to the -c option.  Tell
+   parse_and_execute not to fork for a simple command. */
+static int
+run_one_command (command)
+     char *command;
+{
+  int code;
+
+  code = setjmp (top_level);
+
+  if (code != NOT_JUMPED)
+    {
+#if defined (PROCESS_SUBSTITUTION)
+      unlink_fifo_list ();
+#endif /* PROCESS_SUBSTITUTION */
+      switch (code)
+       {
+         /* Some kind of throw to top_level has occured. */
+       case FORCE_EOF:
+         return last_command_exit_value = 127;
+       case ERREXIT:
+       case EXITPROG:
+         return last_command_exit_value;
+       case DISCARD:
+         return last_command_exit_value = 1;
+       default:
+         command_error ("run_one_command", CMDERR_BADJUMP, code, 0);
+       }
+    }
+   return (parse_and_execute (savestring (command), "-c", SEVAL_NOHIST));
+}
+#endif /* ONESHOT */
+
+static int
+bind_args (argv, arg_start, arg_end, start_index)
+     char **argv;
+     int arg_start, arg_end, start_index;
+{
+  register int i;
+  WORD_LIST *args;
+
+  for (i = arg_start, args = (WORD_LIST *)NULL; i != arg_end; i++)
+    args = make_word_list (make_word (argv[i]), args);
+  if (args)
+    {
+      args = REVERSE_LIST (args, WORD_LIST *);
+      if (start_index == 0)    /* bind to $0...$n for sh -c command */
+       {
+         /* Posix.2 4.56.3 says that the first argument after sh -c command
+            becomes $0, and the rest of the arguments become $1...$n */
+         shell_name = savestring (args->word->word);
+         FREE (dollar_vars[0]);
+         dollar_vars[0] = savestring (args->word->word);
+         remember_args (args->next, 1);
+         push_args (args->next);       /* BASH_ARGV and BASH_ARGC */
+       }
+      else                     /* bind to $1...$n for shell script */
+        {
+         remember_args (args, 1);
+         push_args (args);             /* BASH_ARGV and BASH_ARGC */
+        }
+
+      dispose_words (args);
+    }
+
+  return (i);
+}
+
+void
+unbind_args ()
+{
+  remember_args ((WORD_LIST *)NULL, 1);
+  pop_args ();                         /* Reset BASH_ARGV and BASH_ARGC */
+}
+
+static void
+start_debugger ()
+{
+#if defined (DEBUGGER) && defined (DEBUGGER_START_FILE)
+  int old_errexit;
+
+  old_errexit = exit_immediately_on_error;
+  exit_immediately_on_error = 0;
+
+  maybe_execute_file (DEBUGGER_START_FILE, 1);
+  function_trace_mode = 1;
+
+  exit_immediately_on_error += old_errexit;
+#endif
+}
+
+static int
+open_shell_script (script_name)
+     char *script_name;
+{
+  int fd, e, fd_is_tty;
+  char *filename, *path_filename, *t;
+  char sample[80];
+  int sample_len;
+  struct stat sb;
+#if defined (ARRAY_VARS)
+  SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
+  ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
+#endif
+
+  filename = savestring (script_name);
+
+  fd = open (filename, O_RDONLY);
+  if ((fd < 0) && (errno == ENOENT) && (absolute_program (filename) == 0))
+    {
+      e = errno;
+      /* If it's not in the current directory, try looking through PATH
+        for it. */
+      path_filename = find_path_file (script_name);
+      if (path_filename)
+       {
+         free (filename);
+         filename = path_filename;
+         fd = open (filename, O_RDONLY);
+       }
+      else
+       errno = e;
+    }
+
+  if (fd < 0)
+    {
+      e = errno;
+      file_error (filename);
+      exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
+    }
+
+  free (dollar_vars[0]);
+  dollar_vars[0] = savestring (script_name);
+
+#if defined (ARRAY_VARS)
+  GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
+  GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
+  GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
+
+  array_push (bash_source_a, filename);
+  if (bash_lineno_a)
+    {
+      t = itos (executing_line_number ());
+      array_push (bash_lineno_a, t);
+      free (t);
+    }
+  array_push (funcname_a, "main");
+#endif
+
+#ifdef HAVE_DEV_FD
+  fd_is_tty = isatty (fd);
+#else
+  fd_is_tty = 0;
+#endif
+
+  /* Only do this with non-tty file descriptors we can seek on. */
+  if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1))
+    {
+      /* Check to see if the `file' in `bash file' is a binary file
+        according to the same tests done by execute_simple_command (),
+        and report an error and exit if it is. */
+      sample_len = read (fd, sample, sizeof (sample));
+      if (sample_len < 0)
+       {
+         e = errno;
+         if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
+           internal_error (_("%s: is a directory"), filename);
+         else
+           {
+             errno = e;
+             file_error (filename);
+           }
+         exit (EX_NOEXEC);
+       }
+      else if (sample_len > 0 && (check_binary_file (sample, sample_len)))
+       {
+         internal_error ("%s: cannot execute binary file", filename);
+         exit (EX_BINARY_FILE);
+       }
+      /* Now rewind the file back to the beginning. */
+      lseek (fd, 0L, 0);
+    }
+
+  /* Open the script.  But try to move the file descriptor to a randomly
+     large one, in the hopes that any descriptors used by the script will
+     not match with ours. */
+  fd = move_to_high_fd (fd, 0, -1);
+
+#if defined (__CYGWIN__) && defined (O_TEXT)
+  setmode (fd, O_TEXT);
+#endif
+
+#if defined (BUFFERED_INPUT)
+  default_buffered_input = fd;
+  SET_CLOSE_ON_EXEC (default_buffered_input);
+#else /* !BUFFERED_INPUT */
+  default_input = fdopen (fd, "r");
+
+  if (default_input == 0)
+    {
+      file_error (filename);
+      exit (EX_NOTFOUND);
+    }
+
+  SET_CLOSE_ON_EXEC (fd);
+  if (fileno (default_input) != fd)
+    SET_CLOSE_ON_EXEC (fileno (default_input));
+#endif /* !BUFFERED_INPUT */
+
+  /* Just about the only way for this code to be executed is if something
+     like `bash -i /dev/stdin' is executed. */
+  if (interactive_shell && fd_is_tty)
+    {
+      dup2 (fd, 0);
+      close (fd);
+      fd = 0;
+#if defined (BUFFERED_INPUT)
+      default_buffered_input = 0;
+#else
+      fclose (default_input);
+      default_input = stdin;
+#endif
+    }
+  else if (forced_interactive && fd_is_tty == 0)
+    /* But if a script is called with something like `bash -i scriptname',
+       we need to do a non-interactive setup here, since we didn't do it
+       before. */
+    init_noninteractive ();
+
+  free (filename);
+  return (fd);
+}
+
+/* Initialize the input routines for the parser. */
+static void
+set_bash_input ()
+{
+  /* Make sure the fd from which we are reading input is not in
+     no-delay mode. */
+#if defined (BUFFERED_INPUT)
+  if (interactive == 0)
+    sh_unset_nodelay_mode (default_buffered_input);
+  else
+#endif /* !BUFFERED_INPUT */
+    sh_unset_nodelay_mode (fileno (stdin));
+
+  /* with_input_from_stdin really means `with_input_from_readline' */
+  if (interactive && no_line_editing == 0)
+    with_input_from_stdin ();
+  else
+#if defined (BUFFERED_INPUT)
+    {
+      if (interactive == 0)
+       with_input_from_buffered_stream (default_buffered_input, dollar_vars[0]);
+      else
+       with_input_from_stream (default_input, dollar_vars[0]);
+    }
+#else /* !BUFFERED_INPUT */
+    with_input_from_stream (default_input, dollar_vars[0]);
+#endif /* !BUFFERED_INPUT */
+}
+
+/* Close the current shell script input source and forget about it.  This is
+   extern so execute_cmd.c:initialize_subshell() can call it.  If CHECK_ZERO
+   is non-zero, we close default_buffered_input even if it's the standard
+   input (fd 0). */
+void
+unset_bash_input (check_zero)
+     int check_zero;
+{
+#if defined (BUFFERED_INPUT)
+  if ((check_zero && default_buffered_input >= 0) ||
+      (check_zero == 0 && default_buffered_input > 0))
+    {
+      close_buffered_fd (default_buffered_input);
+      default_buffered_input = bash_input.location.buffered_fd = -1;
+    }
+#else /* !BUFFERED_INPUT */
+  if (default_input)
+    {
+      fclose (default_input);
+      default_input = (FILE *)NULL;
+    }
+#endif /* !BUFFERED_INPUT */
+}
+      
+
+#if !defined (PROGRAM)
+#  define PROGRAM "bash"
+#endif
+
+static void
+set_shell_name (argv0)
+     char *argv0;
+{
+  /* Here's a hack.  If the name of this shell is "sh", then don't do
+     any startup files; just try to be more like /bin/sh. */
+  shell_name = base_pathname (argv0);
+
+  if (*shell_name == '-')
+    {
+      shell_name++;
+      login_shell++;
+    }
+
+  if (shell_name[0] == 's' && shell_name[1] == 'h' && shell_name[2] == '\0')
+    act_like_sh++;
+  if (shell_name[0] == 's' && shell_name[1] == 'u' && shell_name[2] == '\0')
+    su_shell++;
+
+  shell_name = argv0;
+  FREE (dollar_vars[0]);
+  dollar_vars[0] = savestring (shell_name);
+
+  /* A program may start an interactive shell with
+         "execl ("/bin/bash", "-", NULL)".
+     If so, default the name of this shell to our name. */
+  if (!shell_name || !*shell_name || (shell_name[0] == '-' && !shell_name[1]))
+    shell_name = PROGRAM;
+}
+
+static void
+init_interactive ()
+{
+  interactive_shell = startup_state = interactive = 1;
+  expand_aliases = 1;
+}
+
+static void
+init_noninteractive ()
+{
+#if defined (HISTORY)
+  bash_history_reinit (0);
+#endif /* HISTORY */
+  interactive_shell = startup_state = interactive = 0;
+  expand_aliases = posixly_correct;    /* XXX - was 0 not posixly_correct */
+  no_line_editing = 1;
+#if defined (JOB_CONTROL)
+  set_job_control (0);
+#endif /* JOB_CONTROL */
+}
+
+void
+get_current_user_info ()
+{
+  struct passwd *entry;
+
+  /* Don't fetch this more than once. */
+  if (current_user.user_name == 0)
+    {
+      entry = getpwuid (current_user.uid);
+      if (entry)
+       {
+         current_user.user_name = savestring (entry->pw_name);
+         current_user.shell = (entry->pw_shell && entry->pw_shell[0])
+                               ? savestring (entry->pw_shell)
+                               : savestring ("/bin/sh");
+         current_user.home_dir = savestring (entry->pw_dir);
+       }
+      else
+       {
+         current_user.user_name = _("I have no name!");
+         current_user.user_name = savestring (current_user.user_name);
+         current_user.shell = savestring ("/bin/sh");
+         current_user.home_dir = savestring ("/");
+       }
+      endpwent ();
+    }
+}
+
+/* Do whatever is necessary to initialize the shell.
+   Put new initializations in here. */
+static void
+shell_initialize ()
+{
+  char hostname[256];
+
+  /* Line buffer output for stderr and stdout. */
+  if (shell_initialized == 0)
+    {
+      sh_setlinebuf (stderr);
+      sh_setlinebuf (stdout);
+    }
+
+  /* Sort the array of shell builtins so that the binary search in
+     find_shell_builtin () works correctly. */
+  initialize_shell_builtins ();
+
+  /* Initialize the trap signal handlers before installing our own
+     signal handlers.  traps.c:restore_original_signals () is responsible
+     for restoring the original default signal handlers.  That function
+     is called when we make a new child. */
+  initialize_traps ();
+  initialize_signals (0);
+
+  /* It's highly unlikely that this will change. */
+  if (current_host_name == 0)
+    {
+      /* Initialize current_host_name. */
+      if (gethostname (hostname, 255) < 0)
+       current_host_name = "??host??";
+      else
+       current_host_name = savestring (hostname);
+    }
+
+  /* Initialize the stuff in current_user that comes from the password
+     file.  We don't need to do this right away if the shell is not
+     interactive. */
+  if (interactive_shell)
+    get_current_user_info ();
+
+  /* Initialize our interface to the tilde expander. */
+  tilde_initialize ();
+
+  /* Initialize internal and environment variables.  Don't import shell
+     functions from the environment if we are running in privileged or
+     restricted mode or if the shell is running setuid. */
+#if defined (RESTRICTED_SHELL)
+  initialize_shell_variables (shell_environment, privileged_mode||restricted||running_setuid);
+#else
+  initialize_shell_variables (shell_environment, privileged_mode||running_setuid);
+#endif
+
+  /* Initialize the data structures for storing and running jobs. */
+  initialize_job_control (0);
+
+  /* Initialize input streams to null. */
+  initialize_bash_input ();
+
+  initialize_flags ();
+
+  /* Initialize the shell options.  Don't import the shell options
+     from the environment variable $SHELLOPTS if we are running in
+     privileged or restricted mode or if the shell is running setuid. */
+#if defined (RESTRICTED_SHELL)
+  initialize_shell_options (privileged_mode||restricted||running_setuid);
+#else
+  initialize_shell_options (privileged_mode||running_setuid);
+#endif
+}
+
+/* Function called by main () when it appears that the shell has already
+   had some initialization performed.  This is supposed to reset the world
+   back to a pristine state, as if we had been exec'ed. */
+static void
+shell_reinitialize ()
+{
+  /* The default shell prompts. */
+  primary_prompt = PPROMPT;
+  secondary_prompt = SPROMPT;
+
+  /* Things that get 1. */
+  current_command_number = 1;
+
+  /* We have decided that the ~/.bashrc file should not be executed
+     for the invocation of each shell script.  If the variable $ENV
+     (or $BASH_ENV) is set, its value is used as the name of a file
+     to source. */
+  no_rc = no_profile = 1;
+
+  /* Things that get 0. */
+  login_shell = make_login_shell = interactive = executing = 0;
+  debugging = do_version = line_number = last_command_exit_value = 0;
+  forced_interactive = interactive_shell = subshell_environment = 0;
+  expand_aliases = 0;
+
+#if defined (HISTORY)
+  bash_history_reinit (0);
+#endif /* HISTORY */
+
+#if defined (RESTRICTED_SHELL)
+  restricted = 0;
+#endif /* RESTRICTED_SHELL */
+
+  /* Ensure that the default startup file is used.  (Except that we don't
+     execute this file for reinitialized shells). */
+  bashrc_file = "~/.bashrc";
+
+  /* Delete all variables and functions.  They will be reinitialized when
+     the environment is parsed. */
+  delete_all_contexts (shell_variables);
+  delete_all_variables (shell_functions);
+
+  shell_reinitialized = 1;
+}
+
+static void
+show_shell_usage (fp, extra)
+     FILE *fp;
+     int extra;
+{
+  int i;
+  char *set_opts, *s, *t;
+
+  if (extra)
+    fprintf (fp, "GNU bash, version %s-(%s)\n", shell_version_string (), MACHTYPE);
+  fprintf (fp, _("Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n"),
+            shell_name, shell_name);
+  fputs (_("GNU long options:\n"), fp);
+  for (i = 0; long_args[i].name; i++)
+    fprintf (fp, "\t--%s\n", long_args[i].name);
+
+  fputs (_("Shell options:\n"), fp);
+  fputs (_("\t-irsD or -c command or -O shopt_option\t\t(invocation only)\n"), fp);
+
+  for (i = 0, set_opts = 0; shell_builtins[i].name; i++)
+    if (STREQ (shell_builtins[i].name, "set"))
+      set_opts = savestring (shell_builtins[i].short_doc);
+  if (set_opts)
+    {
+      s = xstrchr (set_opts, '[');
+      if (s == 0)
+       s = set_opts;
+      while (*++s == '-')
+       ;
+      t = xstrchr (s, ']');
+      if (t)
+       *t = '\0';
+      fprintf (fp, _("\t-%s or -o option\n"), s);
+      free (set_opts);
+    }
+
+  if (extra)
+    {
+      fprintf (fp, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name);
+      fprintf (fp, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name);
+      fprintf (fp, _("Use the `bashbug' command to report bugs.\n"));
+    }
+}
+
+static void
+add_shopt_to_alist (opt, on_or_off)
+     char *opt;
+     int on_or_off;
+{
+  if (shopt_ind >= shopt_len)
+    {
+      shopt_len += 8;
+      shopt_alist = (STRING_INT_ALIST *)xrealloc (shopt_alist, shopt_len * sizeof (shopt_alist[0]));
+    }
+  shopt_alist[shopt_ind].word = opt;
+  shopt_alist[shopt_ind].token = on_or_off;
+  shopt_ind++;
+}
+
+static void
+run_shopt_alist ()
+{
+  register int i;
+
+  for (i = 0; i < shopt_ind; i++)
+    if (shopt_setopt (shopt_alist[i].word, (shopt_alist[i].token == '-')) != EXECUTION_SUCCESS)
+      exit (EX_BADUSAGE);
+  free (shopt_alist);
+  shopt_alist = 0;
+  shopt_ind = shopt_len = 0;
+}
diff --git a/subst.c b/subst.c
index 85393c6..c72b8a6 100644 (file)
--- a/subst.c
+++ b/subst.c
@@ -6503,10 +6503,12 @@ add_string:
            assignoff = sindex;
          if (sindex == assignoff && string[sindex+1] == '~')   /* XXX */
            word->flags |= W_ITILDE;
+#if 0
          else if ((word->flags & W_ASSIGNMENT) &&
                   (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
                   string[sindex+1] == '~')
            word->flags |= W_ITILDE;
+#endif
          goto add_character;
 
        case ':':
index 9ba8aa6..85393c6 100644 (file)
--- a/subst.c~
+++ b/subst.c~
@@ -2201,7 +2201,11 @@ do_assignment_internal (word, expand)
       tlen = STRLEN (temp);
 
 #if defined (ARRAY_VARS)
+#  if 0
       if (expand && temp[0] == LPAREN && temp[tlen-1] == RPAREN)
+#else
+      if (expand && (word->flags & W_COMPASSIGN))
+#endif
        {
          assign_list = ni = 1;
          value = extract_array_assignment_list (temp, &ni);
@@ -4347,7 +4351,7 @@ command_substitute (string, quoted)
      char *string;
      int quoted;
 {
-  pid_t pid, old_pid, old_pipeline_pgrp;
+  pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
   char *istring;
   int result, fildes[2], function_value, pflags, rc;
 
@@ -4395,7 +4399,14 @@ command_substitute (string, quoted)
   cleanup_the_pipeline ();
 #endif
 
+  old_async_pid = last_asynchronous_pid;
+#if 0
   pid = make_child ((char *)NULL, 0);
+#else
+  pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
+#endif
+  last_asynchronous_pid = old_async_pid;
+
   if (pid == 0)
     /* Reset the signal handlers in the child, but don't free the
        trap strings. */
index 3efcf32..72ec06a 100755 (executable)
@@ -1,4 +1,4 @@
-BUILD_DIR=/usr/local/build/chet/bash/bash-current
+BUILD_DIR=/usr/local/build/bash/bash-current
 THIS_SH=$BUILD_DIR/bash
 PATH=$PATH:$BUILD_DIR
 
index fa2ae2a..4047025 100644 (file)
@@ -128,6 +128,10 @@ grep [ 123 ] *
 length = 3
 value = new1 new2 new3
 ./array.tests: line 237: narray: unbound variable
+./array1.sub: line 1: syntax error near unexpected token `('
+./array1.sub: line 1: `printf "%s\n" -a a=(a 'b  c')'
+./array2.sub: line 1: syntax error near unexpected token `('
+./array2.sub: line 1: `declare -a ''=(a 'b c')'
 
 a b c d e f g
 for case if then else
@@ -135,10 +139,10 @@ for case if then else
 12 14 16 18 20
 4414758999202
 aaa bbb
-./array.tests: line 277: syntax error near unexpected token `<>'
-./array.tests: line 277: `metas=( <> < > ! )'
-./array.tests: line 278: syntax error near unexpected token `<>'
-./array.tests: line 278: `metas=( [1]=<> [2]=< [3]=> [4]=! )'
+./array.tests: line 280: syntax error near unexpected token `<>'
+./array.tests: line 280: `metas=( <> < > ! )'
+./array.tests: line 281: syntax error near unexpected token `<>'
+./array.tests: line 281: `metas=( [1]=<> [2]=< [3]=> [4]=! )'
 abc 3
 case 4
 abc case if then else 5
@@ -178,3 +182,13 @@ negative offset 2 - expect seven
 seven
 out-of-range offset
 
+e
+4
+1 4 7 10
+'b
+b  c
+$0
+t
+[3]=abcde r s t u v
+e
+9
index 99c5da7..b22cb7d 100644 (file)
@@ -177,3 +177,14 @@ seven
 negative offset 2 - expect seven
 seven
 out-of-range offset
+
+e
+4
+1 4 7 10
+'b
+b  c
+$0
+t
+[3]=abcde r s t u v
+e
+9
index 4f5d830..20eaf20 100644 (file)
@@ -236,6 +236,9 @@ echo "value = ${barray[*]}"
 set -u
 ( echo ${#narray[4]} )
 
+${THIS_SH} ./array1.sub
+${THIS_SH} ./array2.sub
+
 # some old bugs and ksh93 compatibility tests
 set +u
 cd /tmp
@@ -332,3 +335,62 @@ echo ${av[@]: -1:2}
 
 echo out-of-range offset
 echo ${av[@]:12}
+
+# parsing problems and other inconsistencies not fixed until post bash-3.0
+unset x
+declare -a x=(')' $$)
+[ ${x[1]} -eq $$ ] || echo bad
+
+unset x
+declare -a x=(a b c d e)
+echo ${x[4]}
+
+z=([1]=one [4]=four [7]=seven [10]=ten)
+
+echo ${#z[@]}
+
+echo ${!z[@]}
+
+unset x
+declare -a x=(a \'b  c\')
+
+echo "${x[1]}"
+
+unset x
+declare -a x=(a 'b  c')
+
+echo "${x[1]}"
+
+unset x
+declare -a x=($0)
+[ "${x[@]}" = $0 ] || echo double expansion of \$0
+declare -a x=(\$0)
+echo "${x[@]}"
+
+: ${TMPDIR:=/tmp}
+
+mkdir $TMPDIR/bash-test-$$
+cd $TMPDIR/bash-test-$$
+
+trap "cd / ; rm -rf $TMPDIR/bash-test/$$" 0 1 2 3 6 15
+
+touch '[3]=abcde'
+
+touch r s t u v
+
+declare -a x=(*)
+
+echo ${x[3]}
+echo ${x[@]}
+
+unset x
+x=(a b c d e)
+
+echo ${x[*]: -1}
+
+unset x[4]
+unset x[2]
+
+x[9]='9'
+
+echo ${x[*]: -1}
index 4f5d830..0d63878 100644 (file)
@@ -332,3 +332,65 @@ echo ${av[@]: -1:2}
 
 echo out-of-range offset
 echo ${av[@]:12}
+
+# parsing problems and other inconsistencies not fixed until post bash-3.0
+unset x
+declare -a x=(')' $$)
+[ ${x[1]} -eq $$ ] || echo bad
+
+unset x
+declare -a x=(a b c d e)
+echo ${x[4]}
+
+z=([1]=one [4]=four [7]=seven [10]=ten)
+
+echo ${#z[@]}
+
+echo ${!z[@]}
+
+unset x
+declare -a x=(a \'b  c\')
+
+echo "${x[1]}"
+
+unset x
+declare -a x=(a 'b  c')
+
+echo "${x[1]}"
+
+unset x
+declare -a x=($0)
+[ "${x[@]}" = $0 ] || echo double expansion of \$0
+declare -a x=(\$0)
+echo "${x[@]}"
+
+: ${TMPDIR:=/tmp}
+
+mkdir $TMPDIR/bash-test-$$
+cd $TMPDIR/bash-test-$$
+
+trap "cd / ; rm -rf $TMPDIR/bash-test/$$" 0 1 2 3 6 15
+
+touch '[3]=abcde'
+
+touch r s t u v
+
+declare -a x=(*)
+
+echo ${x[3]}
+echo ${x[@]}
+
+unset x
+x=(a b c d e)
+
+echo ${x[*]: -1}
+
+unset x[4]
+unset x[2]
+
+x[9]='9'
+
+echo ${x[*]: -1}
+
+${THIS_SH} ./array1.sub
+${THIS_SH} ./array2.sub
diff --git a/tests/array1.sub b/tests/array1.sub
new file mode 100644 (file)
index 0000000..86e9332
--- /dev/null
@@ -0,0 +1 @@
+printf "%s\n" -a a=(a 'b  c')
diff --git a/tests/array2.sub b/tests/array2.sub
new file mode 100644 (file)
index 0000000..0e6417d
--- /dev/null
@@ -0,0 +1 @@
+declare -a ''=(a 'b c')
index fbd2624..c7d42dd 100644 (file)
@@ -51,3 +51,4 @@ this is ohio-state
 0
 1
 testb
+after
diff --git a/tests/exec.right~ b/tests/exec.right~
new file mode 100644 (file)
index 0000000..fbd2624
--- /dev/null
@@ -0,0 +1,53 @@
+before exec1.sub: one two three
+calling exec1.sub
+aa bb cc dd ee
+after exec1.sub with args: 0
+
+after exec1.sub without args: 0
+./execscript: line 20: notthere: command not found
+127
+/tmp/bash: notthere: No such file or directory
+127
+/bin/sh: /bin/sh: cannot execute binary file
+126
+./execscript: line 39: /: is a directory
+126
+/: /: cannot execute binary file
+126
+./execscript: line 46: .: /: is a directory
+1
+127
+0
+this is bashenv
+./exec3.sub: line 3: /tmp/bash-notthere: No such file or directory
+./exec3.sub: line 3: exec: /tmp/bash-notthere: cannot execute: No such file or directory
+126
+./execscript: line 68: notthere: No such file or directory
+127
+./execscript: line 71: notthere: No such file or directory
+127
+./execscript: line 74: notthere: No such file or directory
+127
+this is sh
+this is sh
+unset
+ok
+5
+./exec5.sub: line 4: exec: bash-notthere: not found
+127
+this is ohio-state
+0
+1
+1
+0
+42
+42
+0
+1
+1
+0
+0
+1
+0
+1
+testb
index 61722f2..09a4ba5 100644 (file)
@@ -104,3 +104,7 @@ ${THIS_SH} ./exec6.sub
 
 # checks for properly deciding what constitutes an executable file
 ${THIS_SH} ./exec7.sub
+
+true | `echo true` &
+
+echo after
diff --git a/tests/execscript~ b/tests/execscript~
new file mode 100644 (file)
index 0000000..61722f2
--- /dev/null
@@ -0,0 +1,106 @@
+export LC_ALL=C
+export LANG=C
+
+if [ $UID -eq 0 ]; then
+        echo "execscript: the test suite should not be run as root" >&2
+fi
+
+set -- one two three
+echo before exec1.sub: "$@"
+echo calling exec1.sub
+./exec1.sub aa bb cc dd ee
+echo after exec1.sub with args: $?
+./exec1.sub
+echo after exec1.sub without args: $?
+
+# set up a fixed path so we know notthere will not be found
+PATH=/usr/bin:/bin:/usr/local/bin:
+export PATH
+
+notthere
+echo $?
+
+# this is iffy, since the error messages may vary from system to system
+# and /tmp might not exist
+ln -s ${THIS_SH} /tmp/bash 2>/dev/null
+if [ -f /tmp/bash ]; then
+       /tmp/bash notthere
+else
+       ${THIS_SH} notthere
+fi
+echo $?
+rm -f /tmp/bash
+
+# /bin/sh should be there on all systems
+${THIS_SH} /bin/sh
+echo $?
+
+# try executing a directory
+/
+echo $?
+
+${THIS_SH} /
+echo $?
+
+# try sourcing a directory
+. /
+echo $?
+
+# try sourcing a binary file -- post-2.04 versions don't do the binary file
+# check, and will probably fail with `command not found', or status 127
+. ${THIS_SH} 2>/dev/null
+echo $?
+
+# post-bash-2.05 versions allow sourcing non-regular files
+. /dev/null
+echo $?
+
+# kill two birds with one test -- test out the BASH_ENV code
+echo echo this is bashenv > /tmp/bashenv
+export BASH_ENV=/tmp/bashenv
+${THIS_SH} ./exec3.sub
+rm -f /tmp/bashenv
+unset BASH_ENV
+
+# we're resetting the $PATH to empty, so this should be last
+PATH=
+
+notthere
+echo $?
+
+command notthere
+echo $?
+
+command -p notthere
+echo $?
+
+# but -p should guarantee that we find all the standard utilities, even
+# with an empty or unset $PATH
+command -p sh -c 'echo this is $0'
+unset PATH
+command -p sh -c 'echo this is $0'
+
+# a bug in bash before bash-2.01 caused PATH to be set to the empty string
+# when command -p was run with PATH unset
+echo ${PATH-unset}
+
+echo "echo ok" | ${THIS_SH} -t
+
+${THIS_SH} ./exec2.sub
+echo $?
+
+${THIS_SH} ./exec4.sub
+
+# try exec'ing a command that cannot be found in $PATH
+${THIS_SH} ./exec5.sub
+
+# this was a bug in bash versions before bash-2.04
+${THIS_SH} -c 'cat </dev/null | cat >/dev/null' >&-
+
+# checks for proper return values in subshell commands with inverted return
+# values
+
+${THIS_SH} ./exec6.sub
+
+# checks for properly deciding what constitutes an executable file
+${THIS_SH} ./exec7.sub
index f8512c3..884b5a6 100644 (file)
@@ -372,3 +372,9 @@ a="a b c d e"
 declare b=$a
 expect '<a> <b> <c> <d> <e>'
 recho $b
+
+a="a?b?c"
+
+echo ${a//\\?/ }
+
+echo ${a//\?/ }
diff --git a/tests/exp-tests~ b/tests/exp-tests~
new file mode 100644 (file)
index 0000000..f8512c3
--- /dev/null
@@ -0,0 +1,374 @@
+#
+# A suite of tests for bash word expansions
+#
+# This tests parameter and variable expansion, with an empahsis on
+# proper quoting behavior.
+#
+# Chet Ramey
+
+#
+# If you comment out the body of this function, you can do a diff against
+# `expansion-tests.right' to see if the shell is behaving correctly
+#
+expect()
+{
+       echo expect "$@"
+}
+
+# Test the substitution quoting characters (CTLESC and CTLNUL) in different
+# combinations
+
+expect "<^A>"
+recho `echo '\ 1'`
+expect "<^A>"
+recho `echo "\ 1"`
+expect "<^B>"
+recho `echo '\ 2'`
+expect "<^B>"
+recho `echo "\ 2"`
+expect "<^A>"
+recho `echo \ 1`
+expect "<^B>"
+recho `echo \ 2`
+
+# Test null strings without variable expansion
+expect "<abcdefgh>"
+recho abcd""efgh
+expect "<abcdefgh>"
+recho abcd''efgh
+expect "<abcdefgh>"
+recho ""abcdefgh
+expect "<abcdefgh>"
+recho ''abcdefgh
+expect "<abcd>"
+recho abcd""
+expect "<abcd>"
+recho abcd''
+
+# Test the quirky behavior of $@ in ""
+expect nothing
+recho "$@"
+expect "< >"
+recho " $@"
+expect "<-->"
+recho "-${@}-"
+
+# Test null strings with variable expansion that fails
+expect '<>'
+recho $xxx""
+expect '<>'
+recho ""$xxx
+expect '<>'
+recho $xxx''
+expect '<>'
+recho ''$xxx
+expect '<>'
+recho $xxx""$yyy
+expect '<>'
+recho $xxx''$yyy
+
+# Test null strings with variable expansion that succeeds
+xxx=abc
+yyy=def
+
+expect '<abc>'
+recho $xxx""
+expect '<abc>'
+recho ""$xxx
+expect '<abc>'
+recho $xxx''
+expect '<abc>'
+recho ''$xxx
+expect '<abcdef>'
+recho $xxx""$yyy
+expect '<abcdef>'
+recho $xxx''$yyy
+
+unset xxx yyy
+
+# Test the unquoted special quoting characters
+expect "<^A>"
+recho \ 1
+expect "<^B>"
+recho \ 2
+expect "<^A>"
+recho "\ 1"
+expect "<^B>"
+recho "\ 2"
+expect "<^A>"
+recho '\ 1'
+expect "<^B>"
+recho '\ 2'
+
+# Test expansion of a variable that is unset
+expect nothing
+recho $xxx
+expect '<>'
+recho "$xxx"
+
+expect nothing
+recho "$xxx${@}"
+
+# Test empty string expansion
+expect '<>'
+recho ""
+expect '<>'
+recho ''
+
+# Test command substitution with (disabled) history substitution
+expect '<Hello World!>'
+# set +H
+recho "`echo \"Hello world!\"`"
+
+# Test some shell special characters
+expect '<`>'
+recho "\`"
+expect '<">'
+recho "\""
+expect '<\^A>'
+recho "\\ 1"
+
+expect '<\$>'
+recho "\\$"
+
+expect '<\\>'
+recho "\\\\"
+
+# This should give argv[1] = a argv[2] = b
+expect '<a> <b>'
+FOO=`echo 'a b' | tr ' ' '\012'`
+recho $FOO
+
+# This should give argv[1] = ^A argv[2] = ^B
+expect '<^A> <^B>'
+FOO=`echo '\ 1 \ 2' | tr ' ' '\012'`
+recho $FOO
+
+# Test quoted and unquoted globbing characters
+expect '<**>'
+recho "*"*
+
+expect '<\.\./*/>'
+recho "\.\./*/"
+
+# Test patterns that come up when the shell quotes funny character
+# combinations
+expect '<^A^B^A^B>'
+recho '\ 1\ 2\ 1\ 2'
+expect '<^A^A>'
+recho '\ 1\ 1'
+expect '<^A^B>'
+recho '\ 1\ 2'
+expect '<^A^A^B>'
+recho '\ 1\ 1\ 2'
+
+# More tests of "$@"
+set abc def ghi jkl
+expect '<  abc> <def> <ghi> <jkl  >'
+recho "  $@  "
+expect '<  abc> <def> <ghi> <jkl  >'
+recho "${1+  $@  }"
+
+set abc def ghi jkl
+expect '<--abc> <def> <ghi> <jkl-->'
+recho "--$@--"
+
+set "a b" cd ef gh
+expect '<a b> <cd> <ef> <gh>'
+recho ${1+"$@"}
+expect '<a b> <cd> <ef> <gh>'
+recho ${foo:-"$@"}
+expect '<a b> <cd> <ef> <gh>'
+recho "${@}"
+
+expect '<  >'
+recho "  "
+expect '< - >'
+recho " - "
+
+# Test combinations of different types of quoting in a fully-quoted string
+# (so the WHOLLY_QUOTED tests fail and it doesn't get set)
+expect '</^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/>'
+recho "/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*"'$'"/\1/"
+
+# Test the various Posix parameter expansions
+
+expect '<foo bar>'
+recho "${x:-$(echo "foo bar")}"
+expect '<foo> <bar>'
+recho ${x:-$(echo "foo bar")}
+
+unset X
+expect '<abc>'
+recho ${X:=abc}
+expect '<abc>'
+recho $X
+
+set a b c
+expect '<posix>'
+recho ${3:+posix}
+
+POSIX=/usr/posix
+expect '<10>'
+recho ${#POSIX}
+
+# remove shortest trailing match
+x=file.c
+expect '<file.o>'
+recho ${x%.c}.o
+
+# remove longest trailing match
+x=posix/src/std
+expect '<posix>'
+recho ${x%%/*}
+
+# remove shortest leading pattern
+x=$HOME/src/cmd
+expect '</src/cmd>'
+recho ${x#$HOME}
+
+# remove longest leading pattern
+x=/one/two/three
+expect '<three>'
+recho ${x##*/}
+
+# pattern removal of patterns that don't match
+z=abcdef
+
+expect '<abcdef>'
+recho ${z#xyz}
+expect '<abcdef>'
+recho ${z##xyz}
+
+expect '<abcdef>'
+recho ${z%xyz}
+expect '<abcdef>'
+recho ${z%%xyz}
+
+# Command substitution and the quirky differences between `` and $()
+
+expect '<\$x>'
+recho '\$x'
+
+expect '<$x>'
+recho `echo '\$x'`
+
+expect '<\$x>'
+recho $(echo '\$x')
+
+# The difference between $* "$*" and "$@"
+
+set "abc" "def ghi" "jkl"
+
+expect '<abc> <def> <ghi> <jkl>'
+recho $*
+
+expect '<abc def ghi jkl>'
+recho "$*"
+
+OIFS="$IFS"
+IFS=":$IFS"
+
+# The special behavior of "$*", using the first character of $IFS as separator
+expect '<abc:def ghi:jkl>'
+recho "$*"
+
+IFS="$OIFS"
+
+expect '<abc> <def ghi> <jkl>'
+recho "$@"
+
+expect '<xxabc> <def ghi> <jklyy>'
+recho "xx$@yy"
+
+expect '<abc> <def ghi> <jklabc> <def ghi> <jkl>'
+recho "$@$@"
+
+foo=abc
+bar=def
+
+expect '<abcdef>'
+recho "$foo""$bar"
+
+unset foo
+set $foo bar '' xyz "$foo" abc
+
+expect '<bar> <> <xyz> <> <abc>'
+recho "$@"
+
+# More tests of quoting and deferred evaluation
+
+foo=10 x=foo
+y='$'$x
+expect '<$foo>'
+recho $y
+eval y='$'$x
+expect '<10>'
+recho $y
+
+# case statements
+
+NL='
+'
+x='ab
+cd'
+
+expect '<newline expected>'
+case "$x" in
+*$NL*) recho "newline expected" ;;
+esac
+
+expect '<got it>'
+case \? in
+*"?"*) recho "got it" ;;
+esac
+
+expect '<got it>'
+case \? in
+*\?*) recho "got it" ;;
+esac
+
+set one two three four five
+expect '<one> <three> <five>'
+recho $1 $3 ${5} $8 ${9}
+
+# length tests on positional parameters and some special parameters
+
+expect '<5> <5>'
+recho $# ${#}
+expect '<3>'
+recho ${#1}
+expect '<1>'
+recho ${##}
+expect '<1>'
+recho ${#?}
+expect '<5>'
+recho ${#@}
+expect '<5>'
+recho ${#*}
+expect '<5>'
+recho "${#@}"
+expect '<5>'
+recho "${#*}"
+
+expect '<42>'
+recho $((28 + 14))
+expect '<26>'
+recho $[ 13 * 2 ]
+
+expect '<\>'
+recho `echo \\\\`
+
+expect '<~>'
+recho '~'
+
+expect nothing
+recho $!
+expect nothing
+recho ${!}
+
+# test word splitting of assignment statements not preceding a command
+a="a b c d e"
+declare b=$a
+expect '<a> <b> <c> <d> <e>'
+recho $b
index 747c80e..fdadbd9 100644 (file)
@@ -143,3 +143,5 @@ argv[2] = <b>
 argv[3] = <c>
 argv[4] = <d>
 argv[5] = <e>
+a?b?c
+a b c
index f677f96..db9447e 100644 (file)
@@ -24,3 +24,4 @@ match 15
 match 16
 match 17
 match 18
+ok 19
diff --git a/tests/extglob3.right~ b/tests/extglob3.right~
new file mode 100644 (file)
index 0000000..f677f96
--- /dev/null
@@ -0,0 +1,26 @@
+match 1
+match 2
+match 3
+match 4
+match 1a
+match 1b
+match 2a
+match 2b
+match 3a
+match 3b
+match 4a
+match 4b
+match 5
+match 6
+match 7
+match 8
+match 9
+match 10
+match 11
+match 12
+match 13
+match 14
+match 15
+match 16
+match 17
+match 18
index e77edd2..60454a2 100644 (file)
@@ -51,3 +51,6 @@ shopt -s extglob
 [[ ab/../ == +(??|a*)/..?(/) ]] && echo match 17
 
 [[ ab/../ == +(a*)/..?(/) ]] && echo match 18
+
+#
+j="@(x)" ; [[ x == $j ]] && echo ok 19
diff --git a/tests/extglob3.tests~ b/tests/extglob3.tests~
new file mode 100644 (file)
index 0000000..e77edd2
--- /dev/null
@@ -0,0 +1,53 @@
+shopt -s extglob
+
+[[ ab/../ == @(ab|+([^/]))/..?(/) ]] && echo match 1
+
+[[ ab/../ == +([^/])/..?(/) ]] && echo match 2
+
+[[ ab/../ == @(ab|?b)/..?(/) ]] && echo match 3
+
+[[ ab/../ == +([^/])/../ ]] && echo match 4
+
+[[ ab/../ == +([!/])/..?(/) ]] && echo match 1a
+
+[[ ab/../ == @(ab|+([!/]))/..?(/) ]] && echo match 1b
+
+[[ ab/../ == +([!/])/../ ]] && echo match 2a
+
+[[ ab/../ == +([!/])/..?(/) ]] && echo match 2b
+
+[[ ab/../ == +([!/])/..@(/) ]] && echo match 3a
+
+[[ ab/../ == +(ab)/..?(/) ]] && echo match 3b
+
+[[ ab/../ == [!/][!/]/../ ]] && echo match 4a
+
+[[ ab/../ == @(ab|?b)/..?(/) ]] && echo match 4b
+
+[[ ab/../ == [^/][^/]/../ ]] && echo match 5
+
+[[ ab/../ == ?b/..?(/) ]] && echo match 6
+
+[[ ab/../ == +(?b)/..?(/) ]] && echo match 7
+
+[[ ab/../ == +(?b|?b)/..?(/) ]] && echo match 8
+
+[[ ab/../ == @(?b|?b)/..?(/) ]] && echo match 9
+
+[[ ab/../ == @(a?|?b)/..?(/) ]] && echo match 10
+
+[[ ab/../ == ?(ab)/..?(/) ]] && echo match 11
+
+[[ ab/../ == ?(ab|??)/..?(/) ]] && echo match 12
+
+[[ ab/../ == @(??)/..?(/) ]] && echo match 13
+
+[[ ab/../ == @(??|a*)/..?(/) ]] && echo match 14
+
+[[ ab/../ == @(a*)/..?(/) ]] && echo match 15
+
+[[ ab/../ == +(??)/..?(/) ]] && echo match 16
+
+[[ ab/../ == +(??|a*)/..?(/) ]] && echo match 17
+
+[[ ab/../ == +(a*)/..?(/) ]] && echo match 18
diff --git a/tests/intl.right b/tests/intl.right
new file mode 100644 (file)
index 0000000..bf0dd93
--- /dev/null
@@ -0,0 +1,10 @@
+1
+AéB
+B
+B
+ok 1
+ok 2
+aéb
+0000000  141 303 251 142                                                
+0000004
diff --git a/tests/intl.tests b/tests/intl.tests
new file mode 100644 (file)
index 0000000..3a9111a
--- /dev/null
@@ -0,0 +1,38 @@
+LANG=en_US.UTF-8
+
+a=$'\303\251'
+
+echo "$a"
+
+echo ${#a}
+
+b=$'A\303\251B'
+
+echo "$b"
+
+echo ${b: -1}
+
+c=AeB
+
+echo ${c: -1}
+
+unset a
+a=$(printf '%b' 'A\303\251B')
+IFS=$(printf '%b' '\303\251')
+
+case "$a" in
+"A${IFS}B")    echo ok 1 ;;
+*)             echo bad 1 ;;
+esac
+
+set $a
+
+case $1 in
+A)             echo ok 2 ;;
+*)             echo bad 2 ;;
+esac
+
+set a b
+
+printf '%s\n' "$*"
+printf '%s' "$*" | od -b
diff --git a/tests/intl.tests~ b/tests/intl.tests~
new file mode 100644 (file)
index 0000000..7e3cb37
--- /dev/null
@@ -0,0 +1,38 @@
+LANG=en_US.UTF-8
+
+a=$'\303\251'
+
+echo "$a"
+
+echo ${#a}
+
+b=$'A\303\251B'
+
+echo "$b"
+
+echo ${b: -1}
+
+c=AeB
+
+echo ${c: -1}
+
+unset a
+a=$(printf '%b' 'A\303\251B')
+IFS=$(printf '%b' '\303\251')
+
+case "$a" in
+"A${IFS}B")    echo yes ;;
+*)             echo no ;;
+esac
+
+set $a
+
+case $1 in
+A)             echo ok 2 ;;
+*)             echo bad 2 ;;
+esac
+
+set a b
+
+printf '%s\n' "$*"
+printf '%s' "$*" | od -b
index 54b05f8..e7ea3c7 100644 (file)
@@ -94,3 +94,5 @@ after read
 0
 0
 0
+before block
+after block
index 19cf9a1..b52d613 100644 (file)
@@ -156,3 +156,18 @@ ${THIS_SH} ./redir5.sub
 
 # test behavior after a write error with a builtin command
 ${THIS_SH} ./redir6.sub
+
+# problem with redirections using fds bash uses internally
+: ${TMPDIR:=/tmp}
+
+trap 'rm -f $TMPDIR/bash-redir-$$' 0 1 2 3 6 15
+
+echo before block
+{
+       echo before redir
+       exec 10>&1
+       echo after redir
+} > $TMPDIR/bash-redir-$$
+
+echo after block
+
diff --git a/tests/redir.tests~ b/tests/redir.tests~
new file mode 100644 (file)
index 0000000..19cf9a1
--- /dev/null
@@ -0,0 +1,158 @@
+export LC_ALL=C
+export LANG=C
+
+# catch-all for remaining untested redirection stuff
+set +o posix
+
+echo abc > /tmp/redir-test
+cat /tmp/redir-test
+
+set -o noclobber
+
+#this should be an error
+echo def > /tmp/redir-test
+cat /tmp/redir-test
+
+# but this should succeed
+echo def > /tmp/redir-test-2
+cat /tmp/redir-test-2
+
+# and so should this
+echo def >| /tmp/redir-test
+cat /tmp/redir-test
+
+set +o noclobber
+rm /tmp/redir-test /tmp/redir-test-2
+
+# this should be an error
+z="a b"
+cat < $z
+
+echo "Point 1"
+
+exec 3</etc/passwd
+exec 4>/tmp/bash-a
+exec 5>/tmp/bash-b
+echo "Point 2"
+
+echo to a 1>&4
+echo to b 1>&5
+cat /tmp/bash-a
+cat /tmp/bash-b
+exec 11</dev/null
+echo "Point 3"
+
+echo to a 1>&4
+echo to b 1>&5
+cat /tmp/bash-a
+cat /tmp/bash-b
+
+exec 11<&-
+echo "Point 4"
+
+exec 6<>/tmp/bash-c
+echo to c 1>&6
+cat /tmp/bash-c
+echo "Point 5"
+
+rm -f /tmp/bash-a /tmp/bash-b /tmp/bash-c
+
+#
+# Test the effect of input buffering on the shell's input
+#
+${THIS_SH} < redir1.sub
+
+# more open, close, duplicate file descriptors
+${THIS_SH} ./redir3.sub < ./redir3.in1
+
+# still more redirections
+${THIS_SH} ./redir4.sub < redir4.in1
+
+# various forms of null redirection
+testf()
+{
+       if [ -f "$1" ]; then
+               rm -f "$1"
+       else
+               echo oops -- $1 not found
+       fi
+}
+
+> /tmp/null-redir-a
+testf /tmp/null-redir-a
+
+$EXIT > /tmp/null-redir-b
+testf /tmp/null-redir-b
+
+( > /tmp/null-redir-c )
+testf /tmp/null-redir-c
+
+$EXIT > /tmp/null-redir-d &
+wait
+testf /tmp/null-redir-d
+
+exit 3 | $EXIT > /tmp/null-redir-e
+echo $? -- ${PIPESTATUS[@]}
+testf /tmp/null-redir-e
+
+exit 4 | > /tmp/null-redir-f
+echo $? -- ${PIPESTATUS[@]}
+testf /tmp/null-redir-f
+
+> /tmp/null-redir-g &
+wait
+testf /tmp/null-redir-g
+
+exec >/tmp/null-redir-h &
+wait
+testf /tmp/null-redir-h
+
+# make sure async commands don't get /dev/null as stdin when an explicit
+# input redirection is supplied
+for x in 1 2 3; do
+       { read line ; echo $line ; } &
+       wait
+       { read line ; echo $line ; } &
+       wait
+done << EOF
+ab
+cd
+ef
+gh
+ij
+kl
+EOF
+
+# make sure async commands get /dev/null as stdin in the absence of any
+# input redirection
+/bin/cat &
+wait
+echo $?
+
+# make sure that loops work OK with here documents and are not run in
+# subshells
+while read line; do
+       echo $line
+       l2=$line
+done << EOF
+ab
+cd
+EOF
+echo $l2
+
+# These should not echo anything -- bug in versions before 2.04
+( ( echo hello 1>&3 ) 3>&1 ) >/dev/null 2>&1
+
+( ( echo hello 1>&3 ) 3>&1 ) >/dev/null 2>&1 | cat
+
+# in posix mode, non-interactive shells are not allowed to perform
+# filename expansion on input redirections, even if they expand to
+# a single filename
+set -o posix
+cat < redir1.*
+
+# test ksh93 dup-and-close (move fd) redirections
+${THIS_SH} ./redir5.sub
+
+# test behavior after a write error with a builtin command
+${THIS_SH} ./redir6.sub
diff --git a/tests/run-intl b/tests/run-intl
new file mode 100644 (file)
index 0000000..48b1c83
--- /dev/null
@@ -0,0 +1,7 @@
+# See whether or not we can use `diff -a'
+( diff -a ./intl.right ./intl.right >/dev/null 2>&1 ) && AFLAG=-a
+
+echo "warning: some of these tests will fail if you do not have UTF-8" >&2
+echo "warning: locales installed on your system." >&2
+${THIS_SH} ./intl.tests > /tmp/xx
+diff $AFLAG /tmp/xx intl.right && rm -f /tmp/xx
diff --git a/tests/run-intl~ b/tests/run-intl~
new file mode 100644 (file)
index 0000000..c31a379
--- /dev/null
@@ -0,0 +1,4 @@
+echo "warning: some of these tests will fail if you do not have UTF-8" >&2
+echo "warning: locales installed on your system." >&2
+${THIS_SH} ./intl.tests > /tmp/xx
+diff /tmp/xx intl.right && rm -f /tmp/xx
index 90897fc..fce0468 100644 (file)
@@ -15,3 +15,10 @@ ok
 ~
 make -k FOO=/usr/xyz/mumble
 /usr/xyz/mumble
+HOME=~
+HOME=~
+/usr/$x/abc
+HOME=~
+/usr/$x/abc
+HOME=/usr/$x/abc
+/usr/$x/abc
index 3e2464f..ff6c76f 100644 (file)
@@ -44,3 +44,27 @@ echo make -k FOO=~/mumble
 
 typeset FOO=~/mumble
 echo "$FOO"
+
+h=HOME=~
+echo $h
+
+export h=HOME=~
+echo $h
+
+x=1234
+HOME='/usr/$x/abc'
+
+echo ~
+
+# behavior differs here in posix mode
+set -o posix
+
+eval echo $h
+eval $h
+echo $HOME
+
+set +o posix
+
+eval echo $h
+eval $h
+echo $HOME
diff --git a/tests/tilde2.tests~ b/tests/tilde2.tests~
new file mode 100644 (file)
index 0000000..e569727
--- /dev/null
@@ -0,0 +1,62 @@
+HOME=/usr/xyz
+XPATH=/bin:/usr/bin:.
+
+ADDPATH=PATH=~/bin:$XPATH
+
+echo $ADDPATH
+
+unset ADDPATH
+: ${ADDPATH:=~/bin:~/bin2:$XPATH}
+echo $ADDPATH
+
+unset ADDPATH
+: ${ADDPATH:=PATH=~/bin:~/bin2:$XPATH}
+echo $ADDPATH
+
+cat << !
+~/bin
+!
+
+echo "~"
+
+echo ${TPATH:-~}
+echo "${TPATH:-~}"
+echo "${TPATH:-"~"}"
+
+echo "${XPATH+~}"
+
+recho "\a"
+recho "${TPATH:-\a}"
+
+SHELL=~/bash
+echo $SHELL
+
+case $SHELL in
+~/bash)        echo ok;;
+*)     echo bad;;
+esac
+
+somevar=
+echo "${somevar:-~}"
+echo "${somevar:-"~"}"
+
+echo make -k FOO=~/mumble
+
+typeset FOO=~/mumble
+echo "$FOO"
+
+h=HOME=~
+echo $h
+
+export h=HOME=~
+echo $h
+
+x=1234
+HOME='/usr/$x/abc'
+
+echo ~
+
+eval echo $h
+eval $h
+
+echo $HOME