Source-Changes-HG archive

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

[src/trunk]: src/lib/libedit Stop the read module from poking the el_chared.c...



details:   https://anonhg.NetBSD.org/src/rev/1a4ace19f70a
branches:  trunk
changeset: 345372:1a4ace19f70a
user:      christos <christos%NetBSD.org@localhost>
date:      Sun May 22 19:44:26 2016 +0000

description:
Stop the read module from poking the el_chared.c_macro data structure
currently belonging to the chared module.  The read module does so
from three of its functions, while no other module uses the macro
data, not even the chared module itself.  That's quite logical
because macros are a feature of input handling, all of which is
done by the read module, and none by the chared module.  So move
the data into the read modules's own opaque data structure, struct
el_read_t.

That simplifies internal interfaces in several respects: The
semi-public chared.h has one fewer struct, one fewer #define, and
one fewer member in struct el_chared_t; all three move to one single
C file, read.c, and are now module-local.  And the internal interface
function ch_reset() needs one fewer argument, making the code of many
functions in various modules more readable.

The price is one additional internal interface function, read_end(),
10 lines long including comments, called publicly from exactly one
place: el_end() in el.c.  That's hardly an increase in complexity
since most other modules already have their *_end() function, read.c
was the odd one out not having one.

>From Ingo Schwarze

diffstat:

 lib/libedit/chared.c |  30 +++----------------------
 lib/libedit/chared.h |  14 +----------
 lib/libedit/common.c |   6 ++--
 lib/libedit/el.c     |   7 +++--
 lib/libedit/read.c   |  59 ++++++++++++++++++++++++++++++++++++++++++++-------
 lib/libedit/read.h   |   3 +-
 6 files changed, 65 insertions(+), 54 deletions(-)

diffs (truncated from 364 to 300 lines):

diff -r 360ae030aea2 -r 1a4ace19f70a lib/libedit/chared.c
--- a/lib/libedit/chared.c      Sun May 22 19:28:39 2016 +0000
+++ b/lib/libedit/chared.c      Sun May 22 19:44:26 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chared.c,v 1.55 2016/05/09 21:46:56 christos Exp $     */
+/*     $NetBSD: chared.c,v 1.56 2016/05/22 19:44:26 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)chared.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: chared.c,v 1.55 2016/05/09 21:46:56 christos Exp $");
+__RCSID("$NetBSD: chared.c,v 1.56 2016/05/22 19:44:26 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -52,8 +52,6 @@
 #include "common.h"
 #include "fcns.h"
 
-static void ch__clearmacro (EditLine *);
-
 /* value to leave unused in line buffer */
 #define        EL_LEAVE        2
 
@@ -398,8 +396,6 @@
 libedit_private int
 ch_init(EditLine *el)
 {
-       c_macro_t *ma = &el->el_chared.c_macro;
-
        el->el_line.buffer              = el_malloc(EL_BUFSIZ *
            sizeof(*el->el_line.buffer));
        if (el->el_line.buffer == NULL)
@@ -451,11 +447,6 @@
        el->el_state.argument           = 1;
        el->el_state.lastcmd            = ED_UNASSIGNED;
 
-       ma->level       = -1;
-       ma->offset      = 0;
-       ma->macro       = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
-       if (ma->macro == NULL)
-               return -1;
        return 0;
 }
 
@@ -463,7 +454,7 @@
  *     Reset the character editor
  */
 libedit_private void
-ch_reset(EditLine *el, int mclear)
+ch_reset(EditLine *el)
 {
        el->el_line.cursor              = el->el_line.buffer;
        el->el_line.lastchar            = el->el_line.buffer;
@@ -485,17 +476,6 @@
        el->el_state.lastcmd            = ED_UNASSIGNED;
 
        el->el_history.eventno          = 0;
-
-       if (mclear)
-               ch__clearmacro(el);
-}
-
-static void
-ch__clearmacro(EditLine *el)
-{
-       c_macro_t *ma = &el->el_chared.c_macro;
-       while (ma->level >= 0)
-               el_free(ma->macro[ma->level--]);
 }
 
 /* ch_enlargebufs():
@@ -606,9 +586,7 @@
        el->el_chared.c_redo.cmd = ED_UNASSIGNED;
        el_free(el->el_chared.c_kill.buf);
        el->el_chared.c_kill.buf = NULL;
-       ch_reset(el, 1);
-       el_free(el->el_chared.c_macro.macro);
-       el->el_chared.c_macro.macro = NULL;
+       ch_reset(el);
 }
 
 
diff -r 360ae030aea2 -r 1a4ace19f70a lib/libedit/chared.h
--- a/lib/libedit/chared.h      Sun May 22 19:28:39 2016 +0000
+++ b/lib/libedit/chared.h      Sun May 22 19:44:26 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chared.h,v 1.29 2016/05/09 21:46:56 christos Exp $     */
+/*     $NetBSD: chared.h,v 1.30 2016/05/22 19:44:26 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -40,8 +40,6 @@
 #ifndef _h_el_chared
 #define        _h_el_chared
 
-#define        EL_MAXMACRO     10
-
 /*
  * This is an issue of basic "vi" look-and-feel. Defining VI_MOVE works
  * like real vi: i.e. the transition from command<->insert modes moves
@@ -54,13 +52,6 @@
  */
 #define        VI_MOVE
 
-
-typedef struct c_macro_t {
-       int       level;
-       int       offset;
-       wchar_t **macro;
-} c_macro_t;
-
 /*
  * Undo information for vi - no undo in emacs (yet)
  */
