From 3050086473efb34eeb578b486bcd6bfabadeb3c1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 11 Sep 2015 06:04:59 -0700 Subject: linenoise: lino_copy function. * linenoise/linenoise.c (link_into_list, unlink_from_list): New static functions. (lino_make): Use link_into_list instead of open-coded list manipulation. (lino_copy): New function. (lino_free): Use unlink_from list instead of open-coded list manipulation. * linenoise/linenoise.h (lino_copy): Declared. --- linenoise/linenoise.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'linenoise/linenoise.c') diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index 32f427aa..1440a68a 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -981,6 +981,21 @@ char *linenoise(lino_t *ls, const char *prompt) } } +static void link_into_list(lino_t *list, lino_t *ls) +{ + ls->prev = list; + ls->next = list->next; + list->next->prev = ls; + list->next = ls; +} + +static void unlink_from_list(lino_t *ls) +{ + ls->prev->next = ls->next; + ls->next->prev = ls->prev; + ls->next = ls->prev = 0; +} + lino_t *lino_make(int ifd, int ofd) { lino_t *ls = (lino_t *) chk_malloc(sizeof *ls); @@ -991,15 +1006,29 @@ lino_t *lino_make(int ifd, int ofd) ls->ifd = ifd; ls->ofd = ofd; - ls->prev = &lino_list; - ls->next = lino_list.next; - lino_list.next->prev = ls; - lino_list.next = ls; + link_into_list(&lino_list, ls); + } + + return ls; +} + +lino_t *lino_copy(lino_t *le) +{ + lino_t *ls = (lino_t *) chk_malloc(sizeof *ls); + + if (ls != 0) { + *ls = *le; + ls->history_len = 0; + ls->history = 0; + ls->rawmode = 0; + + link_into_list(&lino_list, ls); } return ls; } + static void free_hist(lino_t *ls); static void lino_cleanup(lino_t *ls) @@ -1010,9 +1039,7 @@ static void lino_cleanup(lino_t *ls) void lino_free(lino_t *ls) { - ls->prev->next = ls->next; - ls->next->prev = ls->prev; - ls->next = ls->prev = 0; + unlink_from_list(ls); lino_cleanup(ls); free(ls); } -- cgit v1.2.3