aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJari Aalto <jari.aalto@cante.net>1998-04-17 19:52:44 +0000
committerJari Aalto <jari.aalto@cante.net>2009-09-12 16:46:51 +0000
commitcce855bc5b117cb7ae70064131120687bc69fac0 (patch)
tree39c7a4ec8f6d22ef03df74f2684e6a04fef10399 /lib
parente8ce775db824de329b81293b4e5d8fbd65624528 (diff)
downloadandroid_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.tar.gz
android_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.tar.bz2
android_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.zip
Imported from ../bash-2.02.tar.gz.
Diffstat (limited to 'lib')
-rw-r--r--lib/glob/collsyms.h129
-rw-r--r--lib/glob/fnmatch.c816
-rw-r--r--lib/glob/fnmatch.h22
-rw-r--r--lib/glob/glob.c285
-rw-r--r--lib/glob/glob.h1
-rw-r--r--lib/malloc/getpagesize.h7
-rw-r--r--lib/malloc/gmalloc.c1213
-rw-r--r--lib/malloc/malloc.c957
-rw-r--r--lib/malloc/ogmalloc.c1579
-rw-r--r--lib/malloc/omalloc.c759
-rw-r--r--lib/readline/Makefile.in2
-rw-r--r--lib/readline/bind.c291
-rw-r--r--lib/readline/callback.c8
-rw-r--r--lib/readline/chardefs.h15
-rw-r--r--lib/readline/complete.c824
-rw-r--r--lib/readline/display.c4
-rw-r--r--lib/readline/doc/hist.texinfo17
-rw-r--r--lib/readline/doc/hsuser.texinfo122
-rw-r--r--lib/readline/doc/rlman.texinfo17
-rw-r--r--lib/readline/doc/rltech.texinfo10
-rw-r--r--lib/readline/doc/rluser.texinfo247
-rw-r--r--lib/readline/examples/rltest.c23
-rw-r--r--lib/readline/funmap.c4
-rw-r--r--lib/readline/histexpand.c46
-rw-r--r--lib/readline/histfile.c49
-rw-r--r--lib/readline/history.c3
-rw-r--r--lib/readline/histsearch.c3
-rw-r--r--lib/readline/isearch.c17
-rw-r--r--lib/readline/keymaps.h4
-rw-r--r--lib/readline/kill.c98
-rw-r--r--lib/readline/nls.c3
-rw-r--r--lib/readline/parens.c2
-rw-r--r--lib/readline/readline.c18
-rw-r--r--lib/readline/readline.h14
-rw-r--r--lib/readline/rltty.h12
-rw-r--r--lib/readline/rlwinsize.h58
-rw-r--r--lib/readline/shell.c9
-rw-r--r--lib/readline/terminal.c23
-rw-r--r--lib/readline/tilde.c3
-rw-r--r--lib/readline/util.c62
-rw-r--r--lib/readline/vi_mode.c7
-rw-r--r--lib/sh/Makefile.in164
-rw-r--r--lib/sh/clktck.c55
-rw-r--r--lib/sh/getcwd.c319
-rw-r--r--lib/sh/getenv.c99
-rw-r--r--lib/sh/itos.c64
-rw-r--r--lib/sh/oslib.c233
-rw-r--r--lib/sh/setlinebuf.c41
-rw-r--r--lib/sh/strcasecmp.c88
-rw-r--r--lib/sh/strerror.c74
-rw-r--r--lib/sh/strtod.c197
-rw-r--r--lib/sh/strtol.c213
-rw-r--r--lib/sh/strtoul.c26
-rw-r--r--lib/sh/vprint.c85
-rw-r--r--lib/termcap/termcap.c6
-rw-r--r--lib/tilde/tilde.c3
56 files changed, 7547 insertions, 1903 deletions
diff --git a/lib/glob/collsyms.h b/lib/glob/collsyms.h
new file mode 100644
index 0000000..4f90083
--- /dev/null
+++ b/lib/glob/collsyms.h
@@ -0,0 +1,129 @@
+/* collsyms.h -- collating symbol names and their corresponding characters
+ (in ascii) as given by POSIX.2 in table 2.8. */
+
+/* Copyright (C) 1997 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef _COLLSYMS_H_
+# define _COLLSYSMS_H_
+
+/* The upper-case letters, lower-case letters, and digits are omitted from
+ this table. The digits are not included in the table in the POSIX.2
+ spec. The upper and lower case letters are translated by the code
+ in fnmatch.c:collsym(). */
+
+typedef struct _collsym {
+ char *name;
+ char code;
+} COLLSYM;
+
+static COLLSYM posix_collsyms[] =
+{
+ "NUL", '\0',
+ "SOH", '\001',
+ "STX", '\002',
+ "ETX", '\003',
+ "EOT", '\004',
+ "ENQ", '\005',
+ "ACK", '\006',
+#ifdef __STDC__
+ "alert", '\a',
+#else
+ "alert", '\007',
+#endif
+ "backspace", '\b',
+ "tab", '\t',
+ "newline", '\n',
+ "vertical-tab", '\v',
+ "form-feed", '\f',
+ "carriage-return", '\r',
+ "SO", '\016',
+ "SI", '\017',
+ "DLE", '\020',
+ "DC1", '\021',
+ "DC2", '\022',
+ "DC3", '\023',
+ "DC4", '\024',
+ "NAK", '\025',
+ "SYN", '\026',
+ "ETB", '\027',
+ "CAN", '\030',
+ "EM", '\031',
+ "SUB", '\032',
+ "ESC", '\033',
+ "IS4", '\034',
+ "IS3", '\035',
+ "IS2", '\036',
+ "IS1", '\037',
+ "space", ' ',
+ "exclamation-mark", '!',
+ "quotation-mark", '"',
+ "number-sign", '#',
+ "dollar-sign", '$',
+ "percent-sign", '%',
+ "ampersand", '&',
+ "apostrophe", '\'',
+ "left-parenthesis", '(',
+ "right-parenthesis", ')',
+ "asterisk", '*',
+ "plus-sign", '+',
+ "comma", ',',
+ "hyphen", '-',
+ "minus", '-', /* extension from POSIX.2 */
+ "dash", '-', /* extension from POSIX.2 */
+ "period", '.',
+ "slash", '/',
+ "solidus", '/', /* extension from POSIX.2 */
+ "zero", '0',
+ "one", '1',
+ "two", '2',
+ "three", '3',
+ "four", '4',
+ "five", '5',
+ "six", '6',
+ "seven", '7',
+ "eight", '8',
+ "nine", '9',
+ "colon", ':',
+ "semicolon", ';',
+ "less-than-sign", '<',
+ "equals-sign", '=',
+ "greater-than-sign", '>',
+ "question-mark", '?',
+ "commercial-at", '@',
+ /* upper-case letters omitted */
+ "left-square-bracket",'[',
+ "backslash", '\\',
+ "reverse-solidus", '\\',
+ "right-square-bracket", ']',
+ "circumflex", '^',
+ "circumflex-accent", '^', /* extension from POSIX.2 */
+ "underscore", '_',
+ "grave-accent", '`',
+ /* lower-case letters omitted */
+ "left-brace", '{', /* extension from POSIX.2 */
+ "left-curly-bracket", '{',
+ "vertical-line", '|',
+ "right-brace", '}', /* extension from POSIX.2 */
+ "right-curly-bracket", '}',
+ "tilde", '~',
+ "DEL", '\177',
+ 0, 0,
+};
+
+#endif
diff --git a/lib/glob/fnmatch.c b/lib/glob/fnmatch.c
index 2f8eb24..b5fdcc1 100644
--- a/lib/glob/fnmatch.c
+++ b/lib/glob/fnmatch.c
@@ -1,238 +1,738 @@
-/* Copyright (C) 1991 Free Software Foundation, Inc.
-This file is part of the GNU C Library.
+/* fnmatch.c -- ksh-like extended pattern matching for the shell and filename
+ globbing. */
+
+/* Copyright (C) 1991, 1997 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <config.h>
+
+#include "fnmatch.h"
+#include "collsyms.h"
+#include <ctype.h>
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+static int gmatch ();
+static char *brackmatch ();
+#ifdef EXTENDED_GLOB
+static int extmatch ();
+#endif
+
+#if !defined (isascii)
+# define isascii(c) ((unsigned int)(c) <= 0177)
+#endif
-The GNU C 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
-Library General Public License for more details.
+/* Note that these evaluate C many times. */
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA. */
+#define ISUPPER(c) (isascii (c) && isupper (c))
+#define ISLOWER(c) (isascii (c) && islower (c))
-#include <errno.h>
-#include "fnmatch.h"
+#ifndef isblank
+# define isblank(c) ((c) == ' ' || (c) == '\t')
+#endif
-#if !defined (__GNU_LIBRARY__) && !defined (STDC_HEADERS)
-# if !defined (errno)
-extern int errno;
-# endif /* !errno */
+#ifndef isgraph
+# define isgraph(c) ((c) != ' ' && isprint((c)))
#endif
-/* Match STRING against the filename pattern PATTERN, returning zero if
- it matches, FNM_NOMATCH if not. */
+#ifndef isxdigit
+# define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
+#endif
+
+# define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
+
+#ifndef STREQ
+#define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
+#define STREQN(a, b, n) ((a)[0] == (b)[0] && strncmp(a, b, n) == 0)
+#endif
+
+#if defined (HAVE_STRCOLL)
+static int rangecmp (c1, c2)
+ int c1, c2;
+{
+ static char s1[2] = { ' ', '\0' };
+ static char s2[2] = { ' ', '\0' };
+ int ret;
+
+ /* Eight bits only. Period. */
+ c1 &= 0xFF;
+ c2 &= 0xFF;
+
+ if (c1 == c2)
+ return (0);
+
+ s1[0] = c1;
+ s2[0] = c2;
+
+ if ((ret = strcoll (s1, s2)) != 0)
+ return ret;
+ return (c1 - c2);
+}
+#else /* !HAVE_STRCOLL */
+# define rangecmp(c1, c2) ((c1) - (c2))
+#endif /* !HAVE_STRCOLL */
+
+#if defined (HAVE_STRCOLL)
+static int collequiv (c1, c2)
+ int c1, c2;
+{
+ return (rangecmp (c1, c2) == 0);
+}
+#else
+# define collequiv(c1, c2) ((c1) == (c2))
+#endif
+
+static int
+collsym (s, len)
+ char *s;
+ int len;
+{
+ register struct _collsym *csp;
+
+ for (csp = posix_collsyms; csp->name; csp++)
+ {
+ if (STREQN(csp->name, s, len) && csp->name[len] == '\0')
+ return (csp->code);
+ }
+ if (len == 1)
+ return s[0];
+ return -1;
+}
+
int
fnmatch (pattern, string, flags)
char *pattern;
char *string;
int flags;
{
- register char *p = pattern, *n = string;
- register char c;
+ char *se, *pe;
- if ((flags & ~__FNM_FLAGS) != 0)
- {
- errno = EINVAL;
- return (-1);
- }
+ if (string == 0 || pattern == 0)
+ return FNM_NOMATCH;
+
+ se = string + strlen (string);
+ pe = pattern + strlen (pattern);
+
+ return (gmatch (string, se, pattern, pe, flags));
+}
+
+/* Match STRING against the filename pattern PATTERN, returning zero if
+ it matches, FNM_NOMATCH if not. */
+static int
+gmatch (string, se, pattern, pe, flags)
+ char *string, *se;
+ char *pattern, *pe;
+ int flags;
+{
+ register char *p, *n; /* pattern, string */
+ register char c; /* current pattern character */
+ register char sc; /* current string character */
- while ((c = *p++) != '\0')
+ p = pattern;
+ n = string;
+
+ if (string == 0 || pattern == 0)
+ return FNM_NOMATCH;
+
+ while (p < pe)
{
+ c = *p++;
+ c = FOLD (c);
+
+ sc = n < se ? *n : '\0';
+
+#ifdef EXTENDED_GLOB
+ if ((flags & FNM_EXTMATCH) && *p == '(' &&
+ (c == '+' || c == '*' || c == '?' || c == '@' || c == '!')) /* ) */
+ /* extmatch () will handle recursively calling gmatch, so we can
+ just return what extmatch() returns. */
+ return (extmatch (c, n, se, p, pe, flags));
+#endif
+
switch (c)
{
- case '?':
- if (*n == '\0')
- return (FNM_NOMATCH);
- else if ((flags & FNM_PATHNAME) && *n == '/')
+ case '?': /* Match single character */
+ if (sc == '\0')
+ return FNM_NOMATCH;
+ else if ((flags & FNM_PATHNAME) && sc == '/')
/* If we are matching a pathname, `?' can never match a `/'. */
- return (FNM_NOMATCH);
- else if ((flags & FNM_PERIOD) && *n == '.' &&
+ return FNM_NOMATCH;
+ else if ((flags & FNM_PERIOD) && sc == '.' &&
(n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
/* `?' cannot match a `.' if it is the first character of the
string or if it is the first character following a slash and
we are matching a pathname. */
- return (FNM_NOMATCH);
+ return FNM_NOMATCH;
break;
- case '\\':
- if (!(flags & FNM_NOESCAPE))
+ case '\\': /* backslash escape removes special meaning */
+ if (p == pe)
+ return FNM_NOMATCH;
+
+ if ((flags & FNM_NOESCAPE) == 0)
{
c = *p++;
- if (c == '\0')
- return (FNM_NOMATCH);
+ /* A trailing `\' cannot match. */
+ if (p > pe)
+ return FNM_NOMATCH;
+ c = FOLD (c);
}
- if (*n != c)
- return (FNM_NOMATCH);
+ if (FOLD (sc) != c)
+ return FNM_NOMATCH;
break;
- case '*':
- if ((flags & FNM_PERIOD) && *n == '.' &&
+ case '*': /* Match zero or more characters */
+ if (p == pe)
+ return 0;
+
+ if ((flags & FNM_PERIOD) && sc == '.' &&
(n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
/* `*' cannot match a `.' if it is the first character of the
string or if it is the first character following a slash and
we are matching a pathname. */
- return (FNM_NOMATCH);
+ return FNM_NOMATCH;
/* Collapse multiple consecutive, `*' and `?', but make sure that
one character of the string is consumed for each `?'. */
- for (c = *p++; c == '?' || c == '*'; c = *p++)
+ for (c = *p++; (c == '?' || c == '*'); c = *p++)
{
- if ((flags & FNM_PATHNAME) && *n == '/')
+ if ((flags & FNM_PATHNAME) && sc == '/')
/* A slash does not match a wildcard under FNM_PATHNAME. */
- return (FNM_NOMATCH);
+ return FNM_NOMATCH;
else if (c == '?')
{
- if (*n == '\0')
- return (FNM_NOMATCH);
+ if (sc == '\0')
+ return FNM_NOMATCH;
/* One character of the string is consumed in matching
this ? wildcard, so *??? won't match if there are
fewer than three characters. */
n++;
+ sc = n < se ? *n : '\0';
}
+
+#ifdef EXTENDED_GLOB
+ /* Handle ******(patlist) */
+ if ((flags & FNM_EXTMATCH) && c == '*' && *p == '(') /*)*/
+ return (extmatch (c, n, se, p, pe, flags));
+#endif
+ if (p == pe)
+ break;
}
- if (c == '\0')
+ /* If we've hit the end of the pattern and the last character of
+ the pattern was handled by the loop above, we've succeeded.
+ Otherwise, we need to match that last character. */
+ if (p == pe && (c == '?' || c == '*'))
return (0);
/* General case, use recursion. */
{
- char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
- for (--p; *n != '\0'; ++n)
+ char c1;
+
+ c1 = ((flags & FNM_NOESCAPE) == 0 && c == '\\') ? *p : c;
+ c1 = FOLD (c1);
+ for (--p; n < se; ++n)
/* Only call fnmatch if the first character indicates a
possible match. */
- if ((c == '[' || *n == c1) &&
- fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
+ if ((c == '[' || FOLD (*n) == c1) &&
+ gmatch (n, se, p, pe, flags & ~FNM_PERIOD) == 0)
return (0);
- return (FNM_NOMATCH);
+ return FNM_NOMATCH;
}
case '[':
{
- /* Nonzero if the sense of the character class is inverted. */
- register int not;
-
- if (*n == '\0')
- return (FNM_NOMATCH);
+ if (sc == '\0' || n == se)
+ return FNM_NOMATCH;
/* A character class cannot match a `.' if it is the first
character of the string or if it is the first character
following a slash and we are matching a pathname. */
- if ((flags & FNM_PERIOD) && *n == '.' &&
+ if ((flags & FNM_PERIOD) && sc == '.' &&
(n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
return (FNM_NOMATCH);
- /* POSIX.2 2.8.3.1.2 says: `An expression containing a `[' that
- is not preceded by a backslash and is not part of a bracket
- expression produces undefined results.' This implementation
- treats the `[' as just a character to be matched if there is
- not a closing `]'. This code will have to be changed when
- POSIX.2 character classes are implemented. */
- {
- register char *np;
-
- for (np = p; np && *np && *np != ']'; np++)
- ;
-
- if (np && !*np)
- {
- if (*n != '[')
- return (FNM_NOMATCH);
- break;
- }
- }
-
- not = (*p == '!' || *p == '^');
- if (not)
- ++p;
-
- c = *p++;
- for (;;)
- {
- register char cstart, cend;
-
- /* Initialize cstart and cend in case `-' is the last
- character of the pattern. */
- cstart = cend = c;
-
- if (!(flags & FNM_NOESCAPE) && c == '\\')
- {
- if (*p == '\0')
- return FNM_NOMATCH;
- cstart = cend = *p++;
- }
-
- if (c == '\0')
- /* [ (unterminated) loses. */
- return (FNM_NOMATCH);
-
- c = *p++;
-
- if ((flags & FNM_PATHNAME) && c == '/')
- /* [/] can never match. */
- return (FNM_NOMATCH);
-
- /* This introduces a range, unless the `-' is the last
- character of the class. Find the end of the range
- and move past it. */
- if (c == '-' && *p != ']')
- {
- cend = *p++;
- if (!(flags & FNM_NOESCAPE) && cend == '\\')
- cend = *p++;
- if (cend == '\0')
- return (FNM_NOMATCH);
-
- c = *p++;
- }
-
- if (*n >= cstart && *n <= cend)
- goto matched;
-
- if (c == ']')
- break;
- }
- if (!not)
- return (FNM_NOMATCH);
- break;
-
- matched:
- /* Skip the rest of the [...] that already matched. */
- while (c != ']')
- {
- if (c == '\0')
- /* [... (unterminated) loses. */
- return (FNM_NOMATCH);
-
- c = *p++;
- if (!(flags & FNM_NOESCAPE) && c == '\\')
- {
- if (*p == '\0')
- return FNM_NOMATCH;
- /* XXX 1003.2d11 is unclear if this is right. */
- ++p;
- }
- }
- if (not)
- return (FNM_NOMATCH);
+ p = brackmatch (p, sc, flags);
+ if (p == 0)
+ return FNM_NOMATCH;
}
break;
default:
- if (c != *n)
+ if (c != FOLD (sc))
return (FNM_NOMATCH);
}
++n;
}
- if (*n == '\0')
+ if (n == se)
return (0);
+ if ((flags & FNM_LEADING_DIR) && *n == '/')
+ /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
+ return 0;
+
+ return (FNM_NOMATCH);
+}
+
+/* Parse a bracket expression collating symbol ([.sym.]) starting at P, find
+ the value of the symbol, and move P past the collating symbol expression.
+ The value is returned in *VP, if VP is not null. */
+static char *
+parse_collsym (p, vp)
+ char *p;
+ int *vp;
+{
+ register int pc;
+ int val;
+
+ p++; /* move past the `.' */
+
+ for (pc = 0; p[pc]; pc++)
+ if (p[pc] == '.' && p[pc+1] == ']')
+ break;
+ val = collsym (p, pc);
+ if (vp)
+ *vp = val;
+ return (p + pc + 2);
+}
+
+static char *
+brackmatch (p, test, flags)
+ char *p;
+ unsigned char test;
+ int flags;
+{
+ register char cstart, cend, c;
+ register int not; /* Nonzero if the sense of the character class is inverted. */
+ int pc, brcnt;
+ char *savep;
+
+ test = FOLD (test);
+
+ savep = p;
+
+ /* POSIX.2 3.13.1 says that an exclamation mark (`!') shall replace the
+ circumflex (`^') in its role in a `nonmatching list'. A bracket
+ expression starging with an unquoted circumflex character produces
+ unspecified results. This implementation treats the two identically. */
+ if (not = (*p == '!' || *p == '^'))
+ ++p;
+
+ c = *p++;
+ for (;;)
+ {
+ /* Initialize cstart and cend in case `-' is the last
+ character of the pattern. */
+ cstart = cend = c;
+
+ /* POSIX.2 equivalence class: [=c=]. See POSIX.2 2.8.3.2. Find
+ the end of the equivalence class, move the pattern pointer past
+ it, and check for equivalence. XXX - this handles only
+ single-character equivalence classes, which is wrong, or at
+ least incomplete. */
+ if (c == '[' && *p == '=' && p[2] == '=' && p[3] == ']')
+ {
+ pc = FOLD (p[1]);
+ p += 4;
+ if (collequiv (test, pc))
+ goto matched;
+ else
+ {
+ c = *p++;
+ if (c == '\0')
+ return ((test == '[') ? savep : (char *)0);
+ c = FOLD (c);
+ continue;
+ }
+ }
+
+ /* POSIX.2 character class expression. See POSIX.2 2.8.3.2. */
+ if (c == '[' && *p == ':')
+ {
+ pc = 0; /* make sure invalid char classes don't match. */
+ if (STREQN (p+1, "alnum:]", 7))
+ { pc = isalnum (test); p += 8; }
+ else if (STREQN (p+1, "alpha:]", 7))
+ { pc = isalpha (test); p += 8; }
+ else if (STREQN (p+1, "blank:]", 7))
+ { pc = isblank (test); p += 8; }
+ else if (STREQN (p+1, "cntrl:]", 7))
+ { pc = iscntrl (test); p += 8; }
+ else if (STREQN (p+1, "digit:]", 7))
+ { pc = isdigit (test); p += 8; }
+ else if (STREQN (p+1, "graph:]", 7))
+ { pc = isgraph (test); p += 8; }
+ else if (STREQN (p+1, "lower:]", 7))
+ { pc = ISLOWER (test); p += 8; }
+ else if (STREQN (p+1, "print:]", 7))
+ { pc = isprint (test); p += 8; }
+ else if (STREQN (p+1, "punct:]", 7))
+ { pc = ispunct (test); p += 8; }
+ else if (STREQN (p+1, "space:]", 7))
+ { pc = isspace (test); p += 8; }
+ else if (STREQN (p+1, "upper:]", 7))
+ { pc = ISUPPER (test); p += 8; }
+ else if (STREQN (p+1, "xdigit:]", 8))
+ { pc = isxdigit (test); p += 9; }
+ else if (STREQN (p+1, "ascii:]", 7))
+ { pc = isascii (test); p += 8; }
+ if (pc)
+ goto matched;
+ else
+ {
+ /* continue the loop here, since this expression can't be
+ the first part of a range expression. */
+ c = *p++;
+ if (c == '\0')
+ return ((test == '[') ? savep : (char *)0);
+ else if (c == ']')
+ break;
+ c = FOLD (c);
+ continue;
+ }
+ }
+
+ /* POSIX.2 collating symbols. See POSIX.2 2.8.3.2. Find the end of
+ the symbol name, make sure it is terminated by `.]', translate
+ the name to a character using the external table, and do the
+ comparison. */
+ if (c == '[' && *p == '.')
+ {
+ p = parse_collsym (p, &pc);
+ /* An invalid collating symbol cannot be the first point of a
+ range. If it is, we set cstart to one greater than `test',
+ so any comparisons later will fail. */
+ cstart = (pc == -1) ? test + 1 : pc;
+ }
+
+ if (!(flags & FNM_NOESCAPE) && c == '\\')
+ {
+ if (*p == '\0')
+ return (char *)0;
+ cstart = cend = *p++;
+ }
+
+ cstart = cend = FOLD (cstart);
+
+ /* POSIX.2 2.8.3.1.2 says: `An expression containing a `[' that
+ is not preceded by a backslash and is not part of a bracket
+ expression produces undefined results.' This implementation
+ treats the `[' as just a character to be matched if there is
+ not a closing `]'. */
+ if (c == '\0')
+ return ((test == '[') ? savep : (char *)0);
+
+ c = *p++;
+ c = FOLD (c);
+
+ if ((flags & FNM_PATHNAME) && c == '/')
+ /* [/] can never match when matching a pathname. */
+ return (char *)0;
+
+ /* This introduces a range, unless the `-' is the last
+ character of the class. Find the end of the range
+ and move past it. */
+ if (c == '-' && *p != ']')
+ {
+ cend = *p++;
+ if (!(flags & FNM_NOESCAPE) && cend == '\\')
+ cend = *p++;
+ if (cend == '\0')
+ return (char *)0;
+ if (cend == '[' && *p == '.')
+ {
+ p = parse_collsym (p, &pc);
+ /* An invalid collating symbol cannot be the second part of a
+ range expression. If we get one, we set cend to one fewer
+ than the test character to make sure the range test fails. */
+ cend = (pc == -1) ? test - 1 : pc;
+ }
+ cend = FOLD (cend);
+
+ c = *p++;
+
+ /* POSIX.2 2.8.3.2: ``The ending range point shall collate
+ equal to or higher than the starting range point; otherwise
+ the expression shall be treated as invalid.'' Note that this
+ applies to only the range expression; the rest of the bracket
+ expression is still checked for matches. */
+ if (rangecmp (cstart, cend) > 0)
+ {
+ if (c == ']')
+ break;
+ c = FOLD (c);
+ continue;
+ }
+ }
+
+ if (rangecmp (test, cstart) >= 0 && rangecmp (test, cend) <= 0)
+ goto matched;
+
+ if (c == ']')
+ break;
+ }
+ /* No match. */
+ return (!not ? (char *)0 : p);
+
+matched:
+ /* Skip the rest of the [...] that already matched. */
+ brcnt = (c != ']') + (c == '[' && (*p == '=' || *p == ':' || *p == '.'));
+ while (brcnt > 0)
+ {
+ /* A `[' without a matching `]' is just another character to match. */
+ if (c == '\0')
+ return ((test == '[') ? savep : (char *)0);
+
+ c = *p++;
+ if (c == '[' && (*p == '=' || *p == ':' || *p == '.'))
+ brcnt++;
+ else if (c == ']')
+ brcnt--;
+ else if (!(flags & FNM_NOESCAPE) && c == '\\')
+ {
+ if (*p == '\0')
+ return (char *)0;
+ /* XXX 1003.2d11 is unclear if this is right. */
+ ++p;
+ }
+ }
+ return (not ? (char *)0 : p);
+}
+
+#if defined (EXTENDED_GLOB)
+/* ksh-like extended pattern matching:
+
+ [?*+@!](pat-list)
+
+ where pat-list is a list of one or patterns separated by `|'. Operation
+ is as follows:
+
+ ?(patlist) match zero or one of the given patterns
+ *(patlist) match zero or more of the given patterns
+ +(patlist) match one or more of the given patterns
+ @(patlist) match exactly one of the given patterns
+ !(patlist) match anything except one of the given patterns
+*/
+
+/* Scan a pattern starting at STRING and ending at END, keeping track of
+ embedded () and []. If DELIM is 0, we scan until a matching `)'
+ because we're scanning a `patlist'. Otherwise, we scan until we see
+ DELIM. In all cases, we never scan past END. The return value is the
+ first character after the matching DELIM. */
+static char *
+patscan (string, end, delim)
+ char *string, *end;
+ int delim;
+{
+ int pnest, bnest;
+ char *s, c;
+
+ pnest = bnest = 0;
+ for (s = string; c = *s; s++)
+ {
+ switch (c)
+ {
+ case '\0':
+ return ((char *)0);
+ case '[':
+ bnest++;
+ break;
+ case ']':
+ if (bnest)
+ bnest--;
+ break;
+ case '(':
+ if (bnest == 0)
+ pnest++;
+ break;
+ case ')':
+ if (bnest == 0)
+ pnest--;
+ if (pnest <= 0)
+ return ++s;
+ break;
+ case '|':
+ if (bnest == 0 && pnest == 0 && delim == '|')
+ return ++s;
+ break;
+ }
+ }
+ return (char *)0;
+}
+
+/* Return 0 if dequoted pattern matches S in the current locale. */
+static int
+strcompare (p, pe, s, se)
+ char *p, *pe, *s, *se;
+{
+ int ret;
+ char c1, c2;
+
+ c1 = *pe;
+ c2 = *se;
+
+ *pe = *se = '\0';
+#if defined (HAVE_STRCOLL)
+ ret = strcoll (p, s);
+#else
+ ret = strcmp (p, s);
+#endif
+
+ *pe = c1;
+ *se = c2;
+
+ return (ret == 0 ? ret : FNM_NOMATCH);
+}
+
+/* Match a ksh extended pattern specifier. Return FNM_NOMATCH on failure or
+ 0 on success. This is handed the entire rest of the pattern and string
+ the first time an extended pattern specifier is encountered, so it calls
+ gmatch recursively. */
+static int
+extmatch (xc, s, se, p, pe, flags)
+ int xc; /* select which operation */
+ char *s, *se;
+ char *p, *pe;
+ int flags;
+{
+ char *prest; /* pointer to rest of pattern */
+ char *psub; /* pointer to sub-pattern */
+ char *pnext; /* pointer to next sub-pattern */
+ char *srest; /* pointer to rest of string */
+ int m1, m2;
+
+ switch (xc)
+ {
+ case '+': /* match one or more occurrences */
+ case '*': /* match zero or more occurrences */
+ prest = patscan (p, pe, 0);
+ if (prest == 0)
+ /* If PREST is 0, we failed to scan a valid pattern. In this
+ case, we just want to compare the two as strings. */
+ return (strcompare (p - 1, pe, s, se));
+
+ /* If we can get away with no matches, don't even bother. Just
+ call gmatch on the rest of the pattern and return success if
+ it succeeds. */
+ if (xc == '*' && (gmatch (s, se, prest, pe, flags) == 0))
+ return 0;
+
+ /* OK, we have to do this the hard way. First, we make sure one of
+ the subpatterns matches, then we try to match the rest of the
+ string. */
+ for (psub = p + 1; ; psub = pnext)
+ {
+ pnext = patscan (psub, pe, '|');
+ for (srest = s; srest <= se; srest++)
+ {
+ /* Match this substring (S -> SREST) against this
+ subpattern (psub -> pnext - 1) */
+ m1 = gmatch (s, srest, psub, pnext - 1, flags) == 0;
+ /* OK, we matched a subpattern, so make sure the rest of the
+ string matches the rest of the pattern. Also handle
+ multiple matches of the pattern. */
+ if (m1)
+ m2 = (gmatch (srest, se, prest, pe, flags) == 0) ||
+ (s != srest && gmatch (srest, se, p - 1, pe, flags) == 0);
+ if (m1 && m2)
+ return (0);
+ }
+ if (pnext == prest)
+ break;
+ }
+ return (FNM_NOMATCH);
+
+ case '?': /* match zero or one of the patterns */
+ case '@': /* match exactly one of the patterns */
+ prest = patscan (p, pe, 0);
+ if (prest == 0)
+ return (strcompare (p - 1, pe, s, se));
+
+ /* If we can get away with no matches, don't even bother. Just
+ call gmatch on the rest of the pattern and return success if
+ it succeeds. */
+ if (xc == '?' && (gmatch (s, se, prest, pe, flags) == 0))
+ return 0;
+
+ /* OK, we have to do this the hard way. First, we see if one of
+ the subpatterns matches, then, if it does, we try to match the
+ rest of the string. */
+ for (psub = p + 1; ; psub = pnext)
+ {
+ pnext = patscan (psub, pe, '|');
+ srest = (prest == pe) ? se : s;
+ for ( ; srest <= se; srest++)
+ {
+ if (gmatch (s, srest, psub, pnext - 1, flags) == 0 &&
+ gmatch (srest, se, prest, pe, flags) == 0)
+ return (0);
+ }
+ if (pnext == prest)
+ break;
+ }
+ return (FNM_NOMATCH);
+
+ case '!': /* match anything *except* one of the patterns */
+ prest = patscan (p, pe, 0);
+ if (prest == 0)
+ return (strcompare (p - 1, pe, s, se));
+
+ for (srest = s; srest <= se; srest++)
+ {
+ m1 = 0;
+ for (psub = p + 1; ; psub = pnext)
+ {
+ pnext = patscan (psub, pe, '|');
+ /* If one of the patterns matches, just bail immediately. */
+ if (m1 = (gmatch (s, srest, psub, pnext - 1, flags) == 0))
+ break;
+ if (pnext == prest)
+ break;
+ }
+ if (m1 == 0 && gmatch (srest, se, prest, pe, flags) == 0)
+ return (0);
+ }
+ return (FNM_NOMATCH);
+ }
+
return (FNM_NOMATCH);
}
+#endif /* EXTENDED_GLOB */
+
+#ifdef TEST
+main (c, v)
+ int c;
+ char **v;
+{
+ char *string, *pat;
+
+ string = v[1];
+ pat = v[2];
+
+ if (fnmatch (pat, string, 0) == 0)
+ {
+ printf ("%s matches %s\n", string, pat);
+ exit (0);
+ }
+ else
+ {
+ printf ("%s does not match %s\n", string, pat);
+ exit (1);
+ }
+}
+#endif
diff --git a/lib/glob/fnmatch.h b/lib/glob/fnmatch.h
index 62c8c8f..ac0ba20 100644
--- a/lib/glob/fnmatch.h
+++ b/lib/glob/fnmatch.h
@@ -17,14 +17,24 @@ not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#ifndef _FNMATCH_H
-
#define _FNMATCH_H 1
+/* We #undef these before defining them because some losing systems
+ (HP-UX A.08.07 for example) define these in <unistd.h>. */
+#undef FNM_PATHNAME
+#undef FNM_NOESCAPE
+#undef FNM_PERIOD
+
/* Bits set in the FLAGS argument to `fnmatch'. */
-#define FNM_PATHNAME (1 << 0)/* No wildcard can ever match `/'. */
-#define FNM_NOESCAPE (1 << 1)/* Backslashes don't quote special chars. */
-#define FNM_PERIOD (1 << 2)/* Leading `.' is matched only explicitly. */
-#define __FNM_FLAGS (FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD)
+/* standard flags */
+#define FNM_PATHNAME (1 << 0) /* No wildcard can ever match `/'. */
+#define FNM_NOESCAPE (1 << 1) /* Backslashes don't quote special chars. */
+#define FNM_PERIOD (1 << 2) /* Leading `.' is matched only explicitly. */
+
+/* extended flags */
+#define FNM_LEADING_DIR (1 << 3) /* Ignore `/...' after a match. */
+#define FNM_CASEFOLD (1 << 4) /* Compare without regard to case. */
+#define FNM_EXTMATCH (1 << 5) /* Use ksh-like extended matching. */
/* Value returned by `fnmatch' if STRING does not match PATTERN. */
#define FNM_NOMATCH 1
@@ -33,4 +43,4 @@ Cambridge, MA 02139, USA. */
returning zero if it matches, FNM_NOMATCH if not. */
extern int fnmatch();
-#endif /* fnmatch.h */
+#endif /* _FNMATCH_H */
diff --git a/lib/glob/glob.c b/lib/glob/glob.c
index 6d2f58f..6a9679f 100644
--- a/lib/glob/glob.c
+++ b/lib/glob/glob.c
@@ -25,20 +25,29 @@
#pragma alloca
#endif /* _AIX && RISC6000 && !__GNUC__ */
+#if defined (SHELL)
+# include "bashtypes.h"
+#else
+# include <sys/types.h>
+#endif
+
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
-#if defined (HAVE_STDLIB_H)
-# include <stdlib.h>
+#if defined (SHELL)
+# include "bashansi.h"
#else
-# if defined (SHELL)
-# include "ansi_stdlib.h"
-# endif /* SHELL */
+# if defined (HAVE_STDLIB_H)
+# include <stdlib.h>
+# endif
+# if defined (HAVE_STRING_H)
+# include <string.h>
+# else /* !HAVE_STRING_H */
+# include <strings.h>
+# endif /* !HAVE_STRING_H */
#endif
-#include <sys/types.h>
-
#if defined (HAVE_DIRENT_H)
# include <dirent.h>
# define D_NAMLEN(d) strlen ((d)->d_name)
@@ -66,27 +75,25 @@
# define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
#endif /* _POSIX_SOURCE */
-#if defined (HAVE_STRING_H)
-# include <string.h>
-#else /* !HAVE_STRING_H */
-# include <strings.h>
-#endif /* !HAVE_STRING_H */
-
#if !defined (HAVE_BCOPY)
# define bcopy(s, d, n) ((void) memcpy ((d), (s), (n)))
#endif /* !HAVE_BCOPY */
-/* If the opendir () on your system lets you open non-directory files,
- then we consider that not robust. */
-#if defined (OPENDIR_NOT_ROBUST)
-# if defined (SHELL)
-# include "posixstat.h"
-# else /* !SHELL */
-# include <sys/stat.h>
-# endif /* !SHELL */
-#endif /* OPENDIR_NOT_ROBUST */
-
-#include "memalloc.h"
+#if defined (SHELL)
+# include "posixstat.h"
+#else /* !SHELL */
+# include <sys/stat.h>
+#endif /* !SHELL */
+
+#include "filecntl.h"
+#if !defined (F_OK)
+# define F_OK 0
+#endif
+
+#if defined (SHELL)
+# include "memalloc.h"
+#endif
+
#include "fnmatch.h"
#if !defined (HAVE_STDLIB_H) && !defined (SHELL)
@@ -104,14 +111,20 @@ extern void free ();
#if defined (SHELL)
extern void throw_to_top_level ();
+extern int test_eaccess ();
extern int interrupt_state;
+extern int extended_glob;
#endif /* SHELL */
/* 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;
@@ -120,9 +133,12 @@ int
glob_pattern_p (pattern)
char *pattern;
{
- register char *p = pattern;
+ register char *p;
register char c;
- int open = 0;
+ int bopen;
+
+ p = pattern;
+ bopen = 0;
while ((c = *p++) != '\0')
switch (c)
@@ -132,13 +148,20 @@ glob_pattern_p (pattern)
return (1);
case '[': /* Only accept an open brace if there is a close */
- open++; /* brace to match it. Bracket expressions must be */
+ bopen++; /* brace to match it. Bracket expressions must be */
continue; /* complete, according to Posix.2 */
case ']':
- if (open)
+ if (bopen)
return (1);
continue;
+ case '+': /* extended matching operators */
+ case '@':
+ case '!':
+ if (*p == '(') /*) */
+ return (1);
+ continue;
+
case '\\':
if (*p++ == '\0')
return (0);
@@ -168,6 +191,35 @@ dequote_pathname (pathname)
}
+
+/* Test whether NAME exists. */
+
+#if defined (HAVE_LSTAT)
+# define GLOB_TESTNAME(name) (lstat (name, &finfo))
+#else /* !HAVE_LSTAT */
+# if defined (SHELL) && !defined (AFS)
+# define GLOB_TESTNAME(name) (test_eaccess (nextname, F_OK))
+# else /* !SHELL || AFS */
+# define GLOB_TESTNAME(name) (access (nextname, F_OK))
+# endif /* !SHELL || 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.
@@ -204,103 +256,161 @@ glob_vector (pat, dir)
int lose, skip;
register char **name_vector;
register unsigned int i;
-#if defined (OPENDIR_NOT_ROBUST)
- struct stat finfo;
-
- if (stat (dir, &finfo) < 0)
- return ((char **) &glob_error_return);
-
- if (!S_ISDIR (finfo.st_mode))
- return ((char **) &glob_error_return);
-#endif /* OPENDIR_NOT_ROBUST */
-
- d = opendir (dir);
- if (d == NULL)
- return ((char **) &glob_error_return);
+ int flags; /* Flags passed to fnmatch (). */
lastlink = 0;
- count = 0;
- lose = 0;
- skip = 0;
+ count = lose = skip = 0;
/* If PAT is empty, skip the loop, but return one (empty) filename. */
- if (!pat || !*pat)
+ if (pat == 0 || *pat == '\0')
{
+ if (glob_testdir (dir) < 0)
+ return ((char **) &glob_error_return);
+
nextlink = (struct globval *)alloca (sizeof (struct globval));
- nextlink->next = lastlink;
+ nextlink->next = (struct globval *)0;
nextname = (char *) malloc (1);
- if (!nextname)
+ if (nextname == 0)
lose = 1;
else
{
lastlink = nextlink;
nextlink->name = nextname;
nextname[0] = '\0';
- count++;
+ count = 1;
}
+
skip = 1;
}
- /* 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 (!skip)
+ /* 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 flags; /* Flags passed to fnmatch (). */
-#if defined (SHELL)
- /* Make globbing interruptible in the bash shell. */
- if (interrupt_state)
+ 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);
+ if (nextname == 0)
+ lose = 1;
+ else
{
- closedir (d);
- lose = 1;
- goto lost;
+ strcpy (nextname, dir);
+ nextname[dirlen++] = '/';
+ strcpy (nextname + dirlen, pat);
+
+ if (GLOB_TESTNAME (nextname) >= 0)
+ {
+ free (nextname);
+ nextlink = (struct globval *)alloca (sizeof (struct globval));
+ nextlink->next = (struct globval *)0;
+ nextname = (char *) malloc (strlen (pat) + 1);
+ if (nextname == 0)
+ lose = 1;
+ else
+ {
+ lastlink = nextlink;
+ nextlink->name = nextname;
+ strcpy (nextname, pat);
+ count = 1;
+ }
+ }
+ else
+ free (nextname);
}
-#endif /* SHELL */
-
- dp = readdir (d);
- if (dp == NULL)
- break;
- /* If this directory entry is not to be used, try again. */
- if (!REAL_DIR_ENTRY (dp))
- continue;
+ skip = 1;
+ }
- /* If a dot must be explicity matched, check to see if they do. */
- if (noglob_dot_filenames && dp->d_name[0] == '.' && pat[0] != '.' &&
- (pat[0] != '\\' || pat[1] != '.'))
- continue;
+ 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 fnmatch(). We don't
+ need to do this every time through the loop. */
flags = (noglob_dot_filenames ? FNM_PERIOD : 0) | FNM_PATHNAME;
- if (fnmatch (pat, dp->d_name, flags) != FNM_NOMATCH)
+#ifdef FNM_CASEFOLD
+ if (glob_ignore_case)
+ flags |= FNM_CASEFOLD;
+#endif
+
+#ifdef SHELL
+ if (extended_glob)
+ flags |= FNM_EXTMATCH;
+#endif
+
+ /* 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)
{
- nextlink = (struct globval *) alloca (sizeof (struct globval));
- nextlink->next = lastlink;
- nextname = (char *) malloc (D_NAMLEN (dp) + 1);
- if (nextname == NULL)
+#if defined (SHELL)
+ /* Make globbing interruptible in the shell. */
+ if (interrupt_state)
{
lose = 1;
break;
}
- lastlink = nextlink;
- nextlink->name = nextname;
- bcopy (dp->d_name, nextname, D_NAMLEN (dp) + 1);
- ++count;
+#endif /* SHELL */
+
+ 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 a dot must be explicity matched, check to see if they do. */
+ if (noglob_dot_filenames && dp->d_name[0] == '.' && pat[0] != '.' &&
+ (pat[0] != '\\' || pat[1] != '.'))
+ continue;
+
+ if (fnmatch (pat, dp->d_name, flags) != FNM_NOMATCH)
+ {
+ nextlink = (struct globval *) alloca (sizeof (struct globval));
+ nextlink->next = lastlink;
+ nextname = (char *) malloc (D_NAMLEN (dp) + 1);
+ if (nextname == NULL)
+ {
+ lose = 1;
+ break;
+ }
+ lastlink = nextlink;
+ nextlink->name = nextname;
+ bcopy (dp->d_name, nextname, D_NAMLEN (dp) + 1);
+ ++count;
+ }
}
+
+ (void) closedir (d);
}
- (void) closedir (d);
- if (!lose)
+ if (lose == 0)
{
name_vector = (char **) malloc ((count + 1) * sizeof (char *));
lose |= name_vector == NULL;
}
/* Have we run out of memory? */
-#if defined (SHELL)
- lost:
-#endif
if (lose)
{
/* Here free the strings we have got. */
@@ -313,7 +423,8 @@ glob_vector (pat, dir)
if (interrupt_state)
throw_to_top_level ();
#endif /* SHELL */
- return (NULL);
+
+ return ((char **)NULL);
}
/* Copy the name pointers from the linked list into the vector. */
diff --git a/lib/glob/glob.h b/lib/glob/glob.h
index a72dede..ac83f1e 100644
--- a/lib/glob/glob.h
+++ b/lib/glob/glob.h
@@ -26,5 +26,6 @@ extern char **glob_filename __P((char *));
extern char *glob_error_return;
extern int noglob_dot_filenames;
+extern int glob_ignore_case;
#endif /* _GLOB_H_ */
diff --git a/lib/malloc/getpagesize.h b/lib/malloc/getpagesize.h
index 0b75b89..6085a5c 100644
--- a/lib/malloc/getpagesize.h
+++ b/lib/malloc/getpagesize.h
@@ -16,6 +16,9 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
# if defined (_SC_PAGESIZE)
# define getpagesize() sysconf(_SC_PAGESIZE)
@@ -27,7 +30,9 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#endif
#if !defined (getpagesize)
-# include <sys/param.h>
+# ifndef _MINIX
+# include <sys/param.h>
+# endif
# if defined (PAGESIZE)
# define getpagesize() PAGESIZE
# else /* !PAGESIZE */
diff --git a/lib/malloc/gmalloc.c b/lib/malloc/gmalloc.c
index 8690b12..dd24146 100644
--- a/lib/malloc/gmalloc.c
+++ b/lib/malloc/gmalloc.c
@@ -1,7 +1,3 @@
-/* DO NOT EDIT THIS FILE -- it is automagically generated. -*- C -*- */
-
-#define _MALLOC_INTERNAL
-
/* The malloc headers and source files from the C library follow here. */
/* Declarations for `malloc' and friends.
@@ -20,118 +16,173 @@ Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.
+ot, write to the Free Software Foundation, Inc., 59 Temple Place -
+Suite 330, Boston, MA 02111-1307, USA.
The author may be reached (Email) at the address mike@ai.mit.edu,
or (US mail) as Mike Haertel c/o Free Software Foundation. */
-#ifndef _MALLOC_H
-
-#define _MALLOC_H 1
+/* XXX NOTES:
+ 1. Augment the mstats struct so we can see how many blocks for fragments
+ and how many blocks for large requests were allocated.
+*/
-#ifdef _MALLOC_INTERNAL
+/* CHANGES:
+ 1. Reorganized the source for my benefit.
+ 2. Integrated the range-checking code by default.
+ 3. free(0) no longer dumps core.
+ 4. Extended the statistics.
+ 5. Fixed a couple of places where the stats were not kept correctly.
+*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
-#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
-#include <string.h>
+#if defined (HAVE_STRING_H)
+# include <string.h>
#else
-#ifndef memset
-#define memset(s, zero, n) bzero ((s), (n))
-#endif
-#ifndef memcpy
-#define memcpy(d, s, n) bcopy ((s), (d), (n))
-#endif
+# include <strings.h>
#endif
-#if defined (__GNU_LIBRARY__) || (defined (__STDC__) && __STDC__)
-#include <limits.h>
-#else
-#ifndef CHAR_BIT
-#define CHAR_BIT 8
+#if defined (HAVE_LIMITS_H)
+# include <limits.h>
#endif
+
+#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
+# include <unistd.h>
#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+#if defined (HAVE_STDDEF_H)
+# include <stddef.h>
#endif
+#include <errno.h>
-#endif /* _MALLOC_INTERNAL. */
+#if defined (RCHECK) && !defined (botch)
+# include <stdio.h>
+# define STDIO_H_INCLUDED
+#endif
+#include "stdc.h"
-#ifdef __cplusplus
-extern "C"
-{
+#ifndef errno
+extern int errno;
#endif
-#if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
-#undef __P
-#define __P(args) args
-#undef __ptr_t
-#define __ptr_t void *
-#else /* Not C++ or ANSI C. */
-#undef __P
-#define __P(args) ()
-#undef const
-#define const
-#undef __ptr_t
-#define __ptr_t char *
-#endif /* C++ or ANSI C. */
-
-#if defined (__STDC__) && __STDC__
-#include <stddef.h>
-#define __malloc_size_t size_t
-#define __malloc_ptrdiff_t ptrdiff_t
+/* Need an autoconf test for this. */
+#if __STDC__
+# undef genptr_t
+# define genptr_t void *
#else
-#define __malloc_size_t unsigned int
-#define __malloc_ptrdiff_t int
+# undef genptr_t
+# define genptr_t char *
+#endif /* !__STDC__ */
+
+#if !defined (HAVE_MEMSET)
+# define memset(s, zero, n) bzero ((s), (n))
+#endif
+#if !defined (HAVE_MEMCPY)
+# define memcpy(d, s, n) bcopy ((s), (d), (n))
+#endif
+
+/* Cope with systems lacking `memmove'. */
+#if !defined (HAVE_MEMMOVE) && !defined (memmove)
+static void malloc_safe_bcopy __P ((genptr_t, genptr_t, size_t));
+# define memmove(to, from, size) malloc_safe_bcopy ((from), (to), (size))
#endif
#ifndef NULL
#define NULL 0
#endif
+#ifndef min
+#define min(A, B) ((A) < (B) ? (A) : (B))
+#endif
+
+/* Return values for `mprobe': these are the kinds of inconsistencies that
+ `mcheck' enables detection of. */
+enum mcheck_status
+ {
+ MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
+ MCHECK_OK, /* Block is fine. */
+ MCHECK_FREE, /* Block freed twice. */
+ MCHECK_HEAD, /* Memory before the block was clobbered. */
+ MCHECK_TAIL /* Memory after the block was clobbered. */
+ };
+
+/* Statistics available to the user. */
+struct mstats
+ {
+ size_t bytes_total; /* Total size of the heap. */
+ size_t chunks_used; /* Chunks allocated by the user. */
+ size_t bytes_used; /* Byte total of user-allocated chunks. */
+ size_t chunks_free; /* Chunks in the free list. */
+ size_t bytes_free; /* Byte total of chunks in the free list. */
+ int nmalloc; /* Total number of calls to malloc. */
+ int nfree; /* Total number of calls to free. */
+ int nrealloc; /* Total number of calls to realloc. */
+ int nsbrk; /* Total number of calls to sbrk. */
+ size_t tsbrk; /* Total number of bytes allocated via sbrk. */
+ int negsbrk; /* Total number of calls to sbrk with a negative arg */
+ size_t tnegsbrk; /* Total number of bytes returned to the kernel. */
+ };
+
+#ifdef RCHECK
+/* Arbitrary magical numbers. */
+#define MAGICWORD 0xfedabeeb
+#define MAGICFREE 0xd8675309
+#define MAGICBYTE ((char) 0xd7)
+#define MALLOCFLOOD ((char) 0x93)
+#define FREEFLOOD ((char) 0x95)
+struct hdr
+ {
+ size_t size; /* Exact size requested by user. */
+ u_int32_t magic; /* Magic number to check header integrity. */
+ };
+#endif /* RCHECK */
+
+/* Functions exported by this library. */
/* Allocate SIZE bytes of memory. */
-extern __ptr_t malloc __P ((__malloc_size_t __size));
+extern genptr_t malloc __P ((size_t __size));
+
/* Re-allocate the previously allocated block
- in __ptr_t, making the new block SIZE bytes long. */
-extern __ptr_t realloc __P ((__ptr_t __ptr, __malloc_size_t __size));
+ in genptr_t, making the new block SIZE bytes long. */
+extern genptr_t realloc __P ((genptr_t __ptr, size_t __size));
+
/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
-extern __ptr_t calloc __P ((__malloc_size_t __nmemb, __malloc_size_t __size));
+extern genptr_t calloc __P ((size_t __nmemb, size_t __size));
+
/* Free a block allocated by `malloc', `realloc' or `calloc'. */
-extern void free __P ((__ptr_t __ptr));
+extern void free __P ((genptr_t __ptr));
/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
-#if ! (defined (_MALLOC_INTERNAL) && __DJGPP__ - 0 == 1) /* Avoid conflict. */
-extern __ptr_t memalign __P ((__malloc_size_t __alignment,
- __malloc_size_t __size));
-#endif
+extern genptr_t memalign __P ((size_t __alignment, size_t __size));
-/* Allocate SIZE bytes on a page boundary. */
-#if ! (defined (_MALLOC_INTERNAL) && defined (emacs)) /* Avoid conflict. */
-extern __ptr_t valloc __P ((__malloc_size_t __size));
-#endif
+/* Pick up the current statistics. */
+extern struct mstats mstats __P ((void));
+#ifdef RCHECK
+extern enum mcheck_status mprobe __P((genptr_t ptr));
+#endif
-#ifdef _MALLOC_INTERNAL
+/* End of exported functions. */
/* The allocator divides the heap into blocks of fixed size; large
requests receive one or more whole blocks, and small requests
receive a fragment of a block. Fragment sizes are powers of two,
and all fragments of a block are the same size. When all the
fragments in a block have been freed, the block itself is freed. */
-#define INT_BIT (CHAR_BIT * sizeof(int))
-#define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
-#define BLOCKSIZE (1 << BLOCKLOG)
+#define BLOCKLOG 12
+#define BLOCKSIZE 4096 /* 1 << BLOCKLOG */
#define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
/* Determine the amount of memory spanned by the initial heap table
(not an absolute limit). */
-#define HEAP (INT_BIT > 16 ? 4194304 : 65536)
+#define HEAP 4194304 /* 1 << 22 */
/* Number of contiguous free blocks allowed to build up at the end of
memory before they will be returned to the system. */
@@ -150,40 +201,43 @@ typedef union
{
struct
{
- __malloc_size_t nfree; /* Free frags in a fragmented block. */
- __malloc_size_t first; /* First free fragment of the block. */
+ size_t nfree; /* Free frags in a fragmented block. */
+ size_t first; /* First free fragment of the block. */
} frag;
/* For a large object, in its first block, this has the number
of blocks in the object. In the other blocks, this has a
negative number which says how far back the first block is. */
- __malloc_ptrdiff_t size;
+ ptrdiff_t size;
} info;
} busy;
- /* Heap information for a free block
- (that may be the first of a free cluster). */
+ /* Heap information for a free block (that may be the first of a
+ free cluster). */
struct
{
- __malloc_size_t size; /* Size (in blocks) of a free cluster. */
- __malloc_size_t next; /* Index of next free cluster. */
- __malloc_size_t prev; /* Index of previous free cluster. */
+ size_t size; /* Size (in blocks) of a free cluster. */
+ size_t next; /* Index of next free cluster. */
+ size_t prev; /* Index of previous free cluster. */
} free;
} malloc_info;
/* Pointer to first block of the heap. */
-extern char *_heapbase;
+static char *_heapbase;
/* Table indexed by block number giving per-block information. */
-extern malloc_info *_heapinfo;
+static malloc_info *_heapinfo;
/* Address to block number and vice versa. */
#define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
-#define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
+#define ADDRESS(B) ((genptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
+
+/* Number of info entries. */
+static size_t heapsize;
/* Current search index for the heap table. */
-extern __malloc_size_t _heapindex;
+static size_t _heapindex;
/* Limit of valid info table indices. */
-extern __malloc_size_t _heaplimit;
+static size_t _heaplimit;
/* Doubly linked lists of free fragments. */
struct list
@@ -193,270 +247,127 @@ struct list
};
/* Free list headers for each fragment size. */
-extern struct list _fraghead[];
+static struct list _fraghead[BLOCKLOG];
-/* List of blocks allocated with `memalign' (or `valloc'). */
+/* List of blocks allocated with `memalign'. */
struct alignlist
{
struct alignlist *next;
- __ptr_t aligned; /* The address that memaligned returned. */
- __ptr_t exact; /* The address that malloc returned. */
+ genptr_t aligned; /* The address that memaligned returned. */
+ genptr_t exact; /* The address that malloc returned. */
};
-extern struct alignlist *_aligned_blocks;
-/* Instrumentation. */
-extern __malloc_size_t _chunks_used;
-extern __malloc_size_t _bytes_used;
-extern __malloc_size_t _chunks_free;
-extern __malloc_size_t _bytes_free;
+/* List of blocks allocated by memalign. */
+static struct alignlist *_aligned_blocks = NULL;
/* Internal versions of `malloc', `realloc', and `free'
- used when these functions need to call each other.
- They are the same but don't call the hooks. */
-extern __ptr_t _malloc_internal __P ((__malloc_size_t __size));
-extern __ptr_t _realloc_internal __P ((__ptr_t __ptr, __malloc_size_t __size));
-extern void _free_internal __P ((__ptr_t __ptr));
-
-#endif /* _MALLOC_INTERNAL. */
+ used when these functions need to call each other. */
+static genptr_t imalloc __P ((size_t __size));
+static genptr_t irealloc __P ((genptr_t __ptr, size_t __size));
+static void ifree __P ((genptr_t __ptr));
/* Given an address in the middle of a malloc'd object,
return the address of the beginning of the object. */
-extern __ptr_t malloc_find_object_address __P ((__ptr_t __ptr));
+static genptr_t malloc_find_object_address __P ((genptr_t __ptr));
/* Underlying allocation function; successive calls should
return contiguous pieces of memory. */
-extern __ptr_t (*__morecore) __P ((__malloc_ptrdiff_t __size));
-
-/* Default value of `__morecore'. */
-extern __ptr_t __default_morecore __P ((__malloc_ptrdiff_t __size));
-
-/* If not NULL, this function is called after each time
- `__morecore' is called to increase the data size. */
-extern void (*__after_morecore_hook) __P ((void));
+static genptr_t default_morecore __P ((ptrdiff_t __size));
/* Number of extra blocks to get each time we ask for more core.
- This reduces the frequency of calling `(*__morecore)'. */
-extern __malloc_size_t __malloc_extra_blocks;
+ This reduces the frequency of calling `default_morecore'. */
+static size_t malloc_extra_blocks;
/* Nonzero if `malloc' has been called and done its initialization. */
-extern int __malloc_initialized;
+static int malloc_initialized;
/* Function called to initialize malloc data structures. */
-extern int __malloc_initialize __P ((void));
+static int malloc_initialize __P ((void));
-/* Hooks for debugging versions. */
-extern void (*__malloc_initialize_hook) __P ((void));
-extern void (*__free_hook) __P ((__ptr_t __ptr));
-extern __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
-extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
-extern __ptr_t (*__memalign_hook) __P ((__malloc_size_t __size,
- __malloc_size_t __alignment));
-
-/* Return values for `mprobe': these are the kinds of inconsistencies that
- `mcheck' enables detection of. */
-enum mcheck_status
- {
- MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
- MCHECK_OK, /* Block is fine. */
- MCHECK_FREE, /* Block freed twice. */
- MCHECK_HEAD, /* Memory before the block was clobbered. */
- MCHECK_TAIL /* Memory after the block was clobbered. */
- };
-
-/* Activate a standard collection of debugging hooks. This must be called
- before `malloc' is ever called. ABORTFUNC is called with an error code
- (see enum above) when an inconsistency is detected. If ABORTFUNC is
- null, the standard function prints on stderr and then calls `abort'. */
-extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
-
-/* Check for aberrations in a particular malloc'd block. You must have
- called `mcheck' already. These are the same checks that `mcheck' does
- when you free or reallocate a block. */
-extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
-
-/* Activate a standard collection of tracing hooks. */
-extern void mtrace __P ((void));
-extern void muntrace __P ((void));
-
-/* Statistics available to the user. */
-struct mstats
- {
- __malloc_size_t bytes_total; /* Total size of the heap. */
- __malloc_size_t chunks_used; /* Chunks allocated by the user. */
- __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
- __malloc_size_t chunks_free; /* Chunks in the free list. */
- __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
- };
-
-/* Pick up the current statistics. */
-extern struct mstats mstats __P ((void));
-
-/* Call WARNFUN with a warning message when memory usage is high. */
-extern void memory_warnings __P ((__ptr_t __start,
- void (*__warnfun) __P ((const char *))));
-
-
-/* Relocating allocator. */
-
-/* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
-extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
-
-/* Free the storage allocated in HANDLEPTR. */
-extern void r_alloc_free __P ((__ptr_t *__handleptr));
-
-/* Adjust the block at HANDLEPTR to be SIZE bytes long. */
-extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
-
-
-#ifdef __cplusplus
-}
+#ifdef RCHECK
+static void zmemset __P((genptr_t, int, size_t));
+static enum mcheck_status checkhdr __P((const struct hdr *));
+static void mabort __P((enum mcheck_status));
#endif
-#endif /* malloc.h */
-/* Memory allocator `malloc'.
- Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
- Written May 1989 by Mike Haertel.
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This 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
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with this library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.
-
- The author may be reached (Email) at the address mike@ai.mit.edu,
- or (US mail) as Mike Haertel c/o Free Software Foundation. */
-
-#ifndef _MALLOC_INTERNAL
-#define _MALLOC_INTERNAL
-#include <malloc.h>
-#endif
-#include <errno.h>
-
-/* How to really get more memory. */
-__ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
-
-/* Debugging hook for `malloc'. */
-__ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
-
-/* Pointer to the base of the first block. */
-char *_heapbase;
-
-/* Block information table. Allocated with align/__free (not malloc/free). */
-malloc_info *_heapinfo;
-
-/* Number of info entries. */
-static __malloc_size_t heapsize;
-
-/* Search index in the info table. */
-__malloc_size_t _heapindex;
-
-/* Limit of valid info table indices. */
-__malloc_size_t _heaplimit;
-
-/* Free lists for each fragment size. */
-struct list _fraghead[BLOCKLOG];
-
/* Instrumentation. */
-__malloc_size_t _chunks_used;
-__malloc_size_t _bytes_used;
-__malloc_size_t _chunks_free;
-__malloc_size_t _bytes_free;
-
-/* Are you experienced? */
-int __malloc_initialized;
-
-__malloc_size_t __malloc_extra_blocks;
-
-void (*__malloc_initialize_hook) __P ((void));
-void (*__after_morecore_hook) __P ((void));
-
+static size_t chunks_used;
+static size_t bytes_used;
+static size_t chunks_free;
+static size_t bytes_free;
+static int nmalloc, nfree, nrealloc;
+static int nsbrk;
+static size_t tsbrk;
+static int negsbrk;
+static size_t tnegsbrk;
/* Aligned allocation. */
-static __ptr_t align __P ((__malloc_size_t));
-static __ptr_t
+static genptr_t
align (size)
- __malloc_size_t size;
+ size_t size;
{
- __ptr_t result;
+ genptr_t result;
unsigned long int adj;
- result = (*__morecore) (size);
+ result = default_morecore (size);
adj = (unsigned long int) ((unsigned long int) ((char *) result -
(char *) NULL)) % BLOCKSIZE;
if (adj != 0)
{
- __ptr_t new;
+ genptr_t new;
adj = BLOCKSIZE - adj;
- new = (*__morecore) (adj);
+ new = default_morecore (adj);
result = (char *) result + adj;
}
- if (__after_morecore_hook)
- (*__after_morecore_hook) ();
-
return result;
}
/* Get SIZE bytes, if we can get them starting at END.
Return the address of the space we got.
If we cannot get space at END, fail and return -1. */
-static __ptr_t get_contiguous_space __P ((__malloc_ptrdiff_t, __ptr_t));
-static __ptr_t
+static genptr_t
get_contiguous_space (size, position)
- __malloc_ptrdiff_t size;
- __ptr_t position;
+ ptrdiff_t size;
+ genptr_t position;
{
- __ptr_t before;
- __ptr_t after;
+ genptr_t before;
+ genptr_t after;
- before = (*__morecore) (0);
+ before = default_morecore (0);
/* If we can tell in advance that the break is at the wrong place,
fail now. */
if (before != position)
return 0;
/* Allocate SIZE bytes and get the address of them. */
- after = (*__morecore) (size);
+ after = default_morecore (size);
if (!after)
return 0;
/* It was not contiguous--reject it. */
if (after != position)
{
- (*__morecore) (- size);
+ default_morecore (- size);
return 0;
}
return after;
}
-
/* This is called when `_heapinfo' and `heapsize' have just
been set to describe a new info table. Set up the table
to describe itself and account for it in the statistics. */
-static void register_heapinfo __P ((void));
-#ifdef __GNUC__
-__inline__
-#endif
-static void
+inline static void
register_heapinfo ()
{
- __malloc_size_t block, blocks;
+ size_t block, blocks;
block = BLOCK (_heapinfo);
blocks = BLOCKIFY (heapsize * sizeof (malloc_info));
/* Account for the _heapinfo block itself in the statistics. */
- _bytes_used += blocks * BLOCKSIZE;
- ++_chunks_used;
+ bytes_used += blocks * BLOCKSIZE;
+ ++chunks_used;
/* Describe the heapinfo block itself in the heapinfo. */
_heapinfo[block].busy.type = 0;
@@ -467,15 +378,12 @@ register_heapinfo ()
}
/* Set everything up and remember that we have. */
-int
-__malloc_initialize ()
+static int
+malloc_initialize ()
{
- if (__malloc_initialized)
+ if (malloc_initialized)
return 0;
- if (__malloc_initialize_hook)
- (*__malloc_initialize_hook) ();
-
heapsize = HEAP / BLOCKSIZE;
_heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
if (_heapinfo == NULL)
@@ -489,22 +397,43 @@ __malloc_initialize ()
register_heapinfo ();
- __malloc_initialized = 1;
+ malloc_initialized = 1;
return 1;
}
+/* Allocate INCREMENT more bytes of data space,
+ and return the start of data space, or NULL on errors.
+ If INCREMENT is negative, shrink data space. */
+static genptr_t
+default_morecore (increment)
+ ptrdiff_t increment;
+{
+ genptr_t result;
+
+ nsbrk++;
+ tsbrk += increment;
+ if (increment < 0)
+ {
+ negsbrk++;
+ tnegsbrk += -increment;
+ }
+ result = (genptr_t) sbrk (increment);
+ if ((long)result == -1L)
+ return NULL;
+ return result;
+}
+
static int morecore_recursing;
/* Get neatly aligned memory, initializing or
growing the heap info table as necessary. */
-static __ptr_t morecore __P ((__malloc_size_t));
-static __ptr_t
+static genptr_t
morecore (size)
- __malloc_size_t size;
+ size_t size;
{
- __ptr_t result;
+ genptr_t result;
malloc_info *newinfo, *oldinfo;
- __malloc_size_t newsize;
+ size_t newsize;
if (morecore_recursing)
/* Avoid recursion. The caller will know how to handle a null return. */
@@ -515,7 +444,7 @@ morecore (size)
return NULL;
/* Check if we need to grow the info table. */
- if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
+ if ((size_t) BLOCK ((char *) result + size) > heapsize)
{
/* Calculate the new _heapinfo table size. We do not account for the
added blocks in the table itself, as we hope to place them in
@@ -523,8 +452,8 @@ morecore (size)
existing table. */
newsize = heapsize;
do
- newsize *= 2;
- while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize);
+ newsize <<= 1;
+ while ((size_t) BLOCK ((char *) result + size) > newsize);
/* We must not reuse existing core for the new info table when called
from realloc in the case of growing a large block, because the
@@ -540,8 +469,7 @@ morecore (size)
`morecore_recursing' flag and return null. */
int save = errno; /* Don't want to clobber errno with ENOMEM. */
morecore_recursing = 1;
- newinfo = (malloc_info *) _realloc_internal
- (_heapinfo, newsize * sizeof (malloc_info));
+ newinfo = (malloc_info *) irealloc (_heapinfo, newsize * sizeof (malloc_info));
morecore_recursing = 0;
if (newinfo == NULL)
errno = save;
@@ -550,8 +478,7 @@ morecore (size)
/* We found some space in core, and realloc has put the old
table's blocks on the free list. Now zero the new part
of the table and install the new table location. */
- memset (&newinfo[heapsize], 0,
- (newsize - heapsize) * sizeof (malloc_info));
+ memset (&newinfo[heapsize], 0, (newsize - heapsize) * sizeof (malloc_info));
_heapinfo = newinfo;
heapsize = newsize;
goto got_heap;
@@ -566,37 +493,34 @@ morecore (size)
/* Did it fail? */
if (newinfo == NULL)
{
- (*__morecore) (-size);
+ default_morecore (-size);
return NULL;
}
/* Is it big enough to record status for its own space?
If so, we win. */
- if ((__malloc_size_t) BLOCK ((char *) newinfo
- + newsize * sizeof (malloc_info))
- < newsize)
+ if ((size_t) BLOCK ((char *) newinfo + newsize * sizeof (malloc_info)) < newsize)
break;
/* Must try again. First give back most of what we just got. */
- (*__morecore) (- newsize * sizeof (malloc_info));
+ default_morecore (- newsize * sizeof (malloc_info));
newsize *= 2;
}
/* Copy the old table to the beginning of the new,
and zero the rest of the new table. */
memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
- memset (&newinfo[heapsize], 0,
- (newsize - heapsize) * sizeof (malloc_info));
+ memset (&newinfo[heapsize], 0, (newsize - heapsize) * sizeof (malloc_info));
oldinfo = _heapinfo;
_heapinfo = newinfo;
heapsize = newsize;
register_heapinfo ();
- /* Reset _heaplimit so _free_internal never decides
+ /* Reset _heaplimit so ifree never decides
it can relocate or resize the info table. */
_heaplimit = 0;
- _free_internal (oldinfo);
+ ifree (oldinfo);
/* The new heap limit includes the new table just allocated. */
_heaplimit = BLOCK ((char *) newinfo + heapsize * sizeof (malloc_info));
@@ -609,13 +533,13 @@ morecore (size)
}
/* Allocate memory from the heap. */
-__ptr_t
-_malloc_internal (size)
- __malloc_size_t size;
+static genptr_t
+imalloc (size)
+ size_t size;
{
- __ptr_t result;
- __malloc_size_t block, blocks, lastblocks, start;
- register __malloc_size_t i;
+ genptr_t result;
+ size_t block, blocks, lastblocks, start;
+ register size_t i;
struct list *next;
/* ANSI C allows `malloc (0)' to either return NULL, or to return a
@@ -625,7 +549,7 @@ _malloc_internal (size)
expects `malloc (0)' to return non-NULL and breaks otherwise.
Be compatible. */
-#if 0
+#if 0
if (size == 0)
return NULL;
#endif
@@ -643,7 +567,7 @@ _malloc_internal (size)
{
/* Small allocation to receive a fragment of a block.
Determine the logarithm to base two of the fragment size. */
- register __malloc_size_t log = 1;
+ register size_t log = 1;
--size;
while ((size /= 2) != 0)
++log;
@@ -656,7 +580,7 @@ _malloc_internal (size)
/* There are free fragments of this size.
Pop a fragment out of the fragment list and return it.
Update the block's nfree and first counters. */
- result = (__ptr_t) next;
+ result = (genptr_t) next;
next->prev->next = next->next;
if (next->next != NULL)
next->next->prev = next->prev;
@@ -667,16 +591,16 @@ _malloc_internal (size)
% BLOCKSIZE) >> log;
/* Update the statistics. */
- ++_chunks_used;
- _bytes_used += 1 << log;
- --_chunks_free;
- _bytes_free -= 1 << log;
+ ++chunks_used;
+ bytes_used += 1 << log;
+ --chunks_free;
+ bytes_free -= 1 << log;
}
else
{
/* No free fragments of the desired size, so get a new block
and break it into fragments, returning the first. */
- result = malloc (BLOCKSIZE);
+ result = imalloc (BLOCKSIZE);
if (result == NULL)
return NULL;
@@ -686,7 +610,7 @@ _malloc_internal (size)
next->prev = &_fraghead[log];
_fraghead[log].next = next;
- for (i = 2; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
+ for (i = 2; i < (size_t) (BLOCKSIZE >> log); ++i)
{
next = (struct list *) ((char *) result + (i << log));
next->next = _fraghead[log].next;
@@ -701,9 +625,9 @@ _malloc_internal (size)
_heapinfo[block].busy.info.frag.nfree = i - 1;
_heapinfo[block].busy.info.frag.first = i - 1;
- _chunks_free += (BLOCKSIZE >> log) - 1;
- _bytes_free += BLOCKSIZE - (1 << log);
- _bytes_used -= BLOCKSIZE - (1 << log);
+ chunks_free += (BLOCKSIZE >> log) - 1;
+ bytes_free += BLOCKSIZE - (1 << log);
+ bytes_used -= BLOCKSIZE - (1 << log);
}
}
else
@@ -720,7 +644,7 @@ _malloc_internal (size)
if (block == start)
{
/* Need to get more from the system. Get a little extra. */
- __malloc_size_t wantblocks = blocks + __malloc_extra_blocks;
+ size_t wantblocks = blocks + malloc_extra_blocks;
block = _heapinfo[0].free.prev;
lastblocks = _heapinfo[block].free.size;
/* Check to see if the new core will be contiguous with the
@@ -737,7 +661,7 @@ _malloc_internal (size)
changed, if it got combined with a freed info table. */
block = _heapinfo[0].free.prev;
_heapinfo[block].free.size += (wantblocks - lastblocks);
- _bytes_free += (wantblocks - lastblocks) * BLOCKSIZE;
+ bytes_free += (wantblocks - lastblocks) * BLOCKSIZE;
_heaplimit += wantblocks - lastblocks;
continue;
}
@@ -751,7 +675,8 @@ _malloc_internal (size)
_heapinfo[block].free.next = 0;
_heapinfo[0].free.prev = block;
_heapinfo[_heapinfo[block].free.prev].free.next = block;
- ++_chunks_free;
+ ++chunks_free;
+ bytes_free += wantblocks * BLOCKSIZE;
/* Now loop to use some of that block for this allocation. */
}
}
@@ -781,14 +706,14 @@ _malloc_internal (size)
= _heapinfo[block].free.prev;
_heapinfo[_heapinfo[block].free.prev].free.next
= _heapindex = _heapinfo[block].free.next;
- --_chunks_free;
+ --chunks_free;
}
_heapinfo[block].busy.type = 0;
_heapinfo[block].busy.info.size = blocks;
- ++_chunks_used;
- _bytes_used += blocks * BLOCKSIZE;
- _bytes_free -= blocks * BLOCKSIZE;
+ ++chunks_used;
+ bytes_used += blocks * BLOCKSIZE;
+ bytes_free -= blocks * BLOCKSIZE;
/* Mark all the blocks of the object just allocated except for the
first with a negative number so you can find the first block by
@@ -800,113 +725,55 @@ _malloc_internal (size)
return result;
}
-__ptr_t
+genptr_t
malloc (size)
- __malloc_size_t size;
-{
- if (!__malloc_initialized && !__malloc_initialize ())
- return NULL;
-
- return (__malloc_hook != NULL ? *__malloc_hook : _malloc_internal) (size);
-}
-
-#ifndef _LIBC
-
-/* On some ANSI C systems, some libc functions call _malloc, _free
- and _realloc. Make them use the GNU functions. */
-
-__ptr_t
-_malloc (size)
- __malloc_size_t size;
+ size_t size;
{
- return malloc (size);
-}
-
-void
-_free (ptr)
- __ptr_t ptr;
-{
- free (ptr);
-}
-
-__ptr_t
-_realloc (ptr, size)
- __ptr_t ptr;
- __malloc_size_t size;
-{
- return realloc (ptr, size);
-}
-
+#ifdef RCHECK
+ struct hdr *hdr;
#endif
-/* Free a block of memory allocated by `malloc'.
- Copyright 1990, 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
- Written May 1989 by Mike Haertel.
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This 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
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with this library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.
+ nmalloc++;
- The author may be reached (Email) at the address mike@ai.mit.edu,
- or (US mail) as Mike Haertel c/o Free Software Foundation. */
-
-#ifndef _MALLOC_INTERNAL
-#define _MALLOC_INTERNAL
-#include <malloc.h>
-#endif
+ if (malloc_initialized == 0 && malloc_initialize () == 0)
+ return NULL;
+#ifdef RCHECK
+ hdr = (struct hdr *) imalloc (sizeof (struct hdr) + size + 1);
+ if (hdr == NULL)
+ return NULL;
-/* Cope with systems lacking `memmove'. */
-#ifndef memmove
-#if (defined (MEMMOVE_MISSING) || \
- !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
-#ifdef emacs
-#undef __malloc_safe_bcopy
-#define __malloc_safe_bcopy safe_bcopy
-#endif
-/* This function is defined in realloc.c. */
-extern void __malloc_safe_bcopy __P ((__ptr_t, __ptr_t, __malloc_size_t));
-#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
-#endif
+ hdr->size = size;
+ hdr->magic = MAGICWORD;
+ ((char *) &hdr[1])[size] = MAGICBYTE;
+ zmemset ((genptr_t) (hdr + 1), MALLOCFLOOD, size);
+ return (genptr_t) (hdr + 1);
+#else
+ return (imalloc (size));
#endif
+}
+/* Free a block of memory allocated by `malloc'. */
-/* Debugging hook for free. */
-void (*__free_hook) __P ((__ptr_t __ptr));
-
-/* List of blocks allocated by memalign. */
-struct alignlist *_aligned_blocks = NULL;
-
-/* Return memory to the heap.
- Like `free' but don't call a __free_hook if there is one. */
-void
-_free_internal (ptr)
- __ptr_t ptr;
+/* Return memory to the heap. */
+static void
+ifree (ptr)
+ genptr_t ptr;
{
int type;
- __malloc_size_t block, blocks;
- register __malloc_size_t i;
+ size_t block, blocks;
+ register size_t i;
struct list *prev, *next;
- __ptr_t curbrk;
- const __malloc_size_t lesscore_threshold
- /* Threshold of free space at which we will return some to the system. */
- = FINAL_FREE_BLOCKS + 2 * __malloc_extra_blocks;
-
+ genptr_t curbrk;
+ size_t lesscore_threshold;
register struct alignlist *l;
if (ptr == NULL)
return;
+ /* Threshold of free space at which we will return some to the system. */
+ lesscore_threshold = FINAL_FREE_BLOCKS + 2 * malloc_extra_blocks;
+
for (l = _aligned_blocks; l != NULL; l = l->next)
if (l->aligned == ptr)
{
@@ -922,9 +789,9 @@ _free_internal (ptr)
{
case 0:
/* Get as many statistics as early as we can. */
- --_chunks_used;
- _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
- _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
+ --chunks_used;
+ bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
+ bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
/* Find the free cluster previous to this one in the free list.
Start searching at the last block referenced; this may benefit
@@ -956,7 +823,7 @@ _free_internal (ptr)
_heapinfo[block].free.prev = i;
_heapinfo[i].free.next = block;
_heapinfo[_heapinfo[block].free.next].free.prev = block;
- ++_chunks_free;
+ ++chunks_free;
}
/* Now that the block is linked in, see if we can coalesce it
@@ -969,14 +836,14 @@ _free_internal (ptr)
_heapinfo[block].free.next
= _heapinfo[_heapinfo[block].free.next].free.next;
_heapinfo[_heapinfo[block].free.next].free.prev = block;
- --_chunks_free;
+ --chunks_free;
}
/* How many trailing free blocks are there now? */
blocks = _heapinfo[block].free.size;
/* Where is the current end of accessible core? */
- curbrk = (*__morecore) (0);
+ curbrk = default_morecore (0);
if (_heaplimit != 0 && curbrk == ADDRESS (_heaplimit))
{
@@ -984,12 +851,12 @@ _free_internal (ptr)
It's possible that moving _heapinfo will allow us to
return some space to the system. */
- __malloc_size_t info_block = BLOCK (_heapinfo);
- __malloc_size_t info_blocks = _heapinfo[info_block].busy.info.size;
- __malloc_size_t prev_block = _heapinfo[block].free.prev;
- __malloc_size_t prev_blocks = _heapinfo[prev_block].free.size;
- __malloc_size_t next_block = _heapinfo[block].free.next;
- __malloc_size_t next_blocks = _heapinfo[next_block].free.size;
+ size_t info_block = BLOCK (_heapinfo);
+ size_t info_blocks = _heapinfo[info_block].busy.info.size;
+ size_t prev_block = _heapinfo[block].free.prev;
+ size_t prev_blocks = _heapinfo[prev_block].free.size;
+ size_t next_block = _heapinfo[block].free.next;
+ size_t next_blocks = _heapinfo[next_block].free.size;
if (/* Win if this block being freed is last in core, the info table
is just before it, the previous free block is just before the
@@ -1012,14 +879,14 @@ _free_internal (ptr)
)
{
malloc_info *newinfo;
- __malloc_size_t oldlimit = _heaplimit;
+ size_t oldlimit = _heaplimit;
/* Free the old info table, clearing _heaplimit to avoid
recursion into this code. We don't want to return the
table's blocks to the system before we have copied them to
the new location. */
_heaplimit = 0;
- _free_internal (_heapinfo);
+ ifree (_heapinfo);
_heaplimit = oldlimit;
/* Tell malloc to search from the beginning of the heap for
@@ -1027,7 +894,7 @@ _free_internal (ptr)
_heapindex = 0;
/* Allocate new space for the info table and move its data. */
- newinfo = (malloc_info *) _malloc_internal (info_blocks
+ newinfo = (malloc_info *) imalloc (info_blocks
* BLOCKSIZE);
memmove (newinfo, _heapinfo, info_blocks * BLOCKSIZE);
_heapinfo = newinfo;
@@ -1043,16 +910,16 @@ _free_internal (ptr)
/* Now see if we can return stuff to the system. */
if (block + blocks == _heaplimit && blocks >= lesscore_threshold)
{
- register __malloc_size_t bytes = blocks * BLOCKSIZE;
+ register size_t bytes = blocks * BLOCKSIZE;
_heaplimit -= blocks;
- (*__morecore) (-bytes);
+ default_morecore (-bytes);
_heapinfo[_heapinfo[block].free.prev].free.next
= _heapinfo[block].free.next;
_heapinfo[_heapinfo[block].free.next].free.prev
= _heapinfo[block].free.prev;
block = _heapinfo[block].free.prev;
- --_chunks_free;
- _bytes_free -= bytes;
+ --chunks_free;
+ bytes_free -= bytes;
}
}
@@ -1062,10 +929,10 @@ _free_internal (ptr)
default:
/* Do some of the statistics. */
- --_chunks_used;
- _bytes_used -= 1 << type;
- ++_chunks_free;
- _bytes_free += 1 << type;
+ --chunks_used;
+ bytes_used -= 1 << type;
+ ++chunks_free;
+ bytes_free += 1 << type;
/* Get the address of the first free fragment in this block. */
prev = (struct list *) ((char *) ADDRESS (block) +
@@ -1076,7 +943,7 @@ _free_internal (ptr)
/* If all fragments of this block are free, remove them
from the fragment list and free the whole block. */
next = prev;
- for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
+ for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i)
next = next->next;
prev->prev->next = next;
if (next != NULL)
@@ -1085,12 +952,12 @@ _free_internal (ptr)
_heapinfo[block].busy.info.size = 1;
/* Keep the statistics accurate. */
- ++_chunks_used;
- _bytes_used += BLOCKSIZE;
- _chunks_free -= BLOCKSIZE >> type;
- _bytes_free -= BLOCKSIZE;
+ ++chunks_used;
+ bytes_used += BLOCKSIZE;
+ chunks_free -= BLOCKSIZE >> type;
+ bytes_free -= BLOCKSIZE;
- free (ADDRESS (block));
+ ifree (ADDRESS (block));
}
else if (_heapinfo[block].busy.info.frag.nfree != 0)
{
@@ -1128,76 +995,46 @@ _free_internal (ptr)
/* Return memory to the heap. */
void
free (ptr)
- __ptr_t ptr;
+ genptr_t ptr;
{
- if (__free_hook != NULL)
- (*__free_hook) (ptr);
- else
- _free_internal (ptr);
-}
-
-/* Define the `cfree' alias for `free'. */
-#ifdef weak_alias
-weak_alias (free, cfree)
-#else
-void
-cfree (ptr)
- __ptr_t ptr;
-{
- free (ptr);
-}
+#ifdef RCHECK
+ struct hdr *hdr;
#endif
-/* Change the size of a block allocated by `malloc'.
- Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
- Written May 1989 by Mike Haertel.
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This 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
-Library General Public License for more details.
-You should have received a copy of the GNU Library General Public
-License along with this library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.
+ nfree++;
- The author may be reached (Email) at the address mike@ai.mit.edu,
- or (US mail) as Mike Haertel c/o Free Software Foundation. */
+ if (ptr == 0)
+ return;
-#ifndef _MALLOC_INTERNAL
-#define _MALLOC_INTERNAL
-#include <malloc.h>
+#ifdef RCHECK
+ hdr = ((struct hdr *) ptr) - 1;
+ checkhdr (hdr);
+ hdr->magic = MAGICFREE;
+ zmemset (ptr, FREEFLOOD, hdr->size);
+ ifree (hdr);
+#else
+ ifree (ptr);
#endif
+}
+/* Change the size of a block allocated by `malloc'. */
-
-/* Cope with systems lacking `memmove'. */
-#if (defined (MEMMOVE_MISSING) || \
- !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
-
-#ifdef emacs
-#undef __malloc_safe_bcopy
-#define __malloc_safe_bcopy safe_bcopy
-#else
-
+#ifndef HAVE_MEMMOVE
/* Snarfed directly from Emacs src/dispnew.c:
XXX Should use system bcopy if it handles overlap. */
/* Like bcopy except never gets confused by overlap. */
-void
-__malloc_safe_bcopy (afrom, ato, size)
- __ptr_t afrom;
- __ptr_t ato;
- __malloc_size_t size;
+static void
+malloc_safe_bcopy (afrom, ato, size)
+ genptr_t afrom;
+ genptr_t ato;
+ size_t size;
{
- char *from = afrom, *to = ato;
+ char *from, *to;
+ from = afrom;
+ to = ato;
if (size <= 0 || from == to)
return;
@@ -1245,20 +1082,7 @@ __malloc_safe_bcopy (afrom, ato, size)
}
}
}
-#endif /* emacs */
-
-#ifndef memmove
-extern void __malloc_safe_bcopy __P ((__ptr_t, __ptr_t, __malloc_size_t));
-#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
-#endif
-
-#endif
-
-
-#define min(A, B) ((A) < (B) ? (A) : (B))
-
-/* Debugging hook for realloc. */
-__ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
+#endif /* !HAVE_MEMMOVE */
/* Resize the given region to the new size, returning a pointer
to the (possibly moved) region. This is optimized for speed;
@@ -1266,22 +1090,22 @@ __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
achieved by unconditionally allocating and copying to a
new region. This module has incestuous knowledge of the
internals of both free and malloc. */
-__ptr_t
-_realloc_internal (ptr, size)
- __ptr_t ptr;
- __malloc_size_t size;
+static genptr_t
+irealloc (ptr, size)
+ genptr_t ptr;
+ size_t size;
{
- __ptr_t result;
+ genptr_t result;
int type;
- __malloc_size_t block, blocks, oldlimit;
+ size_t block, blocks, oldlimit;
if (size == 0)
{
- _free_internal (ptr);
- return _malloc_internal (0);
+ ifree (ptr);
+ return imalloc (0);
}
else if (ptr == NULL)
- return _malloc_internal (size);
+ return imalloc (size);
block = BLOCK (ptr);
@@ -1292,11 +1116,11 @@ _realloc_internal (ptr, size)
/* Maybe reallocate a large block to a small fragment. */
if (size <= BLOCKSIZE / 2)
{
- result = _malloc_internal (size);
+ result = imalloc (size);
if (result != NULL)
{
memcpy (result, ptr, size);
- _free_internal (ptr);
+ ifree (ptr);
return result;
}
}
@@ -1314,9 +1138,9 @@ _realloc_internal (ptr, size)
_heapinfo[block].busy.info.size = blocks;
/* We have just created a new chunk by splitting a chunk in two.
Now we will free this chunk; increment the statistics counter
- so it doesn't become wrong when _free_internal decrements it. */
- ++_chunks_used;
- _free_internal (ADDRESS (block + blocks));
+ so it doesn't become wrong when ifree decrements it. */
+ ++chunks_used;
+ ifree (ADDRESS (block + blocks));
result = ptr;
}
else if (blocks == _heapinfo[block].busy.info.size)
@@ -1331,8 +1155,8 @@ _realloc_internal (ptr, size)
/* Prevent free from actually returning memory to the system. */
oldlimit = _heaplimit;
_heaplimit = 0;
- _free_internal (ptr);
- result = _malloc_internal (size);
+ ifree (ptr);
+ result = imalloc (size);
if (_heaplimit == 0)
_heaplimit = oldlimit;
if (result == NULL)
@@ -1341,13 +1165,13 @@ _realloc_internal (ptr, size)
the thing we just freed. Unfortunately it might
have been coalesced with its neighbors. */
if (_heapindex == block)
- (void) _malloc_internal (blocks * BLOCKSIZE);
+ (void) imalloc (blocks * BLOCKSIZE);
else
{
- __ptr_t previous
- = _malloc_internal ((block - _heapindex) * BLOCKSIZE);
- (void) _malloc_internal (blocks * BLOCKSIZE);
- _free_internal (previous);
+ genptr_t previous;
+ previous = imalloc ((block - _heapindex) * BLOCKSIZE);
+ (void) imalloc (blocks * BLOCKSIZE);
+ ifree (previous);
}
return NULL;
}
@@ -1359,19 +1183,19 @@ _realloc_internal (ptr, size)
default:
/* Old size is a fragment; type is logarithm
to base two of the fragment size. */
- if (size > (__malloc_size_t) (1 << (type - 1)) &&
- size <= (__malloc_size_t) (1 << type))
+ if (size > (size_t) (1 << (type - 1)) &&
+ size <= (size_t) (1 << type))
/* The new size is the same kind of fragment. */
result = ptr;
else
{
/* The new size is different; allocate a new space,
and copy the lesser of the new size and the old. */
- result = _malloc_internal (size);
+ result = imalloc (size);
if (result == NULL)
return NULL;
- memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
- _free_internal (ptr);
+ memcpy (result, ptr, min (size, (size_t) 1 << type));
+ ifree (ptr);
}
break;
}
@@ -1379,148 +1203,75 @@ _realloc_internal (ptr, size)
return result;
}
-__ptr_t
+genptr_t
realloc (ptr, size)
- __ptr_t ptr;
- __malloc_size_t size;
+ genptr_t ptr;
+ size_t size;
{
- if (!__malloc_initialized && !__malloc_initialize ())
+#ifdef RCHECK
+ struct hdr *hdr;
+ size_t osize;
+#endif
+
+ if (malloc_initialized == 0 && malloc_initialize () == 0)
return NULL;
- return (__realloc_hook != NULL ? *__realloc_hook : _realloc_internal)
- (ptr, size);
-}
-/* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
+ nrealloc++;
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+#ifdef RCHECK
+ hdr = ((struct hdr *) ptr) - 1;
+ osize = hdr->size;
-This 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
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with this library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.
-
- The author may be reached (Email) at the address mike@ai.mit.edu,
- or (US mail) as Mike Haertel c/o Free Software Foundation. */
+ checkhdr (hdr);
+ if (size < osize)
+ zmemset ((char *) ptr + size, FREEFLOOD, osize - size);
+ hdr = (struct hdr *) irealloc ((genptr_t) hdr, sizeof (struct hdr) + size + 1);
+ if (hdr == NULL)
+ return NULL;
-#ifndef _MALLOC_INTERNAL
-#define _MALLOC_INTERNAL
-#include <malloc.h>
+ hdr->size = size;
+ hdr->magic = MAGICWORD;
+ ((char *) &hdr[1])[size] = MAGICBYTE;
+ if (size > osize)
+ zmemset ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
+ return (genptr_t) (hdr + 1);
+#else
+ return (irealloc (ptr, size));
#endif
+}
/* Allocate an array of NMEMB elements each SIZE bytes long.
The entire array is initialized to zeros. */
-__ptr_t
+genptr_t
calloc (nmemb, size)
- register __malloc_size_t nmemb;
- register __malloc_size_t size;
+ register size_t nmemb;
+ register size_t size;
{
- register __ptr_t result = malloc (nmemb * size);
+ register genptr_t result;
+ result = malloc (nmemb * size);
if (result != NULL)
(void) memset (result, 0, nmemb * size);
return result;
}
-/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
-This file is part of the GNU C Library.
-The GNU C 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 C 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.
-
-You should have received a copy of the GNU General Public License
-along with the GNU C Library; see the file COPYING. If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
-
-#ifndef _MALLOC_INTERNAL
-#define _MALLOC_INTERNAL
-#include <malloc.h>
-#endif
-
-#ifndef __GNU_LIBRARY__
-#define __sbrk sbrk
-#endif
-
-#ifdef __GNU_LIBRARY__
-/* It is best not to declare this and cast its result on foreign operating
- systems with potentially hostile include files. */
-
-#include <stddef.h>
-extern __ptr_t __sbrk __P ((ptrdiff_t increment));
-#endif
-
-#ifndef NULL
-#define NULL 0
-#endif
-
-/* Allocate INCREMENT more bytes of data space,
- and return the start of data space, or NULL on errors.
- If INCREMENT is negative, shrink data space. */
-__ptr_t
-__default_morecore (increment)
- __malloc_ptrdiff_t increment;
+/* Define the `cfree' alias for `free'. */
+void
+cfree (ptr)
+ genptr_t ptr;
{
- __ptr_t result = (__ptr_t) __sbrk (increment);
- if (result == (__ptr_t) -1)
- return NULL;
- return result;
+ free (ptr);
}
-/* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This 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
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with this library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA. */
-
-#ifndef _MALLOC_INTERNAL
-#define _MALLOC_INTERNAL
-#include <malloc.h>
-#endif
-
-#if __DJGPP__ - 0 == 1
-
-/* There is some problem with memalign in DJGPP v1 and we are supposed
- to omit it. Noone told me why, they just told me to do it. */
-
-#else
-
-__ptr_t (*__memalign_hook) __P ((size_t __size, size_t __alignment));
-
-__ptr_t
+genptr_t
memalign (alignment, size)
- __malloc_size_t alignment;
- __malloc_size_t size;
+ size_t alignment;
+ size_t size;
{
- __ptr_t result;
+ genptr_t result;
unsigned long int adj, lastadj;
- if (__memalign_hook)
- return (*__memalign_hook) (alignment, size);
-
/* Allocate a block with enough extra space to pad the block with up to
(ALIGNMENT - 1) bytes if necessary. */
result = malloc (size + alignment - 1);
@@ -1560,7 +1311,7 @@ memalign (alignment, size)
break;
if (l == NULL)
{
- l = (struct alignlist *) malloc (sizeof (struct alignlist));
+ l = (struct alignlist *) imalloc (sizeof (struct alignlist));
if (l == NULL)
{
free (result);
@@ -1576,4 +1327,160 @@ memalign (alignment, size)
return result;
}
-#endif /* Not DJGPP v1 */
+/* On some ANSI C systems, some libc functions call _malloc, _free
+ and _realloc. Make them use the GNU functions. */
+
+genptr_t
+_malloc (size)
+ size_t size;
+{
+ return malloc (size);
+}
+
+void
+_free (ptr)
+ genptr_t ptr;
+{
+ free (ptr);
+}
+
+genptr_t
+_realloc (ptr, size)
+ genptr_t ptr;
+ size_t size;
+{
+ return realloc (ptr, size);
+}
+
+struct mstats
+mstats ()
+{
+ struct mstats result;
+
+ result.bytes_total = (char *) default_morecore (0) - _heapbase;
+ result.chunks_used = chunks_used;
+ result.bytes_used = bytes_used;
+ result.chunks_free = chunks_free;
+ result.bytes_free = bytes_free;
+ result.nmalloc = nmalloc;
+ result.nrealloc = nrealloc;
+ result.nfree = nfree;
+ result.nsbrk = nsbrk;
+ result.tsbrk = tsbrk;
+ result.negsbrk = negsbrk;
+ result.tnegsbrk = tnegsbrk;
+
+ return result;
+}
+
+#ifdef RCHECK
+/* Standard debugging hooks for `malloc'. */
+
+static void
+zmemset (ptr, val, size)
+ genptr_t ptr;
+ int val;
+ size_t size;
+{
+ char *cp = ptr;
+
+ while (size--)
+ *cp++ = val;
+}
+
+static enum mcheck_status
+checkhdr (hdr)
+ const struct hdr *hdr;
+{
+ enum mcheck_status status;
+
+ switch (hdr->magic)
+ {
+ default:
+ status = MCHECK_HEAD;
+ break;
+ case MAGICFREE:
+ status = MCHECK_FREE;
+ break;
+ case MAGICWORD:
+ if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
+ status = MCHECK_TAIL;
+ else
+ status = MCHECK_OK;
+ break;
+ }
+ if (status != MCHECK_OK)
+ mabort (status);
+ return status;
+}
+
+#ifndef botch
+botch (msg)
+ char *msg;
+{
+ fprintf (stderr, "mcheck: %s\n", msg);
+ fflush (stderr);
+ abort ();
+}
+#endif
+
+static void
+mabort (status)
+ enum mcheck_status status;
+{
+ const char *msg;
+
+ switch (status)
+ {
+ case MCHECK_OK:
+ msg = "memory is consistent, library is buggy";
+ break;
+ case MCHECK_HEAD:
+ msg = "memory clobbered before allocated block";
+ break;
+ case MCHECK_TAIL:
+ msg = "memory clobbered past end of allocated block";
+ break;
+ case MCHECK_FREE:
+ msg = "block freed twice";
+ break;
+ default:
+ msg = "bogus mcheck_status, library is buggy";
+ break;
+ }
+
+ botch (msg);
+}
+
+enum mcheck_status
+mprobe (ptr)
+ genptr_t ptr;
+{
+ return checkhdr ((struct hdr *)ptr);
+}
+
+#ifndef STDIO_H_INCLUDED
+# include <stdio.h>
+#endif
+
+void
+print_malloc_stats (s)
+ char *s;
+{
+ struct mstats ms;
+
+ ms = mstats ();
+ fprintf (stderr, "Memory allocation statistics: %s\n", s ? s : "");
+ fprintf (stderr, "\nTotal chunks in use: %d, total chunks free: %d\n",
+ ms.chunks_used, ms.chunks_free);
+ fprintf (stderr, "Total bytes in use: %u, total bytes free: %u\n",
+ ms.bytes_used, ms.bytes_free);
+ fprintf (stderr, "Total bytes (from heapbase): %d\n", ms.bytes_total);
+ fprintf (stderr, "Total mallocs: %d, total frees: %d, total reallocs: %d\n",
+ ms.nmalloc, ms.nfree, ms.nrealloc);
+ fprintf (stderr, "Total sbrks: %d, total bytes via sbrk: %d\n",
+ ms.nsbrk, ms.tsbrk);
+ fprintf (stderr, "Total negative sbrks: %d, total bytes returned to kernel: %d\n",
+ ms.negsbrk, ms.tnegsbrk);
+}
+#endif /* RCHECK */
diff --git a/lib/malloc/malloc.c b/lib/malloc/malloc.c
index a8b232a..c42ca3c 100644
--- a/lib/malloc/malloc.c
+++ b/lib/malloc/malloc.c
@@ -1,6 +1,6 @@
-/* dynamic memory allocation for GNU. */
+/* malloc.c - dynamic memory allocation for bash. */
-/* Copyright (C) 1985, 1987 Free Software Foundation, Inc.
+/* Copyright (C) 1985, 1987, 1997 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
@@ -41,21 +41,17 @@ what you give them. Help stamp out software-hoarding! */
* Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
* No longer Emacs-specific; can serve as all-purpose malloc for GNU.
* You should call malloc_init to reinitialize after loading dumped Emacs.
- * Call malloc_stats to get info on memory stats if MSTATS turned on.
+ * Call malloc_stats to get info on memory stats if MALLOC_STATS turned on.
* realloc knows how to return same block given, just changing its size,
* if the power of 2 is correct.
*/
+#define MALLOC_STATS /* for the time being */
/*
* nextf[i] is the pointer to the next free block of size 2^(i+3). The
* smallest allocatable block is 8 bytes. The overhead information will
* go in the first int of the block, and the returned pointer will point
* to the second.
- *
-#ifdef MSTATS
- * nmalloc[i] is the difference between the number of mallocs and frees
- * for a given block size.
-#endif
*/
/* Define this to have free() write 0xcf into memory as it's freed, to
@@ -65,38 +61,38 @@ what you give them. Help stamp out software-hoarding! */
# define MEMSCRAMBLE
#endif
-#if defined (emacs) || defined (HAVE_CONFIG_H)
+#if defined (HAVE_CONFIG_H)
# include <config.h>
-#endif /* emacs */
+#endif /* HAVE_CONFIG_H */
+
+#if defined (SHELL)
+# include "bashtypes.h"
+#else
+# include <sys/types.h>
+#endif
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
/* Determine which kind of system this is. */
-#if defined (SHELL)
-# include "bashtypes.h"
+#include <signal.h>
+
+#if defined (HAVE_STRING_H)
+# include <string.h>
#else
-# include <sys/types.h>
+# include <strings.h>
#endif
-#include <signal.h>
+
+#if defined (MALLOC_STATS) || !defined (botch)
+# include <stdio.h>
+#endif /* MALLOC_STATS || !botch */
/* Define getpagesize () if the system does not. */
#ifndef HAVE_GETPAGESIZE
# include "getpagesize.h"
#endif
-#if defined (HAVE_RESOURCE)
-# include <sys/time.h>
-# include <sys/resource.h>
-#endif /* HAVE_RESOURCE */
-
-/* Check for the needed symbols. If they aren't present, this
- system's <sys/resource.h> isn't very useful to us. */
-#if !defined (RLIMIT_DATA)
-# undef HAVE_RESOURCE
-#endif
-
#if __GNUC__ > 1
# define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
#else /* !__GNUC__ */
@@ -115,7 +111,7 @@ what you give them. Help stamp out software-hoarding! */
# define NULL 0
#endif
-#define start_of_data() &etext
+#define NBUCKETS 30
#define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
#define ISFREE ((char) 0x54) /* magic byte that implies free block */
@@ -124,147 +120,275 @@ what you give them. Help stamp out software-hoarding! */
memalign, with the rest of the word
being the distance to the true
beginning of the block. */
-extern char etext;
#if !defined (SBRK_DECLARED)
extern char *sbrk ();
#endif /* !SBRK_DECLARED */
-/* These two are for user programs to look at, when they are interested. */
-unsigned int malloc_sbrk_used; /* amount of data space used now */
-unsigned int malloc_sbrk_unused; /* amount more we can have */
-
-/* start of data space; can be changed by calling init_malloc */
-static char *data_space_start;
-
-static void get_lim_data ();
-
-#ifdef MSTATS
-static int nmalloc[30];
-static int nmal, nfre;
-#endif /* MSTATS */
-
-/* If range checking is not turned on, all we have is a flag indicating
- whether memory is allocated, an index in nextf[], and a size field; to
- realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
- on whether the former can hold the exact size (given the value of
- 'index'). If range checking is on, we always need to know how much space
- is allocated, so the 'size' field is never used. */
-
-struct mhead {
- char mh_alloc; /* ISALLOC or ISFREE */
- char mh_index; /* index in nextf[] */
-/* Remainder are valid only when block is allocated */
- unsigned short mh_size; /* size, if < 0x10000 */
-#ifdef RCHECK
- unsigned int mh_nbytes; /* number of bytes allocated */
- int mh_magic4; /* should be == MAGIC4 */
-#endif /* RCHECK */
+#ifdef MALLOC_STATS
+/*
+ * NMALLOC[i] is the difference between the number of mallocs and frees
+ * for a given block size. TMALLOC[i] is the total number of mallocs for
+ * a given block size. NMORECORE[i] is the total number of calls to
+ * morecore(i). NMAL and NFRE are counts of the number of calls to malloc()
+ * and free(), respectively. NREALLOC is the total number of calls to
+ * realloc(); NRCOPY is the number of times realloc() had to allocate new
+ * memory and copy to it. NRECURSE is a count of the number of recursive
+ * calls to malloc() for the same bucket size, which can be caused by calls
+ * to malloc() from a signal handler. NSBRK is the number of calls to sbrk()
+ * (whether by morecore() or for alignment); TSBRK is the total number of
+ * bytes requested from the kernel with sbrk(). BYTESUSED is the total
+ * number of bytes consumed by blocks currently in use; BYTESFREE is the
+ * total number of bytes currently on all of the free lists. NBSPLIT is
+ * the number of times a larger block was split to satisfy a smaller request.
+ * NBCOALESCE is the number of times two adjacent smaller blocks off the free
+ * list were combined to satisfy a larger request.
+ */
+struct _malstats {
+ int nmalloc[NBUCKETS];
+ int tmalloc[NBUCKETS];
+ int nmorecore[NBUCKETS];
+ int nmal;
+ int nfre;
+ int nrealloc;
+ int nrcopy;
+ int nrecurse;
+ int nsbrk;
+ int32_t tsbrk;
+ int32_t bytesused;
+ int32_t bytesfree;
+ int nbsplit;
+ int nbcoalesce;
+};
+
+static struct _malstats _mstats;
+
+/* Return statistics describing allocation of blocks of size BLOCKSIZE.
+ NFREE is the number of free blocks for this allocation size. NUSED
+ is the number of blocks in use. NMAL is the number of requests for
+ blocks of size BLOCKSIZE. NMORECORE is the number of times we had
+ to call MORECORE to repopulate the free list for this bucket. */
+struct bucket_stats {
+ u_int32_t blocksize;
+ int nfree;
+ int nused;
+ int nmal;
+ int nmorecore;
+};
+#endif /* MALLOC_STATS */
+
+/* We have a flag indicating whether memory is allocated, an index in
+ nextf[], a size field, and a sentinel value to determine whether or
+ not a caller wrote before the start of allocated memory; to realloc()
+ memory we either copy mh_nbytes or just change mh_nbytes if there is
+ enough room in the block for the new size. Range checking is always
+ done. */
+union mhead {
+ union mhead *mh_align;
+ struct {
+ char mi_alloc; /* ISALLOC or ISFREE */ /* 1 */
+ char mi_index; /* index in nextf[] */ /* 1 */
+ /* Remainder are valid only when block is allocated */
+ u_int32_t mi_nbytes; /* # of bytes allocated */ /* 4 */
+ unsigned short mi_magic2;/* should be == MAGIC2 */ /* 2 */
+ } minfo;
};
+#define mh_alloc minfo.mi_alloc
+#define mh_index minfo.mi_index
+#define mh_nbytes minfo.mi_nbytes
+#define mh_magic2 minfo.mi_magic2
/* Access free-list pointer of a block.
- It is stored at block + 4.
- This is not a field in the mhead structure
- because we want sizeof (struct mhead)
- to describe the overhead for when the block is in use,
- and we do not want the free-list pointer to count in that. */
+ It is stored at block + sizeof (char *).
+ This is not a field in the mhead structure
+ because we want sizeof (struct mhead)
+ to describe the overhead for when the block is in use,
+ and we do not want the free-list pointer to count in that. */
#define CHAIN(a) \
- (*(struct mhead **) (sizeof (char *) + (char *) (a)))
+ (*(union mhead **) (sizeof (char *) + (char *) (a)))
-#ifdef RCHECK
-# include <stdio.h>
-# if !defined (botch)
-# define botch(x) abort ()
-# else
-extern void botch();
-# endif /* botch */
+#if defined (botch)
+extern void botch ();
+#else
+static void
+botch (s)
+ char *s;
+{
+ fprintf (stderr, "\r\nmalloc: assertion botched: %s\r\n", s);
+ (void)fflush (stderr);
+ abort ();
+}
+#endif /* !botch */
-# if !defined (__STRING)
-# if defined (__STDC__)
-# define __STRING(x) #x
-# else
-# define __STRING(x) "x"
-# endif
+#if !defined (__STRING)
+# if defined (__STDC__)
+# define __STRING(x) #x
+# else
+# define __STRING(x) "x"
# endif
+#endif /* !__STRING */
- /* To implement range checking, we write magic values in at the beginning
- and end of each allocated block, and make sure they are undisturbed
- whenever a free or a realloc occurs. */
-
- /* Written in each of the 4 bytes following the block's real space */
-# define MAGIC1 0x55
- /* Written in the 4 bytes before the block's real space */
-# define MAGIC4 0x55555555
-# define ASSERT(p) if (!(p)) botch(__STRING(p)); else
-# define EXTRA 4 /* 4 bytes extra for MAGIC1s */
-#else /* !RCHECK */
-# define ASSERT(p)
-# define EXTRA 0
-#endif /* RCHECK */
+/* To implement range checking, we write magic values in at the beginning
+ and end of each allocated block, and make sure they are undisturbed
+ whenever a free or a realloc occurs. */
+
+/* Written in each of the 4 bytes following the block's real space */
+#define MAGIC1 0x55
+/* Written in the 2 bytes before the block's real space */
+#define MAGIC2 0x5555
+#define ASSERT(p) do { if (!(p)) botch(__STRING(p)); } while (0)
+#define MSLOP 4 /* 4 bytes extra for MAGIC1s */
+
+/* Minimum and maximum bucket indices for block splitting (and to bound
+ the search for a block to split). */
+#define SPLIT_MIN 3
+#define SPLIT_MID 9
+#define SPLIT_MAX 12
+
+/* Minimum and maximum bucket indices for block coalescing. */
+#define COMBINE_MIN 6
+#define COMBINE_MAX (pagebucket - 1)
+
+#define MIN_COMBINE_FREE 4
/* nextf[i] is free list of blocks of size 2**(i + 3) */
-static struct mhead *nextf[30];
+static union mhead *nextf[NBUCKETS];
/* busy[i] is nonzero while allocation of block size i is in progress. */
-static char busy[30];
+static char busy[NBUCKETS];
-/* Number of bytes of writable memory we can expect to be able to get */
-static unsigned int lim_data;
+static int pagesz; /* system page size. */
+static int pagebucket; /* bucket for requests a page in size */
-/* Level number of warnings already issued.
- 0 -- no warnings issued.
- 1 -- 75% warning already issued.
- 2 -- 85% warning already issued.
-*/
-static int warnlevel;
+#if 0
+/* Coalesce two adjacent free blocks off the free list for size NU - 1,
+ as long as there are at least MIN_COMBINE_FREE free blocks and we
+ can find two adjacent free blocks. nextf[NU -1] is assumed to not
+ be busy; the caller (morecore()) checks for this. */
+static void
+bcoalesce (nu)
+ register int nu;
+{
+ register union mhead *mp, *mp1, *mp2;
+ register int nfree, nbuck;
+ unsigned long siz;
-/* Function to call to issue a warning;
- 0 means don't issue them. */
-static void (*warnfunction) ();
+ nbuck = nu - 1;
+ if (nextf[nbuck] == 0)
+ return;
-/* nonzero once initial bunch of free blocks made */
-static int gotpool;
+ nfree = 1;
+ mp1 = nextf[nbuck];
+ mp = CHAIN (mp1);
+ mp2 = (union mhead *)0;
+ while (CHAIN (mp))
+ {
+ mp2 = mp1;
+ mp1 = mp;
+ mp = CHAIN (mp);
+ nfree++;
+ /* We may not want to run all the way through the free list here;
+ if we do not, we need to check a threshold value here and break
+ if nfree exceeds it. */
+ }
+ if (nfree < MIN_COMBINE_FREE)
+ return;
+ /* OK, now we have mp1 pointing to the block we want to add to nextf[NU].
+ CHAIN(mp2) must equal mp1. Check that mp1 and mp are adjacent. */
+ if (CHAIN(mp2) != mp1)
+ botch ("bcoalesce: CHAIN(mp2) != mp1");
+ siz = 1 << (nbuck + 3);
+ if (CHAIN (mp1) != (union mhead *)((char *)mp1 + siz))
+ return; /* not adjacent */
+
+#ifdef MALLOC_STATS
+ _mstats.nbcoalesce++;
+#endif
-char *_malloc_base;
+ /* Since they are adjacent, remove them from the free list */
+ CHAIN (mp2) = CHAIN (mp);
-static void getpool ();
+ /* And add the combined two blocks to nextf[NU]. */
+ mp1->mh_alloc = ISFREE;
+ mp1->mh_index = nu;
+ CHAIN (mp1) = nextf[nu];
+ nextf[nu] = mp1;
+}
+#endif
-/* Cause reinitialization based on job parameters;
- also declare where the end of pure storage is. */
-void
-malloc_init (start, warnfun)
- char *start;
- void (*warnfun) ();
+/* Split a block at index > NU (but less than SPLIT_MAX) into a set of
+ blocks of the correct size, and attach them to nextf[NU]. nextf[NU]
+ is assumed to be empty. Must be called with signals blocked (e.g.,
+ by morecore()). */
+static void
+bsplit (nu)
+ register int nu;
{
- if (start)
- data_space_start = start;
- lim_data = 0;
- warnlevel = 0;
- warnfunction = warnfun;
-}
+ register union mhead *mp;
+ int nbuck, nblks;
+ unsigned long siz;
-/* Return the maximum size to which MEM can be realloc'd
- without actually requiring copying. */
+ if (nu >= SPLIT_MID)
+ {
+ for (nbuck = SPLIT_MAX; nbuck > nu; nbuck--)
+ {
+ if (busy[nbuck] || nextf[nbuck] == 0)
+ continue;
+ break;
+ }
+ }
+ else
+ {
+ for (nbuck = nu + 1; nbuck <= SPLIT_MAX; nbuck++)
+ {
+ if (busy[nbuck] || nextf[nbuck] == 0)
+ continue;
+ break;
+ }
+ }
-int
-malloc_usable_size (mem)
- char *mem;
-{
- int blocksize = 8 << (((struct mhead *) mem) - 1) -> mh_index;
+ if (nbuck > SPLIT_MAX || nbuck <= nu)
+ return;
+
+ /* XXX might want to split only if nextf[nbuck] has >= 2 blocks free
+ and nbuck is below some threshold. */
+
+#ifdef MALLOC_STATS
+ _mstats.nbsplit++;
+#endif
+
+ /* Figure out how many blocks we'll get. */
+ siz = (1 << (nu + 3));
+ nblks = (1 << (nbuck + 3)) / siz;
- return blocksize - sizeof (struct mhead) - EXTRA;
+ /* Remove the block from the chain of larger blocks. */
+ mp = nextf[nbuck];
+ nextf[nbuck] = CHAIN (mp);
+
+ /* Split the block and put it on the requested chain. */
+ nextf[nu] = mp;
+ while (1)
+ {
+ mp->mh_alloc = ISFREE;
+ mp->mh_index = nu;
+ if (--nblks <= 0) break;
+ CHAIN (mp) = (union mhead *)((char *)mp + siz);
+ mp = (union mhead *)((char *)mp + siz);
+ }
+ CHAIN (mp) = 0;
}
static void
morecore (nu) /* ask system for more memory */
register int nu; /* size index to get more of */
{
- register char *cp;
+ register union mhead *mp;
register int nblks;
- register unsigned int siz;
+ register long siz;
+ long sbrk_amt; /* amount to get via sbrk() */
/* Block all signals in case we are executed from a signal handler. */
#if defined (HAVE_BSD_SIGNALS)
@@ -279,82 +403,88 @@ morecore (nu) /* ask system for more memory */
# endif /* HAVE_POSIX_SIGNALS */
#endif /* HAVE_BSD_SIGNALS */
- if (!data_space_start)
+ siz = 1 << (nu + 3); /* size of desired block for nextf[nu] */
+
+ if (siz < 0)
+ return; /* oops */
+
+#ifdef MALLOC_STATS
+ _mstats.nmorecore[nu]++;
+#endif
+
+ /* Try to split a larger block here, if we're within the range of sizes
+ to split. */
+ if (nu >= SPLIT_MIN && nu < SPLIT_MAX)
+ {
+ bsplit (nu);
+ if (nextf[nu] != 0)
+ goto morecore_done;
+ }
+
+#if 0
+ /* Try to coalesce two adjacent blocks from the free list on nextf[nu - 1],
+ if we can, and we're withing the range of the block coalescing limits. */
+ if (nu >= COMBINE_MIN && nu < COMBINE_MAX && busy[nu - 1] == 0 && nextf[nu - 1])
+ {
+ bcoalesce (nu);
+ if (nextf[nu] != 0)
+ goto morecore_done;
+ }
+#endif
+
+ /* Take at least a page, and figure out how many blocks of the requested
+ size we're getting. */
+ if (siz <= pagesz)
{
- data_space_start = start_of_data ();
+ sbrk_amt = pagesz;
+ nblks = sbrk_amt / siz;
}
+ else
+ {
+ /* We always want to request an integral multiple of the page size
+ from the kernel, so let's compute whether or not `siz' is such
+ an amount. If it is, we can just request it. If not, we want
+ the smallest integral multiple of pagesize that is larger than
+ `siz' and will satisfy the request. */
+ sbrk_amt = siz % pagesz;
+ if (sbrk_amt == 0)
+ sbrk_amt = siz;
+ else
+ sbrk_amt = siz + pagesz - sbrk_amt;
+ nblks = 1;
+ }
+
+#ifdef MALLOC_STATS
+ _mstats.nsbrk++;
+ _mstats.tsbrk += sbrk_amt;
+#endif
+
+ mp = (union mhead *) sbrk (sbrk_amt);
- if (lim_data == 0)
- get_lim_data ();
-
- /* On initial startup, get two blocks of each size up to 1k bytes */
- if (!gotpool)
- { getpool (); getpool (); gotpool = 1; }
-
- /* Find current end of memory and issue warning if getting near max */
-
- cp = sbrk (0);
- siz = cp - data_space_start;
- malloc_sbrk_used = siz;
- malloc_sbrk_unused = lim_data - siz;
-
- if (warnfunction)
- switch (warnlevel)
- {
- case 0:
- if (siz > (lim_data / 4) * 3)
- {
- warnlevel++;
- (*warnfunction) ("Warning: past 75% of memory limit");
- }
- break;
- case 1:
- if (siz > (lim_data / 20) * 17)
- {
- warnlevel++;
- (*warnfunction) ("Warning: past 85% of memory limit");
- }
- break;
- case 2:
- if (siz > (lim_data / 20) * 19)
- {
- warnlevel++;
- (*warnfunction) ("Warning: past 95% of memory limit");
- }
- break;
- }
-
- if ((int) cp & 0x3ff) /* land on 1K boundaries */
- sbrk (1024 - ((int) cp & 0x3ff));
-
- /* Take at least 2k, and figure out how many blocks of the desired size
- we're about to get */
- nblks = 1;
- if ((siz = nu) < 8)
- nblks = 1 << ((siz = 8) - nu);
-
- if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
- return; /* no more room! */
-
- if ((int) cp & 7)
- { /* shouldn't happen, but just in case */
- cp = (char *) (((int) cp + 8) & ~7);
+ /* Totally out of memory. */
+ if ((long)mp == -1)
+ return;
+
+ /* shouldn't happen, but just in case -- require 8-byte alignment */
+ if ((long)mp & 7)
+ {
+ mp = (union mhead *) (((long)mp + 8) & ~7);
nblks--;
}
- /* save new header and link the nblks blocks together */
- nextf[nu] = (struct mhead *) cp;
- siz = 1 << (nu + 3);
+ /* save new header and link the nblks blocks together */
+ nextf[nu] = mp;
while (1)
{
- ((struct mhead *) cp) -> mh_alloc = ISFREE;
- ((struct mhead *) cp) -> mh_index = nu;
+ mp->mh_alloc = ISFREE;
+ mp->mh_index = nu;
if (--nblks <= 0) break;
- CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
- cp += siz;
+ CHAIN (mp) = (union mhead *)((char *)mp + siz);
+ mp = (union mhead *)((char *)mp + siz);
}
- CHAIN ((struct mhead *) cp) = 0;
+ CHAIN (mp) = 0;
+morecore_done:
#if defined (HAVE_BSD_SIGNALS)
sigsetmask (oldmask);
#else
@@ -364,44 +494,6 @@ morecore (nu) /* ask system for more memory */
#endif /* HAVE_BSD_SIGNALS */
}
-static void
-getpool ()
-{
- register int nu;
- register char *cp = sbrk (0);
-
- if ((int) cp & 0x3ff) /* land on 1K boundaries */
- sbrk (1024 - ((int) cp & 0x3ff));
-
- /* Record address of start of space allocated by malloc. */
- if (_malloc_base == 0)
- _malloc_base = cp;
-
- /* Get 2k of storage */
-
- cp = sbrk (04000);
- if (cp == (char *) -1)
- return;
-
- /* Divide it into an initial 8-word block
- plus one block of size 2**nu for nu = 3 ... 10. */
-
- CHAIN (cp) = nextf[0];
- nextf[0] = (struct mhead *) cp;
- ((struct mhead *) cp) -> mh_alloc = ISFREE;
- ((struct mhead *) cp) -> mh_index = 0;
- cp += 8;
-
- for (nu = 0; nu < 7; nu++)
- {
- CHAIN (cp) = nextf[nu];
- nextf[nu] = (struct mhead *) cp;
- ((struct mhead *) cp) -> mh_alloc = ISFREE;
- ((struct mhead *) cp) -> mh_index = nu;
- cp += 8 << nu;
- }
-}
-
#if defined (MEMSCRAMBLE) || !defined (NO_CALLOC)
static char *
zmemset (s, c, n)
@@ -418,75 +510,130 @@ zmemset (s, c, n)
}
#endif /* MEMSCRAMBLE || !NO_CALLOC */
+static void
+malloc_debug_dummy ()
+{
+ ;
+}
+
char *
malloc (n) /* get a block */
- unsigned int n;
+ size_t n;
{
- register struct mhead *p;
- register unsigned int nbytes;
- register int nunits = 0;
+ register union mhead *p;
+ register long nbytes;
+ register int nunits;
+ /* Get the system page size and align break pointer so everything will
+ be page-aligned. The page size must be at least 1K -- anything
+ smaller is increased. */
+ if (pagesz == 0)
+ {
+ register long sbrk_needed;
+
+ pagesz = getpagesize ();
+ if (pagesz < 1024)
+ pagesz = 1024;
+ /* OK, how much do we need to allocate to make things page-aligned?
+ This partial page is wasted space. Once we figure out how much
+ to advance the break pointer, go ahead and do it. */
+ sbrk_needed = pagesz - ((long)sbrk (0) & (pagesz - 1)); /* sbrk(0) % pagesz */
+ if (sbrk_needed < 0)
+ sbrk_needed += pagesz;
+ /* Now allocate the wasted space. */
+ if (sbrk_needed)
+ {
+#ifdef MALLOC_STATS
+ _mstats.nsbrk++;
+ _mstats.tsbrk += sbrk_needed;
+#endif
+ if ((long)sbrk (sbrk_needed) == -1)
+ return (NULL);
+ }
+ nunits = 0;
+ nbytes = 8;
+ while (pagesz > nbytes)
+ {
+ nbytes <<= 1;
+ nunits++;
+ }
+ pagebucket = nunits;
+ }
+
/* Figure out how many bytes are required, rounding up to the nearest
- multiple of 4, then figure out which nextf[] area to use */
- nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
- {
- register unsigned int shiftr = (nbytes - 1) >> 2;
+ multiple of 4, then figure out which nextf[] area to use. Try to
+ be smart about where to start searching -- if the number of bytes
+ needed is greater than the page size, we can start at pagebucket. */
+ nbytes = (n + sizeof *p + MSLOP + 3) & ~3;
+ nunits = 0;
+ if (nbytes <= (pagesz >> 1))
+ {
+ register unsigned int shiftr;
- while (shiftr >>= 1)
- nunits++;
- }
+ shiftr = (nbytes - 1) >> 2; /* == (nbytes - 1) / 4 */
+ while (shiftr >>= 1) /* == (nbytes - 1) / {8,16,32,...} */
+ nunits++;
+ }
+ else
+ {
+ register u_int32_t amt;
+
+ nunits = pagebucket;
+ amt = pagesz;
+ while (nbytes > amt)
+ {
+ amt <<= 1;
+ nunits++;
+ }
+ }
/* In case this is reentrant use of malloc from signal handler,
pick a block size that no other malloc level is currently
trying to allocate. That's the easiest harmless way not to
interfere with the other level of execution. */
+#ifdef MALLOC_STATS
+ if (busy[nunits]) _mstats.nrecurse++;
+#endif
while (busy[nunits]) nunits++;
busy[nunits] = 1;
/* If there are no blocks of the appropriate size, go get some */
- /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
if (nextf[nunits] == 0)
morecore (nunits);
/* Get one block off the list, and set the new list head */
- if ((p = nextf[nunits]) == 0)
+ if ((p = nextf[nunits]) == NULL)
{
busy[nunits] = 0;
- return 0;
+ return NULL;
}
nextf[nunits] = CHAIN (p);
busy[nunits] = 0;
/* Check for free block clobbered */
- /* If not for this check, we would gobble a clobbered free chain ptr */
- /* and bomb out on the NEXT allocate of this size block */
- if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
-#ifdef RCHECK
- botch ("block on free list clobbered");
-#else /* not RCHECK */
- abort ();
-#endif /* not RCHECK */
+ /* If not for this check, we would gobble a clobbered free chain ptr
+ and bomb out on the NEXT allocate of this size block */
+ if (p->mh_alloc != ISFREE || p->mh_index != nunits)
+ botch ("malloc: block on free list clobbered");
/* Fill in the info, and if range checking, set up the magic numbers */
- p -> mh_alloc = ISALLOC;
-#ifdef RCHECK
- p -> mh_nbytes = n;
- p -> mh_magic4 = MAGIC4;
+ p->mh_alloc = ISALLOC;
+ p->mh_nbytes = n;
+ p->mh_magic2 = MAGIC2;
{
register char *m = (char *) (p + 1) + n;
*m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
}
-#else /* not RCHECK */
- p -> mh_size = n;
-#endif /* not RCHECK */
+
#ifdef MEMSCRAMBLE
zmemset ((char *)(p + 1), 0xdf, n); /* scramble previous contents */
#endif
-#ifdef MSTATS
- nmalloc[nunits]++;
- nmal++;
-#endif /* MSTATS */
+#ifdef MALLOC_STATS
+ _mstats.nmalloc[nunits]++;
+ _mstats.tmalloc[nunits]++;
+ _mstats.nmal++;
+#endif /* MALLOC_STATS */
return (char *) (p + 1);
}
@@ -494,143 +641,123 @@ void
free (mem)
char *mem;
{
- register struct mhead *p;
- {
- register char *ap = mem;
+ register union mhead *p;
+ register char *ap;
+ register int nunits;
+
+ if ((ap = mem) == 0)
+ return;
- if (ap == 0)
- return;
+ p = (union mhead *) ap - 1;
- p = (struct mhead *) ap - 1;
+ if (p->mh_alloc == ISMEMALIGN)
+ {
+ ap -= p->mh_nbytes;
+ p = (union mhead *) ap - 1;
+ }
+
+ if (p->mh_alloc != ISALLOC)
+ {
+ if (p->mh_alloc == ISFREE)
+ botch ("free: called with already freed block argument");
+ else
+ botch ("free: called with unallocated block argument");
+ }
+
+ ASSERT (p->mh_magic2 == MAGIC2);
+ ap += p->mh_nbytes;
+ ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
+ ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
- if (p -> mh_alloc == ISMEMALIGN)
- {
-#ifdef RCHECK
- ap -= p->mh_nbytes;
-#else
- ap -= p->mh_size; /* XXX */
-#endif
- p = (struct mhead *) ap - 1;
- }
-
-#ifndef RCHECK
- if (p -> mh_alloc != ISALLOC)
- abort ();
-
-#else /* RCHECK */
- if (p -> mh_alloc != ISALLOC)
- {
- if (p -> mh_alloc == ISFREE)
- botch ("free: Called with already freed block argument\n");
- else
- botch ("free: Called with unallocated block argument\n");
- }
-
- ASSERT (p -> mh_magic4 == MAGIC4);
- ap += p -> mh_nbytes;
- ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
- ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
-#endif /* RCHECK */
- }
#ifdef MEMSCRAMBLE
- {
- register int n;
-
-#ifdef RCHECK
- n = p->mh_nbytes;
-#else /* not RCHECK */
- n = p->mh_size;
-#endif /* not RCHECK */
- zmemset (mem, 0xcf, n);
- }
+ zmemset (mem, 0xcf, p->mh_nbytes);
#endif
- {
- register int nunits = p -> mh_index;
-
- ASSERT (nunits <= 29);
- p -> mh_alloc = ISFREE;
-
- /* Protect against signal handlers calling malloc. */
- busy[nunits] = 1;
- /* Put this block on the free list. */
- CHAIN (p) = nextf[nunits];
- nextf[nunits] = p;
- busy[nunits] = 0;
-
-#ifdef MSTATS
- nmalloc[nunits]--;
- nfre++;
-#endif /* MSTATS */
- }
+
+ nunits = p->mh_index;
+
+ ASSERT (nunits < NBUCKETS);
+ p->mh_alloc = ISFREE;
+
+ /* Protect against signal handlers calling malloc. */
+ busy[nunits] = 1;
+ /* Put this block on the free list. */
+ CHAIN (p) = nextf[nunits];
+ nextf[nunits] = p;
+ busy[nunits] = 0;
+
+#ifdef MALLOC_STATS
+ _mstats.nmalloc[nunits]--;
+ _mstats.nfre++;
+#endif /* MALLOC_STATS */
}
char *
realloc (mem, n)
char *mem;
- register unsigned int n;
+ register size_t n;
{
- register struct mhead *p;
- register unsigned int tocopy;
+ register union mhead *p;
+ register u_int32_t tocopy;
register unsigned int nbytes;
register int nunits;
+ register char *m;
+
+#ifdef MALLOC_STATS
+ _mstats.nrealloc++;
+#endif
- if ((p = (struct mhead *) mem) == 0)
+ if (n == 0)
+ {
+ free (mem);
+ return (NULL);
+ }
+ if ((p = (union mhead *) mem) == 0)
return malloc (n);
p--;
- nunits = p -> mh_index;
- ASSERT (p -> mh_alloc == ISALLOC);
-#ifdef RCHECK
- ASSERT (p -> mh_magic4 == MAGIC4);
- {
- register char *m = mem + (tocopy = p -> mh_nbytes);
- ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
- ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
- }
-#else /* not RCHECK */
- if (p -> mh_index >= 13)
- tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
- else
- tocopy = p -> mh_size;
-#endif /* not RCHECK */
+ nunits = p->mh_index;
+ ASSERT (p->mh_alloc == ISALLOC);
+ ASSERT (p->mh_magic2 == MAGIC2);
+
+ m = mem + (tocopy = p->mh_nbytes);
+ ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
+ ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
/* See if desired size rounds to same power of 2 as actual size. */
- nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
+ nbytes = (n + sizeof *p + MSLOP + 7) & ~7;
/* If ok, use the same block, just marking its size as changed. */
if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
{
-#ifdef RCHECK
- register char *m = mem + tocopy;
+ m = mem + tocopy;
*m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
- p-> mh_nbytes = n;
+ p->mh_nbytes = n;
m = mem + n;
*m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1;
-#else /* not RCHECK */
- p -> mh_size = n;
-#endif /* not RCHECK */
return mem;
}
+#ifdef MALLOC_STATS
+ _mstats.nrcopy++;
+#endif
+
if (n < tocopy)
tocopy = n;
- {
- register char *new;
- if ((new = malloc (n)) == 0)
- return 0;
- FASTCOPY (mem, new, tocopy);
- free (mem);
- return new;
- }
+ if ((m = malloc (n)) == 0)
+ return 0;
+ FASTCOPY (mem, m, tocopy);
+ free (mem);
+ return m;
}
char *
memalign (alignment, size)
- unsigned int alignment, size;
+ unsigned int alignment;
+ size_t size;
{
register char *ptr;
register char *aligned;
- register struct mhead *p;
+ register union mhead *p;
ptr = malloc (size + alignment);
@@ -644,9 +771,9 @@ memalign (alignment, size)
/* Store a suitable indication of how to free the block,
so that free can find the true beginning of it. */
- p = (struct mhead *) aligned - 1;
- p -> mh_size = aligned - ptr;
- p -> mh_alloc = ISMEMALIGN;
+ p = (union mhead *) aligned - 1;
+ p->mh_nbytes = aligned - ptr;
+ p->mh_alloc = ISMEMALIGN;
return aligned;
}
@@ -688,72 +815,80 @@ cfree (p)
}
#endif /* !NO_CALLOC */
-#ifdef MSTATS
-/* Return statistics describing allocation of blocks of size 2**n. */
-
-struct mstats_value
- {
- int blocksize;
- int nfree;
- int nused;
- };
+#ifdef MALLOC_STATS
-struct mstats_value
-malloc_stats (size)
+struct bucket_stats
+malloc_bucket_stats (size)
int size;
{
- struct mstats_value v;
- register int i;
- register struct mhead *p;
+ struct bucket_stats v;
+ register union mhead *p;
v.nfree = 0;
- if (size < 0 || size >= 30)
+ if (size < 0 || size >= NBUCKETS)
{
v.blocksize = 0;
- v.nused = 0;
+ v.nused = v.nmal = 0;
return v;
}
v.blocksize = 1 << (size + 3);
- v.nused = nmalloc[size];
+ v.nused = _mstats.nmalloc[size];
+ v.nmal = _mstats.tmalloc[size];
+ v.nmorecore = _mstats.nmorecore[size];
for (p = nextf[size]; p; p = CHAIN (p))
v.nfree++;
return v;
}
-#endif /* MSTATS */
-/*
- * This function returns the total number of bytes that the process
- * will be allowed to allocate via the sbrk(2) system call. On
- * BSD systems this is the total space allocatable to stack and
- * data. On USG systems this is the data space only.
- */
-
-#if !defined (HAVE_RESOURCE)
-extern long ulimit ();
+/* Return a copy of _MSTATS, with two additional fields filled in:
+ BYTESFREE is the total number of bytes on free lists. BYTESUSED
+ is the total number of bytes in use. These two fields are fairly
+ expensive to compute, so we do it only when asked to. */
+struct _malstats
+malloc_stats ()
+{
+ struct _malstats result;
+ struct bucket_stats v;
+ register int i;
-static void
-get_lim_data ()
-{
- lim_data = ulimit (3, 0);
- lim_data -= (long) data_space_start;
+ result = _mstats;
+ result.bytesused = result.bytesfree = 0;
+ for (i = 0; i < NBUCKETS; i++)
+ {
+ v = malloc_bucket_stats (i);
+ result.bytesfree += v.nfree * v.blocksize;
+ result.bytesused += v.nused * v.blocksize;
+ }
+ return (result);
}
-#else /* HAVE_RESOURCE */
-static void
-get_lim_data ()
+void
+print_malloc_stats (s)
+ char *s;
{
- struct rlimit XXrlimit;
+ register int i;
+ int totused, totfree;
+ struct bucket_stats v;
- getrlimit (RLIMIT_DATA, &XXrlimit);
-#ifdef RLIM_INFINITY
- lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
-#else
- lim_data = XXrlimit.rlim_cur; /* soft limit */
-#endif
+ fprintf (stderr, "Memory allocation statistics: %s\n\tsize\tfree\tin use\ttotal\tmorecore\n", s ? s : "");
+ for (i = totused = totfree = 0; i < NBUCKETS; i++)
+ {
+ v = malloc_bucket_stats (i);
+ fprintf (stderr, "%12lu\t%4d\t%6d\t%5d\t%8d\n", v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore);
+ totfree += v.nfree * v.blocksize;
+ totused += v.nused * v.blocksize;
+ }
+ fprintf (stderr, "\nTotal bytes in use: %d, total bytes free: %d\n",
+ totused, totfree);
+ fprintf (stderr, "Total mallocs: %d, total frees: %d, total reallocs: %d (%d copies)\n",
+ _mstats.nmal, _mstats.nfre, _mstats.nrealloc, _mstats.nrcopy);
+ fprintf (stderr, "Total sbrks: %d, total bytes via sbrk: %d\n",
+ _mstats.nsbrk, _mstats.tsbrk);
+ fprintf (stderr, "Total blocks split: %d, total block coalesces: %d\n",
+ _mstats.nbsplit, _mstats.nbcoalesce);
}
-
-#endif /* HAVE_RESOURCE */
+#endif /* MALLOC_STATS */
diff --git a/lib/malloc/ogmalloc.c b/lib/malloc/ogmalloc.c
new file mode 100644
index 0000000..8690b12
--- /dev/null
+++ b/lib/malloc/ogmalloc.c
@@ -0,0 +1,1579 @@
+/* DO NOT EDIT THIS FILE -- it is automagically generated. -*- C -*- */
+
+#define _MALLOC_INTERNAL
+
+/* The malloc headers and source files from the C library follow here. */
+
+/* Declarations for `malloc' and friends.
+ Copyright 1990, 91, 92, 93, 95, 96 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_H
+
+#define _MALLOC_H 1
+
+#ifdef _MALLOC_INTERNAL
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
+#include <string.h>
+#else
+#ifndef memset
+#define memset(s, zero, n) bzero ((s), (n))
+#endif
+#ifndef memcpy
+#define memcpy(d, s, n) bcopy ((s), (d), (n))
+#endif
+#endif
+
+#if defined (__GNU_LIBRARY__) || (defined (__STDC__) && __STDC__)
+#include <limits.h>
+#else
+#ifndef CHAR_BIT
+#define CHAR_BIT 8
+#endif
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#endif /* _MALLOC_INTERNAL. */
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
+#undef __P
+#define __P(args) args
+#undef __ptr_t
+#define __ptr_t void *
+#else /* Not C++ or ANSI C. */
+#undef __P
+#define __P(args) ()
+#undef const
+#define const
+#undef __ptr_t
+#define __ptr_t char *
+#endif /* C++ or ANSI C. */
+
+#if defined (__STDC__) && __STDC__
+#include <stddef.h>
+#define __malloc_size_t size_t
+#define __malloc_ptrdiff_t ptrdiff_t
+#else
+#define __malloc_size_t unsigned int
+#define __malloc_ptrdiff_t int
+#endif
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+
+/* Allocate SIZE bytes of memory. */
+extern __ptr_t malloc __P ((__malloc_size_t __size));
+/* Re-allocate the previously allocated block
+ in __ptr_t, making the new block SIZE bytes long. */
+extern __ptr_t realloc __P ((__ptr_t __ptr, __malloc_size_t __size));
+/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
+extern __ptr_t calloc __P ((__malloc_size_t __nmemb, __malloc_size_t __size));
+/* Free a block allocated by `malloc', `realloc' or `calloc'. */
+extern void free __P ((__ptr_t __ptr));
+
+/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
+#if ! (defined (_MALLOC_INTERNAL) && __DJGPP__ - 0 == 1) /* Avoid conflict. */
+extern __ptr_t memalign __P ((__malloc_size_t __alignment,
+ __malloc_size_t __size));
+#endif
+
+/* Allocate SIZE bytes on a page boundary. */
+#if ! (defined (_MALLOC_INTERNAL) && defined (emacs)) /* Avoid conflict. */
+extern __ptr_t valloc __P ((__malloc_size_t __size));
+#endif
+
+
+#ifdef _MALLOC_INTERNAL
+
+/* The allocator divides the heap into blocks of fixed size; large
+ requests receive one or more whole blocks, and small requests
+ receive a fragment of a block. Fragment sizes are powers of two,
+ and all fragments of a block are the same size. When all the
+ fragments in a block have been freed, the block itself is freed. */
+#define INT_BIT (CHAR_BIT * sizeof(int))
+#define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
+#define BLOCKSIZE (1 << BLOCKLOG)
+#define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
+
+/* Determine the amount of memory spanned by the initial heap table
+ (not an absolute limit). */
+#define HEAP (INT_BIT > 16 ? 4194304 : 65536)
+
+/* Number of contiguous free blocks allowed to build up at the end of
+ memory before they will be returned to the system. */
+#define FINAL_FREE_BLOCKS 8
+
+/* Data structure giving per-block information. */
+typedef union
+ {
+ /* Heap information for a busy block. */
+ struct
+ {
+ /* Zero for a large (multiblock) object, or positive giving the
+ logarithm to the base two of the fragment size. */
+ int type;
+ union
+ {
+ struct
+ {
+ __malloc_size_t nfree; /* Free frags in a fragmented block. */
+ __malloc_size_t first; /* First free fragment of the block. */
+ } frag;
+ /* For a large object, in its first block, this has the number
+ of blocks in the object. In the other blocks, this has a
+ negative number which says how far back the first block is. */
+ __malloc_ptrdiff_t size;
+ } info;
+ } busy;
+ /* Heap information for a free block
+ (that may be the first of a free cluster). */
+ struct
+ {
+ __malloc_size_t size; /* Size (in blocks) of a free cluster. */
+ __malloc_size_t next; /* Index of next free cluster. */
+ __malloc_size_t prev; /* Index of previous free cluster. */
+ } free;
+ } malloc_info;
+
+/* Pointer to first block of the heap. */
+extern char *_heapbase;
+
+/* Table indexed by block number giving per-block information. */
+extern malloc_info *_heapinfo;
+
+/* Address to block number and vice versa. */
+#define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
+#define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
+
+/* Current search index for the heap table. */
+extern __malloc_size_t _heapindex;
+
+/* Limit of valid info table indices. */
+extern __malloc_size_t _heaplimit;
+
+/* Doubly linked lists of free fragments. */
+struct list
+ {
+ struct list *next;
+ struct list *prev;
+ };
+
+/* Free list headers for each fragment size. */
+extern struct list _fraghead[];
+
+/* List of blocks allocated with `memalign' (or `valloc'). */
+struct alignlist
+ {
+ struct alignlist *next;
+ __ptr_t aligned; /* The address that memaligned returned. */
+ __ptr_t exact; /* The address that malloc returned. */
+ };
+extern struct alignlist *_aligned_blocks;
+
+/* Instrumentation. */
+extern __malloc_size_t _chunks_used;
+extern __malloc_size_t _bytes_used;
+extern __malloc_size_t _chunks_free;
+extern __malloc_size_t _bytes_free;
+
+/* Internal versions of `malloc', `realloc', and `free'
+ used when these functions need to call each other.
+ They are the same but don't call the hooks. */
+extern __ptr_t _malloc_internal __P ((__malloc_size_t __size));
+extern __ptr_t _realloc_internal __P ((__ptr_t __ptr, __malloc_size_t __size));
+extern void _free_internal __P ((__ptr_t __ptr));
+
+#endif /* _MALLOC_INTERNAL. */
+
+/* Given an address in the middle of a malloc'd object,
+ return the address of the beginning of the object. */
+extern __ptr_t malloc_find_object_address __P ((__ptr_t __ptr));
+
+/* Underlying allocation function; successive calls should
+ return contiguous pieces of memory. */
+extern __ptr_t (*__morecore) __P ((__malloc_ptrdiff_t __size));
+
+/* Default value of `__morecore'. */
+extern __ptr_t __default_morecore __P ((__malloc_ptrdiff_t __size));
+
+/* If not NULL, this function is called after each time
+ `__morecore' is called to increase the data size. */
+extern void (*__after_morecore_hook) __P ((void));
+
+/* Number of extra blocks to get each time we ask for more core.
+ This reduces the frequency of calling `(*__morecore)'. */
+extern __malloc_size_t __malloc_extra_blocks;
+
+/* Nonzero if `malloc' has been called and done its initialization. */
+extern int __malloc_initialized;
+/* Function called to initialize malloc data structures. */
+extern int __malloc_initialize __P ((void));
+
+/* Hooks for debugging versions. */
+extern void (*__malloc_initialize_hook) __P ((void));
+extern void (*__free_hook) __P ((__ptr_t __ptr));
+extern __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
+extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
+extern __ptr_t (*__memalign_hook) __P ((__malloc_size_t __size,
+ __malloc_size_t __alignment));
+
+/* Return values for `mprobe': these are the kinds of inconsistencies that
+ `mcheck' enables detection of. */
+enum mcheck_status
+ {
+ MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
+ MCHECK_OK, /* Block is fine. */
+ MCHECK_FREE, /* Block freed twice. */
+ MCHECK_HEAD, /* Memory before the block was clobbered. */
+ MCHECK_TAIL /* Memory after the block was clobbered. */
+ };
+
+/* Activate a standard collection of debugging hooks. This must be called
+ before `malloc' is ever called. ABORTFUNC is called with an error code
+ (see enum above) when an inconsistency is detected. If ABORTFUNC is
+ null, the standard function prints on stderr and then calls `abort'. */
+extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
+
+/* Check for aberrations in a particular malloc'd block. You must have
+ called `mcheck' already. These are the same checks that `mcheck' does
+ when you free or reallocate a block. */
+extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
+
+/* Activate a standard collection of tracing hooks. */
+extern void mtrace __P ((void));
+extern void muntrace __P ((void));
+
+/* Statistics available to the user. */
+struct mstats
+ {
+ __malloc_size_t bytes_total; /* Total size of the heap. */
+ __malloc_size_t chunks_used; /* Chunks allocated by the user. */
+ __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
+ __malloc_size_t chunks_free; /* Chunks in the free list. */
+ __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
+ };
+
+/* Pick up the current statistics. */
+extern struct mstats mstats __P ((void));
+
+/* Call WARNFUN with a warning message when memory usage is high. */
+extern void memory_warnings __P ((__ptr_t __start,
+ void (*__warnfun) __P ((const char *))));
+
+
+/* Relocating allocator. */
+
+/* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
+extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
+
+/* Free the storage allocated in HANDLEPTR. */
+extern void r_alloc_free __P ((__ptr_t *__handleptr));
+
+/* Adjust the block at HANDLEPTR to be SIZE bytes long. */
+extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* malloc.h */
+/* Memory allocator `malloc'.
+ Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+#include <errno.h>
+
+/* How to really get more memory. */
+__ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
+
+/* Debugging hook for `malloc'. */
+__ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
+
+/* Pointer to the base of the first block. */
+char *_heapbase;
+
+/* Block information table. Allocated with align/__free (not malloc/free). */
+malloc_info *_heapinfo;
+
+/* Number of info entries. */
+static __malloc_size_t heapsize;
+
+/* Search index in the info table. */
+__malloc_size_t _heapindex;
+
+/* Limit of valid info table indices. */
+__malloc_size_t _heaplimit;
+
+/* Free lists for each fragment size. */
+struct list _fraghead[BLOCKLOG];
+
+/* Instrumentation. */
+__malloc_size_t _chunks_used;
+__malloc_size_t _bytes_used;
+__malloc_size_t _chunks_free;
+__malloc_size_t _bytes_free;
+
+/* Are you experienced? */
+int __malloc_initialized;
+
+__malloc_size_t __malloc_extra_blocks;
+
+void (*__malloc_initialize_hook) __P ((void));
+void (*__after_morecore_hook) __P ((void));
+
+
+/* Aligned allocation. */
+static __ptr_t align __P ((__malloc_size_t));
+static __ptr_t
+align (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ unsigned long int adj;
+
+ result = (*__morecore) (size);
+ adj = (unsigned long int) ((unsigned long int) ((char *) result -
+ (char *) NULL)) % BLOCKSIZE;
+ if (adj != 0)
+ {
+ __ptr_t new;
+ adj = BLOCKSIZE - adj;
+ new = (*__morecore) (adj);
+ result = (char *) result + adj;
+ }
+
+ if (__after_morecore_hook)
+ (*__after_morecore_hook) ();
+
+ return result;
+}
+
+/* Get SIZE bytes, if we can get them starting at END.
+ Return the address of the space we got.
+ If we cannot get space at END, fail and return -1. */
+static __ptr_t get_contiguous_space __P ((__malloc_ptrdiff_t, __ptr_t));
+static __ptr_t
+get_contiguous_space (size, position)
+ __malloc_ptrdiff_t size;
+ __ptr_t position;
+{
+ __ptr_t before;
+ __ptr_t after;
+
+ before = (*__morecore) (0);
+ /* If we can tell in advance that the break is at the wrong place,
+ fail now. */
+ if (before != position)
+ return 0;
+
+ /* Allocate SIZE bytes and get the address of them. */
+ after = (*__morecore) (size);
+ if (!after)
+ return 0;
+
+ /* It was not contiguous--reject it. */
+ if (after != position)
+ {
+ (*__morecore) (- size);
+ return 0;
+ }
+
+ return after;
+}
+
+
+/* This is called when `_heapinfo' and `heapsize' have just
+ been set to describe a new info table. Set up the table
+ to describe itself and account for it in the statistics. */
+static void register_heapinfo __P ((void));
+#ifdef __GNUC__
+__inline__
+#endif
+static void
+register_heapinfo ()
+{
+ __malloc_size_t block, blocks;
+
+ block = BLOCK (_heapinfo);
+ blocks = BLOCKIFY (heapsize * sizeof (malloc_info));
+
+ /* Account for the _heapinfo block itself in the statistics. */
+ _bytes_used += blocks * BLOCKSIZE;
+ ++_chunks_used;
+
+ /* Describe the heapinfo block itself in the heapinfo. */
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = blocks;
+ /* Leave back-pointers for malloc_find_address. */
+ while (--blocks > 0)
+ _heapinfo[block + blocks].busy.info.size = -blocks;
+}
+
+/* Set everything up and remember that we have. */
+int
+__malloc_initialize ()
+{
+ if (__malloc_initialized)
+ return 0;
+
+ if (__malloc_initialize_hook)
+ (*__malloc_initialize_hook) ();
+
+ heapsize = HEAP / BLOCKSIZE;
+ _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
+ if (_heapinfo == NULL)
+ return 0;
+ memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
+ _heapinfo[0].free.size = 0;
+ _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
+ _heapindex = 0;
+ _heapbase = (char *) _heapinfo;
+ _heaplimit = BLOCK (_heapbase + heapsize * sizeof (malloc_info));
+
+ register_heapinfo ();
+
+ __malloc_initialized = 1;
+ return 1;
+}
+
+static int morecore_recursing;
+
+/* Get neatly aligned memory, initializing or
+ growing the heap info table as necessary. */
+static __ptr_t morecore __P ((__malloc_size_t));
+static __ptr_t
+morecore (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ malloc_info *newinfo, *oldinfo;
+ __malloc_size_t newsize;
+
+ if (morecore_recursing)
+ /* Avoid recursion. The caller will know how to handle a null return. */
+ return NULL;
+
+ result = align (size);
+ if (result == NULL)
+ return NULL;
+
+ /* Check if we need to grow the info table. */
+ if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
+ {
+ /* Calculate the new _heapinfo table size. We do not account for the
+ added blocks in the table itself, as we hope to place them in
+ existing free space, which is already covered by part of the
+ existing table. */
+ newsize = heapsize;
+ do
+ newsize *= 2;
+ while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize);
+
+ /* We must not reuse existing core for the new info table when called
+ from realloc in the case of growing a large block, because the
+ block being grown is momentarily marked as free. In this case
+ _heaplimit is zero so we know not to reuse space for internal
+ allocation. */
+ if (_heaplimit != 0)
+ {
+ /* First try to allocate the new info table in core we already
+ have, in the usual way using realloc. If realloc cannot
+ extend it in place or relocate it to existing sufficient core,
+ we will get called again, and the code above will notice the
+ `morecore_recursing' flag and return null. */
+ int save = errno; /* Don't want to clobber errno with ENOMEM. */
+ morecore_recursing = 1;
+ newinfo = (malloc_info *) _realloc_internal
+ (_heapinfo, newsize * sizeof (malloc_info));
+ morecore_recursing = 0;
+ if (newinfo == NULL)
+ errno = save;
+ else
+ {
+ /* We found some space in core, and realloc has put the old
+ table's blocks on the free list. Now zero the new part
+ of the table and install the new table location. */
+ memset (&newinfo[heapsize], 0,
+ (newsize - heapsize) * sizeof (malloc_info));
+ _heapinfo = newinfo;
+ heapsize = newsize;
+ goto got_heap;
+ }
+ }
+
+ /* Allocate new space for the malloc info table. */
+ while (1)
+ {
+ newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
+
+ /* Did it fail? */
+ if (newinfo == NULL)
+ {
+ (*__morecore) (-size);
+ return NULL;
+ }
+
+ /* Is it big enough to record status for its own space?
+ If so, we win. */
+ if ((__malloc_size_t) BLOCK ((char *) newinfo
+ + newsize * sizeof (malloc_info))
+ < newsize)
+ break;
+
+ /* Must try again. First give back most of what we just got. */
+ (*__morecore) (- newsize * sizeof (malloc_info));
+ newsize *= 2;
+ }
+
+ /* Copy the old table to the beginning of the new,
+ and zero the rest of the new table. */
+ memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
+ memset (&newinfo[heapsize], 0,
+ (newsize - heapsize) * sizeof (malloc_info));
+ oldinfo = _heapinfo;
+ _heapinfo = newinfo;
+ heapsize = newsize;
+
+ register_heapinfo ();
+
+ /* Reset _heaplimit so _free_internal never decides
+ it can relocate or resize the info table. */
+ _heaplimit = 0;
+ _free_internal (oldinfo);
+
+ /* The new heap limit includes the new table just allocated. */
+ _heaplimit = BLOCK ((char *) newinfo + heapsize * sizeof (malloc_info));
+ return result;
+ }
+
+ got_heap:
+ _heaplimit = BLOCK ((char *) result + size);
+ return result;
+}
+
+/* Allocate memory from the heap. */
+__ptr_t
+_malloc_internal (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ __malloc_size_t block, blocks, lastblocks, start;
+ register __malloc_size_t i;
+ struct list *next;
+
+ /* ANSI C allows `malloc (0)' to either return NULL, or to return a
+ valid address you can realloc and free (though not dereference).
+
+ It turns out that some extant code (sunrpc, at least Ultrix's version)
+ expects `malloc (0)' to return non-NULL and breaks otherwise.
+ Be compatible. */
+
+#if 0
+ if (size == 0)
+ return NULL;
+#endif
+
+ if (size < sizeof (struct list))
+ size = sizeof (struct list);
+
+#ifdef SUNOS_LOCALTIME_BUG
+ if (size < 16)
+ size = 16;
+#endif
+
+ /* Determine the allocation policy based on the request size. */
+ if (size <= BLOCKSIZE / 2)
+ {
+ /* Small allocation to receive a fragment of a block.
+ Determine the logarithm to base two of the fragment size. */
+ register __malloc_size_t log = 1;
+ --size;
+ while ((size /= 2) != 0)
+ ++log;
+
+ /* Look in the fragment lists for a
+ free fragment of the desired size. */
+ next = _fraghead[log].next;
+ if (next != NULL)
+ {
+ /* There are free fragments of this size.
+ Pop a fragment out of the fragment list and return it.
+ Update the block's nfree and first counters. */
+ result = (__ptr_t) next;
+ next->prev->next = next->next;
+ if (next->next != NULL)
+ next->next->prev = next->prev;
+ block = BLOCK (result);
+ if (--_heapinfo[block].busy.info.frag.nfree != 0)
+ _heapinfo[block].busy.info.frag.first = (unsigned long int)
+ ((unsigned long int) ((char *) next->next - (char *) NULL)
+ % BLOCKSIZE) >> log;
+
+ /* Update the statistics. */
+ ++_chunks_used;
+ _bytes_used += 1 << log;
+ --_chunks_free;
+ _bytes_free -= 1 << log;
+ }
+ else
+ {
+ /* No free fragments of the desired size, so get a new block
+ and break it into fragments, returning the first. */
+ result = malloc (BLOCKSIZE);
+ if (result == NULL)
+ return NULL;
+
+ /* Link all fragments but the first into the free list. */
+ next = (struct list *) ((char *) result + (1 << log));
+ next->next = NULL;
+ next->prev = &_fraghead[log];
+ _fraghead[log].next = next;
+
+ for (i = 2; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
+ {
+ next = (struct list *) ((char *) result + (i << log));
+ next->next = _fraghead[log].next;
+ next->prev = &_fraghead[log];
+ next->prev->next = next;
+ next->next->prev = next;
+ }
+
+ /* Initialize the nfree and first counters for this block. */
+ block = BLOCK (result);
+ _heapinfo[block].busy.type = log;
+ _heapinfo[block].busy.info.frag.nfree = i - 1;
+ _heapinfo[block].busy.info.frag.first = i - 1;
+
+ _chunks_free += (BLOCKSIZE >> log) - 1;
+ _bytes_free += BLOCKSIZE - (1 << log);
+ _bytes_used -= BLOCKSIZE - (1 << log);
+ }
+ }
+ else
+ {
+ /* Large allocation to receive one or more blocks.
+ Search the free list in a circle starting at the last place visited.
+ If we loop completely around without finding a large enough
+ space we will have to get more memory from the system. */
+ blocks = BLOCKIFY (size);
+ start = block = _heapindex;
+ while (_heapinfo[block].free.size < blocks)
+ {
+ block = _heapinfo[block].free.next;
+ if (block == start)
+ {
+ /* Need to get more from the system. Get a little extra. */
+ __malloc_size_t wantblocks = blocks + __malloc_extra_blocks;
+ block = _heapinfo[0].free.prev;
+ lastblocks = _heapinfo[block].free.size;
+ /* Check to see if the new core will be contiguous with the
+ final free block; if so we don't need to get as much. */
+ if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
+ /* We can't do this if we will have to make the heap info
+ table bigger to accomodate the new space. */
+ block + wantblocks <= heapsize &&
+ get_contiguous_space ((wantblocks - lastblocks) * BLOCKSIZE,
+ ADDRESS (block + lastblocks)))
+ {
+ /* We got it contiguously. Which block we are extending
+ (the `final free block' referred to above) might have
+ changed, if it got combined with a freed info table. */
+ block = _heapinfo[0].free.prev;
+ _heapinfo[block].free.size += (wantblocks - lastblocks);
+ _bytes_free += (wantblocks - lastblocks) * BLOCKSIZE;
+ _heaplimit += wantblocks - lastblocks;
+ continue;
+ }
+ result = morecore (wantblocks * BLOCKSIZE);
+ if (result == NULL)
+ return NULL;
+ block = BLOCK (result);
+ /* Put the new block at the end of the free list. */
+ _heapinfo[block].free.size = wantblocks;
+ _heapinfo[block].free.prev = _heapinfo[0].free.prev;
+ _heapinfo[block].free.next = 0;
+ _heapinfo[0].free.prev = block;
+ _heapinfo[_heapinfo[block].free.prev].free.next = block;
+ ++_chunks_free;
+ /* Now loop to use some of that block for this allocation. */
+ }
+ }
+
+ /* At this point we have found a suitable free list entry.
+ Figure out how to remove what we need from the list. */
+ result = ADDRESS (block);
+ if (_heapinfo[block].free.size > blocks)
+ {
+ /* The block we found has a bit left over,
+ so relink the tail end back into the free list. */
+ _heapinfo[block + blocks].free.size
+ = _heapinfo[block].free.size - blocks;
+ _heapinfo[block + blocks].free.next
+ = _heapinfo[block].free.next;
+ _heapinfo[block + blocks].free.prev
+ = _heapinfo[block].free.prev;
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapindex = block + blocks;
+ }
+ else
+ {
+ /* The block exactly matches our requirements,
+ so just remove it from the list. */
+ _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapinfo[block].free.prev;
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapindex = _heapinfo[block].free.next;
+ --_chunks_free;
+ }
+
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = blocks;
+ ++_chunks_used;
+ _bytes_used += blocks * BLOCKSIZE;
+ _bytes_free -= blocks * BLOCKSIZE;
+
+ /* Mark all the blocks of the object just allocated except for the
+ first with a negative number so you can find the first block by
+ adding that adjustment. */
+ while (--blocks > 0)
+ _heapinfo[block + blocks].busy.info.size = -blocks;
+ }
+
+ return result;
+}
+
+__ptr_t
+malloc (size)
+ __malloc_size_t size;
+{
+ if (!__malloc_initialized && !__malloc_initialize ())
+ return NULL;
+
+ return (__malloc_hook != NULL ? *__malloc_hook : _malloc_internal) (size);
+}
+
+#ifndef _LIBC
+
+/* On some ANSI C systems, some libc functions call _malloc, _free
+ and _realloc. Make them use the GNU functions. */
+
+__ptr_t
+_malloc (size)
+ __malloc_size_t size;
+{
+ return malloc (size);
+}
+
+void
+_free (ptr)
+ __ptr_t ptr;
+{
+ free (ptr);
+}
+
+__ptr_t
+_realloc (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ return realloc (ptr, size);
+}
+
+#endif
+/* Free a block of memory allocated by `malloc'.
+ Copyright 1990, 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+
+/* Cope with systems lacking `memmove'. */
+#ifndef memmove
+#if (defined (MEMMOVE_MISSING) || \
+ !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
+#ifdef emacs
+#undef __malloc_safe_bcopy
+#define __malloc_safe_bcopy safe_bcopy
+#endif
+/* This function is defined in realloc.c. */
+extern void __malloc_safe_bcopy __P ((__ptr_t, __ptr_t, __malloc_size_t));
+#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
+#endif
+#endif
+
+
+/* Debugging hook for free. */
+void (*__free_hook) __P ((__ptr_t __ptr));
+
+/* List of blocks allocated by memalign. */
+struct alignlist *_aligned_blocks = NULL;
+
+/* Return memory to the heap.
+ Like `free' but don't call a __free_hook if there is one. */
+void
+_free_internal (ptr)
+ __ptr_t ptr;
+{
+ int type;
+ __malloc_size_t block, blocks;
+ register __malloc_size_t i;
+ struct list *prev, *next;
+ __ptr_t curbrk;
+ const __malloc_size_t lesscore_threshold
+ /* Threshold of free space at which we will return some to the system. */
+ = FINAL_FREE_BLOCKS + 2 * __malloc_extra_blocks;
+
+ register struct alignlist *l;
+
+ if (ptr == NULL)
+ return;
+
+ for (l = _aligned_blocks; l != NULL; l = l->next)
+ if (l->aligned == ptr)
+ {
+ l->aligned = NULL; /* Mark the slot in the list as free. */
+ ptr = l->exact;
+ break;
+ }
+
+ block = BLOCK (ptr);
+
+ type = _heapinfo[block].busy.type;
+ switch (type)
+ {
+ case 0:
+ /* Get as many statistics as early as we can. */
+ --_chunks_used;
+ _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
+ _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
+
+ /* Find the free cluster previous to this one in the free list.
+ Start searching at the last block referenced; this may benefit
+ programs with locality of allocation. */
+ i = _heapindex;
+ if (i > block)
+ while (i > block)
+ i = _heapinfo[i].free.prev;
+ else
+ {
+ do
+ i = _heapinfo[i].free.next;
+ while (i > 0 && i < block);
+ i = _heapinfo[i].free.prev;
+ }
+
+ /* Determine how to link this block into the free list. */
+ if (block == i + _heapinfo[i].free.size)
+ {
+ /* Coalesce this block with its predecessor. */
+ _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
+ block = i;
+ }
+ else
+ {
+ /* Really link this block back into the free list. */
+ _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
+ _heapinfo[block].free.next = _heapinfo[i].free.next;
+ _heapinfo[block].free.prev = i;
+ _heapinfo[i].free.next = block;
+ _heapinfo[_heapinfo[block].free.next].free.prev = block;
+ ++_chunks_free;
+ }
+
+ /* Now that the block is linked in, see if we can coalesce it
+ with its successor (by deleting its successor from the list
+ and adding in its size). */
+ if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
+ {
+ _heapinfo[block].free.size
+ += _heapinfo[_heapinfo[block].free.next].free.size;
+ _heapinfo[block].free.next
+ = _heapinfo[_heapinfo[block].free.next].free.next;
+ _heapinfo[_heapinfo[block].free.next].free.prev = block;
+ --_chunks_free;
+ }
+
+ /* How many trailing free blocks are there now? */
+ blocks = _heapinfo[block].free.size;
+
+ /* Where is the current end of accessible core? */
+ curbrk = (*__morecore) (0);
+
+ if (_heaplimit != 0 && curbrk == ADDRESS (_heaplimit))
+ {
+ /* The end of the malloc heap is at the end of accessible core.
+ It's possible that moving _heapinfo will allow us to
+ return some space to the system. */
+
+ __malloc_size_t info_block = BLOCK (_heapinfo);
+ __malloc_size_t info_blocks = _heapinfo[info_block].busy.info.size;
+ __malloc_size_t prev_block = _heapinfo[block].free.prev;
+ __malloc_size_t prev_blocks = _heapinfo[prev_block].free.size;
+ __malloc_size_t next_block = _heapinfo[block].free.next;
+ __malloc_size_t next_blocks = _heapinfo[next_block].free.size;
+
+ if (/* Win if this block being freed is last in core, the info table
+ is just before it, the previous free block is just before the
+ info table, and the two free blocks together form a useful
+ amount to return to the system. */
+ (block + blocks == _heaplimit &&
+ info_block + info_blocks == block &&
+ prev_block != 0 && prev_block + prev_blocks == info_block &&
+ blocks + prev_blocks >= lesscore_threshold) ||
+ /* Nope, not the case. We can also win if this block being
+ freed is just before the info table, and the table extends
+ to the end of core or is followed only by a free block,
+ and the total free space is worth returning to the system. */
+ (block + blocks == info_block &&
+ ((info_block + info_blocks == _heaplimit &&
+ blocks >= lesscore_threshold) ||
+ (info_block + info_blocks == next_block &&
+ next_block + next_blocks == _heaplimit &&
+ blocks + next_blocks >= lesscore_threshold)))
+ )
+ {
+ malloc_info *newinfo;
+ __malloc_size_t oldlimit = _heaplimit;
+
+ /* Free the old info table, clearing _heaplimit to avoid
+ recursion into this code. We don't want to return the
+ table's blocks to the system before we have copied them to
+ the new location. */
+ _heaplimit = 0;
+ _free_internal (_heapinfo);
+ _heaplimit = oldlimit;
+
+ /* Tell malloc to search from the beginning of the heap for
+ free blocks, so it doesn't reuse the ones just freed. */
+ _heapindex = 0;
+
+ /* Allocate new space for the info table and move its data. */
+ newinfo = (malloc_info *) _malloc_internal (info_blocks
+ * BLOCKSIZE);
+ memmove (newinfo, _heapinfo, info_blocks * BLOCKSIZE);
+ _heapinfo = newinfo;
+
+ /* We should now have coalesced the free block with the
+ blocks freed from the old info table. Examine the entire
+ trailing free block to decide below whether to return some
+ to the system. */
+ block = _heapinfo[0].free.prev;
+ blocks = _heapinfo[block].free.size;
+ }
+
+ /* Now see if we can return stuff to the system. */
+ if (block + blocks == _heaplimit && blocks >= lesscore_threshold)
+ {
+ register __malloc_size_t bytes = blocks * BLOCKSIZE;
+ _heaplimit -= blocks;
+ (*__morecore) (-bytes);
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapinfo[block].free.next;
+ _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapinfo[block].free.prev;
+ block = _heapinfo[block].free.prev;
+ --_chunks_free;
+ _bytes_free -= bytes;
+ }
+ }
+
+ /* Set the next search to begin at this block. */
+ _heapindex = block;
+ break;
+
+ default:
+ /* Do some of the statistics. */
+ --_chunks_used;
+ _bytes_used -= 1 << type;
+ ++_chunks_free;
+ _bytes_free += 1 << type;
+
+ /* Get the address of the first free fragment in this block. */
+ prev = (struct list *) ((char *) ADDRESS (block) +
+ (_heapinfo[block].busy.info.frag.first << type));
+
+ if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
+ {
+ /* If all fragments of this block are free, remove them
+ from the fragment list and free the whole block. */
+ next = prev;
+ for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
+ next = next->next;
+ prev->prev->next = next;
+ if (next != NULL)
+ next->prev = prev->prev;
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = 1;
+
+ /* Keep the statistics accurate. */
+ ++_chunks_used;
+ _bytes_used += BLOCKSIZE;
+ _chunks_free -= BLOCKSIZE >> type;
+ _bytes_free -= BLOCKSIZE;
+
+ free (ADDRESS (block));
+ }
+ else if (_heapinfo[block].busy.info.frag.nfree != 0)
+ {
+ /* If some fragments of this block are free, link this
+ fragment into the fragment list after the first free
+ fragment of this block. */
+ next = (struct list *) ptr;
+ next->next = prev->next;
+ next->prev = prev;
+ prev->next = next;
+ if (next->next != NULL)
+ next->next->prev = next;
+ ++_heapinfo[block].busy.info.frag.nfree;
+ }
+ else
+ {
+ /* No fragments of this block are free, so link this
+ fragment into the fragment list and announce that
+ it is the first free fragment of this block. */
+ prev = (struct list *) ptr;
+ _heapinfo[block].busy.info.frag.nfree = 1;
+ _heapinfo[block].busy.info.frag.first = (unsigned long int)
+ ((unsigned long int) ((char *) ptr - (char *) NULL)
+ % BLOCKSIZE >> type);
+ prev->next = _fraghead[type].next;
+ prev->prev = &_fraghead[type];
+ prev->prev->next = prev;
+ if (prev->next != NULL)
+ prev->next->prev = prev;
+ }
+ break;
+ }
+}
+
+/* Return memory to the heap. */
+void
+free (ptr)
+ __ptr_t ptr;
+{
+ if (__free_hook != NULL)
+ (*__free_hook) (ptr);
+ else
+ _free_internal (ptr);
+}
+
+/* Define the `cfree' alias for `free'. */
+#ifdef weak_alias
+weak_alias (free, cfree)
+#else
+void
+cfree (ptr)
+ __ptr_t ptr;
+{
+ free (ptr);
+}
+#endif
+/* Change the size of a block allocated by `malloc'.
+ Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+
+
+/* Cope with systems lacking `memmove'. */
+#if (defined (MEMMOVE_MISSING) || \
+ !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
+
+#ifdef emacs
+#undef __malloc_safe_bcopy
+#define __malloc_safe_bcopy safe_bcopy
+#else
+
+/* Snarfed directly from Emacs src/dispnew.c:
+ XXX Should use system bcopy if it handles overlap. */
+
+/* Like bcopy except never gets confused by overlap. */
+
+void
+__malloc_safe_bcopy (afrom, ato, size)
+ __ptr_t afrom;
+ __ptr_t ato;
+ __malloc_size_t size;
+{
+ char *from = afrom, *to = ato;
+
+ if (size <= 0 || from == to)
+ return;
+
+ /* If the source and destination don't overlap, then bcopy can
+ handle it. If they do overlap, but the destination is lower in
+ memory than the source, we'll assume bcopy can handle that. */
+ if (to < from || from + size <= to)
+ bcopy (from, to, size);
+
+ /* Otherwise, we'll copy from the end. */
+ else
+ {
+ register char *endf = from + size;
+ register char *endt = to + size;
+
+ /* If TO - FROM is large, then we should break the copy into
+ nonoverlapping chunks of TO - FROM bytes each. However, if
+ TO - FROM is small, then the bcopy function call overhead
+ makes this not worth it. The crossover point could be about
+ anywhere. Since I don't think the obvious copy loop is too
+ bad, I'm trying to err in its favor. */
+ if (to - from < 64)
+ {
+ do
+ *--endt = *--endf;
+ while (endf != from);
+ }
+ else
+ {
+ for (;;)
+ {
+ endt -= (to - from);
+ endf -= (to - from);
+
+ if (endt < to)
+ break;
+
+ bcopy (endf, endt, to - from);
+ }
+
+ /* If SIZE wasn't a multiple of TO - FROM, there will be a
+ little left over. The amount left over is
+ (endt + (to - from)) - to, which is endt - from. */
+ bcopy (from, to, endt - from);
+ }
+ }
+}
+#endif /* emacs */
+
+#ifndef memmove
+extern void __malloc_safe_bcopy __P ((__ptr_t, __ptr_t, __malloc_size_t));
+#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
+#endif
+
+#endif
+
+
+#define min(A, B) ((A) < (B) ? (A) : (B))
+
+/* Debugging hook for realloc. */
+__ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
+
+/* Resize the given region to the new size, returning a pointer
+ to the (possibly moved) region. This is optimized for speed;
+ some benchmarks seem to indicate that greater compactness is
+ achieved by unconditionally allocating and copying to a
+ new region. This module has incestuous knowledge of the
+ internals of both free and malloc. */
+__ptr_t
+_realloc_internal (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ int type;
+ __malloc_size_t block, blocks, oldlimit;
+
+ if (size == 0)
+ {
+ _free_internal (ptr);
+ return _malloc_internal (0);
+ }
+ else if (ptr == NULL)
+ return _malloc_internal (size);
+
+ block = BLOCK (ptr);
+
+ type = _heapinfo[block].busy.type;
+ switch (type)
+ {
+ case 0:
+ /* Maybe reallocate a large block to a small fragment. */
+ if (size <= BLOCKSIZE / 2)
+ {
+ result = _malloc_internal (size);
+ if (result != NULL)
+ {
+ memcpy (result, ptr, size);
+ _free_internal (ptr);
+ return result;
+ }
+ }
+
+ /* The new size is a large allocation as well;
+ see if we can hold it in place. */
+ blocks = BLOCKIFY (size);
+ if (blocks < _heapinfo[block].busy.info.size)
+ {
+ /* The new size is smaller; return
+ excess memory to the free list. */
+ _heapinfo[block + blocks].busy.type = 0;
+ _heapinfo[block + blocks].busy.info.size
+ = _heapinfo[block].busy.info.size - blocks;
+ _heapinfo[block].busy.info.size = blocks;
+ /* We have just created a new chunk by splitting a chunk in two.
+ Now we will free this chunk; increment the statistics counter
+ so it doesn't become wrong when _free_internal decrements it. */
+ ++_chunks_used;
+ _free_internal (ADDRESS (block + blocks));
+ result = ptr;
+ }
+ else if (blocks == _heapinfo[block].busy.info.size)
+ /* No size change necessary. */
+ result = ptr;
+ else
+ {
+ /* Won't fit, so allocate a new region that will.
+ Free the old region first in case there is sufficient
+ adjacent free space to grow without moving. */
+ blocks = _heapinfo[block].busy.info.size;
+ /* Prevent free from actually returning memory to the system. */
+ oldlimit = _heaplimit;
+ _heaplimit = 0;
+ _free_internal (ptr);
+ result = _malloc_internal (size);
+ if (_heaplimit == 0)
+ _heaplimit = oldlimit;
+ if (result == NULL)
+ {
+ /* Now we're really in trouble. We have to unfree
+ the thing we just freed. Unfortunately it might
+ have been coalesced with its neighbors. */
+ if (_heapindex == block)
+ (void) _malloc_internal (blocks * BLOCKSIZE);
+ else
+ {
+ __ptr_t previous
+ = _malloc_internal ((block - _heapindex) * BLOCKSIZE);
+ (void) _malloc_internal (blocks * BLOCKSIZE);
+ _free_internal (previous);
+ }
+ return NULL;
+ }
+ if (ptr != result)
+ memmove (result, ptr, blocks * BLOCKSIZE);
+ }
+ break;
+
+ default:
+ /* Old size is a fragment; type is logarithm
+ to base two of the fragment size. */
+ if (size > (__malloc_size_t) (1 << (type - 1)) &&
+ size <= (__malloc_size_t) (1 << type))
+ /* The new size is the same kind of fragment. */
+ result = ptr;
+ else
+ {
+ /* The new size is different; allocate a new space,
+ and copy the lesser of the new size and the old. */
+ result = _malloc_internal (size);
+ if (result == NULL)
+ return NULL;
+ memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
+ _free_internal (ptr);
+ }
+ break;
+ }
+
+ return result;
+}
+
+__ptr_t
+realloc (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ if (!__malloc_initialized && !__malloc_initialize ())
+ return NULL;
+
+ return (__realloc_hook != NULL ? *__realloc_hook : _realloc_internal)
+ (ptr, size);
+}
+/* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* Allocate an array of NMEMB elements each SIZE bytes long.
+ The entire array is initialized to zeros. */
+__ptr_t
+calloc (nmemb, size)
+ register __malloc_size_t nmemb;
+ register __malloc_size_t size;
+{
+ register __ptr_t result = malloc (nmemb * size);
+
+ if (result != NULL)
+ (void) memset (result, 0, nmemb * size);
+
+ return result;
+}
+/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C 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 C 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.
+
+You should have received a copy of the GNU General Public License
+along with the GNU C Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#ifndef __GNU_LIBRARY__
+#define __sbrk sbrk
+#endif
+
+#ifdef __GNU_LIBRARY__
+/* It is best not to declare this and cast its result on foreign operating
+ systems with potentially hostile include files. */
+
+#include <stddef.h>
+extern __ptr_t __sbrk __P ((ptrdiff_t increment));
+#endif
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+/* Allocate INCREMENT more bytes of data space,
+ and return the start of data space, or NULL on errors.
+ If INCREMENT is negative, shrink data space. */
+__ptr_t
+__default_morecore (increment)
+ __malloc_ptrdiff_t increment;
+{
+ __ptr_t result = (__ptr_t) __sbrk (increment);
+ if (result == (__ptr_t) -1)
+ return NULL;
+ return result;
+}
+/* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#if __DJGPP__ - 0 == 1
+
+/* There is some problem with memalign in DJGPP v1 and we are supposed
+ to omit it. Noone told me why, they just told me to do it. */
+
+#else
+
+__ptr_t (*__memalign_hook) __P ((size_t __size, size_t __alignment));
+
+__ptr_t
+memalign (alignment, size)
+ __malloc_size_t alignment;
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ unsigned long int adj, lastadj;
+
+ if (__memalign_hook)
+ return (*__memalign_hook) (alignment, size);
+
+ /* Allocate a block with enough extra space to pad the block with up to
+ (ALIGNMENT - 1) bytes if necessary. */
+ result = malloc (size + alignment - 1);
+ if (result == NULL)
+ return NULL;
+
+ /* Figure out how much we will need to pad this particular block
+ to achieve the required alignment. */
+ adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;
+
+ do
+ {
+ /* Reallocate the block with only as much excess as it needs. */
+ free (result);
+ result = malloc (adj + size);
+ if (result == NULL) /* Impossible unless interrupted. */
+ return NULL;
+
+ lastadj = adj;
+ adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;
+ /* It's conceivable we might have been so unlucky as to get a
+ different block with weaker alignment. If so, this block is too
+ short to contain SIZE after alignment correction. So we must
+ try again and get another block, slightly larger. */
+ } while (adj > lastadj);
+
+ if (adj != 0)
+ {
+ /* Record this block in the list of aligned blocks, so that `free'
+ can identify the pointer it is passed, which will be in the middle
+ of an allocated block. */
+
+ struct alignlist *l;
+ for (l = _aligned_blocks; l != NULL; l = l->next)
+ if (l->aligned == NULL)
+ /* This slot is free. Use it. */
+ break;
+ if (l == NULL)
+ {
+ l = (struct alignlist *) malloc (sizeof (struct alignlist));
+ if (l == NULL)
+ {
+ free (result);
+ return NULL;
+ }
+ l->next = _aligned_blocks;
+ _aligned_blocks = l;
+ }
+ l->exact = result;
+ result = l->aligned = (char *) result + alignment - adj;
+ }
+
+ return result;
+}
+
+#endif /* Not DJGPP v1 */
diff --git a/lib/malloc/omalloc.c b/lib/malloc/omalloc.c
new file mode 100644
index 0000000..a8b232a
--- /dev/null
+++ b/lib/malloc/omalloc.c
@@ -0,0 +1,759 @@
+/* dynamic memory allocation for GNU. */
+
+/* Copyright (C) 1985, 1987 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+In other words, you are welcome to use, share and improve this program.
+You are forbidden to forbid anyone else to use, share and improve
+what you give them. Help stamp out software-hoarding! */
+
+/*
+ * @(#)nmalloc.c 1 (Caltech) 2/21/82
+ *
+ * U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
+ *
+ * Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
+ *
+ * This is a very fast storage allocator. It allocates blocks of a small
+ * number of different sizes, and keeps free lists of each size. Blocks
+ * that don't exactly fit are passed up to the next larger size. In this
+ * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
+ * This is designed for use in a program that uses vast quantities of
+ * memory, but bombs when it runs out. To make it a little better, it
+ * warns the user when he starts to get near the end.
+ *
+ * June 84, ACT: modified rcheck code to check the range given to malloc,
+ * rather than the range determined by the 2-power used.
+ *
+ * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
+ * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
+ * You should call malloc_init to reinitialize after loading dumped Emacs.
+ * Call malloc_stats to get info on memory stats if MSTATS turned on.
+ * realloc knows how to return same block given, just changing its size,
+ * if the power of 2 is correct.
+ */
+
+/*
+ * nextf[i] is the pointer to the next free block of size 2^(i+3). The
+ * smallest allocatable block is 8 bytes. The overhead information will
+ * go in the first int of the block, and the returned pointer will point
+ * to the second.
+ *
+#ifdef MSTATS
+ * nmalloc[i] is the difference between the number of mallocs and frees
+ * for a given block size.
+#endif
+ */
+
+/* Define this to have free() write 0xcf into memory as it's freed, to
+ uncover callers that refer to freed memory. */
+/* SCO 3.2v4 getcwd and possibly other libc routines fail with MEMSCRAMBLE */
+#if !defined (NO_MEMSCRAMBLE)
+# define MEMSCRAMBLE
+#endif
+
+#if defined (emacs) || defined (HAVE_CONFIG_H)
+# include <config.h>
+#endif /* emacs */
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+/* Determine which kind of system this is. */
+#if defined (SHELL)
+# include "bashtypes.h"
+#else
+# include <sys/types.h>
+#endif
+#include <signal.h>
+
+/* Define getpagesize () if the system does not. */
+#ifndef HAVE_GETPAGESIZE
+# include "getpagesize.h"
+#endif
+
+#if defined (HAVE_RESOURCE)
+# include <sys/time.h>
+# include <sys/resource.h>
+#endif /* HAVE_RESOURCE */
+
+/* Check for the needed symbols. If they aren't present, this
+ system's <sys/resource.h> isn't very useful to us. */
+#if !defined (RLIMIT_DATA)
+# undef HAVE_RESOURCE
+#endif
+
+#if __GNUC__ > 1
+# define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
+#else /* !__GNUC__ */
+# if !defined (HAVE_BCOPY)
+# if !defined (HAVE_MEMMOVE)
+# define FASTCOPY(s, d, n) memcpy (d, s, n)
+# else
+# define FASTCOPY(s, d, n) memmove (d, s, n)
+# endif /* !HAVE_MEMMOVE */
+# else /* HAVE_BCOPY */
+# define FASTCOPY(s, d, n) bcopy (s, d, n)
+# endif /* HAVE_BCOPY */
+#endif /* !__GNUC__ */
+
+#if !defined (NULL)
+# define NULL 0
+#endif
+
+#define start_of_data() &etext
+
+#define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
+#define ISFREE ((char) 0x54) /* magic byte that implies free block */
+ /* this is for error checking only */
+#define ISMEMALIGN ((char) 0xd6) /* Stored before the value returned by
+ memalign, with the rest of the word
+ being the distance to the true
+ beginning of the block. */
+extern char etext;
+
+#if !defined (SBRK_DECLARED)
+extern char *sbrk ();
+#endif /* !SBRK_DECLARED */
+
+/* These two are for user programs to look at, when they are interested. */
+unsigned int malloc_sbrk_used; /* amount of data space used now */
+unsigned int malloc_sbrk_unused; /* amount more we can have */
+
+/* start of data space; can be changed by calling init_malloc */
+static char *data_space_start;
+
+static void get_lim_data ();
+
+#ifdef MSTATS
+static int nmalloc[30];
+static int nmal, nfre;
+#endif /* MSTATS */
+
+/* If range checking is not turned on, all we have is a flag indicating
+ whether memory is allocated, an index in nextf[], and a size field; to
+ realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
+ on whether the former can hold the exact size (given the value of
+ 'index'). If range checking is on, we always need to know how much space
+ is allocated, so the 'size' field is never used. */
+
+struct mhead {
+ char mh_alloc; /* ISALLOC or ISFREE */
+ char mh_index; /* index in nextf[] */
+/* Remainder are valid only when block is allocated */
+ unsigned short mh_size; /* size, if < 0x10000 */
+#ifdef RCHECK
+ unsigned int mh_nbytes; /* number of bytes allocated */
+ int mh_magic4; /* should be == MAGIC4 */
+#endif /* RCHECK */
+};
+
+/* Access free-list pointer of a block.
+ It is stored at block + 4.
+ This is not a field in the mhead structure
+ because we want sizeof (struct mhead)
+ to describe the overhead for when the block is in use,
+ and we do not want the free-list pointer to count in that. */
+
+#define CHAIN(a) \
+ (*(struct mhead **) (sizeof (char *) + (char *) (a)))
+
+#ifdef RCHECK
+# include <stdio.h>
+# if !defined (botch)
+# define botch(x) abort ()
+# else
+extern void botch();
+# endif /* botch */
+
+# if !defined (__STRING)
+# if defined (__STDC__)
+# define __STRING(x) #x
+# else
+# define __STRING(x) "x"
+# endif
+# endif
+
+ /* To implement range checking, we write magic values in at the beginning
+ and end of each allocated block, and make sure they are undisturbed
+ whenever a free or a realloc occurs. */
+
+ /* Written in each of the 4 bytes following the block's real space */
+# define MAGIC1 0x55
+ /* Written in the 4 bytes before the block's real space */
+# define MAGIC4 0x55555555
+# define ASSERT(p) if (!(p)) botch(__STRING(p)); else
+# define EXTRA 4 /* 4 bytes extra for MAGIC1s */
+#else /* !RCHECK */
+# define ASSERT(p)
+# define EXTRA 0
+#endif /* RCHECK */
+
+/* nextf[i] is free list of blocks of size 2**(i + 3) */
+
+static struct mhead *nextf[30];
+
+/* busy[i] is nonzero while allocation of block size i is in progress. */
+
+static char busy[30];
+
+/* Number of bytes of writable memory we can expect to be able to get */
+static unsigned int lim_data;
+
+/* Level number of warnings already issued.
+ 0 -- no warnings issued.
+ 1 -- 75% warning already issued.
+ 2 -- 85% warning already issued.
+*/
+static int warnlevel;
+
+/* Function to call to issue a warning;
+ 0 means don't issue them. */
+static void (*warnfunction) ();
+
+/* nonzero once initial bunch of free blocks made */
+static int gotpool;
+
+char *_malloc_base;
+
+static void getpool ();
+
+/* Cause reinitialization based on job parameters;
+ also declare where the end of pure storage is. */
+void
+malloc_init (start, warnfun)
+ char *start;
+ void (*warnfun) ();
+{
+ if (start)
+ data_space_start = start;
+ lim_data = 0;
+ warnlevel = 0;
+ warnfunction = warnfun;
+}
+
+/* Return the maximum size to which MEM can be realloc'd
+ without actually requiring copying. */
+
+int
+malloc_usable_size (mem)
+ char *mem;
+{
+ int blocksize = 8 << (((struct mhead *) mem) - 1) -> mh_index;
+
+ return blocksize - sizeof (struct mhead) - EXTRA;
+}
+
+static void
+morecore (nu) /* ask system for more memory */
+ register int nu; /* size index to get more of */
+{
+ register char *cp;
+ register int nblks;
+ register unsigned int siz;
+
+ /* Block all signals in case we are executed from a signal handler. */
+#if defined (HAVE_BSD_SIGNALS)
+ int oldmask;
+ oldmask = sigsetmask (-1);
+#else
+# if defined (HAVE_POSIX_SIGNALS)
+ sigset_t set, oset;
+ sigfillset (&set);
+ sigemptyset (&oset);
+ sigprocmask (SIG_BLOCK, &set, &oset);
+# endif /* HAVE_POSIX_SIGNALS */
+#endif /* HAVE_BSD_SIGNALS */
+
+ if (!data_space_start)
+ {
+ data_space_start = start_of_data ();
+ }
+
+ if (lim_data == 0)
+ get_lim_data ();
+
+ /* On initial startup, get two blocks of each size up to 1k bytes */
+ if (!gotpool)
+ { getpool (); getpool (); gotpool = 1; }
+
+ /* Find current end of memory and issue warning if getting near max */
+
+ cp = sbrk (0);
+ siz = cp - data_space_start;
+ malloc_sbrk_used = siz;
+ malloc_sbrk_unused = lim_data - siz;
+
+ if (warnfunction)
+ switch (warnlevel)
+ {
+ case 0:
+ if (siz > (lim_data / 4) * 3)
+ {
+ warnlevel++;
+ (*warnfunction) ("Warning: past 75% of memory limit");
+ }
+ break;
+ case 1:
+ if (siz > (lim_data / 20) * 17)
+ {
+ warnlevel++;
+ (*warnfunction) ("Warning: past 85% of memory limit");
+ }
+ break;
+ case 2:
+ if (siz > (lim_data / 20) * 19)
+ {
+ warnlevel++;
+ (*warnfunction) ("Warning: past 95% of memory limit");
+ }
+ break;
+ }
+
+ if ((int) cp & 0x3ff) /* land on 1K boundaries */
+ sbrk (1024 - ((int) cp & 0x3ff));
+
+ /* Take at least 2k, and figure out how many blocks of the desired size
+ we're about to get */
+ nblks = 1;
+ if ((siz = nu) < 8)
+ nblks = 1 << ((siz = 8) - nu);
+
+ if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
+ return; /* no more room! */
+
+ if ((int) cp & 7)
+ { /* shouldn't happen, but just in case */
+ cp = (char *) (((int) cp + 8) & ~7);
+ nblks--;
+ }
+
+ /* save new header and link the nblks blocks together */
+ nextf[nu] = (struct mhead *) cp;
+ siz = 1 << (nu + 3);
+ while (1)
+ {
+ ((struct mhead *) cp) -> mh_alloc = ISFREE;
+ ((struct mhead *) cp) -> mh_index = nu;
+ if (--nblks <= 0) break;
+ CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
+ cp += siz;
+ }
+ CHAIN ((struct mhead *) cp) = 0;
+
+#if defined (HAVE_BSD_SIGNALS)
+ sigsetmask (oldmask);
+#else
+# if defined (HAVE_POSIX_SIGNALS)
+ sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
+# endif
+#endif /* HAVE_BSD_SIGNALS */
+}
+
+static void
+getpool ()
+{
+ register int nu;
+ register char *cp = sbrk (0);
+
+ if ((int) cp & 0x3ff) /* land on 1K boundaries */
+ sbrk (1024 - ((int) cp & 0x3ff));
+
+ /* Record address of start of space allocated by malloc. */
+ if (_malloc_base == 0)
+ _malloc_base = cp;
+
+ /* Get 2k of storage */
+
+ cp = sbrk (04000);
+ if (cp == (char *) -1)
+ return;
+
+ /* Divide it into an initial 8-word block
+ plus one block of size 2**nu for nu = 3 ... 10. */
+
+ CHAIN (cp) = nextf[0];
+ nextf[0] = (struct mhead *) cp;
+ ((struct mhead *) cp) -> mh_alloc = ISFREE;
+ ((struct mhead *) cp) -> mh_index = 0;
+ cp += 8;
+
+ for (nu = 0; nu < 7; nu++)
+ {
+ CHAIN (cp) = nextf[nu];
+ nextf[nu] = (struct mhead *) cp;
+ ((struct mhead *) cp) -> mh_alloc = ISFREE;
+ ((struct mhead *) cp) -> mh_index = nu;
+ cp += 8 << nu;
+ }
+}
+
+#if defined (MEMSCRAMBLE) || !defined (NO_CALLOC)
+static char *
+zmemset (s, c, n)
+ char *s;
+ int c;
+ register int n;
+{
+ register char *sp;
+
+ sp = s;
+ while (--n >= 0)
+ *sp++ = c;
+ return (s);
+}
+#endif /* MEMSCRAMBLE || !NO_CALLOC */
+
+char *
+malloc (n) /* get a block */
+ unsigned int n;
+{
+ register struct mhead *p;
+ register unsigned int nbytes;
+ register int nunits = 0;
+
+ /* Figure out how many bytes are required, rounding up to the nearest
+ multiple of 4, then figure out which nextf[] area to use */
+ nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
+ {
+ register unsigned int shiftr = (nbytes - 1) >> 2;
+
+ while (shiftr >>= 1)
+ nunits++;
+ }
+
+ /* In case this is reentrant use of malloc from signal handler,
+ pick a block size that no other malloc level is currently
+ trying to allocate. That's the easiest harmless way not to
+ interfere with the other level of execution. */
+ while (busy[nunits]) nunits++;
+ busy[nunits] = 1;
+
+ /* If there are no blocks of the appropriate size, go get some */
+ /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
+ if (nextf[nunits] == 0)
+ morecore (nunits);
+
+ /* Get one block off the list, and set the new list head */
+ if ((p = nextf[nunits]) == 0)
+ {
+ busy[nunits] = 0;
+ return 0;
+ }
+ nextf[nunits] = CHAIN (p);
+ busy[nunits] = 0;
+
+ /* Check for free block clobbered */
+ /* If not for this check, we would gobble a clobbered free chain ptr */
+ /* and bomb out on the NEXT allocate of this size block */
+ if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
+#ifdef RCHECK
+ botch ("block on free list clobbered");
+#else /* not RCHECK */
+ abort ();
+#endif /* not RCHECK */
+
+ /* Fill in the info, and if range checking, set up the magic numbers */
+ p -> mh_alloc = ISALLOC;
+#ifdef RCHECK
+ p -> mh_nbytes = n;
+ p -> mh_magic4 = MAGIC4;
+ {
+ register char *m = (char *) (p + 1) + n;
+
+ *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
+ }
+#else /* not RCHECK */
+ p -> mh_size = n;
+#endif /* not RCHECK */
+#ifdef MEMSCRAMBLE
+ zmemset ((char *)(p + 1), 0xdf, n); /* scramble previous contents */
+#endif
+#ifdef MSTATS
+ nmalloc[nunits]++;
+ nmal++;
+#endif /* MSTATS */
+ return (char *) (p + 1);
+}
+
+void
+free (mem)
+ char *mem;
+{
+ register struct mhead *p;
+ {
+ register char *ap = mem;
+
+ if (ap == 0)
+ return;
+
+ p = (struct mhead *) ap - 1;
+
+ if (p -> mh_alloc == ISMEMALIGN)
+ {
+#ifdef RCHECK
+ ap -= p->mh_nbytes;
+#else
+ ap -= p->mh_size; /* XXX */
+#endif
+ p = (struct mhead *) ap - 1;
+ }
+
+#ifndef RCHECK
+ if (p -> mh_alloc != ISALLOC)
+ abort ();
+
+#else /* RCHECK */
+ if (p -> mh_alloc != ISALLOC)
+ {
+ if (p -> mh_alloc == ISFREE)
+ botch ("free: Called with already freed block argument\n");
+ else
+ botch ("free: Called with unallocated block argument\n");
+ }
+
+ ASSERT (p -> mh_magic4 == MAGIC4);
+ ap += p -> mh_nbytes;
+ ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
+ ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
+#endif /* RCHECK */
+ }
+#ifdef MEMSCRAMBLE
+ {
+ register int n;
+
+#ifdef RCHECK
+ n = p->mh_nbytes;
+#else /* not RCHECK */
+ n = p->mh_size;
+#endif /* not RCHECK */
+ zmemset (mem, 0xcf, n);
+ }
+#endif
+ {
+ register int nunits = p -> mh_index;
+
+ ASSERT (nunits <= 29);
+ p -> mh_alloc = ISFREE;
+
+ /* Protect against signal handlers calling malloc. */
+ busy[nunits] = 1;
+ /* Put this block on the free list. */
+ CHAIN (p) = nextf[nunits];
+ nextf[nunits] = p;
+ busy[nunits] = 0;
+
+#ifdef MSTATS
+ nmalloc[nunits]--;
+ nfre++;
+#endif /* MSTATS */
+ }
+}
+
+char *
+realloc (mem, n)
+ char *mem;
+ register unsigned int n;
+{
+ register struct mhead *p;
+ register unsigned int tocopy;
+ register unsigned int nbytes;
+ register int nunits;
+
+ if ((p = (struct mhead *) mem) == 0)
+ return malloc (n);
+ p--;
+ nunits = p -> mh_index;
+ ASSERT (p -> mh_alloc == ISALLOC);
+#ifdef RCHECK
+ ASSERT (p -> mh_magic4 == MAGIC4);
+ {
+ register char *m = mem + (tocopy = p -> mh_nbytes);
+ ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
+ ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
+ }
+#else /* not RCHECK */
+ if (p -> mh_index >= 13)
+ tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
+ else
+ tocopy = p -> mh_size;
+#endif /* not RCHECK */
+
+ /* See if desired size rounds to same power of 2 as actual size. */
+ nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
+
+ /* If ok, use the same block, just marking its size as changed. */
+ if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
+ {
+#ifdef RCHECK
+ register char *m = mem + tocopy;
+ *m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
+ p-> mh_nbytes = n;
+ m = mem + n;
+ *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1;
+#else /* not RCHECK */
+ p -> mh_size = n;
+#endif /* not RCHECK */
+ return mem;
+ }
+
+ if (n < tocopy)
+ tocopy = n;
+ {
+ register char *new;
+
+ if ((new = malloc (n)) == 0)
+ return 0;
+ FASTCOPY (mem, new, tocopy);
+ free (mem);
+ return new;
+ }
+}
+
+char *
+memalign (alignment, size)
+ unsigned int alignment, size;
+{
+ register char *ptr;
+ register char *aligned;
+ register struct mhead *p;
+
+ ptr = malloc (size + alignment);
+
+ if (ptr == 0)
+ return 0;
+ /* If entire block has the desired alignment, just accept it. */
+ if (((int) ptr & (alignment - 1)) == 0)
+ return ptr;
+ /* Otherwise, get address of byte in the block that has that alignment. */
+ aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
+
+ /* Store a suitable indication of how to free the block,
+ so that free can find the true beginning of it. */
+ p = (struct mhead *) aligned - 1;
+ p -> mh_size = aligned - ptr;
+ p -> mh_alloc = ISMEMALIGN;
+ return aligned;
+}
+
+#if !defined (HPUX)
+/* This runs into trouble with getpagesize on HPUX, and Multimax machines.
+ Patching out seems cleaner than the ugly fix needed. */
+#if defined (__STDC__)
+void *
+#else
+char *
+#endif
+valloc (size)
+ size_t size;
+{
+ return memalign (getpagesize (), size);
+}
+#endif /* !HPUX */
+
+#ifndef NO_CALLOC
+char *
+calloc (n, s)
+ size_t n, s;
+{
+ size_t total;
+ char *result;
+
+ total = n * s;
+ result = malloc (total);
+ if (result)
+ zmemset (result, 0, total);
+ return result;
+}
+
+void
+cfree (p)
+ char *p;
+{
+ free (p);
+}
+#endif /* !NO_CALLOC */
+
+#ifdef MSTATS
+/* Return statistics describing allocation of blocks of size 2**n. */
+
+struct mstats_value
+ {
+ int blocksize;
+ int nfree;
+ int nused;
+ };
+
+struct mstats_value
+malloc_stats (size)
+ int size;
+{
+ struct mstats_value v;
+ register int i;
+ register struct mhead *p;
+
+ v.nfree = 0;
+
+ if (size < 0 || size >= 30)
+ {
+ v.blocksize = 0;
+ v.nused = 0;
+ return v;
+ }
+
+ v.blocksize = 1 << (size + 3);
+ v.nused = nmalloc[size];
+
+ for (p = nextf[size]; p; p = CHAIN (p))
+ v.nfree++;
+
+ return v;
+}
+#endif /* MSTATS */
+
+/*
+ * This function returns the total number of bytes that the process
+ * will be allowed to allocate via the sbrk(2) system call. On
+ * BSD systems this is the total space allocatable to stack and
+ * data. On USG systems this is the data space only.
+ */
+
+#if !defined (HAVE_RESOURCE)
+extern long ulimit ();
+
+static void
+get_lim_data ()
+{
+ lim_data = ulimit (3, 0);
+ lim_data -= (long) data_space_start;
+}
+
+#else /* HAVE_RESOURCE */
+static void
+get_lim_data ()
+{
+ struct rlimit XXrlimit;
+
+ getrlimit (RLIMIT_DATA, &XXrlimit);
+#ifdef RLIM_INFINITY
+ lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
+#else
+ lim_data = XXrlimit.rlim_cur; /* soft limit */
+#endif
+}
+
+#endif /* HAVE_RESOURCE */
diff --git a/lib/readline/Makefile.in b/lib/readline/Makefile.in
index 01ff0cb..31eddd6 100644
--- a/lib/readline/Makefile.in
+++ b/lib/readline/Makefile.in
@@ -190,7 +190,7 @@ rltty.o: readline.h keymaps.h chardefs.h tilde.h
search.o: rldefs.h ${BUILD_DIR}/config.h rlconf.h
search.o: readline.h keymaps.h chardefs.h tilde.h
search.o: ansi_stdlib.h history.h
-shell.o: ${BUILD_DIR}/config.h
+shell.o: ${BUILD_DIR}/config.h ansi_stdlib.h
signals.o: rldefs.h ${BUILD_DIR}/config.h rlconf.h
signals.o: readline.h keymaps.h chardefs.h tilde.h
signals.o: history.h
diff --git a/lib/readline/bind.c b/lib/readline/bind.c
index 24c8c48..300530a 100644
--- a/lib/readline/bind.c
+++ b/lib/readline/bind.c
@@ -70,6 +70,8 @@ extern int _rl_convert_meta_chars_to_ascii;
extern int _rl_output_meta_chars;
extern int _rl_complete_show_all;
extern int _rl_complete_mark_directories;
+extern int _rl_print_completions_horizontally;
+extern int _rl_completion_case_fold;
extern int _rl_enable_keypad;
#if defined (PAREN_MATCHING)
extern int rl_blink_matching_paren;
@@ -105,6 +107,7 @@ Keymap rl_binding_keymap;
/* Forward declarations */
void rl_set_keymap_from_edit_mode ();
+static int _rl_read_init_file ();
static int glean_key_from_name ();
static int substring_member_of_array ();
@@ -198,6 +201,35 @@ rl_unbind_key_in_map (key, map)
return (rl_bind_key_in_map (key, (Function *)NULL, map));
}
+/* Unbind all keys bound to FUNCTION in MAP. */
+int
+rl_unbind_function_in_map (func, map)
+ Function *func;
+ Keymap map;
+{
+ register int i;
+
+ for (i = 0; i < KEYMAP_SIZE; i++)
+ {
+ if (map[i].type == ISFUNC && map[i].function == func)
+ map[i].function = (Function *)NULL;
+ }
+}
+
+int
+rl_unbind_command_in_map (command, map)
+ char *command;
+ Keymap map;
+{
+ Function *func;
+ register int i;
+
+ func = rl_named_function (command);
+ if (func == 0)
+ return 0;
+ return (rl_unbind_function_in_map (func, map));
+}
+
/* Bind the key sequence represented by the string KEYSEQ to
FUNCTION. This makes new keymaps as necessary. The initial
place to do bindings is in MAP. */
@@ -313,7 +345,7 @@ rl_translate_keyseq (seq, array, len)
char *seq, *array;
int *len;
{
- register int i, c, l;
+ register int i, c, l, temp;
for (i = l = 0; c = seq[i]; i++)
{
@@ -324,7 +356,8 @@ rl_translate_keyseq (seq, array, len)
if (c == 0)
break;
- if (((c == 'C' || c == 'M') && seq[i + 1] == '-') || (c == 'e'))
+ /* Handle \C- and \M- prefixes. */
+ if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
{
/* Handle special case of backwards define. */
if (strncmp (&seq[i], "C-\\M-", 5) == 0)
@@ -332,31 +365,83 @@ rl_translate_keyseq (seq, array, len)
array[l++] = ESC;
i += 5;
array[l++] = CTRL (_rl_to_upper (seq[i]));
- if (!seq[i])
+ if (seq[i] == '\0')
i--;
- continue;
}
-
- switch (c)
+ else if (c == 'M')
{
- case 'M':
i++;
array[l++] = ESC; /* XXX */
- break;
-
- case 'C':
+ }
+ else if (c == 'C')
+ {
i += 2;
/* Special hack for C-?... */
array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
- break;
-
- case 'e':
- array[l++] = ESC;
}
-
continue;
+ }
+
+ /* Translate other backslash-escaped characters. These are the
+ same escape sequences that bash's `echo' and `printf' builtins
+ handle, with the addition of \d -> RUBOUT. A backslash
+ preceding a character that is not special is stripped. */
+ switch (c)
+ {
+ case 'a':
+ array[l++] = '\007';
+ break;
+ case 'b':
+ array[l++] = '\b';
+ break;
+ case 'd':
+ array[l++] = RUBOUT; /* readline-specific */
+ break;
+ case 'e':
+ array[l++] = ESC;
+ break;
+ case 'f':
+ array[l++] = '\f';
+ break;
+ case 'n':
+ array[l++] = NEWLINE;
+ break;
+ case 'r':
+ array[l++] = RETURN;
+ break;
+ case 't':
+ array[l++] = TAB;
+ break;
+ case 'v':
+ array[l++] = 0x0B;
+ break;
+ case '\\':
+ array[l++] = '\\';
+ break;
+ case '0': case '1': case '2': case '3':
+ case '4': case '5': case '6': case '7':
+ i++;
+ for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
+ c = (c * 8) + OCTVALUE (seq[i]);
+ i--; /* auto-increment in for loop */
+ array[l++] = c % (largest_char + 1);
+ break;
+ case 'x':
+ i++;
+ for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
+ c = (c * 16) + HEXVALUE (seq[i]);
+ if (temp == 3)
+ c = 'x';
+ i--; /* auto-increment in for loop */
+ array[l++] = c % (largest_char + 1);
+ break;
+ default: /* backslashes before non-special chars just add the char */
+ array[l++] = c;
+ break; /* the backslash is stripped */
}
+ continue;
}
+
array[l++] = c;
}
@@ -541,8 +626,55 @@ static char *last_readline_init_file = (char *)NULL;
/* The file we're currently reading key bindings from. */
static char *current_readline_init_file;
+static int current_readline_init_include_level;
static int current_readline_init_lineno;
+/* Read FILENAME into a locally-allocated buffer and return the buffer.
+ The size of the buffer is returned in *SIZEP. Returns NULL if any
+ errors were encountered. */
+static char *
+_rl_read_file (filename, sizep)
+ char *filename;
+ size_t *sizep;
+{
+ struct stat finfo;
+ size_t file_size;
+ char *buffer;
+ int i, file;
+
+ if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
+ return ((char *)NULL);
+
+ file_size = (size_t)finfo.st_size;
+
+ /* check for overflow on very large files */
+ if (file_size != finfo.st_size || file_size + 1 < file_size)
+ {
+ if (file >= 0)
+ close (file);
+#if defined (EFBIG)
+ errno = EFBIG;
+#endif
+ return ((char *)NULL);
+ }
+
+ /* Read the file into BUFFER. */
+ buffer = (char *)xmalloc (file_size + 1);
+ i = read (file, buffer, file_size);
+ close (file);
+
+ if (i < file_size)
+ {
+ free (buffer);
+ return ((char *)NULL);
+ }
+
+ buffer[file_size] = '\0';
+ if (sizep)
+ *sizep = file_size;
+ return (buffer);
+}
+
/* Re-read the current keybindings file. */
int
rl_re_read_init_file (count, ignore)
@@ -565,11 +697,6 @@ int
rl_read_init_file (filename)
char *filename;
{
- register int i;
- char *buffer, *openname, *line, *end;
- struct stat finfo;
- int file;
-
/* Default the filename. */
if (filename == 0)
{
@@ -583,39 +710,37 @@ rl_read_init_file (filename)
if (*filename == 0)
filename = DEFAULT_INPUTRC;
- current_readline_init_file = filename;
- openname = tilde_expand (filename);
+ return (_rl_read_init_file (filename, 0));
+}
- if ((stat (openname, &finfo) < 0) ||
- (file = open (openname, O_RDONLY, 0666)) < 0)
- {
- free (openname);
- return (errno);
- }
- else
- free (openname);
+static int
+_rl_read_init_file (filename, include_level)
+ char *filename;
+ int include_level;
+{
+ register int i;
+ char *buffer, *openname, *line, *end;
+ size_t file_size;
- if (filename != last_readline_init_file)
- {
- if (last_readline_init_file)
- free (last_readline_init_file);
+ current_readline_init_file = filename;
+ current_readline_init_include_level = include_level;
+ openname = tilde_expand (filename);
+ buffer = _rl_read_file (openname, &file_size);
+ if (buffer == 0)
+ return (errno);
+
+ if (include_level == 0 && filename != last_readline_init_file)
+ {
+ FREE (last_readline_init_file);
last_readline_init_file = savestring (filename);
}
- /* Read the file into BUFFER. */
- buffer = (char *)xmalloc ((int)finfo.st_size + 1);
- i = read (file, buffer, finfo.st_size);
- close (file);
-
- if (i != finfo.st_size)
- return (errno);
-
/* Loop over the lines in the file. Lines that start with `#' are
comments; all other lines are commands for readline initialization. */
current_readline_init_lineno = 1;
line = buffer;
- end = buffer + finfo.st_size;
+ end = buffer + file_size;
while (line < end)
{
/* Find the end of this line. */
@@ -639,6 +764,7 @@ rl_read_init_file (filename)
line += i + 1;
current_readline_init_lineno++;
}
+
free (buffer);
return (0);
}
@@ -697,7 +823,7 @@ parser_if (args)
if (args[i])
args[i++] = '\0';
- /* Handle "if term=foo" and "if mode=emacs" constructs. If this
+ /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
isn't term=foo, or mode=emacs, then check to see if the first
word in ARGS is the same as the value stored in rl_readline_name. */
if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
@@ -749,9 +875,9 @@ parser_else (args)
{
register int i;
- if (!if_stack_depth)
+ if (if_stack_depth == 0)
{
- /* Error message? */
+ _rl_init_file_error ("$else found without matching $if");
return 0;
}
@@ -775,12 +901,36 @@ parser_endif (args)
if (if_stack_depth)
_rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
else
- {
- /* *** What, no error message? *** */
- }
+ _rl_init_file_error ("$endif without matching $if");
return 0;
}
+static int
+parser_include (args)
+ char *args;
+{
+ char *old_init_file, *e;
+ int old_line_number, old_include_level, r;
+
+ if (_rl_parsing_conditionalized_out)
+ return (0);
+
+ old_init_file = current_readline_init_file;
+ old_line_number = current_readline_init_lineno;
+ old_include_level = current_readline_init_include_level;
+
+ e = strchr (args, '\n');
+ if (e)
+ *e = '\0';
+ r = _rl_read_init_file (args, old_include_level + 1);
+
+ current_readline_init_file = old_init_file;
+ current_readline_init_lineno = old_line_number;
+ current_readline_init_include_level = old_include_level;
+
+ return r;
+}
+
/* Associate textual names with actual functions. */
static struct {
char *name;
@@ -789,6 +939,7 @@ static struct {
{ "if", parser_if },
{ "endif", parser_endif },
{ "else", parser_else },
+ { "include", parser_include },
{ (char *)0x0, (Function *)0x0 }
};
@@ -825,7 +976,8 @@ handle_parser_directive (statement)
return (0);
}
- /* *** Should an error message be output? */
+ /* display an error message about the unknown parser directive */
+ _rl_init_file_error ("unknown parser directive");
return (1);
}
@@ -940,10 +1092,9 @@ rl_parse_and_bind (string)
the quoted string delimiter, like the shell. */
if (*funname == '\'' || *funname == '"')
{
- int delimiter = string[i++];
- int passc = 0;
+ int delimiter = string[i++], passc;
- for (; c = string[i]; i++)
+ for (passc = 0; c = string[i]; i++)
{
if (passc)
{
@@ -981,11 +1132,11 @@ rl_parse_and_bind (string)
rl_set_key (). Otherwise, let the older code deal with it. */
if (*string == '"')
{
- char *seq = xmalloc (1 + strlen (string));
- register int j, k = 0;
- int passc = 0;
+ char *seq;
+ register int j, k, passc;
- for (j = 1; string[j]; j++)
+ seq = xmalloc (1 + strlen (string));
+ for (j = 1, k = passc = 0; string[j]; j++)
{
/* Allow backslash to quote characters, but leave them in place.
This allows a string to end with a backslash quoting another
@@ -1078,6 +1229,7 @@ static struct {
#if defined (PAREN_MATCHING)
{ "blink-matching-paren", &rl_blink_matching_paren },
#endif
+ { "completion-ignore-case", &_rl_completion_case_fold },
{ "convert-meta", &_rl_convert_meta_chars_to_ascii },
{ "disable-completion", &rl_inhibit_completion },
{ "enable-keypad", &_rl_enable_keypad },
@@ -1088,6 +1240,7 @@ static struct {
{ "mark-modified-lines", &_rl_mark_modified_lines },
{ "meta-flag", &_rl_meta_flag },
{ "output-meta", &_rl_output_meta_chars },
+ { "print-completions-horizontally", &_rl_print_completions_horizontally },
{ "show-all-if-ambiguous", &_rl_complete_show_all },
#if defined (VISIBLE_STATS)
{ "visible-stats", &rl_visible_stats },
@@ -1186,6 +1339,7 @@ rl_variable_bind (name, value)
_rl_bell_preference = AUDIBLE_BELL;
}
+ /* For the time being, unknown variable names are simply ignored. */
return 0;
}
@@ -1338,7 +1492,7 @@ _rl_get_keyname (key)
int key;
{
char *keyname;
- int i, c;
+ int i, c, v;
keyname = (char *)xmalloc (8);
@@ -1383,6 +1537,18 @@ _rl_get_keyname (key)
c = _rl_to_lower (UNCTRL (c));
}
+ /* XXX experimental code. Turn the characters that are not ASCII or
+ ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
+ This changes C. */
+ if (c >= 128 && c <= 159)
+ {
+ keyname[i++] = '\\';
+ keyname[i++] = '2';
+ c -= 128;
+ keyname[i++] = (c / 8) + '0';
+ c = (c % 8) + '0';
+ }
+
/* Now, if the character needs to be quoted with a backslash, do that. */
if (c == '\\' || c == '"')
keyname[i++] = '\\';
@@ -1692,10 +1858,13 @@ rl_variable_dumper (print_readably)
/* bell-style */
switch (_rl_bell_preference)
{
- case NO_BELL: kname = "none"; break;
- case VISIBLE_BELL: kname = "visible"; break;
+ case NO_BELL:
+ kname = "none"; break;
+ case VISIBLE_BELL:
+ kname = "visible"; break;
case AUDIBLE_BELL:
- default: kname = "audible"; break;
+ default:
+ kname = "audible"; break;
}
if (print_readably)
fprintf (rl_outstream, "set bell-style %s\n", kname);
diff --git a/lib/readline/callback.c b/lib/readline/callback.c
index 04c5bbd..34dbc72 100644
--- a/lib/readline/callback.c
+++ b/lib/readline/callback.c
@@ -55,10 +55,10 @@ extern int rl_visible_prompt_length;
things to handle at once, and dispatches them via select(). Call
rl_callback_handler_install() with the prompt and a function to call
whenever a complete line of input is ready. The user must then
- call readline_char() every time some input is available, and
- readline_char() will call the user's function with the complete text
- read in at each end of line. The terminal is kept prepped and signals
- handled all the time, except during calls to the user's function. */
+ call rl_callback_read_char() every time some input is available, and
+ rl_callback_read_char() will call the user's function with the complete
+ text read in at each end of line. The terminal is kept prepped and
+ signals handled all the time, except during calls to the user's function. */
VFunction *rl_linefunc; /* user callback function */
static int in_handler; /* terminal_prepped and signals set? */
diff --git a/lib/readline/chardefs.h b/lib/readline/chardefs.h
index 8e6f0ef..3e9e273 100644
--- a/lib/readline/chardefs.h
+++ b/lib/readline/chardefs.h
@@ -121,7 +121,20 @@
#ifdef ESC
#undef ESC
#endif
-
#define ESC CTRL('[')
+#ifndef ISOCTAL
+#define ISOCTAL(c) ((c) >= '0' && (c) <= '7')
+#endif
+#define OCTVALUE(c) ((c) - '0')
+
+#ifndef isxdigit
+# define isxdigit(c) (isdigit((c)) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
+#endif
+
+#define HEXVALUE(c) \
+ (((c) >= 'a' && (c) <= 'f') \
+ ? (c)-'a'+10 \
+ : (c) >= 'A' && (c) <= 'F' ? (c)-'A'+10 : (c)-'0')
+
#endif /* _CHARDEFS_H_ */
diff --git a/lib/readline/complete.c b/lib/readline/complete.c
index 552cb6c..985e898 100644
--- a/lib/readline/complete.c
+++ b/lib/readline/complete.c
@@ -75,6 +75,7 @@ extern char *tilde_expand ();
extern char *rl_copy_text ();
extern void _rl_abort_internal ();
extern int _rl_qsort_string_compare ();
+extern void _rl_replace_text ();
extern Function *rl_last_func;
extern int rl_editing_mode;
@@ -88,26 +89,32 @@ extern int rl_display_fixed;
char *filename_completion_function ();
char **completion_matches ();
+#if defined (VISIBLE_STATS)
+# if !defined (X_OK)
+# define X_OK 1
+# endif
+static int stat_char ();
+#endif
+
static char *rl_quote_filename ();
static char *rl_strpbrk ();
static char **remove_duplicate_matches ();
-static void insert_text ();
static void insert_match ();
-static void append_to_match ();
+static int append_to_match ();
static void insert_all_matches ();
static void display_matches ();
static int compute_lcd_of_matches ();
extern char *xmalloc (), *xrealloc ();
-/* If non-zero, then this is the address of a function to call when
- completing on a directory name. The function is called with
- the address of a string (the current directory name) as an arg. */
-Function *rl_directory_completion_hook = (Function *)NULL;
+/* **************************************************************** */
+/* */
+/* Completion matching, from readline's point of view. */
+/* */
+/* **************************************************************** */
-/* Non-zero means readline completion functions perform tilde expansion. */
-int rl_complete_with_tilde_expansion = 0;
+/* Variables known only to the readline library. */
/* If non-zero, non-unique completions always show the list of matches. */
int _rl_complete_show_all = 0;
@@ -115,27 +122,29 @@ int _rl_complete_show_all = 0;
/* If non-zero, completed directory names have a slash appended. */
int _rl_complete_mark_directories = 1;
-#if defined (VISIBLE_STATS)
-# if !defined (X_OK)
-# define X_OK 1
-# endif
+/* If non-zero, completions are printed horizontally in alphabetical order,
+ like `ls -x'. */
+int _rl_print_completions_horizontally;
-static int stat_char ();
+/* Non-zero means that case is not significant in filename completion. */
+int _rl_completion_case_fold;
+
+/* Global variables available to applications using readline. */
+#if defined (VISIBLE_STATS)
/* Non-zero means add an additional character to each filename displayed
during listing completion iff rl_filename_completion_desired which helps
to indicate the type of file being listed. */
int rl_visible_stats = 0;
#endif /* VISIBLE_STATS */
-/* **************************************************************** */
-/* */
-/* Completion matching, from readline's point of view. */
-/* */
-/* **************************************************************** */
+/* If non-zero, then this is the address of a function to call when
+ completing on a directory name. The function is called with
+ the address of a string (the current directory name) as an arg. */
+Function *rl_directory_completion_hook = (Function *)NULL;
-/* Local variable states what happened during the last completion attempt. */
-static int completion_changed_buffer;
+/* Non-zero means readline completion functions perform tilde expansion. */
+int rl_complete_with_tilde_expansion = 0;
/* Pointer to the generator function for completion_matches ().
NULL means to use filename_completion_function (), the default filename
@@ -242,6 +251,17 @@ int rl_completion_append_character = ' ';
/* If non-zero, inhibit completion (temporarily). */
int rl_inhibit_completion;
+/* Variables local to this file. */
+
+/* Local variable states what happened during the last completion attempt. */
+static int completion_changed_buffer;
+
+/*************************************/
+/* */
+/* Bindable completion functions */
+/* */
+/*************************************/
+
/* Complete the word at or before point. You have supplied the function
that does the initial simple matching selection algorithm (see
completion_matches ()). The default is to do filename completion. */
@@ -274,6 +294,33 @@ rl_insert_completions (ignore, invoking_key)
return (rl_complete_internal ('*'));
}
+/************************************/
+/* */
+/* Completion utility functions */
+/* */
+/************************************/
+
+/* Find the first occurrence in STRING1 of any character from STRING2.
+ Return a pointer to the character in STRING1. */
+static char *
+rl_strpbrk (string1, string2)
+ char *string1, *string2;
+{
+ register char *scan;
+
+ for (; *string1; string1++)
+ {
+ for (scan = string2; *scan; scan++)
+ {
+ if (*string1 == *scan)
+ {
+ return (string1);
+ }
+ }
+ }
+ return ((char *)NULL);
+}
+
/* The user must press "y" or "n". Non-zero return means "y" pressed. */
static int
get_y_or_n ()
@@ -293,6 +340,63 @@ get_y_or_n ()
}
}
+#if defined (VISIBLE_STATS)
+/* Return the character which best describes FILENAME.
+ `@' for symbolic links
+ `/' for directories
+ `*' for executables
+ `=' for sockets
+ `|' for FIFOs
+ `%' for character special devices
+ `#' for block special devices */
+static int
+stat_char (filename)
+ char *filename;
+{
+ struct stat finfo;
+ int character, r;
+
+#if defined (HAVE_LSTAT) && defined (S_ISLNK)
+ r = lstat (filename, &finfo);
+#else
+ r = stat (filename, &finfo);
+#endif
+
+ if (r == -1)
+ return (0);
+
+ character = 0;
+ if (S_ISDIR (finfo.st_mode))
+ character = '/';
+#if defined (S_ISCHR)
+ else if (S_ISCHR (finfo.st_mode))
+ character = '%';
+#endif /* S_ISCHR */
+#if defined (S_ISBLK)
+ else if (S_ISBLK (finfo.st_mode))
+ character = '#';
+#endif /* S_ISBLK */
+#if defined (S_ISLNK)
+ else if (S_ISLNK (finfo.st_mode))
+ character = '@';
+#endif /* S_ISLNK */
+#if defined (S_ISSOCK)
+ else if (S_ISSOCK (finfo.st_mode))
+ character = '=';
+#endif /* S_ISSOCK */
+#if defined (S_ISFIFO)
+ else if (S_ISFIFO (finfo.st_mode))
+ character = '|';
+#endif
+ else if (S_ISREG (finfo.st_mode))
+ {
+ if (access (filename, X_OK) == 0)
+ character = '*';
+ }
+ return (character);
+}
+#endif /* VISIBLE_STATS */
+
/* Return the portion of PATHNAME that should be output when listing
possible completions. If we are hacking filename completion, we
are only interested in the basename, the portion following the
@@ -309,26 +413,34 @@ printable_part (pathname)
/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
are using it, check for and output a single character for `special'
- filenames. Return 1 if we printed an extension character, 0 if not. */
+ filenames. Return the number of characters we output. */
#define PUTX(c) \
+ do { \
if (CTRL_CHAR (c)) \
{ \
putc ('^', rl_outstream); \
putc (UNCTRL (c), rl_outstream); \
+ printed_len += 2; \
} \
else if (c == RUBOUT) \
{ \
putc ('^', rl_outstream); \
putc ('?', rl_outstream); \
+ printed_len += 2; \
} \
else \
- putc (c, rl_outstream)
+ { \
+ putc (c, rl_outstream); \
+ printed_len++; \
+ } \
+ } while (0)
static int
print_filename (to_print, full_pathname)
char *to_print, *full_pathname;
{
+ int printed_len = 0;
#if !defined (VISIBLE_STATS)
char *s;
@@ -336,7 +448,6 @@ print_filename (to_print, full_pathname)
{
PUTX (*s);
}
- return 0;
#else
char *s, c, *new_full_pathname;
int extension_char, slen, tlen;
@@ -381,12 +492,13 @@ print_filename (to_print, full_pathname)
free (s);
if (extension_char)
- putc (extension_char, rl_outstream);
- return (extension_char != 0);
+ {
+ putc (extension_char, rl_outstream);
+ printed_len++;
+ }
}
- else
- return 0;
#endif /* VISIBLE_STATS */
+ return printed_len;
}
static char *
@@ -636,6 +748,125 @@ remove_duplicate_matches (matches)
return (temp_array);
}
+/* Find the common prefix of the list of matches, and put it into
+ matches[0]. */
+static int
+compute_lcd_of_matches (match_list, matches, text)
+ char **match_list;
+ int matches;
+ char *text;
+{
+ register int i, c1, c2, si;
+ int low; /* Count of max-matched characters. */
+
+ /* If only one match, just use that. Otherwise, compare each
+ member of the list with the next, finding out where they
+ stop matching. */
+ if (matches == 1)
+ {
+ match_list[0] = match_list[1];
+ match_list[1] = (char *)NULL;
+ return 1;
+ }
+
+ for (i = 1, low = 100000; i < matches; i++)
+ {
+ if (_rl_completion_case_fold)
+ {
+ for (si = 0;
+ (c1 = _rl_to_lower(match_list[i][si])) &&
+ (c2 = _rl_to_lower(match_list[i + 1][si]));
+ si++)
+ if (c1 != c2)
+ break;
+ }
+ else
+ {
+ for (si = 0;
+ (c1 = match_list[i][si]) &&
+ (c2 = match_list[i + 1][si]);
+ si++)
+ if (c1 != c2)
+ break;
+ }
+
+ if (low > si)
+ low = si;
+ }
+
+ /* If there were multiple matches, but none matched up to even the
+ first character, and the user typed something, use that as the
+ value of matches[0]. */
+ if (low == 0 && text && *text)
+ {
+ match_list[0] = xmalloc (strlen (text) + 1);
+ strcpy (match_list[0], text);
+ }
+ else
+ {
+ match_list[0] = xmalloc (low + 1);
+ strncpy (match_list[0], match_list[1], low);
+ match_list[0][low] = '\0';
+ }
+
+ return matches;
+}
+
+static int
+postprocess_matches (text, matchesp, matching_filenames)
+ char *text;
+ char ***matchesp;
+ int matching_filenames;
+{
+ char *t, **matches, **temp_matches;
+ int nmatch, i;
+
+ matches = *matchesp;
+
+ /* It seems to me that in all the cases we handle we would like
+ to ignore duplicate possiblilities. Scan for the text to
+ insert being identical to the other completions. */
+ if (rl_ignore_completion_duplicates)
+ {
+ temp_matches = remove_duplicate_matches (matches);
+ free (matches);
+ matches = temp_matches;
+ }
+
+ /* If we are matching filenames, then here is our chance to
+ do clever processing by re-examining the list. Call the
+ ignore function with the array as a parameter. It can
+ munge the array, deleting matches as it desires. */
+ if (rl_ignore_some_completions_function && matching_filenames)
+ {
+ for (nmatch = 1; matches[nmatch]; nmatch++)
+ ;
+ (void)(*rl_ignore_some_completions_function) (matches);
+ if (matches == 0 || matches[0] == 0)
+ {
+ FREE (matches);
+ ding ();
+ *matchesp = (char **)0;
+ return 0;
+ }
+ else
+ {
+ /* If we removed some matches, recompute the common prefix. */
+ for (i = 1; matches[i]; i++)
+ ;
+ if (i > 1 && i < nmatch)
+ {
+ t = matches[0];
+ compute_lcd_of_matches (matches, i - 1, text);
+ FREE (t);
+ }
+ }
+ }
+
+ *matchesp = matches;
+ return (1);
+}
+
static void
display_matches (matches)
char **matches;
@@ -718,25 +949,47 @@ display_matches (matches)
if (rl_ignore_completion_duplicates == 0)
qsort (matches + 1, len, sizeof (char *), _rl_qsort_string_compare);
- /* Print the sorted items, up-and-down alphabetically, like ls. */
crlf ();
- for (i = 1; i <= count; i++)
+ if (_rl_print_completions_horizontally == 0)
{
- for (j = 0, l = i; j < limit; j++)
+ /* Print the sorted items, up-and-down alphabetically, like ls. */
+ for (i = 1; i <= count; i++)
{
- if (l > len || matches[l] == 0)
- break;
- else
+ for (j = 0, l = i; j < limit; j++)
{
- temp = printable_part (matches[l]);
- printed_len = strlen (temp) + print_filename (temp, matches[l]);
+ if (l > len || matches[l] == 0)
+ break;
+ else
+ {
+ temp = printable_part (matches[l]);
+ printed_len = print_filename (temp, matches[l]);
- if (j + 1 < limit)
+ if (j + 1 < limit)
+ for (k = 0; k < max - printed_len; k++)
+ putc (' ', rl_outstream);
+ }
+ l += count;
+ }
+ crlf ();
+ }
+ }
+ else
+ {
+ /* Print the sorted items, across alphabetically, like ls -x. */
+ for (i = 1; matches[i]; i++)
+ {
+ temp = printable_part (matches[i]);
+ printed_len = print_filename (temp, matches[i]);
+ /* Have we reached the end of this line? */
+ if (matches[i+1])
+ {
+ if (i && (limit > 1) && (i % limit) == 0)
+ crlf ();
+ else
for (k = 0; k < max - printed_len; k++)
putc (' ', rl_outstream);
}
- l += count;
}
crlf ();
}
@@ -749,18 +1002,6 @@ display_matches (matches)
#endif
}
-static void
-insert_text (text, start, end)
- char *text;
- int start, end;
-{
- rl_begin_undo_group ();
- rl_delete_text (start, end + 1);
- rl_point = start;
- rl_insert_text (text);
- rl_end_undo_group ();
-}
-
static char *
make_quoted_replacement (match, mtype, qc)
char *match;
@@ -833,7 +1074,7 @@ insert_match (match, start, mtype, qc)
else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
replacement[0] != oqc)
start--;
- insert_text (replacement, start, rl_point - 1);
+ _rl_replace_text (replacement, start, rl_point - 1);
if (replacement != match)
free (replacement);
}
@@ -842,8 +1083,9 @@ insert_match (match, start, mtype, qc)
/* Append any necessary closing quote and a separator character to the
just-inserted match. If the user has specified that directories
should be marked by a trailing `/', append one of those instead. The
- default trailing character */
-static void
+ default trailing character is a space. Returns the number of characters
+ appended. */
+static int
append_to_match (text, delimiter, quote_char)
char *text;
int delimiter, quote_char;
@@ -883,6 +1125,8 @@ append_to_match (text, delimiter, quote_char)
if (rl_point == rl_end)
rl_insert_text (temp_string);
}
+
+ return (temp_string_index);
}
static void
@@ -935,27 +1179,24 @@ int
rl_complete_internal (what_to_do)
int what_to_do;
{
- char **matches, **temp_matches;
+ char **matches;
Function *our_func;
- int start, end, delimiter, found_quote, nmatch, i;
- char *text, *saved_line_buffer, *t;
+ int start, end, delimiter, found_quote, i;
+ char *text, *saved_line_buffer;
char quote_char;
- saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
-
- our_func = rl_completion_entry_function
- ? rl_completion_entry_function
- : (Function *)filename_completion_function;
-
/* Only the completion entry function can change these. */
rl_filename_completion_desired = 0;
rl_filename_quoting_desired = 1;
-
rl_completion_type = what_to_do;
+ saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
+ our_func = rl_completion_entry_function
+ ? rl_completion_entry_function
+ : (Function *)filename_completion_function;
+
/* We now look backwards for the start of a filename/variable word. */
end = rl_point;
-
found_quote = delimiter = 0;
quote_char = '\0';
@@ -975,50 +1216,19 @@ rl_complete_internal (what_to_do)
ding ();
FREE (saved_line_buffer);
free (text);
- return 0;
- }
-
- /* It seems to me that in all the cases we handle we would like
- to ignore duplicate possiblilities. Scan for the text to
- insert being identical to the other completions. */
- if (rl_ignore_completion_duplicates)
- {
- temp_matches = remove_duplicate_matches (matches);
- free (matches);
- matches = temp_matches;
+ return (0);
}
- /* If we are matching filenames, then here is our chance to
- do clever processing by re-examining the list. Call the
- ignore function with the array as a parameter. It can
- munge the array, deleting matches as it desires. */
- if (rl_ignore_some_completions_function &&
- our_func == (Function *)filename_completion_function)
+ /* If we are matching filenames, our_func will have been set to
+ filename_completion_function */
+ i = our_func == (Function *)filename_completion_function;
+ if (postprocess_matches (text, &matches, i) == 0)
{
- for (nmatch = 1; matches[nmatch]; nmatch++)
- ;
- (void)(*rl_ignore_some_completions_function) (matches);
- if (matches == 0 || matches[0] == 0)
- {
- FREE (matches);
- ding ();
- FREE (saved_line_buffer);
- FREE (text);
- return 0;
- }
- else
- {
- /* If we removed some matches, recompute the common prefix. */
- for (i = 1; matches[i]; i++)
- ;
- if (i > 1 && i < nmatch)
- {
- t = matches[0];
- compute_lcd_of_matches (matches, i - 1, text);
- FREE (t);
- }
- }
+ FREE (saved_line_buffer);
+ free (text);
+ return (0);
}
+
free (text);
switch (what_to_do)
@@ -1081,62 +1291,67 @@ rl_complete_internal (what_to_do)
return 0;
}
-#if defined (VISIBLE_STATS)
-/* Return the character which best describes FILENAME.
- `@' for symbolic links
- `/' for directories
- `*' for executables
- `=' for sockets
- `|' for FIFOs
- `%' for character special devices
- `#' for block special devices */
-static int
-stat_char (filename)
- char *filename;
+/***************************************************************/
+/* */
+/* Application-callable completion match generator functions */
+/* */
+/***************************************************************/
+
+/* Return an array of (char *) which is a list of completions for TEXT.
+ If there are no completions, return a NULL pointer.
+ The first entry in the returned array is the substitution for TEXT.
+ The remaining entries are the possible completions.
+ The array is terminated with a NULL pointer.
+
+ ENTRY_FUNCTION is a function of two args, and returns a (char *).
+ The first argument is TEXT.
+ The second is a state argument; it should be zero on the first call, and
+ non-zero on subsequent calls. It returns a NULL pointer to the caller
+ when there are no more matches.
+ */
+char **
+completion_matches (text, entry_function)
+ char *text;
+ CPFunction *entry_function;
{
- struct stat finfo;
- int character, r;
+ /* Number of slots in match_list. */
+ int match_list_size;
-#if defined (HAVE_LSTAT) && defined (S_ISLNK)
- r = lstat (filename, &finfo);
-#else
- r = stat (filename, &finfo);
-#endif
+ /* The list of matches. */
+ char **match_list;
- if (r == -1)
- return (0);
+ /* Number of matches actually found. */
+ int matches;
- character = 0;
- if (S_ISDIR (finfo.st_mode))
- character = '/';
-#if defined (S_ISCHR)
- else if (S_ISCHR (finfo.st_mode))
- character = '%';
-#endif /* S_ISCHR */
-#if defined (S_ISBLK)
- else if (S_ISBLK (finfo.st_mode))
- character = '#';
-#endif /* S_ISBLK */
-#if defined (S_ISLNK)
- else if (S_ISLNK (finfo.st_mode))
- character = '@';
-#endif /* S_ISLNK */
-#if defined (S_ISSOCK)
- else if (S_ISSOCK (finfo.st_mode))
- character = '=';
-#endif /* S_ISSOCK */
-#if defined (S_ISFIFO)
- else if (S_ISFIFO (finfo.st_mode))
- character = '|';
-#endif
- else if (S_ISREG (finfo.st_mode))
+ /* Temporary string binder. */
+ char *string;
+
+ matches = 0;
+ match_list_size = 10;
+ match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
+ match_list[1] = (char *)NULL;
+
+ while (string = (*entry_function) (text, matches))
{
- if (access (filename, X_OK) == 0)
- character = '*';
+ if (matches + 1 == match_list_size)
+ match_list = (char **)xrealloc
+ (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
+
+ match_list[++matches] = string;
+ match_list[matches + 1] = (char *)NULL;
}
- return (character);
+
+ /* If there were any matches, then look through them finding out the
+ lowest common denominator. That then becomes match_list[0]. */
+ if (matches)
+ compute_lcd_of_matches (match_list, matches, text);
+ else /* There were no matches. */
+ {
+ free (match_list);
+ match_list = (char **)NULL;
+ }
+ return (match_list);
}
-#endif /* VISIBLE_STATS */
/* A completion function for usernames.
TEXT contains a partial username preceded by a random
@@ -1194,135 +1409,6 @@ username_completion_function (text, state)
#endif /* !__GO32__ */
}
-/* **************************************************************** */
-/* */
-/* Completion */
-/* */
-/* **************************************************************** */
-
-/* Non-zero means that case is not significant in completion. */
-int completion_case_fold = 0;
-
-/* Find the common prefix of the list of matches, and put it into
- matches[0]. */
-static int
-compute_lcd_of_matches (match_list, matches, text)
- char **match_list;
- int matches;
- char *text;
-{
- register int i, c1, c2, si;
- int low; /* Count of max-matched characters. */
-
- /* If only one match, just use that. Otherwise, compare each
- member of the list with the next, finding out where they
- stop matching. */
- if (matches == 1)
- {
- match_list[0] = match_list[1];
- match_list[1] = (char *)NULL;
- return 1;
- }
-
- for (i = 1, low = 100000; i < matches; i++)
- {
- if (completion_case_fold)
- {
- for (si = 0;
- (c1 = _rl_to_lower(match_list[i][si])) &&
- (c2 = _rl_to_lower(match_list[i + 1][si]));
- si++)
- if (c1 != c2)
- break;
- }
- else
- {
- for (si = 0;
- (c1 = match_list[i][si]) &&
- (c2 = match_list[i + 1][si]);
- si++)
- if (c1 != c2)
- break;
- }
-
- if (low > si)
- low = si;
- }
-
- /* If there were multiple matches, but none matched up to even the
- first character, and the user typed something, use that as the
- value of matches[0]. */
- if (low == 0 && text && *text)
- {
- match_list[0] = xmalloc (strlen (text) + 1);
- strcpy (match_list[0], text);
- }
- else
- {
- match_list[0] = xmalloc (low + 1);
- strncpy (match_list[0], match_list[1], low);
- match_list[0][low] = '\0';
- }
-
- return matches;
-}
-
-/* Return an array of (char *) which is a list of completions for TEXT.
- If there are no completions, return a NULL pointer.
- The first entry in the returned array is the substitution for TEXT.
- The remaining entries are the possible completions.
- The array is terminated with a NULL pointer.
-
- ENTRY_FUNCTION is a function of two args, and returns a (char *).
- The first argument is TEXT.
- The second is a state argument; it should be zero on the first call, and
- non-zero on subsequent calls. It returns a NULL pointer to the caller
- when there are no more matches.
- */
-char **
-completion_matches (text, entry_function)
- char *text;
- CPFunction *entry_function;
-{
- /* Number of slots in match_list. */
- int match_list_size;
-
- /* The list of matches. */
- char **match_list;
-
- /* Number of matches actually found. */
- int matches;
-
- /* Temporary string binder. */
- char *string;
-
- matches = 0;
- match_list_size = 10;
- match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
- match_list[1] = (char *)NULL;
-
- while (string = (*entry_function) (text, matches))
- {
- if (matches + 1 == match_list_size)
- match_list = (char **)xrealloc
- (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
-
- match_list[++matches] = string;
- match_list[matches + 1] = (char *)NULL;
- }
-
- /* If there were any matches, then look through them finding out the
- lowest common denominator. That then becomes match_list[0]. */
- if (matches)
- compute_lcd_of_matches (match_list, matches, text);
- else /* There were no matches. */
- {
- free (match_list);
- match_list = (char **)NULL;
- }
- return (match_list);
-}
-
/* Okay, now we write the entry_function for filename completion. In the
general case. Note that completion in the shell is a little different
because of all the pathnames that must be followed when looking up the
@@ -1421,10 +1507,20 @@ filename_completion_function (text, state)
{
/* Otherwise, if these match up to the length of filename, then
it is a match. */
- if ((entry->d_name[0] == filename[0]) &&
- (((int)D_NAMLEN (entry)) >= filename_len) &&
- (strncmp (filename, entry->d_name, filename_len) == 0))
- break;
+ if (_rl_completion_case_fold)
+ {
+ if ((_rl_to_lower (entry->d_name[0]) == _rl_to_lower (filename[0])) &&
+ (((int)D_NAMLEN (entry)) >= filename_len) &&
+ (_rl_strnicmp (filename, entry->d_name, filename_len) == 0))
+ break;
+ }
+ else
+ {
+ if ((entry->d_name[0] == filename[0]) &&
+ (((int)D_NAMLEN (entry)) >= filename_len) &&
+ (strncmp (filename, entry->d_name, filename_len) == 0))
+ break;
+ }
}
}
@@ -1487,74 +1583,120 @@ filename_completion_function (text, state)
}
}
-/* A function for simple tilde expansion. */
+/* An initial implementation of a menu completion function a la tcsh. The
+ first time (if the last readline command was not rl_menu_complete), we
+ generate the list of matches. This code is very similar to the code in
+ rl_complete_internal -- there should be a way to combine the two. Then,
+ for each item in the list of matches, we insert the match in an undoable
+ fashion, with the appropriate character appended (this happens on the
+ second and subsequent consecutive calls to rl_menu_complete). When we
+ hit the end of the match list, we restore the original unmatched text,
+ ring the bell, and reset the counter to zero. */
int
-rl_tilde_expand (ignore, key)
- int ignore, key;
+rl_menu_complete (count, ignore)
+ int count, ignore;
{
- register int start, end;
- char *homedir, *temp;
- int len;
+ Function *our_func;
+ int matching_filenames, found_quote;
+
+ static char *orig_text;
+ static char **matches = (char **)0;
+ static int match_list_index = 0;
+ static int match_list_size = 0;
+ static int orig_start, orig_end;
+ static char quote_char;
+ static int delimiter;
+
+ /* The first time through, we generate the list of matches and set things
+ up to insert them. */
+ if (rl_last_func != rl_menu_complete)
+ {
+ /* Clean up from previous call, if any. */
+ FREE (orig_text);
+ if (matches)
+ {
+ for (match_list_index = 0; matches[match_list_index]; match_list_index++)
+ free (matches[match_list_index]);
+ free (matches);
+ }
- end = rl_point;
- start = end - 1;
+ match_list_index = match_list_size = 0;
+ matches = (char **)NULL;
- if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
- {
- homedir = tilde_expand ("~");
- insert_text (homedir, start, end);
- return (0);
- }
- else if (rl_line_buffer[start] != '~')
- {
- for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
+ /* Only the completion entry function can change these. */
+ rl_filename_completion_desired = 0;
+ rl_filename_quoting_desired = 1;
+ rl_completion_type = '%';
+
+ our_func = rl_completion_entry_function
+ ? rl_completion_entry_function
+ : (Function *)filename_completion_function;
+
+ /* We now look backwards for the start of a filename/variable word. */
+ orig_end = rl_point;
+ found_quote = delimiter = 0;
+ quote_char = '\0';
+
+ if (rl_point)
+ /* This (possibly) changes rl_point. If it returns a non-zero char,
+ we know we have an open quote. */
+ quote_char = find_completion_word (&found_quote, &delimiter);
+
+ orig_start = rl_point;
+ rl_point = orig_end;
+
+ orig_text = rl_copy_text (orig_start, orig_end);
+ matches = gen_completion_matches (orig_text, orig_start, orig_end,
+ our_func, found_quote, quote_char);
+
+ /* If we are matching filenames, our_func will have been set to
+ filename_completion_function */
+ matching_filenames = our_func == (Function *)filename_completion_function;
+ if (matches == 0 || postprocess_matches (orig_text, &matches, matching_filenames) == 0)
+ {
+ ding ();
+ FREE (matches);
+ matches = (char **)0;
+ FREE (orig_text);
+ orig_text = (char *)0;
+ completion_changed_buffer = 0;
+ return (0);
+ }
+
+ for (match_list_size = 0; matches[match_list_size]; match_list_size++)
;
- start++;
+ /* matches[0] is lcd if match_list_size > 1, but the circular buffer
+ code below should take care of it. */
}
- end = start;
- do
- end++;
- while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
-
- if (whitespace (rl_line_buffer[end]) || end >= rl_end)
- end--;
+ /* Now we have the list of matches. Replace the text between
+ rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
+ matches[match_list_index], and add any necessary closing char. */
- /* If the first character of the current word is a tilde, perform
- tilde expansion and insert the result. If not a tilde, do
- nothing. */
- if (rl_line_buffer[start] == '~')
+ if (matches == 0 || match_list_size == 0)
{
- len = end - start + 1;
- temp = xmalloc (len + 1);
- strncpy (temp, rl_line_buffer + start, len);
- temp[len] = '\0';
- homedir = tilde_expand (temp);
- free (temp);
-
- insert_text (homedir, start, end);
+ ding ();
+ FREE (matches);
+ matches = (char **)0;
+ completion_changed_buffer = 0;
+ return (0);
}
- return (0);
-}
-
-/* Find the first occurrence in STRING1 of any character from STRING2.
- Return a pointer to the character in STRING1. */
-static char *
-rl_strpbrk (string1, string2)
- char *string1, *string2;
-{
- register char *scan;
+ match_list_index = (match_list_index + count) % match_list_size;
+ if (match_list_index < 0)
+ match_list_index += match_list_size;
- for (; *string1; string1++)
+ if (match_list_index == 0)
{
- for (scan = string2; *scan; scan++)
- {
- if (*string1 == *scan)
- {
- return (string1);
- }
- }
+ ding ();
+ insert_match (orig_text, orig_start, MULT_MATCH, &quote_char);
}
- return ((char *)NULL);
+ else
+ {
+ insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
+ append_to_match (matches[match_list_index], delimiter, quote_char);
+ }
+
+ completion_changed_buffer = 1;
+ return (0);
}
diff --git a/lib/readline/display.c b/lib/readline/display.c
index 609d827..edb5b20 100644
--- a/lib/readline/display.c
+++ b/lib/readline/display.c
@@ -72,7 +72,11 @@ extern int _rl_prefer_visible_bell;
/* Variables and functions imported from terminal.c */
extern void _rl_output_some_chars ();
+#ifdef _MINIX
+extern void _rl_output_character_function ();
+#else
extern int _rl_output_character_function ();
+#endif
extern int _rl_backspace ();
extern char *term_clreol, *term_clrpag;
diff --git a/lib/readline/doc/hist.texinfo b/lib/readline/doc/hist.texinfo
index aa04553..be8742f 100644
--- a/lib/readline/doc/hist.texinfo
+++ b/lib/readline/doc/hist.texinfo
@@ -7,20 +7,25 @@
@setchapternewpage odd
@ignore
-last change: Thu Mar 21 16:07:29 EST 1996
+last change: Thu Apr 2 14:38:22 EST 1998
@end ignore
-@set EDITION 2.1
-@set VERSION 2.1
-@set UPDATED 21 March 1996
-@set UPDATE-MONTH March 1996
+@set EDITION 2.2
+@set VERSION 2.2
+@set UPDATED 2 April 1998
+@set UPDATE-MONTH April 1998
+
+@dircategory Libraries
+@direntry
+* History: (history). The GNU history library API
+@end direntry
@ifinfo
This document describes the GNU History library, a programming tool that
provides a consistent user interface for recalling lines of previously
typed input.
-Copyright (C) 1988, 1991, 1993, 1995, 1996 Free Software Foundation, Inc.
+Copyright (C) 1988, 1991, 1993, 1995, 1996, 1998 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
diff --git a/lib/readline/doc/hsuser.texinfo b/lib/readline/doc/hsuser.texinfo
index 6e95649..76cb63b 100644
--- a/lib/readline/doc/hsuser.texinfo
+++ b/lib/readline/doc/hsuser.texinfo
@@ -29,7 +29,7 @@ into another language, under the above conditions for modified versions.
@ifset BashFeatures
This chapter describes how to use the GNU History Library interactively,
from a user's standpoint. It should be considered a user's guide. For
-information on using the GNU History Library in your own programs,
+information on using the GNU History Library in other programs,
see the GNU Readline Library Manual.
@end ifset
@ifclear BashFeatures
@@ -43,6 +43,8 @@ information on using the GNU History Library in your own programs,
@menu
* Bash History Facilities:: How Bash lets you manipulate your command
history.
+* Bash History Builtins:: The Bash builtin commands that manipulate
+ the command history.
* History Interaction:: What it feels like using History as a user.
@end menu
@end ifset
@@ -84,11 +86,10 @@ not saved. After saving the history, the history file is truncated
to contain no more than @code{$HISTFILESIZE}
lines. If @code{HISTFILESIZE} is not set, no truncation is performed.
-The builtin command @code{fc} (@pxref{Korn Shell Builtins})
-may be used to list or edit and re-execute a portion of
-the history list. The @code{history} builtin (@pxref{C Shell Builtins})
-can be used to display or modify the history list and
-manipulate the history file.
+The builtin command @code{fc} may be used to list or edit and re-execute
+a portion of the history list.
+The @code{history} builtin can be used to display or modify the history
+list and manipulate the history file.
When using the command-line editing, search commands
are available in each editing mode that provide access to the
history list.
@@ -104,11 +105,103 @@ semicolons where necessary to preserve syntactic correctness.
The @code{lithist}
shell option causes the shell to save the command with embedded newlines
instead of semicolons.
-@xref{Bash Builtins} for a description of @code{shopt}.
+@xref{Bash Builtins}, for a description of @code{shopt}.
+
+@node Bash History Builtins
+@section Bash History Builtins
+@cindex history builtins
+
+Bash provides two builtin commands that allow you to manipulate the
+history list and history file.
+
+@table @code
+
+@item fc
+@btindex fc
+@example
+@code{fc [-e @var{ename}] [-nlr] [@var{first}] [@var{last}]}
+@code{fc -s [@var{pat}=@var{rep}] [@var{command}]}
+@end example
+
+Fix Command. In the first form, a range of commands from @var{first} to
+@var{last} is selected from the history list. Both @var{first} and
+@var{last} may be specified as a string (to locate the most recent
+command beginning with that string) or as a number (an index into the
+history list, where a negative number is used as an offset from the
+current command number). If @var{last} is not specified it is set to
+@var{first}. If @var{first} is not specified it is set to the previous
+command for editing and @minus{}16 for listing. If the @samp{-l} flag is
+given, the commands are listed on standard output. The @samp{-n} flag
+suppresses the command numbers when listing. The @samp{-r} flag
+reverses the order of the listing. Otherwise, the editor given by
+@var{ename} is invoked on a file containing those commands. If
+@var{ename} is not given, the value of the following variable expansion
+is used: @code{$@{FCEDIT:-$@{EDITOR:-vi@}@}}. This says to use the
+value of the @code{FCEDIT} variable if set, or the value of the
+@code{EDITOR} variable if that is set, or @code{vi} if neither is set.
+When editing is complete, the edited commands are echoed and executed.
+
+In the second form, @var{command} is re-executed after each instance
+of @var{pat} in the selected command is replaced by @var{rep}.
+
+A useful alias to use with the @code{fc} command is @code{r='fc -s'}, so
+that typing @samp{r cc} runs the last command beginning with @code{cc}
+and typing @samp{r} re-executes the last command (@pxref{Aliases}).
+
+@item history
+@btindex history
+@example
+history [-c] [@var{n}]
+history [-anrw] [@var{filename}]
+history -ps @var{arg}
+@end example
+
+Display the history list with line numbers. Lines prefixed with
+with a @samp{*} have been modified. An argument of @var{n} says
+to list only the last @var{n} lines. Options, if supplied, have
+the following meanings:
+
+@table @code
+@item -w
+Write out the current history to the history file.
+
+@item -r
+Read the current history file and append its contents to
+the history list.
+
+@item -a
+Append the new
+history lines (history lines entered since the beginning of the
+current Bash session) to the history file.
+
+@item -n
+Append the history lines not already read from the history file
+to the current history list. These are lines appended to the history
+file since the beginning of the current Bash session.
+
+@item -c
+Clear the history list. This may be combined
+with the other options to replace the history list completely.
+
+@item -s
+The @var{arg}s are added to the end of
+the history list as a single entry.
+
+@item -p
+Perform history substitution on the @var{arg}s and display the result
+on the standard output, without storing the results in the history list.
+@end table
+
+When the @samp{-w}, @samp{-r}, @samp{-a}, or @samp{-n} option is
+used, if @var{filename}
+is given, then it is used as the history file. If not, then
+the value of the @code{HISTFILE} variable is used.
+
+@end table
@end ifset
@node History Interaction
-@section Interactive History Expansion
+@section History Expansion
@cindex history expansion
The History library provides a history expansion feature that is similar
@@ -121,14 +214,14 @@ arguments to a previous command into the current input line, or
fix errors in previous commands quickly.
History expansion takes place in two parts. The first is to determine
-which line from the previous history should be used during substitution.
+which line from the history list should be used during substitution.
The second is to select portions of that line for inclusion into the
-current one. The line selected from the previous history is called the
+current one. The line selected from the history is called the
@dfn{event}, and the portions of that line that are acted upon are
called @dfn{words}. Various @dfn{modifiers} are available to manipulate
the selected words. The line is broken into words in the same fashion
-that Bash does, so that several English (or Unix) words
-surrounded by quotes are considered as one word.
+that Bash does, so that several words
+surrounded by quotes are considered one word.
History expansions are introduced by the appearance of the
history expansion character, which is @samp{!} by default.
@ifset BashFeatures
@@ -153,6 +246,7 @@ may be used to see what a history expansion will do before using it.
The @samp{-s} option to the @code{history} builtin may be used to
add commands to the end of the history list without actually executing
them, so that they are available for subsequent recall.
+This is most useful in conjunction with Readline.
The shell allows control of the various characters used by the
history expansion mechanism with the @code{histchars} variable.
@@ -176,7 +270,7 @@ history list.
@item @code{!}
Start a history substitution, except when followed by a space, tab,
-the end of the line, @key{=} or @key{(}.
+the end of the line, @samp{=} or @samp{(}.
@item @code{!@var{n}}
Refer to command line @var{n}.
@@ -210,7 +304,7 @@ The entire command line typed so far.
Word designators are used to select desired words from the event.
A @samp{:} separates the event specification from the word designator. It
-can be omitted if the word designator begins with a @samp{^}, @samp{$},
+may be omitted if the word designator begins with a @samp{^}, @samp{$},
@samp{*}, @samp{-}, or @samp{%}. Words are numbered from the beginning
of the line, with the first word being denoted by 0 (zero). Words are
inserted into the current line separated by single spaces.
diff --git a/lib/readline/doc/rlman.texinfo b/lib/readline/doc/rlman.texinfo
index 655f3db..dd6478a 100644
--- a/lib/readline/doc/rlman.texinfo
+++ b/lib/readline/doc/rlman.texinfo
@@ -7,20 +7,25 @@
@setchapternewpage odd
@ignore
-last change: Thu Mar 21 16:06:39 EST 1996
+last change: Thu Apr 2 14:39:03 EST 1998
@end ignore
-@set EDITION 2.1
-@set VERSION 2.1
-@set UPDATED 21 March 1996
-@set UPDATE-MONTH March 1996
+@set EDITION 2.2
+@set VERSION 2.2
+@set UPDATED 2 April 1998
+@set UPDATE-MONTH April 1998
+
+@dircategory Libraries
+@direntry
+* Readline: (readline). The GNU readline library API
+@end direntry
@ifinfo
This document describes the GNU Readline Library, a utility which aids
in the consistency of user interface across discrete programs that need
to provide a command line interface.
-Copyright (C) 1988, 1991 Free Software Foundation, Inc.
+Copyright (C) 1988, 1991, 1993, 1996, 1998 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
diff --git a/lib/readline/doc/rltech.texinfo b/lib/readline/doc/rltech.texinfo
index ea0d317..bce5087 100644
--- a/lib/readline/doc/rltech.texinfo
+++ b/lib/readline/doc/rltech.texinfo
@@ -454,6 +454,14 @@ Bind @var{key} to the null function in @var{map}.
Returns non-zero in case of error.
@end deftypefun
+@deftypefun int rl_unbind_function_in_map (Function *function, Keymap map)
+Unbind all keys that execute @var{function} in @var{map}.
+@end deftypefun
+
+@deftypefun int rl_unbind_command_in_map (char *command, Keymap map)
+Unbind all keys that are bound to @var{command} in @var{map}.
+@end deftypefun
+
@deftypefun int rl_generic_bind (int type, char *keyseq, char *data, Keymap map)
Bind the key sequence represented by the string @var{keyseq} to the arbitrary
pointer @var{data}. @var{type} says what kind of data is pointed to by
@@ -1034,7 +1042,7 @@ unless they also appear within this list.
@deftypevar {char *} rl_filename_quote_characters
A list of characters that cause a filename to be quoted by the completer
-when they appear in a completed filename. The default is empty.
+when they appear in a completed filename. The default is the null string.
@end deftypevar
@deftypevar {char *} rl_special_prefixes
diff --git a/lib/readline/doc/rluser.texinfo b/lib/readline/doc/rluser.texinfo
index f7d6fd8..b2fd060 100644
--- a/lib/readline/doc/rluser.texinfo
+++ b/lib/readline/doc/rluser.texinfo
@@ -135,7 +135,7 @@ Delete the character underneath the cursor.
@item @w{Printing characters}
Insert the character into the line at the cursor.
@item @key{C-_}
-Undo the last thing that you did. You can undo all the way back to an
+Undo the last editing command. You can undo all the way back to an
empty line.
@end table
@@ -155,7 +155,7 @@ Move to the start of the line.
@item C-e
Move to the end of the line.
@item M-f
-Move forward a word.
+Move forward a word, where a word is composed of letters and digits.
@item M-b
Move backward a word.
@item C-l
@@ -207,7 +207,7 @@ Kill from the cursor to the previous whitespace. This is different than
@end table
-And, here is how to @dfn{yank} the text back into the line. Yanking
+Here is how to @dfn{yank} the text back into the line. Yanking
means to copy the most-recently-killed text from the kill buffer.
@table @key
@@ -227,10 +227,10 @@ argument acts as a repeat count, other times it is the @i{sign} of the
argument that is significant. If you pass a negative argument to a
command which normally acts in a forward direction, that command will
act in a backward direction. For example, to kill text back to the
-start of the line, you might type @w{@kbd{M-- C-k}}.
+start of the line, you might type @samp{M-- C-k}.
The general way to pass numeric arguments to a command is to type meta
-digits before the command. If the first `digit' you type is a minus
+digits before the command. If the first `digit' typed is a minus
sign (@key{-}), then the sign of the argument will be negative. Once
you have typed one meta digit to get the argument started, you can type
the remainder of the digits, and then the command. For example, to give
@@ -248,38 +248,38 @@ There are two search modes: @var{incremental} and @var{non-incremental}.
Incremental searches begin before the user has finished typing the
search string.
-As each character of the search string is typed, readline displays
+As each character of the search string is typed, Readline displays
the next entry from the history matching the string typed so far.
An incremental search requires only as many characters as needed to
find the desired history entry.
-The Escape character is used to terminate an incremental search.
-Control-J will also terminate the search.
-Control-G will abort an incremental search and restore the original
-line.
+The @key{ESC} character is used to terminate an incremental search.
+@key{C-j} will also terminate the search.
+@key{C-g} will abort an incremental search and restore the original line.
When the search is terminated, the history entry containing the
search string becomes the current line.
-To find other matching entries in the history list, type Control-S or
-Control-R as appropriate.
+To find other matching entries in the history list, type @key{C-s} or
+@key{C-r} as appropriate.
This will search backward or forward in the history for the next
entry matching the search string typed so far.
-Any other key sequence bound to a readline command will terminate
+Any other key sequence bound to a Readline command will terminate
the search and execute that command.
-For instance, a @code{newline} will terminate the search and accept
+For instance, a @key{RET} will terminate the search and accept
the line, thereby executing the command from the history list.
Non-incremental searches read the entire search string before starting
to search for matching history lines. The search string may be
-typed by the user or part of the contents of the current line.
+typed by the user or be part of the contents of the current line.
@node Readline Init File
@section Readline Init File
@cindex initialization file, readline
Although the Readline library comes with a set of @code{emacs}-like
-keybindings installed by default,
-it is possible that you would like to use a different set
-of keybindings. You can customize programs that use Readline by putting
-commands in an @dfn{inputrc} file in your home directory. The name of this
+keybindings installed by default, it is possible to use a different set
+of keybindings.
+Any user can customize programs that use Readline by putting
+commands in an @dfn{inputrc} file in his home directory.
+The name of this
@ifset BashFeatures
file is taken from the value of the shell variable @code{INPUTRC}. If
@end ifset
@@ -314,16 +314,18 @@ denote variable settings and key bindings.
@table @asis
@item Variable Settings
-You can change the state of a few variables in Readline by
-using the @code{set} command within the init file. Here is how you
-would specify that you wish to use @code{vi} line editing commands:
+You can modify the run-time behavior of Readline by
+altering the values of variables in Readline
+using the @code{set} command within the init file. Here is how to
+change from the default Emacs-like key binding to use
+@code{vi} line editing commands:
@example
set editing-mode vi
@end example
-Right now, there are only a few variables which can be set;
-so few, in fact, that we just list them here:
+A great deal of run-time behavior is changeable with the following
+variables.
@table @code
@@ -341,6 +343,11 @@ The string to insert at the beginning of the line when the
@code{insert-comment} command is executed. The default value
is @code{"#"}.
+@item completion-ignore-case
+If set to @samp{on}, Readline performs filename matching and completion
+in a case-insensitive fashion.
+The default value is @samp{off}.
+
@item completion-query-items
@vindex completion-query-items
The number of possible completions that determines when the user is
@@ -353,26 +360,26 @@ them; otherwise, they are simply listed. The default limit is
@item convert-meta
@vindex convert-meta
If set to @samp{on}, Readline will convert characters with the
-eigth bit set to an ASCII key sequence by stripping the eigth
+eighth bit set to an ASCII key sequence by stripping the eighth
bit and prepending an @key{ESC} character, converting them to a
meta-prefixed key sequence. The default value is @samp{on}.
@item disable-completion
@vindex disable-completion
-If set to @samp{On}, readline will inhibit word completion.
+If set to @samp{On}, Readline will inhibit word completion.
Completion characters will be inserted into the line as if they had
been mapped to @code{self-insert}. The default is @samp{off}.
@item editing-mode
@vindex editing-mode
-The @code{editing-mode} variable controls which editing mode you are
-using. By default, Readline starts up in Emacs editing mode, where
-the keystrokes are most similar to Emacs. This variable can be
+The @code{editing-mode} variable controls which default set of
+key bindings is used. By default, Readline starts up in Emacs editing
+mode, where the keystrokes are most similar to Emacs. This variable can be
set to either @samp{emacs} or @samp{vi}.
@item enable-keypad
@vindex enable-keypad
-When set to @samp{on}, readline will try to enable the application
+When set to @samp{on}, Readline will try to enable the application
keypad when it is called. Some systems need this to enable the
arrow keys. The default is @samp{off}.
@@ -384,7 +391,7 @@ attempts word completion. The default is @samp{off}.
@item horizontal-scroll-mode
@vindex horizontal-scroll-mode
This variable can be set to either @samp{on} or @samp{off}. Setting it
-to @samp{on} means that the text of the lines that you edit will scroll
+to @samp{on} means that the text of the lines being edited will scroll
horizontally on a single screen line when they are longer than the width
of the screen, instead of wrapping onto a new screen line. By default,
this variable is set to @samp{off}.
@@ -411,8 +418,8 @@ appended. The default is @samp{on}.
@item mark-modified-lines
@vindex mark-modified-lines
-This variable, when set to @samp{on}, says to display an asterisk
-(@samp{*}) at the start of history lines which have been modified.
+This variable, when set to @samp{on}, causes Readline to display an
+asterisk (@samp{*}) at the start of history lines which have been modified.
This variable is @samp{off} by default.
@item input-meta
@@ -430,6 +437,11 @@ If set to @samp{on}, Readline will display characters with the
eighth bit set directly rather than as a meta-prefixed escape
sequence. The default is @samp{off}.
+@item print-completions-horizontally
+If set to @samp{on}, Readline will display completions with matches
+sorted horizontally in alphabetical order, rather than down the screen.
+The default is @samp{off}.
+
@item show-all-if-ambiguous
@vindex show-all-if-ambiguous
This alters the default behavior of the completion functions. If
@@ -449,9 +461,9 @@ completions. The default is @samp{off}.
@item Key Bindings
The syntax for controlling key bindings in the init file is
simple. First you have to know the name of the command that you
-want to change. The following pages contain tables of the command name,
-the default keybinding, and a short description of what the command
-does.
+want to change. The following sections contain tables of the command
+name, the default keybinding, if any, and a short description of what
+the command does.
Once you know the name of the command, simply place the name of the key
you wish to bind the command to, a colon, and then the name of the
@@ -468,8 +480,8 @@ Meta-Rubout: backward-kill-word
Control-o: "> output"
@end example
-In the above example, @samp{C-u} is bound to the function
-@code{universal-argument}, and @samp{C-o} is bound to run the macro
+In the above example, @key{C-u} is bound to the function
+@code{universal-argument}, and @key{C-o} is bound to run the macro
expressed on the right hand side (that is, to insert the text
@samp{> output} into the line).
@@ -486,12 +498,16 @@ special character names are not recognized.
"\e[11~": "Function Key 1"
@end example
-In the above example, @samp{C-u} is bound to the function
+In the above example, @key{C-u} is bound to the function
@code{universal-argument} (just as it was in the first example),
-@samp{C-x C-r} is bound to the function @code{re-read-init-file}, and
-@samp{ESC [ 1 1 ~} is bound to insert the text @samp{Function Key 1}.
-The following escape sequences are available when specifying key
-sequences:
+@samp{@key{C-x} @key{C-r}} is bound to the function @code{re-read-init-file},
+and @samp{@key{ESC} @key{[} @key{1} @key{1} @key{~}} is bound to insert
+the text @samp{Function Key 1}.
+
+@end table
+
+The following GNU Emacs style escape sequences are available when
+specifying key sequences:
@table @code
@item @kbd{\C-}
@@ -508,11 +524,40 @@ backslash
@key{'}
@end table
-When entering the text of a macro, single or double quotes should
-be used to indicate a macro definition. Unquoted text
-is assumed to be a function name. Backslash
-will quote any character in the macro text, including @samp{"}
-and @samp{'}.
+In addition to the GNU Emacs style escape sequences, a second
+set of backslash escapes is available:
+
+@table @code
+@item \a
+alert (bell)
+@item \b
+backspace
+@item \d
+delete
+@item \f
+form feed
+@item \n
+newline
+@item \r
+carriage return
+@item \t
+horizontal tab
+@item \v
+vertical tab
+@item \@var{nnn}
+the character whose ASCII code is the octal value @var{nnn}
+(one to three digits)
+@item \x@var{nnn}
+the character whose ASCII code is the hexadecimal value @var{nnn}
+(one to three digits)
+@end table
+
+When entering the text of a macro, single or double quotes must
+be used to indicate a macro definition.
+Unquoted text is assumed to be a function name.
+In the macro body, the backslash escapes described above are expanded.
+Backslash will quote any other character in the macro text,
+including @samp{"} and @samp{'}.
For example, the following binding will make @samp{C-x \}
insert a single @samp{\} into the line:
@example
@@ -520,7 +565,6 @@ insert a single @samp{\} into the line:
@end example
@end table
-@end table
@node Conditional Init Constructs
@subsection Conditional Init Constructs
@@ -528,7 +572,7 @@ insert a single @samp{\} into the line:
Readline implements a facility similar in spirit to the conditional
compilation features of the C preprocessor which allows key
bindings and variable settings to be performed as the result
-of tests. There are three parser directives used.
+of tests. There are four parser directives used.
@table @code
@item $if
@@ -550,8 +594,8 @@ Readline is starting out in @code{emacs} mode.
The @code{term=} form may be used to include terminal-specific
key bindings, perhaps to bind the key sequences output by the
terminal's function keys. The word on the right side of the
-@samp{=} is tested against the full name of the terminal and the
-portion of the terminal name before the first @samp{-}. This
+@samp{=} is tested against both the full name of the terminal and
+the portion of the terminal name before the first @samp{-}. This
allows @code{sun} to match both @code{sun} and @code{sun-cmd},
for instance.
@@ -571,12 +615,19 @@ $endif
@end table
@item $endif
-This command, as you saw in the previous example, terminates an
+This command, as seen in the previous example, terminates an
@code{$if} command.
@item $else
Commands in this branch of the @code{$if} directive are executed if
the test fails.
+
+@item $include
+This directive takes a single filename as an argument and reads commands
+and bindings from that file.
+@example
+$include /etc/inputrc
+@end example
@end table
@node Sample Init File
@@ -594,6 +645,11 @@ binding, variable assignment, and conditional syntax.
# You can re-read the inputrc file with C-x C-r.
# Lines beginning with '#' are comments.
#
+# First, include any systemwide bindings and variable assignments from
+# /etc/Inputrc
+$include /etc/Inputrc
+
+#
# Set various bindings for emacs mode.
set editing-mode emacs
@@ -738,8 +794,9 @@ Refresh the current line. By default, this is unbound.
@ifset BashFeatures
Accept the line regardless of where the cursor is. If this line is
non-empty, add it to the history list according to the setting of
-the @code{HISTCONTROL} variable. If this line was a history
-line, then restore the history line to its original state.
+the @code{HISTCONTROL} and @code{HISTIGNORE} variables.
+If this line was a history line, then restore the history line to its
+original state.
@end ifset
@ifclear BashFeatures
Accept the line regardless of where the cursor is. If this line is
@@ -757,7 +814,8 @@ Move `down' through the history list.
Move to the first line in the history.
@item end-of-history (M->)
-Move to the end of the input history, i.e., the line you are entering.
+Move to the end of the input history, i.e., the line currently
+being entered.
@item reverse-search-history (C-r)
Search backward starting at the current line and moving `up' through
@@ -780,7 +838,7 @@ for a string supplied by the user.
@item history-search-forward ()
Search forward through the history for the string of characters
between the start of the current line and the current cursor
-position (the `point'). This is a non-incremental search. By
+position (the @var{point}). This is a non-incremental search. By
default, this command is unbound.
@item history-search-backward ()
@@ -799,6 +857,8 @@ inserts the @var{n}th word from the end of the previous command.
Insert last argument to the previous command (the last word of the
previous history entry). With an
argument, behave exactly like @code{yank-nth-arg}.
+Successive calls to @code{yank-last-arg} move back through the history
+list, inserting the last argument of each line in turn.
@end ftable
@@ -809,18 +869,21 @@ argument, behave exactly like @code{yank-nth-arg}.
@item delete-char (C-d)
Delete the character under the cursor. If the cursor is at the
beginning of the line, there are no characters in the line, and
-the last character typed was not @kbd{C-d}, then return @code{EOF}.
+the last character typed was not bound to @code{delete-char}, then
+return @code{EOF}.
@item backward-delete-char (Rubout)
-Delete the character behind the cursor. A numeric arg says to kill
-the characters instead of deleting them.
+Delete the character behind the cursor. A numeric argument means
+to kill the characters instead of deleting them.
@item quoted-insert (C-q, C-v)
-Add the next character that you type to the line verbatim. This is
+Add the next character typed to the line verbatim. This is
how to insert key sequences like @key{C-q}, for example.
+@ifclear BashFeatures
@item tab-insert (M-TAB)
Insert a tab character.
+@end ifclear
@item self-insert (a, b, A, 1, !, ...)
Insert yourself.
@@ -831,7 +894,7 @@ the character at the cursor, moving the
cursor forward as well. If the insertion point
is at the end of the line, then this
transposes the last two characters of the line.
-Negative argumentss don't work.
+Negative arguments don't work.
@item transpose-words (M-t)
Drag the word behind the cursor past the word in front of the cursor
@@ -839,15 +902,15 @@ moving the cursor over that word as well.
@item upcase-word (M-u)
Uppercase the current (or following) word. With a negative argument,
-do the previous word, but do not move the cursor.
+uppercase the previous word, but do not move the cursor.
@item downcase-word (M-l)
Lowercase the current (or following) word. With a negative argument,
-do the previous word, but do not move the cursor.
+lowercase the previous word, but do not move the cursor.
@item capitalize-word (M-c)
Capitalize the current (or following) word. With a negative argument,
-do the previous word, but do not move the cursor.
+capitalize the previous word, but do not move the cursor.
@end ftable
@@ -864,7 +927,7 @@ Kill backward to the beginning of the line.
@item unix-line-discard (C-u)
Kill backward from the cursor to the beginning of the current line.
-Save the killed text on the kill-ring.
+The killed text is saved on the kill-ring.
@item kill-whole-line ()
Kill all characters on the current line, no matter where the
@@ -888,19 +951,21 @@ Delete all spaces and tabs around point. By default, this is unbound.
@item kill-region ()
Kill the text between the point and the @emph{mark} (saved
-cursor position. This text is referred to as the @var{region}.
+cursor position). This text is referred to as the @var{region}.
By default, this command is unbound.
@item copy-region-as-kill ()
-Copy the text in the region to the kill buffer, so you can yank it
+Copy the text in the region to the kill buffer, so it can be yanked
right away. By default, this command is unbound.
@item copy-backward-word ()
Copy the word before point to the kill buffer.
+The word boundaries are the same as @code{backward-word}.
By default, this command is unbound.
@item copy-forward-word ()
Copy the word following point to the kill buffer.
+The word boundaries are the same as @code{forward-word}.
By default, this command is unbound.
@item yank (C-y)
@@ -943,8 +1008,8 @@ By default, this is not bound to a key.
Attempt to do completion on the text before the cursor. This is
application-specific. Generally, if you are typing a filename
argument, you can do filename completion; if you are typing a command,
-you can do command completion, if you are typing in a symbol to GDB, you
-can do symbol name completion, if you are typing in a variable to Bash,
+you can do command completion; if you are typing in a symbol to GDB, you
+can do symbol name completion; if you are typing in a variable to Bash,
you can do variable name completion, and so on.
@ifset BashFeatures
Bash attempts completion treating the text as a variable (if the
@@ -961,6 +1026,19 @@ List the possible completions of the text before the cursor.
Insert all completions of the text before point that would have
been generated by @code{possible-completions}.
+@item menu-complete ()
+Similar to @code{complete}, but replaces the word to be completed
+with a single match from the list of possible completions.
+Repeated execution of @code{menu-complete} steps through the list
+of possible completions, inserting each match in turn.
+At the end of the list of completions, the bell is rung and the
+original text is restored.
+An argument of @var{n} moves @var{n} positions forward in the list
+of matches; a negative argument may be used to move backward
+through the list.
+This command is intended to be bound to @code{TAB}, but is unbound
+by default.
+
@ifset BashFeatures
@item complete-filename (M-/)
Attempt filename completion on the text before point.
@@ -997,7 +1075,7 @@ treating it as a hostname.
Attempt completion on the text before point, treating
it as a command name. Command completion attempts to
match the text against aliases, reserved words, shell
-functions, builtins, and finally executable filenames,
+functions, shell builtins, and finally executable filenames,
in that order.
@item possible-command-completions (C-x !)
@@ -1052,7 +1130,7 @@ If the metafied character @var{x} is lowercase, run the command
that is bound to the corresponding uppercase character.
@item prefix-meta (ESC)
-Make the next character that you type be metafied. This is for people
+Make the next character typed be metafied. This is for keyboards
without a meta key. Typing @samp{ESC f} is equivalent to typing
@samp{M-f}.
@@ -1060,7 +1138,7 @@ without a meta key. Typing @samp{ESC f} is equivalent to typing
Incremental undo, separately remembered for each line.
@item revert-line (M-r)
-Undo all changes made to this line. This is like typing the @code{undo}
+Undo all changes made to this line. This is like executing the @code{undo}
command enough times to get back to the beginning.
@item tilde-expand (M-~)
@@ -1093,18 +1171,18 @@ This makes the current line a shell comment.
@item dump-functions ()
Print all of the functions and their key bindings to the
-readline output stream. If a numeric argument is supplied,
+Readline output stream. If a numeric argument is supplied,
the output is formatted in such a way that it can be made part
of an @var{inputrc} file. This command is unbound by default.
@item dump-variables ()
Print all of the settable variables and their values to the
-readline output stream. If a numeric argument is supplied,
+Readline output stream. If a numeric argument is supplied,
the output is formatted in such a way that it can be made part
of an @var{inputrc} file. This command is unbound by default.
@item dump-macros ()
-Print all of the readline key sequences bound to macros and the
+Print all of the Readline key sequences bound to macros and the
strings they ouput. If a numeric argument is supplied,
the output is formatted in such a way that it can be made part
of an @var{inputrc} file. This command is unbound by default.
@@ -1116,24 +1194,27 @@ and the list of matching file names is inserted, replacing the word.
@item glob-list-expansions (C-x g)
The list of expansions that would have been generated by
-@code{glob-expand-word}
-is inserted into the line, replacing the word before point.
+@code{glob-expand-word} is displayed, and the line is redrawn.
@item display-shell-version (C-x C-v)
Display version information about the current instance of Bash.
@item shell-expand-line (M-C-e)
-Expand the line the way the shell does when it reads it. This
-performs alias and history expansion as well as all of the shell
-word expansions.
+Expand the line as the shell does.
+This performs alias and history expansion as well as all of the shell
+word expansions (@pxref{Shell Expansions}).
@item history-expand-line (M-^)
Perform history expansion on the current line.
-@item alias-expand-line
+@item magic-space ()
+Perform history expansion on the current line and insert a space
+(@pxref{History Interaction}).
+
+@item alias-expand-line ()
Perform alias expansion on the current line (@pxref{Aliases}).
-@item history-and-alias-expand-line
+@item history-and-alias-expand-line ()
Perform history and alias expansion on the current line.
@item insert-last-argument (M-., M-_)
diff --git a/lib/readline/examples/rltest.c b/lib/readline/examples/rltest.c
index ff3ad5c..453f8ec 100644
--- a/lib/readline/examples/rltest.c
+++ b/lib/readline/examples/rltest.c
@@ -19,12 +19,16 @@
#include "readline.h"
#include "history.h"
+extern HIST_ENTRY **history_list ();
+
main ()
{
- HIST_ENTRY **history_list ();
- char *temp = (char *)NULL;
- char *prompt = "readline$ ";
- int done = 0;
+ char *temp, *prompt;
+ int done;
+
+ temp = (char *)NULL;
+ prompt = "readline$ ";
+ done = 0;
while (!done)
{
@@ -47,18 +51,17 @@ main ()
if (strcmp (temp, "list") == 0)
{
- HIST_ENTRY **list = history_list ();
+ HIST_ENTRY **list;
register int i;
+
+ list = history_list ();
if (list)
{
for (i = 0; list[i]; i++)
- {
- fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
- free (list[i]->line);
- }
- free (list);
+ fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
}
}
free (temp);
}
+ exit (0);
}
diff --git a/lib/readline/funmap.c b/lib/readline/funmap.c
index 702fabd..3946e0f 100644
--- a/lib/readline/funmap.c
+++ b/lib/readline/funmap.c
@@ -94,11 +94,15 @@ static FUNMAP default_funmap[] = {
{ "kill-line", rl_kill_line },
{ "kill-region", rl_kill_region },
{ "kill-word", rl_kill_word },
+ { "menu-complete", rl_menu_complete },
{ "next-history", rl_get_next_history },
{ "non-incremental-forward-search-history", rl_noninc_forward_search },
{ "non-incremental-reverse-search-history", rl_noninc_reverse_search },
{ "non-incremental-forward-search-history-again", rl_noninc_forward_search_again },
{ "non-incremental-reverse-search-history-again", rl_noninc_reverse_search_again },
+#ifdef __CYGWIN32__
+ { "paste-from-clipboard", rl_paste_from_clipboard },
+#endif
{ "possible-completions", rl_possible_completions },
{ "previous-history", rl_get_previous_history },
{ "quoted-insert", rl_quoted_insert },
diff --git a/lib/readline/histexpand.c b/lib/readline/histexpand.c
index de71d78..0dc179a 100644
--- a/lib/readline/histexpand.c
+++ b/lib/readline/histexpand.c
@@ -35,6 +35,9 @@
#endif /* HAVE_STDLIB_H */
#if defined (HAVE_UNISTD_H)
+# ifndef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
#endif
@@ -47,6 +50,9 @@
#include "history.h"
#include "histlib.h"
+#define HISTORY_WORD_DELIMITERS " \t\n;&()|<>"
+#define HISTORY_QUOTE_CHARACTERS "\"'`"
+
static char error_pointer;
static char *subst_lhs;
@@ -823,8 +829,8 @@ history_expand (hstring, output)
only_printing = modified = 0;
l = strlen (hstring);
- /* Grovel the string. Only backslash can quote the history escape
- character. We also handle arg specifiers. */
+ /* 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. */
@@ -852,7 +858,18 @@ history_expand (hstring, output)
for (i = 0; string[i]; i++)
{
cc = string[i + 1];
- if (string[i] == history_expansion_char)
+ /* The history_comment_char, if set, appearing that 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;
@@ -867,6 +884,8 @@ history_expand (hstring, output)
else
break;
}
+ /* XXX - at some point, might want to extend this to handle
+ double quotes as well. */
else if (history_quotes_inhibit_expansion && string[i] == '\'')
{
/* If this is bash, single quotes inhibit history expansion. */
@@ -904,6 +923,8 @@ history_expand (hstring, output)
if (tchar == history_expansion_char)
tchar = -3;
+ else if (tchar == history_comment_char)
+ tchar = -2;
switch (tchar)
{
@@ -939,6 +960,19 @@ history_expand (hstring, output)
break;
}
+ case -2: /* history_comment_char */
+ if (i == 0 || member (string[i - 1], HISTORY_WORD_DELIMITERS))
+ {
+ temp = 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];
@@ -1238,7 +1272,7 @@ history_tokenize_internal (string, wind, indp)
/* Get word from string + i; */
- if (member (string[i], "\"'`"))
+ if (member (string[i], HISTORY_QUOTE_CHARACTERS))
delimiter = string[i++];
for (; string[i]; i++)
@@ -1262,10 +1296,10 @@ history_tokenize_internal (string, wind, indp)
continue;
}
- if (!delimiter && (member (string[i], " \t\n;&()|<>")))
+ if (!delimiter && (member (string[i], HISTORY_WORD_DELIMITERS)))
break;
- if (!delimiter && member (string[i], "\"'`"))
+ if (!delimiter && member (string[i], HISTORY_QUOTE_CHARACTERS))
delimiter = string[i];
}
diff --git a/lib/readline/histfile.c b/lib/readline/histfile.c
index 9f96f1c..81dda57 100644
--- a/lib/readline/histfile.c
+++ b/lib/readline/histfile.c
@@ -32,7 +32,9 @@
#include <stdio.h>
#include <sys/types.h>
-#include <sys/file.h>
+#ifndef _MINIX
+# include <sys/file.h>
+#endif
#include <sys/stat.h>
#include <fcntl.h>
@@ -129,19 +131,31 @@ read_history_range (filename, from, to)
int from, to;
{
register int line_start, line_end;
- char *input, *buffer = (char *)NULL;
+ char *input, *buffer;
int file, current_line;
struct stat finfo;
+ size_t file_size;
+ buffer = (char *)NULL;
input = history_filename (filename);
file = open (input, O_RDONLY|O_BINARY, 0666);
if ((file < 0) || (fstat (file, &finfo) == -1))
goto error_and_exit;
- buffer = xmalloc ((int)finfo.st_size + 1);
+ file_size = (size_t)finfo.st_size;
+
+ /* check for overflow on very large files */
+ if (file_size != finfo.st_size || file_size + 1 < file_size)
+ {
+#if defined (EFBIG)
+ errno = EFBIG;
+#endif
+ goto error_and_exit;
+ }
- if (read (file, buffer, finfo.st_size) != finfo.st_size)
+ buffer = xmalloc (file_size + 1);
+ if (read (file, buffer, file_size) != file_size)
{
error_and_exit:
if (file >= 0)
@@ -157,15 +171,15 @@ read_history_range (filename, from, to)
/* Set TO to larger than end of file if negative. */
if (to < 0)
- to = finfo.st_size;
+ to = file_size;
/* Start at beginning of file, work to end. */
line_start = line_end = current_line = 0;
/* Skip lines until we are at FROM. */
- while (line_start < finfo.st_size && current_line < from)
+ while (line_start < file_size && current_line < from)
{
- for (line_end = line_start; line_end < finfo.st_size; line_end++)
+ for (line_end = line_start; line_end < file_size; line_end++)
if (buffer[line_end] == '\n')
{
current_line++;
@@ -176,7 +190,7 @@ read_history_range (filename, from, to)
}
/* If there are lines left to gobble, then gobble them now. */
- for (line_end = line_start; line_end < finfo.st_size; line_end++)
+ for (line_end = line_start; line_end < file_size; line_end++)
if (buffer[line_end] == '\n')
{
buffer[line_end] = '\0';
@@ -209,6 +223,7 @@ history_truncate_file (fname, lines)
int file, chars_read;
char *buffer, *filename;
struct stat finfo;
+ size_t file_size;
buffer = (char *)NULL;
filename = history_filename (fname);
@@ -217,8 +232,20 @@ history_truncate_file (fname, lines)
if (file == -1 || fstat (file, &finfo) == -1)
goto truncate_exit;
- buffer = xmalloc ((int)finfo.st_size + 1);
- chars_read = read (file, buffer, finfo.st_size);
+ file_size = (size_t)finfo.st_size;
+
+ /* check for overflow on very large files */
+ if (file_size != finfo.st_size || file_size + 1 < file_size)
+ {
+ close (file);
+#if defined (EFBIG)
+ errno = EFBIG;
+#endif
+ goto truncate_exit;
+ }
+
+ buffer = xmalloc (file_size + 1);
+ chars_read = read (file, buffer, file_size);
close (file);
if (chars_read <= 0)
@@ -248,7 +275,7 @@ history_truncate_file (fname, lines)
truncate to. */
if (i && ((file = open (filename, O_WRONLY|O_TRUNC|O_BINARY, 0600)) != -1))
{
- write (file, buffer + i, finfo.st_size - i);
+ write (file, buffer + i, file_size - i);
close (file);
}
diff --git a/lib/readline/history.c b/lib/readline/history.c
index fb9d68e..24c5a49 100644
--- a/lib/readline/history.c
+++ b/lib/readline/history.c
@@ -38,6 +38,9 @@
#endif /* HAVE_STDLIB_H */
#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
#endif
diff --git a/lib/readline/histsearch.c b/lib/readline/histsearch.c
index a72a68b..7e98e95 100644
--- a/lib/readline/histsearch.c
+++ b/lib/readline/histsearch.c
@@ -33,6 +33,9 @@
# include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
#endif
#if defined (HAVE_STRING_H)
diff --git a/lib/readline/isearch.c b/lib/readline/isearch.c
index ce2e58b..7decf95 100644
--- a/lib/readline/isearch.c
+++ b/lib/readline/isearch.c
@@ -262,7 +262,7 @@ rl_search_history (direction, invoking_key)
break;
}
- if (c >= 0 && (CTRL_CHAR (c) || META_CHAR (c) || c == RUBOUT) && c != CTRL ('g'))
+ if (c >= 0 && (CTRL_CHAR (c) || META_CHAR (c) || c == RUBOUT) && c != CTRL ('G'))
{
rl_execute_next (c);
break;
@@ -298,6 +298,21 @@ rl_search_history (direction, invoking_key)
free (lines);
return 0;
+#if 0
+ /* delete character from search string. */
+ case -3:
+ if (search_string_index == 0)
+ ding ();
+ else
+ {
+ search_string[--search_string_index] = '\0';
+ /* This is tricky. To do this right, we need to keep a
+ stack of search positions for the current search, with
+ sentinels marking the beginning and end. */
+ }
+ break;
+#endif
+
default:
/* Add character to search string and continue search. */
if (search_string_index + 2 >= search_string_size)
diff --git a/lib/readline/keymaps.h b/lib/readline/keymaps.h
index f8d0e2e..f6143f8 100644
--- a/lib/readline/keymaps.h
+++ b/lib/readline/keymaps.h
@@ -39,9 +39,9 @@ typedef char **CPPFunction ();
/* A keymap contains one entry for each key in the ASCII set.
Each entry consists of a type and a pointer.
- POINTER is the address of a function to run, or the
+ FUNCTION is the address of a function to run, or the
address of a keymap to indirect through.
- TYPE says which kind of thing POINTER is. */
+ TYPE says which kind of thing FUNCTION is. */
typedef struct _keymap_entry {
char type;
Function *function;
diff --git a/lib/readline/kill.c b/lib/readline/kill.c
index 352f37d..a150e3c 100644
--- a/lib/readline/kill.c
+++ b/lib/readline/kill.c
@@ -495,17 +495,32 @@ rl_yank_pop (count, key)
}
}
-/* Yank the COUNTth argument from the previous history line. */
-int
-rl_yank_nth_arg (count, ignore)
- int count, ignore;
+/* Yank the COUNTh argument from the previous history line, skipping
+ HISTORY_SKIP lines before looking for the `previous line'. */
+static int
+rl_yank_nth_arg_internal (count, ignore, history_skip)
+ int count, ignore, history_skip;
{
register HIST_ENTRY *entry;
char *arg;
+ int i;
+
+ if (history_skip)
+ {
+ for (i = 0; i < history_skip; i++)
+ entry = previous_history ();
+ }
entry = previous_history ();
if (entry)
- next_history ();
+ {
+ if (history_skip)
+ {
+ for (i = 0; i < history_skip; i++)
+ next_history ();
+ }
+ next_history ();
+ }
else
{
ding ();
@@ -538,6 +553,14 @@ rl_yank_nth_arg (count, ignore)
return 0;
}
+/* Yank the COUNTth argument from the previous history line. */
+int
+rl_yank_nth_arg (count, ignore)
+ int count, ignore;
+{
+ return (rl_yank_nth_arg_internal (count, ignore, 0));
+}
+
/* Yank the last argument from the previous history line. This `knows'
how rl_yank_nth_arg treats a count of `$'. With an argument, this
behaves the same as rl_yank_nth_arg. */
@@ -545,8 +568,67 @@ int
rl_yank_last_arg (count, key)
int count, key;
{
- if (rl_explicit_arg)
- return (rl_yank_nth_arg (count, key));
+ static int history_skip = 0;
+ static int explicit_arg_p = 0;
+ static int count_passed = 1;
+ static int direction = 1;
+
+ if (rl_last_func != rl_yank_last_arg)
+ {
+ history_skip = 0;
+ explicit_arg_p = rl_explicit_arg;
+ count_passed = count;
+ direction = 1;
+ }
+ else
+ {
+ rl_do_undo ();
+ if (count < 1)
+ direction = -direction;
+ history_skip += direction;
+ if (history_skip < 0)
+ history_skip = 0;
+ count_passed = count;
+ }
+
+ if (explicit_arg_p)
+ return (rl_yank_nth_arg_internal (count, key, history_skip));
else
- return (rl_yank_nth_arg ('$', key));
+ return (rl_yank_nth_arg_internal ('$', key, history_skip));
+}
+
+/* A special paste command for users of Cygnus's cygwin32. */
+#if defined (__CYGWIN32__)
+#include <windows.h>
+
+int
+rl_paste_from_clipboard (count, key)
+ int count, key;
+{
+ char *data, *ptr;
+ int len;
+
+ if (OpenClipboard (NULL) == 0)
+ return (0);
+
+ data = (char *)GetClipboardData (CF_TEXT);
+ if (data)
+ {
+ ptr = strchr (data, '\r');
+ if (ptr)
+ {
+ len = ptr - data;
+ ptr = xmalloc (len + 1);
+ ptr[len] = '\0';
+ strncpy (ptr, data, len);
+ }
+ else
+ ptr = data;
+ rl_insert_text (ptr);
+ if (ptr != data)
+ free (ptr);
+ CloseClipboard ();
+ }
+ return (0);
}
+#endif /* __CYGWIN32__ */
diff --git a/lib/readline/nls.c b/lib/readline/nls.c
index 7a00a5f..f2d413d 100644
--- a/lib/readline/nls.c
+++ b/lib/readline/nls.c
@@ -69,7 +69,8 @@ static char *legal_lang_values[] =
"iso88598",
"iso88599",
"iso885910",
- "koi8r",
+ "koi8r",
+ "koi8-r",
0
};
diff --git a/lib/readline/parens.c b/lib/readline/parens.c
index 50683f9..2c7251f 100644
--- a/lib/readline/parens.c
+++ b/lib/readline/parens.c
@@ -101,7 +101,7 @@ rl_insert_close (count, invoking_key)
FD_ZERO (&readfds);
FD_SET (fileno (rl_instream), &readfds);
- timer.tv_sec = 1;
+ timer.tv_sec = 0;
timer.tv_usec = 500;
orig_point = rl_point;
diff --git a/lib/readline/readline.c b/lib/readline/readline.c
index dcd8f81..8ff6e98 100644
--- a/lib/readline/readline.c
+++ b/lib/readline/readline.c
@@ -64,7 +64,7 @@
#include "history.h"
#ifndef RL_LIBRARY_VERSION
-# define RL_LIBRARY_VERSION "2.1-bash"
+# define RL_LIBRARY_VERSION "2.2-bash"
#endif
/* Evaluates its arguments multiple times. */
@@ -78,7 +78,11 @@
/* Variables and functions imported from terminal.c */
extern int _rl_init_terminal_io ();
extern void _rl_enable_meta_key ();
+#ifdef _MINIX
+extern void _rl_output_character_function ();
+#else
extern int _rl_output_character_function ();
+#endif
extern void _rl_get_screen_size ();
extern int _rl_enable_meta;
@@ -1027,6 +1031,18 @@ _rl_fix_point (fix_mark_too)
}
#undef _RL_FIX_POINT
+void
+_rl_replace_text (text, start, end)
+ char *text;
+ int start, end;
+{
+ rl_begin_undo_group ();
+ rl_delete_text (start, end + 1);
+ rl_point = start;
+ rl_insert_text (text);
+ rl_end_undo_group ();
+}
+
/* **************************************************************** */
/* */
/* Readline character functions */
diff --git a/lib/readline/readline.h b/lib/readline/readline.h
index 99ec055..280ec32 100644
--- a/lib/readline/readline.h
+++ b/lib/readline/readline.h
@@ -73,6 +73,7 @@ extern int
rl_forward_word (), rl_tab_insert (), rl_yank_pop (), rl_yank_nth_arg (),
rl_backward_kill_word (), rl_backward_kill_line (), rl_transpose_words (),
rl_complete (), rl_possible_completions (), rl_insert_completions (),
+ rl_menu_complete (),
rl_do_lowercase_version (), rl_kill_full_line (),
rl_digit_argument (), rl_universal_argument (), rl_abort (),
rl_undo_command (), rl_revert_line (), rl_beginning_of_history (),
@@ -93,13 +94,19 @@ extern void rl_callback_handler_install ();
extern void rl_callback_read_char ();
extern void rl_callback_handler_remove ();
+/* Not available unless __CYGWIN32__ is defined. */
+#ifdef __CYGWIN32__
+extern int rl_paste_from_clipboard ();
+#endif
+
/* These are *both* defined even when VI_MODE is not. */
extern int rl_vi_editing_mode (), rl_emacs_editing_mode ();
/* Non incremental history searching. */
-extern int
- rl_noninc_forward_search (), rl_noninc_reverse_search (),
- rl_noninc_forward_search_again (), rl_noninc_reverse_search_again ();
+extern int rl_noninc_forward_search ();
+extern int rl_noninc_reverse_search ();
+extern int rl_noninc_forward_search_again ();
+extern int rl_noninc_reverse_search_again ();
/* Things for vi mode. Not available unless readline is compiled -DVI_MODE. */
extern int rl_vi_check ();
@@ -153,6 +160,7 @@ extern char *rl_get_keymap_name ();
extern int rl_bind_key (), rl_bind_key_in_map ();
extern int rl_unbind_key (), rl_unbind_key_in_map ();
+extern int rl_unbind_function_in_map (), rl_unbind_command_in_map ();
extern int rl_set_key ();
extern int rl_generic_bind ();
extern int rl_parse_and_bind ();
diff --git a/lib/readline/rltty.h b/lib/readline/rltty.h
index 3e13704..fe78346 100644
--- a/lib/readline/rltty.h
+++ b/lib/readline/rltty.h
@@ -42,17 +42,7 @@
# include <sgtty.h>
#endif
-/* Stuff for `struct winsize' on various systems. */
-#if defined (HAVE_SYS_STREAM_H)
-# include <sys/stream.h>
-#endif /* HAVE_SYS_STREAM_H */
-#if defined (HAVE_SYS_PTEM_H)
-# include <sys/ptem.h>
-# define _IO_PTEM_H /* work around SVR4.2 1.1.4 bug */
-#endif /* HAVE_SYS_PTEM_H */
-#if defined (HAVE_SYS_PTE_H)
-# include <sys/pte.h>
-#endif /* HAVE_SYS_PTE_H */
+#include "rlwinsize.h"
/* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
it is not already defined. It is used both to determine if a
diff --git a/lib/readline/rlwinsize.h b/lib/readline/rlwinsize.h
new file mode 100644
index 0000000..92b3de1
--- /dev/null
+++ b/lib/readline/rlwinsize.h
@@ -0,0 +1,58 @@
+/* rlwinsize.h -- an attempt to isolate some of the system-specific defines
+ for `struct winsize' and TIOCGWINSZ. */
+
+/* Copyright (C) 1997 Free Software Foundation, Inc.
+
+ This file contains the Readline Library (the Library), a set of
+ routines for providing Emacs style line input to programs that ask
+ for it.
+
+ 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 1, 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,
+ 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#if !defined (_RLWINSIZE_H_)
+#define _RLWINSIZE_H_
+
+#if defined (HAVE_CONFIG_H)
+# include "config.h"
+#endif
+
+/* Try to find the definitions of `struct winsize' and TIOGCWINSZ */
+
+#if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
+# include <sys/ioctl.h>
+#endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
+
+#if defined (STRUCT_WINSIZE_IN_TERMIOS) && !defined (STRUCT_WINSIZE_IN_SYS_IOCTL)
+# include <termios.h>
+#endif /* STRUCT_WINSIZE_IN_TERMIOS && !STRUCT_WINSIZE_IN_SYS_IOCTL */
+
+/* Not in either of the standard places, look around. */
+#if !defined (STRUCT_WINSIZE_IN_TERMIOS) && !defined (STRUCT_WINSIZE_IN_SYS_IOCTL)
+# if defined (HAVE_SYS_STREAM_H)
+# include <sys/stream.h>
+# endif /* HAVE_SYS_STREAM_H */
+# if defined (HAVE_SYS_PTEM_H) /* SVR4.2, at least, has it here */
+# include <sys/ptem.h>
+# define _IO_PTEM_H /* work around SVR4.2 1.1.4 bug */
+# endif /* HAVE_SYS_PTEM_H */
+# if defined (HAVE_SYS_PTE_H) /* ??? */
+# include <sys/pte.h>
+# endif /* HAVE_SYS_PTE_H */
+#endif /* !STRUCT_WINSIZE_IN_TERMIOS && !STRUCT_WINSIZE_IN_SYS_IOCTL */
+
+#endif /* _RL_WINSIZE_H */
+
+
diff --git a/lib/readline/shell.c b/lib/readline/shell.c
index eb99c72..553f3c1 100644
--- a/lib/readline/shell.c
+++ b/lib/readline/shell.c
@@ -27,6 +27,9 @@
#endif
#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
@@ -36,6 +39,12 @@
# include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
+#if defined (HAVE_STRING_H)
+# include <string.h>
+#else
+# include <strings.h>
+#endif /* !HAVE_STRING_H */
+
extern char *xmalloc (), *xrealloc ();
#if !defined (SHELL)
diff --git a/lib/readline/terminal.c b/lib/readline/terminal.c
index 5a8df89..c286696 100644
--- a/lib/readline/terminal.c
+++ b/lib/readline/terminal.c
@@ -434,13 +434,21 @@ rl_reset_terminal (terminal_name)
}
/* A function for the use of tputs () */
+#ifdef _MINIX
+void
+_rl_output_character_function (c)
+ int c;
+{
+ putc (c, _rl_out_stream);
+}
+#else /* !_MINIX */
int
_rl_output_character_function (c)
int c;
{
return putc (c, _rl_out_stream);
}
-
+#endif /* !_MINIX */
/* Write COUNT characters from STRING to the output stream. */
void
_rl_output_some_chars (string, count)
@@ -519,18 +527,11 @@ ding ()
/* */
/* **************************************************************** */
-static int
-outchar (c)
- int c;
-{
- return putc (c, rl_outstream);
-}
-
void
_rl_enable_meta_key ()
{
if (term_has_meta && term_mm)
- tputs (term_mm, 1, outchar);
+ tputs (term_mm, 1, _rl_output_character_function);
}
void
@@ -538,7 +539,7 @@ _rl_control_keypad (on)
int on;
{
if (on && term_ks)
- tputs (term_ks, 1, outchar);
+ tputs (term_ks, 1, _rl_output_character_function);
else if (!on && term_ke)
- tputs (term_ke, 1, outchar);
+ tputs (term_ke, 1, _rl_output_character_function);
}
diff --git a/lib/readline/tilde.c b/lib/readline/tilde.c
index fe263a2..3741f97 100644
--- a/lib/readline/tilde.c
+++ b/lib/readline/tilde.c
@@ -24,6 +24,9 @@
#endif
#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
#endif
diff --git a/lib/readline/util.c b/lib/readline/util.c
index d96b29e..fde012e 100644
--- a/lib/readline/util.c
+++ b/lib/readline/util.c
@@ -166,6 +166,58 @@ rl_extend_line_buffer (len)
_rl_set_the_line ();
}
+
+/* A function for simple tilde expansion. */
+int
+rl_tilde_expand (ignore, key)
+ int ignore, key;
+{
+ register int start, end;
+ char *homedir, *temp;
+ int len;
+
+ end = rl_point;
+ start = end - 1;
+
+ if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
+ {
+ homedir = tilde_expand ("~");
+ _rl_replace_text (homedir, start, end);
+ return (0);
+ }
+ else if (rl_line_buffer[start] != '~')
+ {
+ for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
+ ;
+ start++;
+ }
+
+ end = start;
+ do
+ end++;
+ while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
+
+ if (whitespace (rl_line_buffer[end]) || end >= rl_end)
+ end--;
+
+ /* If the first character of the current word is a tilde, perform
+ tilde expansion and insert the result. If not a tilde, do
+ nothing. */
+ if (rl_line_buffer[start] == '~')
+ {
+ len = end - start + 1;
+ temp = xmalloc (len + 1);
+ strncpy (temp, rl_line_buffer + start, len);
+ temp[len] = '\0';
+ homedir = tilde_expand (temp);
+ free (temp);
+
+ _rl_replace_text (homedir, start, end);
+ }
+
+ return (0);
+}
+
/* **************************************************************** */
/* */
/* String Utility Functions */
@@ -300,3 +352,13 @@ _rl_digit_value (c)
{
return (isdigit (c) ? c - '0' : c);
}
+
+/* Backwards compatibility, now that savestring has been removed from
+ all `public' readline header files. */
+#undef _rl_savestring
+char *
+_rl_savestring (s)
+ char *s;
+{
+ return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
+}
diff --git a/lib/readline/vi_mode.c b/lib/readline/vi_mode.c
index c730296..2f62ec3 100644
--- a/lib/readline/vi_mode.c
+++ b/lib/readline/vi_mode.c
@@ -615,6 +615,13 @@ _rl_vi_save_insert (up)
{
int len, start, end;
+ if (up == 0)
+ {
+ if (vi_insert_buffer_size >= 1)
+ vi_insert_buffer[0] = '\0';
+ return;
+ }
+
start = up->start;
end = up->end;
len = end - start + 1;
diff --git a/lib/sh/Makefile.in b/lib/sh/Makefile.in
new file mode 100644
index 0000000..d98f9c5
--- /dev/null
+++ b/lib/sh/Makefile.in
@@ -0,0 +1,164 @@
+#
+# Makefile for the Bash library
+#
+
+srcdir = @srcdir@
+VPATH = .:@srcdir@
+topdir = @top_srcdir@
+BUILD_DIR = @BUILD_DIR@
+
+POSIX_INC = ${topdir}/lib/posixheaders
+
+INSTALL = @INSTALL@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_DATA = @INSTALL_DATA@
+
+CC = @CC@
+RANLIB = @RANLIB@
+AR = @AR@
+RM = rm -f
+CP = cp
+MV = mv
+
+CFLAGS = @CFLAGS@
+LOCAL_CFLAGS = @LOCAL_CFLAGS@
+CPPFLAGS = @CPPFLAGS@
+LDFLAGS = @LDFLAGS@ @LOCAL_LDFLAGS@
+
+PROFILE_FLAGS = @PROFILE_FLAGS@
+
+DEFS = @DEFS@
+LOCAL_DEFS = @LOCAL_DEFS@
+
+INCLUDES = -I. -I../.. -I$(topdir) -I$(topdir)/lib -I$(POSIX_INC) -I$(srcdir)
+
+CCFLAGS = ${PROFILE_FLAGS} ${INCLUDES} $(DEFS) $(LOCAL_DEFS) $(LOCAL_CFLAGS) \
+ $(CFLAGS) $(CPPFLAGS)
+
+.c.o:
+ $(CC) -c $(CCFLAGS) $<
+
+# The name of the library target.
+LIBRARY_NAME = libsh.a
+
+# The C code source files for this library.
+CSOURCES = clktck.c getcwd.c getenv.c oslib.c setlinebuf.c \
+ strcasecmp.c strerror.c strtod.c strtol.c strtoul.c \
+ vprint.c itos.c
+
+# The header files for this library.
+HSOURCES =
+
+# The object files contained in $(LIBRARY_NAME)
+OBJECTS = clktck.o getcwd.o getenv.o oslib.o setlinebuf.o \
+ strcasecmp.o strerror.o strtod.o strtol.o strtoul.o \
+ vprint.o itos.o
+
+SUPPORT = Makefile
+
+all: $(LIBRARY_NAME)
+
+$(LIBRARY_NAME): $(OBJECTS)
+ $(RM) $@
+ $(AR) cr $@ $(OBJECTS)
+ -test -n "$(RANLIB)" && $(RANLIB) $@
+
+force:
+
+# The rule for 'includes' is written funny so that the if statement
+# always returns TRUE unless there really was an error installing the
+# include files.
+install:
+
+clean:
+ $(RM) $(OBJECTS) $(LIBRARY_NAME)
+
+realclean distclean maintainer-clean: clean
+ $(RM) Makefile
+
+mostlyclean: clean
+
+# Dependencies
+
+# rules for losing makes, like SunOS
+clktck.o: clktck.c
+getcwd.o: getcwd.c
+getenv.o: getenv.c
+itos.o: itos.c
+oslib.o: oslib.c
+setlinebuf.o: setlinebuf.c
+strcasecmp.o: strcasecmp.c
+strerror.o: strerror.c
+strtod.o: strtod.c
+strtol.o: strtol.c
+strtoul.o: strtoul.c
+vprint.o: vprint.c
+
+# all files in the library depend on config.h
+clktck.o: ${BUILD_DIR}/config.h
+getcwd.o: ${BUILD_DIR}/config.h
+getenv.o: ${BUILD_DIR}/config.h
+itos.o: ${BUILD_DIR}/config.h
+oslib.o: ${BUILD_DIR}/config.h
+setlinebuf.o: ${BUILD_DIR}/config.h
+strcasecmp.o: ${BUILD_DIR}/config.h
+strerror.o: ${BUILD_DIR}/config.h
+strtod.o: ${BUILD_DIR}/config.h
+strtol.o: ${BUILD_DIR}/config.h
+strtoul.o: ${BUILD_DIR}/config.h
+vprint.o: ${BUILD_DIR}/config.h
+
+clktck.o: ${topdir}/bashtypes.h
+
+getcwd.o: ${topdir}/bashtypes.h ${topdir}/bashansi.h ${topdir}/maxpath.h
+getcwd.o: ${POSIX_INC}/posixstat.h ${POSIX_INC}/posixdir.h
+getcwd.o: ${POSIX_INC}/memalloc.h ${POSIX_INC}/ansi_stdlib.h
+
+getenv.o: ${topdir}/bashansi.h ${topdir}/ansi_stdlib.h
+getenv.o: ${topdir}/shell.h ${topdir}/bashjmp.h ${topdir}/posixjmp.h
+getenv.o: ${topdir}/command.h ${topdir}/stdc.h ${topdir}/error.h
+getenv.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h
+getenv.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h
+getenv.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h
+getenv.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h
+getenv.o: ${topdir}/pathnames.h ${topdir}/externs.h
+
+itos.o: ${topdir}/bashansi.h ${topdir}/ansi_stdlib.h
+itos.o: ${topdir}/shell.h ${topdir}/bashjmp.h ${topdir}/posixjmp.h
+itos.o: ${topdir}/command.h ${topdir}/stdc.h ${topdir}/error.h
+itos.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h
+itos.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h
+itos.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h
+itos.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h
+itos.o: ${topdir}/pathnames.h ${topdir}/externs.h
+
+oslib.o: ${topdir}/bashtypes.h ${topdir}/bashansi.h ${topdir}/maxpath.h
+oslib.o: ${topdir}/shell.h ${topdir}/bashjmp.h ${topdir}/posixjmp.h
+oslib.o: ${topdir}/command.h ${topdir}/stdc.h ${topdir}/error.h
+oslib.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h
+oslib.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h
+oslib.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h
+oslib.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h
+oslib.o: ${topdir}/pathnames.h ${topdir}/externs.h
+oslib.o: ${POSIX_INC}/posixstat.h ${POSIX_INC}/filecntl.h
+oslib.o: ${POSIX_INC}/ansi_stdlib.h
+
+strcasecmp.o: ${topdir}/stdc.h ${topdir}/bashansi.h ${topdir}/ansi_stdlib.h
+
+strerror.o: ${topdir}/bashtypes.h
+strerror.o: ${topdir}/shell.h ${topdir}/bashjmp.h ${topdir}/posixjmp.h
+strerror.o: ${topdir}/command.h ${topdir}/stdc.h ${topdir}/error.h
+strerror.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h
+strerror.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h
+strerror.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h
+strerror.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h
+strerror.o: ${topdir}/pathnames.h ${topdir}/externs.h
+
+strtod.o: ${topdir}/bashansi.h
+strtod.o: ${POSIX_INC}/ansi_stdlib.h
+
+strtol.o: ${topdir}/bashansi.h
+strtol.o: ${POSIX_INC}/ansi_stdlib.h
+
+strtoul.o: ${topdir}/bashansi.h
+strtoul.o: ${POSIX_INC}/ansi_stdlib.h
diff --git a/lib/sh/clktck.c b/lib/sh/clktck.c
new file mode 100644
index 0000000..b0b674e
--- /dev/null
+++ b/lib/sh/clktck.c
@@ -0,0 +1,55 @@
+/* clktck.c - get the value of CLK_TCK. */
+
+/* Copyright (C) 1997 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
+
+#include <config.h>
+
+#include <bashtypes.h>
+#include <sys/param.h>
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+#if !defined (HAVE_SYSCONF) || !defined (_SC_CLK_TCK)
+# if !defined (CLK_TCK)
+# if defined (HZ)
+# define CLK_TCK HZ
+# else
+# define CLK_TCK 60
+# endif
+# endif /* !CLK_TCK */
+#endif /* !HAVE_SYSCONF && !_SC_CLK_TCK */
+
+long
+get_clk_tck ()
+{
+ static long retval = 0;
+
+ if (retval != 0)
+ return (retval);
+
+#if defined (HAVE_SYSCONF) && defined (_SC_CLK_TCK)
+ retval = sysconf (_SC_CLK_TCK);
+#else /* !SYSCONF || !_SC_CLK_TCK */
+ retval = CLK_TCK;
+#endif /* !SYSCONF || !_SC_CLK_TCK */
+
+ return (retval);
+}
diff --git a/lib/sh/getcwd.c b/lib/sh/getcwd.c
new file mode 100644
index 0000000..9a8b31f
--- /dev/null
+++ b/lib/sh/getcwd.c
@@ -0,0 +1,319 @@
+/* getcwd.c -- stolen from the GNU C library and modified to work with bash. */
+
+/* Copyright (C) 1991 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If
+ not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+ Cambridge, MA 02139, USA. */
+
+#include <config.h>
+
+#if !defined (HAVE_GETCWD)
+
+#include <bashtypes.h>
+#include <errno.h>
+
+#if defined (HAVE_LIMITS_H)
+# include <limits.h>
+#endif
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+#include <posixdir.h>
+#include <posixstat.h>
+#include <maxpath.h>
+#include <memalloc.h>
+
+#include <bashansi.h>
+
+#if !defined (errno)
+extern int errno;
+#endif /* !errno */
+
+#if defined (__STDC__)
+# define CONST const
+# define PTR void *
+#else /* !__STDC__ */
+# define CONST
+# define PTR char *
+#endif /* !__STDC__ */
+
+#if !defined (PATH_MAX)
+# if defined (MAXPATHLEN)
+# define PATH_MAX MAXPATHLEN
+# else /* !MAXPATHLEN */
+# define PATH_MAX 1024
+# endif /* !MAXPATHLEN */
+#endif /* !PATH_MAX */
+
+#if !defined (HAVE_LSTAT)
+# define lstat stat
+#endif
+
+/* Get the pathname of the current working directory,
+ and put it in SIZE bytes of BUF. Returns NULL if the
+ directory couldn't be determined or SIZE was too small.
+ If successful, returns BUF. In GNU, if BUF is NULL,
+ an array is allocated with `malloc'; the array is SIZE
+ bytes long, unless SIZE <= 0, in which case it is as
+ big as necessary. */
+#if defined (__STDC__)
+char *
+getcwd (char *buf, size_t size)
+#else /* !__STDC__ */
+char *
+getcwd (buf, size)
+ char *buf;
+ size_t size;
+#endif /* !__STDC__ */
+{
+ static CONST char dots[]
+ = "../../../../../../../../../../../../../../../../../../../../../../../\
+../../../../../../../../../../../../../../../../../../../../../../../../../../\
+../../../../../../../../../../../../../../../../../../../../../../../../../..";
+ CONST char *dotp, *dotlist;
+ size_t dotsize;
+ dev_t rootdev, thisdev;
+ ino_t rootino, thisino;
+ char path[PATH_MAX + 1];
+ register char *pathp;
+ char *pathbuf;
+ size_t pathsize;
+ struct stat st;
+ int saved_errno;
+
+ if (buf != NULL && size == 0)
+ {
+ errno = EINVAL;
+ return ((char *)NULL);
+ }
+
+ pathsize = sizeof (path);
+ pathp = &path[pathsize];
+ *--pathp = '\0';
+ pathbuf = path;
+
+ if (stat (".", &st) < 0)
+ return ((char *)NULL);
+ thisdev = st.st_dev;
+ thisino = st.st_ino;
+
+ if (stat ("/", &st) < 0)
+ return ((char *)NULL);
+ rootdev = st.st_dev;
+ rootino = st.st_ino;
+
+ saved_errno = 0;
+
+ dotsize = sizeof (dots) - 1;
+ dotp = &dots[sizeof (dots)];
+ dotlist = dots;
+ while (!(thisdev == rootdev && thisino == rootino))
+ {
+ register DIR *dirstream;
+ register struct dirent *d;
+ dev_t dotdev;
+ ino_t dotino;
+ char mount_point;
+ int namlen;
+
+ /* Look at the parent directory. */
+ if (dotp == dotlist)
+ {
+ /* My, what a deep directory tree you have, Grandma. */
+ char *new;
+ if (dotlist == dots)
+ {
+ new = malloc (dotsize * 2 + 1);
+ if (new == NULL)
+ goto lose;
+ memcpy (new, dots, dotsize);
+ }
+ else
+ {
+ new = realloc ((PTR) dotlist, dotsize * 2 + 1);
+ if (new == NULL)
+ goto lose;
+ }
+ memcpy (&new[dotsize], new, dotsize);
+ dotp = &new[dotsize];
+ dotsize *= 2;
+ new[dotsize] = '\0';
+ dotlist = new;
+ }
+
+ dotp -= 3;
+
+ /* Figure out if this directory is a mount point. */
+ if (stat (dotp, &st) < 0)
+ goto lose;
+ dotdev = st.st_dev;
+ dotino = st.st_ino;
+ mount_point = dotdev != thisdev;
+
+ /* Search for the last directory. */
+ dirstream = opendir (dotp);
+ if (dirstream == NULL)
+ goto lose;
+ while ((d = readdir (dirstream)) != NULL)
+ {
+ if (d->d_name[0] == '.' &&
+ (d->d_name[1] == '\0' ||
+ (d->d_name[1] == '.' && d->d_name[2] == '\0')))
+ continue;
+ if (mount_point || d->d_fileno == thisino)
+ {
+ char *name;
+
+ namlen = D_NAMLEN(d);
+ name = (char *)
+ alloca (dotlist + dotsize - dotp + 1 + namlen + 1);
+ memcpy (name, dotp, dotlist + dotsize - dotp);
+ name[dotlist + dotsize - dotp] = '/';
+ memcpy (&name[dotlist + dotsize - dotp + 1],
+ d->d_name, namlen + 1);
+ if (lstat (name, &st) < 0)
+ {
+#if 0
+ int save = errno;
+ (void) closedir (dirstream);
+ errno = save;
+ goto lose;
+#else
+ saved_errno = errno;
+#endif
+ }
+ if (st.st_dev == thisdev && st.st_ino == thisino)
+ break;
+ }
+ }
+ if (d == NULL)
+ {
+#if 0
+ int save = errno;
+#else
+ int save = errno ? errno : saved_errno;
+#endif
+ (void) closedir (dirstream);
+ errno = save;
+ goto lose;
+ }
+ else
+ {
+ size_t space;
+
+ while ((space = pathp - pathbuf) <= namlen)
+ {
+ char *new;
+
+ if (pathbuf == path)
+ {
+ new = malloc (pathsize * 2);
+ if (!new)
+ goto lose;
+ }
+ else
+ {
+ new = realloc ((PTR) pathbuf, (pathsize * 2));
+ if (!new)
+ goto lose;
+ pathp = new + space;
+ }
+ (void) memcpy (new + pathsize + space, pathp, pathsize - space);
+ pathp = new + pathsize + space;
+ pathbuf = new;
+ pathsize *= 2;
+ }
+
+ pathp -= namlen;
+ (void) memcpy (pathp, d->d_name, namlen);
+ *--pathp = '/';
+ (void) closedir (dirstream);
+ }
+
+ thisdev = dotdev;
+ thisino = dotino;
+ }
+
+ if (pathp == &path[sizeof(path) - 1])
+ *--pathp = '/';
+
+ if (dotlist != dots)
+ free ((PTR) dotlist);
+
+ {
+ size_t len = pathbuf + pathsize - pathp;
+ if (buf == NULL)
+ {
+ if (len < (size_t) size)
+ len = size;
+ buf = (char *) malloc (len);
+ if (buf == NULL)
+ goto lose2;
+ }
+ else if ((size_t) size < len)
+ {
+ errno = ERANGE;
+ goto lose2;
+ }
+ (void) memcpy((PTR) buf, (PTR) pathp, len);
+ }
+
+ if (pathbuf != path)
+ free (pathbuf);
+
+ return (buf);
+
+ lose:
+ if ((dotlist != dots) && dotlist)
+ {
+ int e = errno;
+ free ((PTR) dotlist);
+ errno = e;
+ }
+
+ lose2:
+ if ((pathbuf != path) && pathbuf)
+ {
+ int e = errno;
+ free ((PTR) pathbuf);
+ errno = e;
+ }
+ return ((char *)NULL);
+}
+
+#if defined (TEST)
+# include <stdio.h>
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ char b[PATH_MAX];
+
+ if (getcwd(b, sizeof(b)))
+ {
+ printf ("%s\n", b);
+ exit (0);
+ }
+ else
+ {
+ perror ("cwd: getcwd");
+ exit (1);
+ }
+}
+#endif /* TEST */
+#endif /* !HAVE_GETCWD */
diff --git a/lib/sh/getenv.c b/lib/sh/getenv.c
new file mode 100644
index 0000000..6353615
--- /dev/null
+++ b/lib/sh/getenv.c
@@ -0,0 +1,99 @@
+/* getenv.c - get environment variable value from the shell's variable
+ list. */
+
+/* Copyright (C) 1997 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
+
+#include <config.h>
+
+#if defined (CAN_REDEFINE_GETENV)
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+#include <bashansi.h>
+#include <shell.h>
+
+extern char **environ;
+
+/* We supply our own version of getenv () because we want library
+ routines to get the changed values of exported variables. */
+
+/* The NeXT C library has getenv () defined and used in the same file.
+ This screws our scheme. However, Bash will run on the NeXT using
+ the C library getenv (), since right now the only environment variable
+ that we care about is HOME, and that is already defined. */
+static char *last_tempenv_value = (char *)NULL;
+
+char *
+getenv (name)
+#if defined (__linux__) || defined (__bsdi__) || defined (convex)
+ const char *name;
+#else
+ char const *name;
+#endif /* !__linux__ && !__bsdi__ && !convex */
+{
+ SHELL_VAR *var;
+
+ var = find_tempenv_variable ((char *)name);
+ if (var)
+ {
+ FREE (last_tempenv_value);
+
+ last_tempenv_value = savestring (value_cell (var));
+ dispose_variable (var);
+ return (last_tempenv_value);
+ }
+ else if (shell_variables)
+ {
+ var = find_variable ((char *)name);
+ if (var && exported_p (var))
+ return (value_cell (var));
+ }
+ else
+ {
+ register int i, len;
+
+ /* In some cases, s5r3 invokes getenv() before main(); BSD systems
+ using gprof also exhibit this behavior. This means that
+ shell_variables will be 0 when this is invoked. We look up the
+ variable in the real environment in that case. */
+
+ for (i = 0, len = strlen (name); environ[i]; i++)
+ {
+ if ((STREQN (environ[i], name, len)) && (environ[i][len] == '='))
+ return (environ[i] + len + 1);
+ }
+ }
+
+ return ((char *)NULL);
+}
+
+/* Some versions of Unix use _getenv instead. */
+char *
+_getenv (name)
+#if defined (__linux__) || defined (__bsdi__) || defined (convex)
+ const char *name;
+#else
+ char const *name;
+#endif /* !__linux__ && !__bsdi__ && !convex */
+{
+ return (getenv (name));
+}
+#endif /* CAN_REDEFINE_GETENV */
diff --git a/lib/sh/itos.c b/lib/sh/itos.c
new file mode 100644
index 0000000..720e410
--- /dev/null
+++ b/lib/sh/itos.c
@@ -0,0 +1,64 @@
+/* itos.c -- Convert integer to string. */
+
+/* Copyright (C) 1998, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <config.h>
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+#include "bashansi.h"
+#include "shell.h"
+
+/* Number of characters that can appear in a string representation
+ of an integer. 32 is larger than the string rep of 2^^31 - 1. */
+#define MAX_INT_LEN 32
+
+/* Integer to string conversion. This conses the string; the
+ caller should free it. */
+char *
+itos (i)
+ int i;
+{
+ char buf[MAX_INT_LEN], *p, *ret;
+ int negative = 0;
+ unsigned int ui;
+
+ if (i < 0)
+ {
+ negative++;
+ i = -i;
+ }
+
+ ui = (unsigned int) i;
+
+ p = buf + MAX_INT_LEN - 2;
+ p[1] = '\0';
+
+ do
+ *p-- = (ui % 10) + '0';
+ while (ui /= 10);
+
+ if (negative)
+ *p-- = '-';
+
+ ret = savestring (p + 1);
+ return (ret);
+}
diff --git a/lib/sh/oslib.c b/lib/sh/oslib.c
new file mode 100644
index 0000000..d92c32e
--- /dev/null
+++ b/lib/sh/oslib.c
@@ -0,0 +1,233 @@
+/* oslib.c - functions present only in some unix versions. */
+
+/* Copyright (C) 1995 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
+
+#include <config.h>
+
+#include <bashtypes.h>
+#ifndef _MINIX
+# include <sys/param.h>
+#endif
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+#include <posixstat.h>
+#include <filecntl.h>
+#include <bashansi.h>
+
+#include <stdio.h>
+#include <errno.h>
+#include <ctype.h>
+
+#include <shell.h>
+
+#if !defined (errno)
+extern int errno;
+#endif /* !errno */
+
+/* Make the functions strchr and strrchr if they do not exist. */
+#if !defined (HAVE_STRCHR)
+char *
+strchr (string, c)
+ char *string;
+ int c;
+{
+ register char *s;
+
+ for (s = string; s && *s; s++)
+ if (*s == c)
+ return (s);
+
+ return ((char *) NULL);
+}
+
+char *
+strrchr (string, c)
+ char *string;
+ int c;
+{
+ register char *s, *t;
+
+ for (s = string, t = (char *)NULL; s && *s; s++)
+ if (*s == c)
+ t = s;
+ return (t);
+}
+#endif /* !HAVE_STRCHR */
+
+#if !defined (HAVE_DUP2) || defined (DUP2_BROKEN)
+/* Replacement for dup2 (), for those systems which either don't have it,
+ or supply one with broken behaviour. */
+int
+dup2 (fd1, fd2)
+ int fd1, fd2;
+{
+ int saved_errno, r;
+
+ /* If FD1 is not a valid file descriptor, then return immediately with
+ an error. */
+ if (fcntl (fd1, F_GETFL, 0) == -1)
+ return (-1);
+
+ if (fd2 < 0 || fd2 >= getdtablesize ())
+ {
+ errno = EBADF;
+ return (-1);
+ }
+
+ if (fd1 == fd2)
+ return (0);
+
+ saved_errno = errno;
+
+ (void) close (fd2);
+ r = fcntl (fd1, F_DUPFD, fd2);
+
+ if (r >= 0)
+ errno = saved_errno;
+ else
+ if (errno == EINVAL)
+ errno = EBADF;
+
+ /* Force the new file descriptor to remain open across exec () calls. */
+ SET_OPEN_ON_EXEC (fd2);
+ return (r);
+}
+#endif /* !HAVE_DUP2 */
+
+/*
+ * Return the total number of available file descriptors.
+ *
+ * On some systems, like 4.2BSD and its descendents, there is a system call
+ * that returns the size of the descriptor table: getdtablesize(). There are
+ * lots of ways to emulate this on non-BSD systems.
+ *
+ * On System V.3, this can be obtained via a call to ulimit:
+ * return (ulimit(4, 0L));
+ *
+ * On other System V systems, NOFILE is defined in /usr/include/sys/param.h
+ * (this is what we assume below), so we can simply use it:
+ * return (NOFILE);
+ *
+ * On POSIX systems, there are specific functions for retrieving various
+ * configuration parameters:
+ * return (sysconf(_SC_OPEN_MAX));
+ *
+ */
+
+#if !defined (HAVE_GETDTABLESIZE)
+int
+getdtablesize ()
+{
+# if defined (_POSIX_VERSION) && defined (HAVE_SYSCONF) && defined (_SC_OPEN_MAX)
+ return (sysconf(_SC_OPEN_MAX)); /* Posix systems use sysconf */
+# else /* ! (_POSIX_VERSION && HAVE_SYSCONF && _SC_OPEN_MAX) */
+# if defined (ULIMIT_MAXFDS)
+ return (ulimit (4, 0L)); /* System V.3 systems use ulimit(4, 0L) */
+# else /* !ULIMIT_MAXFDS */
+# if defined (NOFILE) /* Other systems use NOFILE */
+ return (NOFILE);
+# else /* !NOFILE */
+ return (20); /* XXX - traditional value is 20 */
+# endif /* !NOFILE */
+# endif /* !ULIMIT_MAXFDS */
+# endif /* ! (_POSIX_VERSION && _SC_OPEN_MAX) */
+}
+#endif /* !HAVE_GETDTABLESIZE */
+
+#if !defined (HAVE_BCOPY)
+void
+bcopy (s,d,n)
+ char *d, *s;
+ int n;
+{
+ FASTCOPY (s, d, n);
+}
+#endif /* !HAVE_BCOPY */
+
+#if !defined (HAVE_BZERO)
+void
+bzero (s, n)
+ char *s;
+ int n;
+{
+ register int i;
+ register char *r;
+
+ for (i = 0, r = s; i < n; i++)
+ *r++ = '\0';
+}
+#endif
+
+#if !defined (HAVE_GETHOSTNAME)
+# if defined (HAVE_UNAME)
+# include <sys/utsname.h>
+int
+gethostname (name, namelen)
+ char *name;
+ int namelen;
+{
+ int i;
+ struct utsname ut;
+
+ --namelen;
+
+ uname (&ut);
+ i = strlen (ut.nodename) + 1;
+ strncpy (name, ut.nodename, i < namelen ? i : namelen);
+ name[namelen] = '\0';
+ return (0);
+}
+# else /* !HAVE_UNAME */
+int
+gethostname (name, namelen)
+ int name, namelen;
+{
+ strncpy (name, "unknown", namelen);
+ name[namelen] = '\0';
+ return 0;
+}
+# endif /* !HAVE_UNAME */
+#endif /* !HAVE_GETHOSTNAME */
+
+#if !defined (HAVE_KILLPG)
+int
+killpg (pgrp, sig)
+ pid_t pgrp;
+ int sig;
+{
+ return (kill (-pgrp, sig));
+}
+#endif /* !HAVE_KILLPG */
+
+#if !defined (HAVE_MKFIFO) && defined (PROCESS_SUBSTITUTION)
+int
+mkfifo (path, mode)
+ char *path;
+ int mode;
+{
+#if defined (S_IFIFO)
+ return (mknod (path, (mode | S_IFIFO), 0));
+#else /* !S_IFIFO */
+ return (-1);
+#endif /* !S_IFIFO */
+}
+#endif /* !HAVE_MKFIFO && PROCESS_SUBSTITUTION */
diff --git a/lib/sh/setlinebuf.c b/lib/sh/setlinebuf.c
new file mode 100644
index 0000000..05f9579
--- /dev/null
+++ b/lib/sh/setlinebuf.c
@@ -0,0 +1,41 @@
+/* setlinebuf.c - line-buffer a stdio stream. */
+
+/* Copyright (C) 1997 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
+
+#include <config.h>
+
+#if !defined (HAVE_SETLINEBUF)
+
+#include <stdio.h>
+
+/* Cause STREAM to buffer lines as opposed to characters or blocks. */
+int
+setlinebuf (stream)
+ FILE *stream;
+{
+#if defined (_IOLBF)
+# if defined (SETVBUF_REVERSED)
+ setvbuf (stream, _IOLBF, (char *)NULL, BUFSIZ);
+# else /* !SETVBUF_REVERSED */
+ setvbuf (stream, (char *)NULL, _IOLBF, BUFSIZ);
+# endif /* !SETVBUF_REVERSED */
+#endif /* _IOLBF */
+ return (0);
+}
+#endif /* !HAVE_SETLINEBUF */
diff --git a/lib/sh/strcasecmp.c b/lib/sh/strcasecmp.c
new file mode 100644
index 0000000..74d98cb
--- /dev/null
+++ b/lib/sh/strcasecmp.c
@@ -0,0 +1,88 @@
+/* strcasecmp.c - functions for case-insensitive string comparison. */
+
+/* Copyright (C) 1995 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
+
+#include <config.h>
+
+#if !defined (HAVE_STRCASECMP)
+
+#include <stdc.h>
+#include <bashansi.h>
+#include <ctype.h>
+
+#if !defined (to_lower)
+# define to_lower(c) (islower(c) ? (c) : tolower(c))
+#endif /* to_lower */
+
+/* Compare at most COUNT characters from string1 to string2. Case
+ doesn't matter. */
+int
+strncasecmp (string1, string2, count)
+ const char *string1;
+ const char *string2;
+ int count;
+{
+ register const char *s1;
+ register const char *s2;
+ register int r;
+
+ if (count <= 0 || (string1 == string2))
+ return 0;
+
+ s1 = string1;
+ s2 = string2;
+ do
+ {
+ if ((r = to_lower (*s1) - to_lower (*s2)) != 0)
+ return r;
+ if (*s1++ == '\0')
+ break;
+ s2++;
+ }
+ while (--count != 0);
+
+ return (0);
+}
+
+/* strcmp (), but caseless. */
+int
+strcasecmp (string1, string2)
+ const char *string1;
+ const char *string2;
+{
+ register const char *s1;
+ register const char *s2;
+ register int r;
+
+ s1 = string1;
+ s2 = string2;
+
+ if (s1 == s2)
+ return (0);
+
+ while ((r = to_lower (*s1) - to_lower (*s2)) == 0)
+ {
+ if (*s1++ == '\0')
+ return 0;
+ s2++;
+ }
+
+ return (r);
+}
+#endif /* !HAVE_STRCASECMP */
diff --git a/lib/sh/strerror.c b/lib/sh/strerror.c
new file mode 100644
index 0000000..4990aa7
--- /dev/null
+++ b/lib/sh/strerror.c
@@ -0,0 +1,74 @@
+/* strerror.c - string corresponding to a particular value of errno. */
+
+/* Copyright (C) 1995 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
+
+#include <config.h>
+
+#if !defined (HAVE_STRERROR)
+
+#include <bashtypes.h>
+#ifndef _MINIX
+# include <sys/param.h>
+#endif
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+
+#include <shell.h>
+
+#if !defined (errno)
+extern int errno;
+#endif /* !errno */
+
+/* Return a string corresponding to the error number E. From
+ the ANSI C spec. */
+#if defined (strerror)
+# undef strerror
+#endif
+
+static char *errbase = "Unknown system error ";
+
+char *
+strerror (e)
+ int e;
+{
+ static char emsg[40];
+#if defined (HAVE_SYS_ERRLIST)
+ extern int sys_nerr;
+ extern char *sys_errlist[];
+
+ if (e > 0 && e < sys_nerr)
+ return (sys_errlist[e]);
+ else
+#endif /* HAVE_SYS_ERRLIST */
+ {
+ char *z;
+
+ z = itos (e);
+ strcpy (emsg, errbase);
+ strcat (emsg, z);
+ free (z);
+ return (&emsg[0]);
+ }
+}
+#endif /* HAVE_STRERROR */
diff --git a/lib/sh/strtod.c b/lib/sh/strtod.c
new file mode 100644
index 0000000..fe0f09d
--- /dev/null
+++ b/lib/sh/strtod.c
@@ -0,0 +1,197 @@
+/* Copyright (C) 1991, 1992 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-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#ifndef HAVE_STRTOD
+
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+
+#include <ctype.h>
+#include <math.h>
+
+#if HAVE_FLOAT_H
+# include <float.h>
+#else
+# define DBL_MAX 1.7976931348623159e+308
+# define DBL_MIN 2.2250738585072010e-308
+#endif
+
+#include <bashansi.h>
+
+#ifndef NULL
+# define NULL 0
+#endif
+
+#ifndef HUGE_VAL
+# define HUGE_VAL HUGE
+#endif
+
+/* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
+ character after the last one used in the number is put in *ENDPTR. */
+double
+strtod (nptr, endptr)
+ const char *nptr;
+ char **endptr;
+{
+ register const char *s;
+ short sign;
+
+ /* The number so far. */
+ double num;
+
+ int got_dot; /* Found a decimal point. */
+ int got_digit; /* Seen any digits. */
+
+ /* The exponent of the number. */
+ long int exponent;
+
+ if (nptr == NULL)
+ {
+ errno = EINVAL;
+ goto noconv;
+ }
+
+ s = nptr;
+
+ /* Eat whitespace. */
+ while (isspace (*s))
+ ++s;
+
+ /* Get the sign. */
+ sign = *s == '-' ? -1 : 1;
+ if (*s == '-' || *s == '+')
+ ++s;
+
+ num = 0.0;
+ got_dot = 0;
+ got_digit = 0;
+ exponent = 0;
+ for (;; ++s)
+ {
+ if (isdigit (*s))
+ {
+ got_digit = 1;
+
+ /* Make sure that multiplication by 10 will not overflow. */
+ if (num > DBL_MAX * 0.1)
+ /* The value of the digit doesn't matter, since we have already
+ gotten as many digits as can be represented in a `double'.
+ This doesn't necessarily mean the result will overflow.
+ The exponent may reduce it to within range.
+
+ We just need to record that there was another
+ digit so that we can multiply by 10 later. */
+ ++exponent;
+ else
+ num = (num * 10.0) + (*s - '0');
+
+ /* Keep track of the number of digits after the decimal point.
+ If we just divided by 10 here, we would lose precision. */
+ if (got_dot)
+ --exponent;
+ }
+ else if (!got_dot && *s == '.')
+ /* Record that we have found the decimal point. */
+ got_dot = 1;
+ else
+ /* Any other character terminates the number. */
+ break;
+ }
+
+ if (!got_digit)
+ goto noconv;
+
+ if (tolower (*s) == 'e')
+ {
+ /* Get the exponent specified after the `e' or `E'. */
+ int save = errno;
+ char *end;
+ long int exp;
+
+ errno = 0;
+ ++s;
+ exp = strtol (s, &end, 10);
+ if (errno == ERANGE)
+ {
+ /* The exponent overflowed a `long int'. It is probably a safe
+ assumption that an exponent that cannot be represented by
+ a `long int' exceeds the limits of a `double'. */
+ if (endptr != NULL)
+ *endptr = end;
+ if (exp < 0)
+ goto underflow;
+ else
+ goto overflow;
+ }
+ else if (end == s)
+ /* There was no exponent. Reset END to point to
+ the 'e' or 'E', so *ENDPTR will be set there. */
+ end = (char *) s - 1;
+ errno = save;
+ s = end;
+ exponent += exp;
+ }
+
+ if (endptr != NULL)
+ *endptr = (char *) s;
+
+ if (num == 0.0)
+ return 0.0;
+
+ /* Multiply NUM by 10 to the EXPONENT power,
+ checking for overflow and underflow. */
+
+ if (exponent < 0)
+ {
+ if (num < DBL_MIN * pow (10.0, (double) -exponent))
+ goto underflow;
+ }
+ else if (exponent > 0)
+ {
+ if (num > DBL_MAX * pow (10.0, (double) -exponent))
+ goto overflow;
+ }
+
+ num *= pow (10.0, (double) exponent);
+
+ return num * sign;
+
+overflow:
+ /* Return an overflow error. */
+ errno = ERANGE;
+ return HUGE_VAL * sign;
+
+underflow:
+ /* Return an underflow error. */
+ if (endptr != NULL)
+ *endptr = (char *) nptr;
+ errno = ERANGE;
+ return 0.0;
+
+noconv:
+ /* There was no number. */
+ if (endptr != NULL)
+ *endptr = (char *) nptr;
+ return 0.0;
+}
+
+#endif /* !HAVE_STRTOD */
diff --git a/lib/sh/strtol.c b/lib/sh/strtol.c
new file mode 100644
index 0000000..aed233f
--- /dev/null
+++ b/lib/sh/strtol.c
@@ -0,0 +1,213 @@
+/* strtol - Convert string representation of a number into an integer value.
+ Copyright (C) 1997 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-1307, USA. */
+
+#include <config.h>
+
+#if !defined (HAVE_STRTOL)
+
+#include <ctype.h>
+#include <errno.h>
+
+#ifndef errno
+extern int errno;
+#endif
+
+#ifndef __set_errno
+# define __set_errno(Val) errno = (Val)
+#endif
+
+#ifdef HAVE_LIMITS_H
+# include <limits.h>
+#endif
+
+#include <bashansi.h>
+
+#ifndef NULL
+# define NULL 0
+#endif
+
+/* Nonzero if we are defining `strtoul', operating on unsigned integers. */
+#ifndef UNSIGNED
+# define UNSIGNED 0
+# define RETTYPE long
+#else
+# define RETTYPE unsigned long
+#endif
+
+/* Determine the name. */
+#if UNSIGNED
+# define strtol strtoul
+#endif
+
+#ifndef CHAR_BIT
+# define CHAR_BIT 8
+#endif
+
+#ifndef ULONG_MAX
+# define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
+# define ULONG_MIN ((unsigned long) 0 - ULONG_MAX)
+#endif
+
+#ifndef LONG_MAX
+# define LONG_MAX ((long) (ULONG_MAX >> 1))
+# define LONG_MIN ((long) (0 - LONG_MAX))
+#endif
+
+/* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
+ If BASE is 0 the base is determined by the presence of a leading
+ zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
+ If BASE is < 2 or > 36, it is reset to 10.
+ If ENDPTR is not NULL, a pointer to the character after the last
+ one converted is stored in *ENDPTR. */
+
+RETTYPE
+strtol (nptr, endptr, base)
+ const char *nptr;
+ char **endptr;
+ int base;
+{
+ int negative;
+ register unsigned long cutoff, i;
+ register unsigned int cutlim;
+ register const char *s;
+ register unsigned char c;
+ const char *save, *end;
+ int overflow;
+
+ if (base < 0 || base == 1 || base > 36)
+ base = 10;
+
+ save = s = nptr;
+
+ /* Skip white space. */
+ while (isspace (*s))
+ ++s;
+ if (*s == '\0')
+ goto noconv;
+
+ /* Check for a sign. */
+ if (*s == '-' || *s == '+')
+ {
+ negative = (*s == '-');
+ ++s;
+ }
+ else
+ negative = 0;
+
+ if (base == 16 && *s == '0' && toupper (s[1]) == 'X')
+ s += 2;
+
+ /* If BASE is zero, figure it out ourselves. */
+ if (base == 0)
+ if (*s == '0')
+ {
+ if (toupper (s[1]) == 'X')
+ {
+ s += 2;
+ base = 16;
+ }
+ else
+ base = 8;
+ }
+ else
+ base = 10;
+
+ /* Save the pointer so we can check later if anything happened. */
+ save = s;
+
+ end = NULL;
+
+ cutoff = ULONG_MAX / (unsigned long int) base;
+ cutlim = ULONG_MAX % (unsigned long int) base;
+
+ overflow = 0;
+ i = 0;
+ for (c = *s; c != '\0'; c = *++s)
+ {
+ if (s == end)
+ break;
+
+ if (c >= '0' && c <= '9')
+ c -= '0';
+ else if (isalpha (c))
+ c = toupper (c) - 'A' + 10;
+ else
+ break;
+
+ if ((int) c >= base)
+ break;
+
+ /* Check for overflow. */
+ if (i > cutoff || (i == cutoff && c > cutlim))
+ overflow = 1;
+ else
+ {
+ i *= (unsigned long int) base;
+ i += c;
+ }
+ }
+
+ /* Check if anything actually happened. */
+ if (s == save)
+ goto noconv;
+
+ /* Store in ENDPTR the address of one character
+ past the last character we converted. */
+ if (endptr != NULL)
+ *endptr = (char *) s;
+
+#if !UNSIGNED
+ /* Check for a value that is within the range of
+ `unsigned long int', but outside the range of `long int'. */
+ if (overflow == 0
+ && i > (negative
+ ? -((unsigned long) (LONG_MIN + 1)) + 1
+ : (unsigned long) LONG_MAX))
+ overflow = 1;
+#endif
+
+ if (overflow)
+ {
+ __set_errno (ERANGE);
+#if UNSIGNED
+ return ULONG_MAX;
+#else
+ return negative ? LONG_MIN : LONG_MAX;
+#endif
+ }
+
+ /* Return the result with the appropriate sign. */
+ return (negative ? -i : i);
+
+noconv:
+ /* We must handle a special case here: the base is 0 or 16 and the
+ first two characters are '0' and 'x', but the rest are no
+ hexadecimal digits. This is no error case. We return 0 and
+ ENDPTR points to the `x'. */
+ if (endptr != NULL)
+ {
+ if (save - nptr >= 2 && toupper (save[-1]) == 'X' && save[-2] == '0')
+ *endptr = (char *) &save[-1];
+ else
+ /* There was no number to convert. */
+ *endptr = (char *) nptr;
+ }
+
+ return 0L;
+}
+
+#endif /* !HAVE_STRTOL */
diff --git a/lib/sh/strtoul.c b/lib/sh/strtoul.c
new file mode 100644
index 0000000..4a75d76
--- /dev/null
+++ b/lib/sh/strtoul.c
@@ -0,0 +1,26 @@
+/* Copyright (C) 1997 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-1307, USA. */
+
+#include <config.h>
+
+#ifndef HAVE_STRTOUL
+
+#define UNSIGNED 1
+#undef HAVE_STRTOL
+
+#include <strtol.c>
+
+#endif /* !HAVE_STRTOUL */
diff --git a/lib/sh/vprint.c b/lib/sh/vprint.c
new file mode 100644
index 0000000..63ea3bf
--- /dev/null
+++ b/lib/sh/vprint.c
@@ -0,0 +1,85 @@
+/* vprint.c -- v[fs]printf() for 4.[23] BSD systems. */
+
+/* Copyright (C) 1987,1989 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <config.h>
+
+#if defined (USE_VFPRINTF_EMULATION)
+
+#include <stdio.h>
+
+#if !defined (NULL)
+# if defined (__STDC__)
+# define NULL ((void *)0)
+# else
+# define NULL 0x0
+# endif /* __STDC__ */
+#endif /* !NULL */
+
+/*
+ * Beware! Don't trust the value returned by either of these functions; it
+ * seems that pre-4.3-tahoe implementations of _doprnt () return the first
+ * argument, i.e. a char *.
+ */
+#include <varargs.h>
+
+int
+vfprintf (iop, fmt, ap)
+ FILE *iop;
+ char *fmt;
+ va_list ap;
+{
+ int len;
+ char localbuf[BUFSIZ];
+
+ if (iop->_flag & _IONBF)
+ {
+ iop->_flag &= ~_IONBF;
+ iop->_ptr = iop->_base = localbuf;
+ len = _doprnt (fmt, ap, iop);
+ (void) fflush (iop);
+ iop->_flag |= _IONBF;
+ iop->_base = NULL;
+ iop->_bufsiz = 0;
+ iop->_cnt = 0;
+ }
+ else
+ len = _doprnt (fmt, ap, iop);
+ return (ferror (iop) ? EOF : len);
+}
+
+/*
+ * Ditto for vsprintf
+ */
+int
+vsprintf (str, fmt, ap)
+ char *str, *fmt;
+ va_list ap;
+{
+ FILE f;
+ int len;
+
+ f._flag = _IOWRT|_IOSTRG;
+ f._ptr = str;
+ f._cnt = 32767;
+ len = _doprnt (fmt, ap, &f);
+ *f._ptr = 0;
+ return (len);
+}
+#endif /* USE_VFPRINTF_EMULATION */
diff --git a/lib/termcap/termcap.c b/lib/termcap/termcap.c
index 2a270c4..eb690c9 100644
--- a/lib/termcap/termcap.c
+++ b/lib/termcap/termcap.c
@@ -21,11 +21,12 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <config.h>
/* Get the O_* definitions for open et al. */
+#ifndef _MINIX
#include <sys/file.h>
-#ifdef USG5
-#include <fcntl.h>
#endif
+#include <fcntl.h>
+
#else /* not HAVE_CONFIG_H */
#ifdef STDC_HEADERS
@@ -777,4 +778,3 @@ tprint (cap)
}
#endif /* TEST */
-
diff --git a/lib/tilde/tilde.c b/lib/tilde/tilde.c
index fe263a2..3741f97 100644
--- a/lib/tilde/tilde.c
+++ b/lib/tilde/tilde.c
@@ -24,6 +24,9 @@
#endif
#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
#endif