@@ -110,7 +101,6 @@
        c_kill_t        c_kill;
        c_redo_t        c_redo;
        c_vcmd_t        c_vcmd;
-       c_macro_t       c_macro;
        el_zfunc_t      c_resizefun;
        el_afunc_t      c_aliasfun;
        void *          c_resizearg;
@@ -156,7 +146,7 @@
 libedit_private int     c_hpos(EditLine *);
 
 libedit_private int     ch_init(EditLine *);
-libedit_private void    ch_reset(EditLine *, int);
+libedit_private void    ch_reset(EditLine *);
 libedit_private int     ch_resizefun(EditLine *, el_zfunc_t, void *);
 libedit_private int     ch_aliasfun(EditLine *, el_afunc_t, void *);
 libedit_private int     ch_enlargebufs(EditLine *, size_t);
diff -r 360ae030aea2 -r 1a4ace19f70a lib/libedit/common.c
--- a/lib/libedit/common.c      Sun May 22 19:28:39 2016 +0000
+++ b/lib/libedit/common.c      Sun May 22 19:44:26 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: common.c,v 1.46 2016/05/09 21:46:56 christos Exp $     */
+/*     $NetBSD: common.c,v 1.47 2016/05/22 19:44:26 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)common.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: common.c,v 1.46 2016/05/09 21:46:56 christos Exp $");
+__RCSID("$NetBSD: common.c,v 1.47 2016/05/22 19:44:26 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -530,7 +530,7 @@
 ed_start_over(EditLine *el, wint_t c __attribute__((__unused__)))
 {
 
-       ch_reset(el, 0);
+       ch_reset(el);
        return CC_REFRESH;
 }
 
diff -r 360ae030aea2 -r 1a4ace19f70a lib/libedit/el.c
--- a/lib/libedit/el.c  Sun May 22 19:28:39 2016 +0000
+++ b/lib/libedit/el.c  Sun May 22 19:44:26 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: el.c,v 1.91 2016/05/09 21:46:56 christos Exp $ */
+/*     $NetBSD: el.c,v 1.92 2016/05/22 19:44:26 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.91 2016/05/09 21:46:56 christos Exp $");
+__RCSID("$NetBSD: el.c,v 1.92 2016/05/22 19:44:26 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -141,6 +141,7 @@
        if (!(el->el_flags & NO_TTY))
                tty_end(el);
        ch_end(el);
+       read_end(el->el_read);
        search_end(el);
        hist_end(el);
        prompt_end(el);
@@ -165,7 +166,7 @@
 {
 
        tty_cookedmode(el);
-       ch_reset(el, 0);                /* XXX: Do we want that? */
+       ch_reset(el);           /* XXX: Do we want that? */
 }
 
 
diff -r 360ae030aea2 -r 1a4ace19f70a lib/libedit/read.c
--- a/lib/libedit/read.c        Sun May 22 19:28:39 2016 +0000
+++ b/lib/libedit/read.c        Sun May 22 19:44:26 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: read.c,v 1.96 2016/05/09 21:46:56 christos Exp $       */
+/*     $NetBSD: read.c,v 1.97 2016/05/22 19:44:26 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.96 2016/05/09 21:46:56 christos Exp $");
+__RCSID("$NetBSD: read.c,v 1.97 2016/05/22 19:44:26 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -57,14 +57,24 @@
 #include "fcns.h"
 #include "read.h"
 
+#define        EL_MAXMACRO     10
+
+struct macros {
+       wchar_t **macro;
+       int       level;
+       int       offset;
+};
+
 struct el_read_t {
+       struct macros    macros;
        el_rfunc_t       read_char;     /* Function to read a character. */
 };
 
 static int     read__fixio(int, int);
 static int     read_char(EditLine *, wchar_t *);
 static int     read_getcmd(EditLine *, el_action_t *, wchar_t *);
-static void    read_pop(c_macro_t *);
+static void    read_clearmacros(struct macros *);
+static void    read_pop(struct macros *);
 
 /* read_init():
  *     Initialize the read stuff
@@ -72,13 +82,35 @@
 libedit_private int
 read_init(EditLine *el)
 {
+       struct macros *ma;
+
        if ((el->el_read = el_malloc(sizeof(*el->el_read))) == NULL)
                return -1;
+
+       ma = &el->el_read->macros;
+       if ((ma->macro = el_malloc(EL_MAXMACRO *
+           sizeof(*ma->macro))) == NULL) {
+               free(el->el_read);
+               return -1;
+       }
+       ma->level = -1;
+       ma->offset = 0;
+
        /* builtin read_char */
        el->el_read->read_char = read_char;
        return 0;
 }
 
+/* el_read_end():
+ *     Free the data structures used by the read stuff.
+ */
+libedit_private void
+read_end(struct el_read_t *el_read)
+{
+       read_clearmacros(&el_read->macros);
+       el_free(el_read->macros.macro);
+       el_read->macros.macro = NULL;
+}
 
 /* el_read_setfn():
  *     Set the read char function to the one provided.
@@ -191,7 +223,7 @@
 void
 el_wpush(EditLine *el, const wchar_t *str)
 {
-       c_macro_t *ma = &el->el_chared.c_macro;
+       struct macros *ma = &el->el_read->macros;
 
        if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
                ma->level++;
@@ -348,7 +380,7 @@
  *     Pop a macro from the stack
  */
 static void
-read_pop(c_macro_t *ma)
+read_pop(struct macros *ma)
 {
        int i;
 
@@ -359,14 +391,22 @@
        ma->offset = 0;
 }
 
+static void



Home | Main Index | Thread Index | Old Index