Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libedit rename historyw -> history_w for consistency.



details:   https://anonhg.NetBSD.org/src/rev/9bfc1a9cfdec
branches:  trunk
changeset: 750509:9bfc1a9cfdec
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Jan 03 18:27:10 2010 +0000

description:
rename historyw -> history_w for consistency.
add wide tst code and make it the default.

diffstat:

 lib/libedit/TEST/Makefile |    4 +-
 lib/libedit/TEST/wtc1.c   |  269 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/libedit/chartype.h    |    4 +-
 lib/libedit/editline.3    |    6 +-
 lib/libedit/histedit.h    |   45 ++++---
 lib/libedit/history.c     |    6 +-
 lib/libedit/readline.c    |   28 ++--
 lib/libedit/tokenizer.c   |    6 +-
 8 files changed, 322 insertions(+), 46 deletions(-)

diffs (truncated from 597 to 300 lines):

diff -r 24f28fe5f55e -r 9bfc1a9cfdec lib/libedit/TEST/Makefile
--- a/lib/libedit/TEST/Makefile Sun Jan 03 17:58:14 2010 +0000
+++ b/lib/libedit/TEST/Makefile Sun Jan 03 18:27:10 2010 +0000
@@ -1,7 +1,7 @@
-# $NetBSD: Makefile,v 1.3 2006/08/31 20:20:38 rpaulo Exp $
+# $NetBSD: Makefile,v 1.4 2010/01/03 18:27:10 christos Exp $
 
 NOMAN=1
-PROG=tc1
+PROG=wtc1
 CPPFLAGS=-I${.CURDIR}/..
 LDADD+=-ledit -ltermcap
 DPADD+=${LIBEDIT} ${LIBTERMCAP}
