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