1/* $NetBSD: hist.c,v 1.24 2016/02/16 22:53:14 christos Exp $ */ 2 3/*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Christos Zoulas of Cornell University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35#include "config.h" 36#if !defined(lint) && !defined(SCCSID) 37#if 0 38static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93"; 39#else 40__RCSID("$NetBSD: hist.c,v 1.24 2016/02/16 22:53:14 christos Exp $"); 41#endif 42#endif /* not lint && not SCCSID */ 43#include <sys/cdefs.h> 44__FBSDID("$FreeBSD: stable/11/lib/libedit/hist.c 313981 2017-02-20 03:33:59Z pfg $"); 45 46/* 47 * hist.c: History access functions 48 */ 49#include <stdlib.h> 50#include <string.h> 51 52#include "el.h" 53 54/* hist_init(): 55 * Initialization function. 56 */ 57protected int 58hist_init(EditLine *el) 59{ 60 61 el->el_history.fun = NULL; 62 el->el_history.ref = NULL; 63 el->el_history.buf = el_malloc(EL_BUFSIZ * sizeof(*el->el_history.buf)); 64 el->el_history.sz = EL_BUFSIZ; 65 if (el->el_history.buf == NULL) 66 return -1; 67 el->el_history.last = el->el_history.buf; 68 return 0; 69} 70 71 72/* hist_end(): 73 * clean up history; 74 */ 75protected void 76hist_end(EditLine *el) 77{ 78 79 el_free(el->el_history.buf); 80 el->el_history.buf = NULL; 81} 82 83 84/* hist_set(): 85 * Set new history interface 86 */ 87protected int 88hist_set(EditLine *el, hist_fun_t fun, void *ptr) 89{ 90 91 el->el_history.ref = ptr; 92 el->el_history.fun = fun; 93 return 0; 94} 95 96 97/* hist_get(): 98 * Get a history line and update it in the buffer. 99 * eventno tells us the event to get. 100 */ 101protected el_action_t 102hist_get(EditLine *el) 103{ 104 const Char *hp; 105 int h; 106 107 if (el->el_history.eventno == 0) { /* if really the current line */ 108 (void) Strncpy(el->el_line.buffer, el->el_history.buf, 109 el->el_history.sz); 110 el->el_line.lastchar = el->el_line.buffer + 111 (el->el_history.last - el->el_history.buf); 112 113#ifdef KSHVI 114 if (el->el_map.type == MAP_VI) 115 el->el_line.cursor = el->el_line.buffer; 116 else 117#endif /* KSHVI */ 118 el->el_line.cursor = el->el_line.lastchar; 119 120 return CC_REFRESH; 121 } 122 if (el->el_history.ref == NULL) 123 return CC_ERROR; 124 125 hp = HIST_FIRST(el); 126 127 if (hp == NULL) 128 return CC_ERROR; 129 130 for (h = 1; h < el->el_history.eventno; h++) 131 if ((hp = HIST_NEXT(el)) == NULL) { 132 el->el_history.eventno = h; 133 return CC_ERROR; 134 } 135 (void) Strncpy(el->el_line.buffer, hp, 136 (size_t)(el->el_line.limit - el->el_line.buffer)); 137 el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] = '\0'; 138 el->el_line.lastchar = el->el_line.buffer + Strlen(el->el_line.buffer); 139 140 if (el->el_line.lastchar > el->el_line.buffer 141 && el->el_line.lastchar[-1] == '\n') 142 el->el_line.lastchar--; 143 if (el->el_line.lastchar > el->el_line.buffer 144 && el->el_line.lastchar[-1] == ' ') 145 el->el_line.lastchar--; 146#ifdef KSHVI 147 if (el->el_map.type == MAP_VI) 148 el->el_line.cursor = el->el_line.buffer; 149 else 150#endif /* KSHVI */ 151 el->el_line.cursor = el->el_line.lastchar; 152 153 return CC_REFRESH; 154} 155 156 157/* hist_command() 158 * process a history command 159 */ 160protected int 161hist_command(EditLine *el, int argc, const Char **argv) 162{ 163 const Char *str; 164 int num; 165 TYPE(HistEvent) ev; 166 167 if (el->el_history.ref == NULL) 168 return -1; 169 170 if (argc == 1 || Strcmp(argv[1], STR("list")) == 0) { 171 /* List history entries */ 172 173 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) 174 (void) fprintf(el->el_outfile, "%d %s", 175 el->el_history.ev.num, ct_encode_string(str, &el->el_scratch)); 176 return 0; 177 } 178 179 if (argc != 3) 180 return -1; 181 182 num = (int)Strtol(argv[2], NULL, 0); 183 184 if (Strcmp(argv[1], STR("size")) == 0) 185 return FUNW(history)(el->el_history.ref, &ev, H_SETSIZE, num); 186 187 if (Strcmp(argv[1], STR("unique")) == 0) 188 return FUNW(history)(el->el_history.ref, &ev, H_SETUNIQUE, num); 189 190 return -1; 191} 192 193/* hist_enlargebuf() 194 * Enlarge history buffer to specified value. Called from el_enlargebufs(). 195 * Return 0 for failure, 1 for success. 196 */ 197protected int 198/*ARGSUSED*/ 199hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz) 200{ 201 Char *newbuf; 202 203 newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf)); 204 if (!newbuf) 205 return 0; 206 207 (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf)); 208 209 el->el_history.last = newbuf + 210 (el->el_history.last - el->el_history.buf); 211 el->el_history.buf = newbuf; 212 el->el_history.sz = newsz; 213 214 return 1; 215} 216 217#ifdef WIDECHAR 218protected wchar_t * 219hist_convert(EditLine *el, int fn, void *arg) 220{ 221 HistEventW ev; 222 if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1) 223 return NULL; 224 return ct_decode_string((const char *)(const void *)ev.str, 225 &el->el_scratch); 226} 227#endif 228