diff -r 24f28fe5f55e -r 9bfc1a9cfdec lib/libedit/TEST/wtc1.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libedit/TEST/wtc1.c   Sun Jan 03 18:27:10 2010 +0000
@@ -0,0 +1,269 @@
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <limits.h>
+#include <locale.h>
+
+#include "../histedit.h"
+
+
+static int continuation;
+volatile sig_atomic_t gotsig;
+
+static wchar_t *
+prompt(EditLine *el)
+{
+       static wchar_t a[] = L"\1\e[7m\1Edit$\1\e[0m\1 ";
+       static wchar_t b[] = L"Edit> ";
+
+       return continuation ? b : a;
+}
+
+
+static void
+sig(int i)
+{
+       gotsig = i;
+}
+
+const char *
+my_wcstombs(const wchar_t *wstr)
+{
+       static struct {
+               char *str;
+               int len;
+       } buf;
+
+       int needed = wcstombs(0, wstr, 0) + 1;
+       if (needed > buf.len) {
+               buf.str = malloc(needed);
+               buf.len = needed;
+       }
+       wcstombs(buf.str, wstr, needed);
+       buf.str[needed - 1] = 0;
+
+       return buf.str;
+}
+
+
+static unsigned char
+complete(EditLine *el, int ch)
+{
+       DIR *dd = opendir(".");
+       struct dirent *dp;
+       const wchar_t *ptr;
+       char *buf, *bptr;
+       const LineInfoW *lf = el_wline(el);
+       int len, mblen, i;
+       unsigned char res;
+
+       /* Find the last word */
+       for (ptr = lf->cursor -1; !iswspace(*ptr) && ptr > lf->buffer; --ptr)
+               continue;
+       len = lf->cursor - ++ptr;
+
+       /* Convert last word to multibyte encoding, so we can compare to it */
+       wctomb(NULL, 0); /* Reset shift state */
+       mblen = MB_LEN_MAX * len + 1;
+       buf = bptr =(char *)malloc(mblen);
+       for (i = 0; i < len; ++i) {
+               /* Note: really should test for -1 return from wctomb */
+               bptr += wctomb(bptr, ptr[i]);
+       }
+       *bptr = 0; /* Terminate multibyte string */
+       mblen = bptr - buf;
+
+       /* Scan directory for matching name */
+       for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
+               if (mblen > strlen(dp->d_name))
+                       continue;
+               if (strncmp(dp->d_name, buf, mblen) == 0) {
+                       if (el_insertstr(el, &dp->d_name[mblen]) == -1)
+                               res = CC_ERROR;
+                       else
+                               res = CC_REFRESH;
+                       break;
+               }
+       }
+
+       closedir(dd);
+       free(buf);
+       return res;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+       EditLine *el = NULL;
+       int numc, ncontinuation;
+       const wchar_t *line;
+       TokenizerW *tok;
+       HistoryW *hist;
+       HistEventW ev;
+#ifdef DEBUG
+       int i;
+#endif
+
+       setlocale(LC_ALL, "");
+
+       (void)signal(SIGINT,  sig);
+       (void)signal(SIGQUIT, sig);
+       (void)signal(SIGHUP,  sig);
+       (void)signal(SIGTERM, sig);
+
+       hist = history_winit();         /* Init built-in history     */
+       history_w(hist, &ev, H_SETSIZE, 100);   /* Remember 100 events       */
+
+       tok = tok_winit(NULL);                  /* Init the tokenizer        */
+
+       el = el_init(argv[0], stdin, stdout, stderr);
+
+       el_wset(el, EL_EDITOR, L"vi");          /* Default editor is vi      */
+       el_wset(el, EL_SIGNAL, 1);              /* Handle signals gracefully */
+       el_wset(el, EL_PROMPT_ESC, prompt, '\1'); /* Set the prompt function */
+
+       el_wset(el, EL_HIST, history_w, hist);  /* FIXME - history_w? */
+
+                                       /* Add a user-defined function  */
+       el_wset(el, EL_ADDFN, L"ed-complete", L"Complete argument", complete);
+
+                                       /* Bind <tab> to it */
+       el_wset(el, EL_BIND, L"^I", L"ed-complete", NULL);
+
+       /*
+       * Bind j, k in vi command mode to previous and next line, instead
+       * of previous and next history.
+       */
+       el_wset(el, EL_BIND, L"-a", L"k", L"ed-prev-line", NULL);
+       el_wset(el, EL_BIND, L"-a", L"j", L"ed-next-line", NULL);
+
+       /* Source the user's defaults file. */
+       el_source(el, NULL);
+
+       while((line = el_wgets(el, &numc)) != NULL && numc != 0) {
+               int ac, cc, co, rc;
+               const wchar_t **av;
+
+               const LineInfoW *li;
+               li = el_wline(el);
+
+#ifdef DEBUG
+               (void)fwprintf(stderr, L"==> got %d %ls", numc, line);
+               (void)fwprintf(stderr, L"  > li `%.*ls_%.*ls'\n",
+                   (li->cursor - li->buffer), li->buffer,
+                   (li->lastchar - 1 - li->cursor),
+                   (li->cursor >= li->lastchar) ? L"" : li->cursor);
+#endif
+
+               if (gotsig) {
+                       (void)fprintf(stderr, "Got signal %d.\n", gotsig);
+                       gotsig = 0;
+                       el_reset(el);
+               }
+
+               if(!continuation && numc == 1)
+                       continue;       /* Only got a linefeed */
+
+               ac = cc = co = 0;
+               ncontinuation = tok_wline(tok, li, &ac, &av, &cc, &co);
+               if (ncontinuation < 0) {
+                       (void) fprintf(stderr, "Internal error\n");
+                       continuation = 0;
+                       continue;
+               }
+
+#ifdef DEBUG
+               (void)fprintf(stderr, "  > nc %d ac %d cc %d co %d\n",
+                       ncontinuation, ac, cc, co);
+#endif
+               history_w(hist, &ev, continuation ? H_APPEND : H_ENTER, line);
+
+               continuation = ncontinuation;
+               ncontinuation = 0;
+               if(continuation)
+                       continue;
+
+#ifdef DEBUG
+               for (i = 0; i < ac; ++i) {
+                       (void)fwprintf(stderr, L"  > arg# %2d ", i);
+                       if (i != cc)
+                               (void)fwprintf(stderr, L"`%ls'\n", av[i]);
+                       else
+                               (void)fwprintf(stderr, L"`%.*ls_%ls'\n",
+                                   co, av[i], av[i] + co);
+               }
+#endif
+
+               if (wcscmp (av[0], L"history") == 0) {
+                       switch(ac) {
+                       case 1:
+                               for(rc = history_w(hist, &ev, H_LAST);
+                                    rc != -1;
+                                    rc = history_w(hist, &ev, H_PREV))
+                                       (void)fwprintf(stdout, L"%4d %ls",
+                                            ev.num, ev.str);
+                               break;
+                       case 2:
+                               if (wcscmp(av[1], L"clear") == 0)
+                                       history_w(hist, &ev, H_CLEAR);
+                               else
+                                       goto badhist;
+                               break;
+                       case 3:
+                               if (wcscmp(av[1], L"load") == 0)
+                                       history_w(hist, &ev, H_LOAD,
+                                           my_wcstombs(av[2]));
+                               else if (wcscmp(av[1], L"save") == 0)
+                                       history_w(hist, &ev, H_SAVE,
+                                           my_wcstombs(av[2]));
+                               else
+                                       goto badhist;
+                               break;
+                       badhist:
+                       default:
+                               (void)fprintf(stderr,
+                                   "Bad history arguments\n");
+                               break;
+                       }
+               } else if (el_wparse(el, ac, av) == -1) {
+                       switch (fork()) {
+                       case 0: {
+                               Tokenizer *ntok = tok_init(NULL);
+                               int nargc;
+                               const char **nav;
+                               tok_str(ntok, my_wcstombs(line), &nargc, &nav);
+                               execvp(nav[0],(char **)nav);
+                               perror(nav[0]);
+                               _exit(1);
+                               /* NOTREACHED */
+                               break;
+                       }
+                       case -1:
+                               perror("fork");
+                               break;
+                       default:
+                               if (wait(&rc) == -1)
+                                       perror("wait");
+                               (void)fprintf(stderr, "Exit %x\n", rc);
+                               break;
+                       }
+               }
+
+               tok_wreset(tok);
+       }
+
+       el_end(el);
+       tok_wend(tok);
+       history_wend(hist);
+
+       fprintf(stdout, "\n");
+       return 0;
+}
+
+
diff -r 24f28fe5f55e -r 9bfc1a9cfdec lib/libedit/chartype.h
--- a/lib/libedit/chartype.h    Sun Jan 03 17:58:14 2010 +0000
+++ b/lib/libedit/chartype.h    Sun Jan 03 18:27:10 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chartype.h,v 1.3 2009/12/31 18:32:37 christos Exp $    */
+/*     $NetBSD: chartype.h,v 1.4 2010/01/03 18:27:10 christos Exp $    */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
 #define Char                   wchar_t
 #define Int                    wint_t
 #define FUN(prefix,rest)       prefix ## _w ## rest
-#define FUNW(type)             type ## w



Home | Main Index | Thread Index | Old Index