Module Name: src Committed By: christos Date: Mon Nov 7 15:30:19 UTC 2016
Modified Files: src/lib/libedit: hist.c Log Message: Change the way the built-in history works; some programs enter history with the trailing newline, others don't so don't make any assumptions about it when printing. Also print the correct event number (generated), separate the event number from the event with a tab, and visually encode the string (don't encode tabs and spaces though). To generate a diff of this commit: cvs rdiff -u -r1.29 -r1.30 src/lib/libedit/hist.c 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/hist.c diff -u src/lib/libedit/hist.c:1.29 src/lib/libedit/hist.c:1.30 --- src/lib/libedit/hist.c:1.29 Mon May 9 17:46:56 2016 +++ src/lib/libedit/hist.c Mon Nov 7 10:30:18 2016 @@ -1,4 +1,4 @@ -/* $NetBSD: hist.c,v 1.29 2016/05/09 21:46:56 christos Exp $ */ +/* $NetBSD: hist.c,v 1.30 2016/11/07 15:30:18 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: hist.c,v 1.29 2016/05/09 21:46:56 christos Exp $"); +__RCSID("$NetBSD: hist.c,v 1.30 2016/11/07 15:30:18 christos Exp $"); #endif #endif /* not lint && not SCCSID */ @@ -46,6 +46,7 @@ __RCSID("$NetBSD: hist.c,v 1.29 2016/05/ */ #include <stdlib.h> #include <string.h> +#include <vis.h> #include "el.h" @@ -166,11 +167,32 @@ hist_command(EditLine *el, int argc, con return -1; if (argc == 1 || wcscmp(argv[1], L"list") == 0) { + size_t maxlen = 0; + char *buf = NULL; + int hno = 1; /* List history entries */ - for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) - (void) fprintf(el->el_outfile, "%d %s", - el->el_history.ev.num, ct_encode_string(str, &el->el_scratch)); + for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) { + char *ptr = + ct_encode_string(str, &el->el_scratch); + size_t len = strlen(ptr); + if (len > 0 && ptr[len - 1] == '\n') + ptr[--len] = '\0'; + len = len * 4 + 1; + if (len >= maxlen) { + maxlen = len + 1024; + char *nbuf = el_realloc(buf, maxlen); + if (nbuf == NULL) { + el_free(buf); + return -1; + } + buf = nbuf; + } + strvis(buf, ptr, VIS_NL); + (void) fprintf(el->el_outfile, "%d\t%s\n", + hno++, buf); + } + el_free(buf); return 0; }