Source-Changes-HG archive

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

[src/trunk]: src/lib/libedit From Ingo Schwarze:



details:   https://anonhg.NetBSD.org/src/rev/ab9867514c93
branches:  trunk
changeset: 344781:ab9867514c93
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Apr 19 19:50:53 2016 +0000

description:
>From Ingo Schwarze:
 - Put the data type el_rfunc_t into the public header <histedit.h>.
 - Make el_read in struct editline an opaque pointer rather
   than an embedded struct.
 - Do not include "read.h" everywhere, but only in the two files
   needing access to el_read, read.c and el.c.
 - To functions that don't need more, pass the struct el_read_t *
   rather than the full EditLine *.
 - Of course, that means that read_init() can now fail from
   memory exhaustion, but it's easy to clean up after that.

diffstat:

 lib/libedit/el.c       |  15 +++++++++------
 lib/libedit/el.h       |   7 ++++---
 lib/libedit/histedit.h |   4 +++-
 lib/libedit/read.c     |  29 ++++++++++++++++++-----------
 lib/libedit/read.h     |  12 +++---------
 5 files changed, 37 insertions(+), 30 deletions(-)

diffs (226 lines):

diff -r e8a9f48494fa -r ab9867514c93 lib/libedit/el.c
--- a/lib/libedit/el.c  Tue Apr 19 15:51:21 2016 +0000
+++ b/lib/libedit/el.c  Tue Apr 19 19:50:53 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: el.c,v 1.88 2016/04/11 18:56:31 christos Exp $ */
+/*     $NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $ */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)el.c       8.2 (Berkeley) 1/3/94";
 #else
