summaryrefslogtreecommitdiff
path: root/system/ksh-openbsd
diff options
context:
space:
mode:
authorDaniel LEVAI <leva@ecentrum.hu>2011-10-08 20:29:28 -0500
committerRobby Workman <rworkman@slackbuilds.org>2011-10-08 20:29:28 -0500
commit7fc7de8808cabfc22347ad9ca0c51600a2d49550 (patch)
tree7eff81c82e0eb078a175dfd464e7470a6d3d2d75 /system/ksh-openbsd
parent0b9d751630eb7df09cffbee79794e17d0020af67 (diff)
downloadslackbuilds-7fc7de8808cabfc22347ad9ca0c51600a2d49550.tar.gz
system/ksh-openbsd: Renamed from "ksh" and updated to 20110909
Signed-off-by: Robby Workman <rworkman@slackbuilds.org>
Diffstat (limited to 'system/ksh-openbsd')
-rw-r--r--system/ksh-openbsd/README14
-rw-r--r--system/ksh-openbsd/doinst.sh4
-rw-r--r--system/ksh-openbsd/ksh-openbsd.SlackBuild101
-rw-r--r--system/ksh-openbsd/ksh-openbsd.info10
-rw-r--r--system/ksh-openbsd/patches/01-ksh-vi-mode-complete-as-command-on-Ctrl-f.diff152
-rw-r--r--system/ksh-openbsd/patches/02-remove_fp.diff217
-rw-r--r--system/ksh-openbsd/patches/03-ksh-vi-Ctrl-l-in-insert-mode-to-clear-the-screen.diff45
-rw-r--r--system/ksh-openbsd/patches/04-ksh-vi-make-Ctrl-f-in-command-mode-behave-the-same-a.diff26
-rw-r--r--system/ksh-openbsd/patches/05-ksh-print-expansions-like-a-menu-with-numbers-in-vi-.diff105
-rw-r--r--system/ksh-openbsd/patches/06-ksh-remove-backslashes-in-filenames-using-lex.diff118
-rw-r--r--system/ksh-openbsd/patches/07-ksh-vi-compensate-for-cursor-move-on-command-mode.diff55
-rw-r--r--system/ksh-openbsd/patches/08-Remove-from-the-list-of-characters-blocking-addition.diff37
-rw-r--r--system/ksh-openbsd/patches/09a-new_history_implementation.diff584
-rw-r--r--system/ksh-openbsd/patches/09b-linux-new_history_implementation.diff36
-rw-r--r--system/ksh-openbsd/patches/10-ksh_mb_binds.diff1115
-rw-r--r--system/ksh-openbsd/patches/linux/Makefile.diff58
-rw-r--r--system/ksh-openbsd/patches/linux/c_sh_c.diff49
-rw-r--r--system/ksh-openbsd/patches/linux/charclass_h.diff36
-rw-r--r--system/ksh-openbsd/patches/linux/emacs_c.diff10
-rw-r--r--system/ksh-openbsd/patches/linux/eval_c.diff19
-rw-r--r--system/ksh-openbsd/patches/linux/history_c.diff10
-rw-r--r--system/ksh-openbsd/patches/linux/jobs_c.diff22
-rw-r--r--system/ksh-openbsd/patches/linux/lex_c.diff11
-rw-r--r--system/ksh-openbsd/patches/linux/main_c.diff15
-rw-r--r--system/ksh-openbsd/patches/linux/misc_c.diff12
-rw-r--r--system/ksh-openbsd/patches/linux/posix_signals_h.diff128
-rw-r--r--system/ksh-openbsd/patches/linux/sh_h.diff10
-rw-r--r--system/ksh-openbsd/patches/linux/strlcat_c.diff65
-rw-r--r--system/ksh-openbsd/patches/linux/strlcat_h.diff7
-rw-r--r--system/ksh-openbsd/patches/linux/strlcpy_c.diff61
-rw-r--r--system/ksh-openbsd/patches/linux/strlcpy_h.diff7
-rw-r--r--system/ksh-openbsd/patches/linux/trap_c.diff45
-rw-r--r--system/ksh-openbsd/patches/linux/var_c.diff53
-rw-r--r--system/ksh-openbsd/patches/linux/vi_c.diff10
-rw-r--r--system/ksh-openbsd/slack-desc18
35 files changed, 3265 insertions, 0 deletions
diff --git a/system/ksh-openbsd/README b/system/ksh-openbsd/README
new file mode 100644
index 0000000000..56d8252f8e
--- /dev/null
+++ b/system/ksh-openbsd/README
@@ -0,0 +1,14 @@
+This package is a patched version of the OpenBSD ksh, which is based on
+the original PD-ksh, but heavily modified and maintained by OpenBSD
+developers. "Patched" means ported to Linux.
+You can download the source package from a mirror listed in the
+slackbuild's info file, or you can always checkout the latest source
+from OpenBSD's repository.
+
+PD-ksh is a clone of the AT&T Korn shell.
+
+PD-ksh has most of the ksh88 features, not much of the ksh93 features,
+and a number of its own features. It is free and quite portable - you
+should be able to compile it easily on pretty much any unix box.
+The vi editing mode is better (IMHO) than that of ksh88 or ksh93
+(command/file completion using tab (optional), less buggy).
diff --git a/system/ksh-openbsd/doinst.sh b/system/ksh-openbsd/doinst.sh
new file mode 100644
index 0000000000..63941d1bac
--- /dev/null
+++ b/system/ksh-openbsd/doinst.sh
@@ -0,0 +1,4 @@
+if ! grep -q '/bin/pdksh' etc/shells ; then
+ printf "/bin/pdksh\n" >> etc/shells ;
+fi
+
diff --git a/system/ksh-openbsd/ksh-openbsd.SlackBuild b/system/ksh-openbsd/ksh-openbsd.SlackBuild
new file mode 100644
index 0000000000..d9b1830856
--- /dev/null
+++ b/system/ksh-openbsd/ksh-openbsd.SlackBuild
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+# Copyright (c) 2010 Daniel LEVAI
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+# * Neither the name of the <organization> nor the
+# names of its contributors may be used to endorse or promote products
+# derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
+# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Build script for the Linux port of OpenBSD's ksh
+
+PRGNAM=ksh-openbsd
+VERSION=20110911
+BUILD=${BUILD:-1}
+TAG=${TAG:-_SBo}
+
+if [ -z "$ARCH" ]; then
+ case "$( uname -m )" in
+ i?86) ARCH=i486 ;;
+ arm*) ARCH=arm ;;
+ *) ARCH=$( uname -m ) ;;
+ esac
+fi
+
+CWD=$(pwd)
+TMP=${TMP:-/tmp/SBo}
+PKG=$TMP/package-$PRGNAM
+OUTPUT=${OUTPUT:-/tmp}
+
+if [ "${ARCH}" == "i486" ];then
+ SLKCFLAGS='-march=i486 -mtune=i686'
+ LIBDIRSUFFIX=""
+elif [ "$ARCH" == "i686" ]; then
+ SLKCFLAGS="-march=i686 -mtune=i686"
+ LIBDIRSUFFIX=""
+elif [ "${ARCH}" == "x86_64" ];then
+ SLKCFLAGS='-fPIC'
+ LIBDIRSUFFIX="64"
+else
+ SLKCFLAGS="-O2"
+ LIBDIRSUFFIX=""
+fi
+
+set -e
+
+rm -Rf $PKG
+mkdir -p $TMP $PKG $OUTPUT
+cd $TMP
+rm -Rf $PRGNAM
+tar xvf $CWD/$PRGNAM.tar.gz
+cd $PRGNAM
+chown -R root:root .
+find . \
+ \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
+ -exec chmod 755 {} \; -o \
+ \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
+ -exec chmod 644 {} \;
+
+for patch in "${CWD}"/patches/linux/*.diff;do
+ patch -p1 < "${patch}"
+done
+for patch in "${CWD}"/patches/*.diff;do
+ patch -p0 < "${patch}"
+done
+
+CFLAGS="$SLKCFLAGS $(getconf LFS_CFLAGS)" make
+make install DESTDIR=$PKG
+
+strip --strip-unneeded $PKG/bin/pdksh
+gzip -9 $PKG/usr/man/man1/pdksh.1
+
+mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
+cp -a \
+ INSTALL LEGAL NOTES PROJECTS README \
+ $PKG/usr/doc/$PRGNAM-$VERSION
+cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
+
+mkdir -p $PKG/install
+cat $CWD/slack-desc > $PKG/install/slack-desc
+cat $CWD/doinst.sh > $PKG/install/doinst.sh
+
+cd $PKG
+/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
diff --git a/system/ksh-openbsd/ksh-openbsd.info b/system/ksh-openbsd/ksh-openbsd.info
new file mode 100644
index 0000000000..9b7616b254
--- /dev/null
+++ b/system/ksh-openbsd/ksh-openbsd.info
@@ -0,0 +1,10 @@
+PRGNAM="ksh-openbsd"
+VERSION="20110909"
+HOMEPAGE="http://leva.ecentrum.hu/slackbuilds/ksh-openbsd.tar.gz"
+DOWNLOAD="http://leva.ecentrum.hu/slackbuilds/ksh-openbsd.tar.gz"
+MD5SUM="636c07775c9d8cd049f670fc916501e7"
+DOWNLOAD_x86_64=""
+MD5SUM_x86_64=""
+MAINTAINER="Daniel LEVAI"
+EMAIL="leva@ecentrum.hu"
+APPROVED="rworkman"
diff --git a/system/ksh-openbsd/patches/01-ksh-vi-mode-complete-as-command-on-Ctrl-f.diff b/system/ksh-openbsd/patches/01-ksh-vi-mode-complete-as-command-on-Ctrl-f.diff
new file mode 100644
index 0000000000..86a7a56c77
--- /dev/null
+++ b/system/ksh-openbsd/patches/01-ksh-vi-mode-complete-as-command-on-Ctrl-f.diff
@@ -0,0 +1,152 @@
+From 6af44d2fa0cdcba11368cf4eee141e42bf0dd422 Mon Sep 17 00:00:00 2001
+From: Alexander Polakov <polachok@gmail.com>
+Date: Sun, 29 May 2011 14:55:46 +0400
+Subject: [PATCH 1/8] ksh/vi mode: complete as command on Ctrl-f.
+
+ * adds a flag for x_cf_glob() to force completion like it was a command
+ * new argument for complete_word() to pass the flag
+ * reuse print_expansions() unused argument to pass the flag
+
+ Why: useful for sudo or one-liner completion
+ XXX: use different key combination?
+---
+ edit.c | 2 ++
+ edit.h | 7 ++++---
+ vi.c | 22 +++++++++++-----------
+ 3 files changed, 17 insertions(+), 14 deletions(-)
+
+diff --git edit.c edit.c
+index 6c357a7..0b1ff7d 100644
+--- edit.c
++++ edit.c
+@@ -599,6 +599,8 @@ x_cf_glob(int flags, const char *buf, int buflen, int pos, int *startp,
+ len = x_locate_word(buf, buflen, pos, startp, &is_command);
+ if (!(flags & XCF_COMMAND))
+ is_command = 0;
++ if (flags & XCF_FORCE_COMMAND)
++ is_command = 1;
+ /* Don't do command globing on zero length strings - it takes too
+ * long and isn't very useful. File globs are more likely to be
+ * useful, so allow these.
+diff --git edit.h edit.h
+index f966fe4..9c75ffc 100644
+--- edit.h
++++ edit.h
+@@ -38,9 +38,10 @@ typedef struct {
+ EXTERN X_chars edchars;
+
+ /* x_cf_glob() flags */
+-#define XCF_COMMAND BIT(0) /* Do command completion */
+-#define XCF_FILE BIT(1) /* Do file completion */
+-#define XCF_FULLPATH BIT(2) /* command completion: store full path */
++#define XCF_COMMAND BIT(0) /* Do command completion */
++#define XCF_FILE BIT(1) /* Do file completion */
++#define XCF_FULLPATH BIT(2) /* command completion: store full path */
++#define XCF_FORCE_COMMAND BIT(3) /* Force completion as a command */
+ #define XCF_COMMAND_FILE (XCF_COMMAND|XCF_FILE)
+
+ /* edit.c */
+diff --git vi.c vi.c
+index 889b35a..e4173c7 100644
+--- vi.c
++++ vi.c
+@@ -58,7 +58,7 @@ static int newcol(int, int);
+ static void display(char *, char *, int);
+ static void ed_mov_opt(int, char *);
+ static int expand_word(int);
+-static int complete_word(int, int);
++static int complete_word(int, int, int);
+ static int print_expansions(struct edstate *, int);
+ static int char_len(int);
+ static void x_vi_zotc(int);
+@@ -651,7 +651,7 @@ vi_insert(int ch)
+ break;
+
+ case Ctrl('f'):
+- complete_word(0, 0);
++ complete_word(0, 0, XCF_FORCE_COMMAND);
+ break;
+
+ case Ctrl('e'):
+@@ -660,7 +660,7 @@ vi_insert(int ch)
+
+ case Ctrl('i'):
+ if (Flag(FVITABCOMPLETE)) {
+- complete_word(0, 0);
++ complete_word(0, 0, 0);
+ break;
+ }
+ /* FALLTHROUGH */
+@@ -1111,14 +1111,14 @@ vi_cmd(int argcnt, const char *cmd)
+
+ case '=': /* at&t ksh */
+ case Ctrl('e'): /* Nonstandard vi/ksh */
+- print_expansions(es, 1);
++ print_expansions(es, 0);
+ break;
+
+
+ case Ctrl('i'): /* Nonstandard vi/ksh */
+ if (!Flag(FVITABCOMPLETE))
+ return -1;
+- complete_word(1, argcnt);
++ complete_word(1, argcnt, 0);
+ break;
+
+ case Ctrl('['): /* some annoying at&t ksh's */
+@@ -1126,7 +1126,7 @@ vi_cmd(int argcnt, const char *cmd)
+ return -1;
+ case '\\': /* at&t ksh */
+ case Ctrl('f'): /* Nonstandard vi/ksh */
+- complete_word(1, argcnt);
++ complete_word(1, argcnt, 0);
+ break;
+
+
+@@ -1939,7 +1939,7 @@ expand_word(int command)
+ }
+
+ static int
+-complete_word(int command, int count)
++complete_word(int command, int count, int flags)
+ {
+ static struct edstate *buf;
+ int rval = 0;
+@@ -1953,7 +1953,7 @@ complete_word(int command, int count)
+
+ /* Undo previous completion */
+ if (command == 0 && expanded == COMPLETE && buf) {
+- print_expansions(buf, 0);
++ print_expansions(buf, flags);
+ expanded = PRINT;
+ return 0;
+ }
+@@ -1971,7 +1971,7 @@ complete_word(int command, int count)
+ /* XCF_FULLPATH for count 'cause the menu printed by print_expansions()
+ * was done this way.
+ */
+- nwords = x_cf_glob(XCF_COMMAND_FILE | (count ? XCF_FULLPATH : 0),
++ nwords = x_cf_glob(XCF_COMMAND_FILE | (count ? XCF_FULLPATH : 0) | flags,
+ es->cbuf, es->linelen, es->cursor,
+ &start, &end, &words, &is_command);
+ if (nwords == 0) {
+@@ -2044,14 +2044,14 @@ complete_word(int command, int count)
+ }
+
+ static int
+-print_expansions(struct edstate *e, int command)
++print_expansions(struct edstate *e, int flags)
+ {
+ int nwords;
+ int start, end;
+ char **words;
+ int is_command;
+
+- nwords = x_cf_glob(XCF_COMMAND_FILE|XCF_FULLPATH,
++ nwords = x_cf_glob(XCF_COMMAND_FILE|XCF_FULLPATH|flags,
+ e->cbuf, e->linelen, e->cursor,
+ &start, &end, &words, &is_command);
+ if (nwords == 0) {
+--
+1.7.5
+
diff --git a/system/ksh-openbsd/patches/02-remove_fp.diff b/system/ksh-openbsd/patches/02-remove_fp.diff
new file mode 100644
index 0000000000..25817d0a56
--- /dev/null
+++ b/system/ksh-openbsd/patches/02-remove_fp.diff
@@ -0,0 +1,217 @@
+Remove disabled FP support.
+From Okan Demirmen.
+Index: ksh_limval.h
+===================================================================
+RCS file: /home/okan/hack/open/cvs/src/bin/ksh/ksh_limval.h,v
+retrieving revision 1.2
+diff -u -p -r1.2 ksh_limval.h
+--- ksh_limval.h 18 Dec 2004 20:55:52 -0000 1.2
++++ ksh_limval.h 14 Mar 2011 10:03:41 -0000
+@@ -4,10 +4,6 @@
+
+ /* limits.h is included in sh.h */
+
+-#ifndef DMAXEXP
+-# define DMAXEXP 128 /* should be big enough */
+-#endif
+-
+ #ifndef BITS
+ # define BITS(t) (CHAR_BIT * sizeof(t))
+ #endif
+Index: shf.c
+===================================================================
+RCS file: /home/okan/hack/open/cvs/src/bin/ksh/shf.c,v
+retrieving revision 1.15
+diff -u -p -r1.15 shf.c
+--- shf.c 2 Apr 2006 00:48:33 -0000 1.15
++++ shf.c 14 Mar 2011 10:03:19 -0000
+@@ -705,15 +705,7 @@ shf_smprintf(const char *fmt, ...)
+ return shf_sclose(&shf); /* null terminates */
+ }
+
+-#undef FP /* if you want floating point stuff */
+-
+ #define BUF_SIZE 128
+-#define FPBUF_SIZE (DMAXEXP+16)/* this must be >
+- * MAX(DMAXEXP, log10(pow(2, DSIGNIF)))
+- * + ceil(log10(DMAXEXP)) + 8 (I think).
+- * Since this is hard to express as a
+- * constant, just use a large buffer.
+- */
+
+ /*
+ * What kinda of machine we on? Hopefully the C compiler will optimize
+@@ -744,18 +736,6 @@ shf_smprintf(const char *fmt, ...)
+ #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
+
+
+-#ifdef FP
+-#include <math.h>
+-
+-static double
+-my_ceil(double d)
+-{
+- double i;
+-
+- return d - modf(d, &i) + (d < 0 ? -1 : 1);
+-}
+-#endif /* FP */
+-
+ int
+ shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
+ {
+@@ -769,17 +749,6 @@ shf_vfprintf(struct shf *shf, const char
+ char numbuf[(BITS(long) + 2) / 3 + 1];
+ /* this stuff for dealing with the buffer */
+ int nwritten = 0;
+-#ifdef FP
+- /* should be in <math.h>
+- * extern double frexp();
+- */
+- extern char *ecvt();
+-
+- double fpnum;
+- int expo, decpt;
+- char style;
+- char fpbuf[FPBUF_SIZE];
+-#endif /* FP */
+
+ if (!fmt)
+ return 0;
+@@ -946,134 +915,6 @@ shf_vfprintf(struct shf *shf, const char
+ precision = len; /* no loss */
+ }
+ break;
+-
+-#ifdef FP
+- case 'e':
+- case 'g':
+- case 'f':
+- {
+- char *p;
+-
+- /*
+- * This could probably be done better,
+- * but it seems to work. Note that gcvt()
+- * is not used, as you cannot tell it to
+- * not strip the zeros.
+- */
+- flags |= FL_NUMBER;
+- if (!(flags & FL_DOT))
+- precision = 6; /* default */
+- /*
+- * Assumes doubles are pushed on
+- * the stack. If this is not so, then
+- * FL_LONG/FL_SHORT should be checked.
+- */
+- fpnum = va_arg(args, double);
+- s = fpbuf;
+- style = c;
+- /*
+- * This is the same as
+- * expo = ceil(log10(fpnum))
+- * but doesn't need -lm. This is an
+- * approximation as expo is rounded up.
+- */
+- (void) frexp(fpnum, &expo);
+- expo = my_ceil(expo / LOG2_10);
+-
+- if (expo < 0)
+- expo = 0;
+-
+- p = ecvt(fpnum, precision + 1 + expo,
+- &decpt, &tmp);
+- if (c == 'g') {
+- if (decpt < -4 || decpt > precision)
+- style = 'e';
+- else
+- style = 'f';
+- if (decpt > 0 && (precision -= decpt) < 0)
+- precision = 0;
+- }
+- if (tmp)
+- *s++ = '-';
+- else if (flags & FL_PLUS)
+- *s++ = '+';
+- else if (flags & FL_BLANK)
+- *s++ = ' ';
+-
+- if (style == 'e')
+- *s++ = *p++;
+- else {
+- if (decpt > 0) {
+- /* Overflow check - should
+- * never have this problem.
+- */
+- if (decpt > &fpbuf[sizeof(fpbuf)] - s - 8)
+- decpt = &fpbuf[sizeof(fpbuf)] - s - 8;
+- (void) memcpy(s, p, decpt);
+- s += decpt;
+- p += decpt;
+- } else
+- *s++ = '0';
+- }
+-
+- /* print the fraction? */
+- if (precision > 0) {
+- *s++ = '.';
+- /* Overflow check - should
+- * never have this problem.
+- */
+- if (precision > &fpbuf[sizeof(fpbuf)] - s - 7)
+- precision = &fpbuf[sizeof(fpbuf)] - s - 7;
+- for (tmp = decpt; tmp++ < 0 &&
+- precision > 0 ; precision--)
+- *s++ = '0';
+- tmp = strlen(p);
+- if (precision > tmp)
+- precision = tmp;
+- /* Overflow check - should
+- * never have this problem.
+- */
+- if (precision > &fpbuf[sizeof(fpbuf)] - s - 7)
+- precision = &fpbuf[sizeof(fpbuf)] - s - 7;
+- (void) memcpy(s, p, precision);
+- s += precision;
+- /*
+- * `g' format strips trailing
+- * zeros after the decimal.
+- */
+- if (c == 'g' && !(flags & FL_HASH)) {
+- while (*--s == '0')
+- ;
+- if (*s != '.')
+- s++;
+- }
+- } else if (flags & FL_HASH)
+- *s++ = '.';
+-
+- if (style == 'e') {
+- *s++ = (flags & FL_UPPER) ? 'E' : 'e';
+- if (--decpt >= 0)
+- *s++ = '+';
+- else {
+- *s++ = '-';
+- decpt = -decpt;
+- }
+- p = &numbuf[sizeof(numbuf)];
+- for (tmp = 0; tmp < 2 || decpt ; tmp++) {
+- *--p = '0' + decpt % 10;
+- decpt /= 10;
+- }
+- tmp = &numbuf[sizeof(numbuf)] - p;
+- (void) memcpy(s, p, tmp);
+- s += tmp;
+- }
+-
+- len = s - fpbuf;
+- s = fpbuf;
+- precision = len;
+- break;
+- }
+-#endif /* FP */
+
+ case 's':
+ if (!(s = va_arg(args, char *)))
+
+
diff --git a/system/ksh-openbsd/patches/03-ksh-vi-Ctrl-l-in-insert-mode-to-clear-the-screen.diff b/system/ksh-openbsd/patches/03-ksh-vi-Ctrl-l-in-insert-mode-to-clear-the-screen.diff
new file mode 100644
index 0000000000..21a57ffb42
--- /dev/null
+++ b/system/ksh-openbsd/patches/03-ksh-vi-Ctrl-l-in-insert-mode-to-clear-the-screen.diff
@@ -0,0 +1,45 @@
+From 13ef2cd4206ed541fe1dae46c91a339fba5cf5cf Mon Sep 17 00:00:00 2001
+From: Alexander Polakov <polachok@gmail.com>
+Date: Sun, 29 May 2011 15:12:31 +0400
+Subject: [PATCH 3/8] ksh/vi: Ctrl-l in insert mode to clear the screen.
+
+ Why: typing "clear" is too slow.
+---
+ ksh.1 | 3 +++
+ vi.c | 6 ++++++
+ 2 files changed, 9 insertions(+), 0 deletions(-)
+
+diff --git ksh.1 ksh.1
+index f4b5815..01130ea 100644
+--- ksh.1
++++ ksh.1
+@@ -5215,6 +5215,9 @@ List all the commands or files that match the current big-word.
+ Macro expansion.
+ Execute the commands found in the alias
+ .Ar c .
++.It ^L
++Clear the screen leaving the current line at the top of the
++screen.
+ .El
+ .Pp
+ Intra-line movement commands:
+diff --git vi.c vi.c
+index e4173c7..d0de478 100644
+--- vi.c
++++ vi.c
+@@ -646,6 +646,12 @@ vi_insert(int ch)
+ return redo_insert(lastac - 1);
+
+ /* { Begin nonstandard vi commands */
++ case Ctrl('l'):
++ /* Use ANSI escape codes to clear the screen */
++ x_puts("\033[2J\033[0;0H");
++ redraw_line(0);
++ break;
++
+ case Ctrl('x'):
+ expand_word(0);
+ break;
+--
+1.7.5
+
diff --git a/system/ksh-openbsd/patches/04-ksh-vi-make-Ctrl-f-in-command-mode-behave-the-same-a.diff b/system/ksh-openbsd/patches/04-ksh-vi-make-Ctrl-f-in-command-mode-behave-the-same-a.diff
new file mode 100644
index 0000000000..6978789ec9
--- /dev/null
+++ b/system/ksh-openbsd/patches/04-ksh-vi-make-Ctrl-f-in-command-mode-behave-the-same-a.diff
@@ -0,0 +1,26 @@
+From 90f648bf6182d558ff6489caefb52240fb2eab4e Mon Sep 17 00:00:00 2001
+From: Alexander Polakov <polachok@gmail.com>
+Date: Sun, 29 May 2011 15:34:55 +0400
+Subject: [PATCH 4/8] ksh/vi: make Ctrl-f in command mode behave the same as
+ in insert.
+
+---
+ vi.c | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git vi.c vi.c
+index d0de478..6eb5d8c 100644
+--- vi.c
++++ vi.c
+@@ -1132,7 +1132,7 @@ vi_cmd(int argcnt, const char *cmd)
+ return -1;
+ case '\\': /* at&t ksh */
+ case Ctrl('f'): /* Nonstandard vi/ksh */
+- complete_word(1, argcnt, 0);
++ complete_word(1, argcnt, XCF_FORCE_COMMAND);
+ break;
+
+
+--
+1.7.5
+
diff --git a/system/ksh-openbsd/patches/05-ksh-print-expansions-like-a-menu-with-numbers-in-vi-.diff b/system/ksh-openbsd/patches/05-ksh-print-expansions-like-a-menu-with-numbers-in-vi-.diff
new file mode 100644
index 0000000000..135f5054c3
--- /dev/null
+++ b/system/ksh-openbsd/patches/05-ksh-print-expansions-like-a-menu-with-numbers-in-vi-.diff
@@ -0,0 +1,105 @@
+From 91ebd29a64451742b65115fc27034c6a3d154f90 Mon Sep 17 00:00:00 2001
+From: Alexander Polakov <polachok@gmail.com>
+Date: Sun, 29 May 2011 15:41:53 +0400
+Subject: [PATCH 5/8] ksh: print expansions like a "menu" (with numbers) in vi
+ mode
+
+ * adds an extra argument to x_print_expansions()
+ - preserves the emacs mode behaviour
+
+ Why: in vi mode one can use the expansion number to
+ complete the word, but w/o numbers printed you
+ have to *count* expansions with your eyes to
+ find out the number. Stupid, huh?
+---
+ edit.c | 7 +++++--
+ edit.h | 2 +-
+ emacs.c | 4 ++--
+ vi.c | 4 ++--
+ 4 files changed, 10 insertions(+), 7 deletions(-)
+
+diff --git edit.c edit.c
+index 0b1ff7d..9cdcc6d 100644
+--- edit.c
++++ edit.c
+@@ -292,7 +292,7 @@ static void glob_path(int flags, const char *pat, XPtrV *wp,
+ const char *path);
+
+ void
+-x_print_expansions(int nwords, char *const *words, int is_command)
++x_print_expansions(int nwords, char *const *words, int is_command, int menu)
+ {
+ int use_copy = 0;
+ int prefix_len;
+@@ -330,7 +330,10 @@ x_print_expansions(int nwords, char *const *words, int is_command)
+ */
+ x_putc('\r');
+ x_putc('\n');
+- pr_list(use_copy ? (char **) XPptrv(l) : words);
++ if (menu)
++ pr_menu(use_copy ? (char **) XPptrv(l) : words);
++ else
++ pr_list(use_copy ? (char **) XPptrv(l) : words);
+
+ if (use_copy)
+ XPfree(l); /* not x_free_words() */
+diff --git edit.h edit.h
+index 37ccf28..258affe 100644
+--- edit.h
++++ edit.h
+@@ -52,7 +52,7 @@ void x_puts(const char *);
+ bool x_mode(bool);
+ int promptlen(const char *, const char **);
+ int x_do_comment(char *, int, int *);
+-void x_print_expansions(int, char *const *, int);
++void x_print_expansions(int, char *const *, int, int);
+ int x_cf_glob(int, const char *, int, int, int *, int *, char ***, int *);
+ int x_longest_prefix(int , char *const *);
+ int x_basename(const char *, const char *);
+diff --git emacs.c emacs.c
+index 0bfda96..3b2d083 100644
+--- emacs.c
++++ emacs.c
+@@ -1684,7 +1684,7 @@ do_complete(int flags, /* XCF_{COMMAND,FILE,COMMAND_FILE} */
+ }
+
+ if (type == CT_LIST) {
+- x_print_expansions(nwords, words, is_command);
++ x_print_expansions(nwords, words, is_command, 0);
+ x_redraw(0);
+ x_free_words(nwords, words);
+ return;
+@@ -1707,7 +1707,7 @@ do_complete(int flags, /* XCF_{COMMAND,FILE,COMMAND_FILE} */
+ }
+
+ if (type == CT_COMPLIST && !completed) {
+- x_print_expansions(nwords, words, is_command);
++ x_print_expansions(nwords, words, is_command, 0);
+ completed = 1;
+ }
+
+diff --git vi.c vi.c
+index 6eb5d8c..95d192c 100644
+--- vi.c
++++ vi.c
+@@ -1990,7 +1990,7 @@ complete_word(int command, int count, int flags)
+ count--;
+ if (count >= nwords) {
+ vi_error();
+- x_print_expansions(nwords, words, is_command);
++ x_print_expansions(nwords, words, is_command, 1);
+ x_free_words(nwords, words);
+ redraw_line(0);
+ return -1;
+@@ -2064,7 +2064,7 @@ print_expansions(struct edstate *e, int flags)
+ vi_error();
+ return -1;
+ }
+- x_print_expansions(nwords, words, is_command);
++ x_print_expansions(nwords, words, is_command, 1);
+ x_free_words(nwords, words);
+ redraw_line(0);
+ return 0;
+--
+1.7.5
+
diff --git a/system/ksh-openbsd/patches/06-ksh-remove-backslashes-in-filenames-using-lex.diff b/system/ksh-openbsd/patches/06-ksh-remove-backslashes-in-filenames-using-lex.diff
new file mode 100644
index 0000000000..04ca29f6f1
--- /dev/null
+++ b/system/ksh-openbsd/patches/06-ksh-remove-backslashes-in-filenames-using-lex.diff
@@ -0,0 +1,118 @@
+From 458d69d8f10d582fd7a546eabc1b555bb7337627 Mon Sep 17 00:00:00 2001
+From: Alexander Polakov <polachok@gmail.com>
+Date: Sun, 29 May 2011 16:05:58 +0400
+Subject: [PATCH 6/8] ksh: remove backslashes in filenames using lex.
+
+ * this also removes the check for matching pattern and
+ returned filename. if it returns 1 name, then it must
+ be the filename of an existing file, right? i think
+ so.
+
+ * change stat() to lstat() to complete broken links
+ (from LEVAI Daniel).
+
+ Why:
+
+ * less ugly code
+ * working completion for special characters like [], () and so
+ * suggested by martynas
+---
+ edit.c | 31 +++++++------------------------
+ lex.c | 4 ++++
+ lex.h | 1 +
+ 3 files changed, 12 insertions(+), 24 deletions(-)
+
+diff --git edit.c edit.c
+index 9cdcc6d..01c2fe6 100644
+--- edit.c
++++ edit.c
+@@ -351,7 +351,7 @@ x_file_glob(int flags, const char *str, int slen, char ***wordsp)
+ {
+ char *toglob;
+ char **words;
+- int nwords, i, idx, escaping;
++ int nwords;
+ XPtrV w;
+ struct source *s, *sold;
+
+@@ -360,20 +360,6 @@ x_file_glob(int flags, const char *str, int slen, char ***wordsp)
+
+ toglob = add_glob(str, slen);
+
+- /* remove all escaping backward slashes */
+- escaping = 0;
+- for (i = 0, idx = 0; toglob[i]; i++) {
+- if (toglob[i] == '\\' && !escaping) {
+- escaping = 1;
+- continue;
+- }
+-
+- toglob[idx] = toglob[i];
+- idx++;
+- if (escaping) escaping = 0;
+- }
+- toglob[idx] = '\0';
+-
+ /*
+ * Convert "foo*" (toglob) to an array of strings (words)
+ */
+@@ -381,7 +367,7 @@ x_file_glob(int flags, const char *str, int slen, char ***wordsp)
+ s = pushs(SWSTR, ATEMP);
+ s->start = s->str = toglob;
+ source = s;
+- if (yylex(ONEWORD) != LWORD) {
++ if (yylex(ONEWORD|RMBKSLSH) != LWORD) {
+ source = sold;
+ internal_errorf(0, "fileglob: substitute error");
+ return 0;
+@@ -397,15 +383,12 @@ x_file_glob(int flags, const char *str, int slen, char ***wordsp)
+ if (nwords == 1) {
+ struct stat statb;
+
+- /* Check if globbing failed (returned glob pattern),
+- * but be careful (E.g. toglob == "ab*" when the file
+- * "ab*" exists is not an error).
+- * Also, check for empty result - happens if we tried
+- * to glob something which evaluated to an empty
+- * string (e.g., "$FOO" when there is no FOO, etc).
++ /* Check if file exists, also, check for empty
++ * result - happens if we tried to glob something
++ * which evaluated to an empty string (e.g.,
++ * "$FOO" when there is no FOO, etc).
+ */
+- if ((strcmp(words[0], toglob) == 0 &&
+- stat(words[0], &statb) < 0) ||
++ if((lstat(words[0], &statb) < 0) ||
+ words[0][0] == '\0') {
+ x_free_words(nwords, words);
+ words = NULL;
+diff --git lex.c lex.c
+index ef741c6..fe3d91d 100644
+--- lex.c
++++ lex.c
+@@ -299,6 +299,10 @@ yylex(int cf)
+ }
+ /* FALLTHROUGH */
+ default:
++ if (cf & RMBKSLSH) {
++ *wp++ = QCHAR, *wp++ = c;
++ break;
++ }
+ Xcheck(ws, wp);
+ if (c) { /* trailing \ is lost */
+ *wp++ = CHAR, *wp++ = '\\';
+diff --git lex.h lex.h
+index 0904fbd..6a0dbf9 100644
+--- lex.h
++++ lex.h
+@@ -113,6 +113,7 @@ typedef union {
+ #define CMDWORD BIT(8) /* parsing simple command (alias related) */
+ #define HEREDELIM BIT(9) /* parsing <<,<<- delimiter */
+ #define HEREDOC BIT(10) /* parsing heredoc */
++#define RMBKSLSH BIT(11) /* remove backslashes */
+
+ #define HERES 10 /* max << in line */
+
+--
+1.7.5
+
diff --git a/system/ksh-openbsd/patches/07-ksh-vi-compensate-for-cursor-move-on-command-mode.diff b/system/ksh-openbsd/patches/07-ksh-vi-compensate-for-cursor-move-on-command-mode.diff
new file mode 100644
index 0000000000..ce51064a01
--- /dev/null
+++ b/system/ksh-openbsd/patches/07-ksh-vi-compensate-for-cursor-move-on-command-mode.diff
@@ -0,0 +1,55 @@
+From fc2058b4b6a64d66fe1ee318bccea42b4569d31f Mon Sep 17 00:00:00 2001
+From: Alexander Polakov <polachok@gmail.com>
+Date: Sun, 29 May 2011 19:23:17 +0400
+Subject: [PATCH 7/8] ksh/vi: compensate for cursor move on command mode
+
+ * move completion cursor one position right if the
+ character is space
+ * when we enter command mode, cursor is moved one
+ position left, and the space on the end is "lost".
+ So we are trying to complete "ls" instead of file
+ while having "ls " and "file.c" when having "file.c ".
+---
+ vi.c | 15 ++++++++++++++-
+ 1 files changed, 14 insertions(+), 1 deletions(-)
+
+diff --git vi.c vi.c
+index 95d192c..0bac6be 100644
+--- vi.c
++++ vi.c
+@@ -1956,6 +1956,7 @@ complete_word(int command, int count, int flags)
+ int match_len;
+ int is_unique;
+ int is_command;
++ int pos;
+
+ /* Undo previous completion */
+ if (command == 0 && expanded == COMPLETE && buf) {
+@@ -1974,11 +1975,23 @@ complete_word(int command, int count, int flags)
+ buf = 0;
+ }
+
++ /* XXX: hack. When we enter command mode, the cursor is moved
++ * one position left. This means that the space at the end is
++ * eaten and file completion becomes command completion.
++ * (see x_locate_word() for more on this)
++ */
++ pos = es->cursor;
++ if (command) {
++ pos += (isspace(es->cbuf[es->cursor]) ? 1 : 0);
++ if (pos > es->linelen)
++ pos = es->linelen;
++ }
++
+ /* XCF_FULLPATH for count 'cause the menu printed by print_expansions()
+ * was done this way.
+ */
+ nwords = x_cf_glob(XCF_COMMAND_FILE | (count ? XCF_FULLPATH : 0) | flags,
+- es->cbuf, es->linelen, es->cursor,
++ es->cbuf, es->linelen, pos,
+ &start, &end, &words, &is_command);
+ if (nwords == 0) {
+ vi_error();
+--
+1.7.5
+
diff --git a/system/ksh-openbsd/patches/08-Remove-from-the-list-of-characters-blocking-addition.diff b/system/ksh-openbsd/patches/08-Remove-from-the-list-of-characters-blocking-addition.diff
new file mode 100644
index 0000000000..9326e8072d
--- /dev/null
+++ b/system/ksh-openbsd/patches/08-Remove-from-the-list-of-characters-blocking-addition.diff
@@ -0,0 +1,37 @@
+From 5fc8a0ae3c807bd57c8e8d55531770914ea6670a Mon Sep 17 00:00:00 2001
+From: Alexander Polakov <polachok@gmail.com>
+Date: Mon, 20 Jun 2011 00:35:46 +0400
+Subject: [PATCH 8/8] Remove $ from the list of characters blocking *
+ addition.
+
+This allows things like $HOME/bi<tab> complete to /home/username/bin.
+---
+ edit.c | 8 ++++----
+ 1 files changed, 4 insertions(+), 4 deletions(-)
+
+diff --git edit.c edit.c
+index 01c2fe6..2702dab 100644
+--- edit.c
++++ edit.c
+@@ -627,14 +627,14 @@ add_glob(const char *str, int slen)
+
+ /*
+ * If the pathname contains a wildcard (an unquoted '*',
+- * '?', or '[') or parameter expansion ('$'), or a ~username
+- * with no trailing slash, then it is globbed based on that
+- * value (i.e., without the appended '*').
++ * '?', or '[') or a ~username with no trailing slash,
++ * then it is globbed based on that value (i.e., without
++ * the appended '*').
+ */
+ for (s = toglob; *s; s++) {
+ if (*s == '\\' && s[1])
+ s++;
+- else if (*s == '*' || *s == '[' || *s == '?' || *s == '$' ||
++ else if ((*s == '*' || *s == '[' || *s == '?') ||
+ (s[1] == '(' /*)*/ && strchr("+@!", *s)))
+ break;
+ else if (*s == '/')
+--
+1.7.5
+
diff --git a/system/ksh-openbsd/patches/09a-new_history_implementation.diff b/system/ksh-openbsd/patches/09a-new_history_implementation.diff
new file mode 100644
index 0000000000..d083b42554
--- /dev/null
+++ b/system/ksh-openbsd/patches/09a-new_history_implementation.diff
@@ -0,0 +1,584 @@
+From: Marco Peereboom
+To: tech@openbsd.org
+Subject: ksh history corruption
+
+I have had enough of corrupt ksh history so I had a look at the code to
+try to fix it. The magical code was very magical so I basically deleted
+most of it and made ksh history into a flat text file. It handles
+multiple ksh instances writing to the same text file with locks just
+like the current ksh does. I haven't noticed any differences in
+behavior running this.
+
+Code is much simpler and it shaves ~4k of the binary too.
+
+Index: alloc.c
+===================================================================
+RCS file: /cvs/src/bin/ksh/alloc.c,v
+retrieving revision 1.8
+diff -u -p -r1.8 alloc.c
+--- alloc.c 21 Jul 2008 17:30:08 -0000 1.8
++++ alloc.c 30 Aug 2011 18:05:47 -0000
+@@ -62,7 +62,7 @@ alloc(size_t size, Area *ap)
+ {
+ struct link *l;
+
+- l = malloc(sizeof(struct link) + size);
++ l = calloc(1, sizeof(struct link) + size);
+ if (l == NULL)
+ internal_errorf(1, "unable to allocate memory");
+ l->next = ap->freelist;
+Index: history.c
+===================================================================
+RCS file: /cvs/src/bin/ksh/history.c,v
+retrieving revision 1.39
+diff -u -p -r1.39 history.c
+--- history.c 19 May 2010 17:36:08 -0000 1.39
++++ history.c 31 Aug 2011 19:33:24 -0000
+@@ -11,8 +11,7 @@
+ * a) the original in-memory history mechanism
+ * b) a more complicated mechanism done by pc@hillside.co.uk
+ * that more closely follows the real ksh way of doing
+- * things. You need to have the mmap system call for this
+- * to work on your system
++ * things.
+ */
+
+ #include "sh.h"
+@@ -22,19 +21,10 @@
+ # include <sys/file.h>
+ # include <sys/mman.h>
+
+-/*
+- * variables for handling the data file
+- */
+-static int histfd;
+-static int hsize;
+-
+-static int hist_count_lines(unsigned char *, int);
+-static int hist_shrink(unsigned char *, int);
+-static unsigned char *hist_skip_back(unsigned char *,int *,int);
+-static void histload(Source *, unsigned char *, int);
+-static void histinsert(Source *, int, unsigned char *);
+-static void writehistfile(int, char *);
+-static int sprinkle(int);
++static void writehistfile(FILE *);
++static FILE *history_open(int *);
++static int history_load(FILE *, Source *);
++static void history_close(FILE *);
+
+ static int hist_execute(char *);
+ static int hist_replace(char **, const char *, const char *, int);
+@@ -45,8 +35,8 @@ static void histbackup(void);
+ static char **current; /* current position in history[] */
+ static char *hname; /* current name of history file */
+ static int hstarted; /* set after hist_init() called */
+-static Source *hist_source;
+-
++static Source *hist_source;
++static struct stat last_sb;
+
+ int
+ c_fc(char **wp)
+@@ -529,15 +519,10 @@ sethistfile(const char *name)
+ /* if the name is the same as the name we have */
+ if (hname && strcmp(hname, name) == 0)
+ return;
+-
+ /*
+ * its a new name - possibly
+ */
+- if (histfd) {
+- /* yes the file is open */
+- (void) close(histfd);
+- histfd = 0;
+- hsize = 0;
++ if (hname) {
+ afree(hname, APERM);
+ hname = NULL;
+ /* let's reset the history */
+@@ -577,18 +562,26 @@ init_histvec(void)
+ void
+ histsave(int lno, const char *cmd, int dowrite)
+ {
+- char **hp;
+- char *c, *cp;
++ char **hp;
++ char *c, *cp;
++ int changed;
++ FILE *f = NULL;
++
++ if (dowrite) {
++ f = history_open(&changed);
++ if (f && changed) {
++ /* reset history */
++ histptr = history - 1;
++ hist_source->line = 0;
++ history_load(f, hist_source);
++ }
++ }
+
+ c = str_save(cmd, APERM);
+ if ((cp = strchr(c, '\n')) != NULL)
+ *cp = '\0';
+
+- if (histfd && dowrite)
+- writehistfile(lno, c);
+-
+ hp = histptr;
+-
+ if (++hp >= history + histsize) { /* remove oldest command */
+ afree((void*)*history, APERM);
+ for (hp = history; hp < history + histsize - 1; hp++)
+@@ -596,371 +589,125 @@ histsave(int lno, const char *cmd, int d
+ }
+ *hp = c;
+ histptr = hp;
+-}
+-
+-/*
+- * Write history data to a file nominated by HISTFILE
+- * if HISTFILE is unset then history still happens, but
+- * the data is not written to a file
+- * All copies of ksh looking at the file will maintain the
+- * same history. This is ksh behaviour.
+- *
+- * This stuff uses mmap()
+- * if your system ain't got it - then you'll have to undef HISTORYFILE
+- */
+
+-/*
+- * Open a history file
+- * Format is:
+- * Bytes 1, 2: HMAGIC - just to check that we are dealing with
+- * the correct object
+- * Then follows a number of stored commands
+- * Each command is
+- * <command byte><command number(4 bytes)><bytes><null>
+- */
+-#define HMAGIC1 0xab
+-#define HMAGIC2 0xcd
+-#define COMMAND 0xff
++ if (dowrite && f) {
++ writehistfile(f);
++ history_close(f);
++ }
++}
+
+-void
+-hist_init(Source *s)
++static FILE *
++history_open(int *changed)
+ {
+- unsigned char *base;
+- int lines;
+- int fd;
+-
+- if (Flag(FTALKING) == 0)
+- return;
+-
+- hstarted = 1;
+-
+- hist_source = s;
+-
+- hname = str_val(global("HISTFILE"));
+- if (hname == NULL)
+- return;
+- hname = str_save(hname, APERM);
++ int fd;
++ FILE *f = NULL;
++ struct stat sb;
+
+- retry:
+- /* we have a file and are interactive */
+- if ((fd = open(hname, O_RDWR|O_CREAT|O_APPEND, 0600)) < 0)
+- return;
+-
+- histfd = savefd(fd);
+- if (histfd != fd)
++ if ((fd = open(hname, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
++ return (NULL);
++ f = fdopen(fd, "r+");
++ if (f == NULL) {
+ close(fd);
++ goto bad;
++ }
+
+- (void) flock(histfd, LOCK_EX);
++ if (fstat(fileno(f), &sb) == -1)
++ goto bad;
++ if (timespeccmp(&sb.st_mtim, &last_sb.st_mtim, ==))
++ *changed = 0;
++ else
++ *changed = 1;
+
+- hsize = lseek(histfd, 0L, SEEK_END);
++ return (f);
++bad:
++ if (f)
++ fclose(f);
+
+- if (hsize == 0) {
+- /* add magic */
+- if (sprinkle(histfd)) {
+- hist_finish();
+- return;
+- }
+- }
+- else if (hsize > 0) {
+- /*
+- * we have some data
+- */
+- base = (unsigned char *)mmap(0, hsize, PROT_READ,
+- MAP_FILE|MAP_PRIVATE, histfd, 0);
+- /*
+- * check on its validity
+- */
+- if (base == MAP_FAILED || *base != HMAGIC1 || base[1] != HMAGIC2) {
+- if (base != MAP_FAILED)
+- munmap((caddr_t)base, hsize);
+- hist_finish();
+- if (unlink(hname) != 0)
+- return;
+- goto retry;
+- }
+- if (hsize > 2) {
+- lines = hist_count_lines(base+2, hsize-2);
+- if (lines > histsize) {
+- /* we need to make the file smaller */
+- if (hist_shrink(base, hsize))
+- if (unlink(hname) != 0)
+- return;
+- munmap((caddr_t)base, hsize);
+- hist_finish();
+- goto retry;
+- }
+- }
+- histload(hist_source, base+2, hsize-2);
+- munmap((caddr_t)base, hsize);
+- }
+- (void) flock(histfd, LOCK_UN);
+- hsize = lseek(histfd, 0L, SEEK_END);
++ return (NULL);
+ }
+
+-typedef enum state {
+- shdr, /* expecting a header */
+- sline, /* looking for a null byte to end the line */
+- sn1, /* bytes 1 to 4 of a line no */
+- sn2, sn3, sn4
+-} State;
++static void
++history_close(FILE *f)
++{
++ fflush(f);
++ fstat(fileno(f), &last_sb);
++ fclose(f);
++}
+
+ static int
+-hist_count_lines(unsigned char *base, int bytes)
++history_load(FILE *f, Source *s)
+ {
+- State state = shdr;
+- int lines = 0;
++ char *p, line[LINE + 1];
++ uint32_t i;
+
+- while (bytes--) {
+- switch (state) {
+- case shdr:
+- if (*base == COMMAND)
+- state = sn1;
++ /* just read it all; will auto resize history upon next command */
++ for (i = 1; ; i++) {
++ p = fgets(line, sizeof line, f);
++ if (p == NULL || feof(f) || ferror(f))
+ break;
+- case sn1:
+- state = sn2; break;
+- case sn2:
+- state = sn3; break;
+- case sn3:
+- state = sn4; break;
+- case sn4:
+- state = sline; break;
+- case sline:
+- if (*base == '\0')
+- lines++, state = shdr;
++ if ((p = strchr(line, '\n')) == NULL) {
++ bi_errorf("history file is corrupt");
++ return (1);
+ }
+- base++;
++ *p = '\0';
++
++ s->line = i;
++ s->cmd_offset = i;
++ histsave(i, (char *)line, 0);
+ }
+- return lines;
++
++ return (0);
+ }
+
+-/*
+- * Shrink the history file to histsize lines
+- */
+-static int
+-hist_shrink(unsigned char *oldbase, int oldbytes)
++void
++hist_init(Source *s)
+ {
+- int fd;
+- char nfile[1024];
+- struct stat statb;
+- unsigned char *nbase = oldbase;
+- int nbytes = oldbytes;
+-
+- nbase = hist_skip_back(nbase, &nbytes, histsize);
+- if (nbase == NULL)
+- return 1;
+- if (nbase == oldbase)
+- return 0;
+-
+- /*
+- * create temp file
+- */
+- (void) shf_snprintf(nfile, sizeof(nfile), "%s.%d", hname, procpid);
+- if ((fd = open(nfile, O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0)
+- return 1;
++ FILE *f = NULL;
++ int changed;
+
+- if (sprinkle(fd)) {
+- close(fd);
+- unlink(nfile);
+- return 1;
+- }
+- if (write(fd, nbase, nbytes) != nbytes) {
+- close(fd);
+- unlink(nfile);
+- return 1;
+- }
+- /*
+- * worry about who owns this file
+- */
+- if (fstat(histfd, &statb) >= 0)
+- fchown(fd, statb.st_uid, statb.st_gid);
+- close(fd);
++ if (Flag(FTALKING) == 0)
++ return;
+
+- /*
+- * rename
+- */
+- if (rename(nfile, hname) < 0)
+- return 1;
+- return 0;
+-}
++ hstarted = 1;
+
++ hist_source = s;
+
+-/*
+- * find a pointer to the data `no' back from the end of the file
+- * return the pointer and the number of bytes left
+- */
+-static unsigned char *
+-hist_skip_back(unsigned char *base, int *bytes, int no)
+-{
+- int lines = 0;
+- unsigned char *ep;
++ hname = str_val(global("HISTFILE"));
++ if (hname == NULL)
++ return;
++ hname = str_save(hname, APERM);
+
+- for (ep = base + *bytes; --ep > base; ) {
+- /* this doesn't really work: the 4 byte line number that is
+- * encoded after the COMMAND byte can itself contain the
+- * COMMAND byte....
+- */
+- for (; ep > base && *ep != COMMAND; ep--)
+- ;
+- if (ep == base)
+- break;
+- if (++lines == no) {
+- *bytes = *bytes - ((char *)ep - (char *)base);
+- return ep;
+- }
+- }
+- return NULL;
+-}
++ f = history_open(&changed);
++ if (f == NULL)
++ return;
+
+-/*
+- * load the history structure from the stored data
+- */
+-static void
+-histload(Source *s, unsigned char *base, int bytes)
+-{
+- State state;
+- int lno = 0;
+- unsigned char *line = NULL;
+-
+- for (state = shdr; bytes-- > 0; base++) {
+- switch (state) {
+- case shdr:
+- if (*base == COMMAND)
+- state = sn1;
+- break;
+- case sn1:
+- lno = (((*base)&0xff)<<24);
+- state = sn2;
+- break;
+- case sn2:
+- lno |= (((*base)&0xff)<<16);
+- state = sn3;
+- break;
+- case sn3:
+- lno |= (((*base)&0xff)<<8);
+- state = sn4;
+- break;
+- case sn4:
+- lno |= (*base)&0xff;
+- line = base+1;
+- state = sline;
+- break;
+- case sline:
+- if (*base == '\0') {
+- /* worry about line numbers */
+- if (histptr >= history && lno-1 != s->line) {
+- /* a replacement ? */
+- histinsert(s, lno, line);
+- }
+- else {
+- s->line = lno;
+- s->cmd_offset = lno;
+- histsave(lno, (char *)line, 0);
+- }
+- state = shdr;
+- }
+- }
+- }
++ history_load(f, s);
++ history_close(f);
+ }
+
+-/*
+- * Insert a line into the history at a specified number
+- */
+ static void
+-histinsert(Source *s, int lno, unsigned char *line)
++writehistfile(FILE *f)
+ {
+- char **hp;
++ int i;
++ char *cmd;
+
+- if (lno >= s->line-(histptr-history) && lno <= s->line) {
+- hp = &histptr[lno-s->line];
+- if (*hp)
+- afree((void*)*hp, APERM);
+- *hp = str_save((char *)line, APERM);
+- }
+-}
++ if (ftruncate(fileno(f), 0) == -1)
++ return;
++ rewind(f);
+
+-/*
+- * write a command to the end of the history file
+- * This *MAY* seem easy but it's also necessary to check
+- * that the history file has not changed in size.
+- * If it has - then some other shell has written to it
+- * and we should read those commands to update our history
+- */
+-static void
+-writehistfile(int lno, char *cmd)
+-{
+- int sizenow;
+- unsigned char *base;
+- unsigned char *new;
+- int bytes;
+- unsigned char hdr[5];
+-
+- (void) flock(histfd, LOCK_EX);
+- sizenow = lseek(histfd, 0L, SEEK_END);
+- if (sizenow != hsize) {
+- /*
+- * Things have changed
+- */
+- if (sizenow > hsize) {
+- /* someone has added some lines */
+- bytes = sizenow - hsize;
+- base = (unsigned char *)mmap(0, sizenow,
+- PROT_READ, MAP_FILE|MAP_PRIVATE, histfd, 0);
+- if (base == MAP_FAILED)
+- goto bad;
+- new = base + hsize;
+- if (*new != COMMAND) {
+- munmap((caddr_t)base, sizenow);
+- goto bad;
+- }
+- hist_source->line--;
+- histload(hist_source, new, bytes);
+- hist_source->line++;
+- lno = hist_source->line;
+- munmap((caddr_t)base, sizenow);
+- hsize = sizenow;
+- } else {
+- /* it has shrunk */
+- /* but to what? */
+- /* we'll give up for now */
+- goto bad;
+- }
++ for (i = 0; i < histsize; i++) {
++ cmd = history[i];
++ if (cmd == NULL)
++ break;
++ if (fprintf(f, "%s\n", cmd) == -1)
++ return;
+ }
+- /*
+- * we can write our bit now
+- */
+- hdr[0] = COMMAND;
+- hdr[1] = (lno>>24)&0xff;
+- hdr[2] = (lno>>16)&0xff;
+- hdr[3] = (lno>>8)&0xff;
+- hdr[4] = lno&0xff;
+- (void) write(histfd, hdr, 5);
+- (void) write(histfd, cmd, strlen(cmd)+1);
+- hsize = lseek(histfd, 0L, SEEK_END);
+- (void) flock(histfd, LOCK_UN);
+- return;
+-bad:
+- hist_finish();
+ }
+
+ void
+ hist_finish(void)
+ {
+- (void) flock(histfd, LOCK_UN);
+- (void) close(histfd);
+- histfd = 0;
+ }
+-
+-/*
+- * add magic to the history file
+- */
+-static int
+-sprinkle(int fd)
+-{
+- static unsigned char mag[] = { HMAGIC1, HMAGIC2 };
+-
+- return(write(fd, mag, 2) != 2);
+-}
+-
+ #else /* HISTORY */
+
+ /* No history to be compiled in: dummy routines to avoid lots more ifdefs */
+
diff --git a/system/ksh-openbsd/patches/09b-linux-new_history_implementation.diff b/system/ksh-openbsd/patches/09b-linux-new_history_implementation.diff
new file mode 100644
index 0000000000..06b0b03980
--- /dev/null
+++ b/system/ksh-openbsd/patches/09b-linux-new_history_implementation.diff
@@ -0,0 +1,36 @@
+Linux specific modifications:
+ - #include stdint.h for uint32_t
+ - #define timespeccmp() which is sys/time.h in OpenBSD
+ - Linux doesn't have any LOCK flags for open(2), so use flock(2) instead
+
+--- ../ksh-openbsd49/history.c 2011-09-01 08:09:51.486181269 +0200
++++ history.c 2011-09-01 08:08:59.657181265 +0200
+@@ -17,11 +17,17 @@
+ #include "sh.h"
+ #include <sys/stat.h>
+ #include "strlcat.h"
++#include "stdint.h"
+
+ #ifdef HISTORY
+ # include <sys/file.h>
+ # include <sys/mman.h>
+
++#define timespeccmp(tsp, usp, cmp) \
++ (((tsp)->tv_sec == (usp)->tv_sec) ? \
++ ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
++ ((tsp)->tv_sec cmp (usp)->tv_sec))
++
+ static void writehistfile(FILE *);
+ static FILE *history_open(int *);
+ static int history_load(FILE *, Source *);
+@@ -604,7 +610,9 @@
+ FILE *f = NULL;
+ struct stat sb;
+
+- if ((fd = open(hname, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
++ if ((fd = open(hname, O_RDWR | O_CREAT, 0600)) == -1)
++ return (NULL);
++ if (flock(fd, LOCK_EX) == -1)
+ return (NULL);
+ f = fdopen(fd, "r+");
+ if (f == NULL) {
diff --git a/system/ksh-openbsd/patches/10-ksh_mb_binds.diff b/system/ksh-openbsd/patches/10-ksh_mb_binds.diff
new file mode 100644
index 0000000000..7af3fd3ee6
--- /dev/null
+++ b/system/ksh-openbsd/patches/10-ksh_mb_binds.diff
@@ -0,0 +1,1115 @@
+From: Marco Peereboom
+Cc: OpenBSD tech
+On Fri, Sep 02, 2011 at 10:41:51AM +1000, Damien Miller wrote:
+> Hi,
+>
+> While people are excited about hacking on ksh(1) - let me add my wish:
+> unrestricted multibyte character binding so I can have ctrl-left_arrow
+> (^[[1;5D on my terminal) bound to backward-word and so forth.
+>
+> Last time I checked the code for bind could only handle a couple of
+> characters after ^[
+>
+> -d
+
+Here you go...
+
+---------------- 8< ----------------
+
+
+Index: Makefile
+===================================================================
+RCS file: /cvs/src/bin/ksh/Makefile,v
+retrieving revision 1.27
+diff -u -p -r1.27 Makefile
+--- Makefile.orig 2011-09-07 09:26:01.000000000 +0200
++++ Makefile 2011-09-07 09:32:19.217691920 +0200
+@@ -16,8 +16,6 @@
+ misc.o path.o shf.o syn.o table.o trap.o tree.o tty.o var.o \
+ version.o vi.o strlcpy.o strlcat.o
+
+-CLEANFILES+= emacs.out
+-
+ all: $(PROG)
+
+ CFLAGS+=-Wall
+@@ -31,12 +29,7 @@
+ install -m644 --no-target-directory ksh.1 $(MANDIR)/man1/pdksh.1
+
+ clean:
+- rm -f $(OBJS) $(PROG) emacs.out
+-
+-.depend emacs.o: emacs.out
+-
+-emacs.out: emacs.c
+- /bin/sh emacs-gen.sh emacs.c > emacs.out
++ rm -f $(OBJS) $(PROG)
+
+ check test:
+ /usr/bin/perl ${.CURDIR}/tests/th -s ${.CURDIR}/tests -p ./ksh -C \
+Index: emacs.c
+===================================================================
+RCS file: /cvs/src/bin/ksh/emacs.c,v
+retrieving revision 1.44
+diff -u -p -r1.44 emacs.c
+--- emacs.c 5 Sep 2011 04:50:33 -0000 1.44
++++ emacs.c 6 Sep 2011 22:36:35 -0000
+@@ -6,6 +6,9 @@
+ * created by Ron Natalie at BRL
+ * modified by Doug Kingston, Doug Gwyn, and Lou Salkind
+ * adapted to PD ksh by Eric Gisin
++ *
++ * partial rewrite by Marco Peereboom <marco@openbsd.org>
++ * under the same license
+ */
+
+ #include "config.h"
+@@ -13,6 +16,7 @@
+
+ #include "sh.h"
+ #include <sys/stat.h>
++#include <sys/queue.h>
+ #include <ctype.h>
+ #include <locale.h>
+ #include "edit.h"
+@@ -37,12 +41,6 @@ struct x_ftab {
+ short xf_flags;
+ };
+
+-struct x_defbindings {
+- u_char xdb_func; /* XFUNC_* */
+- unsigned char xdb_tab;
+- unsigned char xdb_char;
+-};
+-
+ #define XF_ARG 1 /* command takes number prefix */
+ #define XF_NOBIND 2 /* not allowed to bind to function */
+ #define XF_PREFIX 4 /* function sets prefix */
+@@ -51,10 +49,6 @@ struct x_defbindings {
+ #define is_cfs(c) (c == ' ' || c == '\t' || c == '"' || c == '\'')
+ #define is_mfs(c) (!(isalnum(c) || c == '_' || c == '$')) /* Separator for motion */
+
+-# define CHARMASK 0xFF /* 8-bit character mask */
+-# define X_NTABS 3 /* normal, meta1, meta2 */
+-#define X_TABSZ (CHARMASK+1) /* size of keydef tables etc */
+-
+ /* Arguments for do_complete()
+ * 0 = enumerate M-= complete as much as possible and then list
+ * 1 = complete M-Esc
+@@ -66,6 +60,17 @@ typedef enum {
+ CT_COMPLIST /* complete and then list (if non-exact) */
+ } Comp_type;
+
++/* keybindings */
++struct kb_entry {
++ TAILQ_ENTRY(kb_entry) entry;
++ unsigned char *seq;
++ int len;
++ struct x_ftab *ftab;
++ void *args;
++};
++TAILQ_HEAD(kb_list, kb_entry);
++struct kb_list kblist = TAILQ_HEAD_INITIALIZER(kblist);
++
+ /* { from 4.9 edit.h */
+ /*
+ * The following are used for my horizontal scrolling stuff
+@@ -91,20 +96,18 @@ static int x_arg_defaulted;/* x_arg not
+
+ static int xlp_valid;
+ /* end from 4.9 edit.h } */
++static int x_tty; /* are we on a tty? */
++static int x_bind_quiet; /* be quiet when binding keys */
++static int (*x_last_command)(int);
+
+-static int x_prefix1 = CTRL('['), x_prefix2 = CTRL('X');
+ static char **x_histp; /* history position */
+ static int x_nextcmd; /* for newline-and-next */
+ static char *xmp; /* mark pointer */
+-static u_char x_last_command;
+-static u_char (*x_tab)[X_TABSZ]; /* key definition */
+-static char *(*x_atab)[X_TABSZ]; /* macro definitions */
+-static unsigned char x_bound[(X_TABSZ * X_NTABS + 7) / 8];
+ #define KILLSIZE 20
+ static char *killstack[KILLSIZE];
+ static int killsp, killtp;
+-static int x_curprefix;
+-static char *macroptr;
++static int x_literal_set;
++static int x_arg_set;
+ static int prompt_skip;
+ static int prompt_redraw;
+
+@@ -123,9 +126,6 @@ static int x_search(char *, int, in
+ static int x_match(char *, char *);
+ static void x_redraw(int);
+ static void x_push(int);
+-static char * x_mapin(const char *, Area *);
+-static char * x_mapout(int);
+-static void x_print(int, int);
+ static void x_adjust(void);
+ static void x_e_ungetc(int);
+ static int x_e_getc(void);
+@@ -137,18 +137,66 @@ static char *x_lastcp(void);
+ static void do_complete(int, Comp_type);
+ static int x_emacs_putbuf(const char *, size_t);
+
++/* proto's for keybindings */
++static int x_abort(int);
++static int x_beg_hist(int);
++static int x_comp_comm(int);
++static int x_comp_file(int);
++static int x_complete(int);
++static int x_del_back(int);
++static int x_del_bword(int);
++static int x_del_char(int);
++static int x_del_fword(int);
++static int x_del_line(int);
++static int x_draw_line(int);
++static int x_end_hist(int);
++static int x_end_of_text(int);
++static int x_enumerate(int);
++static int x_eot_del(int);
++static int x_error(int);
++static int x_goto_hist(int);
++static int x_ins_string(int);
++static int x_insert(int);
++static int x_kill(int);
++static int x_kill_region(int);
++static int x_list_comm(int);
++static int x_list_file(int);
++static int x_literal(int);
++static int x_meta_yank(int);
++static int x_mv_back(int);
++static int x_mv_begin(int);
++static int x_mv_bword(int);
++static int x_mv_end(int);
++static int x_mv_forw(int);
++static int x_mv_fword(int);
++static int x_newline(int);
++static int x_next_com(int);
++static int x_nl_next_com(int);
++static int x_noop(int);
++static int x_prev_com(int);
++static int x_prev_histword(int);
++static int x_search_char_forw(int);
++static int x_search_char_back(int);
++static int x_search_hist(int);
++static int x_set_mark(int);
++static int x_stuff(int);
++static int x_stuffreset(int);
++static int x_transpose(int);
++static int x_version(int);
++static int x_xchg_point_mark(int);
++static int x_yank(int);
++static int x_comp_list(int);
++static int x_expand(int);
++static int x_fold_capitalize(int);
++static int x_fold_lower(int);
++static int x_fold_upper(int);
++static int x_set_arg(int);
++static int x_comment(int);
++#ifdef DEBUG
++static int x_debug_info(int);
++#endif
+
+-/* The lines between START-FUNC-TAB .. END-FUNC-TAB are run through a
+- * script (emacs-gen.sh) that generates emacs.out which contains:
+- * - function declarations for x_* functions
+- * - defines of the form XFUNC_<name> where <name> is function
+- * name, sans leading x_.
+- * Note that the script treats #ifdef and { 0, 0, 0} specially - use with
+- * caution.
+- */
+-#include "emacs.out"
+ static const struct x_ftab x_ftab[] = {
+-/* @START-FUNC-TAB@ */
+ { x_abort, "abort", 0 },
+ { x_beg_hist, "beginning-of-history", 0 },
+ { x_comp_comm, "complete-command", 0 },
+@@ -173,8 +221,6 @@ static const struct x_ftab x_ftab[] = {
+ { x_list_comm, "list-command", 0 },
+ { x_list_file, "list-file", 0 },
+ { x_literal, "quote", 0 },
+- { x_meta1, "prefix-1", XF_PREFIX },
+- { x_meta2, "prefix-2", XF_PREFIX },
+ { x_meta_yank, "yank-pop", 0 },
+ { x_mv_back, "backward-char", XF_ARG },
+ { x_mv_begin, "beginning-of-line", 0 },
+@@ -212,111 +258,22 @@ static const struct x_ftab x_ftab[] = {
+ { 0, 0, 0 },
+ #endif
+ { 0, 0, 0 },
+-/* @END-FUNC-TAB@ */
+-};
+-
+-static struct x_defbindings const x_defbindings[] = {
+- { XFUNC_del_back, 0, CTRL('?') },
+- { XFUNC_del_bword, 1, CTRL('?') },
+- { XFUNC_eot_del, 0, CTRL('D') },
+- { XFUNC_del_back, 0, CTRL('H') },
+- { XFUNC_del_bword, 1, CTRL('H') },
+- { XFUNC_del_bword, 1, 'h' },
+- { XFUNC_mv_bword, 1, 'b' },
+- { XFUNC_mv_fword, 1, 'f' },
+- { XFUNC_del_fword, 1, 'd' },
+- { XFUNC_mv_back, 0, CTRL('B') },
+- { XFUNC_mv_forw, 0, CTRL('F') },
+- { XFUNC_search_char_forw, 0, CTRL(']') },
+- { XFUNC_search_char_back, 1, CTRL(']') },
+- { XFUNC_newline, 0, CTRL('M') },
+- { XFUNC_newline, 0, CTRL('J') },
+- { XFUNC_end_of_text, 0, CTRL('_') },
+- { XFUNC_abort, 0, CTRL('G') },
+- { XFUNC_prev_com, 0, CTRL('P') },
+- { XFUNC_next_com, 0, CTRL('N') },
+- { XFUNC_nl_next_com, 0, CTRL('O') },
+- { XFUNC_search_hist, 0, CTRL('R') },
+- { XFUNC_beg_hist, 1, '<' },
+- { XFUNC_end_hist, 1, '>' },
+- { XFUNC_goto_hist, 1, 'g' },
+- { XFUNC_mv_end, 0, CTRL('E') },
+- { XFUNC_mv_begin, 0, CTRL('A') },
+- { XFUNC_draw_line, 0, CTRL('L') },
+- { XFUNC_meta1, 0, CTRL('[') },
+- { XFUNC_meta2, 0, CTRL('X') },
+- { XFUNC_kill, 0, CTRL('K') },
+- { XFUNC_yank, 0, CTRL('Y') },
+- { XFUNC_meta_yank, 1, 'y' },
+- { XFUNC_literal, 0, CTRL('^') },
+- { XFUNC_comment, 1, '#' },
+-#if defined(TIOCSTI)
+- { XFUNC_stuff, 0, CTRL('T') },
+-#else
+- { XFUNC_transpose, 0, CTRL('T') },
+-#endif
+- { XFUNC_complete, 1, CTRL('[') },
+- { XFUNC_comp_list, 0, CTRL('I') },
+- { XFUNC_comp_list, 1, '=' },
+- { XFUNC_enumerate, 1, '?' },
+- { XFUNC_expand, 1, '*' },
+- { XFUNC_comp_file, 1, CTRL('X') },
+- { XFUNC_comp_comm, 2, CTRL('[') },
+- { XFUNC_list_comm, 2, '?' },
+- { XFUNC_list_file, 2, CTRL('Y') },
+- { XFUNC_set_mark, 1, ' ' },
+- { XFUNC_kill_region, 0, CTRL('W') },
+- { XFUNC_xchg_point_mark, 2, CTRL('X') },
+- { XFUNC_literal, 0, CTRL('V') },
+-#ifdef DEBUG
+- { XFUNC_debug_info, 1, CTRL('H') },
+-#endif
+- { XFUNC_prev_histword, 1, '.' },
+- { XFUNC_prev_histword, 1, '_' },
+- { XFUNC_set_arg, 1, '0' },
+- { XFUNC_set_arg, 1, '1' },
+- { XFUNC_set_arg, 1, '2' },
+- { XFUNC_set_arg, 1, '3' },
+- { XFUNC_set_arg, 1, '4' },
+- { XFUNC_set_arg, 1, '5' },
+- { XFUNC_set_arg, 1, '6' },
+- { XFUNC_set_arg, 1, '7' },
+- { XFUNC_set_arg, 1, '8' },
+- { XFUNC_set_arg, 1, '9' },
+- { XFUNC_fold_upper, 1, 'U' },
+- { XFUNC_fold_upper, 1, 'u' },
+- { XFUNC_fold_lower, 1, 'L' },
+- { XFUNC_fold_lower, 1, 'l' },
+- { XFUNC_fold_capitalize, 1, 'C' },
+- { XFUNC_fold_capitalize, 1, 'c' },
+- /* These for ansi arrow keys: arguablely shouldn't be here by
+- * default, but its simpler/faster/smaller than using termcap
+- * entries.
+- */
+- { XFUNC_meta2, 1, '[' },
+- { XFUNC_meta2, 1, 'O' },
+- { XFUNC_prev_com, 2, 'A' },
+- { XFUNC_next_com, 2, 'B' },
+- { XFUNC_mv_forw, 2, 'C' },
+- { XFUNC_mv_back, 2, 'D' },
+ };
+
+ int
+ x_emacs(char *buf, size_t len)
+ {
+- int c;
+- const char *p;
+- int i;
+- u_char f;
++ struct kb_entry *k, *kmatch = NULL;
++ char line[LINE + 1];
++ int at = 0, submatch, ret, c;
++ const char *p;
+
+ xbp = xbuf = buf; xend = buf + len;
+ xlp = xcp = xep = buf;
+ *xcp = 0;
+ xlp_valid = true;
+ xmp = NULL;
+- x_curprefix = 0;
+ x_histp = histptr + 1;
+- x_last_command = XFUNC_error;
+
+ xx_cols = x_cols;
+ x_col = promptlen(prompt, &p);
+@@ -343,42 +300,91 @@ x_emacs(char *buf, size_t len)
+ x_nextcmd = -1;
+ }
+
++ line[0] = '\0';
++ x_literal_set = 0;
++ x_arg = -1;
++ x_last_command = NULL;
+ while (1) {
+ x_flush();
+ if ((c = x_e_getc()) < 0)
+ return 0;
+
+- if (ISMETA(c)) {
+- c = META(c);
+- x_curprefix = 1;
++ line[at++] = c;
++ line[at] = '\0';
++
++ if (x_arg == -1) {
++ x_arg = 1;
++ x_arg_defaulted = 1;
+ }
+
+- f = x_curprefix == -1 ? XFUNC_insert :
+- x_tab[x_curprefix][c&CHARMASK];
++ if (x_literal_set) {
++ /* literal, so insert it */
++ x_literal_set = 0;
++ submatch = 0;
++ } else {
++ submatch = 0;
++ kmatch = NULL;
++ TAILQ_FOREACH(k, &kblist, entry) {
++ if (at > k->len)
++ continue;
+
+- if (macroptr && f == XFUNC_ins_string)
+- f = XFUNC_insert;
++ if (!bcmp(k->seq, line, at)) {
++ /* sub match */
++ submatch++;
++ if (k->len == at)
++ kmatch = k;
++ }
+
+- if (!(x_ftab[f].xf_flags & XF_PREFIX) &&
+- x_last_command != XFUNC_set_arg) {
+- x_arg = 1;
+- x_arg_defaulted = 1;
++ /* see if we can abort search early */
++ if (submatch > 1)
++ break;
++ }
++ }
++
++ if (submatch == 1 && kmatch) {
++ if (kmatch->ftab->xf_func == x_ins_string &&
++ kmatch->args) {
++ /* insert macro string */
++ x_ins(kmatch->args);
++ }
++ ret = kmatch->ftab->xf_func(c);
++ } else {
++ if (submatch)
++ continue;
++ if (at == 1)
++ ret = x_insert(c);
++ else
++ ret = x_error(c); /* not matched meta sequence */
+ }
+- i = c | (x_curprefix << 8);
+- x_curprefix = 0;
+- switch (i = (*x_ftab[f].xf_func)(i)) {
++
++ switch (ret) {
+ case KSTD:
+- if (!(x_ftab[f].xf_flags & XF_PREFIX))
+- x_last_command = f;
++ if (kmatch)
++ x_last_command = kmatch->ftab->xf_func;
++ else
++ x_last_command = NULL;
+ break;
+ case KEOL:
+- i = xep - xbuf;
+- return i;
+- case KINTR: /* special case for interrupt */
++ ret = xep - xbuf;
++ return (ret);
++ break;
++ case KINTR:
+ trapsig(SIGINT);
+ x_mode(false);
+ unwind(LSHELL);
++ x_arg = -1;
++ break;
++ default:
++ bi_errorf("invalid return code"); /* can't happen */
+ }
++
++ /* reset meta sequence */
++ at = 0;
++ line[0] = '\0';
++ if (x_arg_set)
++ x_arg_set = 0; /* reset args next time around */
++ else
++ x_arg = -1;
+ }
+ }
+
+@@ -404,15 +410,6 @@ x_insert(int c)
+ static int
+ x_ins_string(int c)
+ {
+- if (macroptr) {
+- x_e_putc(BEL);
+- return KSTD;
+- }
+- macroptr = x_atab[c>>8][c & CHARMASK];
+- if (macroptr && !*macroptr) {
+- /* XXX bell? */
+- macroptr = (char *) 0;
+- }
+ return KSTD;
+ }
+
+@@ -849,6 +846,21 @@ x_eot_del(int c)
+ return (x_del_char(c));
+ }
+
++static void *
++kb_find_hist_func(char c)
++{
++ struct kb_entry *k;
++ char line[LINE + 1];
++
++ line[0] = c;
++ line[1] = '\0';
++ TAILQ_FOREACH(k, &kblist, entry)
++ if (!strcmp(k->seq, line))
++ return (k->ftab->xf_func);
++
++ return (x_insert);
++}
++
+ /* reverse incremental history search */
+ static int
+ x_search_hist(int c)
+@@ -856,7 +868,7 @@ x_search_hist(int c)
+ int offset = -1; /* offset of match in xbuf, else -1 */
+ char pat [256+1]; /* pattern buffer */
+ char *p = pat;
+- u_char f;
++ int (*f)(int);
+
+ *p = '\0';
+ while (1) {
+@@ -867,12 +879,12 @@ x_search_hist(int c)
+ x_flush();
+ if ((c = x_e_getc()) < 0)
+ return KSTD;
+- f = x_tab[0][c&CHARMASK];
++ f = kb_find_hist_func(c);
+ if (c == CTRL('['))
+ break;
+- else if (f == XFUNC_search_hist)
++ else if (f == x_search_hist)
+ offset = x_search(pat, 0, offset);
+- else if (f == XFUNC_del_back) {
++ else if (f == x_del_back) {
+ if (p == pat) {
+ offset = -1;
+ break;
+@@ -884,7 +896,7 @@ x_search_hist(int c)
+ else
+ offset = x_search(pat, 1, offset);
+ continue;
+- } else if (f == XFUNC_insert) {
++ } else if (f == x_insert) {
+ /* add char to pattern */
+ /* overflow check... */
+ if (p >= &pat[sizeof(pat) - 1]) {
+@@ -1104,21 +1116,7 @@ x_transpose(int c)
+ static int
+ x_literal(int c)
+ {
+- x_curprefix = -1;
+- return KSTD;
+-}
+-
+-static int
+-x_meta1(int c)
+-{
+- x_curprefix = 1;
+- return KSTD;
+-}
+-
+-static int
+-x_meta2(int c)
+-{
+- x_curprefix = 2;
++ x_literal_set = 1;
+ return KSTD;
+ }
+
+@@ -1174,7 +1172,7 @@ static int
+ x_meta_yank(int c)
+ {
+ int len;
+- if ((x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank) ||
++ if ((x_last_command != x_yank && x_last_command != x_meta_yank) ||
+ killstack[killtp] == 0) {
+ killtp = killsp;
+ x_e_puts("\nyank something first");
+@@ -1242,175 +1240,231 @@ x_stuff(int c)
+ }
+
+ static char *
+-x_mapin(const char *cp, Area *area)
++kb_encode(const char *s)
+ {
+- char *new, *op;
++ static char l[LINE + 1];
++ int at = 0;
+
+- op = new = str_save(cp, area);
+- while (*cp) {
+- /* XXX -- should handle \^ escape? */
+- if (*cp == '^') {
+- cp++;
+- if (*cp >= '?') /* includes '?'; ASCII */
+- *op++ = CTRL(*cp);
++ l[at] = '\0';
++ while (*s) {
++ if (*s == '^') {
++ s++;
++ if (*s >= '?')
++ l[at++] = CTRL(*s);
+ else {
+- *op++ = '^';
+- cp--;
++ l[at++] = '^';
++ s--;
+ }
+ } else
+- *op++ = *cp;
+- cp++;
++ l[at++] = *s;
++ l[at] = '\0';
++ s++;
+ }
+- *op = '\0';
+-
+- return new;
++ return (l);
+ }
+
+ static char *
+-x_mapout(int c)
++kb_decode(const char *s)
+ {
+- static char buf[8];
+- char *p = buf;
++ static char l[LINE + 1];
++ int i, at = 0;
+
+- if (iscntrl(c)) {
+- *p++ = '^';
+- *p++ = UNCTRL(c);
+- } else
+- *p++ = c;
+- *p = 0;
+- return buf;
++ l[0] = '\0';
++ for (i = 0; i < strlen(s); i++) {
++ if (iscntrl(s[i])) {
++ l[at++] = '^';
++ l[at++] = UNCTRL(s[i]);
++ } else
++ l[at++] = s[i];
++ l[at] = '\0';
++ }
++
++ return (l);
++}
++
++static int
++kb_match(char *s)
++{
++ int len = strlen(s);
++ struct kb_entry *k;
++
++ TAILQ_FOREACH(k, &kblist, entry) {
++ if (len > k->len)
++ continue;
++
++ if (!bcmp(k->seq, s, len))
++ return (1);
++ }
++
++ return (0);
+ }
+
+ static void
+-x_print(int prefix, int key)
++kb_del(struct kb_entry *k)
+ {
+- if (prefix == 1)
+- shprintf("%s", x_mapout(x_prefix1));
+- if (prefix == 2)
+- shprintf("%s", x_mapout(x_prefix2));
+- shprintf("%s = ", x_mapout(key));
+- if (x_tab[prefix][key] != XFUNC_ins_string)
+- shprintf("%s\n", x_ftab[x_tab[prefix][key]].xf_name);
+- else
+- shprintf("'%s'\n", x_atab[prefix][key]);
++ TAILQ_REMOVE(&kblist, k, entry);
++ if (k->args)
++ free(k->args);
++ afree(k, AEDIT);
++}
++
++static struct kb_entry *
++kb_add_string(void *func, void *args, char *str)
++{
++ int i, count;
++ struct kb_entry *k;
++ struct x_ftab *xf = NULL;
++
++ for (i = 0; i < NELEM(x_ftab); i++)
++ if (x_ftab[i].xf_func == func) {
++ xf = (struct x_ftab *)&x_ftab[i];
++ break;
++ }
++ if (xf == NULL)
++ return (NULL);
++
++ if (kb_match(str)) {
++ if (x_bind_quiet == 0)
++ bi_errorf("duplicate binding for %s", kb_decode(str));
++ return (NULL);
++ }
++ count = strlen(str);
++
++ k = alloc(sizeof *k + count + 1, AEDIT);
++ k->seq = (unsigned char *)(k + 1);
++ k->len = count;
++ k->ftab = xf;
++ k->args = args ? strdup(args) : NULL;
++
++ strlcpy(k->seq, str, count + 1);
++
++ TAILQ_INSERT_TAIL(&kblist, k, entry);
++
++ return (k);
++}
++
++static struct kb_entry *
++kb_add(void *func, void *args, ...)
++{
++ va_list ap;
++ int i, count;
++ char l[LINE + 1];
++
++ va_start(ap, args);
++ count = 0;
++ while (va_arg(ap, unsigned int) != 0)
++ count++;
++ va_end(ap);
++
++ va_start(ap, args);
++ for (i = 0; i <= count /* <= is correct */; i++)
++ l[i] = (unsigned char)va_arg(ap, unsigned int);
++ va_end(ap);
++
++ return (kb_add_string(func, args, l));
++}
++
++static void
++kb_print(struct kb_entry *k)
++{
++ if (!(k->ftab->xf_flags & XF_NOBIND))
++ shprintf("%s = %s\n",
++ kb_decode(k->seq), k->ftab->xf_name);
++ else if (k->args) {
++ shprintf("%s = ", kb_decode(k->seq));
++ shprintf("'%s'\n", kb_decode(k->args));
++ }
+ }
+
+ int
+-x_bind( const char *a1, const char *a2,
++x_bind(const char *a1, const char *a2,
+ int macro, /* bind -m */
+ int list) /* bind -l */
+ {
+- u_char f;
+- int prefix, key;
+- char *sp = NULL;
+- char *m1, *m2;
++ int i;
++ struct kb_entry *k;
++ char in[LINE + 1];
+
+- if (x_tab == NULL) {
++ if (x_tty == 0) {
+ bi_errorf("cannot bind, not a tty");
+- return 1;
++ return (1);
+ }
+
+- /* List function names */
+ if (list) {
+- for (f = 0; f < NELEM(x_ftab); f++)
+- if (x_ftab[f].xf_name &&
+- !(x_ftab[f].xf_flags & XF_NOBIND))
+- shprintf("%s\n", x_ftab[f].xf_name);
+- return 0;
++ /* show all function names */
++ for (i = 0; i < NELEM(x_ftab); i++) {
++ if (x_ftab[i].xf_name == NULL)
++ continue;
++ if (x_ftab[i].xf_name &&
++ !(x_ftab[i].xf_flags & XF_NOBIND))
++ shprintf("%s\n", x_ftab[i].xf_name);
++ }
++ return (0);
+ }
+
+ if (a1 == NULL) {
+- for (prefix = 0; prefix < X_NTABS; prefix++)
+- for (key = 0; key < X_TABSZ; key++) {
+- f = x_tab[prefix][key];
+- if (f == XFUNC_insert || f == XFUNC_error ||
+- (macro && f != XFUNC_ins_string))
+- continue;
+- x_print(prefix, key);
+- }
+- return 0;
+- }
+-
+- m2 = m1 = x_mapin(a1, ATEMP);
+- prefix = key = 0;
+- for (;; m1++) {
+- key = *m1 & CHARMASK;
+- if (x_tab[prefix][key] == XFUNC_meta1)
+- prefix = 1;
+- else if (x_tab[prefix][key] == XFUNC_meta2)
+- prefix = 2;
+- else
+- break;
++ /* show all bindings */
++ TAILQ_FOREACH(k, &kblist, entry)
++ kb_print(k);
++ return (0);
+ }
+- afree(m2, ATEMP);
+
++ snprintf(in, sizeof in, "%s", kb_encode(a1));
+ if (a2 == NULL) {
+- x_print(prefix, key);
+- return 0;
++ /* print binding */
++ TAILQ_FOREACH(k, &kblist, entry)
++ if (!strcmp(k->seq, in)) {
++ kb_print(k);
++ return (0);
++ }
++ shprintf("%s = %s\n", kb_decode(a1), "auto-insert");
++ return (0);
+ }
+
+- if (*a2 == 0)
+- f = XFUNC_insert;
+- else if (!macro) {
+- for (f = 0; f < NELEM(x_ftab); f++)
+- if (x_ftab[f].xf_name &&
+- strcmp(x_ftab[f].xf_name, a2) == 0)
+- break;
+- if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
+- bi_errorf("%s: no such function", a2);
+- return 1;
++ if (strlen(a2) == 0) {
++ /* clear binding */
++ TAILQ_FOREACH(k, &kblist, entry)
++ if (!strcmp(k->seq, in))
++ kb_del(k);
++ return (0);
++ }
++
++ /* set binding */
++ if (macro) {
++ /* delete old mapping */
++ TAILQ_FOREACH(k, &kblist, entry)
++ if (!strcmp(k->seq, in))
++ kb_del(k);
++ kb_add_string(x_ins_string, kb_encode(a2), in);
++ return (0);
++ }
++
++ /* set non macro binding */
++ for (i = 0; i < NELEM(x_ftab); i++) {
++ if (x_ftab[i].xf_name == NULL)
++ continue;
++ if (!strcmp(x_ftab[i].xf_name, a2)) {
++ /* delete old mapping */
++ TAILQ_FOREACH(k, &kblist, entry)
++ if (!strcmp(k->seq, in))
++ kb_del(k);
++ kb_add_string(x_ftab[i].xf_func, NULL, in);
++ return (0);
+ }
+-#if 0 /* This breaks the bind commands that map arrow keys */
+- if (f == XFUNC_meta1)
+- x_prefix1 = key;
+- if (f == XFUNC_meta2)
+- x_prefix2 = key;
+-#endif /* 0 */
+- } else {
+- f = XFUNC_ins_string;
+- sp = x_mapin(a2, AEDIT);
+ }
+-
+- if (x_tab[prefix][key] == XFUNC_ins_string && x_atab[prefix][key])
+- afree((void *)x_atab[prefix][key], AEDIT);
+- x_tab[prefix][key] = f;
+- x_atab[prefix][key] = sp;
+-
+- /* Track what the user has bound so x_emacs_keys() won't toast things */
+- if (f == XFUNC_insert)
+- x_bound[(prefix * X_TABSZ + key) / 8] &=
+- ~(1 << ((prefix * X_TABSZ + key) % 8));
+- else
+- x_bound[(prefix * X_TABSZ + key) / 8] |=
+- (1 << ((prefix * X_TABSZ + key) % 8));
+-
+- return 0;
++ bi_errorf("%s: no such function", a2);
++ return (1);
+ }
+
+ void
+ x_init_emacs(void)
+ {
+- int i, j;
+ char *locale;
+
++ x_tty = 1;
+ ainit(AEDIT);
+ x_nextcmd = -1;
+
+- x_tab = (u_char (*)[X_TABSZ]) alloc(sizeofN(*x_tab, X_NTABS), AEDIT);
+- for (j = 0; j < X_TABSZ; j++)
+- x_tab[0][j] = XFUNC_insert;
+- for (i = 1; i < X_NTABS; i++)
+- for (j = 0; j < X_TABSZ; j++)
+- x_tab[i][j] = XFUNC_error;
+- for (i = 0; i < NELEM(x_defbindings); i++)
+- x_tab[(unsigned char)x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
+- = x_defbindings[i].xdb_func;
+-
+- x_atab = (char *(*)[X_TABSZ]) alloc(sizeofN(*x_atab, X_NTABS), AEDIT);
+- for (i = 1; i < X_NTABS; i++)
+- for (j = 0; j < X_TABSZ; j++)
+- x_atab[i][j] = NULL;
+-
+ /* Determine if we can translate meta key or use 8-bit AscII
+ * XXX - It would be nice if there was a locale attribute to
+ * determine if the locale is 7-bit or not.
+@@ -1418,36 +1472,134 @@ x_init_emacs(void)
+ locale = setlocale(LC_CTYPE, NULL);
+ if (locale == NULL || !strcmp(locale, "C") || !strcmp(locale, "POSIX"))
+ Flag(FEMACSUSEMETA) = 1;
+-}
+
+-static void bind_if_not_bound(int p, int k, int func);
+-
+-static void
+-bind_if_not_bound(int p, int k, int func)
+-{
+- /* Has user already bound this key? If so, don't override it */
+- if (x_bound[((p) * X_TABSZ + (k)) / 8] &
+- (1 << (((p) * X_TABSZ + (k)) % 8)))
+- return;
++ /* new keybinding stuff */
++ TAILQ_INIT(&kblist);
+
+- x_tab[p][k] = func;
++ /* man page order */
++ kb_add(x_abort, NULL, CTRL('G'), 0);
++ kb_add(x_mv_back, NULL, CTRL('B'), 0);
++ kb_add(x_mv_back, NULL, CTRL('X'), CTRL('D'), 0);
++ kb_add(x_mv_bword, NULL, CTRL('['), 'b', 0);
++ kb_add(x_beg_hist, NULL, CTRL('['), '<', 0);
++ kb_add(x_mv_begin, NULL, CTRL('A'), 0);
++ kb_add(x_fold_capitalize, NULL, CTRL('['), 'C', 0);
++ kb_add(x_fold_capitalize, NULL, CTRL('['), 'c', 0);
++ kb_add(x_comment, NULL, CTRL('['), '#', 0);
++ kb_add(x_complete, NULL, CTRL('['), CTRL('['), 0);
++ kb_add(x_comp_comm, NULL, CTRL('X'), CTRL('['), 0);
++ kb_add(x_comp_file, NULL, CTRL('['), CTRL('X'), 0);
++ kb_add(x_comp_list, NULL, CTRL('I'), 0);
++ kb_add(x_comp_list, NULL, CTRL('['), '=', 0);
++ kb_add(x_del_back, NULL, CTRL('?'), 0);
++ kb_add(x_del_back, NULL, CTRL('H'), 0);
++ /* x_del_char not assigned by default */
++ kb_add(x_del_bword, NULL, CTRL('['), CTRL('?'), 0);
++ kb_add(x_del_bword, NULL, CTRL('['), CTRL('H'), 0);
++ kb_add(x_del_bword, NULL, CTRL('['), 'h', 0);
++ kb_add(x_del_fword, NULL, CTRL('['), 'd', 0);
++ kb_add(x_next_com, NULL, CTRL('N'), 0);
++ kb_add(x_next_com, NULL, CTRL('X'), 'B', 0);
++ kb_add(x_fold_lower, NULL, CTRL('['), 'L', 0);
++ kb_add(x_fold_lower, NULL, CTRL('['), 'l', 0);
++ kb_add(x_end_hist, NULL, CTRL('['), '>', 0);
++ kb_add(x_mv_end, NULL, CTRL('E'), 0);
++ /* how to handle: eot: ^_, underneath copied from original keybindings */
++ kb_add(x_end_of_text, NULL, CTRL('_'), 0);
++ kb_add(x_eot_del, NULL, CTRL('D'), 0);
++ /* error */
++ kb_add(x_xchg_point_mark, NULL, CTRL('X'), CTRL('X'), 0);
++ kb_add(x_expand, NULL, CTRL('['), '*', 0);
++ kb_add(x_mv_forw, NULL, CTRL('F'), 0);
++ kb_add(x_mv_forw, NULL, CTRL('X'), 'C', 0);
++ kb_add(x_mv_fword, NULL, CTRL('['), 'f', 0);
++ kb_add(x_goto_hist, NULL, CTRL('['), 'g', 0);
++ /* kill-line */
++ kb_add(x_del_bword, NULL, CTRL('W'), 0); /* not what man says */
++ kb_add(x_kill, NULL, CTRL('K'), 0);
++ kb_add(x_enumerate, NULL, CTRL('['), '?', 0);
++ kb_add(x_list_comm, NULL, CTRL('X'), '?', 0);
++ kb_add(x_list_file, NULL, CTRL('X'), CTRL('Y'), 0);
++ kb_add(x_newline, NULL, CTRL('J'), 0);
++ kb_add(x_newline, NULL, CTRL('M'), 0);
++ kb_add(x_nl_next_com, NULL, CTRL('O'), 0);
++ /* no-op */
++ kb_add(x_prev_histword, NULL, CTRL('['), '.', 0);
++ kb_add(x_prev_histword, NULL, CTRL('['), '_', 0);
++ /* how to handle: quote: ^^ */
++ kb_add(x_draw_line, NULL, CTRL('L'), 0);
++ kb_add(x_search_char_back, NULL, CTRL('['), CTRL(']'), 0);
++ kb_add(x_search_char_forw, NULL, CTRL(']'), 0);
++ kb_add(x_search_hist, NULL, CTRL('R'), 0);
++ kb_add(x_set_mark, NULL, CTRL('['), ' ', 0);
++#if defined(TIOCSTI)
++ kb_add(x_stuff, NULL, CTRL('T'), 0);
++ /* stuff-reset */
++#else
++ kb_add(x_transpose, NULL, CTRL('T'), 0);
++#endif
++ kb_add(x_prev_com, NULL, CTRL('P'), 0);
++ kb_add(x_prev_com, NULL, CTRL('X'), 'A', 0);
++ kb_add(x_fold_upper, NULL, CTRL('['), 'U', 0);
++ kb_add(x_fold_upper, NULL, CTRL('['), 'u', 0);
++ kb_add(x_literal, NULL, CTRL('V'), 0);
++ kb_add(x_literal, NULL, CTRL('^'), 0);
++ kb_add(x_yank, NULL, CTRL('Y'), 0);
++ kb_add(x_meta_yank, NULL, CTRL('['), 'y', 0);
++ /* man page ends here */
++
++ /* arrow keys */
++ kb_add(x_prev_com, NULL, CTRL('['), '[', 'A', 0); /* up */
++ kb_add(x_next_com, NULL, CTRL('['), '[', 'B', 0); /* down */
++ kb_add(x_mv_forw, NULL, CTRL('['), '[', 'C', 0); /* right */
++ kb_add(x_mv_back, NULL, CTRL('['), '[', 'D', 0); /* left */
++ kb_add(x_prev_com, NULL, CTRL('['), 'O', 'A', 0); /* up */
++ kb_add(x_next_com, NULL, CTRL('['), 'O', 'B', 0); /* down */
++ kb_add(x_mv_forw, NULL, CTRL('['), 'O', 'C', 0); /* right */
++ kb_add(x_mv_back, NULL, CTRL('['), 'O', 'D', 0); /* left */
++
++ /* more navigation keys */
++ kb_add(x_mv_begin, NULL, CTRL('['), '[', 'H', 0); /* home */
++ kb_add(x_mv_end, NULL, CTRL('['), '[', 'F', 0); /* end */
++ kb_add(x_mv_begin, NULL, CTRL('['), 'O', 'H', 0); /* home */
++ kb_add(x_mv_end, NULL, CTRL('['), 'O', 'F', 0); /* end */
++
++ /* can't be bound */
++ kb_add(x_set_arg, NULL, CTRL('['), '0', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '1', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '2', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '3', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '4', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '5', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '6', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '7', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '8', 0);
++ kb_add(x_set_arg, NULL, CTRL('['), '9', 0);
++
++ /* ctrl arrow keys */
++ kb_add(x_mv_end, NULL, CTRL('['), '[', '1', ';', '5', 'A', 0); /* ctrl up */
++ kb_add(x_mv_begin, NULL, CTRL('['), '[', '1', ';', '5', 'B', 0); /* ctrl down */
++ kb_add(x_mv_fword, NULL, CTRL('['), '[', '1', ';', '5', 'C', 0); /* ctrl right */
++ kb_add(x_mv_bword, NULL, CTRL('['), '[', '1', ';', '5', 'D', 0); /* ctrl left */
+ }
+
+ void
+ x_emacs_keys(X_chars *ec)
+ {
++ x_bind_quiet = 1;
+ if (ec->erase >= 0) {
+- bind_if_not_bound(0, ec->erase, XFUNC_del_back);
+- bind_if_not_bound(1, ec->erase, XFUNC_del_bword);
++ kb_add(x_del_back, NULL, ec->erase, 0);
++ kb_add(x_del_bword, NULL, CTRL('['), ec->erase, 0);
+ }
+ if (ec->kill >= 0)
+- bind_if_not_bound(0, ec->kill, XFUNC_del_line);
++ kb_add(x_del_line, NULL, ec->kill, 0);
+ if (ec->werase >= 0)
+- bind_if_not_bound(0, ec->werase, XFUNC_del_bword);
++ kb_add(x_del_bword, NULL, ec->werase, 0);
+ if (ec->intr >= 0)
+- bind_if_not_bound(0, ec->intr, XFUNC_abort);
++ kb_add(x_abort, NULL, ec->intr, 0);
+ if (ec->quit >= 0)
+- bind_if_not_bound(0, ec->quit, XFUNC_noop);
++ kb_add(x_noop, NULL, ec->quit, 0);
++ x_bind_quiet = 0;
+ }
+
+ static int
+@@ -1704,18 +1856,10 @@ x_e_getc(void)
+ if (unget_char >= 0) {
+ c = unget_char;
+ unget_char = -1;
+- } else {
+- if (macroptr) {
+- c = *macroptr++;
+- if (!c) {
+- macroptr = NULL;
+- c = x_getc();
+- }
+- } else
+- c = x_getc();
+- }
++ } else
++ c = x_getc();
+
+- return c <= CHARMASK ? c : (c & CHARMASK);
++ return c;
+ }
+
+ static void
+@@ -1787,7 +1931,6 @@
+ int n = 0;
+ int first = 1;
+
+- c &= CHARMASK; /* strip command prefix */
+ for (; c >= 0 && isdigit(c); c = x_e_getc(), first = 0) {
+ n = n * 10 + (c - '0');
+ if (n < 0 || n > LINE) {
+@@ -1802,6 +1945,7 @@ x_set_arg(int c)
+ x_e_ungetc(c);
+ x_arg = n;
+ x_arg_defaulted = 0;
++ x_arg_set = 1;
+ }
+ return KSTD;
+ }
+
diff --git a/system/ksh-openbsd/patches/linux/Makefile.diff b/system/ksh-openbsd/patches/linux/Makefile.diff
new file mode 100644
index 0000000000..7c9ca8f8ca
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/Makefile.diff
@@ -0,0 +1,58 @@
+--- ksh-openbsd_cvs/Makefile 2010-02-23 12:49:12.000000000 +0100
++++ ksh-openbsd_cvs.new/Makefile 2010-04-01 21:21:23.498239164 +0200
+@@ -1,28 +1,43 @@
+-# $OpenBSD: Makefile,v 1.27 2009/03/03 20:01:01 millert Exp $
++ifdef ${PREFIX}
++PREFIX=${PREFIX}
++else
++PREFIX=/usr
++endif
++BINDIR=${DESTDIR}/bin
++MANDIR=${DESTDIR}${PREFIX}/man
+
+ PROG= ksh
+ SRCS= alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c \
+ exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c mknod.c \
+ misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
+- version.c vi.c
+-
+-DEFS= -Wall
+-CFLAGS+=${DEFS} -I. -I${.CURDIR} -I${.CURDIR}/../../lib/libc/gen
+-MAN= ksh.1 sh.1
++ version.c vi.c strlcpy.c strlcat.c
++OBJS= alloc.o c_ksh.o c_sh.o c_test.o c_ulimit.o edit.o emacs.o eval.o \
++ exec.o expr.o history.o io.o jobs.o lex.o mail.o main.o mknod.o \
++ misc.o path.o shf.o syn.o table.o trap.o tree.o tty.o var.o \
++ version.o vi.o strlcpy.o strlcat.o
+
+ CLEANFILES+= emacs.out
+
+-LINKS= ${BINDIR}/ksh ${BINDIR}/rksh
+-LINKS+= ${BINDIR}/ksh ${BINDIR}/sh
+-MLINKS= ksh.1 rksh.1
++all: $(PROG)
++
++CFLAGS+=-Wall
++$(PROG): $(OBJS)
++ gcc $(OBJS) -o ksh
++
++install:
++ install -m755 -d $(BINDIR)
++ install -m755 --strip --no-target-directory ksh $(BINDIR)/pdksh
++ install -m755 -d $(MANDIR)/man1
++ install -m644 --no-target-directory ksh.1 $(MANDIR)/man1/pdksh.1
++
++clean:
++ rm -f $(OBJS) $(PROG) emacs.out
+
+ .depend emacs.o: emacs.out
+
+ emacs.out: emacs.c
+- /bin/sh ${.CURDIR}/emacs-gen.sh ${.CURDIR}/emacs.c > emacs.out
++ /bin/sh emacs-gen.sh emacs.c > emacs.out
+
+ check test:
+ /usr/bin/perl ${.CURDIR}/tests/th -s ${.CURDIR}/tests -p ./ksh -C \
+ pdksh,sh,ksh,posix,posix-upu
+-
+-.include <bsd.prog.mk>
diff --git a/system/ksh-openbsd/patches/linux/c_sh_c.diff b/system/ksh-openbsd/patches/linux/c_sh_c.diff
new file mode 100644
index 0000000000..09b74c7a0b
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/c_sh_c.diff
@@ -0,0 +1,49 @@
+--- ksh-openbsd_cvs/c_sh.c 2010-03-27 17:25:30.000000000 +0100
++++ ksh-openbsd_cvs.new/c_sh.c 2010-04-01 21:01:24.132137763 +0200
+@@ -837,19 +837,13 @@ c_mknod(char **wp)
+ {
+ int argc, optc, ismkfifo = 0, ret;
+ char **argv;
+- void *set = NULL;
+- mode_t mode = 0, oldmode = 0;
++ mode_t mode = 0, old_umask = -1;
+
+ while ((optc = ksh_getopt(wp, &builtin_opt, "m:")) != -1) {
+ switch (optc) {
+ case 'm':
+- set = setmode(builtin_opt.optarg);
+- if (set == NULL) {
+- bi_errorf("invalid file mode");
+- return 1;
+- }
+- mode = getmode(set, DEFFILEMODE);
+- free(set);
++ old_umask = umask(0);
++ mode |= strtoul(builtin_opt.optarg, NULL, 8);
+ break;
+ default:
+ goto usage;
+@@ -866,18 +860,17 @@ c_mknod(char **wp)
+ } else if (argc != 4)
+ goto usage;
+
+- if (set)
+- oldmode = umask(0);
+- else
+- mode = DEFFILEMODE;
++ if (old_umask == -1)
++ mode |= DEFFILEMODE;
+
+ if (ismkfifo)
+ ret = domkfifo(argc, argv, mode);
+ else
+ ret = domknod(argc, argv, mode);
+
+- if (set)
+- umask(oldmode);
++ if (old_umask != -1)
++ umask(old_umask);
++
+ return ret;
+ usage:
+ builtin_argv0 = NULL;
diff --git a/system/ksh-openbsd/patches/linux/charclass_h.diff b/system/ksh-openbsd/patches/linux/charclass_h.diff
new file mode 100644
index 0000000000..05e7957075
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/charclass_h.diff
@@ -0,0 +1,36 @@
+--- ksh-openbsd_cvs/charclass.h 1970-01-01 01:00:00.000000000 +0100
++++ ksh-openbsd_cvs.new/charclass.h 2010-04-01 21:01:24.132137763 +0200
+@@ -0,0 +1,33 @@
++/*
++ * Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
++ *
++ * $OpenBSD: charclass.h,v 1.1 2008/10/01 23:04:13 millert Exp $
++ */
++
++#include <ctype.h>
++
++#define isblank(c) __isctype((c), _ISblank)
++
++/*
++ * POSIX character class support for fnmatch() and glob().
++ */
++static struct cclass {
++ const char *name;
++ int (*isctype)(int);
++} cclasses[] = {
++ { "alnum", isalnum },
++ { "alpha", isalpha },
++// { "blank", isblank },
++ { "cntrl", iscntrl },
++ { "digit", isdigit },
++ { "graph", isgraph },
++ { "lower", islower },
++ { "print", isprint },
++ { "punct", ispunct },
++ { "space", isspace },
++ { "upper", isupper },
++ { "xdigit", isxdigit },
++ { NULL, NULL }
++};
++
++#define NCCLASSES (sizeof(cclasses) / sizeof(cclasses[0]) - 1)
diff --git a/system/ksh-openbsd/patches/linux/emacs_c.diff b/system/ksh-openbsd/patches/linux/emacs_c.diff
new file mode 100644
index 0000000000..4bf6c2a6bc
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/emacs_c.diff
@@ -0,0 +1,10 @@
+--- ksh-openbsd_cvs/emacs.c 2010-02-23 12:49:15.000000000 +0100
++++ ksh-openbsd_cvs.new/emacs.c 2010-04-01 21:01:24.133232595 +0200
+@@ -16,6 +16,7 @@
+ #include <ctype.h>
+ #include <locale.h>
+ #include "edit.h"
++#include "strlcpy.h"
+
+ static Area aedit;
+ #define AEDIT &aedit /* area for kill ring and macro defns */
diff --git a/system/ksh-openbsd/patches/linux/eval_c.diff b/system/ksh-openbsd/patches/linux/eval_c.diff
new file mode 100644
index 0000000000..9d580d8a63
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/eval_c.diff
@@ -0,0 +1,19 @@
+--- ksh.orig/eval.c.orig 2011-03-27 14:33:38.000000000 +0200
++++ ksh/eval.c 2011-03-27 15:08:25.986119913 +0200
+@@ -8,6 +8,7 @@
+ #include <pwd.h>
+ #include <dirent.h>
+ #include <sys/stat.h>
++#include "strlcpy.h"
+
+ /*
+ * string expansion
+@@ -692,7 +693,7 @@
+ int c;
+ int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */
+ int stype; /* substitution type */
+- int slen;
++ int slen = 0;
+ char *p;
+ struct tbl *vp;
+
diff --git a/system/ksh-openbsd/patches/linux/history_c.diff b/system/ksh-openbsd/patches/linux/history_c.diff
new file mode 100644
index 0000000000..674e33781e
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/history_c.diff
@@ -0,0 +1,10 @@
+--- ksh-openbsd_cvs/history.c 2010-02-23 12:49:16.000000000 +0100
++++ ksh-openbsd_cvs.new/history.c 2010-04-01 21:01:24.133232595 +0200
+@@ -17,6 +17,7 @@
+
+ #include "sh.h"
+ #include <sys/stat.h>
++#include "strlcat.h"
+
+ #ifdef HISTORY
+ # include <sys/file.h>
diff --git a/system/ksh-openbsd/patches/linux/jobs_c.diff b/system/ksh-openbsd/patches/linux/jobs_c.diff
new file mode 100644
index 0000000000..e50ffd3a94
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/jobs_c.diff
@@ -0,0 +1,22 @@
+--- ksh-openbsd_cvs/jobs.c 2010-02-23 12:49:17.000000000 +0100
++++ ksh-openbsd_cvs.new/jobs.c 2010-04-01 21:01:24.133232595 +0200
+@@ -21,6 +21,19 @@
+ #include <sys/time.h>
+ #include <sys/resource.h>
+ #include "tty.h"
++#include "strlcpy.h"
++
++#ifndef CHILD_MAX
++# if defined(HAVE_SYSCONF) && defined(_SC_CHILD_MAX)
++# define CHILD_MAX sysconf(_SC_CHILD_MAX)
++# else /* _SC_CHILD_MAX */
++# ifdef _POSIX_CHILD_MAX
++# define CHILD_MAX ((_POSIX_CHILD_MAX) * 2)
++# else /* _POSIX_CHILD_MAX */
++# define CHILD_MAX 20
++# endif /* _POSIX_CHILD_MAX */
++# endif /* _SC_CHILD_MAX */
++#endif /* !CHILD_MAX */
+
+ /* Order important! */
+ #define PRUNNING 0
diff --git a/system/ksh-openbsd/patches/linux/lex_c.diff b/system/ksh-openbsd/patches/linux/lex_c.diff
new file mode 100644
index 0000000000..40fd4234f1
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/lex_c.diff
@@ -0,0 +1,11 @@
+--- ksh-openbsd_cvs/lex.c 2010-02-23 12:49:19.000000000 +0100
++++ ksh-openbsd_cvs.new/lex.c 2010-04-01 21:01:24.134232647 +0200
+@@ -7,6 +7,8 @@
+ #include "sh.h"
+ #include <libgen.h>
+ #include <ctype.h>
++#include <time.h>
++#include "strlcpy.h"
+
+
+ /* Structure to keep track of the lexing state and the various pieces of info
diff --git a/system/ksh-openbsd/patches/linux/main_c.diff b/system/ksh-openbsd/patches/linux/main_c.diff
new file mode 100644
index 0000000000..a5d90620c1
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/main_c.diff
@@ -0,0 +1,15 @@
+--- ksh-openbsd_cvs/main.c 2010-02-23 12:49:19.000000000 +0100
++++ ksh-openbsd_cvs.new/main.c 2010-04-01 21:01:24.134232647 +0200
+@@ -9,7 +9,12 @@
+ #include "sh.h"
+ #include <sys/stat.h>
+ #include <pwd.h>
++#include "strlcpy.h"
+
++/* on Linux we do not have _PW_NAME_LEN, but libc6 allows us 32 character usernames */
++#ifndef _PW_NAME_LEN
++# define _PW_NAME_LEN 31
++#endif
+ extern char **environ;
+
+ /*
diff --git a/system/ksh-openbsd/patches/linux/misc_c.diff b/system/ksh-openbsd/patches/linux/misc_c.diff
new file mode 100644
index 0000000000..9b09fba62c
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/misc_c.diff
@@ -0,0 +1,12 @@
+--- ksh-openbsd_cvs/misc.c 2010-02-23 12:49:20.000000000 +0100
++++ ksh-openbsd_cvs.new/misc.c 2010-04-01 21:01:24.134232647 +0200
+@@ -7,7 +7,9 @@
+ #include "sh.h"
+ #include <ctype.h>
+ #include <sys/param.h> /* for MAXPATHLEN */
++#include <grp.h>
+ #include "charclass.h"
++#include "strlcpy.h"
+
+ short ctypes [UCHAR_MAX+1]; /* type bits for unsigned char */
+
diff --git a/system/ksh-openbsd/patches/linux/posix_signals_h.diff b/system/ksh-openbsd/patches/linux/posix_signals_h.diff
new file mode 100644
index 0000000000..2d6b8cec4d
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/posix_signals_h.diff
@@ -0,0 +1,128 @@
+--- ksh-openbsd_cvs/posix_signals.h 1970-01-01 01:00:00.000000000 +0100
++++ ksh-openbsd_cvs.new/posix_signals.h 2010-04-01 21:01:24.134232647 +0200
+@@ -0,0 +1,125 @@
++/*
++ * Copyright (c) 1988, 1993, 1994
++ * The Regents of the University of California. All rights reserved.
++ *
++ * Redistribution and use in source and binary forms, with or without
++ * modification, are permitted provided that the following conditions
++ * are met:
++ * 1. Redistributions of source code must retain the above copyright
++ * notice, this list of conditions and the following disclaimer.
++ * 2. Redistributions in binary form must reproduce the above copyright
++ * notice, this list of conditions and the following disclaimer in the
++ * documentation and/or other materials provided with the distribution.
++ * 3. All advertising materials mentioning features or use of this software
++ * must display the following acknowledgement:
++ * This product includes software developed by the University of
++ * California, Berkeley and its contributors.
++ * 4. Neither the name of the University nor the names of its contributors
++ * may be used to endorse or promote products derived from this software
++ * without specific prior written permission.
++ *
++ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
++ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
++ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++ * SUCH DAMAGE.
++ */
++/*
++ Taken from misc-utils/kill.c in the util-linux-ng package.
++ ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/
++ */
++
++#ifndef POSIX_SIGNALS_H
++#define POSIX_SIGNALS_H
++
++struct signv {
++ char *name;
++ int val;
++} sys_signame[NSIG] = {
++ /* POSIX signals */
++ { "HUP", SIGHUP }, /* 1 */
++ { "INT", SIGINT }, /* 2 */
++ { "QUIT", SIGQUIT }, /* 3 */
++ { "ILL", SIGILL }, /* 4 */
++ { "ABRT", SIGABRT }, /* 6 */
++ { "FPE", SIGFPE }, /* 8 */
++ { "KILL", SIGKILL }, /* 9 */
++ { "SEGV", SIGSEGV }, /* 11 */
++ { "PIPE", SIGPIPE }, /* 13 */
++ { "ALRM", SIGALRM }, /* 14 */
++ { "TERM", SIGTERM }, /* 15 */
++ { "USR1", SIGUSR1 }, /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
++ { "USR2", SIGUSR2 }, /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
++ { "CHLD", SIGCHLD }, /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
++ { "CONT", SIGCONT }, /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
++ { "STOP", SIGSTOP }, /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
++ { "TSTP", SIGTSTP }, /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
++ { "TTIN", SIGTTIN }, /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
++ { "TTOU", SIGTTOU }, /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
++ /* Miscellaneous other signals */
++#ifdef SIGTRAP
++ { "TRAP", SIGTRAP }, /* 5 */
++#endif
++#ifdef SIGIOT
++ { "IOT", SIGIOT }, /* 6, same as SIGABRT */
++#endif
++#ifdef SIGEMT
++ { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
++#endif
++#ifdef SIGBUS
++ { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
++#endif
++#ifdef SIGSYS
++ { "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */
++#endif
++#ifdef SIGSTKFLT
++ { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
++#endif
++#ifdef SIGURG
++ { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
++#endif
++#ifdef SIGIO
++ { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
++#endif
++#ifdef SIGPOLL
++ { "POLL", SIGPOLL }, /* same as SIGIO */
++#endif
++#ifdef SIGCLD
++ { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
++#endif
++#ifdef SIGXCPU
++ { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
++#endif
++#ifdef SIGXFSZ
++ { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
++#endif
++#ifdef SIGVTALRM
++ { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
++#endif
++#ifdef SIGPROF
++ { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
++#endif
++#ifdef SIGPWR
++ { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
++#endif
++#ifdef SIGINFO
++ { "INFO", SIGINFO }, /* 29 (alpha) */
++#endif
++#ifdef SIGLOST
++ { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
++#endif
++#ifdef SIGWINCH
++ { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
++#endif
++#ifdef SIGUNUSED
++ { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
++#endif
++}; /* sys_signame[] */
++
++#endif /* POSIX_SIGNALS_H */
diff --git a/system/ksh-openbsd/patches/linux/sh_h.diff b/system/ksh-openbsd/patches/linux/sh_h.diff
new file mode 100644
index 0000000000..df6de9c4c9
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/sh_h.diff
@@ -0,0 +1,10 @@
+--- ksh-openbsd_cvs/sh.h 2010-02-23 12:49:21.000000000 +0100
++++ ksh-openbsd_cvs.new/sh.h 2010-04-01 21:01:24.135232661 +0200
+@@ -16,6 +16,7 @@
+ #include <stdbool.h>
+ #include <stddef.h>
+ #include <stdlib.h>
++#define __USE_GNU /* needed for setresuid() and setresgid() */
+ #include <unistd.h>
+ #include <string.h>
+ #include <stdarg.h>
diff --git a/system/ksh-openbsd/patches/linux/strlcat_c.diff b/system/ksh-openbsd/patches/linux/strlcat_c.diff
new file mode 100644
index 0000000000..1137522fcb
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/strlcat_c.diff
@@ -0,0 +1,65 @@
+--- ksh-openbsd_cvs/strlcat.c 1970-01-01 01:00:00.000000000 +0100
++++ ksh-openbsd_cvs.new/strlcat.c 2010-04-01 21:01:24.135232661 +0200
+@@ -0,0 +1,62 @@
++/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
++
++/*
++ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
++ *
++ * Permission to use, copy, modify, and distribute this software for any
++ * purpose with or without fee is hereby granted, provided that the above
++ * copyright notice and this permission notice appear in all copies.
++ *
++ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
++ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
++ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
++ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
++ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
++ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
++ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
++ */
++
++/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
++
++// #include "includes.h"
++#ifndef HAVE_STRLCAT
++
++#include <sys/types.h>
++#include <string.h>
++
++/*
++ * Appends src to string dst of size siz (unlike strncat, siz is the
++ * full size of dst, not space left). At most siz-1 characters
++ * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
++ * Returns strlen(src) + MIN(siz, strlen(initial dst)).
++ * If retval >= siz, truncation occurred.
++ */
++size_t
++strlcat(char *dst, const char *src, size_t siz)
++{
++ char *d = dst;
++ const char *s = src;
++ size_t n = siz;
++ size_t dlen;
++
++ /* Find the end of dst and adjust bytes left but don't go past end */
++ while (n-- != 0 && *d != '\0')
++ d++;
++ dlen = d - dst;
++ n = siz - dlen;
++
++ if (n == 0)
++ return(dlen + strlen(s));
++ while (*s != '\0') {
++ if (n != 1) {
++ *d++ = *s;
++ n--;
++ }
++ s++;
++ }
++ *d = '\0';
++
++ return(dlen + (s - src)); /* count does not include NUL */
++}
++
++#endif /* !HAVE_STRLCAT */
diff --git a/system/ksh-openbsd/patches/linux/strlcat_h.diff b/system/ksh-openbsd/patches/linux/strlcat_h.diff
new file mode 100644
index 0000000000..60eb6361f7
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/strlcat_h.diff
@@ -0,0 +1,7 @@
+--- ksh-openbsd_cvs/strlcat.h 1970-01-01 01:00:00.000000000 +0100
++++ ksh-openbsd_cvs.new/strlcat.h 2010-04-01 21:01:24.135232661 +0200
+@@ -0,0 +1,4 @@
++#ifndef STRLCAT_H
++#define STRLCAT_H
++size_t strlcat(char *, const char *, size_t);
++#endif /* STRLCAT_H */
diff --git a/system/ksh-openbsd/patches/linux/strlcpy_c.diff b/system/ksh-openbsd/patches/linux/strlcpy_c.diff
new file mode 100644
index 0000000000..a08f2614bf
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/strlcpy_c.diff
@@ -0,0 +1,61 @@
+--- ksh-openbsd_cvs/strlcpy.c 1970-01-01 01:00:00.000000000 +0100
++++ ksh-openbsd_cvs.new/strlcpy.c 2010-04-01 21:01:24.145380963 +0200
+@@ -0,0 +1,58 @@
++/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
++
++/*
++ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
++ *
++ * Permission to use, copy, modify, and distribute this software for any
++ * purpose with or without fee is hereby granted, provided that the above
++ * copyright notice and this permission notice appear in all copies.
++ *
++ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
++ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
++ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
++ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
++ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
++ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
++ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
++ */
++
++/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
++
++// #include "includes.h"
++#ifndef HAVE_STRLCPY
++
++#include <sys/types.h>
++#include <string.h>
++
++/*
++ * Copy src to string dst of size siz. At most siz-1 characters
++ * will be copied. Always NUL terminates (unless siz == 0).
++ * Returns strlen(src); if retval >= siz, truncation occurred.
++ */
++size_t
++strlcpy(char *dst, const char *src, size_t siz)
++{
++ char *d = dst;
++ const char *s = src;
++ size_t n = siz;
++
++ /* Copy as many bytes as will fit */
++ if (n != 0 && --n != 0) {
++ do {
++ if ((*d++ = *s++) == 0)
++ break;
++ } while (--n != 0);
++ }
++
++ /* Not enough room in dst, add NUL and traverse rest of src */
++ if (n == 0) {
++ if (siz != 0)
++ *d = '\0'; /* NUL-terminate dst */
++ while (*s++)
++ ;
++ }
++
++ return(s - src - 1); /* count does not include NUL */
++}
++
++#endif /* !HAVE_STRLCPY */
diff --git a/system/ksh-openbsd/patches/linux/strlcpy_h.diff b/system/ksh-openbsd/patches/linux/strlcpy_h.diff
new file mode 100644
index 0000000000..d9f75cd41e
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/strlcpy_h.diff
@@ -0,0 +1,7 @@
+--- ksh-openbsd_cvs/strlcpy.h 1970-01-01 01:00:00.000000000 +0100
++++ ksh-openbsd_cvs.new/strlcpy.h 2010-04-01 21:01:24.145380963 +0200
+@@ -0,0 +1,4 @@
++#ifndef STRLCPY_H
++#define STRLCPY_H
++size_t strlcpy(char *, const char *, size_t);
++#endif /* STRLCPY_H */
diff --git a/system/ksh-openbsd/patches/linux/trap_c.diff b/system/ksh-openbsd/patches/linux/trap_c.diff
new file mode 100644
index 0000000000..10f067ff06
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/trap_c.diff
@@ -0,0 +1,45 @@
+--- ksh-openbsd_cvs/trap.c 2010-02-23 12:49:23.000000000 +0100
++++ ksh-openbsd_cvs.new/trap.c 2010-04-01 21:30:25.819238979 +0200
+@@ -5,6 +5,7 @@
+ */
+
+ #include "sh.h"
++#include "posix_signals.h" /* sys_signame[] */
+
+ Trap sigtraps[NSIG + 1];
+
+@@ -13,20 +14,29 @@ static struct sigaction Sigact_ign, Siga
+ void
+ inittraps(void)
+ {
+- int i;
++ int i, j;
+
+- /* Populate sigtraps based on sys_signame and sys_siglist. */
+- for (i = 0; i <= NSIG; i++) {
++ /* Populate sigtraps based on posix_signals.h */
++ for (i = 1; i < NSIG; i++) {
+ sigtraps[i].signal = i;
+ if (i == SIGERR_) {
+ sigtraps[i].name = "ERR";
+ sigtraps[i].mess = "Error handler";
+ } else {
+- sigtraps[i].name = sys_signame[i];
+- sigtraps[i].mess = sys_siglist[i];
++ for(j = 0; j < NSIG; j++) /* find the corresponding SIGNAL number's name in the sys_signame[] array */
++ {
++ if( sys_signame[j].val == sigtraps[i].signal ) /* we got a match */
++ {
++ sigtraps[i].name = sys_signame[j].name;
++ break;
++ }
++ }
++ sigtraps[i].mess = strsignal(sigtraps[i].signal);
+ }
+ }
++ sigtraps[SIGEXIT_].signal = SIGEXIT_;
+ sigtraps[SIGEXIT_].name = "EXIT"; /* our name for signal 0 */
++ sigtraps[SIGEXIT_].mess = "Exited";
+
+ sigemptyset(&Sigact_ign.sa_mask);
+ Sigact_ign.sa_flags = 0; /* interruptible */
diff --git a/system/ksh-openbsd/patches/linux/var_c.diff b/system/ksh-openbsd/patches/linux/var_c.diff
new file mode 100644
index 0000000000..9483a3b4ce
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/var_c.diff
@@ -0,0 +1,53 @@
+--- ksh-openbsd_cvs/var.c 2010-02-23 12:49:23.000000000 +0100
++++ ksh-openbsd_cvs.new/var.c 2010-04-01 21:01:24.145380963 +0200
+@@ -5,6 +5,7 @@
+ #include "ksh_limval.h"
+ #include <sys/stat.h>
+ #include <ctype.h>
++#include "strlcpy.h"
+
+ /*
+ * Variables
+@@ -865,12 +866,6 @@ makenv(void)
+ }
+
+ /*
+- * Someone has set the srand() value, therefore from now on
+- * we return values from rand() instead of arc4random()
+- */
+-int use_rand = 0;
+-
+-/*
+ * Called after a fork in parent to bump the random number generator.
+ * Done to ensure children will not get the same random number sequence
+ * if the parent doesn't use $RANDOM.
+@@ -878,8 +873,7 @@ int use_rand = 0;
+ void
+ change_random(void)
+ {
+- if (use_rand)
+- rand();
++ rand();
+ }
+
+ /*
+@@ -926,10 +920,7 @@ getspec(struct tbl *vp)
+ break;
+ case V_RANDOM:
+ vp->flag &= ~SPECIAL;
+- if (use_rand)
+- setint(vp, (long) (rand() & 0x7fff));
+- else
+- setint(vp, (long) (arc4random() & 0x7fff));
++ setint(vp, (long) (rand() & 0x7fff));
+ vp->flag |= SPECIAL;
+ break;
+ #ifdef HISTORY
+@@ -1030,7 +1021,6 @@ setspec(struct tbl *vp)
+ case V_RANDOM:
+ vp->flag &= ~SPECIAL;
+ srand((unsigned int)intval(vp));
+- use_rand = 1;
+ vp->flag |= SPECIAL;
+ break;
+ case V_SECONDS:
diff --git a/system/ksh-openbsd/patches/linux/vi_c.diff b/system/ksh-openbsd/patches/linux/vi_c.diff
new file mode 100644
index 0000000000..9372933d8e
--- /dev/null
+++ b/system/ksh-openbsd/patches/linux/vi_c.diff
@@ -0,0 +1,10 @@
+--- ksh-openbsd_cvs/vi.c 2010-02-23 12:49:24.000000000 +0100
++++ ksh-openbsd_cvs.new/vi.c 2010-04-01 21:01:24.146107949 +0200
+@@ -13,6 +13,7 @@
+ #include <ctype.h>
+ #include <sys/stat.h> /* completion */
+ #include "edit.h"
++#include "strlcpy.h"
+
+ #define CMDLEN 2048
+ #define Ctrl(c) (c&0x1f)
diff --git a/system/ksh-openbsd/slack-desc b/system/ksh-openbsd/slack-desc
new file mode 100644
index 0000000000..784fb59009
--- /dev/null
+++ b/system/ksh-openbsd/slack-desc
@@ -0,0 +1,18 @@
+# HOW TO EDIT THIS FILE:
+# The "handy ruler" below makes it easier to edit a package description. Line
+# up the first '|' above the ':' following the base package name, and the '|'
+# on the right side marks the last column you can put a character in. You must
+# make exactly 11 lines for the formatting to be correct. It's also
+# customary to leave one space after the ':'.
+
+ |-----handy-ruler------------------------------------------------------|
+ksh-openbsd: ksh-openbsd (OpenBSD ksh ported to Linux)
+ksh-openbsd:
+ksh-openbsd: This package is a patched and ported version of the OpenBSD ksh,
+ksh-openbsd: which is based on the original PD-ksh, but heavily modified and
+ksh-openbsd: maintained by OpenBSD developers.
+ksh-openbsd: "Ported" means ported to Linux. "Patched" means it includes patches
+ksh-openbsd: (improvements, bug fixes) separate from the OpenBSD tree.
+ksh-openbsd:
+ksh-openbsd:
+ksh-openbsd: