Module Name: src Committed By: christos Date: Sun May 22 19:44:26 UTC 2016
Modified Files: src/lib/libedit: chared.c chared.h common.c el.c read.c read.h Log Message: 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 To generate a diff of this commit: cvs rdiff -u -r1.55 -r1.56 src/lib/libedit/chared.c cvs rdiff -u -r1.29 -r1.30 src/lib/libedit/chared.h cvs rdiff -u -r1.46 -r1.47 src/lib/libedit/common.c cvs rdiff -u -r1.91 -r1.92 src/lib/libedit/el.c cvs rdiff -u -r1.96 -r1.97 src/lib/libedit/read.c cvs rdiff -u -r1.11 -r1.12 src/lib/libedit/read.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libedit/chared.c diff -u src/lib/libedit/chared.c:1.55 src/lib/libedit/chared.c:1.56 --- src/lib/libedit/chared.c:1.55 Mon May 9 17:46:56 2016 +++ src/lib/libedit/chared.c Sun May 22 15:44:26 2016 @@ -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 @@ __RCSID("$NetBSD: chared.c,v 1.55 2016/0 #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 @@ cv__endword(wchar_t *p, wchar_t *high, i 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 @@ ch_init(EditLine *el) 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 @@ ch_init(EditLine *el) * 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 @@ ch_reset(EditLine *el, int mclear) 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 @@ ch_end(EditLine *el) 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); } Index: src/lib/libedit/chared.h diff -u src/lib/libedit/chared.h:1.29 src/lib/libedit/chared.h:1.30 --- src/lib/libedit/chared.h:1.29 Mon May 9 17:46:56 2016 +++ src/lib/libedit/chared.h Sun May 22 15:44:26 2016 @@ -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 @@ typedef struct el_chared_t { 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_gets(EditLine *, 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); Index: src/lib/libedit/common.c diff -u src/lib/libedit/common.c:1.46 src/lib/libedit/common.c:1.47 --- src/lib/libedit/common.c:1.46 Mon May 9 17:46:56 2016 +++ src/lib/libedit/common.c Sun May 22 15:44:26 2016 @@ -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 @@ libedit_private el_action_t ed_start_over(EditLine *el, wint_t c __attribute__((__unused__))) { - ch_reset(el, 0); + ch_reset(el); return CC_REFRESH; } Index: src/lib/libedit/el.c diff -u src/lib/libedit/el.c:1.91 src/lib/libedit/el.c:1.92 --- src/lib/libedit/el.c:1.91 Mon May 9 17:46:56 2016 +++ src/lib/libedit/el.c Sun May 22 15:44:26 2016 @@ -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 @@ el_end(EditLine *el) 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 @@ el_reset(EditLine *el) { tty_cookedmode(el); - ch_reset(el, 0); /* XXX: Do we want that? */ + ch_reset(el); /* XXX: Do we want that? */ } Index: src/lib/libedit/read.c diff -u src/lib/libedit/read.c:1.96 src/lib/libedit/read.c:1.97 --- src/lib/libedit/read.c:1.96 Mon May 9 17:46:56 2016 +++ src/lib/libedit/read.c Sun May 22 15:44:26 2016 @@ -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 @@ __RCSID("$NetBSD: read.c,v 1.96 2016/05/ #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 @@ static void read_pop(c_macro_t *); 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 @@ read__fixio(int fd __attribute__((__unus 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 @@ read_char(EditLine *el, wchar_t *cp) * 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 @@ read_pop(c_macro_t *ma) ma->offset = 0; } +static void +read_clearmacros(struct macros *ma) +{ + while (ma->level >= 0) + el_free(ma->macro[ma->level--]); + ma->offset = 0; +} + /* el_wgetc(): * Read a wide character */ int el_wgetc(EditLine *el, wchar_t *cp) { + struct macros *ma = &el->el_read->macros; int num_read; - c_macro_t *ma = &el->el_chared.c_macro; terminal__flush(el); for (;;) { @@ -420,7 +460,7 @@ read_prepare(EditLine *el) we have the wrong size. */ el_resize(el); re_clear_display(el); /* reset the display stuff */ - ch_reset(el, 0); + ch_reset(el); re_refresh(el); /* print the prompt */ if (el->el_flags & UNBUFFERED) @@ -481,7 +521,7 @@ el_wgets(EditLine *el, int *nread) #ifdef FIONREAD - if (el->el_tty.t_mode == EX_IO && el->el_chared.c_macro.level < 0) { + if (el->el_tty.t_mode == EX_IO && el->el_read->macros.level < 0) { int chrs = 0; (void) ioctl(el->el_infd, FIONREAD, &chrs); @@ -644,7 +684,8 @@ el_wgets(EditLine *el, int *nread) #endif /* DEBUG_READ */ /* put (real) cursor in a known place */ re_clear_display(el); /* reset the display stuff */ - ch_reset(el, 1); /* reset the input pointers */ + ch_reset(el); /* reset the input pointers */ + read_clearmacros(&el->el_read->macros); re_refresh(el); /* print the prompt again */ break; Index: src/lib/libedit/read.h diff -u src/lib/libedit/read.h:1.11 src/lib/libedit/read.h:1.12 --- src/lib/libedit/read.h:1.11 Mon May 9 17:46:56 2016 +++ src/lib/libedit/read.h Sun May 22 15:44:26 2016 @@ -1,4 +1,4 @@ -/* $NetBSD: read.h,v 1.11 2016/05/09 21:46:56 christos Exp $ */ +/* $NetBSD: read.h,v 1.12 2016/05/22 19:44:26 christos Exp $ */ /*- * Copyright (c) 2001 The NetBSD Foundation, Inc. @@ -36,6 +36,7 @@ #define _h_el_read libedit_private int read_init(EditLine *); +libedit_private void read_end(struct el_read_t *); libedit_private void read_prepare(EditLine *); libedit_private void read_finish(EditLine *); libedit_private int el_read_setfn(struct el_read_t *, el_rfunc_t);