-__RCSID("$NetBSD: el.c,v 1.88 2016/04/11 18:56:31 christos Exp $");
+__RCSID("$NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -55,6 +55,7 @@
 
 #include "el.h"
 #include "parse.h"
+#include "read.h"
 
 /* el_init():
  *     Initialize editline and set default parameters.
@@ -114,8 +115,10 @@
        (void) hist_init(el);
        (void) prompt_init(el);
        (void) sig_init(el);
-       (void) read_init(el);
-
+       if (read_init(el) == -1) {
+               el_end(el);
+               return NULL;
+       }
        return el;
 }
 
@@ -303,7 +306,7 @@
        case EL_GETCFN:
        {
                el_rfunc_t rc = va_arg(ap, el_rfunc_t);
-               rv = el_read_setfn(el, rc);
+               rv = el_read_setfn(el->el_read, rc);
                break;
        }
 
@@ -442,7 +445,7 @@
        }
 
        case EL_GETCFN:
-               *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
+               *va_arg(ap, el_rfunc_t *) = el_read_getfn(el->el_read);
                rv = 0;
                break;
 
diff -r e8a9f48494fa -r ab9867514c93 lib/libedit/el.h
--- a/lib/libedit/el.h  Tue Apr 19 15:51:21 2016 +0000
+++ b/lib/libedit/el.h  Tue Apr 19 19:50:53 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: el.h,v 1.37 2016/04/18 17:01:19 christos Exp $ */
+/*     $NetBSD: el.h,v 1.38 2016/04/19 19:50:53 christos Exp $ */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -102,7 +102,8 @@
 #include "hist.h"
 #include "map.h"
 #include "sig.h"
-#include "read.h"
+
+struct el_read_t;
 
 struct editline {
        wchar_t          *el_prog;      /* the program name             */
@@ -131,7 +132,7 @@
        el_history_t      el_history;   /* History stuff                */
        el_search_t       el_search;    /* Search stuff                 */
        el_signal_t       el_signal;    /* Signal handling stuff        */
-       el_read_t         el_read;      /* Character reading stuff      */
+       struct el_read_t *el_read;      /* Character reading stuff      */
        ct_buffer_t       el_scratch;   /* Scratch conversion buffer    */
        ct_buffer_t       el_lgcyconv;  /* Buffer for legacy wrappers   */
        LineInfo          el_lgcylinfo; /* Legacy LineInfo buffer       */
diff -r e8a9f48494fa -r ab9867514c93 lib/libedit/histedit.h
--- a/lib/libedit/histedit.h    Tue Apr 19 15:51:21 2016 +0000
+++ b/lib/libedit/histedit.h    Tue Apr 19 19:50:53 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: histedit.h,v 1.55 2016/02/17 19:47:49 christos Exp $   */
+/*     $NetBSD: histedit.h,v 1.56 2016/04/19 19:50:53 christos Exp $   */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -261,6 +261,8 @@
        const wchar_t   *lastchar;
 } LineInfoW;
 
+typedef int    (*el_rfunc_t)(EditLine *, wchar_t *);
+
 const wchar_t  *el_wgets(EditLine *, int *);
 int             el_wgetc(EditLine *, wchar_t *);
 void            el_wpush(EditLine *, const wchar_t *);
diff -r e8a9f48494fa -r ab9867514c93 lib/libedit/read.c
--- a/lib/libedit/read.c        Tue Apr 19 15:51:21 2016 +0000
+++ b/lib/libedit/read.c        Tue Apr 19 19:50:53 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: read.c,v 1.94 2016/04/18 17:01:19 christos Exp $       */
+/*     $NetBSD: read.c,v 1.95 2016/04/19 19:50:53 christos Exp $       */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)read.c     8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: read.c,v 1.94 2016/04/18 17:01:19 christos Exp $");
+__RCSID("$NetBSD: read.c,v 1.95 2016/04/19 19:50:53 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -55,6 +55,11 @@
 
 #include "el.h"
 #include "fcns.h"
+#include "read.h"
+
+struct el_read_t {
+       el_rfunc_t       read_char;     /* Function to read a character. */
+};
 
 static int     read__fixio(int, int);
 static int     read_char(EditLine *, wchar_t *);
@@ -67,8 +72,10 @@
 protected int
 read_init(EditLine *el)
 {
+       if ((el->el_read = el_malloc(sizeof(*el->el_read))) == NULL)
+               return -1;
        /* builtin read_char */
-       el->el_read.read_char = read_char;
+       el->el_read->read_char = read_char;
        return 0;
 }
 
@@ -78,9 +85,9 @@
  *     If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
  */
 protected int
-el_read_setfn(EditLine *el, el_rfunc_t rc)
+el_read_setfn(struct el_read_t *el_read, el_rfunc_t rc)
 {
-       el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
+       el_read->read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
        return 0;
 }
 
@@ -90,10 +97,10 @@
  *     if it is the default one
  */
 protected el_rfunc_t
-el_read_getfn(EditLine *el)
+el_read_getfn(struct el_read_t *el_read)
 {
-       return el->el_read.read_char == read_char ?
-           EL_BUILTIN_GETCFN : el->el_read.read_char;
+       return el_read->read_char == read_char ?
+           EL_BUILTIN_GETCFN : el_read->read_char;
 }
 
 
@@ -390,7 +397,7 @@
 #ifdef DEBUG_READ
        (void) fprintf(el->el_errfile, "Reading a character\n");
 #endif /* DEBUG_READ */
-       num_read = (*el->el_read.read_char)(el, cp);
+       num_read = (*el->el_read->read_char)(el, cp);
        if (num_read < 0)
                el->el_errno = errno;
 #ifdef DEBUG_READ
@@ -448,7 +455,7 @@
                size_t idx;
 
                cp = el->el_line.buffer;
-               while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
+               while ((num = (*el->el_read->read_char)(el, &wc)) == 1) {
                        *cp = wc;
                        /* make sure there is space for next character */
                        if (cp + 1 >= el->el_line.limit) {
@@ -501,7 +508,7 @@
 
                terminal__flush(el);
 
-               while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
+               while ((num = (*el->el_read->read_char)(el, &wc)) == 1) {
                        *cp = wc;
                        /* make sure there is space next character */
                        if (cp + 1 >= el->el_line.limit) {
diff -r e8a9f48494fa -r ab9867514c93 lib/libedit/read.h
--- a/lib/libedit/read.h        Tue Apr 19 15:51:21 2016 +0000
+++ b/lib/libedit/read.h        Tue Apr 19 19:50:53 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: read.h,v 1.9 2016/02/24 17:13:22 christos Exp $        */
+/*     $NetBSD: read.h,v 1.10 2016/04/19 19:50:53 christos Exp $       */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -35,16 +35,10 @@
 #ifndef        _h_el_read
 #define        _h_el_read
 
-typedef int (*el_rfunc_t)(EditLine *, wchar_t *);
-
-typedef struct el_read_t {
-       el_rfunc_t      read_char;      /* Function to read a character */
-} el_read_t;
-
 protected int          read_init(EditLine *);
 protected void         read_prepare(EditLine *);
 protected void         read_finish(EditLine *);
-protected int          el_read_setfn(EditLine *, el_rfunc_t);
-protected el_rfunc_t   el_read_getfn(EditLine *);
+protected int          el_read_setfn(struct el_read_t *, el_rfunc_t);
+protected el_rfunc_t   el_read_getfn(struct el_read_t *);
 
 #endif /* _h_el_read */



Home | Main Index | Thread Index | Old Index